From 46a00d4fb1fff6c0705d0dbc706210428910fbc7 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Thu, 1 Dec 2016 18:37:57 +0100 Subject: [PATCH] runtime vectors and colors complete --- src/game-lib-api-color.js | 6 +++ src/game-lib-api-vector2.js | 4 ++ src/game-lib-api-vector4.js | 6 +++ src/game-lib-helper.js | 91 +++++++++++++++++++++++++++++++++ src/game-lib-mesh.js | 17 +++--- src/game-lib-runtime-color.js | 78 ++++++++++++++++++++++++++++ src/game-lib-runtime-vector2.js | 74 +++++++++++++++++++++++++++ src/game-lib-runtime-vector3.js | 2 +- src/game-lib-runtime-vector4.js | 78 ++++++++++++++++++++++++++++ src/game-lib-scene.js | 41 ++++++++++++--- src/game-lib-texture.js | 2 + 11 files changed, 383 insertions(+), 16 deletions(-) create mode 100644 src/game-lib-api-color.js create mode 100644 src/game-lib-api-vector2.js create mode 100644 src/game-lib-api-vector4.js create mode 100644 src/game-lib-helper.js create mode 100644 src/game-lib-runtime-color.js create mode 100644 src/game-lib-runtime-vector2.js create mode 100644 src/game-lib-runtime-vector4.js diff --git a/src/game-lib-api-color.js b/src/game-lib-api-color.js new file mode 100644 index 0000000..aa75619 --- /dev/null +++ b/src/game-lib-api-color.js @@ -0,0 +1,6 @@ +GameLib.D3.API.Color = function Color(r, g, b, a) { + this.r = r || 1; + this.g = g || 1; + this.b = b || 1; + this.a = a || 0; +}; \ No newline at end of file diff --git a/src/game-lib-api-vector2.js b/src/game-lib-api-vector2.js new file mode 100644 index 0000000..d6e31bc --- /dev/null +++ b/src/game-lib-api-vector2.js @@ -0,0 +1,4 @@ +GameLib.D3.API.Vector2 = function Vector2(x, y) { + this.x = x || 0; + this.y = y || 0; +}; \ No newline at end of file diff --git a/src/game-lib-api-vector4.js b/src/game-lib-api-vector4.js new file mode 100644 index 0000000..054f4a6 --- /dev/null +++ b/src/game-lib-api-vector4.js @@ -0,0 +1,6 @@ +GameLib.D3.API.Vector4 = function Vector4(x, y, z, w) { + this.x = x || 0; + this.y = y || 0; + this.z = z || 0; + this.w = w || 1; +}; \ No newline at end of file diff --git a/src/game-lib-helper.js b/src/game-lib-helper.js new file mode 100644 index 0000000..2257e78 --- /dev/null +++ b/src/game-lib-helper.js @@ -0,0 +1,91 @@ +/** + * Helpers for displaying outlines or making 'invisible' scene objects visible + * @param id + * @param object GameLib.D3.Object + * @param name + * @param graphics + * @param helperType + * @constructor + */ +GameLib.D3.Helper = function Helper( + id, + object, + helperType, + name, + graphics +) { + if (GameLib.D3.Utils.UndefinedOrNull(id)) { + id = GameLib.D3.Tools.RandomId(); + } + this.id = id; + + if (GameLib.D3.Utils.UndefinedOrNull(object)) { + throw new Error('Cannot create helpers for unknown objects'); + } + this.object = object; + + if (GameLib.D3.Utils.UndefinedOrNull(helperType)) { + helperType = GameLib.D3.Helper.HELPER_TYPE_EDGES; + } + this.helperType = helperType; + + if (GameLib.D3.Utils.UndefinedOrNull(name)) { + name = 'Helper (' + this.helperType + ')'; + } + this.name = name; + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.instance = this.createInstance(); +}; + +/** + * Helper types + * @type {string} + */ +GameLib.D3.Helper.HELPER_TYPE_EDGES = 'edges'; +GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT = 'directional-light'; +GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT = 'spot-light'; +GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT = 'point-light'; +GameLib.D3.Helper.HELPER_TYPE_WIREFRAME = 'wireframe'; + + +/** + * Creates a helper instance + * @param update + */ +GameLib.D3.Helper.prototype.createInstance = function(update) { + + var instance = null; + + if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_EDGES) { + instance = new this.graphics.instance.EdgesHelper(this.object.instance, 0x007700); + } + + if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) { + instance = new this.graphics.instance.DirectionalLightHelper(this.object.instance); + } + + if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_POINT_LIGHT) { + instance = new this.graphics.instance.PointLightHelper(this.object.instance, 1); + } + + if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_SPOT_LIGHT) { + instance = new this.graphics.instance.SpotLightHelper(this.object.instance); + } + + if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_WIREFRAME) { + instance = new this.graphics.instance.WireframeHelper(this.object.instance, 0x007700); + } + + if (!instance) { + throw new Error('Unsupported helper type: ' + this.helperType); + } + + if (update) { + this.instance = instance; + } + + return instance; +}; diff --git a/src/game-lib-mesh.js b/src/game-lib-mesh.js index 930b1c3..79498e5 100644 --- a/src/game-lib-mesh.js +++ b/src/game-lib-mesh.js @@ -299,18 +299,19 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.position.y = this.position.y; instance.position.z = this.position.z; - 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.quaternion.x = this.quaternion.x; - // instance.quaternion.y = this.quaternion.y; - // instance.quaternion.z = this.quaternion.z; - // instance.quaternion.w = this.quaternion.w; + // we don't do rotation since it interferes with the quaternion update + // instance.rotation.x = this.rotation.x; + // instance.rotation.y = this.rotation.y; + // instance.rotation.z = this.rotation.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; return instance; }; diff --git a/src/game-lib-runtime-color.js b/src/game-lib-runtime-color.js new file mode 100644 index 0000000..2052812 --- /dev/null +++ b/src/game-lib-runtime-color.js @@ -0,0 +1,78 @@ +/** + * Runtime color for updating instance objects + * @param graphics GameLib.D3.Graphics + * @param parentObject GameLib.D3.* + * @param color GameLib.D3.Color + * @param grain Number + * @constructor + */ +GameLib.D3.Runtime.Color = function RuntimeColor(graphics, parentObject, color, grain) { + + for (var property in color) { + if (color.hasOwnProperty(property)) { + this[property] = color[property]; + } + } + + GameLib.D3.Utils.Extend(GameLib.D3.Runtime.Color, GameLib.D3.Color); + + this.graphics = graphics; + + this.graphics.isNotThreeThrow(); + + this.parentObject = parentObject; + + if (GameLib.D3.Utils.UndefinedOrNull(grain)) { + grain = 0.001; + } + this.grain = grain; + + this.instance = this.createInstance(); +}; + +/** + * Creates an instance color + * @param update + * @returns {*} + */ +GameLib.D3.Runtime.Color.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + instance.r = this.r; + instance.g = this.g; + instance.b = this.b; + instance.a = this.a; + } else { + instance = new this.graphics.instance.Color(this.r, this.g, this.b, this.a); + } + + return instance; +}; + +/** + * Updates the instance color, calls updateInstance on the parent object + */ +GameLib.D3.Runtime.Color.prototype.updateInstance = function() { + + this.createInstance(true); + + if (this.parentObject.updateInstance) { + this.parentObject.updateInstance(); + } +}; + +/** + * Converts runtime color to API Color + * @returns {GameLib.D3.API.Color} + */ +GameLib.D3.Runtime.Color.prototype.toApiColor = function() { + return new GameLib.D3.API.Color( + this.r, + this.g, + this.b, + this.a + ); +}; \ No newline at end of file diff --git a/src/game-lib-runtime-vector2.js b/src/game-lib-runtime-vector2.js new file mode 100644 index 0000000..d10db60 --- /dev/null +++ b/src/game-lib-runtime-vector2.js @@ -0,0 +1,74 @@ +/** + * Runtime vector2 for updating instance objects + * @param graphics GameLib.D3.Graphics + * @param parentObject GameLib.D3.* + * @param vector2 GameLib.D3.Vector2 + * @param grain Number + * @constructor + */ +GameLib.D3.Runtime.Vector2 = function RuntimeVector2(graphics, parentObject, vector2, grain) { + + for (var property in vector2) { + if (vector2.hasOwnProperty(property)) { + this[property] = vector2[property]; + } + } + + GameLib.D3.Utils.Extend(GameLib.D3.Runtime.Vector2, GameLib.D3.Vector2); + + this.graphics = graphics; + + this.graphics.isNotThreeThrow(); + + this.parentObject = parentObject; + + if (GameLib.D3.Utils.UndefinedOrNull(grain)) { + grain = 0.001; + } + this.grain = grain; + + this.instance = this.createInstance(); +}; + +/** + * Creates an instance vector2 + * @param update + * @returns {*} + */ +GameLib.D3.Runtime.Vector2.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + instance.x = this.x; + instance.y = this.y; + } else { + instance = new this.graphics.instance.Vector2(this.x, this.y); + } + + return instance; +}; + +/** + * Updates the instance vector, calls updateInstance on the parent object + */ +GameLib.D3.Runtime.Vector2.prototype.updateInstance = function() { + + this.createInstance(true); + + if (this.parentObject.updateInstance) { + this.parentObject.updateInstance(); + } +}; + +/** + * Converts runtime vector to API Vector + * @returns {GameLib.D3.API.Vector2} + */ +GameLib.D3.Runtime.Vector2.prototype.toApiVector = function() { + return new GameLib.D3.API.Vector2( + this.x, + this.y + ); +}; \ No newline at end of file diff --git a/src/game-lib-runtime-vector3.js b/src/game-lib-runtime-vector3.js index 9349332..5568b5f 100644 --- a/src/game-lib-runtime-vector3.js +++ b/src/game-lib-runtime-vector3.js @@ -68,7 +68,7 @@ GameLib.D3.Runtime.Vector3.prototype.updateInstance = function() { * @returns {GameLib.D3.API.Vector3} */ GameLib.D3.Runtime.Vector3.prototype.toApiVector = function() { - return new GameLib.D3.Api.Vector3( + return new GameLib.D3.API.Vector3( this.x, this.y, this.z diff --git a/src/game-lib-runtime-vector4.js b/src/game-lib-runtime-vector4.js new file mode 100644 index 0000000..5a9b426 --- /dev/null +++ b/src/game-lib-runtime-vector4.js @@ -0,0 +1,78 @@ +/** + * Runtime vector4 for updating instance objects + * @param graphics GameLib.D3.Graphics + * @param parentObject GameLib.D3.* + * @param vector4 GameLib.D3.Vector4 + * @param grain Number + * @constructor + */ +GameLib.D3.Runtime.Vector4 = function RuntimeVector4(graphics, parentObject, vector4, grain) { + + for (var property in vector4) { + if (vector4.hasOwnProperty(property)) { + this[property] = vector4[property]; + } + } + + GameLib.D3.Utils.Extend(GameLib.D3.Runtime.Vector4, GameLib.D3.Vector4); + + this.graphics = graphics; + + this.graphics.isNotThreeThrow(); + + this.parentObject = parentObject; + + if (GameLib.D3.Utils.UndefinedOrNull(grain)) { + grain = 0.001; + } + this.grain = grain; + + this.instance = this.createInstance(); +}; + +/** + * Creates an instance vector4 + * @param update + * @returns {*} + */ +GameLib.D3.Runtime.Vector4.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + instance.x = this.x; + instance.y = this.y; + instance.z = this.z; + instance.w = this.w; + } else { + instance = new this.graphics.instance.Vector4(this.x, this.y, this.z, this.w); + } + + return instance; +}; + +/** + * Updates the instance vector, calls updateInstance on the parent object + */ +GameLib.D3.Runtime.Vector4.prototype.updateInstance = function() { + + this.createInstance(true); + + if (this.parentObject.updateInstance) { + this.parentObject.updateInstance(); + } +}; + +/** + * Converts runtime vector to API Vector + * @returns {GameLib.D3.API.Vector4} + */ +GameLib.D3.Runtime.Vector4.prototype.toApiVector = function() { + return new GameLib.D3.API.Vector4( + this.x, + this.y, + this.z, + this.w + ); +}; \ No newline at end of file diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index b41f942..1662eb6 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -278,7 +278,7 @@ GameLib.D3.Scene.LoadScene = function( var apiTexture = apiMaps[map].texture; - gameLibTextureMap[map].texture = new GameLib.D3.Texture( + var texture = new GameLib.D3.Texture( new GameLib.D3.API.Texture( apiTexture.id, map, @@ -313,6 +313,20 @@ GameLib.D3.Scene.LoadScene = function( gameLibTextureMap[map].instanceMapId, imageFactory ); + + texture.offset = new GameLib.D3.Runtime.Vector2( + graphics, + texture, + texture.offset + ); + + texture.repeat = new GameLib.D3.Runtime.Vector2( + graphics, + texture, + texture.repeat + ); + + gameLibTextureMap[map].texture = texture; } } @@ -399,18 +413,31 @@ GameLib.D3.Scene.LoadScene = function( gameLibMesh.position ); - gameLibMesh.rotation = new GameLib.D3.Runtime.Vector3( - graphics, - gameLibMesh, - gameLibMesh.rotation - ); - gameLibMesh.scale = new GameLib.D3.Runtime.Vector3( graphics, gameLibMesh, gameLibMesh.scale ); + gameLibMesh.up = new GameLib.D3.Runtime.Vector3( + graphics, + gameLibMesh, + gameLibMesh.up + ); + + // we don't do rotation since it interferes with the quaternion update + // gameLibMesh.rotation = new GameLib.D3.Runtime.Vector3( + // graphics, + // gameLibMesh, + // gameLibMesh.rotation + // ); + + gameLibMesh.quaternion = new GameLib.D3.Runtime.Vector4( + graphics, + gameLibMesh, + gameLibMesh.quaternion + ); + gameLibMeshes.push(gameLibMesh); meshIdToMesh[gameLibMesh.id] = gameLibMesh; diff --git a/src/game-lib-texture.js b/src/game-lib-texture.js index f32cc67..594d5df 100644 --- a/src/game-lib-texture.js +++ b/src/game-lib-texture.js @@ -163,6 +163,8 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { instance.flipY = this.flipY; instance.offset.x = this.offset.x; instance.offset.y = this.offset.y; + instance.repeat.x = this.repeat.x; + instance.repeat.y = this.repeat.y; instance.mipmaps = this.mipmaps; instance.unpackAlignment = this.unpackAlignment; instance.premultiplyAlpha = this.premultiplyAlpha;