before animation refactor

beta.r3js.org
-=yb4f310 2017-10-03 14:50:34 +02:00
parent c39d975f8f
commit 3a422a5acf
9 changed files with 995 additions and 363 deletions

View File

@ -92,6 +92,10 @@ GameLib.Event.TOUCH_END = 0x4a;
GameLib.Event.TOUCH_MOVE = 0x4b;
GameLib.Event.TOUCH_CANCEL = 0x4c;
GameLib.Event.GET_REMOTE_API_URL = 0x4d;
GameLib.Event.GET_GRAPHICS_IMPLEMENTATION = 0x4e;
GameLib.Event.GET_PHYSICS_IMPLEMENTATION = 0x4f;
GameLib.Event.GET_CODER_IMPLEMENTATION = 0x50;
GameLib.Event.ANIMATION_MESH_ADDED = 0x51;
/**
@ -180,6 +184,10 @@ GameLib.Event.GetEventName = function(number) {
case 0x4b : return 'touch_move';
case 0x4c : return 'touch_cancel';
case 0x4d : return 'get_remote_api_url';
case 0x4e : return 'get_graphics_implementation';
case 0x4f : return 'get_physics_implementation';
case 0x50 : return 'get_coder_implementation';
case 0x51 : return 'animation_mesh_added';
break;
}

View File

@ -10,7 +10,7 @@
* @param scaleFn
* @param blocking
* @param applyToMeshWhenDone
* @param parentMesh
* @param meshes
* @param parentEntity
* @constructor
*/
@ -25,7 +25,7 @@ GameLib.D3.API.Animation = function (
scaleFn,
blocking,
applyToMeshWhenDone,
parentMesh,
meshes,
parentEntity
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
@ -78,10 +78,10 @@ GameLib.D3.API.Animation = function (
}
this.applyToMeshWhenDone = applyToMeshWhenDone;
if (GameLib.Utils.UndefinedOrNull(parentMesh)) {
parentMesh = null;
if (GameLib.Utils.UndefinedOrNull(meshes)) {
meshes = [];
}
this.parentMesh = parentMesh;
this.meshes = meshes;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
@ -110,7 +110,7 @@ GameLib.D3.API.Animation.FromObject = function(objectComponent) {
objectComponent.scaleFn,
objectComponent.blocking,
objectComponent.applyToMeshWhenDone,
objectComponent.parentMesh,
objectComponent.meshes,
objectComponent.parentEntity
);
};

View File

@ -13,12 +13,14 @@
* @param skinWeights
* @param position GameLib.API.Vector3
* @param quaternion GameLib.API.Quaternion
* @param rotation
* @param scale GameLib.API.Vector3
* @param up
* @param modelMatrix GameLib.API.Matrix4
* @param parentEntity
* @param renderOrder
* @param isBufferMesh
* @param useQuaternion (use quaternion property for rotation rather than rotation property)
* @constructor
*/
GameLib.D3.API.Mesh = function(
@ -35,12 +37,14 @@ GameLib.D3.API.Mesh = function(
skinWeights,
position,
quaternion,
rotation,
scale,
up,
modelMatrix,
parentEntity,
renderOrder,
isBufferMesh
isBufferMesh,
useQuaternion
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
@ -107,6 +111,11 @@ GameLib.D3.API.Mesh = function(
}
this.quaternion = quaternion;
if (GameLib.Utils.UndefinedOrNull(rotation)) {
rotation = new GameLib.API.Vector3(0,0,0);
}
this.rotation = rotation;
if (GameLib.Utils.UndefinedOrNull(scale)) {
scale = new GameLib.API.Vector3(1,1,1);
}
@ -136,6 +145,11 @@ GameLib.D3.API.Mesh = function(
isBufferMesh = false;
}
this.isBufferMesh = isBufferMesh;
if (GameLib.Utils.UndefinedOrNull(useQuaternion)) {
useQuaternion = true;
}
this.useQuaternion = useQuaternion;
};
GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype);
@ -192,6 +206,11 @@ GameLib.D3.API.Mesh.FromObject = function (objectMesh){
apiPosition = GameLib.API.Vector3.FromObject(objectMesh.position);
}
var apiRotation = new GameLib.API.Vector3();
if (objectMesh.rotation) {
apiRotation = GameLib.API.Vector3.FromObject(objectMesh.rotation);
}
var apiQuaternion = new GameLib.API.Quaternion();
if (objectMesh.quaternion) {
apiQuaternion = GameLib.API.Quaternion.FromObject(objectMesh.quaternion);
@ -226,11 +245,13 @@ GameLib.D3.API.Mesh.FromObject = function (objectMesh){
objectMesh.skinWeights,
apiPosition,
apiQuaternion,
apiRotation,
apiScale,
apiUp,
apiModelMatrix,
objectMesh.parentEntity,
objectMesh.renderOrder,
objectMesh.isBufferMesh
objectMesh.isBufferMesh,
objectMesh.useQuaternion
);
};

View File

@ -32,7 +32,7 @@ GameLib.D3.Animation = function(
apiAnimation.scaleFn,
apiAnimation.blocking,
apiAnimation.applyToMeshWhenDone,
apiAnimation.parentMesh,
apiAnimation.meshes,
apiAnimation.parentEntity
);
@ -48,7 +48,10 @@ GameLib.D3.Animation = function(
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_ANIMATION
GameLib.Component.COMPONENT_ANIMATION,
{
'meshes' : [GameLib.D3.Mesh]
}
);
};
@ -167,7 +170,11 @@ GameLib.D3.Animation.prototype.toApiObject = function() {
this.scaleFn,
this.blocking,
this.applyToMeshWhenDone,
GameLib.Utils.IdOrNull(this.parentMesh),
this.meshes.map(
function(mesh) {
return GameLib.Utils.IdOrNull(mesh);
}
),
GameLib.Utils.IdOrNull(this.parentEntity)
);
@ -233,4 +240,16 @@ GameLib.D3.Animation.prototype.launchEditor = function(){
GameLib.D3.Animation.prototype.closeEditor = function(){
var dom = this.editor.getWrapperElement();
dom.parentElement.removeChild(dom);
};
GameLib.D3.Animation.prototype.addMesh = function(mesh) {
this.meshes.push(mesh);
GameLib.Event.Emit(
GameLib.Event.ANIMATION_MESH_ADDED,
{
animation : this,
mesh : mesh
}
)
};

View File

@ -134,6 +134,11 @@ GameLib.D3.Helper.prototype.createInstance = function() {
*/
GameLib.D3.Helper.prototype.updateInstance = function() {
this.instance.position.copy(this.object.instance.position);
if (this.object.instance.parentMesh && this.object.instance.parentMesh.instance) {
this.instance.position.add(this.object.instance.parentMesh.instance.position);
}
this.instance.scale.copy(this.object.instance.scale);
this.instance.quaternion.copy(this.object.instance.quaternion);
};

View File

@ -34,15 +34,16 @@ GameLib.D3.Mesh = function (
apiMesh.skinWeights,
apiMesh.position,
apiMesh.quaternion,
apiMesh.rotation,
apiMesh.scale,
apiMesh.up,
apiMesh.modelMatrix,
apiMesh.parentEntity,
apiMesh.renderOrder,
apiMesh.isBufferMesh
apiMesh.isBufferMesh,
apiMesh.useQuaternion
);
this.faces = this.faces.map(
function(face) {
if (face instanceof GameLib.D3.API.Face) {
@ -56,7 +57,6 @@ GameLib.D3.Mesh = function (
}.bind(this)
);
this.materials = this.materials.map(
function(material) {
if (material instanceof GameLib.D3.API.Material) {
@ -92,6 +92,18 @@ GameLib.D3.Mesh = function (
this
);
this.quaternion = new GameLib.Quaternion(
this.graphics,
this.quaternion,
this
);
this.rotation = new GameLib.Vector3(
this.graphics,
this.rotation,
this
);
this.scale = new GameLib.Vector3(
this.graphics,
this.scale,
@ -104,12 +116,6 @@ GameLib.D3.Mesh = function (
this
);
this.quaternion = new GameLib.Quaternion(
this.graphics,
this.quaternion,
this
);
this.modelMatrix = new GameLib.Matrix4(
this.graphics,
this.modelMatrix,
@ -466,7 +472,44 @@ GameLib.D3.Mesh.prototype.createInstance = function() {
}
}
this.createInstanceDefaults(instance);
instance.name = this.name;
if (this.parentMesh && this.parentMesh.loaded) {
this.parentMesh.add(instance, this);
}
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
if (this.useQuaternion) {
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.quaternion.setFromAxisAngle(
new THREE.Vector3(
this.quaternion.axis.x,
this.quaternion.axis.y,
this.quaternion.axis.z
),
this.quaternion.angle
);
} else {
instance.rotation.x = this.rotation.x;
instance.rotation.y = this.rotation.y;
instance.rotation.z = this.rotation.z;
}
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;
instance.renderOrder = this.renderOrder;
return instance;
};
@ -485,13 +528,19 @@ GameLib.D3.Mesh.prototype.updateInstance = function() {
this.instance.geometry = this.createInstanceGeometry(this.instance.geometry);
}
if (this.updateRotationFromAxisAngle) {
this.updateInstanceRotationFromAxisAngle();
if (this.useQuaternion) {
if (this.updateRotationFromAxisAngle) {
this.updateInstanceRotationFromAxisAngle();
} else {
this.instance.quaternion.x = this.quaternion.x;
this.instance.quaternion.y = this.quaternion.y;
this.instance.quaternion.z = this.quaternion.z;
this.instance.quaternion.w = this.quaternion.w;
}
} else {
this.instance.quaternion.x = this.quaternion.x;
this.instance.quaternion.y = this.quaternion.y;
this.instance.quaternion.z = this.quaternion.z;
this.instance.quaternion.w = this.quaternion.w;
this.instance.rotation.x = this.rotation.x;
this.instance.rotation.y = this.rotation.y;
this.instance.rotation.z = this.rotation.z;
}
if (this.parentMesh && this.parentMesh.loaded) {
@ -773,57 +822,114 @@ GameLib.D3.Mesh.prototype.applyBones = function(geometry) {
};
GameLib.D3.Mesh.prototype.setParentMesh = function(parentMesh) {
this.parentMesh = parentMesh;
if (parentMesh && parentMesh.loaded) {
if (this.instance) {
this.instance.parent = this.parentMesh.instance;
}
/**
* Adds a child instance to this instance
* @param childInstance
* @param child
*/
GameLib.D3.Mesh.prototype.addChild = function(childInstance, child) {
if (!this.instance) {
throw new Error('mesh instance not loaded yet : ' + this.name);
}
if (GameLib.Utils.UndefinedOrNull(childInstance)) {
throw new Error('no child mesh instance');
}
if (GameLib.Utils.UndefinedOrNull(child)) {
throw new Error('no child mesh');
}
this.instance.add(childInstance);
childInstance.parent = this.instance;
child.parentMesh = this;
};
GameLib.D3.Mesh.prototype.createInstanceDefaults = function(instance) {
/**
* Sets a parent for this mesh
* @param parentMesh
*/
GameLib.D3.Mesh.prototype.setParentMesh = function(parentMesh) {
instance.name = this.name;
/**
* Are we removing this child from the parent?
*/
if (GameLib.Utils.UndefinedOrNull(parentMesh)) {
if (this.parentMesh && this.parentMesh.loaded) {
if (this.parentMesh.loaded) {
instance.parent = this.parentMesh.instance;
if (this.instance && this.instance.parent && this.parentScene.instance) {
/**
* Update the parent matrix world
*/
this.instance.parent.updateMatrixWorld();
/**
* Copy the child world position into a temporary vector
* @type {THREE.Vector3}
*/
var vector = new THREE.Vector3();
vector.setFromMatrixPosition(this.instance.matrixWorld);
/**
* Detach child from parent within this scene
*/
THREE.SceneUtils.detach(
this.instance,
this.instance.parent,
this.parentScene.instance
);
/**
* We remember from now on that we have no parent mesh
* @type {null}
*/
this.parentMesh = null;
/**
* We store the world position back to the child
*/
this.position.x = vector.x;
this.position.y = vector.y;
this.position.z = vector.z;
/**
* Update the instance position
*/
this.updateInstancePosition();
/**
* Don't touch this instance parent - since it is now probably a scene object
*/
/**
* TODO: do we apply rotation somehow?
*/
} else {
throw new Error('Not enough information to detach')
}
} else {
if (!this.instance) {
throw new Error('No valid instance at time of adding to parent for mesh ' + this.name);
}
if (!(parentMesh instanceof GameLib.D3.Mesh)) {
throw new Error('Not a valid parent mesh');
}
this.parentMesh = parentMesh;
/**
* Add this as a child to the parent
*/
this.parentMesh.addChild(this.instance, this);
}
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.quaternion.setFromAxisAngle(
new THREE.Vector3(
this.quaternion.axis.x,
this.quaternion.axis.y,
this.quaternion.axis.z
),
this.quaternion.angle
);
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;
instance.renderOrder = this.renderOrder;
};
@ -886,11 +992,14 @@ GameLib.D3.Mesh.prototype.toApiObject = function() {
this.skinWeights,
this.position.toApiObject(),
this.quaternion.toApiObject(),
this.rotation.toApiObject(),
this.scale.toApiObject(),
this.up.toApiObject(),
this.modelMatrix.toApiObject(),
GameLib.Utils.IdOrNull(this.parentEntity),
this.renderOrder
this.renderOrder,
this.isBufferMesh,
this.useQuaternion
);
return apiMesh;
@ -1046,19 +1155,27 @@ GameLib.D3.Mesh.prototype.applyPositionRotationScale = function() {
this.scale.z = 1;
this.instance.scale.set(1,1,1);
/**
* Reset rotation
* @type {number}
*/
this.quaternion.x = 0;
this.quaternion.y = 0;
this.quaternion.z = 0;
this.quaternion.w = 1;
this.quaternion.axis.x = 0;
this.quaternion.axis.y = 0;
this.quaternion.axis.z = 0;
this.quaternion.angle = 0;
this.updateInstanceRotationFromAxisAngle();
if (this.useQuaternion) {
/**
* Reset rotation
* @type {number}
*/
this.quaternion.x = 0;
this.quaternion.y = 0;
this.quaternion.z = 0;
this.quaternion.w = 1;
this.quaternion.axis.x = 0;
this.quaternion.axis.y = 0;
this.quaternion.axis.z = 0;
this.quaternion.angle = 0;
this.updateInstanceRotationFromAxisAngle();
} else {
this.rotation.x = 0;
this.rotation.y = 0;
this.rotation.z = 0;
this.instance.rotation.set(0,0,0);
}
/**
* Update our instance matrix
@ -1092,6 +1209,21 @@ GameLib.D3.Mesh.prototype.updateInstanceRotationFromAxisAngle = function(axis, a
this.quaternion.w = this.quaternion.instance.w;
};
GameLib.D3.Mesh.prototype.updateInstanceRotation = function() {
this.rotation.instance.set(
this.rotation.x,
this.rotation.y,
this.rotation.z
);
this.instance.rotation.copy(this.rotation.instance);
if (this.helper) {
this.helper.updateInstance();
}
};
GameLib.D3.Mesh.prototype.updateInstancePosition = function() {
this.position.instance.set(

View File

@ -89,6 +89,11 @@ GameLib.EntityManager.prototype.removeComponent = function(data) {
data.component.parentEntity.removeComponent(data.component);
}
if (data.component instanceof GameLib.D3.Mesh &&
data.component.parentScene instanceof GameLib.D3.Scene) {
data.component.parentScene.removeObject(data.component);
}
var index = this.register.indexOf(data.component);
if (index !== -1) {

File diff suppressed because it is too large Load Diff

View File

@ -193,7 +193,15 @@ GameLib.System.GUI.prototype.onChange = function(property, subProperty, affected
component[property][subProperty] = value;
if (typeof component[property].updateInstance === 'function') {
if (component instanceof GameLib.D3.Mesh && property === 'rotation') {
component.useQuaternion = false;
}
if (component instanceof GameLib.D3.Mesh && property === 'quaternion') {
component.useQuaternion = true;
}
if (typeof component[property].updateInstance === 'function') {
component[property].updateInstance();
} else if (typeof component[property][subProperty].updateInstance === 'function') {
component[property][subProperty].updateInstance();
@ -276,6 +284,7 @@ GameLib.System.GUI.prototype.buildQuaternionControl = function(folder, component
).name('quaternion.axis.x').onChange(
function(value) {
affected.map(function(component){
component.useQuaternion = true;
component[property]['axis'].x = Number(value);
component.updateInstance();
})
@ -291,6 +300,7 @@ GameLib.System.GUI.prototype.buildQuaternionControl = function(folder, component
).name('quaternion.axis.y').onChange(
function(value) {
affected.map(function(component){
component.useQuaternion = true;
component[property]['axis'].y = Number(value);
component.updateInstance();
})
@ -306,6 +316,7 @@ GameLib.System.GUI.prototype.buildQuaternionControl = function(folder, component
).name('quaternion.axis.z').onChange(
function(value) {
affected.map(function(component){
component.useQuaternion = true;
component[property]['axis'].z = Number(value);
component.updateInstance();
})
@ -335,7 +346,6 @@ GameLib.System.GUI.prototype.buildVectorControl = function(folder, componentTemp
var controllers = [];
if (GameLib.Utils.isVector4(object[property])) {
controllers.push(this.controller(folder, object, property, 'w', step, listen, affected));
}