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();
|
2017-01-17 13:24:45 +01:00
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiSkeleton)) {
|
|
|
|
apiSkeleton = {};
|
|
|
|
}
|
|
|
|
|
2016-12-23 16:07:10 +01:00
|
|
|
GameLib.D3.API.Skeleton.call(
|
|
|
|
this,
|
|
|
|
apiSkeleton.id,
|
|
|
|
apiSkeleton.name,
|
|
|
|
apiSkeleton.bones,
|
|
|
|
apiSkeleton.boneInverses,
|
|
|
|
apiSkeleton.useVertexTexture,
|
|
|
|
apiSkeleton.boneTextureWidth,
|
|
|
|
apiSkeleton.boneTextureHeight,
|
|
|
|
apiSkeleton.boneMatrices,
|
|
|
|
apiSkeleton.boneTexture
|
|
|
|
);
|
2017-01-06 16:53:53 +01:00
|
|
|
|
|
|
|
this.bones = this.bones.map(
|
|
|
|
function(apiBone) {
|
2017-01-17 13:24:45 +01:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.boneInverses = this.boneInverses.map(
|
|
|
|
function(boneInverse) {
|
2017-01-17 13:24:45 +01:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
this.boneMatrices = this.boneMatrices.map(
|
|
|
|
function(boneMatrices) {
|
2017-01-17 13:24:45 +01:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2016-12-23 16:07:10 +01:00
|
|
|
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;
|
|
|
|
};
|