243 lines
6.3 KiB
JavaScript
243 lines
6.3 KiB
JavaScript
/**
|
|
* 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();
|
|
|
|
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;
|
|
}; |