From b543c586e409e31c8004e65fa5f19304e62da953 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Fri, 2 Dec 2016 16:03:03 +0100 Subject: [PATCH] huge refactorings - vector fixes --- src/game-lib-api-camera.js | 2 +- src/game-lib-api-color.js | 25 +- src/game-lib-api-component-interface.js | 2 +- src/game-lib-api-entity.js | 8 +- src/game-lib-api-light.js | 2 +- src/game-lib-api-material.js | 2 +- src/game-lib-api-mesh.js | 2 +- src/game-lib-api-scene.js | 3 +- src/game-lib-api-spline.js | 2 +- src/game-lib-api-texture.js | 2 +- src/game-lib-api-vector2.js | 7 + src/game-lib-camera.js | 26 +- ...lib-runtime-color.js => game-lib-color.js} | 26 +- src/game-lib-component-camera.js | 2 +- src/game-lib-component-colorflash.js | 2 +- src/game-lib-component-colorlerp.js | 2 +- src/game-lib-component-entity-parent.js | 2 +- src/game-lib-component-entity-permutation.js | 8 +- src/game-lib-component-fly-controls.js | 2 +- src/game-lib-component-follow.js | 2 +- src/game-lib-component-interface.js | 2 +- src/game-lib-component-look-at.js | 2 +- src/game-lib-component-mesh-permutation.js | 8 +- src/game-lib-component-offsettor.js | 6 +- src/game-lib-component-path-ai.js | 2 +- src/game-lib-component-path-controls.js | 2 +- src/game-lib-component-path-following.js | 10 +- ...-lib-component-raycast-vehicle-controls.js | 2 +- src/game-lib-component-rotator.js | 6 +- src/game-lib-component-trigger-box-box.js | 2 +- src/game-lib-component-trigger-box-sphere.js | 2 +- ...ame-lib-component-trigger-sphere-sphere.js | 2 +- ...b-component-vehicle-ai-object-avoidance.js | 2 +- ...-lib-component-vehicle-ai-path-steering.js | 2 +- src/game-lib-helper.js | 2 +- src/game-lib-image.js | 2 +- src/game-lib-light.js | 57 +-- src/game-lib-material.js | 59 +-- src/game-lib-mesh.js | 360 ++---------------- src/game-lib-raycast-vehicle.js | 2 +- src/game-lib-raycast-wheel.js | 2 +- src/game-lib-rigid-body-vehicle.js | 2 +- src/game-lib-rigid-body.js | 2 +- src/game-lib-rigid-wheel.js | 2 +- src/game-lib-scene.js | 70 ++-- src/game-lib-texture.js | 16 +- src/game-lib-tools.js | 10 - src/game-lib-utils.js | 306 +++++++++++++++ ...runtime-vector2.js => game-lib-vector2.js} | 63 ++- ...runtime-vector3.js => game-lib-vector3.js} | 88 ++--- ...runtime-vector4.js => game-lib-vector4.js} | 80 ++-- src/index.js | 94 ----- 52 files changed, 617 insertions(+), 779 deletions(-) rename src/{game-lib-runtime-color.js => game-lib-color.js} (75%) delete mode 100644 src/game-lib-tools.js rename src/{game-lib-runtime-vector2.js => game-lib-vector2.js} (59%) rename src/{game-lib-runtime-vector3.js => game-lib-vector3.js} (67%) rename src/{game-lib-runtime-vector4.js => game-lib-vector4.js} (78%) delete mode 100644 src/index.js diff --git a/src/game-lib-api-camera.js b/src/game-lib-api-camera.js index bd50549..ad10a53 100644 --- a/src/game-lib-api-camera.js +++ b/src/game-lib-api-camera.js @@ -35,7 +35,7 @@ GameLib.D3.API.Camera = function( maxZ ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-color.js b/src/game-lib-api-color.js index aa75619..5e58ab5 100644 --- a/src/game-lib-api-color.js +++ b/src/game-lib-api-color.js @@ -1,6 +1,23 @@ 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; + + if (GameLib.D3.Utils.UndefinedOrNull(r)) { + r = 1; + } + this.r = r; + + if (GameLib.D3.Utils.UndefinedOrNull(g)) { + g = 1; + } + this.g = g; + + if (GameLib.D3.Utils.UndefinedOrNull(b)) { + b = 1; + } + this.b = b; + + if (GameLib.D3.Utils.UndefinedOrNull(a)) { + a = 0; + } + this.a = a; + }; \ No newline at end of file diff --git a/src/game-lib-api-component-interface.js b/src/game-lib-api-component-interface.js index 8aa7bf6..344a865 100644 --- a/src/game-lib-api-component-interface.js +++ b/src/game-lib-api-component-interface.js @@ -79,7 +79,7 @@ GameLib.D3.API.ComponentInterface = function( maxSteerAngle ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-entity.js b/src/game-lib-api-entity.js index a9b0a1d..01c34b8 100644 --- a/src/game-lib-api-entity.js +++ b/src/game-lib-api-entity.js @@ -21,7 +21,7 @@ GameLib.D3.API.Entity = function Entity( mesh ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; @@ -88,17 +88,17 @@ GameLib.D3.Entity = function Entity( this.mesh = null; if(GameLib.D3.Utils.UndefinedOrNull(position)) { - position = new GameLib.D3.Vector3(0, 0, 0); + position = new GameLib.D3.API.Vector3(0, 0, 0); } this.position = position; if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) { - quaternion = new GameLib.D3.Vector4(0, 0, 0, 1); + quaternion = new GameLib.D3.API.Vector4(0, 0, 0, 1); } this.quaternion = quaternion; if(GameLib.D3.Utils.UndefinedOrNull(scale)) { - scale = new GameLib.D3.Vector3(1, 1, 1); + scale = new GameLib.D3.API.Vector3(1, 1, 1); } this.scale = scale; }; \ No newline at end of file diff --git a/src/game-lib-api-light.js b/src/game-lib-api-light.js index 68d1bc0..9fe89bd 100644 --- a/src/game-lib-api-light.js +++ b/src/game-lib-api-light.js @@ -35,7 +35,7 @@ GameLib.D3.API.Light = function( penumbra ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-material.js b/src/game-lib-api-material.js index 69a21d1..0f65e42 100644 --- a/src/game-lib-api-material.js +++ b/src/game-lib-api-material.js @@ -119,7 +119,7 @@ GameLib.D3.API.Material = function( envMapIntensity ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-mesh.js b/src/game-lib-api-mesh.js index bf582db..462a9b3 100644 --- a/src/game-lib-api-mesh.js +++ b/src/game-lib-api-mesh.js @@ -39,7 +39,7 @@ GameLib.D3.API.Mesh = function( up ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-scene.js b/src/game-lib-api-scene.js index f56f131..6af8cfc 100644 --- a/src/game-lib-api-scene.js +++ b/src/game-lib-api-scene.js @@ -16,6 +16,7 @@ * @param shapes GameLib.D3.API.Shape[] * @param cameras * @param activeCameraIndex + * @param splines GameLib.D3.API.Spline[] * @constructor */ GameLib.D3.API.Scene = function( @@ -38,7 +39,7 @@ GameLib.D3.API.Scene = function( splines ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-spline.js b/src/game-lib-api-spline.js index 1efd54b..cf163d6 100644 --- a/src/game-lib-api-spline.js +++ b/src/game-lib-api-spline.js @@ -8,7 +8,7 @@ GameLib.D3.API.Spline = function( vertices ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-texture.js b/src/game-lib-api-texture.js index 197fea4..84fc105 100644 --- a/src/game-lib-api-texture.js +++ b/src/game-lib-api-texture.js @@ -47,7 +47,7 @@ GameLib.D3.API.Texture = function( encoding ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-api-vector2.js b/src/game-lib-api-vector2.js index d6e31bc..7796935 100644 --- a/src/game-lib-api-vector2.js +++ b/src/game-lib-api-vector2.js @@ -1,4 +1,11 @@ GameLib.D3.API.Vector2 = function Vector2(x, y) { this.x = x || 0; this.y = y || 0; +}; + +GameLib.D3.API.Vector2.prototype.copy = function () { + return new GameLib.D3.API.Vector2( + this.x, + this.y + ); }; \ No newline at end of file diff --git a/src/game-lib-camera.js b/src/game-lib-camera.js index afb20ff..588fe95 100644 --- a/src/game-lib-camera.js +++ b/src/game-lib-camera.js @@ -18,13 +18,13 @@ GameLib.D3.Camera = function Camera( this.graphics.isNotThreeThrow(); - this.position = new GameLib.D3.Runtime.Vector3( + this.position = new GameLib.D3.Vector3( graphics, this, this.position ); - this.lookAt = new GameLib.D3.Runtime.Vector3( + this.lookAt = new GameLib.D3.Vector3( graphics, this, this.lookAt @@ -55,9 +55,7 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { instance = this.instance; } - if (!instance || - (instance && this.cameraType != instance.cameraType) - ) { + if (!instance) { if (this.cameraType == GameLib.D3.Camera.CAMERA_TYPE_PERSPECTIVE) { instance = new this.graphics.instance.PerspectiveCamera( this.fov, @@ -77,7 +75,7 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { } } - if (update && this.cameraType == instance.cameraType) { + if (update) { instance.minX = this.minX; instance.maxX = this.maxX; instance.minY = this.minY; @@ -90,21 +88,9 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { instance.far = this.far; } - if (update) { - instance.cameraType = this.cameraType; - } + instance.position.copy(this.position.instance); - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; - - instance.lookAt( - new this.graphics.instance.Vector3( - this.lookAt.x, - this.lookAt.y, - this.lookAt.z - ) - ); + instance.lookAt(this.lookAt.instance); instance.updateProjectionMatrix(); diff --git a/src/game-lib-runtime-color.js b/src/game-lib-color.js similarity index 75% rename from src/game-lib-runtime-color.js rename to src/game-lib-color.js index 1c85907..84035db 100644 --- a/src/game-lib-runtime-color.js +++ b/src/game-lib-color.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.D3.Runtime.Color = function RuntimeColor(graphics, parentObject, color, grain) { +GameLib.D3.Color = function RuntimeColor(graphics, parentObject, color, grain) { for (var property in color) { if (color.hasOwnProperty(property)) { @@ -14,7 +14,7 @@ GameLib.D3.Runtime.Color = function RuntimeColor(graphics, parentObject, color, } } - GameLib.D3.Utils.Extend(GameLib.D3.Runtime.Color, GameLib.D3.API.Color); + GameLib.D3.Utils.Extend(GameLib.D3.Color, GameLib.D3.API.Color); this.graphics = graphics; @@ -35,7 +35,7 @@ GameLib.D3.Runtime.Color = function RuntimeColor(graphics, parentObject, color, * @param update * @returns {*} */ -GameLib.D3.Runtime.Color.prototype.createInstance = function(update) { +GameLib.D3.Color.prototype.createInstance = function(update) { var instance = null; @@ -54,7 +54,7 @@ GameLib.D3.Runtime.Color.prototype.createInstance = function(update) { /** * Updates the instance color, calls updateInstance on the parent object */ -GameLib.D3.Runtime.Color.prototype.updateInstance = function() { +GameLib.D3.Color.prototype.updateInstance = function() { this.createInstance(true); @@ -67,7 +67,7 @@ GameLib.D3.Runtime.Color.prototype.updateInstance = function() { * Converts runtime color to API Color * @returns {GameLib.D3.API.Color} */ -GameLib.D3.Runtime.Color.prototype.toApiColor = function() { +GameLib.D3.Color.prototype.toApiColor = function() { return new GameLib.D3.API.Color( this.r, this.g, @@ -80,7 +80,7 @@ GameLib.D3.Runtime.Color.prototype.toApiColor = function() { * Converts the current color to HTML hex format (ex. #ffffff) * @returns {string} */ -GameLib.D3.Runtime.Color.prototype.toHex = function() { +GameLib.D3.Color.prototype.toHex = function() { if (this.r < 0) { this.r = 0; @@ -123,4 +123,18 @@ GameLib.D3.Runtime.Color.prototype.toHex = function() { } return '#' + rf + gf + bf; +}; + +/** + * Sets this object color to what the hex value is + * @param hex + * @returns {string} + */ +GameLib.D3.Color.prototype.fromHex = function(hex) { + + var matches = hex.match(new RegExp('#+(..)(..)(..)')); + + this.r = parseInt(matches[1], 16) / 255.0; + this.g = parseInt(matches[2], 16) / 255.0; + this.b = parseInt(matches[3], 16) / 255.0; }; \ No newline at end of file diff --git a/src/game-lib-component-camera.js b/src/game-lib-component-camera.js index fa01219..d105a6c 100644 --- a/src/game-lib-component-camera.js +++ b/src/game-lib-component-camera.js @@ -11,7 +11,7 @@ GameLib.D3.ComponentCamera = function ComponentCamera( name, camera ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-colorflash.js b/src/game-lib-component-colorflash.js index 1b6b4ee..7179da8 100644 --- a/src/game-lib-component-colorflash.js +++ b/src/game-lib-component-colorflash.js @@ -8,7 +8,7 @@ GameLib.D3.ComponentColorFlash = function ComponentColorFlash( id, name ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-colorlerp.js b/src/game-lib-component-colorlerp.js index 3cf0ecd..766525d 100644 --- a/src/game-lib-component-colorlerp.js +++ b/src/game-lib-component-colorlerp.js @@ -5,7 +5,7 @@ GameLib.D3.ComponentColorLerp = function( endColor, lerpSpeed ) { - this.id = id|| GameLib.D3.Tools.RandomId(); + this.id = id|| GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; } diff --git a/src/game-lib-component-entity-parent.js b/src/game-lib-component-entity-parent.js index b854918..c8ec093 100644 --- a/src/game-lib-component-entity-parent.js +++ b/src/game-lib-component-entity-parent.js @@ -12,7 +12,7 @@ GameLib.D3.ComponentEntityParent = function ComponentEntityParent( parent, centerToOrigin ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-entity-permutation.js b/src/game-lib-component-entity-permutation.js index 49a751a..e9539ac 100644 --- a/src/game-lib-component-entity-permutation.js +++ b/src/game-lib-component-entity-permutation.js @@ -14,7 +14,7 @@ GameLib.D3.ComponentEntityPermutation = function ComponentEntityPermutation( quaternionOffset, scaleOffset ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; @@ -24,17 +24,17 @@ GameLib.D3.ComponentEntityPermutation = function ComponentEntityPermutation( this.parentEntity = null; if(GameLib.D3.Utils.UndefinedOrNull(positionOffset)) { - positionOffset = new GameLib.D3.Vector3(0, 0, 0); + positionOffset = new GameLib.D3.API.Vector3(0, 0, 0); } this.positionOffset = positionOffset; if(GameLib.D3.Utils.UndefinedOrNull(quaternionOffset)) { - quaternionOffset = new GameLib.D3.Vector4(0, 0, 0, 1); + quaternionOffset = new GameLib.D3.API.Vector4(0, 0, 0, 1); } this.quaternionOffset = quaternionOffset; if(GameLib.D3.Utils.UndefinedOrNull(scaleOffset)) { - scaleOffset = new GameLib.D3.Vector3(1, 1, 1); + scaleOffset = new GameLib.D3.API.Vector3(1, 1, 1); } this.scaleOffset = scaleOffset; GameLib.D3.Utils.Extend(GameLib.D3.ComponentEntityPermutation, GameLib.D3.ComponentInterface); diff --git a/src/game-lib-component-fly-controls.js b/src/game-lib-component-fly-controls.js index 42434b2..caa4914 100644 --- a/src/game-lib-component-fly-controls.js +++ b/src/game-lib-component-fly-controls.js @@ -8,7 +8,7 @@ GameLib.D3.ComponentFlyControls = function ComponentFlyControls( id, name ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-follow.js b/src/game-lib-component-follow.js index 8c01eb0..9958864 100644 --- a/src/game-lib-component-follow.js +++ b/src/game-lib-component-follow.js @@ -16,7 +16,7 @@ GameLib.D3.ComponentFollow = function ComponentFollow( minDistance, moveSpeed ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-interface.js b/src/game-lib-component-interface.js index f93626e..a029b97 100644 --- a/src/game-lib-component-interface.js +++ b/src/game-lib-component-interface.js @@ -8,7 +8,7 @@ GameLib.D3.ComponentInterface = function ComponentInterface( id, name ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-look-at.js b/src/game-lib-component-look-at.js index ba36d54..1e74357 100644 --- a/src/game-lib-component-look-at.js +++ b/src/game-lib-component-look-at.js @@ -14,7 +14,7 @@ GameLib.D3.ComponentLookAt = function ComponentLookAt( targetOffset, rotationSpeed ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-mesh-permutation.js b/src/game-lib-component-mesh-permutation.js index fb5502b..4b34645 100644 --- a/src/game-lib-component-mesh-permutation.js +++ b/src/game-lib-component-mesh-permutation.js @@ -14,7 +14,7 @@ GameLib.D3.ComponentMeshPermutation = function ComponentMeshPermutation( quaternionOffset, scaleOffset ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; @@ -23,9 +23,9 @@ GameLib.D3.ComponentMeshPermutation = function ComponentMeshPermutation( this.parentEntity = null; - this.positionOffset = positionOffset || new GameLib.D3.Vector3(0, 0, 0); - this.quaternionOffset = quaternionOffset || new GameLib.D3.Vector4(0, 0, 0, 1); - this.scaleOffset = scaleOffset || new GameLib.D3.Vector3(1, 1, 1); + this.positionOffset = positionOffset || new GameLib.D3.API.Vector3(0, 0, 0); + this.quaternionOffset = quaternionOffset || new GameLib.D3.API.Vector4(0, 0, 0, 1); + this.scaleOffset = scaleOffset || new GameLib.D3.API.Vector3(1, 1, 1); // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentMeshPermutation, GameLib.D3.ComponentInterface); diff --git a/src/game-lib-component-offsettor.js b/src/game-lib-component-offsettor.js index c7e0a87..5a63029 100644 --- a/src/game-lib-component-offsettor.js +++ b/src/game-lib-component-offsettor.js @@ -2,7 +2,7 @@ * * @param id * @param name - * @param axis {GameLib.D3.Vector3} + * @param axis {GameLib.D3.API.Vector3} * @param getOffsetFunc {function} * @param userData {Object} * @constructor @@ -14,7 +14,7 @@ GameLib.D3.ComponentOffsettor = function ComponentOffsettor( getOffsetFunc, userData ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; @@ -22,7 +22,7 @@ GameLib.D3.ComponentOffsettor = function ComponentOffsettor( this.name = name; this.parentEntity = null; - this.axis = axis || new GameLib.D3.Vector3(); + this.axis = axis || new GameLib.D3.API.Vector3(); this.offset = 1; var component = this; this.getOffsetFunc = getOffsetFunc || function(entity, component, userData){ return component.offset; }; diff --git a/src/game-lib-component-path-ai.js b/src/game-lib-component-path-ai.js index 3700ea5..8326f44 100644 --- a/src/game-lib-component-path-ai.js +++ b/src/game-lib-component-path-ai.js @@ -10,7 +10,7 @@ GameLib.D3.ComponentPathAI = function ComponentPathAI( name, sensorLength ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-path-controls.js b/src/game-lib-component-path-controls.js index 80aa98c..2563d70 100644 --- a/src/game-lib-component-path-controls.js +++ b/src/game-lib-component-path-controls.js @@ -8,7 +8,7 @@ GameLib.D3.ComponentPathControls = function ComponentPathControls( id, name ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index eeb8b72..8416989 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -22,7 +22,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( maxOffset, steeringSpeed ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; @@ -37,16 +37,16 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.maxSpeed = maxSpeed || 10.0; this.accel = accel || 2.0; - this.baseOffset = baseOffset || new GameLib.D3.Vector3(); - this.maxOffset = maxOffset || new GameLib.D3.Vector3(10, 10, 10); + this.baseOffset = baseOffset || new GameLib.D3.API.Vector3(); + this.maxOffset = maxOffset || new GameLib.D3.API.Vector3(10, 10, 10); this.steeringSpeed = steeringSpeed || 1.0; // runtime code //#ifdef RUNTIME__ - this.offset = new GameLib.D3.Vector3(); // this one is our destination offset - this.currentOffset = new GameLib.D3.Vector3(); + this.offset = new GameLib.D3.API.Vector3(); // this one is our destination offset + this.currentOffset = new GameLib.D3.API.Vector3(); this.currentPathValue = 0.0; this.currentSpeed = 0.0; this.direction = 0; diff --git a/src/game-lib-component-raycast-vehicle-controls.js b/src/game-lib-component-raycast-vehicle-controls.js index 0c1852a..0686e06 100644 --- a/src/game-lib-component-raycast-vehicle-controls.js +++ b/src/game-lib-component-raycast-vehicle-controls.js @@ -8,7 +8,7 @@ GameLib.D3.ComponentRaycastVehicleControls = function ComponentRaycastVehicleCon maxForce, steering ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-rotator.js b/src/game-lib-component-rotator.js index 69f6d3b..1ab4f0e 100644 --- a/src/game-lib-component-rotator.js +++ b/src/game-lib-component-rotator.js @@ -2,7 +2,7 @@ * * @param id * @param name - * @param axis {GameLib.D3.Vector3} + * @param axis {GameLib.D3.API.Vector3} * @param getRotationFunc {Function} * @param userData {Object} * @constructor @@ -14,7 +14,7 @@ GameLib.D3.ComponentRotator = function ComponentRotator( getRotationFunc, userData ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; @@ -24,7 +24,7 @@ GameLib.D3.ComponentRotator = function ComponentRotator( this.rotation = 0; var component = this; - this.axis = axis || new GameLib.D3.Vector3(); + this.axis = axis || new GameLib.D3.API.Vector3(); this.getRotationFunc = getRotationFunc || function(entity, component, userData){ return component.rotation; }; this.userData = userData; diff --git a/src/game-lib-component-trigger-box-box.js b/src/game-lib-component-trigger-box-box.js index ec44abc..8cb7cd1 100644 --- a/src/game-lib-component-trigger-box-box.js +++ b/src/game-lib-component-trigger-box-box.js @@ -13,7 +13,7 @@ GameLib.D3.ComponentTriggerBoxBox = function ComponentTriggerBoxBox( ) { console.log('triggerbox box!'); - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-trigger-box-sphere.js b/src/game-lib-component-trigger-box-sphere.js index 9a45a94..d955f95 100644 --- a/src/game-lib-component-trigger-box-sphere.js +++ b/src/game-lib-component-trigger-box-sphere.js @@ -11,7 +11,7 @@ GameLib.D3.ComponentTriggerBoxSphere = function ComponentTriggerBoxSphere( onLeave, onSetParent ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-trigger-sphere-sphere.js b/src/game-lib-component-trigger-sphere-sphere.js index 960515c..3ee0b3e 100644 --- a/src/game-lib-component-trigger-sphere-sphere.js +++ b/src/game-lib-component-trigger-sphere-sphere.js @@ -21,7 +21,7 @@ GameLib.D3.ComponentTriggerSphereSphere = function ComponentTriggerSphereSphere( onLeave, onSetParent ) { - this.id = id || GameLib.D3.Tools.RandomId(); + this.id = id || GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; diff --git a/src/game-lib-component-vehicle-ai-object-avoidance.js b/src/game-lib-component-vehicle-ai-object-avoidance.js index 1355d2f..9c9ecc7 100644 --- a/src/game-lib-component-vehicle-ai-object-avoidance.js +++ b/src/game-lib-component-vehicle-ai-object-avoidance.js @@ -10,7 +10,7 @@ GameLib.D3.ComponentVehicleAIObjectAvoidance = function( name, world ) { - this.id = id|| GameLib.D3.Tools.RandomId(); + this.id = id|| GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; } diff --git a/src/game-lib-component-vehicle-ai-path-steering.js b/src/game-lib-component-vehicle-ai-path-steering.js index 8b0befb..5e5c14e 100644 --- a/src/game-lib-component-vehicle-ai-path-steering.js +++ b/src/game-lib-component-vehicle-ai-path-steering.js @@ -14,7 +14,7 @@ GameLib.D3.ComponentVehicleAIPathSteering = function( steeringSpeed, maxSteerAngle ) { - this.id = id|| GameLib.D3.Tools.RandomId(); + this.id = id|| GameLib.D3.Utils.RandomId(); if (typeof name == 'undefined') { name = this.constructor.name; } diff --git a/src/game-lib-helper.js b/src/game-lib-helper.js index 2257e78..b4d6c10 100644 --- a/src/game-lib-helper.js +++ b/src/game-lib-helper.js @@ -15,7 +15,7 @@ GameLib.D3.Helper = function Helper( graphics ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-image.js b/src/game-lib-image.js index af0b9a2..78135eb 100644 --- a/src/game-lib-image.js +++ b/src/game-lib-image.js @@ -76,7 +76,7 @@ GameLib.D3.Image = function( contentType ) { if (GameLib.D3.Utils.UndefinedOrNull(id)) { - id = GameLib.D3.Tools.RandomId(); + id = GameLib.D3.Utils.RandomId(); } this.id = id; diff --git a/src/game-lib-light.js b/src/game-lib-light.js index 2910161..a942e7e 100644 --- a/src/game-lib-light.js +++ b/src/game-lib-light.js @@ -14,40 +14,40 @@ GameLib.D3.Light = function Light( } } - this.color = new GameLib.D3.Runtime.Color( + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.color = new GameLib.D3.Color( graphics, this, this.color, 0.01 ); - this.position = new GameLib.D3.Runtime.Vector3( + this.position = new GameLib.D3.Vector3( graphics, this, this.position ); - this.targetPosition = new GameLib.D3.Runtime.Vector3( + this.targetPosition = new GameLib.D3.Vector3( graphics, this, this.targetPosition ); - this.scale = new GameLib.D3.Runtime.Vector3( + this.scale = new GameLib.D3.Vector3( graphics, this, this.scale ); - this.quaternion = new GameLib.D3.Runtime.Vector4( + this.quaternion = new GameLib.D3.Vector4( graphics, this, this.quaternion ); - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - this.instance = this.createInstance(); }; @@ -76,33 +76,21 @@ GameLib.D3.Light.prototype.createInstance = function(update) { if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { instance = new this.graphics.instance.AmbientLight( - new this.graphics.instance.Color( - this.color.r, - this.color.g, - this.color.b - ), + this.color.instance, this.intensity ); } if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { instance = new this.graphics.instance.DirectionalLight( - new this.graphics.instance.Color( - this.color.r, - this.color.g, - this.color.b - ), + this.color.instance, this.intensity ); } if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) { instance = new this.graphics.instance.PointLight( - new this.graphics.instance.Color( - this.color.r, - this.color.g, - this.color.b - ), + this.color.instance, this.intensity ); instance.distance = this.distance; @@ -111,11 +99,7 @@ GameLib.D3.Light.prototype.createInstance = function(update) { if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) { instance = new this.graphics.instance.SpotLight( - new this.graphics.instance.Color( - this.color.r, - this.color.g, - this.color.b - ), + this.color.instance, this.intensity ); instance.distance = this.distance; @@ -134,24 +118,15 @@ GameLib.D3.Light.prototype.createInstance = function(update) { instance.name = this.name; - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; + instance.position.copy(this.position.instance); - instance.scale.x = this.scale.x; - instance.scale.y = this.scale.y; - instance.scale.z = this.scale.z; + instance.scale.copy(this.scale.instance); if (instance.target) { - instance.target.x = this.targetPosition.x; - instance.target.y = this.targetPosition.y; - instance.target.z = this.targetPosition.z; + instance.target.position.copy(this.targetPosition.instance); } - instance.quaternion.x = this.quaternion.x; - instance.quaternion.y = this.quaternion.y; - instance.quaternion.z = this.quaternion.z; - instance.quaternion.w = this.quaternion.w; + instance.quaternion.copy(this.quaternion.instance); instance.intensity = this.intensity; diff --git a/src/game-lib-material.js b/src/game-lib-material.js index e3460e4..5ff95a4 100644 --- a/src/game-lib-material.js +++ b/src/game-lib-material.js @@ -16,27 +16,27 @@ GameLib.D3.Material = function Material( } } - this.specular = new GameLib.D3.Runtime.Color( + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.specular = new GameLib.D3.Color( graphics, this, this.specular ); - this.color = new GameLib.D3.Runtime.Color( + this.color = new GameLib.D3.Color( graphics, this, this.color ); - this.emissive = new GameLib.D3.Runtime.Color( + this.emissive = new GameLib.D3.Color( graphics, this, this.emissive ); - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - this.instance = this.createInstance(); this.needsUpdate = false; @@ -150,10 +150,7 @@ GameLib.D3.Material.prototype.createInstance = function(update) { instance = this.instance; } - if ( - (!instance) || - (instance && instance.type != this.materialType) - ) { + if (!instance) { if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { @@ -177,20 +174,12 @@ GameLib.D3.Material.prototype.createInstance = function(update) { overdraw: this.overdraw, visible: this.visible, side: this.side, - color: new this.graphics.instance.Color( - this.color.r, - this.color.g, - this.color.b - ), + color: this.color.instance, roughness: this.roughness, metalness: this.metalness, lightMapIntensity: this.lightMapIntensity, aoMapIntensity: this.aoMapIntensity, - emissive: new this.graphics.instance.Color( - this.emissive.r, - this.emissive.g, - this.emissive.b - ), + emissive: this.emissive.instance, emissiveIntensity: this.emissiveIntensity, bumpScale: this.bumpScale, normalScale: this.normalScale, @@ -230,24 +219,12 @@ GameLib.D3.Material.prototype.createInstance = function(update) { overdraw: this.overdraw, visible: this.visible, side: this.side, - color: new this.graphics.instance.Color( - this.color.r, - this.color.g, - this.color.b - ), - specular: new this.graphics.instance.Color( - this.specular.r, - this.specular.g, - this.specular.b - ), + color: this.color.instance, + specular: this.specular.instance, shininess: this.shininess, lightMapIntensity: this.lightMapIntensity, aoMapIntensity: this.aoMapIntensity, - emissive: new this.graphics.instance.Color( - this.emissive.r, - this.emissive.g, - this.emissive.b - ), + emissive: this.emissive.instance, emissiveIntensity: this.emissiveIntensity, bumpScale: this.bumpScale, normalScale: this.normalScale, @@ -287,11 +264,7 @@ GameLib.D3.Material.prototype.createInstance = function(update) { overdraw: this.overdraw, visible: this.visible, side: this.side, - color: new this.graphics.instance.Color( - this.color.r, - this.color.g, - this.color.b - ), + color: this.color.instance, size: this.pointSize, sizeAttenuation: this.pointSizeAttenuation, vertexColors: GameLib.D3.Material.TYPE_NO_COLORS, @@ -319,7 +292,11 @@ GameLib.D3.Material.prototype.createInstance = function(update) { this.hasOwnProperty(property) && instance.hasOwnProperty(property) ) { - instance[property] = this[property]; + if (instance[property] instanceof THREE.Color) { + instance[property].copy(this[property]) + } else { + instance[property] = this[property]; + } } } diff --git a/src/game-lib-mesh.js b/src/game-lib-mesh.js index 4a22704..04f928a 100644 --- a/src/game-lib-mesh.js +++ b/src/game-lib-mesh.js @@ -20,25 +20,39 @@ GameLib.D3.Mesh = function Mesh( this.graphics.isNotThreeThrow(); + this.position = new GameLib.D3.Vector3( + graphics, + this, + this.position + ); + + this.scale = new GameLib.D3.Vector3( + graphics, + this, + this.scale + ); + + this.up = new GameLib.D3.Vector3( + graphics, + this, + this.up + ); + + /** + * We don't do a Runtime rotation since it interferes with the quaternion update + */ + + this.quaternion = new GameLib.D3.Vector4( + graphics, + this, + this.quaternion + ); + this.computeNormals = computeNormals; this.instance = this.createInstance(false); this.needsUpdate = false; - - this.offsetRotation = new GameLib.D3.Runtime.Vector3(graphics, this, new GameLib.D3.API.Vector3(0,0,0)); - - delete this.offsetRotation.updateInstance; - - // this.lookAtPoint = new GameLib.D3.Runtime.Vector3(graphics, this, new GameLib.D3.API.Vector3(0,0,0)); - // - // delete this.offsetRotation.updateInstance; - // - // this.offsetRotation = new GameLib.D3.Runtime.Vector3(graphics, this, new GameLib.D3.API.Vector3(0,0,0)); - // - // delete this.offsetRotation.updateInstance; - - }; /** @@ -295,23 +309,16 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.gameLibObject = this; - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; + instance.position.copy(this.position.instance); - instance.scale.x = this.scale.x; - instance.scale.y = this.scale.y; - instance.scale.z = this.scale.z; + instance.scale.copy(this.scale.instance); - // 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.up.copy(this.up.instance); - 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 - its dealt with by quaternion + */ + instance.quaternion.copy(this.quaternion.instance); return instance; }; @@ -320,303 +327,6 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; -GameLib.D3.prototype.invertWindingOrder = function(triangles) { - - for (var i = 0; i < triangles.length; i++) { - var v1 = triangles[i].v1; - triangles[i].v1 = triangles[i].v2; - triangles[i].v2 = v1; - - var backupUV = triangles[i].triangle.v1uv; - triangles[i].triangle.v1uv = triangles[i].triangle.v2uv; - triangles[i].triangle.v2uv = backupUV; - } - - return triangles; -}; - -/** - * This function resets a the winding order of a mesh from a reference point V (the average center of the mesh) - */ -GameLib.D3.prototype.resetWindingOrder = function(faces, vertices) { - - var vertexList = new GameLib.D3.API.Vector3.Points(); - - for (var v = 0; v < vertices.length; v++) { - vertexList.add(new GameLib.D3.API.Vector3( - vertices[v].position.x, - vertices[v].position.y, - vertices[v].position.z - )); - } - - var V = vertexList.average(); - - var triangles = []; - - for (var s = 0; s < faces.length; s += 3) { - - var v0 = faces[s]; - var v1 = faces[s+1]; - var v2 = faces[s+2]; - - triangles.push( - { - v0 : v0, - v1 : v1, - v2 : v2, - edges : [ - {v0: v0, v1: v1}, - {v0: v1, v1: v2}, - {v0: v2, v1: v0} - ], - winding : 0, - edgeIndex : -1, - processed : false - } - ); - } - - for (var i = 0; i < triangles.length; i++) { - if ( - GameLib.D3.API.Vector3.clockwise( - vertices[triangles[i].v0].position, - vertices[triangles[i].v1].position, - vertices[triangles[i].v2].position, - V - ) - ) { - console.log('clockwise'); - var bv1 = triangles[i].v1; - triangles[i].v1 = triangles[i].v2; - triangles[i].v2 = bv1; - } else { - console.log('not clockwise'); - } - } - - return triangles; -}; - -/** - * This function resets the winding order for triangles in faces, given an initial triangle and orientation edge - * used pseudocode from - * http://stackoverflow.com/questions/17036970/how-to-correct-winding-of-triangles-to-counter-clockwise-direction-of-a-3d-mesh - * We need to use a graph traversal algorithm, - * lets assume we have method that returns neighbor of triangle on given edge - * - * neighbor_on_egde( next_tria, edge ) - * - * to_process = set of pairs triangle and orientation edge, initial state is one good oriented triangle with any edge on it - * processed = set of processed triangles; initial empty - * - * while to_process is not empty: - * next_tria, orientation_edge = to_process.pop() - * add next_tria in processed - * if next_tria is not opposite oriented than orientation_edge: - * change next_tria (ABC) orientation (B<->C) - * for each edge (AB) in next_tria: - * neighbor_tria = neighbor_on_egde( next_tria, edge ) - * if neighbor_tria exists and neighbor_tria not in processed: - * to_process add (neighbor_tria, edge opposite oriented (BA)) - * @param faces GameLib.D3.TriangleFace[] - * @param orientationEdge GameLib.D3.API.Vector2 - * @returns {Array} - */ -GameLib.D3.fixWindingOrder = function(faces, orientationEdge) { - - /** - * Checks if a TriangleFace belonging to a TriangleEdge has already been processed - * @param processed TriangleEdge[] - * @param triangle TriangleFace - * @returns {boolean} - */ - function inProcessed(processed, triangle) { - - for (var i = 0; i < processed.length; i++) { - if (processed[i].triangle.equals(triangle)) { - return true; - } - } - - return false; - } - - /** - * Returns a neighbouring triangle on a specific edge - preserving the edge orientation - * @param edge GameLib.D3.API.Vector2 - * @param faces GameLib.D3.TriangleFace[] - * @param currentTriangle - * @returns {*} - */ - function neighbourOnEdge(edge, faces, currentTriangle) { - - for (var i = 0; i < faces.length; i++) { - if ( - (faces[i].v0 == edge.x && faces[i].v1 == edge.y) || - (faces[i].v1 == edge.x && faces[i].v2 == edge.y) || - (faces[i].v2 == edge.x && faces[i].v0 == edge.y) || - (faces[i].v0 == edge.y && faces[i].v1 == edge.x) || - (faces[i].v1 == edge.y && faces[i].v2 == edge.x) || - (faces[i].v2 == edge.y && faces[i].v0 == edge.x) - ) { - - var triangle = new GameLib.D3.TriangleFace( - faces[i].v0, - faces[i].v1, - faces[i].v2, - faces[i].materialIndex, - faces[i].v0uv, - faces[i].v1uv, - faces[i].v2uv - ); - - if (triangle.equals(currentTriangle)) { - continue; - } - - return new GameLib.D3.TriangleEdge( - triangle, - edge - ); - } - } - - return null; - } - - var toProcess = [ - new GameLib.D3.TriangleEdge( - new GameLib.D3.TriangleFace( - faces[0].v0, - faces[0].v1, - faces[0].v2, - faces[0].materialIndex, - faces[0].v0uv, - faces[0].v1uv, - faces[0].v2uv - ), - orientationEdge - ) - ]; - - var processed = []; - - while (toProcess.length > 0) { - - var triangleEdge = toProcess.pop(); - - /** - * If edge is the same orientation (i.e. the edge order is the same as the given triangle edge) it needs to be reversed - * to have the same winding order) - */ - if ( - (triangleEdge.triangle.v0 == triangleEdge.edge.x && - triangleEdge.triangle.v1 == triangleEdge.edge.y) || - (triangleEdge.triangle.v1 == triangleEdge.edge.x && - triangleEdge.triangle.v2 == triangleEdge.edge.y) || - (triangleEdge.triangle.v2 == triangleEdge.edge.x && - triangleEdge.triangle.v0 == triangleEdge.edge.y) - ) { - var backupV = triangleEdge.triangle.v1; - triangleEdge.triangle.v1 = triangleEdge.triangle.v2; - triangleEdge.triangle.v2 = backupV; - - var backupUV = triangleEdge.triangle.v1uv; - triangleEdge.triangle.v1uv = triangleEdge.triangle.v2uv; - triangleEdge.triangle.v2uv = backupUV; - } - - processed.push(triangleEdge); - - var edges = [ - new GameLib.D3.API.Vector2( - triangleEdge.triangle.v0, - triangleEdge.triangle.v1 - ), - new GameLib.D3.API.Vector2( - triangleEdge.triangle.v1, - triangleEdge.triangle.v2 - ), - new GameLib.D3.API.Vector2( - triangleEdge.triangle.v2, - triangleEdge.triangle.v0 - ) - ]; - - for (var j = 0; j < edges.length; j++) { - var neighbour = neighbourOnEdge(edges[j], faces, triangleEdge.triangle); - if (neighbour && !inProcessed(processed, neighbour.triangle)) { - toProcess.push(neighbour); - } - } - } - - /** - * In processed - we will have some duplicates - only add the unique ones - * @type {Array} - */ - var triangles = []; - for (var i = 0; i < processed.length; i++) { - var found = false; - for (var k = 0; k < triangles.length; k++) { - if (triangles[k].equals(processed[i].triangle)){ - found = true; - break; - } - } - if (!found) { - triangles.push(processed[i].triangle); - } - } - - return triangles; -}; - -/** - * This is a work-around function to fix polys which don't triangulate because - * they could lie on Z-plane (XZ or YZ)) - we translate the poly to the origin, systematically rotate the poly around - * Z then Y axis - * @param verticesFlat [] - * @param grain is the amount to systematically rotate the poly by - a finer grain means a more accurate maximum XY - * @return [] - */ -GameLib.D3.fixPolyZPlane = function(verticesFlat, grain) { - - if ((verticesFlat.length % 3) != 0 && !(verticesFlat.length > 9)) { - console.log("The vertices are not in the right length : " + verticesFlat.length); - } - - var vertices = []; - - var points = new GameLib.D3.API.Vector4.Points(); - - for (var i = 0; i < verticesFlat.length; i += 3) { - points.add(new GameLib.D3.API.Vector3( - verticesFlat[i], - verticesFlat[i + 1], - verticesFlat[i + 2] - )); - } - - points.toOrigin(); - - points.maximizeXDistance(grain); - - points.maximizeYDistance(grain); - - for (i = 0; i < points.vectors.length; i++) { - vertices.push( - [ - points.vectors[i].x, - points.vectors[i].y - ] - ); - } - - return vertices; -}; - GameLib.D3.Mesh.prototype.clone = function() { return _.cloneDeep(this); }; \ No newline at end of file diff --git a/src/game-lib-raycast-vehicle.js b/src/game-lib-raycast-vehicle.js index d1d4887..55befe0 100644 --- a/src/game-lib-raycast-vehicle.js +++ b/src/game-lib-raycast-vehicle.js @@ -14,7 +14,7 @@ GameLib.D3.RaycastVehicle = function( this.engine = engine; this.engine.isNotCannonThrow(); - this.id = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Utils.RandomId(); this.chassisBody = chassisBody; diff --git a/src/game-lib-raycast-wheel.js b/src/game-lib-raycast-wheel.js index c239ff0..10d80ab 100644 --- a/src/game-lib-raycast-wheel.js +++ b/src/game-lib-raycast-wheel.js @@ -30,7 +30,7 @@ GameLib.D3.RaycastWheel = function( this.engine = engine; this.engine.isNotCannonThrow(); - this.id = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Utils.RandomId(); if(typeof chassisConnectionPointLocal == 'undefined' || chassisConnectionPointLocal == null) { chassisConnectionPointLocal = new this.engine.instance.Vec3(); diff --git a/src/game-lib-rigid-body-vehicle.js b/src/game-lib-rigid-body-vehicle.js index 8c4fb09..d41051a 100644 --- a/src/game-lib-rigid-body-vehicle.js +++ b/src/game-lib-rigid-body-vehicle.js @@ -10,7 +10,7 @@ GameLib.D3.RigidBodyVehicle = function( chassisBody, wheels ) { - this.id = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Utils.RandomId(); this.engine = engine; this.engine.isNotCannonThrow(); diff --git a/src/game-lib-rigid-body.js b/src/game-lib-rigid-body.js index 594b267..b739e98 100644 --- a/src/game-lib-rigid-body.js +++ b/src/game-lib-rigid-body.js @@ -39,7 +39,7 @@ GameLib.D3.RigidBody = function( shape, kinematic ) { - this.id = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Utils.RandomId(); this.position = position || new GameLib.D3.API.Vector3(); this.velocity = velocity || new GameLib.D3.API.Vector3(); this.angularVelocity = angularVelocity || new GameLib.D3.API.Vector3(); diff --git a/src/game-lib-rigid-wheel.js b/src/game-lib-rigid-wheel.js index 6d7750d..e57c087 100644 --- a/src/game-lib-rigid-wheel.js +++ b/src/game-lib-rigid-wheel.js @@ -12,7 +12,7 @@ GameLib.D3.RigidWheel = function( axis, direction ) { - this.id = GameLib.D3.Tools.RandomId(); + this.id = GameLib.D3.Utils.RandomId(); this.body = body; this.position = position; this.axis = axis; diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index b2a7a4d..6929d27 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -27,6 +27,28 @@ GameLib.D3.Scene = function Scene( this.graphics = graphics; this.graphics.isNotThreeThrow(); + this.position = new GameLib.D3.Vector3( + graphics, + this, + this.position + ); + + this.scale = new GameLib.D3.Vector3( + graphics, + this, + this.scale + ); + + /** + * We don't do a Runtime rotation since it interferes with the quaternion update + */ + + this.quaternion = new GameLib.D3.Vector4( + graphics, + this, + this.quaternion + ); + if (GameLib.D3.Utils.UndefinedOrNull(meshIdToMesh)) { meshIdToMesh = {}; } @@ -56,22 +78,11 @@ GameLib.D3.Scene.prototype.createInstance = function() { instance.name = this.name; - instance.position.x = this.position.x; - instance.position.y = this.position.y; - instance.position.z = this.position.z; + instance.position = this.position.instance; - instance.rotation.x = this.rotation.x; - instance.rotation.y = this.rotation.y; - instance.rotation.z = this.rotation.z; + instance.scale = this.scale.instance; - 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; + instance.quaternion = this.quaternion.instance; for (var im = 0; im < this.meshes.length; im++) { instance.add(this.meshes[im].instance); @@ -396,37 +407,6 @@ GameLib.D3.Scene.LoadScene = function( ) ); - gameLibMesh.position = new GameLib.D3.Runtime.Vector3( - graphics, - gameLibMesh, - gameLibMesh.position - ); - - 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 393b039..9818e74 100644 --- a/src/game-lib-texture.js +++ b/src/game-lib-texture.js @@ -21,21 +21,21 @@ GameLib.D3.Texture = function Texture( } } - this.offset = new GameLib.D3.Runtime.Vector2( + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.offset = new GameLib.D3.Vector2( graphics, this, this.offset ); - this.repeat = new GameLib.D3.Runtime.Vector2( + this.repeat = new GameLib.D3.Vector2( graphics, this, this.repeat ); - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - this.parentMaterial = parentMaterial; this.parentMaterialInstanceMapId = parentMaterialInstanceMapId; @@ -173,10 +173,8 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { instance.flipY = this.flipY; instance.encoding = this.encoding; 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.offset.copy(this.offset.instance); + instance.repeat.copy(this.repeat.instance); instance.mipmaps = this.mipmaps; instance.unpackAlignment = this.unpackAlignment; instance.premultiplyAlpha = this.premultiplyAlpha; diff --git a/src/game-lib-tools.js b/src/game-lib-tools.js deleted file mode 100644 index 545a007..0000000 --- a/src/game-lib-tools.js +++ /dev/null @@ -1,10 +0,0 @@ -GameLib.D3.Tools = function(){}; - -/** - * Generates a random ID - * @returns {string} - * @constructor - */ -GameLib.D3.Tools.RandomId = function() { - return Math.random().toString(36).substr(2, 10); -}; \ No newline at end of file diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index 1e5754a..bbeb5b4 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -23,4 +23,310 @@ GameLib.D3.Utils.Raycast = function ( world ) { console.log("not implemented yet"); +}; + +/** + * Generates a random ID + * @returns {string} + * @constructor + */ +GameLib.D3.Utils.RandomId = function() { + return Math.random().toString(36).substr(2, 10); +}; + +GameLib.D3.Utils.InvertWindingOrder = function(triangles) { + + for (var i = 0; i < triangles.length; i++) { + var v1 = triangles[i].v1; + triangles[i].v1 = triangles[i].v2; + triangles[i].v2 = v1; + + var backupUV = triangles[i].triangle.v1uv; + triangles[i].triangle.v1uv = triangles[i].triangle.v2uv; + triangles[i].triangle.v2uv = backupUV; + } + + return triangles; +}; + +/** + * This function resets a the winding order of a mesh from a reference point V (the average center of the mesh) + */ +GameLib.D3.Utils.ResetWindingOrder = function(faces, vertices) { + + var vertexList = new GameLib.D3.API.Vector3.Points(); + + for (var v = 0; v < vertices.length; v++) { + vertexList.add(new GameLib.D3.API.Vector3( + vertices[v].position.x, + vertices[v].position.y, + vertices[v].position.z + )); + } + + var V = vertexList.average(); + + var triangles = []; + + for (var s = 0; s < faces.length; s += 3) { + + var v0 = faces[s]; + var v1 = faces[s+1]; + var v2 = faces[s+2]; + + triangles.push( + { + v0 : v0, + v1 : v1, + v2 : v2, + edges : [ + {v0: v0, v1: v1}, + {v0: v1, v1: v2}, + {v0: v2, v1: v0} + ], + winding : 0, + edgeIndex : -1, + processed : false + } + ); + } + + for (var i = 0; i < triangles.length; i++) { + if ( + GameLib.D3.API.Vector3.clockwise( + vertices[triangles[i].v0].position, + vertices[triangles[i].v1].position, + vertices[triangles[i].v2].position, + V + ) + ) { + console.log('clockwise'); + var bv1 = triangles[i].v1; + triangles[i].v1 = triangles[i].v2; + triangles[i].v2 = bv1; + } else { + console.log('not clockwise'); + } + } + + return triangles; +}; + +/** + * This function resets the winding order for triangles in faces, given an initial triangle and orientation edge + * used pseudocode from + * http://stackoverflow.com/questions/17036970/how-to-correct-winding-of-triangles-to-counter-clockwise-direction-of-a-3d-mesh + * We need to use a graph traversal algorithm, + * lets assume we have method that returns neighbor of triangle on given edge + * + * neighbor_on_egde( next_tria, edge ) + * + * to_process = set of pairs triangle and orientation edge, initial state is one good oriented triangle with any edge on it + * processed = set of processed triangles; initial empty + * + * while to_process is not empty: + * next_tria, orientation_edge = to_process.pop() + * add next_tria in processed + * if next_tria is not opposite oriented than orientation_edge: + * change next_tria (ABC) orientation (B<->C) + * for each edge (AB) in next_tria: + * neighbor_tria = neighbor_on_egde( next_tria, edge ) + * if neighbor_tria exists and neighbor_tria not in processed: + * to_process add (neighbor_tria, edge opposite oriented (BA)) + * @param faces GameLib.D3.TriangleFace[] + * @param orientationEdge GameLib.D3.API.Vector2 + * @returns {Array} + */ +GameLib.D3.Utils.FixWindingOrder = function(faces, orientationEdge) { + + /** + * Checks if a TriangleFace belonging to a TriangleEdge has already been processed + * @param processed TriangleEdge[] + * @param triangle TriangleFace + * @returns {boolean} + */ + function inProcessed(processed, triangle) { + + for (var i = 0; i < processed.length; i++) { + if (processed[i].triangle.equals(triangle)) { + return true; + } + } + + return false; + } + + /** + * Returns a neighbouring triangle on a specific edge - preserving the edge orientation + * @param edge GameLib.D3.API.Vector2 + * @param faces GameLib.D3.TriangleFace[] + * @param currentTriangle + * @returns {*} + */ + function neighbourOnEdge(edge, faces, currentTriangle) { + + for (var i = 0; i < faces.length; i++) { + if ( + (faces[i].v0 == edge.x && faces[i].v1 == edge.y) || + (faces[i].v1 == edge.x && faces[i].v2 == edge.y) || + (faces[i].v2 == edge.x && faces[i].v0 == edge.y) || + (faces[i].v0 == edge.y && faces[i].v1 == edge.x) || + (faces[i].v1 == edge.y && faces[i].v2 == edge.x) || + (faces[i].v2 == edge.y && faces[i].v0 == edge.x) + ) { + + var triangle = new GameLib.D3.TriangleFace( + faces[i].v0, + faces[i].v1, + faces[i].v2, + faces[i].materialIndex, + faces[i].v0uv, + faces[i].v1uv, + faces[i].v2uv + ); + + if (triangle.equals(currentTriangle)) { + continue; + } + + return new GameLib.D3.TriangleEdge( + triangle, + edge + ); + } + } + + return null; + } + + var toProcess = [ + new GameLib.D3.TriangleEdge( + new GameLib.D3.TriangleFace( + faces[0].v0, + faces[0].v1, + faces[0].v2, + faces[0].materialIndex, + faces[0].v0uv, + faces[0].v1uv, + faces[0].v2uv + ), + orientationEdge + ) + ]; + + var processed = []; + + while (toProcess.length > 0) { + + var triangleEdge = toProcess.pop(); + + /** + * If edge is the same orientation (i.e. the edge order is the same as the given triangle edge) it needs to be reversed + * to have the same winding order) + */ + if ( + (triangleEdge.triangle.v0 == triangleEdge.edge.x && + triangleEdge.triangle.v1 == triangleEdge.edge.y) || + (triangleEdge.triangle.v1 == triangleEdge.edge.x && + triangleEdge.triangle.v2 == triangleEdge.edge.y) || + (triangleEdge.triangle.v2 == triangleEdge.edge.x && + triangleEdge.triangle.v0 == triangleEdge.edge.y) + ) { + var backupV = triangleEdge.triangle.v1; + triangleEdge.triangle.v1 = triangleEdge.triangle.v2; + triangleEdge.triangle.v2 = backupV; + + var backupUV = triangleEdge.triangle.v1uv; + triangleEdge.triangle.v1uv = triangleEdge.triangle.v2uv; + triangleEdge.triangle.v2uv = backupUV; + } + + processed.push(triangleEdge); + + var edges = [ + new GameLib.D3.API.Vector2( + triangleEdge.triangle.v0, + triangleEdge.triangle.v1 + ), + new GameLib.D3.API.Vector2( + triangleEdge.triangle.v1, + triangleEdge.triangle.v2 + ), + new GameLib.D3.API.Vector2( + triangleEdge.triangle.v2, + triangleEdge.triangle.v0 + ) + ]; + + for (var j = 0; j < edges.length; j++) { + var neighbour = neighbourOnEdge(edges[j], faces, triangleEdge.triangle); + if (neighbour && !inProcessed(processed, neighbour.triangle)) { + toProcess.push(neighbour); + } + } + } + + /** + * In processed - we will have some duplicates - only add the unique ones + * @type {Array} + */ + var triangles = []; + for (var i = 0; i < processed.length; i++) { + var found = false; + for (var k = 0; k < triangles.length; k++) { + if (triangles[k].equals(processed[i].triangle)){ + found = true; + break; + } + } + if (!found) { + triangles.push(processed[i].triangle); + } + } + + return triangles; +}; + +/** + * This is a work-around function to fix polys which don't triangulate because + * they could lie on Z-plane (XZ or YZ)) - we translate the poly to the origin, systematically rotate the poly around + * Z then Y axis + * @param verticesFlat [] + * @param grain is the amount to systematically rotate the poly by - a finer grain means a more accurate maximum XY + * @return [] + */ +GameLib.D3.Utils.FixPolyZPlane = function(verticesFlat, grain) { + + if ((verticesFlat.length % 3) != 0 && !(verticesFlat.length > 9)) { + console.log("The vertices are not in the right length : " + verticesFlat.length); + } + + var vertices = []; + + var points = new GameLib.D3.API.Vector4.Points(); + + for (var i = 0; i < verticesFlat.length; i += 3) { + points.add(new GameLib.D3.API.Vector3( + verticesFlat[i], + verticesFlat[i + 1], + verticesFlat[i + 2] + )); + } + + points.toOrigin(); + + points.maximizeXDistance(grain); + + points.maximizeYDistance(grain); + + for (i = 0; i < points.vectors.length; i++) { + vertices.push( + [ + points.vectors[i].x, + points.vectors[i].y + ] + ); + } + + return vertices; }; \ No newline at end of file diff --git a/src/game-lib-runtime-vector2.js b/src/game-lib-vector2.js similarity index 59% rename from src/game-lib-runtime-vector2.js rename to src/game-lib-vector2.js index bdd00e4..0cd6025 100644 --- a/src/game-lib-runtime-vector2.js +++ b/src/game-lib-vector2.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.D3.Runtime.Vector2 = function RuntimeVector2(graphics, parentObject, vector2, grain) { +GameLib.D3.Vector2 = function RuntimeVector2(graphics, parentObject, vector2, grain) { for (var property in vector2) { if (vector2.hasOwnProperty(property)) { @@ -14,7 +14,7 @@ GameLib.D3.Runtime.Vector2 = function RuntimeVector2(graphics, parentObject, vec } } - GameLib.D3.Utils.Extend(GameLib.D3.Runtime.Vector2, GameLib.D3.API.Vector2); + GameLib.D3.Utils.Extend(GameLib.D3.Vector2, GameLib.D3.API.Vector2); this.graphics = graphics; @@ -35,7 +35,7 @@ GameLib.D3.Runtime.Vector2 = function RuntimeVector2(graphics, parentObject, vec * @param update * @returns {*} */ -GameLib.D3.Runtime.Vector2.prototype.createInstance = function(update) { +GameLib.D3.Vector2.prototype.createInstance = function(update) { var instance = null; @@ -53,7 +53,7 @@ GameLib.D3.Runtime.Vector2.prototype.createInstance = function(update) { /** * Updates the instance vector, calls updateInstance on the parent object */ -GameLib.D3.Runtime.Vector2.prototype.updateInstance = function() { +GameLib.D3.Vector2.prototype.updateInstance = function() { this.createInstance(true); @@ -66,113 +66,108 @@ GameLib.D3.Runtime.Vector2.prototype.updateInstance = function() { * Converts runtime vector to API Vector * @returns {GameLib.D3.API.Vector2} */ -GameLib.D3.Runtime.Vector2.prototype.toApiVector = function() { +GameLib.D3.Vector2.prototype.toApiVector = function() { return new GameLib.D3.API.Vector2( this.x, this.y ); }; -GameLib.D3.Runtime.Vector2 = function Vector2(x, y) { - this.x = x || 0; - this.y = y || 0; -}; - -GameLib.D3.Runtime.Vector2.prototype.copy = function (v) { +GameLib.D3.Vector2.prototype.copy = function (v) { if (!GameLib.D3.Utils.UndefinedOrNull(v)) { this.x = v.x; this.y = v.y; return this; } else { - return new GameLib.D3.Runtime.Vector2( + return new GameLib.D3.Vector2( this.x, this.y ); } }; -GameLib.D3.Runtime.Vector2.prototype.equals = function(v) { +GameLib.D3.Vector2.prototype.equals = function(v) { return (((this.x == v.x) && (this.y == v.y)) || ((this.y == v.x) && (this.x == v.y))); }; -GameLib.D3.Runtime.Vector2.prototype.add = function(v) { - return new GameLib.D3.Runtime.Vector2( +GameLib.D3.Vector2.prototype.add = function(v) { + return new GameLib.D3.Vector2( this.x + v.x, this.y + v.y ); }; -GameLib.D3.Runtime.Vector2.prototype.subtract = function(v) { - return new GameLib.D3.Runtime.Vector2( +GameLib.D3.Vector2.prototype.subtract = function(v) { + return new GameLib.D3.Vector2( this.x - v.x, this.y - v.y ); }; -GameLib.D3.Runtime.Vector2.prototype.multiply = function(v) { - if (v instanceof GameLib.D3.Runtime.Vector2) { - return new GameLib.D3.Runtime.Vector2( +GameLib.D3.Vector2.prototype.multiply = function(v) { + if (v instanceof GameLib.D3.Vector2) { + return new GameLib.D3.Vector2( this.x * v.x, this.y * v.y ); } else if (isNumber(v)) { - return new GameLib.D3.Runtime.Vector2( + return new GameLib.D3.Vector2( this.x * v, this.y * v ); } }; -GameLib.D3.Runtime.Vector2.prototype.divide = function(v) { - if (v instanceof GameLib.D3.Runtime.Vector2) { - return new GameLib.D3.Runtime.Vector2( +GameLib.D3.Vector2.prototype.divide = function(v) { + if (v instanceof GameLib.D3.Vector2) { + return new GameLib.D3.Vector2( this.x * (1.0 / v.x), this.y * (1.0 / v.y) ); } else if (isNumber(v)) { var invS = 1.0 / v; - return new GameLib.D3.Runtime.Vector2( + return new GameLib.D3.Vector2( this.x * invS, this.y * invS ); } }; -GameLib.D3.Runtime.Vector2.prototype.set = function(x, y) { +GameLib.D3.Vector2.prototype.set = function(x, y) { this.x = x; this.y = y; }; -GameLib.D3.Runtime.Vector2.prototype.clamp = function(min, max) { - return new GameLib.D3.Runtime.Vector2( +GameLib.D3.Vector2.prototype.clamp = function(min, max) { + return new GameLib.D3.Vector2( Math.max(min.x, Math.min(max.x, this.x)), Math.max(min.y, Math.min(max.y, this.y)) ); }; -GameLib.D3.Runtime.Vector2.prototype.length = function() { +GameLib.D3.Vector2.prototype.length = function() { return Math.sqrt(this.x * this.x + this.y * this.y); }; -GameLib.D3.Runtime.Vector2.prototype.dot = function(v) { +GameLib.D3.Vector2.prototype.dot = function(v) { return this.x * v.x + this.y * v.y; }; -GameLib.D3.Runtime.Vector2.prototype.normalize = function() { +GameLib.D3.Vector2.prototype.normalize = function() { return this.multiply(1.0 / this.length()); }; -GameLib.D3.Runtime.Vector2.prototype.angle = function() { +GameLib.D3.Vector2.prototype.angle = function() { var angle = Math.atan2(this.y, this.x); if ( angle < 0 ) angle += 2 * Math.PI; return angle; }; -GameLib.D3.Runtime.Vector2.prototype.lerp = function ( v, alpha ) { - return new GameLib.D3.Runtime.Vector2( +GameLib.D3.Vector2.prototype.lerp = function ( v, alpha ) { + return new GameLib.D3.Vector2( this.x + ( v.x - this.x ) * alpha, this.y + ( v.y - this.y ) * alpha ); diff --git a/src/game-lib-runtime-vector3.js b/src/game-lib-vector3.js similarity index 67% rename from src/game-lib-runtime-vector3.js rename to src/game-lib-vector3.js index f0ba13e..c72ea2e 100644 --- a/src/game-lib-runtime-vector3.js +++ b/src/game-lib-vector3.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.D3.Runtime.Vector3 = function RuntimeVector3(graphics, parentObject, vector3, grain) { +GameLib.D3.Vector3 = function RuntimeVector3(graphics, parentObject, vector3, grain) { for (var property in vector3) { if (vector3.hasOwnProperty(property)) { @@ -14,7 +14,7 @@ GameLib.D3.Runtime.Vector3 = function RuntimeVector3(graphics, parentObject, vec } } - GameLib.D3.Utils.Extend(GameLib.D3.Runtime.Vector3, GameLib.D3.API.Vector3); + GameLib.D3.Utils.Extend(GameLib.D3.Vector3, GameLib.D3.API.Vector3); this.graphics = graphics; @@ -35,7 +35,7 @@ GameLib.D3.Runtime.Vector3 = function RuntimeVector3(graphics, parentObject, vec * @param update * @returns {*} */ -GameLib.D3.Runtime.Vector3.prototype.createInstance = function(update) { +GameLib.D3.Vector3.prototype.createInstance = function(update) { var instance = null; @@ -54,7 +54,7 @@ GameLib.D3.Runtime.Vector3.prototype.createInstance = function(update) { /** * Updates the instance vector, calls updateInstance on the parent object */ -GameLib.D3.Runtime.Vector3.prototype.updateInstance = function() { +GameLib.D3.Vector3.prototype.updateInstance = function() { this.createInstance(true); @@ -67,7 +67,7 @@ GameLib.D3.Runtime.Vector3.prototype.updateInstance = function() { * Converts runtime vector to API Vector * @returns {GameLib.D3.API.Vector3} */ -GameLib.D3.Runtime.Vector3.prototype.toApiVector = function() { +GameLib.D3.Vector3.prototype.toApiVector = function() { return new GameLib.D3.API.Vector3( this.x, this.y, @@ -75,24 +75,24 @@ GameLib.D3.Runtime.Vector3.prototype.toApiVector = function() { ); }; -GameLib.D3.Runtime.Vector3.prototype.subtract = function (v) { - return new GameLib.D3.Runtime.Vector3( +GameLib.D3.Vector3.prototype.subtract = function (v) { + return new GameLib.D3.Vector3( this.x - v.x, this.y - v.y, this.z - v.z ); }; -GameLib.D3.Runtime.Vector3.prototype.sub = function (v) { - return new GameLib.D3.Runtime.Vector3( +GameLib.D3.Vector3.prototype.sub = function (v) { + return new GameLib.D3.Vector3( this.x - v.x, this.y - v.y, this.z - v.z ); }; -GameLib.D3.Runtime.Vector3.prototype.cross = function (v) { - return new GameLib.D3.Runtime.Vector3( +GameLib.D3.Vector3.prototype.cross = function (v) { + return new GameLib.D3.Vector3( this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x @@ -100,66 +100,66 @@ GameLib.D3.Runtime.Vector3.prototype.cross = function (v) { }; -GameLib.D3.Runtime.Vector3.clockwise = function (u, v, w, viewPoint) { - var normal = GameLib.D3.Runtime.Vector3.normal(u, v, w); +GameLib.D3.Vector3.clockwise = function (u, v, w, viewPoint) { + var normal = GameLib.D3.Vector3.normal(u, v, w); var uv = u.copy(); var winding = normal.dot(uv.subtract(viewPoint)); return (winding > 0); }; -GameLib.D3.Runtime.Vector3.normal = function (u, v, w) { +GameLib.D3.Vector3.normal = function (u, v, w) { var vv = v.copy(); var wv = w.copy(); return vv.subtract(u).cross(wv.subtract(u)); }; -GameLib.D3.Runtime.Vector3.prototype.lookAt = function (at, up) { +GameLib.D3.Vector3.prototype.lookAt = function (at, up) { var lookAtMatrix = GameLib.D3.Matrix4.lookAt(this, at, up); return this.multiply(lookAtMatrix); }; -GameLib.D3.Runtime.Vector3.prototype.translate = function (v) { +GameLib.D3.Vector3.prototype.translate = function (v) { this.x += v.x; this.y += v.y; this.z += v.z; return this; }; -GameLib.D3.Runtime.Vector3.prototype.add = function (v) { - return new GameLib.D3.Runtime.Vector3( +GameLib.D3.Vector3.prototype.add = function (v) { + return new GameLib.D3.Vector3( this.x + v.x, this.y + v.y, this.z + v.z ); }; -GameLib.D3.Runtime.Vector3.prototype.squared = function () { +GameLib.D3.Vector3.prototype.squared = function () { return this.x * this.x + this.y * this.y + this.z * this.z; }; -GameLib.D3.Runtime.Vector3.prototype.copy = function () { - return new GameLib.D3.Runtime.Vector3( +GameLib.D3.Vector3.prototype.copy = function () { + return new GameLib.D3.Vector3( this.x, this.y, this.z ); }; -GameLib.D3.Runtime.Vector3.prototype.set = function (x, y, z) { +GameLib.D3.Vector3.prototype.set = function (x, y, z) { this.x = x; this.y = y; this.z = z; }; -GameLib.D3.Runtime.Vector3.prototype.lerp = function ( v, alpha ) { - return new GameLib.D3.Runtime.Vector3( +GameLib.D3.Vector3.prototype.lerp = function ( v, alpha ) { + return new GameLib.D3.Vector3( this.x + ( v.x - this.x ) * alpha, this.y + ( v.y - this.y ) * alpha, this.z + ( v.z - this.z ) * alpha ); }; -GameLib.D3.Runtime.Vector3.prototype.distanceTo = function(v) { +GameLib.D3.Vector3.prototype.distanceTo = function(v) { var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z; @@ -169,7 +169,7 @@ GameLib.D3.Runtime.Vector3.prototype.distanceTo = function(v) { /** * @return {number} */ -GameLib.D3.Runtime.Vector3.AngleDirection = function(forward, directionToCheck, up) { +GameLib.D3.Vector3.AngleDirection = function(forward, directionToCheck, up) { var perp = forward.cross(directionToCheck); var dir = perp.dot(up); @@ -183,10 +183,10 @@ GameLib.D3.Runtime.Vector3.AngleDirection = function(forward, directionToCheck, }; -GameLib.D3.Runtime.Vector3.prototype.multiply = function (s) { - if (s instanceof GameLib.D3.Runtime.Vector3) { +GameLib.D3.Vector3.prototype.multiply = function (s) { + if (s instanceof GameLib.D3.Vector3) { - return new GameLib.D3.Runtime.Vector3( + return new GameLib.D3.Vector3( this.x * s.x, this.y * s.y, this.z * s.z @@ -198,7 +198,7 @@ GameLib.D3.Runtime.Vector3.prototype.multiply = function (s) { var y = s.rows[1].x * this.x + s.rows[1].y * this.y + s.rows[1].z * this.z + s.rows[1].w; var z = s.rows[2].x * this.x + s.rows[2].y * this.y + s.rows[2].z * this.z + s.rows[2].w; - return new GameLib.D3.Runtime.Vector3( + return new GameLib.D3.Vector3( x, y, z @@ -210,7 +210,7 @@ GameLib.D3.Runtime.Vector3.prototype.multiply = function (s) { var y = s.rows[1].x * this.x + s.rows[1].y * this.y + s.rows[1].z * this.z; var z = s.rows[2].x * this.x + s.rows[2].y * this.y + s.rows[2].z * this.z; - return new GameLib.D3.Runtime.Vector3( + return new GameLib.D3.Vector3( x, y, z @@ -218,7 +218,7 @@ GameLib.D3.Runtime.Vector3.prototype.multiply = function (s) { } else if(!isNaN(parseFloat(s)) && isFinite(s)) { - return new GameLib.D3.Runtime.Vector3( + return new GameLib.D3.Vector3( this.x * s, this.y * s, this.z * s @@ -233,11 +233,11 @@ GameLib.D3.Runtime.Vector3.prototype.multiply = function (s) { }; -GameLib.D3.Runtime.Vector3.prototype.dot = function (v) { +GameLib.D3.Vector3.prototype.dot = function (v) { return (this.x * v.x) + (this.y * v.y) + (this.z * v.z); }; -GameLib.D3.Runtime.Vector3.prototype.normalize = function () { +GameLib.D3.Vector3.prototype.normalize = function () { var EPSILON = 0.000001; var v2 = this.squared(); @@ -246,22 +246,22 @@ GameLib.D3.Runtime.Vector3.prototype.normalize = function () { } var invLength = 1.0 / Math.sqrt(v2); - return new GameLib.D3.Runtime.Vector3( + return new GameLib.D3.Vector3( this.x * invLength, this.y * invLength, this.z * invLength ); }; -GameLib.D3.Runtime.Vector3.prototype.clone = function () { - return new GameLib.D3.Runtime.Vector3( +GameLib.D3.Vector3.prototype.clone = function () { + return new GameLib.D3.Vector3( this.x, this.y, this.z ); }; -GameLib.D3.Runtime.Vector3.prototype.applyQuaternion = function(q) { +GameLib.D3.Vector3.prototype.applyQuaternion = function(q) { var x = this.x, y = this.y, z = this.z; var qx = q.x, qy = q.y, qz = q.z, qw = q.w; @@ -274,14 +274,14 @@ GameLib.D3.Runtime.Vector3.prototype.applyQuaternion = function(q) { // calculate result * inverse quat - return new GameLib.D3.Runtime.Vector3( + return new GameLib.D3.Vector3( ix * qw + iw * - qx + iy * - qz - iz * - qy, iy * qw + iw * - qy + iz * - qx - ix * - qz, iz * qw + iw * - qz + ix * - qy - iy * - qx ); }; -GameLib.D3.Runtime.Vector3.prototype.clamp = function(min, max) { +GameLib.D3.Vector3.prototype.clamp = function(min, max) { this.x = Math.max( min.x, Math.min( max.x, this.x ) ); this.y = Math.max( min.y, Math.min( max.y, this.y ) ); this.z = Math.max( min.z, Math.min( max.z, this.z ) ); @@ -289,22 +289,22 @@ GameLib.D3.Runtime.Vector3.prototype.clamp = function(min, max) { return this; }; -GameLib.D3.Runtime.Vector3.prototype.negate = function() { +GameLib.D3.Vector3.prototype.negate = function() { this.x = -this.x; this.y = -this.y; this.z = -this.z; return this; }; -GameLib.D3.Runtime.Vector3.prototype.length = function() { +GameLib.D3.Vector3.prototype.length = function() { return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z ); }; -GameLib.D3.Runtime.Vector3.prototype.reflect = function(normal) { +GameLib.D3.Vector3.prototype.reflect = function(normal) { return this.sub( v1.copy( normal ).multiply( 2 * this.dot( normal ) ) ); }; -GameLib.D3.Runtime.Vector3.prototype.angleTo = function (v) { +GameLib.D3.Vector3.prototype.angleTo = function (v) { var theta = this.dot( v ) / ( Math.sqrt( this.lengthSq() * v.lengthSq() ) ); return Math.acos( exports.Math.clamp( theta, - 1, 1 ) ); }; \ No newline at end of file diff --git a/src/game-lib-runtime-vector4.js b/src/game-lib-vector4.js similarity index 78% rename from src/game-lib-runtime-vector4.js rename to src/game-lib-vector4.js index af30af8..74df1db 100644 --- a/src/game-lib-runtime-vector4.js +++ b/src/game-lib-vector4.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.D3.Runtime.Vector4 = function RuntimeVector4(graphics, parentObject, vector4, grain) { +GameLib.D3.Vector4 = function RuntimeVector4(graphics, parentObject, vector4, grain) { for (var property in vector4) { if (vector4.hasOwnProperty(property)) { @@ -14,7 +14,7 @@ GameLib.D3.Runtime.Vector4 = function RuntimeVector4(graphics, parentObject, vec } } - GameLib.D3.Utils.Extend(GameLib.D3.Runtime.Vector4, GameLib.D3.API.Vector4); + GameLib.D3.Utils.Extend(GameLib.D3.Vector4, GameLib.D3.API.Vector4); this.graphics = graphics; @@ -35,7 +35,7 @@ GameLib.D3.Runtime.Vector4 = function RuntimeVector4(graphics, parentObject, vec * @param update * @returns {*} */ -GameLib.D3.Runtime.Vector4.prototype.createInstance = function(update) { +GameLib.D3.Vector4.prototype.createInstance = function(update) { var instance = null; @@ -55,7 +55,7 @@ GameLib.D3.Runtime.Vector4.prototype.createInstance = function(update) { /** * Updates the instance vector, calls updateInstance on the parent object */ -GameLib.D3.Runtime.Vector4.prototype.updateInstance = function() { +GameLib.D3.Vector4.prototype.updateInstance = function() { this.createInstance(true); @@ -68,7 +68,7 @@ GameLib.D3.Runtime.Vector4.prototype.updateInstance = function() { * Converts runtime vector to API Vector * @returns {GameLib.D3.API.Vector4} */ -GameLib.D3.Runtime.Vector4.prototype.toApiVector = function() { +GameLib.D3.Vector4.prototype.toApiVector = function() { return new GameLib.D3.API.Vector4( this.x, this.y, @@ -77,39 +77,15 @@ GameLib.D3.Runtime.Vector4.prototype.toApiVector = function() { ); }; -GameLib.D3.Runtime.Vector4 = function Vector4(x, y, z, w) { - - this.x = 0; - this.y = 0; - this.z = 0; - this.w = 1; - - if (x) { - this.x = x; - } - - if (y) { - this.y = y; - } - - if (z) { - this.z = z; - } - - if (w) { - this.w = w; - } -}; - -GameLib.D3.Runtime.Vector4.prototype.translate = function (v) { +GameLib.D3.Vector4.prototype.translate = function (v) { this.x += v.x; this.y += v.y; this.z += v.z; return this; }; -GameLib.D3.Runtime.Vector4.prototype.copy = function () { - return new GameLib.D3.Runtime.Vector4( +GameLib.D3.Vector4.prototype.copy = function () { + return new GameLib.D3.Vector4( this.x, this.y, this.z, @@ -117,8 +93,8 @@ GameLib.D3.Runtime.Vector4.prototype.copy = function () { ); }; -GameLib.D3.Runtime.Vector4.prototype.multiply = function (s) { - if (s instanceof GameLib.D3.API.Vector3) { +GameLib.D3.Vector4.prototype.multiply = function (s) { + if (s instanceof GameLib.D3.Vector3) { this.x *= s.x; this.y *= s.y; this.z *= s.z; @@ -138,7 +114,7 @@ GameLib.D3.Runtime.Vector4.prototype.multiply = function (s) { }; -GameLib.D3.Runtime.Vector4.prototype.normalize = function () { +GameLib.D3.Vector4.prototype.normalize = function () { // note - leave w untouched var EPSILON = 0.000001; @@ -158,7 +134,7 @@ GameLib.D3.Runtime.Vector4.prototype.normalize = function () { return this; }; -GameLib.D3.Runtime.Vector4.prototype.subtract = function (v) { +GameLib.D3.Vector4.prototype.subtract = function (v) { if (v instanceof GameLib.D3.API.Vector3) { this.x -= v.x; @@ -166,7 +142,7 @@ GameLib.D3.Runtime.Vector4.prototype.subtract = function (v) { this.z -= v.z; } - if (v instanceof GameLib.D3.Runtime.Vector4) { + if (v instanceof GameLib.D3.Vector4) { this.x -= v.x; this.y -= v.y; this.z -= v.z; @@ -176,14 +152,14 @@ GameLib.D3.Runtime.Vector4.prototype.subtract = function (v) { return this; }; -GameLib.D3.Runtime.Vector4.Points = function () { +GameLib.D3.Vector4.Points = function () { this.vectors = []; }; -GameLib.D3.Runtime.Vector4.Points.prototype.add = function (vector) { +GameLib.D3.Vector4.Points.prototype.add = function (vector) { if (vector instanceof GameLib.D3.API.Vector3) { - vector = new GameLib.D3.Runtime.Vector4( + vector = new GameLib.D3.Vector4( vector.x, vector.y, vector.z, @@ -191,7 +167,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.add = function (vector) { ) } - if (!vector instanceof GameLib.D3.Runtime.Vector4) { + if (!vector instanceof GameLib.D3.Vector4) { console.warn("Vector needs to be of type Vector4"); throw new Error("Vector needs to be of type Vector4"); } @@ -201,7 +177,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.add = function (vector) { return this; }; -GameLib.D3.Runtime.Vector4.Points.prototype.copy = function () { +GameLib.D3.Vector4.Points.prototype.copy = function () { var vectors = []; @@ -212,7 +188,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.copy = function () { return vectors; }; -GameLib.D3.Runtime.Vector4.Points.prototype.maximizeXDistance = function (grain) { +GameLib.D3.Vector4.Points.prototype.maximizeXDistance = function (grain) { // console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2)); @@ -257,7 +233,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.maximizeXDistance = function (grain) }; -GameLib.D3.Runtime.Vector4.Points.prototype.maximizeYDistance = function (grain) { +GameLib.D3.Vector4.Points.prototype.maximizeYDistance = function (grain) { // console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2)); @@ -302,7 +278,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.maximizeYDistance = function (grain) }; -GameLib.D3.Runtime.Vector4.Points.prototype.lookAt = function (at, up) { +GameLib.D3.Vector4.Points.prototype.lookAt = function (at, up) { var polyCenter = this.average(); @@ -310,9 +286,9 @@ GameLib.D3.Runtime.Vector4.Points.prototype.lookAt = function (at, up) { var lookAtMatrix = new GameLib.D3.Matrix4().lookAt(polyCenter, at, up); - lookAtMatrix.rows[0] = new GameLib.D3.Runtime.Vector4(1, 0, 0, 0); - lookAtMatrix.rows[1] = new GameLib.D3.Runtime.Vector4(0, 0, 1, 0); - lookAtMatrix.rows[2] = new GameLib.D3.Runtime.Vector4(0, 1, 0, 0); + lookAtMatrix.rows[0] = new GameLib.D3.Vector4(1, 0, 0, 0); + lookAtMatrix.rows[1] = new GameLib.D3.Vector4(0, 0, 1, 0); + lookAtMatrix.rows[2] = new GameLib.D3.Vector4(0, 1, 0, 0); console.log("look at matrix : " + JSON.stringify(lookAtMatrix, null, 2)); @@ -323,7 +299,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.lookAt = function (at, up) { } }; -GameLib.D3.Runtime.Vector4.Points.prototype.distances = function () { +GameLib.D3.Vector4.Points.prototype.distances = function () { var minX = this.vectors[0].x; var minY = this.vectors[0].y; @@ -362,7 +338,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.distances = function () { ) }; -GameLib.D3.Runtime.Vector4.Points.prototype.average = function () { +GameLib.D3.Vector4.Points.prototype.average = function () { var averageX = 0; var averageY = 0; var averageZ = 0; @@ -380,7 +356,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.average = function () { ); }; -GameLib.D3.Runtime.Vector4.Points.prototype.negate = function () { +GameLib.D3.Vector4.Points.prototype.negate = function () { for (var i = 0; i < this.vectors.length; i++) { this.vectors[i].x *= -1; @@ -392,7 +368,7 @@ GameLib.D3.Runtime.Vector4.Points.prototype.negate = function () { }; -GameLib.D3.Runtime.Vector4.Points.prototype.toOrigin = function () { +GameLib.D3.Vector4.Points.prototype.toOrigin = function () { var distanceFromOrigin = this.average().negate(); diff --git a/src/index.js b/src/index.js deleted file mode 100644 index c95e242..0000000 --- a/src/index.js +++ /dev/null @@ -1,94 +0,0 @@ -if (typeof GameLib === 'undefined') { - function GameLib() {} -} - -if (typeof GameLib.D3 === 'undefined') { - GameLib.D3 = function() {}; -} - -if (typeof require != 'undefined') { - GameLib.D3.API.Bone = require('./game-lib-bone.js'); - GameLib.D3.API.BoneWeight = require('./game-lib-bone-weight'); - GameLib.D3.Broadphase = require('./game-lib-broadphase'); - GameLib.D3.API.Color = require('./game-lib-color'); - GameLib.D3.FlyControls = require('./game-lib-fly-controls'); - GameLib.D3.Engine = require('./game-lib-engine'); - GameLib.D3.Game = require('./game-lib-game'); - GameLib.D3.Heightmap = require('./game-lib-heightmap'); - GameLib.D3.Image = require('./game-lib-image'); - GameLib.D3.Light = require('./game-lib-light'); - GameLib.D3.Material = require('./game-lib-material'); - GameLib.D3.Matrix3 = require('./game-lib-matrix-3'); - GameLib.D3.Matrix4 = require('./game-lib-matrix-4'); - GameLib.D3.Mesh = require('./game-lib-mesh'); - GameLib.D3.Physics = require('./game-lib-physics'); - GameLib.D3.PolyVertex = require('./game-lib-poly-vertex'); - GameLib.D3.RaycastVehicle = require('./game-lib-raycast-vehicle'); - GameLib.D3.RigidBody = require('./game-lib-rigid-body'); - GameLib.D3.RigidBodyVehicle = require('./game-lib-rigid-body-vehicle'); - GameLib.D3.Scene = require('./game-lib-scene'); - GameLib.D3.Shape = require('./game-lib-shape'); - GameLib.D3.Skeleton = require('./game-lib-skeleton'); - GameLib.D3.SkyBox = require('./game-lib-sky-box'); - GameLib.D3.Solver = require('./game-lib-solver'); - GameLib.D3.Texture = require('./game-lib-texture'); - GameLib.D3.TriangleEdge = require('./game-lib-triangle-edge'); - GameLib.D3.TriangleFace = require('./game-lib-triangle-face'); - GameLib.D3.API.Vector2 = require('./game-lib-vector-2'); - GameLib.D3.API.Vector3 = require('./game-lib-vector-3'); - GameLib.D3.API.Vector4 = require('./game-lib-vector-4'); - GameLib.D3.API.Vector4.Points = require('./game-lib-vector-4-points'); - GameLib.D3.Vertex = require('./game-lib-vertex'); - GameLib.D3.World = require('./game-lib-world'); -} - -if (typeof module != 'undefined') { - module.exports = GameLib; -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -if (typeof module != 'undefined') { - module.exports = GameLib; -} \ No newline at end of file