From cb8abfb5ec3a572b4c17160c055e095a41bc4a09 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Tue, 23 Jan 2018 23:10:20 +0100 Subject: [PATCH] Lights updated --- src/game-lib-a-component-a.js | 44 +++- src/game-lib-api-controls-d3-editor.js | 17 +- src/game-lib-api-controls-keyboard.js | 5 - src/game-lib-api-controls-mouse.js | 5 - src/game-lib-api-controls-touch.js | 15 +- src/game-lib-d3-api-light-a.js | 95 ++++++++ src/game-lib-d3-api-light-ambient.js | 29 +++ src/game-lib-d3-api-light-directional.js | 60 +++++ src/game-lib-d3-api-light-hemisphere.js | 53 +++++ src/game-lib-d3-api-light-point.js | 73 ++++++ src/game-lib-d3-api-light-rect-area.js | 87 ++++++++ src/game-lib-d3-api-light-spot.js | 101 +++++++++ src/game-lib-d3-api-light.js | 173 --------------- src/game-lib-d3-helper.js | 6 +- src/game-lib-d3-light-a.js | 118 ++++++++++ src/game-lib-d3-light-ambient.js | 64 ++++++ src/game-lib-d3-light-directional.js | 142 ++++++++++++ src/game-lib-d3-light-hemisphere.js | 119 ++++++++++ src/game-lib-d3-light-point.js | 130 +++++++++++ src/game-lib-d3-light-rect-area.js | 157 +++++++++++++ src/game-lib-d3-light-spot.js | 184 +++++++++++++++ src/game-lib-d3-light.js | 270 ----------------------- src/game-lib-system-gui.js | 10 +- src/game-lib-vector3.js | 17 +- 24 files changed, 1491 insertions(+), 483 deletions(-) create mode 100644 src/game-lib-d3-api-light-a.js create mode 100644 src/game-lib-d3-api-light-ambient.js create mode 100644 src/game-lib-d3-api-light-directional.js create mode 100644 src/game-lib-d3-api-light-hemisphere.js create mode 100644 src/game-lib-d3-api-light-point.js create mode 100644 src/game-lib-d3-api-light-rect-area.js create mode 100644 src/game-lib-d3-api-light-spot.js delete mode 100644 src/game-lib-d3-api-light.js create mode 100644 src/game-lib-d3-light-a.js create mode 100644 src/game-lib-d3-light-ambient.js create mode 100644 src/game-lib-d3-light-directional.js create mode 100644 src/game-lib-d3-light-hemisphere.js create mode 100644 src/game-lib-d3-light-point.js create mode 100644 src/game-lib-d3-light-rect-area.js create mode 100644 src/game-lib-d3-light-spot.js delete mode 100644 src/game-lib-d3-light.js diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index 98f2944..1b301c4 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -212,7 +212,7 @@ GameLib.Component.CAMERA = 0x5; GameLib.Component.SOCKET = 0x6; GameLib.Component.MESH = 0x7; GameLib.Component.SPLINE = 0x8; -GameLib.Component.LIGHT = 0x9; +//GameLib.Component.LIGHT = 0x9; GameLib.Component.PLANE = 0xa; GameLib.Component.COMPOSER = 0xb; GameLib.Component.RENDER_TARGET = 0xc; @@ -277,6 +277,12 @@ GameLib.Component.SYSTEM_PHYSICS = 0x46; GameLib.Component.SYSTEM_RENDER = 0x47; GameLib.Component.SYSTEM_STORAGE = 0x48; GameLib.Component.SYSTEM_VISUALIZATION = 0x49; +GameLib.Component.LIGHT_AMBIENT = 0x4a; +GameLib.Component.LIGHT_DIRECTIONAL = 0x4b; +GameLib.Component.LIGHT_HEMISPHERE = 0x4c; +GameLib.Component.LIGHT_POINT = 0x4d; +GameLib.Component.LIGHT_RECT_AREA = 0x4e; +GameLib.Component.LIGHT_SPOT = 0x4f; GameLib.Component.FOG = 0x50; GameLib.Component.MESH_LINE = 0x51; GameLib.Component.PARTICLE_ENGINE = 0x52; @@ -739,6 +745,42 @@ GameLib.Component.GetComponentInfo = function(number) { constructor : GameLib.System.Visualization, apiConstructor : GameLib.API.System }; + case 0x4a : return { + name : 'GameLib.D3.Light.Ambient', + runtime : GameLib.Component.GRAPHICS_RUNTIME, + constructor : GameLib.D3.Light.Ambient, + apiConstructor : GameLib.D3.API.Light.Ambient + }; + case 0x4b : return { + name : 'GameLib.D3.Light.Directional', + runtime : GameLib.Component.GRAPHICS_RUNTIME, + constructor : GameLib.D3.Light.Directional, + apiConstructor : GameLib.D3.API.Light.Directional + }; + case 0x4c : return { + name : 'GameLib.D3.Light.Hemisphere', + runtime : GameLib.Component.GRAPHICS_RUNTIME, + constructor : GameLib.D3.Light.Hemisphere, + apiConstructor : GameLib.D3.API.Light.Hemisphere + }; + case 0x4d : return { + name : 'GameLib.D3.Light.Point', + runtime : GameLib.Component.GRAPHICS_RUNTIME, + constructor : GameLib.D3.Light.Point, + apiConstructor : GameLib.D3.API.Light.Point + }; + case 0x4e : return { + name : 'GameLib.D3.Light.RectArea', + runtime : GameLib.Component.GRAPHICS_RUNTIME, + constructor : GameLib.D3.Light.RectArea, + apiConstructor : GameLib.D3.API.Light.RectArea + }; + case 0x4f : return { + name : 'GameLib.D3.Light.Spot', + runtime : GameLib.Component.GRAPHICS_RUNTIME, + constructor : GameLib.D3.Light.Spot, + apiConstructor : GameLib.D3.API.Light.Spot + }; case 0x50 : return { name : 'GameLib.D3.Fog', runtime : GameLib.Component.GRAPHICS_RUNTIME, diff --git a/src/game-lib-api-controls-d3-editor.js b/src/game-lib-api-controls-d3-editor.js index ff61c91..86cbe61 100644 --- a/src/game-lib-api-controls-d3-editor.js +++ b/src/game-lib-api-controls-d3-editor.js @@ -16,15 +16,6 @@ GameLib.API.Controls.D3.Editor = function( }; } - GameLib.API.Controls.call( - this, - apiControls.id, - apiControls.name, - apiControls.controlsType, - apiControls.canvas, - apiControls.parentEntity - ); - if (GameLib.Utils.UndefinedOrNull(raycaster)) { raycaster = new GameLib.D3.API.Raycaster(); } @@ -35,9 +26,13 @@ GameLib.API.Controls.D3.Editor = function( } this.camera = camera; - GameLib.API.Component.call( + GameLib.API.Controls.call( this, - GameLib.Component.CONTROLS_EDITOR + apiControls.id, + apiControls.name, + apiControls.controlsType, + apiControls.canvas, + apiControls.parentEntity ); }; diff --git a/src/game-lib-api-controls-keyboard.js b/src/game-lib-api-controls-keyboard.js index 7477e83..158f143 100644 --- a/src/game-lib-api-controls-keyboard.js +++ b/src/game-lib-api-controls-keyboard.js @@ -20,11 +20,6 @@ GameLib.API.Controls.Keyboard = function( apiControls.canvas, apiControls.parentEntity ); - - GameLib.API.Component.call( - this, - GameLib.Component.CONTROLS_KEYBOARD - ); }; GameLib.API.Controls.Keyboard.prototype = Object.create(GameLib.API.Controls.prototype); diff --git a/src/game-lib-api-controls-mouse.js b/src/game-lib-api-controls-mouse.js index 361c489..b24309e 100644 --- a/src/game-lib-api-controls-mouse.js +++ b/src/game-lib-api-controls-mouse.js @@ -20,11 +20,6 @@ GameLib.API.Controls.Mouse = function( apiControls.canvas, apiControls.parentEntity ); - - GameLib.API.Component.call( - this, - GameLib.Component.CONTROLS_MOUSE - ); }; GameLib.API.Controls.Mouse.prototype = Object.create(GameLib.API.Controls.prototype); diff --git a/src/game-lib-api-controls-touch.js b/src/game-lib-api-controls-touch.js index c16ecbe..7572b02 100644 --- a/src/game-lib-api-controls-touch.js +++ b/src/game-lib-api-controls-touch.js @@ -14,6 +14,11 @@ GameLib.API.Controls.Touch = function( }; } + if (GameLib.Utils.UndefinedOrNull(sensitivity)) { + sensitivity = 5; + } + this.sensitivity = sensitivity; + GameLib.API.Controls.call( this, apiControls.id, @@ -22,16 +27,6 @@ GameLib.API.Controls.Touch = function( apiControls.canvas, apiControls.parentEntity ); - - if (GameLib.Utils.UndefinedOrNull(sensitivity)) { - sensitivity = 5; - } - this.sensitivity = sensitivity; - - GameLib.API.Component.call( - this, - GameLib.Component.CONTROLS_TOUCH - ); }; GameLib.API.Controls.Touch.prototype = Object.create(GameLib.API.Controls.prototype); diff --git a/src/game-lib-d3-api-light-a.js b/src/game-lib-d3-api-light-a.js new file mode 100644 index 0000000..d881040 --- /dev/null +++ b/src/game-lib-d3-api-light-a.js @@ -0,0 +1,95 @@ +/** + * Raw Light API object - should always correspond with the Light Schema + * @param id + * @param lightType + * @param name + * @param color + * @param intensity + * @param parentScene + * @param parentEntity + * @constructor + */ +GameLib.D3.API.Light = function( + id, + name, + lightType, + color, + intensity, + parentScene, + parentEntity +) { + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Light (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(lightType)) { + lightType = GameLib.D3.API.Light.LIGHT_TYPE_AMBIENT; + } + this.lightType = lightType; + + if (GameLib.Utils.UndefinedOrNull(color)) { + color = new GameLib.API.Color(1,1,1); + } + this.color = color; + + if (GameLib.Utils.UndefinedOrNull(intensity)) { + intensity = 1; + } + this.intensity = intensity; + + if (GameLib.Utils.UndefinedOrNull(parentScene)) { + parentScene = null; + } + this.parentScene = parentScene; + + var componentType = null; + + switch (this.lightType) { + case GameLib.D3.API.Light.LIGHT_TYPE_AMBIENT : + componentType = GameLib.Component.LIGHT_AMBIENT; + break; + case GameLib.D3.API.Light.LIGHT_TYPE_DIRECTIONAL : + componentType = GameLib.Component.LIGHT_DIRECTIONAL; + break; + case GameLib.D3.API.Light.LIGHT_TYPE_POINT : + componentType = GameLib.Component.LIGHT_POINT; + break; + case GameLib.D3.API.Light.LIGHT_TYPE_SPOT : + componentType = GameLib.Component.LIGHT_SPOT; + break; + case GameLib.D3.API.Light.LIGHT_TYPE_HEMISPHERE : + componentType = GameLib.Component.LIGHT_HEMISPHERE; + break; + case GameLib.D3.API.Light.LIGHT_TYPE_RECT_AREA : + componentType = GameLib.Component.LIGHT_RECT_AREA; + break; + default : + console.error('could not determine light component type'); + } + + GameLib.API.Component.call( + this, + componentType, + parentEntity + ); +}; + +GameLib.D3.API.Light.prototype = Object.create(GameLib.API.Component.prototype); +GameLib.D3.API.Light.prototype.constructor = GameLib.D3.API.Light; + +/** + * Light Types + * @type {number} + */ +GameLib.D3.API.Light.LIGHT_TYPE_AMBIENT = 0x1; +GameLib.D3.API.Light.LIGHT_TYPE_DIRECTIONAL = 0x2; +GameLib.D3.API.Light.LIGHT_TYPE_POINT = 0x3; +GameLib.D3.API.Light.LIGHT_TYPE_SPOT = 0x4; +GameLib.D3.API.Light.LIGHT_TYPE_HEMISPHERE = 0x5; +GameLib.D3.API.Light.LIGHT_TYPE_RECT_AREA = 0x6; \ No newline at end of file diff --git a/src/game-lib-d3-api-light-ambient.js b/src/game-lib-d3-api-light-ambient.js new file mode 100644 index 0000000..8ae921f --- /dev/null +++ b/src/game-lib-d3-api-light-ambient.js @@ -0,0 +1,29 @@ +/** + * Raw Light API object - should always correspond with the Light Schema + * @constructor + * @param apiLight + */ +GameLib.D3.API.Light.Ambient = function( + apiLight +) { + + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_AMBIENT + }; + } + + GameLib.D3.API.Light.call( + this, + apiLight.id, + apiLight.name, + apiLight.lightType, + apiLight.color, + apiLight.intensity, + apiLight.parentScene, + apiLight.parentEntity + ); +}; + +GameLib.D3.API.Light.Ambient.prototype = Object.create(GameLib.D3.API.Light.prototype); +GameLib.D3.API.Light.Ambient.prototype.constructor = GameLib.D3.API.Light.Ambient; \ No newline at end of file diff --git a/src/game-lib-d3-api-light-directional.js b/src/game-lib-d3-api-light-directional.js new file mode 100644 index 0000000..7f4bb50 --- /dev/null +++ b/src/game-lib-d3-api-light-directional.js @@ -0,0 +1,60 @@ +/** + * Raw Light API object - should always correspond with the Light Schema + * @constructor + * @param apiLight + * @param castShadow + * @param position + * @param shadow + * @param target + */ +GameLib.D3.API.Light.Directional = function( + apiLight, + castShadow, + position, + shadow, + target +) { + + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_DIRECTIONAL + }; + } + + if (GameLib.Utils.UndefinedOrNull(castShadow)) { + castShadow = false; + } + this.castShadow = castShadow; + + /** + * Light shines from the top + */ + if (GameLib.Utils.UndefinedOrNull(position)) { + position = new GameLib.API.Vector3(0,1,0); + } + this.position = position; + + if (GameLib.Utils.UndefinedOrNull(shadow)) { + shadow = null; + } + this.shadow = shadow; + + if (GameLib.Utils.UndefinedOrNull(target)) { + target = null; + } + this.target = target; + + GameLib.D3.API.Light.call( + this, + apiLight.id, + apiLight.name, + apiLight.lightType, + apiLight.color, + apiLight.intensity, + apiLight.parentScene, + apiLight.parentEntity + ); +}; + +GameLib.D3.API.Light.Directional.prototype = Object.create(GameLib.D3.API.Light.prototype); +GameLib.D3.API.Light.Directional.prototype.constructor = GameLib.D3.API.Light.Directional; \ No newline at end of file diff --git a/src/game-lib-d3-api-light-hemisphere.js b/src/game-lib-d3-api-light-hemisphere.js new file mode 100644 index 0000000..0cc0110 --- /dev/null +++ b/src/game-lib-d3-api-light-hemisphere.js @@ -0,0 +1,53 @@ +/** + * Raw Light API object - should always correspond with the Light Schema + * @constructor + * @param apiLight + * @param castShadow + * @param position + * @param groundColor + */ +GameLib.D3.API.Light.Hemisphere = function( + apiLight, + castShadow, + position, + groundColor +) { + + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_HEMISPHERE + }; + } + + if (GameLib.Utils.UndefinedOrNull(castShadow)) { + castShadow = false; + } + this.castShadow = castShadow; + + /** + * Light shines from the top + */ + if (GameLib.Utils.UndefinedOrNull(position)) { + position = new GameLib.API.Vector3(0,1,0); + } + this.position = position; + + if (GameLib.Utils.UndefinedOrNull(groundColor)) { + groundColor = new GameLib.API.Color(1,1,1); + } + this.groundColor = groundColor; + + GameLib.D3.API.Light.call( + this, + apiLight.id, + apiLight.name, + apiLight.lightType, + apiLight.color, + apiLight.intensity, + apiLight.parentScene, + apiLight.parentEntity + ); +}; + +GameLib.D3.API.Light.Hemisphere.prototype = Object.create(GameLib.D3.API.Light.prototype); +GameLib.D3.API.Light.Hemisphere.prototype.constructor = GameLib.D3.API.Light.Hemisphere; \ No newline at end of file diff --git a/src/game-lib-d3-api-light-point.js b/src/game-lib-d3-api-light-point.js new file mode 100644 index 0000000..d74371a --- /dev/null +++ b/src/game-lib-d3-api-light-point.js @@ -0,0 +1,73 @@ +/** + * Raw Light API object - should always correspond with the Light Schema + * @constructor + * @param apiLight + * @param position + * @param decay + * @param distance + * @param power + * @param shadow + */ +GameLib.D3.API.Light.Point = function( + apiLight, + position, + decay, + distance, + power, + shadow +) { + + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_POINT + }; + } + + /** + * Light shines from the top + */ + if (GameLib.Utils.UndefinedOrNull(position)) { + position = new GameLib.API.Vector3(0,0,0); + } + this.position = position; + + /** + * In physically correct mode, decay is 2 + */ + if (GameLib.Utils.UndefinedOrNull(decay)) { + decay = 1; + } + this.decay = decay; + + /** + * In physically correct mode, decay is 2 + */ + if (GameLib.Utils.UndefinedOrNull(distance)) { + distance = 0; + } + this.distance = distance; + + if (GameLib.Utils.UndefinedOrNull(power)) { + power = 4 * Math.PI; + } + this.power = power; + + if (GameLib.Utils.UndefinedOrNull(shadow)) { + shadow = null; + } + this.shadow = shadow; + + GameLib.D3.API.Light.call( + this, + apiLight.id, + apiLight.name, + apiLight.lightType, + apiLight.color, + apiLight.intensity, + apiLight.parentScene, + apiLight.parentEntity + ); +}; + +GameLib.D3.API.Light.Point.prototype = Object.create(GameLib.D3.API.Light.prototype); +GameLib.D3.API.Light.Point.prototype.constructor = GameLib.D3.API.Light.Point; \ No newline at end of file diff --git a/src/game-lib-d3-api-light-rect-area.js b/src/game-lib-d3-api-light-rect-area.js new file mode 100644 index 0000000..0fdeb41 --- /dev/null +++ b/src/game-lib-d3-api-light-rect-area.js @@ -0,0 +1,87 @@ +/** + * Raw Light API object - should always correspond with the Light Schema + * @constructor + * @param apiLight + * @param position + * @param castShadow + * @param decay + * @param distance + * @param width + * @param height + * @param target + */ +GameLib.D3.API.Light.RectArea = function( + apiLight, + position, + castShadow, + decay, + distance, + width, + height, + target +) { + + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_RECT_AREA + }; + } + + /** + * Light shines from the top + */ + if (GameLib.Utils.UndefinedOrNull(position)) { + position = new GameLib.API.Vector3(0,1,0); + } + this.position = position; + + if (GameLib.Utils.UndefinedOrNull(castShadow)) { + castShadow = false; + } + this.castShadow = castShadow; + + /** + * In physically correct mode, decay is 2 + */ + if (GameLib.Utils.UndefinedOrNull(decay)) { + decay = 1; + } + this.decay = decay; + + /** + * In physically correct mode, decay is 2 + */ + if (GameLib.Utils.UndefinedOrNull(distance)) { + distance = 0; + } + this.distance = distance; + + if (GameLib.Utils.UndefinedOrNull(width)) { + width = 10; + } + this.width = width; + + if (GameLib.Utils.UndefinedOrNull(height)) { + height = 10; + } + this.height = height; + + if (GameLib.Utils.UndefinedOrNull(target)) { + target = null; + } + this.target = target; + + GameLib.D3.API.Light.call( + this, + apiLight.id, + apiLight.name, + apiLight.lightType, + apiLight.color, + apiLight.intensity, + apiLight.parentScene, + apiLight.parentEntity + ); +}; + +GameLib.D3.API.Light.RectArea.prototype = Object.create(GameLib.D3.API.Light.prototype); +GameLib.D3.API.Light.RectArea.prototype.constructor = GameLib.D3.API.Light.RectArea; \ No newline at end of file diff --git a/src/game-lib-d3-api-light-spot.js b/src/game-lib-d3-api-light-spot.js new file mode 100644 index 0000000..972ae90 --- /dev/null +++ b/src/game-lib-d3-api-light-spot.js @@ -0,0 +1,101 @@ +/** + * Raw Light API object - should always correspond with the Light Schema + * @constructor + * @param apiLight + * @param position + * @param angle + * @param castShadow + * @param decay + * @param distance + * @param penumbra + * @param power + * @param shadow + * @param target + */ +GameLib.D3.API.Light.Spot = function( + apiLight, + position, + angle, + castShadow, + decay, + distance, + penumbra, + power, + shadow, + target +) { + + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_SPOT + }; + } + + /** + * Light shines from the top + */ + if (GameLib.Utils.UndefinedOrNull(position)) { + position = new GameLib.API.Vector3(0,1,0); + } + this.position = position; + + /** + * angle should be no more than Math.PI / 2 + */ + if (GameLib.Utils.UndefinedOrNull(angle)) { + angle = Math.PI / 3; + } + this.angle = angle; + + if (GameLib.Utils.UndefinedOrNull(castShadow)) { + castShadow = false; + } + this.castShadow = castShadow; + + /** + * In physically correct mode, decay is 2 + */ + if (GameLib.Utils.UndefinedOrNull(decay)) { + decay = 1; + } + this.decay = decay; + + if (GameLib.Utils.UndefinedOrNull(distance)) { + distance = 0; + } + this.distance = distance; + + if (GameLib.Utils.UndefinedOrNull(penumbra)) { + penumbra = 0; + } + this.penumbra = penumbra; + + if (GameLib.Utils.UndefinedOrNull(power)) { + power = Math.PI * 4; + } + this.power = power; + + if (GameLib.Utils.UndefinedOrNull(shadow)) { + shadow = null; + } + this.shadow = shadow; + + if (GameLib.Utils.UndefinedOrNull(target)) { + target = null; + } + this.target = target; + + GameLib.D3.API.Light.call( + this, + apiLight.id, + apiLight.name, + apiLight.lightType, + apiLight.color, + apiLight.intensity, + apiLight.parentScene, + apiLight.parentEntity + ); +}; + +GameLib.D3.API.Light.Spot.prototype = Object.create(GameLib.D3.API.Light.prototype); +GameLib.D3.API.Light.Spot.prototype.constructor = GameLib.D3.API.Light.Spot; \ No newline at end of file diff --git a/src/game-lib-d3-api-light.js b/src/game-lib-d3-api-light.js deleted file mode 100644 index f0fea43..0000000 --- a/src/game-lib-d3-api-light.js +++ /dev/null @@ -1,173 +0,0 @@ -/** - * Raw Light API object - should always correspond with the Light Schema - * @param id - * @param lightType - * @param name - * @param color - * @param intensity - * @param position - * @param targetPosition - * @param quaternion - * @param rotation - * @param scale - * @param distance - * @param decay - * @param power - * @param angle - * @param penumbra - * @param parentScene - * @param parentEntity - * @constructor - */ -GameLib.D3.API.Light = function( - id, - lightType, - name, - color, - intensity, - position, - targetPosition, - quaternion, - rotation, - scale, - distance, - decay, - power, - angle, - penumbra, - parentScene, - parentEntity -) { - if (GameLib.Utils.UndefinedOrNull(id)) { - id = GameLib.Utils.RandomId(); - } - this.id = id; - - if (GameLib.Utils.UndefinedOrNull(lightType)) { - lightType = GameLib.D3.Light.LIGHT_TYPE_AMBIENT; - } - this.lightType = lightType; - - if (GameLib.Utils.UndefinedOrNull(name)) { - - if (this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { - name = 'Ambient '; - } - - if (this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { - name = 'Directional '; - } - - if (this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT) { - name = 'Point '; - } - - if (this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT) { - name = 'Spot '; - } - - name += 'Light (' + id + ')'; - } - this.name = name; - - if (GameLib.Utils.UndefinedOrNull(color)) { - color = new GameLib.API.Color(1,1,1,1); - } - this.color = color; - - if (GameLib.Utils.UndefinedOrNull(intensity)) { - intensity = 1; - } - this.intensity = intensity; - - if (GameLib.Utils.UndefinedOrNull(position)) { - position = new GameLib.API.Vector3(10,10,10); - } - this.position = position; - - if (GameLib.Utils.UndefinedOrNull(targetPosition)) { - targetPosition = new GameLib.API.Vector3(0,0,0); - } - this.targetPosition = targetPosition; - - if (GameLib.Utils.UndefinedOrNull(quaternion)){ - quaternion = new GameLib.API.Quaternion(); - } - 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); - } - this.scale = scale; - - if (GameLib.Utils.UndefinedOrNull(distance)){ - distance = 0; - } - this.distance = distance; - - if (GameLib.Utils.UndefinedOrNull(decay)){ - decay = 1; - } - this.decay = decay; - - if (GameLib.Utils.UndefinedOrNull(power)){ - power = 4 * Math.PI; - } - this.power = power; - - if (GameLib.Utils.UndefinedOrNull(angle)){ - angle = Math.PI / 3; - } - this.angle = angle; - - if (GameLib.Utils.UndefinedOrNull(penumbra)){ - penumbra = 0; - } - this.penumbra = penumbra; - - if (GameLib.Utils.UndefinedOrNull(parentScene)){ - parentScene = null; - } - this.parentScene = parentScene; - - GameLib.API.Component.call( - this, - GameLib.Component.LIGHT, - parentEntity - ); -}; - -GameLib.D3.API.Light.prototype = Object.create(GameLib.API.Component.prototype); -GameLib.D3.API.Light.prototype.constructor = GameLib.D3.API.Light; - -/** - * Returns an API light from an Object light - * @param objectLight - * @constructor - */ -GameLib.D3.API.Light.FromObject = function(objectLight) { - return new GameLib.D3.API.Light( - objectLight.id, - objectLight.lightType, - objectLight.name, - GameLib.API.Color.FromObject(objectLight.color), - objectLight.intensity, - GameLib.API.Vector3.FromObject(objectLight.position), - GameLib.API.Vector3.FromObject(objectLight.targetPosition), - GameLib.API.Quaternion.FromObject(objectLight.quaternion), - GameLib.API.Vector3.FromObject(objectLight.rotation), - GameLib.API.Vector3.FromObject(objectLight.scale), - objectLight.distance, - objectLight.decay, - objectLight.power, - objectLight.angle, - objectLight.penumbra, - objectLight.parentScene, - objectLight.parentEntity - ); -}; diff --git a/src/game-lib-d3-helper.js b/src/game-lib-d3-helper.js index b95d469..dc27b5b 100644 --- a/src/game-lib-d3-helper.js +++ b/src/game-lib-d3-helper.js @@ -47,15 +47,15 @@ GameLib.D3.Helper = function( } if (object instanceof GameLib.D3.Light) { - if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { + if (object.lightType === GameLib.D3.API.Light.LIGHT_TYPE_DIRECTIONAL) { helperType = GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT; } - if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT) { + if (object.lightType === GameLib.D3.API.Light.LIGHT_TYPE_POINT) { helperType = GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT; } - if (object.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT) { + if (object.lightType === GameLib.D3.API.Light.LIGHT_TYPE_SPOT) { helperType = GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT; } } diff --git a/src/game-lib-d3-light-a.js b/src/game-lib-d3-light-a.js new file mode 100644 index 0000000..18a230e --- /dev/null +++ b/src/game-lib-d3-light-a.js @@ -0,0 +1,118 @@ +/** + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param graphics GameLib.GraphicsRuntime + * @param apiLight GameLib.D3.API.Light + * @constructor + */ +GameLib.D3.Light = function( + graphics, + apiLight +) { + + if (GameLib.Utils.UndefinedOrNull(apiLight)) { + apiLight = {}; + } + + GameLib.API.Controls.call( + this, + apiLight.id, + apiLight.name, + apiLight.lightType, + apiLight.color, + apiLight.intensity, + apiLight.parentScene, + apiLight.parentEntity + ); + + var linkedObjects = { + 'parentScene' : GameLib.D3.Scene + }; + + switch (this.componentType) { + + case GameLib.Component.LIGHT_DIRECTIONAL : + case GameLib.Component.LIGHT_SPOT : + linkedObjects.shadow = GameLib.D3.Shadow; + linkedObjects.target = GameLib.Component; + break; + case GameLib.Component.LIGHT_POINT : + linkedObjects.shadow = GameLib.D3.Shadow; + break; + case GameLib.Component.LIGHT_RECT_AREA : + linkedObjects.target = GameLib.Component; + break; + default: + break; + } + + GameLib.Component.call( + this, + linkedObjects, + delayed + ); + +}; + +GameLib.D3.Light.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.Light.prototype.constructor = GameLib.D3.Light; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.prototype.createInstance = function() { + + if (!this.instance) { + console.warn('call the specific light createInstance first before calling its parent'); + return; + } + + this.instance.color.set(this.color.toHex()); + + this.instance.intensity = this.intensity; + + GameLib.Component.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.prototype.updateInstance = function(property) { + + if (GameLib.Utils.UndefinedOrNull(property)) { + console.warn('no property for light: ' + this.name); + } + + if (property === 'color') { + this.instance.color.set(this.color.toHex()); + } + + if (property === 'intensity') { + this.instance.intensity = this.intensity; + } + + if (property === 'parentScene') { + console.warn('todo: implement parentScene change for light') + } + + if (property === 'parentEntity') { + console.warn('todo: implement parentEntity change for light') + } + +}; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.prototype.toApiObject = function() { + return new GameLib.D3.API.Light( + this.id, + this.name, + this.lightType, + this.color.toApiObject(), + this.intensity, + GameLib.Utils.IdOrNull(this.parentScene), + GameLib.Utils.IdOrNull(this.parentEntity) + ); +}; diff --git a/src/game-lib-d3-light-ambient.js b/src/game-lib-d3-light-ambient.js new file mode 100644 index 0000000..6c40881 --- /dev/null +++ b/src/game-lib-d3-light-ambient.js @@ -0,0 +1,64 @@ +/** + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param graphics GameLib.GraphicsRuntime + * @param apiAmbientLight + * @constructor + */ +GameLib.D3.Light.Ambient = function( + graphics, + apiAmbientLight +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiAmbientLight)) { + apiAmbientLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_AMBIENT + }; + } + + GameLib.D3.API.Light.Ambient.call( + this, + apiAmbientLight + ); + + GameLib.D3.Light.call( + this, + this.graphics, + apiAmbientLight + ); + +}; + +GameLib.D3.Light.Ambient.prototype = Object.create(GameLib.D3.Light.prototype); +GameLib.D3.Light.Ambient.prototype.constructor = GameLib.D3.Light.Ambient; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.Ambient.prototype.createInstance = function() { + + this.instance = new THREE.AmbientLight(); + + GameLib.D3.Light.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.Ambient.prototype.updateInstance = function(property) { + GameLib.D3.Light.prototype.updateInstance.call(this, property); +}; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.Ambient.prototype.toApiObject = function() { + + var apiLight = GameLib.D3.Light.prototype.toApiObject.call(this); + + return apiLight; +}; diff --git a/src/game-lib-d3-light-directional.js b/src/game-lib-d3-light-directional.js new file mode 100644 index 0000000..ea50468 --- /dev/null +++ b/src/game-lib-d3-light-directional.js @@ -0,0 +1,142 @@ +/** + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param graphics GameLib.GraphicsRuntime + * @param apiDirectionalLight + * @constructor + */ +GameLib.D3.Light.Directional = function( + graphics, + apiDirectionalLight +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiDirectionalLight)) { + apiDirectionalLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_DIRECTIONAL + }; + } + + GameLib.D3.API.Light.Directional.call( + this, + apiDirectionalLight, + apiDirectionalLight.castShadow, + apiDirectionalLight.position, + apiDirectionalLight.shadow, + apiDirectionalLight.target + ); + + this.position = new GameLib.Vector3( + this.graphics, + this.position, + this + ); + + if (this.shadow instanceof GameLib.D3.API.Shadow) { + this.shadow = new GameLib.D3.Shadow( + this.graphics, + this.shadow + ) + } + + if (this.target instanceof GameLib.API.Component) { + console.warn('todo: implement generic api component to runtime component here'); + } + + GameLib.D3.Light.call( + this, + this.graphics, + apiDirectionalLight + ); + +}; + +GameLib.D3.Light.Directional.prototype = Object.create(GameLib.D3.Light.prototype); +GameLib.D3.Light.Directional.prototype.constructor = GameLib.D3.Light.Directional; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.Directional.prototype.createInstance = function() { + + this.instance = new THREE.DirectionalLight(); + + this.instance.castShadow = this.castShadow; + + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + + if (this.shadow && this.shadow.instance) { + this.instance.shadow = this.shadow.instance; + } + + if (this.target && this.target.instance) { + this.instance.target = this.target.instance; + + if (this.parentScene && this.parentScene.instance) { + this.parentScene.addObject(this.target); + } + } + + GameLib.D3.Light.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.Directional.prototype.updateInstance = function(property, oldTarget) { + + if (property === 'castShadow') { + this.instance.castShadow = this.castShadow; + return; + } + + if (property === 'position') { + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + return; + } + + if (property === 'shadow') { + this.instance.shadow = this.shadow.instance; + return; + } + + if (property === 'target') { + + if (typeof oldTarget === 'undefined') { + console.warn('oldTarget undefined'); + } + + if (oldTarget) { + if (this.parentScene) { + this.parentScene.removeObject(oldTarget); + this.parentScene.addObject(this.target); + } + } + + return; + } + + GameLib.D3.Light.prototype.updateInstance.call(this, property); +}; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.Directional.prototype.toApiObject = function() { + + var apiDirectionalLight = GameLib.D3.Light.prototype.toApiObject.call(this); + + apiDirectionalLight.castShadow = this.castShadow; + apiDirectionalLight.position = this.position.toApiObject(); + apiDirectionalLight.shadow = GameLib.Utils.IdOrNull(this.shadow); + apiDirectionalLight.target = GameLib.Utils.IdOrNull(this.target); + + return apiDirectionalLight; +}; diff --git a/src/game-lib-d3-light-hemisphere.js b/src/game-lib-d3-light-hemisphere.js new file mode 100644 index 0000000..2dbc467 --- /dev/null +++ b/src/game-lib-d3-light-hemisphere.js @@ -0,0 +1,119 @@ +/** + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param graphics GameLib.GraphicsRuntime + * @param apiHemisphereLight + * @constructor + */ +GameLib.D3.Light.Hemisphere = function( + graphics, + apiHemisphereLight +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiHemisphereLight)) { + apiHemisphereLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_HEMISPHERE + }; + } + + GameLib.D3.API.Light.Hemisphere.call( + this, + apiHemisphereLight, + apiHemisphereLight.castShadow, + apiHemisphereLight.position, + apiHemisphereLight.groundColor + ); + + this.position = new GameLib.Vector3( + this.graphics, + this.position, + this + ); + + this.groundColor = new GameLib.Color( + this.graphics, + this.groundColor, + this + ); + + GameLib.D3.Light.call( + this, + this.graphics, + apiHemisphereLight + ); + +}; + +GameLib.D3.Light.Hemisphere.prototype = Object.create(GameLib.D3.Light.prototype); +GameLib.D3.Light.Hemisphere.prototype.constructor = GameLib.D3.Light.Hemisphere; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.Hemisphere.prototype.createInstance = function() { + + this.instance = new THREE.HemisphereLight(); + + this.instance.castShadow = this.castShadow; + + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + + if (this.shadow && this.shadow.instance) { + this.instance.shadow = this.shadow.instance; + } + + if (this.target && this.target.instance) { + this.instance.target = this.target.instance; + + if (this.parentScene && this.parentScene.instance) { + this.parentScene.addObject(this.target); + } + } + + GameLib.D3.Light.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.Hemisphere.prototype.updateInstance = function(property, oldTarget) { + + if (property === 'castShadow') { + this.instance.castShadow = this.castShadow; + return; + } + + if (property === 'position') { + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + return; + } + + if (property === 'groundColor') { + this.instance.groundColor.set(this.color.toHex()); + return; + } + + GameLib.D3.Light.prototype.updateInstance.call(this, property); +}; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.Hemisphere.prototype.toApiObject = function() { + + var apiHemisphereLight = GameLib.D3.Light.prototype.toApiObject.call(this); + + apiHemisphereLight.castShadow = this.castShadow; + apiHemisphereLight.position = this.position.toApiObject(); + apiHemisphereLight.groundColor = this.groundColor.toApiObject(); + + return apiHemisphereLight; +}; diff --git a/src/game-lib-d3-light-point.js b/src/game-lib-d3-light-point.js new file mode 100644 index 0000000..e20a9ec --- /dev/null +++ b/src/game-lib-d3-light-point.js @@ -0,0 +1,130 @@ +/** + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param graphics GameLib.GraphicsRuntime + * @param apiPointLight + * @constructor + */ +GameLib.D3.Light.Point = function( + graphics, + apiPointLight +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiPointLight)) { + apiPointLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_POINT + }; + } + + GameLib.D3.API.Light.Point.call( + this, + apiPointLight, + apiPointLight.position, + apiPointLight.decay, + apiPointLight.distance, + apiPointLight.power, + apiPointLight.shadow + ); + + this.position = new GameLib.Vector3( + this.graphics, + this.position, + this + ); + + if (this.shadow instanceof GameLib.D3.API.Shadow) { + this.shadow = new GameLib.D3.Shadow( + this.graphics, + this.shadow + ) + } + + GameLib.D3.Light.call( + this, + this.graphics, + apiPointLight + ); + +}; + +GameLib.D3.Light.Point.prototype = Object.create(GameLib.D3.Light.prototype); +GameLib.D3.Light.Point.prototype.constructor = GameLib.D3.Light.Point; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.Point.prototype.createInstance = function() { + + this.instance = new THREE.PointLight(); + + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + + this.instance.decay = this.decay; + this.instance.distance = this.distance; + this.instance.power = this.power; + + if (this.shadow && this.shadow.instance) { + this.instance.shadow = this.shadow.instance; + } + + GameLib.D3.Light.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.Point.prototype.updateInstance = function(property) { + + if (property === 'position') { + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + return; + } + + if (property === 'decay') { + this.instance.decay = this.decay; + return; + } + + if (property === 'distance') { + this.instance.distance = this.distance; + return; + } + + if (property === 'power') { + this.instance.power = this.power; + return; + } + + if (property === 'shadow') { + if (this.shadow && this.shadow.instance) { + this.instance.shadow = this.shadow.instance; + } + return; + } + + GameLib.D3.Light.prototype.updateInstance.call(this, property); +}; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.Point.prototype.toApiObject = function() { + + var apiPointLight = GameLib.D3.Light.prototype.toApiObject.call(this); + + apiPointLight.position = this.position.toApiObject(); + apiPointLight.decay = this.decay; + apiPointLight.distance = this.distance; + apiPointLight.power = this.power; + apiPointLight.shadow = GameLib.Utils.IdOrNull(this.shadow); + + return apiPointLight; +}; diff --git a/src/game-lib-d3-light-rect-area.js b/src/game-lib-d3-light-rect-area.js new file mode 100644 index 0000000..44c3d13 --- /dev/null +++ b/src/game-lib-d3-light-rect-area.js @@ -0,0 +1,157 @@ +/** + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param graphics GameLib.GraphicsRuntime + * @param apiRectAreaLight + * @constructor + */ +GameLib.D3.Light.RectArea = function( + graphics, + apiRectAreaLight +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiRectAreaLight)) { + apiRectAreaLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_RECT_AREA + }; + } + + GameLib.D3.API.Light.RectArea.call( + this, + apiRectAreaLight, + apiRectAreaLight.position, + apiRectAreaLight.castShadow, + apiRectAreaLight.decay, + apiRectAreaLight.distance, + apiRectAreaLight.width, + apiRectAreaLight.height, + apiRectAreaLight.target + ); + + this.position = new GameLib.Vector3( + this.graphics, + this.position, + this + ); + + if (this.target instanceof GameLib.API.Component) { + console.warn('todo: implement generic api component to runtime component here'); + } + + GameLib.D3.Light.call( + this, + this.graphics, + apiRectAreaLight + ); + +}; + +GameLib.D3.Light.RectArea.prototype = Object.create(GameLib.D3.Light.prototype); +GameLib.D3.Light.RectArea.prototype.constructor = GameLib.D3.Light.RectArea; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.RectArea.prototype.createInstance = function() { + + this.instance = new THREE.RectAreaLight(); + + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + + this.instance.castShadow = this.castShadow; + + this.instance.decay = this.decay; + this.instance.distance = this.distance; + + this.instance.width = this.width; + this.instance.height = this.height; + + if (this.target && this.target.instance) { + this.instance.target = this.target.instance; + + if (this.parentScene && this.parentScene.instance) { + this.parentScene.addObject(this.target); + } + } + GameLib.D3.Light.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.RectArea.prototype.updateInstance = function(property, oldTarget) { + + if (property === 'castShadow') { + this.instance.castShadow = this.castShadow; + return; + } + + if (property === 'position') { + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + return; + } + + if (property === 'decay') { + this.instance.decay = this.decay; + return; + } + + if (property === 'distance') { + this.instance.distance = this.distance; + return; + } + + if (property === 'width') { + this.instance.width = this.width; + return; + } + + if (property === 'height') { + this.instance.height = this.height; + return; + } + + if (property === 'target') { + + if (typeof oldTarget === 'undefined') { + console.warn('oldTarget undefined'); + } + + if (oldTarget) { + if (this.parentScene) { + this.parentScene.removeObject(oldTarget); + this.parentScene.addObject(this.target); + } + } + + return; + } + + GameLib.D3.Light.prototype.updateInstance.call(this, property); +}; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.RectArea.prototype.toApiObject = function() { + + var apiRectAreaLight = GameLib.D3.Light.prototype.toApiObject.call(this); + + apiRectAreaLight.position = this.position.toApiObject(); + apiRectAreaLight.castShadow = this.castShadow; + apiRectAreaLight.decay = this.decay; + apiRectAreaLight.distance = this.distance; + apiRectAreaLight.width = this.width; + apiRectAreaLight.height = this.height; + apiRectAreaLight.target = GameLib.Utils.IdOrNull(this.target); + + return apiRectAreaLight; +}; diff --git a/src/game-lib-d3-light-spot.js b/src/game-lib-d3-light-spot.js new file mode 100644 index 0000000..4829d06 --- /dev/null +++ b/src/game-lib-d3-light-spot.js @@ -0,0 +1,184 @@ +/** + * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created + * @param graphics GameLib.GraphicsRuntime + * @param apiSpotLight + * @constructor + */ +GameLib.D3.Light.Spot = function( + graphics, + apiSpotLight +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiSpotLight)) { + apiSpotLight = { + lightType : GameLib.D3.API.Light.LIGHT_TYPE_SPOT + }; + } + + GameLib.D3.API.Light.Spot.call( + this, + apiSpotLight, + apiSpotLight.position, + apiSpotLight.angle, + apiSpotLight.castShadow, + apiSpotLight.decay, + apiSpotLight.distance, + apiSpotLight.penumbra, + apiSpotLight.power, + apiSpotLight.shadow, + apiSpotLight.target + ); + + this.position = new GameLib.Vector3( + this.graphics, + this.position, + this + ); + + if (this.shadow instanceof GameLib.D3.API.Shadow) { + this.shadow = new GameLib.D3.Shadow( + this.graphics, + this.shadow + ) + } + + if (this.target instanceof GameLib.API.Component) { + console.warn('todo: implement generic api component to runtime component here'); + } + + GameLib.D3.Light.call( + this, + this.graphics, + apiSpotLight + ); + +}; + +GameLib.D3.Light.Spot.prototype = Object.create(GameLib.D3.Light.prototype); +GameLib.D3.Light.Spot.prototype.constructor = GameLib.D3.Light.Spot; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Light.Spot.prototype.createInstance = function() { + + this.instance = new THREE.SpotLight(); + + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + + this.instance.angle = this.angle; + this.instance.castShadow = this.castShadow; + this.instance.decay = this.decay; + this.instance.distance = this.distance; + this.instance.penumbra = this.penumbra; + this.instance.power = this.power; + + if (this.shadow && this.shadow.instance) { + this.instance.shadow = this.shadow.instance; + } + + if (this.target && this.target.instance) { + this.instance.target = this.target.instance; + + if (this.parentScene && this.parentScene.instance) { + this.parentScene.addObject(this.target); + } + } + + GameLib.D3.Light.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Light.Spot.prototype.updateInstance = function(property, oldTarget) { + + if (property === 'position') { + this.instance.position.x = this.position.x; + this.instance.position.y = this.position.y; + this.instance.position.z = this.position.z; + return; + } + + if (property === 'angle') { + this.instance.angle = this.angle; + return; + } + + if (property === 'castShadow') { + this.instance.castShadow = this.castShadow; + return; + } + + if (property === 'decay') { + this.instance.decay = this.decay; + return; + } + + if (property === 'distance') { + this.instance.distance = this.distance; + return; + } + + if (property === 'penumbra') { + this.instance.penumbra = this.penumbra; + return; + } + + if (property === 'power') { + this.instance.power = this.power; + return; + } + + if (property === 'shadow') { + if (this.shadow && this.shadow.instance) { + this.instance.shadow = this.shadow.instance; + } + return; + } + + if (property === 'target') { + + if (typeof oldTarget === 'undefined') { + console.warn('oldTarget undefined'); + } + + if (oldTarget) { + if (this.parentScene) { + this.parentScene.removeObject(oldTarget); + this.parentScene.addObject(this.target); + } + } + + return; + } + + GameLib.D3.Light.prototype.updateInstance.call(this, property); +}; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.Spot.prototype.toApiObject = function() { + + var apiSpotLight = GameLib.D3.Light.prototype.toApiObject.call(this); + + apiSpotLight.position = this.position.toApiObject(); + apiSpotLight.angle = this.angle; + apiSpotLight.castShadow = this.castShadow; + apiSpotLight.decay = this.decay; + apiSpotLight.distance = this.distance; + apiSpotLight.penumbra = this.penumbra; + apiSpotLight.power = this.power; + apiSpotLight.shadow = GameLib.Utils.IdOrNull(this.shadow); + apiSpotLight.target = GameLib.Utils.IdOrNull(this.target); + + return apiSpotLight; +}; diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js deleted file mode 100644 index 12e3045..0000000 --- a/src/game-lib-d3-light.js +++ /dev/null @@ -1,270 +0,0 @@ -/** - * Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created - * @param graphics GameLib.GraphicsRuntime - * @param apiLight GameLib.D3.API.Light - * @constructor - */ -GameLib.D3.Light = function( - graphics, - apiLight -) { - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - - if (GameLib.Utils.UndefinedOrNull(apiLight)) { - apiLight = {}; - } - - GameLib.D3.API.Light.call( - this, - apiLight.id, - apiLight.lightType, - apiLight.name, - apiLight.color, - apiLight.intensity, - apiLight.position, - apiLight.targetPosition, - apiLight.quaternion, - apiLight.rotation, - apiLight.scale, - apiLight.distance, - apiLight.decay, - apiLight.power, - apiLight.angle, - apiLight.penumbra, - apiLight.parentScene, - apiLight.parentEntity - ); - - this.color = new GameLib.Color( - graphics, - this.color, - this - ); - - this.position = new GameLib.Vector3( - graphics, - this.position, - this - ); - - this.targetPosition = new GameLib.Vector3( - graphics, - this.targetPosition, - this - ); - - this.scale = new GameLib.Vector3( - graphics, - this.scale, - this - ); - - this.quaternion = new GameLib.Quaternion( - graphics, - this.quaternion, - this - ); - - this.rotation = new GameLib.Vector3( - graphics, - this.rotation, - this - ); - - GameLib.Component.call( - this, - { - 'parentScene' : GameLib.D3.Scene - } - ); -}; - -GameLib.D3.Light.prototype = Object.create(GameLib.Component.prototype); -GameLib.D3.Light.prototype.constructor = GameLib.D3.Light; - -/** - * Light Types - * @type {number} - */ -GameLib.D3.Light.LIGHT_TYPE_AMBIENT = 0x1; -GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL = 0x2; -GameLib.D3.Light.LIGHT_TYPE_POINT = 0x3; -GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4; - -/** - * Creates a light instance - * @returns {*} - */ -GameLib.D3.Light.prototype.createInstance = function() { - - if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT || - this.lightType === 'AmbientLight' - ) { - this.instance = new THREE.AmbientLight( - this.color.instance, - this.intensity - ); - } else if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL || - this.lightType === 'DirectionalLight' - ) { - this.instance = new THREE.DirectionalLight( - this.color.instance, - this.intensity - ); - } else if ( - this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT || - this.lightType === 'PointLight' - ) { - 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.lightType === 'SpotLight' - ) { - 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 { - console.warn('unsupported light type: ' + this.lightType); - return; - } - - 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()); - - GameLib.Component.prototype.createInstance.call(this); -}; - -/** - * Updates the instance with the current state - */ -GameLib.D3.Light.prototype.updateInstance = function(property) { - - if (GameLib.Utils.UndefinedOrNull(property)) { - console.warn('no property for light: ' + this.name); - } - - if (property === 'lightType') { - this.parentScene.instance.remove(this.instance); - this.createInstance(); - this.parentScene.instance.add(this.instance); - } - - if (property === 'name') { - this.instance.name = this.name; - } - - if (property === 'position') { - this.instance.position.x = this.position.x; - this.instance.position.y = this.position.y; - this.instance.position.z = this.position.z; - } - - if (property === 'scale') { - this.instance.scale.x = this.scale.x; - this.instance.scale.y = this.scale.y; - this.instance.scale.z = this.scale.z; - } - - if (property === 'target') { - 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; - } - } - - if (property === 'quaternion') { - 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; - } - - if (property === 'intensity') { - this.instance.intensity = this.intensity; - } - - if (property === 'color') { - this.instance.color.set(this.color.toHex()); - } - - /** - * 'parentScene' is handled by LinkingSystem - */ -}; - -/** - * Converts a GameLib.D3.Light to a GameLib.D3.API.Light - * @returns {GameLib.D3.API.Light} - */ -GameLib.D3.Light.prototype.toApiObject = function() { - return new GameLib.D3.API.Light( - this.id, - this.lightType, - this.name, - this.color.toApiObject(), - this.intensity, - this.position.toApiObject(), - this.targetPosition.toApiObject(), - this.quaternion.toApiObject(), - this.rotation.toApiObject(), - this.scale.toApiObject(), - this.distance, - this.decay, - this.power, - this.angle, - this.penumbra, - GameLib.Utils.IdOrNull(this.parentScene), - GameLib.Utils.IdOrNull(this.parentEntity) - ); -}; - -/** - * Returns a new GameLib.D3.Light from a GameLib.D3.API.Light - * @param graphics GameLib.GraphicsRuntime - * @param objectLight GameLib.D3.API.Light - * @returns {GameLib.D3.Light} - */ -GameLib.D3.Light.FromObject = function(graphics, objectLight) { - - return new GameLib.D3.Light( - graphics, - GameLib.D3.API.Light.FromObject(objectLight) - ); - -}; \ No newline at end of file diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index 11076ca..f739fda 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -1392,10 +1392,12 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate, 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 + 'ambient': GameLib.D3.API.Light.LIGHT_TYPE_AMBIENT, + 'directional': GameLib.D3.API.Light.LIGHT_TYPE_DIRECTIONAL, + 'spot': GameLib.D3.API.Light.LIGHT_TYPE_SPOT, + 'point': GameLib.D3.API.Light.LIGHT_TYPE_POINT, + 'hemisphere': GameLib.D3.API.Light.LIGHT_TYPE_HEMISPHERE, + 'rect area': GameLib.D3.API.Light.LIGHT_TYPE_RECT_AREA } ) ); diff --git a/src/game-lib-vector3.js b/src/game-lib-vector3.js index 1717d4d..ccc31b8 100644 --- a/src/game-lib-vector3.js +++ b/src/game-lib-vector3.js @@ -4,13 +4,17 @@ * @param apiVector3 GameLib.API.Vector3 * @param parentObject GameLib.* * @param grain Number + * @param min + * @param max * @constructor */ GameLib.Vector3 = function ( implementation, apiVector3, parentObject, - grain + grain, + min, + max ) { this.implementation = implementation; @@ -48,6 +52,17 @@ GameLib.Vector3 = function ( } this.grain = grain; + if (GameLib.Utils.UndefinedOrNull(min)) { + min = 0; + } + this.min = min; + + if (GameLib.Utils.UndefinedOrNull(max)) { + max = 1; + } + this.max = max; + + this.createInstance(); };