light fixes - also parent scenes for lights

beta.r3js.org
-=yb4f310 2017-06-10 10:04:29 +02:00
parent 8f40160d53
commit 7b70d8201e
6 changed files with 233 additions and 92 deletions

View File

@ -15,6 +15,7 @@
* @param power * @param power
* @param angle * @param angle
* @param penumbra * @param penumbra
* @param parentScene
* @param parentEntity * @param parentEntity
* @constructor * @constructor
*/ */
@ -34,12 +35,15 @@ GameLib.D3.API.Light = function(
power, power,
angle, angle,
penumbra, penumbra,
parentScene,
parentEntity parentEntity
) { ) {
GameLib.Component.call( GameLib.Component.call(
this, this,
GameLib.Component.COMPONENT_LIGHT, GameLib.Component.COMPONENT_LIGHT,
null, {
'parentScene' : GameLib.D3.Scene
},
null, null,
parentEntity parentEntity
); );
@ -118,6 +122,11 @@ GameLib.D3.API.Light = function(
penumbra = 0; penumbra = 0;
} }
this.penumbra = penumbra; this.penumbra = penumbra;
if (GameLib.Utils.UndefinedOrNull(parentScene)){
parentScene = null;
}
this.parentScene = parentScene;
}; };
GameLib.D3.API.Light.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Light.prototype = Object.create(GameLib.Component.prototype);
@ -145,6 +154,7 @@ GameLib.D3.API.Light.FromObjectLight = function(objectLight) {
objectLight.power, objectLight.power,
objectLight.angle, objectLight.angle,
objectLight.penumbra, objectLight.penumbra,
objectLight.parentScene,
objectLight.parentEntity objectLight.parentEntity
); );
}; };

View File

@ -96,13 +96,103 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4;
*/ */
GameLib.D3.Light.prototype.createInstance = function(update) { GameLib.D3.Light.prototype.createInstance = function(update) {
var instance = null;
if (update) { if (update) {
instance = this.instance;
var oldInstance = null;
if (GameLib.Utils.UndefinedOrNull(this.instance)) {
console.warn('cannot update an non-existent light');
return
} }
if (!instance) { if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT &&
!(this.instance instanceof THREE.AmbientLight)
) {
oldInstance = this.instance;
this.instance = new THREE.AmbientLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL &&
!(this.instance instanceof THREE.DirectionalLight)
) {
oldInstance = this.instance;
this.instance = new THREE.DirectionalLight(
this.color.instance,
this.intensity
);
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT &&
!(this.instance instanceof THREE.PointLight)
) {
oldInstance = this.instance;
this.instance = new THREE.PointLight(
this.color.instance,
this.intensity
);
this.instance.distance = this.distance;
this.instance.decay = this.decay;
} else if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT &&
!(this.instance instanceof THREE.SpotLight)
) {
oldInstance = this.instance;
this.instance = new THREE.SpotLight(
this.color.instance,
this.intensity
);
this.instance.distance = this.distance;
this.instance.angle = this.angle;
this.instance.penumbra = this.penumbra;
this.instance.decay = this.decay;
} else {
/**
* Light type not supported or light types match
*/
}
if (oldInstance && this.parentScene) {
this.parentScene.instance.remove(oldInstance);
this.parentScene.instance.add(this.instance);
}
this.instance.name = this.name;
this.instance.position.x = this.position.x;
this.instance.position.y = this.position.y;
this.instance.position.z = this.position.z;
this.instance.scale.x = this.scale.x;
this.instance.scale.y = this.scale.y;
this.instance.scale.z = this.scale.z;
if (this.instance.target) {
this.instance.target.position.x = this.targetPosition.x;
this.instance.target.position.y = this.targetPosition.y;
this.instance.target.position.z = this.targetPosition.z;
}
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.intensity = this.intensity;
this.instance.color.set(this.color.toHex());
} else {
var instance = null;
if ( if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT || this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT ||
@ -112,9 +202,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) {
this.color.instance, this.color.instance,
this.intensity this.intensity
); );
} } else if (
if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL || this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL ||
this.lightType === 'DirectionalLight' this.lightType === 'DirectionalLight'
) { ) {
@ -122,9 +210,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) {
this.color.instance, this.color.instance,
this.intensity this.intensity
); );
} } else if (
if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT || this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT ||
this.lightType === 'PointLight' this.lightType === 'PointLight'
) { ) {
@ -134,9 +220,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) {
); );
instance.distance = this.distance; instance.distance = this.distance;
instance.decay = this.decay; instance.decay = this.decay;
} } else if (
if (
this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT || this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT ||
this.lightType === 'SpotLight' this.lightType === 'SpotLight'
) { ) {
@ -148,15 +232,10 @@ GameLib.D3.Light.prototype.createInstance = function(update) {
instance.angle = this.angle; instance.angle = this.angle;
instance.penumbra = this.penumbra; instance.penumbra = this.penumbra;
instance.decay = this.decay; instance.decay = this.decay;
} else {
console.warn('unsupported light type: ' + this.lightType);
return null;
} }
}
if (!instance) {
console.warn('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?');
throw new Error('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?');
}
instance.gameLibObject = this;
instance.name = this.name; instance.name = this.name;
@ -184,17 +263,14 @@ GameLib.D3.Light.prototype.createInstance = function(update) {
instance.color.set(this.color.toHex()); instance.color.set(this.color.toHex());
return instance; return instance;
}
}; };
/** /**
* Updates the instance with the current state * Updates the instance with the current state
*/ */
GameLib.D3.Light.prototype.updateInstance = function() { GameLib.D3.Light.prototype.updateInstance = function() {
this.instance = this.createInstance(true); this.createInstance(true);
};
GameLib.D3.Light.prototype.clone = function() {
return _.cloneDeep(this);
}; };
/** /**

View File

@ -458,14 +458,6 @@ GameLib.D3.Mesh.prototype.updateInstance = function() {
this.createInstance(true); this.createInstance(true);
}; };
/**
* Clones a mesh
* @returns {*}
*/
GameLib.D3.Mesh.prototype.clone = function() {
return _.cloneDeep(this);
};
/** /**
* Converts a GameLib.D3.Mesh to a GameLib.D3.API.Mesh * Converts a GameLib.D3.Mesh to a GameLib.D3.API.Mesh
* @returns {GameLib.D3.API.Mesh} * @returns {GameLib.D3.API.Mesh}
@ -524,3 +516,31 @@ GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh) {
); );
}; };
/**
* Centers the mesh around origin
*/
GameLib.D3.Mesh.prototype.centerAroundOrigin = function() {
var localPosition = this.instance.geometry.center();
this.instance.position.set(0,0,0);
this.instance.updateMatrix();
this.position.x = this.instance.position.x;
this.position.y = this.instance.position.y;
this.position.z = this.instance.position.z;
for (var v = 0; v < this.instance.geometry.vertices.length; v++) {
this.vertices[v].position.x = this.instance.geometry.vertices[v].x;
this.vertices[v].position.y = this.instance.geometry.vertices[v].y;
this.vertices[v].position.z = this.instance.geometry.vertices[v].z;
}
this.localPosition.x = -localPosition.x;
this.localPosition.y = -localPosition.y;
this.localPosition.z = -localPosition.z;
this.updateInstance();
};

View File

@ -269,50 +269,64 @@ GameLib.D3.Scene.FromObjectScene = function(
/** /**
* Adds a mesh to the scene * Adds a mesh to the scene
* @param mesh GameLib.D3.Mesh * @param object GameLib.D3.Mesh
*/ */
GameLib.D3.Scene.prototype.addMesh = function(mesh) { GameLib.D3.Scene.prototype.addObject = function(object) {
if (mesh instanceof GameLib.D3.Mesh) { if (object instanceof GameLib.D3.Mesh) {
this.meshes.push(mesh); this.meshes.push(object);
} else if (mesh instanceof GameLib.D3.API.Mesh) { } else if (object instanceof GameLib.D3.API.Mesh) {
mesh = new GameLib.D3.Mesh( object = new GameLib.D3.Mesh(
this.graphics, this.graphics,
mesh object
); );
this.meshes.push(mesh); this.meshes.push(object);
} }
mesh.parentScene = this; if (object instanceof GameLib.D3.Light) {
this.lights.push(object);
} else if (object instanceof GameLib.D3.API.Light) {
object = new GameLib.D3.Light(
this.graphics,
object
);
this.lights.push(object);
}
this.instance.add(mesh.instance); object.parentScene = this;
this.instance.add(object.instance);
this.buildIdToObject(); this.buildIdToObject();
}; };
/** /**
* *
* @param mesh * @param object
*/ */
GameLib.D3.Scene.prototype.removeMesh = function(mesh) { GameLib.D3.Scene.prototype.removeObject = function(object) {
var index = -1; var index = -1;
if (mesh instanceof GameLib.D3.Mesh) { if (object instanceof GameLib.D3.Mesh) {
index = this.meshes.indexOf(mesh); index = this.meshes.indexOf(object);
} else {
console.warn('Cannot remove this mesh - what is this ?' + mesh.toString());
throw new Error('Cannot remove this mesh - what is this ?' + mesh.toString())
}
this.instance.remove(mesh.instance);
if (index !== -1) { if (index !== -1) {
this.meshes.splice(index, 1); this.meshes.splice(index, 1);
} }
} else if (object instanceof GameLib.D3.Light) {
index = this.lights.indexOf(object);
if (index !== -1) {
this.lights.splice(index, 1);
}
} else {
console.warn('Cannot remove this mesh - what is this ?' + object.toString())
return;
}
if (mesh.parentScene === this) { this.instance.remove(object.instance);
mesh.parentScene = null;
if (object.parentScene === this) {
object.parentScene = null;
} }
this.buildIdToObject(); this.buildIdToObject();

View File

@ -328,8 +328,8 @@ GameLib.EntityManager.prototype.onParentSceneChange = function(data) {
/** /**
* We remove the mesh from the old scene and add it to the new scene * We remove the mesh from the old scene and add it to the new scene
*/ */
data.originalScene.removeMesh(data.object); data.originalScene.removeObject(data.object);
data.newScene.addMesh(data.object); data.newScene.addObject(data.object);
/** /**
* We inherit the parent entity of this new scene * We inherit the parent entity of this new scene

View File

@ -519,6 +519,19 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property) {
} }
).name(property).listen() ).name(property).listen()
); );
} else if (object instanceof GameLib.D3.Light && property === 'lightType') {
handles.push(
folder.add(
object,
property,
{
'ambient': GameLib.D3.Light.LIGHT_TYPE_AMBIENT,
'directional': GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL,
'spot': GameLib.D3.Light.LIGHT_TYPE_SPOT,
'point': GameLib.D3.Light.LIGHT_TYPE_POINT
}
).name(property).listen()
);
} else { } else {
/** /**
@ -695,20 +708,27 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di
folder.add( folder.add(
object[property], object[property],
'w', 'w',
object[property].w -100,
100
).name(property + '.w').listen().onChange(function(){object.updateInstance()}); ).name(property + '.w').listen().onChange(function(){object.updateInstance()});
} }
folder.add( folder.add(
object[property], object[property],
'x', 'x',
object[property].x -100,
).name(property + '.x').listen().onChange(function(){object.updateInstance()}); 100
).name(property + '.x').listen().onChange(
function(){
object.updateInstance()
}
);
folder.add( folder.add(
object[property], object[property],
'y', 'y',
object[property].y -100,
100
).name(property + '.y').listen().onChange(function(){object.updateInstance()}); ).name(property + '.y').listen().onChange(function(){object.updateInstance()});
if ( if (
@ -718,7 +738,8 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di
folder.add( folder.add(
object[property], object[property],
'z', 'z',
object[property].z -100,
100
).name(property + '.z').listen().onChange(function () { ).name(property + '.z').listen().onChange(function () {
object.updateInstance() object.updateInstance()
}); });