From 7b70d8201ebd385a136c59b56bc67b79e9fadb77 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Sat, 10 Jun 2017 10:04:29 +0200 Subject: [PATCH] light fixes - also parent scenes for lights --- src/game-lib-d3-api-light.js | 12 ++- src/game-lib-d3-light.js | 182 +++++++++++++++++++++++---------- src/game-lib-d3-mesh-0.js | 36 +++++-- src/game-lib-d3-scene.js | 60 ++++++----- src/game-lib-entity-manager.js | 4 +- src/game-lib-gui.js | 31 +++++- 6 files changed, 233 insertions(+), 92 deletions(-) diff --git a/src/game-lib-d3-api-light.js b/src/game-lib-d3-api-light.js index cb3b203..837ea06 100644 --- a/src/game-lib-d3-api-light.js +++ b/src/game-lib-d3-api-light.js @@ -15,6 +15,7 @@ * @param power * @param angle * @param penumbra + * @param parentScene * @param parentEntity * @constructor */ @@ -34,12 +35,15 @@ GameLib.D3.API.Light = function( power, angle, penumbra, + parentScene, parentEntity ) { GameLib.Component.call( this, GameLib.Component.COMPONENT_LIGHT, - null, + { + 'parentScene' : GameLib.D3.Scene + }, null, parentEntity ); @@ -118,6 +122,11 @@ GameLib.D3.API.Light = function( penumbra = 0; } this.penumbra = penumbra; + + if (GameLib.Utils.UndefinedOrNull(parentScene)){ + parentScene = null; + } + this.parentScene = parentScene; }; GameLib.D3.API.Light.prototype = Object.create(GameLib.Component.prototype); @@ -145,6 +154,7 @@ GameLib.D3.API.Light.FromObjectLight = function(objectLight) { objectLight.power, objectLight.angle, objectLight.penumbra, + objectLight.parentScene, objectLight.parentEntity ); }; diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index e66dc51..db3fe6e 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -96,13 +96,103 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4; */ GameLib.D3.Light.prototype.createInstance = function(update) { - var instance = null; - if (update) { - instance = this.instance; - } - if (!instance) { + var oldInstance = null; + + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + console.warn('cannot update an non-existent light'); + return + } + + 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 ( this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT || @@ -112,9 +202,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) { this.color.instance, this.intensity ); - } - - if ( + } else if ( this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL || this.lightType === 'DirectionalLight' ) { @@ -122,9 +210,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) { this.color.instance, this.intensity ); - } - - if ( + } else if ( this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT || this.lightType === 'PointLight' ) { @@ -134,9 +220,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) { ); instance.distance = this.distance; instance.decay = this.decay; - } - - if ( + } else if ( this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT || this.lightType === 'SpotLight' ) { @@ -148,53 +232,45 @@ GameLib.D3.Light.prototype.createInstance = function(update) { instance.angle = this.angle; instance.penumbra = this.penumbra; instance.decay = this.decay; + } else { + console.warn('unsupported light type: ' + this.lightType); + return null; } + + instance.name = this.name; + + instance.position.x = this.position.x; + instance.position.y = this.position.y; + instance.position.z = this.position.z; + + instance.scale.x = this.scale.x; + instance.scale.y = this.scale.y; + instance.scale.z = this.scale.z; + + if (instance.target) { + instance.target.position.x = this.targetPosition.x; + instance.target.position.y = this.targetPosition.y; + instance.target.position.z = this.targetPosition.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.intensity = this.intensity; + + instance.color.set(this.color.toHex()); + + return instance; } - - 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.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; - - instance.scale.x = this.scale.x; - instance.scale.y = this.scale.y; - instance.scale.z = this.scale.z; - - if (instance.target) { - instance.target.position.x = this.targetPosition.x; - instance.target.position.y = this.targetPosition.y; - instance.target.position.z = this.targetPosition.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.intensity = this.intensity; - - instance.color.set(this.color.toHex()); - - return instance; }; /** * Updates the instance with the current state */ GameLib.D3.Light.prototype.updateInstance = function() { - this.instance = this.createInstance(true); -}; - -GameLib.D3.Light.prototype.clone = function() { - return _.cloneDeep(this); + this.createInstance(true); }; /** diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index fcb360d..4f1c91d 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -458,14 +458,6 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { 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 * @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(); +}; \ No newline at end of file diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 3cd24a5..d281151 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -269,50 +269,64 @@ GameLib.D3.Scene.FromObjectScene = function( /** * 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) { - this.meshes.push(mesh); - } else if (mesh instanceof GameLib.D3.API.Mesh) { - mesh = new GameLib.D3.Mesh( + if (object instanceof GameLib.D3.Mesh) { + this.meshes.push(object); + } else if (object instanceof GameLib.D3.API.Mesh) { + object = new GameLib.D3.Mesh( 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(); }; /** * - * @param mesh + * @param object */ -GameLib.D3.Scene.prototype.removeMesh = function(mesh) { +GameLib.D3.Scene.prototype.removeObject = function(object) { var index = -1; - if (mesh instanceof GameLib.D3.Mesh) { - index = this.meshes.indexOf(mesh); + if (object instanceof GameLib.D3.Mesh) { + index = this.meshes.indexOf(object); + if (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 ?' + mesh.toString()); - throw new Error('Cannot remove this mesh - what is this ?' + mesh.toString()) + console.warn('Cannot remove this mesh - what is this ?' + object.toString()) + return; } - this.instance.remove(mesh.instance); + this.instance.remove(object.instance); - if (index !== -1) { - this.meshes.splice(index, 1); - } - - if (mesh.parentScene === this) { - mesh.parentScene = null; + if (object.parentScene === this) { + object.parentScene = null; } this.buildIdToObject(); diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index f762cf8..9e33ffb 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -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 */ - data.originalScene.removeMesh(data.object); - data.newScene.addMesh(data.object); + data.originalScene.removeObject(data.object); + data.newScene.addObject(data.object); /** * We inherit the parent entity of this new scene diff --git a/src/game-lib-gui.js b/src/game-lib-gui.js index 82b211b..aee5a46 100644 --- a/src/game-lib-gui.js +++ b/src/game-lib-gui.js @@ -519,6 +519,19 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property) { } ).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 { /** @@ -695,20 +708,27 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di folder.add( object[property], 'w', - object[property].w + -100, + 100 ).name(property + '.w').listen().onChange(function(){object.updateInstance()}); } folder.add( object[property], 'x', - object[property].x - ).name(property + '.x').listen().onChange(function(){object.updateInstance()}); + -100, + 100 + ).name(property + '.x').listen().onChange( + function(){ + object.updateInstance() + } + ); folder.add( object[property], 'y', - object[property].y + -100, + 100 ).name(property + '.y').listen().onChange(function(){object.updateInstance()}); if ( @@ -718,7 +738,8 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di folder.add( object[property], 'z', - object[property].z + -100, + 100 ).name(property + '.z').listen().onChange(function () { object.updateInstance() });