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

233 lines
5.6 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();
if (GameLib.Utils.UndefinedOrNull(apiSkeleton)) {
apiSkeleton = {};
}
if (apiSkeleton instanceof GameLib.D3.Skeleton) {
return 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,
2017-01-19 17:50:11 +01:00
apiSkeleton.boneTexture,
apiSkeleton.parentEntity
2016-12-23 16:07:10 +01:00
);
2017-01-06 16:53:53 +01:00
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');
}
2017-01-06 16:53:53 +01:00
}.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');
}
2017-01-06 16:53:53 +01:00
}.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');
}
2017-01-06 16:53:53 +01:00
}.bind(this)
);
2017-01-19 17:50:11 +01:00
this.buildIdToObject();
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}
*/
2017-05-16 14:51:57 +02:00
GameLib.D3.Skeleton.prototype.toApiObject = function() {
2016-12-23 16:07:10 +01:00
var apiSkeleton = new GameLib.D3.API.Skeleton(
this.id,
this.name,
this.bones.map(
function (bone) {
2017-05-16 14:51:57 +02:00
return bone.toApiObject();
2016-12-23 16:07:10 +01:00
}
),
this.boneInverses.map(
function (boneInverse) {
2017-05-16 14:51:57 +02:00
return boneInverse.toApiObject();
2016-12-23 16:07:10 +01:00
}
),
this.useVertexTexture,
this.boneTextureWidth,
this.boneTextureHeight,
this.boneMatrices.map(
function (boneMatrix) {
2017-05-16 14:51:57 +02:00
return boneMatrix.toApiObject();
2016-12-23 16:07:10 +01:00
}
),
2017-01-19 17:50:11 +01:00
this.boneTexture,
this.parentEntity
2016-12-23 16:07:10 +01:00
);
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;
};