From deca83d5b81875460a17e3c09e08ed288bea5279 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Fri, 23 Dec 2016 16:07:10 +0100 Subject: [PATCH] stable..? --- src/game-lib-d3-api-bone-weight.js | 2 +- src/game-lib-d3-api-bone.js | 60 +++--- src/game-lib-d3-api-skeleton.js | 79 ++++++++ src/game-lib-d3-api-spline.js | 2 +- src/game-lib-d3-bone-weight.js | 89 +++++++++ src/game-lib-d3-bone.js | 171 +++++++++++++++++ src/game-lib-d3-helper.js | 15 +- src/game-lib-d3-mesh.js | 84 ++------ src/game-lib-d3-scene.js | 15 +- src/game-lib-d3-skeleton.js | 296 +++++++++++++++++++++++------ src/game-lib-d3-spline.js | 20 +- src/game-lib-d3-vertex.js | 10 +- 12 files changed, 668 insertions(+), 175 deletions(-) create mode 100644 src/game-lib-d3-api-skeleton.js create mode 100644 src/game-lib-d3-bone-weight.js create mode 100644 src/game-lib-d3-bone.js diff --git a/src/game-lib-d3-api-bone-weight.js b/src/game-lib-d3-api-bone-weight.js index 18bb6a1..3d262f8 100644 --- a/src/game-lib-d3-api-bone-weight.js +++ b/src/game-lib-d3-api-bone-weight.js @@ -4,7 +4,7 @@ * @param weight float * @constructor */ -GameLib.D3.API.BoneWeight = function BoneWeight( +GameLib.D3.API.BoneWeight = function ( boneIndex, weight ) { diff --git a/src/game-lib-d3-api-bone.js b/src/game-lib-d3-api-bone.js index 465a38a..94945a7 100644 --- a/src/game-lib-d3-api-bone.js +++ b/src/game-lib-d3-api-bone.js @@ -2,63 +2,61 @@ * Bone Superset * @param id * @param name string - * @param boneId * @param childBoneIds - * @param parentBoneId - * @param quaternion - * @param position - * @param rotation + * @param parentBoneIds + * @param quaternion GameLib.API.Quaternion + * @param position GameLib.API.Vector3 * @param scale GameLib.API.Vector3 - * @param up + * @param up GameLib.API.Vector3 * @constructor */ -GameLib.D3.API.Bone = function Bone( +GameLib.D3.API.Bone = function ( id, - boneId, name, childBoneIds, - parentBoneId, - quaternion, + parentBoneIds, position, - rotation, + quaternion, scale, up ) { - this.id = id; - this.name = name; - this.boneId = boneId; - if (typeof childBoneIds == 'undefined') { + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Bone (' + this.id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(childBoneIds)) { childBoneIds = []; } this.childBoneIds = childBoneIds; - if (typeof parentBoneId == 'undefined') { - parentBoneId = null; + if (GameLib.Utils.UndefinedOrNull(parentBoneIds)) { + parentBoneIds = []; } - this.parentBoneId = parentBoneId; + this.parentBoneIds = parentBoneIds; - if (typeof quaternion == 'undefined') { + if (GameLib.Utils.UndefinedOrNull(position)) { + position = new GameLib.API.Vector3(); + } + this.position = position; + + if (GameLib.Utils.UndefinedOrNull(quaternion)) { quaternion = new GameLib.API.Quaternion(); } this.quaternion = quaternion; - if (typeof position == 'undefined') { - position = new GameLib.API.Vector3(0,0,0); - } - this.position = position; - - if (typeof rotation == 'undefined') { - rotation = new GameLib.API.Vector3(0,0,0); - } - this.rotation = rotation; - - if (typeof scale == 'undefined') { + if (GameLib.Utils.UndefinedOrNull(scale)) { scale = new GameLib.API.Vector3(1,1,1); } this.scale = scale; - if (typeof up == 'undefined') { + if (GameLib.Utils.UndefinedOrNull(up)) { up = new GameLib.API.Vector3(0,1,0); } this.up = up; diff --git a/src/game-lib-d3-api-skeleton.js b/src/game-lib-d3-api-skeleton.js new file mode 100644 index 0000000..4a09666 --- /dev/null +++ b/src/game-lib-d3-api-skeleton.js @@ -0,0 +1,79 @@ +/** + * API Skeleton + * @param id + * @param name + * @param bones GameLib.D3.API.Bone[] + * @param boneInverses GameLib.API.Matrix4[] + * @param useVertexTexture boolean + * @param boneTextureWidth Number + * @param boneTextureHeight Number + * @param boneMatrices GameLib.API.Matrix4[] + * @param boneTexture null (not implemented) + * @constructor + */ +GameLib.D3.API.Skeleton = function ( + id, + name, + bones, + boneInverses, + useVertexTexture, + boneTextureWidth, + boneTextureHeight, + boneMatrices, + boneTexture +) { + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Skeleton'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(bones)) { + bones = []; + } + this.bones = bones; + + /** + * An array of Matrix4s that represent the inverse of the matrixWorld of the individual bones. + */ + if (GameLib.Utils.UndefinedOrNull(boneInverses)) { + boneInverses = []; + } + this.boneInverses = boneInverses; + + /** + * Use a vertex texture in the shader - allows for more than 4 bones per vertex, not supported by all devices + */ + if (GameLib.Utils.UndefinedOrNull(useVertexTexture)) { + useVertexTexture = false; + } + this.useVertexTexture = useVertexTexture; + if (useVertexTexture) { + console.warn('support for vertex texture bones is not supported yet - something could break somewhere'); + } + + if (GameLib.Utils.UndefinedOrNull(boneTextureWidth)) { + boneTextureWidth = 0; + } + this.boneTextureWidth = boneTextureWidth; + + if (GameLib.Utils.UndefinedOrNull(boneTextureHeight)) { + boneTextureHeight = 0; + } + this.boneTextureHeight = boneTextureHeight; + + if (GameLib.Utils.UndefinedOrNull(boneMatrices)) { + boneMatrices = []; + } + this.boneMatrices = boneMatrices; + + if (GameLib.Utils.UndefinedOrNull(boneTexture)) { + boneTexture = null; + } + this.boneTexture = boneTexture; +}; + diff --git a/src/game-lib-d3-api-spline.js b/src/game-lib-d3-api-spline.js index a1fa0c5..ad267be 100644 --- a/src/game-lib-d3-api-spline.js +++ b/src/game-lib-d3-api-spline.js @@ -2,7 +2,7 @@ * API Spline * @param id String * @param name String - * @param vertices GameLib.API.Vertex[] + * @param vertices GameLib.API.Vector3[] * @constructor */ GameLib.D3.API.Spline = function( diff --git a/src/game-lib-d3-bone-weight.js b/src/game-lib-d3-bone-weight.js new file mode 100644 index 0000000..dad6f93 --- /dev/null +++ b/src/game-lib-d3-bone-weight.js @@ -0,0 +1,89 @@ +/** + * BoneWeight Superset + * @constructor + * @param graphics GameLib.D3.Graphics + * @param apiBoneWeight GameLib.D3.API.BoneWeight + */ +GameLib.D3.BoneWeight = function RuntimeBoneWeight( + graphics, + apiBoneWeight +) { + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.BoneWeight.call( + this, + apiBoneWeight.boneIndex, + apiBoneWeight.weight + ); + + this.instance = this.createInstance(); +}; + +GameLib.D3.BoneWeight.prototype = Object.create(GameLib.D3.API.BoneWeight.prototype); +GameLib.D3.BoneWeight.prototype.constructor = GameLib.D3.BoneWeight; + + +/** + * Creates an instance boneWeight + * @param update boolean + */ +GameLib.D3.BoneWeight.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + //TODO - update instance with boneWeight info + instance = this.instance; + } else { + //TODO - fix this + instance = new THREE.Quaternion(); + } + + return instance; +}; + +/** + * Updates the instance + */ +GameLib.D3.BoneWeight.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * Converts a GameLib.D3.BoneWeight to GameLib.D3.API.BoneWeight + * @returns {GameLib.D3.API.BoneWeight} + */ +GameLib.D3.BoneWeight.prototype.toApiBoneWeight = function() { + + var apiBoneWeight = new GameLib.D3.API.BoneWeight( + this.boneIndex, + this.weight + ); + + return apiBoneWeight; +}; + +/** + * Returns a GameLib.D3.BoneWeight from a boneWeight Object + * @param graphics GameLib.D3.Graphics + * @param objectBoneWeight Object + * @returns {GameLib.D3.BoneWeight} + * @constructor + */ +GameLib.D3.BoneWeight.FromObjectBoneWeight = function( + graphics, + objectBoneWeight +) { + var apiBoneWeight = new GameLib.D3.API.BoneWeight( + objectBoneWeight.boneIndex, + objectBoneWeight.weight + ); + + var boneWeight = new GameLib.D3.BoneWeight( + graphics, + apiBoneWeight + ); + + return boneWeight; +}; \ No newline at end of file diff --git a/src/game-lib-d3-bone.js b/src/game-lib-d3-bone.js new file mode 100644 index 0000000..d049e84 --- /dev/null +++ b/src/game-lib-d3-bone.js @@ -0,0 +1,171 @@ +/** + * Bone Superset + * @constructor + * @param graphics GameLib.D3.Graphics + * @param apiBone GameLib.D3.API.Bone + */ +GameLib.D3.Bone = function RuntimeBone( + graphics, + apiBone +) { + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Bone.call( + this, + apiBone.id, + apiBone.name, + apiBone.childBoneIds, + apiBone.parentBoneIds, + apiBone.position, + apiBone.quaternion, + apiBone.scale, + apiBone.up + ); + + this.position = new GameLib.Vector3( + graphics, + this, + this.position + ); + + this.quaternion = new GameLib.Quaternion( + graphics, + this, + this.quaternion + ); + + this.scale = new GameLib.Vector3( + graphics, + this, + this.scale + ); + + this.up = new GameLib.Vector3( + graphics, + this, + this.up + ); + + this.instance = this.createInstance(); +}; + +GameLib.D3.Bone.prototype = Object.create(GameLib.D3.API.Bone.prototype); +GameLib.D3.Bone.prototype.constructor = GameLib.D3.Bone; + + +/** + * Creates an instance bone + * @param update boolean + */ +GameLib.D3.Bone.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + } else { + instance = new THREE.Bone(); + } + + instance.name = this.name; + + instance.position.x = this.position.x; + instance.position.y = this.position.y; + instance.position.z = this.position.z; + + instance.quaternion.x = this.quaternion.x; + instance.quaternion.y = this.quaternion.y; + instance.quaternion.z = this.quaternion.z; + instance.quaternion.w = this.quaternion.w; + + instance.scale.x = this.scale.x; + instance.scale.y = this.scale.y; + instance.scale.z = this.scale.z; + + instance.up.x = this.up.x; + instance.up.y = this.up.y; + instance.up.z = this.up.z; + + return instance; +}; + +/** + * Updates the instance + */ +GameLib.D3.Bone.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * Converts a GameLib.D3.Bone to GameLib.D3.API.Bone + * @returns {GameLib.D3.API.Bone} + */ +GameLib.D3.Bone.prototype.toApiBone = function() { + + var apiBone = new GameLib.D3.API.Bone( + this.id, + this.name, + this.childBoneIds, + this.parentBoneIds, + this.position.toApiVector(), + this.quaternion.toApiQuaternion(), + this.scale.toApiVector(), + this.up.toApiVector() + ); + + return apiBone; +}; + +/** + * Returns a GameLib.D3.Bone from a bone Object + * @param graphics GameLib.D3.Graphics + * @param objectBone Object + * @returns GameLib.D3.Bone + * @constructor + */ +GameLib.D3.Bone.FromObjectBone = function( + graphics, + objectBone +) { + var apiBone = new GameLib.D3.API.Bone( + objectBone.id, + objectBone.name, + objectBone.childBoneIds, + objectBone.parentBoneIds, + new GameLib.API.Vector3( + objectBone.position.x, + objectBone.position.y, + objectBone.position.z + ), + new GameLib.API.Quaternion( + objectBone.quaternion.x, + objectBone.quaternion.y, + objectBone.quaternion.z, + objectBone.quaternion.w, + new GameLib.API.Vector3( + objectBone.quaternion.axis.x, + objectBone.quaternion.axis.y, + objectBone.quaternion.axis.z + ), + objectBone.quaternion.angle + ), + new GameLib.API.Vector3( + objectBone.scale.x, + objectBone.scale.y, + objectBone.scale.z + ), + new GameLib.API.Vector3( + objectBone.up.x, + objectBone.up.y, + objectBone.up.z + ) + ); + + var bone = new GameLib.D3.Bone( + graphics, + apiBone + ); + + return bone; +}; \ No newline at end of file diff --git a/src/game-lib-d3-helper.js b/src/game-lib-d3-helper.js index 4e51550..de866dd 100644 --- a/src/game-lib-d3-helper.js +++ b/src/game-lib-d3-helper.js @@ -49,6 +49,7 @@ GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT = 'directional-light'; GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT = 'spot-light'; GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT = 'point-light'; GameLib.D3.Helper.HELPER_TYPE_WIREFRAME = 'wireframe'; +GameLib.D3.Helper.HELPER_TYPE_SKELETON = 'skeleton'; /** @@ -60,23 +61,27 @@ GameLib.D3.Helper.prototype.createInstance = function(update) { var instance = null; if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_EDGES) { - instance = new this.graphics.instance.EdgesHelper(this.object.instance, 0x007700); + instance = new THREE.EdgesHelper(this.object.instance, 0x007700); } if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) { - instance = new this.graphics.instance.DirectionalLightHelper(this.object.instance); + instance = new THREE.DirectionalLightHelper(this.object.instance); } if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) { - instance = new this.graphics.instance.PointLightHelper(this.object.instance, 1); + instance = new THREE.PointLightHelper(this.object.instance, 1); } if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) { - instance = new this.graphics.instance.SpotLightHelper(this.object.instance); + instance = new THREE.SpotLightHelper(this.object.instance); } if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) { - instance = new this.graphics.instance.WireframeHelper(this.object.instance, 0x007700); + instance = new THREE.WireframeHelper(this.object.instance, 0x007700); + } + + if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SKELETON) { + instance = new THREE.SkeletonHelper(this.object.skeleton.instance); } if (!instance) { diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index 57dbc6e..25917e9 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -239,64 +239,21 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { var instanceMaterial = this.materials[0].instance; if (this.meshType == GameLib.D3.Mesh.TYPE_NORMAL) { - instance = new this.graphics.instance.Mesh(instanceGeometry, instanceMaterial); + instance = new THREE.Mesh(instanceGeometry, instanceMaterial); } if (this.meshType == GameLib.D3.Mesh.TYPE_CURVE) { - instance = new this.graphics.instance.Points(instanceGeometry, instanceMaterial); + instance = new THREE.Points(instanceGeometry, instanceMaterial); } if (this.meshType == GameLib.D3.Mesh.TYPE_SKINNED) { - var bones = this.skeleton.bones; - - var instanceBones = []; - - for (var bi = 0; bi < bones.length; bi++) { - - var bone = new this.graphics.instance.Bone(); - - bone.name = bones[bi].name; - - bone.position.x = bones[bi].position.x; - bone.position.y = bones[bi].position.y; - bone.position.z = bones[bi].position.z; - - bone.rotation.x = bones[bi].rotation.x; - bone.rotation.y = bones[bi].rotation.y; - bone.rotation.z = bones[bi].rotation.z; - - bone.quaternion.x = bones[bi].quaternion.x; - bone.quaternion.y = bones[bi].quaternion.y; - bone.quaternion.z = bones[bi].quaternion.z; - bone.quaternion.w = bones[bi].quaternion.w; - - bone.scale.x = bones[bi].scale.x; - bone.scale.y = bones[bi].scale.y; - bone.scale.z = bones[bi].scale.z; - - bone.up.x = bones[bi].up.x; - bone.up.y = bones[bi].up.y; - bone.up.z = bones[bi].up.z; - - instanceBones.push(bone); - } - - /** - * Setup the bone relationships - */ - for (var br = 0; br < bones.length; br++) { - for (var cbi = 0; cbi < bones[br].childBoneIds.length; cbi++) { - instanceBones[br].add(instanceBones[bones[br].childBoneIds[cbi]]); - } - } - /** * Setup bones (indexes) */ for (var si = 0; si < this.skinIndices.length; si++) { instanceGeometry.skinIndices.push( - new this.graphics.instance.Quaternion( + new THREE.Vector4( this.skinIndices[si].x, this.skinIndices[si].y, this.skinIndices[si].z, @@ -310,7 +267,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { */ for (var sw = 0; sw < this.skinWeights.length; sw++) { instanceGeometry.skinWeights.push( - new this.graphics.instance.Quaternion( + new THREE.Vector4( this.skinWeights[sw].x, this.skinWeights[sw].y, this.skinWeights[sw].z, @@ -319,25 +276,11 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { ); } - instance = new this.graphics.instance.SkinnedMesh(instanceGeometry, instanceMaterial); + instance = new THREE.SkinnedMesh(instanceGeometry, instanceMaterial); - var skeleton = new this.graphics.instance.Skeleton(instanceBones); + instance.add(this.skeleton.rootBoneInstance); - skeleton.useVertexTexture = this.skeleton.useVertexTexture; - - for (var i = 0; i < bones.length; i++) { - if (bones[i].parentBoneId === null) { - instance.add(instanceBones[i]); - break; - } - } - - instance.bind(skeleton); - - instance.pose(); - - instance.skeleton.skeletonHelper = new this.graphics.instance.SkeletonHelper(instance); - instance.skeleton.skeletonHelper.material.linewidth = 5; + instance.bind(this.skeleton.instance); } if (instance == null) { @@ -426,6 +369,12 @@ GameLib.D3.Mesh.prototype.clone = function() { */ GameLib.D3.Mesh.prototype.toApiMesh = function() { + var apiSkeleton = null; + + if (this.skeleton) { + apiSkeleton = this.skeleton.toApiSkeleton(); + } + return new GameLib.D3.API.Mesh( this.id, this.meshType, @@ -440,7 +389,7 @@ GameLib.D3.Mesh.prototype.toApiMesh = function() { this.materials.map(function(material){return material.toApiMaterial()}), GameLib.Utils.IdOrNull(this.parentMesh), GameLib.Utils.IdOrNull(this.parentScene), - this.skeleton, + apiSkeleton, this.skinIndices, this.skinWeights, this.position.toApiVector(), @@ -488,7 +437,10 @@ GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh, computeNormals, ), objectMesh.parentMesh, objectMesh.parentScene, - objectMesh.skeleton, + GameLib.D3.Skeleton.FromObjectSkeleton( + graphics, + objectMesh.skeleton + ), objectMesh.skinIndices, objectMesh.skinWeights, new GameLib.API.Vector3( diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 6424ce8..611565e 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -83,6 +83,18 @@ GameLib.D3.Scene = function Scene( } } + for (var m = 0; m < this.meshes.length; m++) { + if (this.meshes[m].skeleton) { + this.meshes[m].skeleton.bones.map( + function (bone) { + this.idToObject[bone.id] = bone; + }.bind(this) + ); + + this.idToObject[this.meshes[m].skeleton.id] = this.meshes[m].skeleton; + } + } + this.entityManager.entities.map( function (entity) { this.idToObject[entity.id] = entity; @@ -186,8 +198,7 @@ GameLib.D3.Scene.prototype.toApiScene = function() { apiEntityManager, apiShapes, apiCameras, - this.activeCameraIndex, - apiSplines + this.activeCameraIndex ); }; diff --git a/src/game-lib-d3-skeleton.js b/src/game-lib-d3-skeleton.js index f83180a..878eb88 100644 --- a/src/game-lib-d3-skeleton.js +++ b/src/game-lib-d3-skeleton.js @@ -1,69 +1,243 @@ /** * Skeleton Superset - * @param id - * @param bones GameLib.D3.API.Bone - * @param boneInverses - * @param useVertexTexture - * @param boneTextureWidth - * @param boneTextureHeight - * @param boneMatrices - * @param boneTexture * @constructor + * @param graphics GameLib.D3.Graphics + * @param apiSkeleton GameLib.D3.API.Skeleton */ GameLib.D3.Skeleton = function Skeleton( - id, - bones, - boneInverses, - useVertexTexture, - boneTextureWidth, - boneTextureHeight, - boneMatrices, - boneTexture + graphics, + apiSkeleton ) { - this.id = id; - - this.bones = bones; - - /** - * An array of Matrix4s that represent the inverse of the matrixWorld of the individual bones. - * @type GameLib.Matrix4[] - */ - if (typeof boneInverses == 'undefined') { - boneInverses = []; - } - this.boneInverses = boneInverses; - - /** - * Use a vertex texture in the shader - allows for more than 4 bones per vertex, not supported by all devices - * @type {boolean} - */ - if (typeof useVertexTexture == 'undefined') { - useVertexTexture = false; - } - this.useVertexTexture = useVertexTexture; - - if (this.useVertexTexture == true) { - console.warn('support for vertex texture bones is not supported yet - something could break somewhere'); - } - - if (typeof boneTextureWidth == 'undefined') { - boneTextureWidth = 0; - } - this.boneTextureWidth = boneTextureWidth; - - if (typeof boneTextureHeight == 'undefined') { - boneTextureHeight = 0; - } - this.boneTextureHeight = boneTextureHeight; - - if (typeof boneMatrices == 'undefined') { - boneMatrices = []; - } - this.boneMatrices = boneMatrices; - - if (typeof boneTexture == 'undefined') { - boneTexture = []; - } - this.boneTexture = boneTexture; + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Skeleton.call( + this, + apiSkeleton.id, + apiSkeleton.name, + apiSkeleton.bones, + apiSkeleton.boneInverses, + apiSkeleton.useVertexTexture, + apiSkeleton.boneTextureWidth, + apiSkeleton.boneTextureHeight, + apiSkeleton.boneMatrices, + apiSkeleton.boneTexture + ); + + 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.toApiSkeleton = function() { + + var apiSkeleton = new GameLib.D3.API.Skeleton( + this.id, + this.name, + this.bones.map( + function (bone) { + return bone.toApiBone(); + } + ), + this.boneInverses.map( + function (boneInverse) { + return boneInverse.toApiMatrix(); + } + ), + this.useVertexTexture, + this.boneTextureWidth, + this.boneTextureHeight, + this.boneMatrices.map( + function (boneMatrix) { + return boneMatrix.toApiMatrix(); + } + ), + this.boneTexture + ); + + 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 = new GameLib.D3.API.Skeleton( + objectSkeleton.id, + objectSkeleton.name, + objectSkeleton.bones.map( + function (objectBone) { + return GameLib.D3.Bone.FromObjectBone(graphics, objectBone); + } + ), + objectSkeleton.boneInverses.map( + function (boneInverse) { + return new GameLib.D3.API.Matrix4( + new GameLib.D3.Vector4( + boneInverse[0], + boneInverse[1], + boneInverse[2], + boneInverse[3] + ), + new GameLib.D3.Vector4( + boneInverse[4], + boneInverse[5], + boneInverse[6], + boneInverse[7] + ), + new GameLib.D3.Vector4( + boneInverse[8], + boneInverse[9], + boneInverse[10], + boneInverse[11] + ), + new GameLib.D3.Vector4( + boneInverse[12], + boneInverse[13], + boneInverse[14], + boneInverse[15] + ) + ); + } + ), + objectSkeleton.useVertexTexture, + objectSkeleton.boneTextureWidth, + objectSkeleton.boneTextureHeight, + objectSkeleton.boneMatrices.map( + function (boneMatrix) { + return new GameLib.D3.API.Matrix4( + new GameLib.D3.Vector4( + boneMatrix[0], + boneMatrix[1], + boneMatrix[2], + boneMatrix[3] + ), + new GameLib.D3.Vector4( + boneMatrix[4], + boneMatrix[5], + boneMatrix[6], + boneMatrix[7] + ), + new GameLib.D3.Vector4( + boneMatrix[8], + boneMatrix[9], + boneMatrix[10], + boneMatrix[11] + ), + new GameLib.D3.Vector4( + boneMatrix[12], + boneMatrix[13], + boneMatrix[14], + boneMatrix[15] + ) + ); + } + ), + objectSkeleton.boneTexture + ); + + var skeleton = new GameLib.D3.Skeleton( + graphics, + apiSkeleton + ); + + return skeleton; +}; \ No newline at end of file diff --git a/src/game-lib-d3-spline.js b/src/game-lib-d3-spline.js index 004ad42..d20f680 100644 --- a/src/game-lib-d3-spline.js +++ b/src/game-lib-d3-spline.js @@ -18,6 +18,16 @@ GameLib.D3.Spline = function RuntimeSpline( apiSpline.vertices ); + this.vertices = this.vertices.map( + function (vertex) { + return new GameLib.Vector3( + graphics, + this, + vertex + ) + } + ); + this.instance = this.createInstance(); }; @@ -76,7 +86,7 @@ GameLib.D3.Spline.prototype.toApiComponent = function() { this.name, this.vertices.map( function (vertex) { - return vertex.toApiVertex() + return vertex.toApiVector() } ) ); @@ -87,7 +97,7 @@ GameLib.D3.Spline.prototype.toApiComponent = function() { * Returns a GameLib.D3.Spline from a spline Object * @param graphics GameLib.D3.Graphics * @param objectComponent Object - * @returns {GameLib.D3.Spline} + * @returns GameLib.D3.Spline * @constructor */ GameLib.D3.Spline.FromObjectComponent = function( @@ -99,7 +109,11 @@ GameLib.D3.Spline.FromObjectComponent = function( objectComponent.name, objectComponent.vertices.map( function (objectVertex) { - return GameLib.D3.Vertex.FromObjectVertex(graphics, objectVertex); + return new GameLib.API.Vector3( + objectVertex.x, + objectVertex.y, + objectVertex.z + ) } ) ); diff --git a/src/game-lib-d3-vertex.js b/src/game-lib-d3-vertex.js index 6580deb..add914a 100644 --- a/src/game-lib-d3-vertex.js +++ b/src/game-lib-d3-vertex.js @@ -17,11 +17,11 @@ GameLib.D3.Vertex = function Vertex( apiVertex.boneWeights ); - // this.position = new GameLib.Vector3( - // this.graphics, - // null, - // this.position - // ); + this.position = new GameLib.Vector3( + this.graphics, + null, + this.position + ); //TODO: GameLib.D3.BoneWeight implementation