r3-legacy/src/game-lib-d3-skeleton.js

171 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-10-14 12:32:53 +02:00
/**
* Skeleton Superset
* @constructor
2016-12-23 16:07:10 +01:00
* @param graphics GameLib.D3.Graphics
* @param apiSkeleton GameLib.D3.API.Skeleton
2016-10-14 12:32:53 +02:00
*/
2016-11-21 16:08:39 +01:00
GameLib.D3.Skeleton = function Skeleton(
2016-12-23 16:07:10 +01:00
graphics,
apiSkeleton
2016-10-14 12:32:53 +02:00
) {
2016-12-23 16:07:10 +01:00
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();
};
2016-10-14 12:32:53 +02:00
2016-12-23 16:07:10 +01:00
GameLib.D3.Skeleton.prototype = Object.create(GameLib.D3.API.Skeleton.prototype);
GameLib.D3.Skeleton.prototype.constructor = GameLib.D3.Skeleton;
2016-10-14 12:32:53 +02:00
2016-12-23 16:07:10 +01:00
/**
* Creates an instance skeleton
* @param update boolean
*/
GameLib.D3.Skeleton.prototype.createInstance = function(update) {
2016-10-14 12:32:53 +02:00
2016-12-23 16:07:10 +01:00
var instance = null;
2016-10-14 12:32:53 +02:00
2016-12-23 16:07:10 +01:00
if (update) {
//TODO - update instance with bone info
instance = this.instance;
} else {
instance = new THREE.Skeleton(
this.bones.map (
function (bone) {
return bone.instance;
}
)
);
2016-10-14 12:32:53 +02:00
}
2016-12-23 16:07:10 +01:00
instance.useVertexTexture = this.useVertexTexture;
var parentBoneInstance = this.bones.reduce(
function (result, bone) {
if (result) {
return result;
}
2016-10-14 12:32:53 +02:00
2016-12-23 16:07:10 +01:00
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;
2016-10-14 12:32:53 +02:00
}
2016-12-23 16:07:10 +01:00
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;
2016-10-14 12:32:53 +02:00
};
2016-12-23 16:07:10 +01:00
/**
* 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;
}
2017-01-05 19:34:28 +01:00
var apiSkeleton = GameLib.D3.API.Skeleton.FromObjectSkeleton(objectSkeleton);
2016-12-23 16:07:10 +01:00
var skeleton = new GameLib.D3.Skeleton(
graphics,
apiSkeleton
);
return skeleton;
};