/** * Skeleton Superset * @constructor * @param graphics GameLib.D3.Graphics * @param apiSkeleton GameLib.D3.API.Skeleton */ GameLib.D3.Skeleton = function Skeleton( graphics, apiSkeleton ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiSkeleton)) { apiSkeleton = {}; } if (apiSkeleton instanceof GameLib.D3.Skeleton) { return apiSkeleton; } GameLib.D3.API.Skeleton.call( this, apiSkeleton.id, apiSkeleton.name, apiSkeleton.bones, apiSkeleton.boneInverses, apiSkeleton.useVertexTexture, apiSkeleton.boneTextureWidth, apiSkeleton.boneTextureHeight, apiSkeleton.boneMatrices, apiSkeleton.boneTexture, apiSkeleton.parentEntity ); this.bones = this.bones.map( function(apiBone) { if (apiBone instanceof GameLib.D3.API.Bone) { return new GameLib.D3.Bone( this.graphics, apiBone ) } else { console.warn('apiBone not an instance of API.Bone'); throw new Error('apiBone not an instance of API.Bone'); } }.bind(this) ); this.boneInverses = this.boneInverses.map( function(boneInverse) { if (boneInverse instanceof GameLib.API.Matrix4) { return new GameLib.Matrix4( this.graphics, boneInverse, this ); } else { console.warn('boneInverse not an instance of API.Matrix4'); throw new Error('boneInverse not an instance of API.Matrix4'); } }.bind(this) ); this.boneMatrices = this.boneMatrices.map( function(boneMatrices) { if (boneMatrices instanceof GameLib.API.Matrix4) { return new GameLib.Matrix4( this.graphics, boneMatrices, this ); } else { console.warn('boneMatrices not an instance of API.Matrix4'); throw new Error('boneMatrices not an instance of API.Matrix4'); } }.bind(this) ); this.buildIdToObject(); this.instance = this.createInstance(); }; GameLib.D3.Skeleton.prototype = Object.create(GameLib.D3.API.Skeleton.prototype); GameLib.D3.Skeleton.prototype.constructor = GameLib.D3.Skeleton; /** * Creates an instance skeleton * @param update boolean */ GameLib.D3.Skeleton.prototype.createInstance = function(update) { var instance = null; if (update) { //TODO - update instance with bone info instance = this.instance; } else { instance = new THREE.Skeleton( this.bones.map ( function (bone) { return bone.instance; } ) ); } instance.useVertexTexture = this.useVertexTexture; var parentBoneInstance = this.bones.reduce( function (result, bone) { if (result) { return result; } if (bone.parentBoneIds.length == 0) { return bone.instance; } return null; }, null ); if (!parentBoneInstance) { console.warn('Did not find the main parent bone - skeleton will be broken'); return; } this.rootBoneInstance = parentBoneInstance; this.boneIdToBone = {}; this.bones.map( function (bone) { this.boneIdToBone[bone.id] = bone; }.bind(this) ); this.bones.map( function (__parentBoneInstance) { return function(bone) { bone.childBoneIds.map( function (childBoneId) { __parentBoneInstance.add(this.boneIdToBone[childBoneId].instance); }.bind(this) ); }; }(parentBoneInstance).bind(this) ); instance.update(); instance.calculateInverses(); return instance; }; /** * Updates the instance */ GameLib.D3.Skeleton.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; /** * Converts a GameLib.D3.Skeleton to GameLib.D3.API.Skeleton * @returns {GameLib.D3.API.Skeleton} */ GameLib.D3.Skeleton.prototype.toApiObject = function() { var apiSkeleton = new GameLib.D3.API.Skeleton( this.id, this.name, this.bones.map( function (bone) { return bone.toApiObject(); } ), this.boneInverses.map( function (boneInverse) { return boneInverse.toApiObject(); } ), this.useVertexTexture, this.boneTextureWidth, this.boneTextureHeight, this.boneMatrices.map( function (boneMatrix) { return boneMatrix.toApiObject(); } ), this.boneTexture, this.parentEntity ); return apiSkeleton; }; /** * Returns a GameLib.D3.Skeleton from a skeleton Object * @param graphics GameLib.D3.Graphics * @param objectSkeleton Object * @returns {GameLib.D3.Skeleton} * @constructor */ GameLib.D3.Skeleton.FromObjectSkeleton = function( graphics, objectSkeleton ) { if (!objectSkeleton) { return null; } var apiSkeleton = GameLib.D3.API.Skeleton.FromObjectSkeleton(objectSkeleton); var skeleton = new GameLib.D3.Skeleton( graphics, apiSkeleton ); return skeleton; };