From 6c2a6a38acaece29172cd6bdd5f2b13e8680ca00 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Fri, 9 Dec 2016 20:32:09 +0100 Subject: [PATCH] something broken somewhere --- src/game-lib-api-bone.js | 2 +- src/game-lib-api-camera.js | 4 +- src/game-lib-api-color.js | 2 +- src/game-lib-api-component-interface.js | 270 +++---- src/game-lib-api-entity.js | 8 +- src/game-lib-api-light.js | 2 +- src/game-lib-api-matrix4.js | 40 +- src/game-lib-api-mesh.js | 11 +- ...tor4-a.js => game-lib-api-quaternion-a.js} | 37 +- ...s.js => game-lib-api-quaternion-points.js} | 34 +- src/game-lib-api-scene.js | 10 +- src/game-lib-broadphase.js | 4 + src/game-lib-camera.js | 85 ++- src/game-lib-component-entity-permutation.js | 2 +- src/game-lib-component-follow.js | 75 +- src/game-lib-component-interface.js | 334 +++++++++ src/game-lib-component-look-at.js | 41 +- src/game-lib-component-mesh-permutation.js | 2 +- src/game-lib-component-path-following.js | 187 ++--- src/game-lib-engine.js | 8 + src/game-lib-entity.js | 113 ++- src/game-lib-heightmap.js | 12 + src/game-lib-light.js | 89 ++- src/game-lib-material.js | 173 ++++- src/game-lib-matrix-3.js | 6 +- src/game-lib-matrix-4.js | 74 +- src/game-lib-mesh.js | 146 +++- ...-lib-vector4.js => game-lib-quaternion.js} | 55 +- src/game-lib-rigid-body.js | 10 +- src/game-lib-scene.js | 683 +++++++----------- src/game-lib-shape.js | 34 + src/game-lib-solver.js | 5 + src/game-lib-spline.js | 35 + src/game-lib-texture-map.js | 12 + src/game-lib-texture-maps.js | 21 + src/game-lib-texture.js | 92 +++ src/game-lib-utils.js | 40 +- src/game-lib-world.js | 28 + 38 files changed, 1887 insertions(+), 899 deletions(-) rename src/{game-lib-api-vector4-a.js => game-lib-api-quaternion-a.js} (66%) rename src/{game-lib-api-vector4-points.js => game-lib-api-quaternion-points.js} (81%) rename src/{game-lib-vector4.js => game-lib-quaternion.js} (58%) diff --git a/src/game-lib-api-bone.js b/src/game-lib-api-bone.js index 503d443..ab3bd55 100644 --- a/src/game-lib-api-bone.js +++ b/src/game-lib-api-bone.js @@ -39,7 +39,7 @@ GameLib.D3.API.Bone = function Bone( this.parentBoneId = parentBoneId; if (typeof quaternion == 'undefined') { - quaternion = new GameLib.D3.API.Vector4(); + quaternion = new GameLib.D3.API.Quaternion(); } this.quaternion = quaternion; diff --git a/src/game-lib-api-camera.js b/src/game-lib-api-camera.js index 6777019..4e56a59 100644 --- a/src/game-lib-api-camera.js +++ b/src/game-lib-api-camera.js @@ -15,7 +15,7 @@ * @param maxY * @param minZ * @param maxZ - * @param quaternion GameLib.D3.Vector4 + * @param quaternion GameLib.D3.Quaternion * @constructor */ GameLib.D3.API.Camera = function( @@ -81,7 +81,7 @@ GameLib.D3.API.Camera = function( this.position = position; if (GameLib.D3.Utils.UndefinedOrNull(quaternion)) { - quaternion = new GameLib.D3.API.Vector4(); + quaternion = new GameLib.D3.API.Quaternion(); } this.quaternion = quaternion; diff --git a/src/game-lib-api-color.js b/src/game-lib-api-color.js index 5e58ab5..d114cbc 100644 --- a/src/game-lib-api-color.js +++ b/src/game-lib-api-color.js @@ -1,4 +1,4 @@ -GameLib.D3.API.Color = function Color(r, g, b, a) { +GameLib.D3.API.Color = function ApiColor(r, g, b, a) { if (GameLib.D3.Utils.UndefinedOrNull(r)) { r = 1; diff --git a/src/game-lib-api-component-interface.js b/src/game-lib-api-component-interface.js index 344a865..9d29925 100644 --- a/src/game-lib-api-component-interface.js +++ b/src/game-lib-api-component-interface.js @@ -3,81 +3,75 @@ * @param id * @param name * @param componentType - * @param camera - * @param startColor - * @param endColor - * @param lerpSpeed - * @param targetEntity + * @param camera GameLib.D3.Camera + * @param parentEntity GameLib.D3.Entity + * @param targetEntity GameLib.D3.Entity * @param targetOffset * @param minDistance * @param moveSpeed + * @param target + * @param targetToParent + * @param rotatedTargetOffset + * @param rotated * @param rotationSpeed - * @param positionOffset - * @param quaternionOffset - * @param scaleOffset - * @param sensorLength - * @param spline - * @param normalSpline - * @param acceleration + * @param lookAtMatrix + * @param spline GameLib.D3.Spline + * @param mesh GameLib.D3.Mesh + * @param accelleration * @param maxSpeed * @param baseOffset * @param maxOffset * @param steeringSpeed - * @param frontLWheelIndex - * @param frontRWheelIndex - * @param backLWheelIndex - * @param backRWheelIndex - * @param maxForce - * @param steering - * @param entitiesToCheck - * @param onInside - * @param onEnter - * @param onLeave - * @param onSetParent - * @param sphereRadius - * @param world - * @param maxSteerAngle + * @param up + * @param currentOffset + * @param currentPathValue + * @param currentSpeed + * @param direction + * @param currentPosition * @constructor */ GameLib.D3.API.ComponentInterface = function( + // General id, name, componentType, + + // Camera Component camera, - startColor, - endColor, - lerpSpeed, + parentEntity, + + // Follow Component targetEntity, targetOffset, minDistance, moveSpeed, + target, + targetToParent, + rotatedTargetOffset, + rotated, + + // LookAt Component rotationSpeed, - positionOffset, - quaternionOffset, - scaleOffset, - sensorLength, + lookAtMatrix, + up, + + // Path Following Component spline, - normalSpline, - acceleration, + mesh, + accelleration, maxSpeed, baseOffset, maxOffset, steeringSpeed, - frontLWheelIndex, - frontRWheelIndex, - backLWheelIndex, - backRWheelIndex, - maxForce, - steering, - entitiesToCheck, - onInside, - onEnter, - onLeave, - onSetParent, - sphereRadius, - world, - maxSteerAngle + currentOffset, + currentPathValue, + currentSpeed, + direction, + currentPosition ) { + /** + * General + */ if (GameLib.D3.Utils.UndefinedOrNull(id)) { id = GameLib.D3.Utils.RandomId(); } @@ -93,88 +87,100 @@ GameLib.D3.API.ComponentInterface = function( } this.componentType = componentType; + /** + * Camera Component + */ + if (GameLib.D3.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; + if (GameLib.D3.Utils.UndefinedOrNull(camera)) { camera = null; } this.camera = camera; - if (GameLib.D3.Utils.UndefinedOrNull(startColor)) { - startColor = new GameLib.D3.API.Vector3(0, 0, 0); - } - this.startColor = startColor; - - if (GameLib.D3.Utils.UndefinedOrNull(endColor)) { - endColor = new GameLib.D3.API.Vector3(1, 1, 1); - } - this.endColor = endColor; - - if (GameLib.D3.Utils.UndefinedOrNull(lerpSpeed)) { - lerpSpeed = 1.0; - } - this.lerpSpeed = lerpSpeed; - + /** + * Follow Component + */ if (GameLib.D3.Utils.UndefinedOrNull(targetEntity)) { targetEntity = null; } this.targetEntity = targetEntity; - if (GameLib.D3.Utils.UndefinedOrNull(targetOffset)) { + if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) { targetOffset = new GameLib.D3.API.Vector3(0, 0, 0); } this.targetOffset = targetOffset; - if (GameLib.D3.Utils.UndefinedOrNull(minDistance)) { - minDistance = 0; - } - this.minDistance = minDistance; - if (GameLib.D3.Utils.UndefinedOrNull(moveSpeed)) { moveSpeed = 12.5; } this.moveSpeed = moveSpeed; + if (GameLib.D3.Utils.UndefinedOrNull(minDistance)) { + minDistance = 0; + } + this.minDistance = minDistance; + + if(GameLib.D3.Utils.UndefinedOrNull(target)) { + target = new GameLib.D3.API.Vector3(0, 0, 0); + } + this.target = target; + + if(GameLib.D3.Utils.UndefinedOrNull(targetToParent)) { + targetToParent = new GameLib.D3.API.Vector3(0, 0, 0); + } + this.targetToParent = targetToParent; + + if(GameLib.D3.Utils.UndefinedOrNull(rotatedTargetOffset)) { + rotatedTargetOffset = new GameLib.D3.API.Vector3(0, 0, 0); + } + this.rotatedTargetOffset = rotatedTargetOffset; + + if (GameLib.D3.Utils.UndefinedOrNull(rotated)) { + rotated = new GameLib.D3.API.Quaternion(); + } + this.rotated = rotated; + + /** + * LookAt Component + */ if (GameLib.D3.Utils.UndefinedOrNull(rotationSpeed)) { rotationSpeed = 22.0; } this.rotationSpeed = rotationSpeed; - if (GameLib.D3.Utils.UndefinedOrNull(positionOffset)) { - positionOffset = new GameLib.D3.API.Vector3(); + if (GameLib.D3.Utils.UndefinedOrNull(lookAtMatrix)) { + lookAtMatrix = new GameLib.D3.API.Matrix4(); } - this.positionOffset = positionOffset; + this.lookAtMatrix = lookAtMatrix; - if (GameLib.D3.Utils.UndefinedOrNull(quaternionOffset)) { - quaternionOffset = new GameLib.D3.API.Vector4(); + if(GameLib.D3.Utils.UndefinedOrNull(up)) { + up = new GameLib.D3.API.Vector3(0, 1, 0); } - this.quaternionOffset = quaternionOffset; - - if (GameLib.D3.Utils.UndefinedOrNull(scaleOffset)) { - scaleOffset = new GameLib.D3.API.Vector3(); - } - this.scaleOffset = scaleOffset; - - if (GameLib.D3.Utils.UndefinedOrNull(sensorLength)) { - sensorLength = 5; - } - this.sensorLength = sensorLength; + this.up = up; + /** + * Path Following Component + */ if (GameLib.D3.Utils.UndefinedOrNull(spline)) { - spline = new GameLib.D3.API.Spline(null, 'spline'); + spline = null; } this.spline = spline; - if (GameLib.D3.Utils.UndefinedOrNull(normalSpline)) { - normalSpline = new GameLib.D3.API.Spline(null, 'normal-spline'); + if (GameLib.D3.Utils.UndefinedOrNull(mesh)) { + mesh = null; } - this.normalSpline = normalSpline; + this.mesh = mesh; - if (GameLib.D3.Utils.UndefinedOrNull(acceleration)) { - acceleration = 2.0; + if (GameLib.D3.Utils.UndefinedOrNull(accelleration)) { + accelleration = 0.1; } - this.acceleration = acceleration; + this.accelleration = accelleration; if (GameLib.D3.Utils.UndefinedOrNull(maxSpeed)) { - maxSpeed = 10.0; + maxSpeed = 0.03; } this.maxSpeed = maxSpeed; @@ -193,74 +199,28 @@ GameLib.D3.API.ComponentInterface = function( } this.steeringSpeed = steeringSpeed; - if (GameLib.D3.Utils.UndefinedOrNull(frontLWheelIndex)) { - frontLWheelIndex = 1; + if (GameLib.D3.Utils.UndefinedOrNull(currentOffset)) { + currentOffset = new GameLib.D3.API.Vector3(); } - this.frontLWheelIndex = frontLWheelIndex; + this.currentOffset = currentOffset; - if (GameLib.D3.Utils.UndefinedOrNull(frontRWheelIndex)) { - frontRWheelIndex = 2; + if (GameLib.D3.Utils.UndefinedOrNull(currentPathValue)) { + currentPathValue = 0; } - this.frontRWheelIndex = frontRWheelIndex; + this.currentPathValue = currentPathValue; - if (GameLib.D3.Utils.UndefinedOrNull(backLWheelIndex)) { - backLWheelIndex = 3; + if (GameLib.D3.Utils.UndefinedOrNull(currentSpeed)) { + currentSpeed = 0; } - this.backLWheelIndex = backLWheelIndex; + this.currentSpeed = currentSpeed; - if (GameLib.D3.Utils.UndefinedOrNull(backRWheelIndex)) { - backRWheelIndex = 4; + if (GameLib.D3.Utils.UndefinedOrNull(direction)) { + direction = 1; } - this.backRWheelIndex = backRWheelIndex; + this.direction = direction; - if (GameLib.D3.Utils.UndefinedOrNull(maxForce)) { - maxForce = 400; + if (GameLib.D3.Utils.UndefinedOrNull(currentPosition)) { + currentPosition = new GameLib.D3.API.Vector3(); } - this.maxForce = maxForce; - - if (GameLib.D3.Utils.UndefinedOrNull(steering)) { - steering = 0.5; - } - this.steering = steering; - - if (GameLib.D3.Utils.UndefinedOrNull(entitiesToCheck)) { - entitiesToCheck = []; - } - this.entitiesToCheck = entitiesToCheck; - - if (GameLib.D3.Utils.UndefinedOrNull(onInside)) { - onInside = null; - } - this.onInside = onInside; - - if (GameLib.D3.Utils.UndefinedOrNull(onEnter)) { - onEnter = null; - } - this.onEnter = onEnter; - - if (GameLib.D3.Utils.UndefinedOrNull(onLeave)) { - onLeave = null; - } - this.onLeave = onLeave; - - if (GameLib.D3.Utils.UndefinedOrNull(onSetParent)) { - onSetParent = null; - } - this.onSetParent = onSetParent; - - if (GameLib.D3.Utils.UndefinedOrNull(sphereRadius)) { - sphereRadius = 1.0; - } - this.sphereRadius = sphereRadius; - - if (GameLib.D3.Utils.UndefinedOrNull(world)) { - world = null; - } - this.world = world; - - if (GameLib.D3.Utils.UndefinedOrNull(maxSteerAngle)) { - maxSteerAngle = 0.5; - } - this.maxSteerAngle = maxSteerAngle; - + this.currentPosition = currentPosition; }; \ No newline at end of file diff --git a/src/game-lib-api-entity.js b/src/game-lib-api-entity.js index 01c34b8..edf71db 100644 --- a/src/game-lib-api-entity.js +++ b/src/game-lib-api-entity.js @@ -6,8 +6,8 @@ * @param position * @param quaternion * @param scale - * @param parentScene - * @param mesh + * @param parentScene GameLib.D3.Scene + * @param mesh GameLib.D3.Mesh * @constructor */ GameLib.D3.API.Entity = function Entity( @@ -41,7 +41,7 @@ GameLib.D3.API.Entity = function Entity( this.position = position; if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) { - quaternion = new GameLib.D3.API.Vector4(); + quaternion = new GameLib.D3.API.Quaternion(); } this.quaternion = quaternion; @@ -93,7 +93,7 @@ GameLib.D3.Entity = function Entity( this.position = position; if(GameLib.D3.Utils.UndefinedOrNull(quaternion)) { - quaternion = new GameLib.D3.API.Vector4(0, 0, 0, 1); + quaternion = new GameLib.D3.API.Quaternion(0, 0, 0, 1); } this.quaternion = quaternion; diff --git a/src/game-lib-api-light.js b/src/game-lib-api-light.js index 9fe89bd..a15cdb5 100644 --- a/src/game-lib-api-light.js +++ b/src/game-lib-api-light.js @@ -70,7 +70,7 @@ GameLib.D3.API.Light = function( this.targetPosition = targetPosition; if (typeof quaternion == 'undefined'){ - quaternion = new GameLib.D3.API.Vector4(); + quaternion = new GameLib.D3.API.Quaternion(); } this.quaternion = quaternion; diff --git a/src/game-lib-api-matrix4.js b/src/game-lib-api-matrix4.js index 6493ddf..55a181d 100644 --- a/src/game-lib-api-matrix4.js +++ b/src/game-lib-api-matrix4.js @@ -1,9 +1,9 @@ /** * Api Matrix 4 - * @param row0 GameLib.D3.API.Vector4 - * @param row1 GameLib.D3.API.Vector4 - * @param row2 GameLib.D3.API.Vector4 - * @param row3 GameLib.D3.API.Vector4 + * @param row0 GameLib.D3.API.Quaternion + * @param row1 GameLib.D3.API.Quaternion + * @param row2 GameLib.D3.API.Quaternion + * @param row3 GameLib.D3.API.Quaternion * @constructor */ GameLib.D3.API.Matrix4 = function ApiMatrix4( @@ -15,42 +15,42 @@ GameLib.D3.API.Matrix4 = function ApiMatrix4( this.rows = []; if (GameLib.D3.Utils.UndefinedOrNull(row0)) { - row0 = new GameLib.D3.API.Vector4(1, 0, 0, 0); + row0 = new GameLib.D3.API.Quaternion(1, 0, 0, 0); } this.rows[0] = row0; if (GameLib.D3.Utils.UndefinedOrNull(row1)) { - row1 = new GameLib.D3.API.Vector4(0, 1, 0, 0); + row1 = new GameLib.D3.API.Quaternion(0, 1, 0, 0); } this.rows[1] = row1; if (GameLib.D3.Utils.UndefinedOrNull(row2)) { - row2 = new GameLib.D3.API.Vector4(0, 0, 1, 0); + row2 = new GameLib.D3.API.Quaternion(0, 0, 1, 0); } this.rows[2] = row2; if (GameLib.D3.Utils.UndefinedOrNull(row3)) { - row3 = new GameLib.D3.API.Vector4(0, 0, 0, 1); + row3 = new GameLib.D3.API.Quaternion(0, 0, 0, 1); } this.rows[3] = row3; }; GameLib.D3.API.Matrix4.prototype.rotationMatrixX = function (radians) { this.identity(); - this.rows[1] = new GameLib.D3.API.Vector4(0, Math.cos(radians), -1 * Math.sin(radians), 0); - this.rows[2] = new GameLib.D3.API.Vector4(0, Math.sin(radians), Math.cos(radians), 0); + this.rows[1] = new GameLib.D3.API.Quaternion(0, Math.cos(radians), -1 * Math.sin(radians), 0); + this.rows[2] = new GameLib.D3.API.Quaternion(0, Math.sin(radians), Math.cos(radians), 0); return this; }; GameLib.D3.API.Matrix4.prototype.rotationMatrixY = function (radians) { this.identity(); - this.rows[0] = new GameLib.D3.API.Vector4( + this.rows[0] = new GameLib.D3.API.Quaternion( Math.cos(radians), 0, Math.sin(radians), 0 ); - this.rows[2] = new GameLib.D3.API.Vector4( + this.rows[2] = new GameLib.D3.API.Quaternion( -1 * Math.sin(radians), 0, Math.cos(radians), @@ -61,8 +61,8 @@ GameLib.D3.API.Matrix4.prototype.rotationMatrixY = function (radians) { GameLib.D3.API.Matrix4.prototype.rotationMatrixZ = function (radians) { this.identity(); - this.rows[0] = new GameLib.D3.API.Vector4(Math.cos(radians), -1 * Math.sin(radians), 0, 0); - this.rows[1] = new GameLib.D3.API.Vector4(Math.sin(radians), Math.cos(radians), 0, 0); + this.rows[0] = new GameLib.D3.API.Quaternion(Math.cos(radians), -1 * Math.sin(radians), 0, 0); + this.rows[1] = new GameLib.D3.API.Quaternion(Math.sin(radians), Math.cos(radians), 0, 0); return this; }; @@ -85,8 +85,8 @@ GameLib.D3.API.Matrix4.prototype.rotateZ = function (radians, point) { }; GameLib.D3.API.Matrix4.prototype.multiply = function (mvp) { - if (mvp instanceof GameLib.D3.API.Vector4) { - return new GameLib.D3.API.Vector4( + if (mvp instanceof GameLib.D3.API.Quaternion) { + return new GameLib.D3.API.Quaternion( this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z + this.rows[0].w * mvp.w, this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z + this.rows[1].w * mvp.w, this.rows[2].x * mvp.x + this.rows[2].y * mvp.y + this.rows[2].z * mvp.z + this.rows[2].w * mvp.w, @@ -103,10 +103,10 @@ GameLib.D3.API.Matrix4.prototype.multiply = function (mvp) { GameLib.D3.API.Matrix4.prototype.identity = function () { this.rows = [ - new GameLib.D3.API.Vector4(1, 0, 0, 0), - new GameLib.D3.API.Vector4(0, 1, 0, 0), - new GameLib.D3.API.Vector4(0, 0, 1, 0), - new GameLib.D3.API.Vector4(0, 0, 0, 1) + new GameLib.D3.API.Quaternion(1, 0, 0, 0), + new GameLib.D3.API.Quaternion(0, 1, 0, 0), + new GameLib.D3.API.Quaternion(0, 0, 1, 0), + new GameLib.D3.API.Quaternion(0, 0, 0, 1) ]; }; diff --git a/src/game-lib-api-mesh.js b/src/game-lib-api-mesh.js index 462a9b3..598e70a 100644 --- a/src/game-lib-api-mesh.js +++ b/src/game-lib-api-mesh.js @@ -13,8 +13,7 @@ * @param skinIndices * @param skinWeights * @param position GameLib.D3.API.Vector3 - * @param quaternion GameLib.D3.API.Vector4 - * @param rotation GameLib.D3.API.Vector3 + * @param quaternion GameLib.D3.API.Quaternion * @param scale GameLib.D3.API.Vector3 * @param up * @constructor @@ -34,7 +33,6 @@ GameLib.D3.API.Mesh = function( skinWeights, position, quaternion, - rotation, scale, up ) { @@ -104,15 +102,10 @@ GameLib.D3.API.Mesh = function( this.position = position; if (GameLib.D3.Utils.UndefinedOrNull(quaternion)) { - quaternion = new GameLib.D3.API.Vector4(); + quaternion = new GameLib.D3.API.Quaternion(); } this.quaternion = quaternion; - if (GameLib.D3.Utils.UndefinedOrNull(rotation)) { - rotation = new GameLib.D3.API.Vector3(0,0,0); - } - this.rotation = rotation; - if (GameLib.D3.Utils.UndefinedOrNull(scale)) { scale = new GameLib.D3.API.Vector3(1,1,1); } diff --git a/src/game-lib-api-vector4-a.js b/src/game-lib-api-quaternion-a.js similarity index 66% rename from src/game-lib-api-vector4-a.js rename to src/game-lib-api-quaternion-a.js index 2e9cd9e..eec0afe 100644 --- a/src/game-lib-api-vector4-a.js +++ b/src/game-lib-api-quaternion-a.js @@ -1,4 +1,4 @@ -GameLib.D3.API.Vector4 = function ApiVector4(x, y, z, w) { +GameLib.D3.API.Quaternion = function ApiQuaternion(x, y, z, w, axis, angle) { if (GameLib.D3.Utils.UndefinedOrNull(x)) { x = 0; @@ -20,17 +20,26 @@ GameLib.D3.API.Vector4 = function ApiVector4(x, y, z, w) { } this.w = w; + if (GameLib.D3.Utils.UndefinedOrNull(axis)) { + axis = new GameLib.D3.API.Vector3(0, 0, 0); + } + this.axis = axis; + + if (GameLib.D3.Utils.UndefinedOrNull(angle)) { + angle = 0; + } + this.angle = angle; }; -GameLib.D3.API.Vector4.prototype.translate = function (v) { +GameLib.D3.API.Quaternion.prototype.translate = function (v) { this.x += v.x; this.y += v.y; this.z += v.z; return this; }; -GameLib.D3.API.Vector4.prototype.copy = function () { - return new GameLib.D3.API.Vector4( +GameLib.D3.API.Quaternion.prototype.copy = function () { + return new GameLib.D3.API.Quaternion( this.x, this.y, this.z, @@ -41,7 +50,7 @@ GameLib.D3.API.Vector4.prototype.copy = function () { /** * Note, this normalize function leaves 'w' component untouched */ -GameLib.D3.API.Vector4.prototype.normalize = function () { +GameLib.D3.API.Quaternion.prototype.normalize = function () { var EPSILON = 0.000001; @@ -58,7 +67,7 @@ GameLib.D3.API.Vector4.prototype.normalize = function () { this.z *= invLength; }; -GameLib.D3.API.Vector4.prototype.multiply = function (s) { +GameLib.D3.API.Quaternion.prototype.multiply = function (s) { if (s instanceof GameLib.D3.API.Vector3) { this.x *= s.x; this.y *= s.y; @@ -78,7 +87,19 @@ GameLib.D3.API.Vector4.prototype.multiply = function (s) { } }; -GameLib.D3.API.Vector4.prototype.subtract = function (v) { +GameLib.D3.API.Quaternion.prototype.setFromAngle = function (angle) { + + this.instance.setFromAxisAngle(this.axis.instance, angle); + + this.x = this.instance.x; + this.y = this.instance.y; + this.z = this.instance.z; + this.w = this.instance.w; + + return this; +}; + +GameLib.D3.API.Quaternion.prototype.subtract = function (v) { if (v instanceof GameLib.D3.API.Vector3) { this.x -= v.x; @@ -86,7 +107,7 @@ GameLib.D3.API.Vector4.prototype.subtract = function (v) { this.z -= v.z; } - if (v instanceof GameLib.D3.API.Vector4) { + if (v instanceof GameLib.D3.API.Quaternion) { this.x -= v.x; this.y -= v.y; this.z -= v.z; diff --git a/src/game-lib-api-vector4-points.js b/src/game-lib-api-quaternion-points.js similarity index 81% rename from src/game-lib-api-vector4-points.js rename to src/game-lib-api-quaternion-points.js index 98c1abe..49a718f 100644 --- a/src/game-lib-api-vector4-points.js +++ b/src/game-lib-api-quaternion-points.js @@ -1,11 +1,11 @@ -GameLib.D3.API.Vector4.Points = function () { +GameLib.D3.API.Quaternion.Points = function () { this.vectors = []; }; -GameLib.D3.API.Vector4.Points.prototype.add = function (vector) { +GameLib.D3.API.Quaternion.Points.prototype.add = function (vector) { if (vector instanceof GameLib.D3.API.Vector3) { - vector = new GameLib.D3.API.Vector4( + vector = new GameLib.D3.API.Quaternion( vector.x, vector.y, vector.z, @@ -13,9 +13,9 @@ GameLib.D3.API.Vector4.Points.prototype.add = function (vector) { ) } - if (!vector instanceof GameLib.D3.API.Vector4) { - console.warn("Vector needs to be of type Vector4"); - throw new Error("Vector needs to be of type Vector4"); + if (!vector instanceof GameLib.D3.API.Quaternion) { + console.warn("Vector needs to be of type Quaternion"); + throw new Error("Vector needs to be of type Quaternion"); } this.vectors.push(vector); @@ -23,7 +23,7 @@ GameLib.D3.API.Vector4.Points.prototype.add = function (vector) { return this; }; -GameLib.D3.API.Vector4.Points.prototype.copy = function () { +GameLib.D3.API.Quaternion.Points.prototype.copy = function () { var vectors = []; @@ -34,7 +34,7 @@ GameLib.D3.API.Vector4.Points.prototype.copy = function () { return vectors; }; -GameLib.D3.API.Vector4.Points.prototype.maximizeXDistance = function (grain) { +GameLib.D3.API.Quaternion.Points.prototype.maximizeXDistance = function (grain) { // console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2)); @@ -79,7 +79,7 @@ GameLib.D3.API.Vector4.Points.prototype.maximizeXDistance = function (grain) { }; -GameLib.D3.API.Vector4.Points.prototype.maximizeYDistance = function (grain) { +GameLib.D3.API.Quaternion.Points.prototype.maximizeYDistance = function (grain) { // console.log("vectors (before): " + JSON.stringify(this.vectors, null, 2)); @@ -124,7 +124,7 @@ GameLib.D3.API.Vector4.Points.prototype.maximizeYDistance = function (grain) { }; -GameLib.D3.API.Vector4.Points.prototype.lookAt = function (at, up) { +GameLib.D3.API.Quaternion.Points.prototype.lookAt = function (at, up) { var polyCenter = this.average(); @@ -132,9 +132,9 @@ GameLib.D3.API.Vector4.Points.prototype.lookAt = function (at, up) { var lookAtMatrix = new GameLib.D3.API.Matrix4().lookAt(polyCenter, at, up); - lookAtMatrix.rows[0] = new GameLib.D3.API.Vector4(1, 0, 0, 0); - lookAtMatrix.rows[1] = new GameLib.D3.API.Vector4(0, 0, 1, 0); - lookAtMatrix.rows[2] = new GameLib.D3.API.Vector4(0, 1, 0, 0); + lookAtMatrix.rows[0] = new GameLib.D3.API.Quaternion(1, 0, 0, 0); + lookAtMatrix.rows[1] = new GameLib.D3.API.Quaternion(0, 0, 1, 0); + lookAtMatrix.rows[2] = new GameLib.D3.API.Quaternion(0, 1, 0, 0); console.log("look at matrix : " + JSON.stringify(lookAtMatrix, null, 2)); @@ -145,7 +145,7 @@ GameLib.D3.API.Vector4.Points.prototype.lookAt = function (at, up) { } }; -GameLib.D3.API.Vector4.Points.prototype.distances = function () { +GameLib.D3.API.Quaternion.Points.prototype.distances = function () { var minX = this.vectors[0].x; var minY = this.vectors[0].y; @@ -184,7 +184,7 @@ GameLib.D3.API.Vector4.Points.prototype.distances = function () { ) }; -GameLib.D3.API.Vector4.Points.prototype.average = function () { +GameLib.D3.API.Quaternion.Points.prototype.average = function () { var averageX = 0; var averageY = 0; var averageZ = 0; @@ -202,7 +202,7 @@ GameLib.D3.API.Vector4.Points.prototype.average = function () { ); }; -GameLib.D3.API.Vector4.Points.prototype.negate = function () { +GameLib.D3.API.Quaternion.Points.prototype.negate = function () { for (var i = 0; i < this.vectors.length; i++) { this.vectors[i].x *= -1; @@ -214,7 +214,7 @@ GameLib.D3.API.Vector4.Points.prototype.negate = function () { }; -GameLib.D3.API.Vector4.Points.prototype.toOrigin = function () { +GameLib.D3.API.Quaternion.Points.prototype.toOrigin = function () { var distanceFromOrigin = this.average().negate(); diff --git a/src/game-lib-api-scene.js b/src/game-lib-api-scene.js index 6af8cfc..ab04bce 100644 --- a/src/game-lib-api-scene.js +++ b/src/game-lib-api-scene.js @@ -4,7 +4,7 @@ * @param path String * @param name String * @param meshes GameLib.D3.API.Mesh [] - * @param quaternion GameLib.D3.API.Vector4 + * @param quaternion GameLib.D3.API.Quaternion * @param position GameLib.D3.API.Vector3 * @param rotation GameLib.D3.API.Vector3 * @param scale GameLib.D3.API.Vector3 @@ -26,7 +26,6 @@ GameLib.D3.API.Scene = function( meshes, quaternion, position, - rotation, scale, parentSceneId, lights, @@ -59,7 +58,7 @@ GameLib.D3.API.Scene = function( this.meshes = meshes; if (GameLib.D3.Utils.UndefinedOrNull(quaternion)) { - quaternion = new GameLib.D3.API.Vector4(); + quaternion = new GameLib.D3.API.Quaternion(); } this.quaternion = quaternion; @@ -68,11 +67,6 @@ GameLib.D3.API.Scene = function( } this.position = position; - if (GameLib.D3.Utils.UndefinedOrNull(rotation)) { - rotation = new GameLib.D3.API.Vector3(); - } - this.rotation = rotation; - if (GameLib.D3.Utils.UndefinedOrNull(scale)) { scale = new GameLib.D3.API.Vector3(1,1,1); } diff --git a/src/game-lib-broadphase.js b/src/game-lib-broadphase.js index 8053e30..eb2bf5d 100644 --- a/src/game-lib-broadphase.js +++ b/src/game-lib-broadphase.js @@ -51,6 +51,10 @@ GameLib.D3.Broadphase.prototype.createInstance = function() { return instance; }; +GameLib.D3.Broadphase.prototype.toApiBroadphase = function() { + return null; +}; + /** * Broadphase Types * @type {number} diff --git a/src/game-lib-camera.js b/src/game-lib-camera.js index 69d806c..1c65014 100644 --- a/src/game-lib-camera.js +++ b/src/game-lib-camera.js @@ -24,7 +24,7 @@ GameLib.D3.Camera = function Camera( this.position ); - this.quaternion = new GameLib.D3.Vector4( + this.quaternion = new GameLib.D3.Quaternion( graphics, this, this.quaternion @@ -115,4 +115,85 @@ GameLib.D3.Camera.prototype.createInstance = function(update) { */ GameLib.D3.Camera.prototype.updateInstance = function() { this.instance = this.createInstance(true); -}; \ No newline at end of file +}; + +/** + * Converts a GameLib.D3.Camera to a new GameLib.D3.API.Camera + * @returns {GameLib.D3.API.Camera} + */ +GameLib.D3.Camera.prototype.toApiCamera = function() { + + return new GameLib.D3.API.Camera( + this.id, + this.cameraType, + this.name, + this.fov, + this.aspect, + this.near, + this.far, + this.position.toApiVector(), + this.lookAt.toApiVector(), + this.minX, + this.maxX, + this.minY, + this.maxY, + this.minZ, + this.maxZ, + this.quaternion.toApiQuaternion() + ); + +}; + +/** + * Converts from an Object camera to a GameLib.D3.Camera + * @param graphics GameLib.D3.Graphics + * @param objectCamera Object + * @returns {GameLib.D3.Camera} + * @constructor + */ +GameLib.D3.Camera.FromObjectCamera = function(graphics, objectCamera) { + + var apiCamera = new GameLib.D3.API.Camera( + objectCamera.id, + objectCamera.cameraType, + objectCamera.name, + objectCamera.fov, + objectCamera.aspect, + objectCamera.near, + objectCamera.far, + new GameLib.D3.API.Vector3( + objectCamera.position.x, + objectCamera.position.y, + objectCamera.position.z + ), + new GameLib.D3.API.Vector3( + objectCamera.lookAt.x, + objectCamera.lookAt.y, + objectCamera.lookAt.z + ), + objectCamera.minX, + objectCamera.maxX, + objectCamera.minY, + objectCamera.maxY, + objectCamera.minZ, + objectCamera.maxZ, + new GameLib.D3.API.Quaternion( + objectCamera.quaternion.x, + objectCamera.quaternion.y, + objectCamera.quaternion.z, + objectCamera.quaternion.w, + new GameLib.D3.API.Vector3( + objectCamera.quaternion.axis.x, + objectCamera.quaternion.axis.y, + objectCamera.quaternion.axis.z + ), + objectCamera.quaternion.angle + ) + ); + + return new GameLib.D3.Camera( + graphics, + apiCamera + ); + +}; diff --git a/src/game-lib-component-entity-permutation.js b/src/game-lib-component-entity-permutation.js index e9539ac..ea01997 100644 --- a/src/game-lib-component-entity-permutation.js +++ b/src/game-lib-component-entity-permutation.js @@ -29,7 +29,7 @@ GameLib.D3.ComponentEntityPermutation = function ComponentEntityPermutation( if(GameLib.D3.Utils.UndefinedOrNull(quaternionOffset)) { - quaternionOffset = new GameLib.D3.API.Vector4(0, 0, 0, 1); + quaternionOffset = new GameLib.D3.API.Quaternion(0, 0, 0, 1); } this.quaternionOffset = quaternionOffset; diff --git a/src/game-lib-component-follow.js b/src/game-lib-component-follow.js index 0fb230d..a837238 100644 --- a/src/game-lib-component-follow.js +++ b/src/game-lib-component-follow.js @@ -8,10 +8,10 @@ * @param minDistance * @param moveSpeed * @param parentEntity - * @param target GameLib.D3.Vector3 - * @param targetToParent GameLib.D3.Vector3 - * @param rotatedTargetOffset GameLib.D3.Vector3 - * @param rotated GameLib.D3.Vector4 + * @param target GameLib.D3.API.Vector3 + * @param targetToParent GameLib.D3.API.Vector3 + * @param rotatedTargetOffset GameLib.D3.API.Vector3 + * @param rotated GameLib.D3.API.Quaternion * @constructor */ GameLib.D3.ComponentFollow = function ComponentFollow( @@ -52,12 +52,14 @@ GameLib.D3.ComponentFollow = function ComponentFollow( this.targetEntity = targetEntity; if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) { - targetOffset = new GameLib.D3.Vector3( - this.graphics, - this, - new GameLib.D3.API.Vector3(0, 0, 0) - ); + targetOffset = new GameLib.D3.API.Vector3(0, 0, 0); } + targetOffset = new GameLib.D3.Vector3( + this.graphics, + this, + targetOffset + ); + this.targetOffset = targetOffset; if (GameLib.D3.Utils.UndefinedOrNull(moveSpeed)) { @@ -70,40 +72,49 @@ GameLib.D3.ComponentFollow = function ComponentFollow( } this.minDistance = minDistance; - if (GameLib.D3.Utils.UndefinedOrNull(target)) { - target = new GameLib.D3.Vector3( - this.graphics, - this, - new GameLib.D3.API.Vector3(0, 0, 0) - ); + if(GameLib.D3.Utils.UndefinedOrNull(target)) { + target = new GameLib.D3.API.Vector3(0, 0, 0); } + target = new GameLib.D3.Vector3( + this.graphics, + this, + target + ); + this.target = target; - if (GameLib.D3.Utils.UndefinedOrNull(targetToParent)) { - targetToParent = new GameLib.D3.Vector3( - this.graphics, - this, - new GameLib.D3.API.Vector3(0, 0, 0) - ); + + if(GameLib.D3.Utils.UndefinedOrNull(targetToParent)) { + targetToParent = new GameLib.D3.API.Vector3(0, 0, 0); } + targetToParent = new GameLib.D3.Vector3( + this.graphics, + this, + targetToParent + ); + this.targetToParent = targetToParent; - if (GameLib.D3.Utils.UndefinedOrNull(rotatedTargetOffset)) { - rotatedTargetOffset = new GameLib.D3.Vector3( - this.graphics, - this, - new GameLib.D3.API.Vector3(0, 0, 0) - ); + if(GameLib.D3.Utils.UndefinedOrNull(rotatedTargetOffset)) { + rotatedTargetOffset = new GameLib.D3.API.Vector3(0, 0, 0); } + rotatedTargetOffset = new GameLib.D3.Vector3( + this.graphics, + this, + rotatedTargetOffset + ); + this.rotatedTargetOffset = rotatedTargetOffset; if (GameLib.D3.Utils.UndefinedOrNull(rotated)) { - rotated = new GameLib.D3.Vector3( - this.graphics, - this, - new GameLib.D3.API.Vector3(0, 0, 0) - ); + rotated = new GameLib.D3.API.Quaternion(); } + rotated = new GameLib.D3.Quaternion( + this.graphics, + this, + rotated + ); + this.rotated = rotated; GameLib.D3.Utils.Extend(GameLib.D3.ComponentFollow, GameLib.D3.ComponentInterface); diff --git a/src/game-lib-component-interface.js b/src/game-lib-component-interface.js index a029b97..30c1701 100644 --- a/src/game-lib-component-interface.js +++ b/src/game-lib-component-interface.js @@ -37,7 +37,321 @@ GameLib.D3.ComponentInterface.COMPONENT_TRIGGER_SPHERE_SPHERE = 0xF; GameLib.D3.ComponentInterface.COMPONENT_VEHICLE_AI_OBJECT_AVOIDENCE = 0x10; GameLib.D3.ComponentInterface.COMPONENT_VEHICLE_AI_PATH_STEERING = 0x11; +GameLib.D3.ComponentInterface.prototype.toApiComponent = function() { + var apiComponent = null; + + if (this instanceof GameLib.D3.ComponentCamera) { + + apiComponent = new GameLib.D3.API.ComponentInterface( + this.id, + this.name, + GameLib.D3.ComponentInterface.COMPONENT_CAMERA, + GameLib.D3.Utils.IdOrNull(this.camera), + GameLib.D3.Utils.IdOrNull(this.parentEntity) + ); + + } else if (this instanceof GameLib.D3.ComponentFollow) { + + apiComponent = new GameLib.D3.API.ComponentInterface( + this.id, + this.name, + GameLib.D3.ComponentInterface.COMPONENT_FOLLOW, + null, + GameLib.D3.Utils.IdOrNull(this.parentEntity), + GameLib.D3.Utils.IdOrNull(this.targetEntity), + this.targetOffset.toApiVector(), + this.minDistance, + this.moveSpeed, + this.target.toApiVector(), + this.targetToParent.toApiVector(), + this.rotatedTargetOffset.toApiVector(), + this.rotated.toApiQuaternion() + ); + + } else if (this instanceof GameLib.D3.ComponentLookAt) { + + apiComponent = new GameLib.D3.API.ComponentInterface( + this.id, + this.name, + GameLib.D3.ComponentInterface.COMPONENT_LOOK_AT, + null, + GameLib.D3.Utils.IdOrNull(this.parentEntity), + GameLib.D3.Utils.IdOrNull(this.targetEntity), + this.targetOffset.toApiVector(), + null, + null, + null, + null, + null, + null, + this.rotationSpeed, + this.lookAtMatrix.toApiMatrix(), + this.up.toApiVector() + ) + } else if (this instanceof GameLib.D3.ComponentPathFollowing) { + + apiComponent = new GameLib.D3.API.ComponentInterface( + this.id, + this.name, + GameLib.D3.ComponentInterface.COMPONENT_PATH_FOLLOWING, + null, + GameLib.D3.Utils.IdOrNull(this.parentEntity), + GameLib.D3.Utils.IdOrNull(this.targetEntity), + this.targetOffset.toApiVector(), + null, + null, + null, + null, + null, + null, + null, + null, + this.up.toApiVector(), + GameLib.D3.Utils.IdOrNull(this.spline), + GameLib.D3.Utils.IdOrNull(this.mesh), + this.accelleration, + this.maxSpeed, + this.baseOffset.toApiVector(), + this.maxOffset.toApiVector(), + this.steeringSpeed, + this.currentOffset.toApiVector(), + this.currentPathValue, + this.currentSpeed, + this.direction, + this.currentPosition.toApiVector() + ); + + } else { + console.warn('cannot save components of this type yet: ' + this.componentType); + } + + return apiComponent; +}; + +/** + * Converts and Object component into a proper GameLib.D3.Component + * @param graphics + * @param objectComponent + * @constructor + */ +GameLib.D3.ComponentInterface.FromObjectComponent = function(graphics, objectComponent) { + + var component = null; + + switch (objectComponent.componentType) { + + case GameLib.D3.ComponentInterface.COMPONENT_CAMERA : + component = new GameLib.D3.ComponentCamera( + objectComponent.id, + objectComponent.name, + objectComponent.camera, + objectComponent.parentEntity + ); + break; + + case GameLib.D3.ComponentInterface.COMPONENT_FOLLOW : + component = new GameLib.D3.ComponentFollow( + objectComponent.id, + objectComponent.name, + graphics, + objectComponent.targetEntity, + new GameLib.D3.API.Vector3 ( + objectComponent.targetOffset.x, + objectComponent.targetOffset.y, + objectComponent.targetOffset.z + ), + objectComponent.minDistance, + objectComponent.moveSpeed, + objectComponent.parentEntity, + new GameLib.D3.API.Vector3( + objectComponent.target.x, + objectComponent.target.y, + objectComponent.target.z + ), + new GameLib.D3.API.Vector3( + objectComponent.targetToParent.x, + objectComponent.targetToParent.y, + objectComponent.targetToParent.z + ), + new GameLib.D3.API.Vector3( + objectComponent.rotatedTargetOffset.x, + objectComponent.rotatedTargetOffset.y, + objectComponent.rotatedTargetOffset.z + ), + new GameLib.D3.API.Quaternion( + objectComponent.rotated.x, + objectComponent.rotated.x, + objectComponent.rotated.x, + objectComponent.rotated.x, + new GameLib.D3.API.Vector3( + objectComponent.rotated.axis.x, + objectComponent.rotated.axis.y, + objectComponent.rotated.axis.z + ), + objectComponent.rotated.angle + ) + ); + break; + + case GameLib.D3.ComponentInterface.COMPONENT_LOOK_AT : + component = new GameLib.D3.ComponentLookAt( + objectComponent.id, + objectComponent.name, + graphics, + objectComponent.parentEntity, + objectComponent.targetEntity, + new GameLib.D3.API.Vector3( + objectComponent.targetOffset.x, + objectComponent.targetOffset.y, + objectComponent.targetOffset.z + ), + objectComponent.rotationSpeed, + objectComponent.lookAtMatrix, + new GameLib.D3.API.Vector3( + objectComponent.up.x, + objectComponent.up.y, + objectComponent.up.z + ) + ); + break; + + case GameLib.D3.ComponentInterface.COMPONENT_PATH_FOLLOWING : + component = new GameLib.D3.ComponentPathFollowing( + objectComponent.id, + objectComponent.name, + graphics, + objectComponent.parentEntity, + objectComponent.spline, + objectComponent.mesh, + objectComponent.accelleration, + objectComponent.maxSpeed, + new GameLib.D3.API.Vector3( + objectComponent.baseOffset.x, + objectComponent.baseOffset.y, + objectComponent.baseOffset.z + ), + new GameLib.D3.API.Vector3( + objectComponent.maxOffset.x, + objectComponent.maxOffset.y, + objectComponent.maxOffset.z + ), + objectComponent.steeringSpeed, + new GameLib.D3.API.Vector3( + objectComponent.targetOffset.x, + objectComponent.targetOffset.y, + objectComponent.targetOffset.z + ), + new GameLib.D3.API.Vector3( + objectComponent.currentOffset.x, + objectComponent.currentOffset.y, + objectComponent.currentOffset.z + ), + objectComponent.currentPathValue, + objectComponent.currentSpeed, + objectComponent.direction, + null, + null, + null, + null, + null, + null, + new GameLib.D3.API.Vector3( + objectComponent.up.x, + objectComponent.up.y, + objectComponent.up.z + ) + ); + break; + default: + console.warn('This type of component is not yet read from the database:' + objectComponent.componentType); + + } + + return component; + // + // var apiComponent = new GameLib.D3.API.ComponentInterface( + // objectComponent.id, + // objectComponent.name, + // objectComponent.componentType, + // objectComponent.camera, + // new GameLib.D3.API.Color( + // objectComponent.startColor.r, + // objectComponent.startColor.g, + // objectComponent.startColor.b, + // objectComponent.startColor.a + // ), + // new GameLib.D3.API.Color( + // objectComponent.endColor.r, + // objectComponent.endColor.g, + // objectComponent.endColor.b, + // objectComponent.endColor.a + // ), + // objectComponent.lerpSpeed, + // objectComponent.targetEntity, + // new GameLib.D3.API.Vector3( + // objectComponent.targetOffset.x, + // objectComponent.targetOffset.y, + // objectComponent.targetOffset.z + // ), + // objectComponent.minDistance, + // objectComponent.moveSpeed, + // objectComponent.rotationSpeed, + // new GameLib.D3.API.Vector3( + // objectComponent.positionOffset.x, + // objectComponent.positionOffset.y, + // objectComponent.positionOffset.z + // ), + // new GameLib.D3.API.Vector3( + // objectComponent.quaternionOffset.x, + // objectComponent.quaternionOffset.y, + // objectComponent.quaternionOffset.z + // ), + // new GameLib.D3.API.Vector3( + // objectComponent.scaleOffset.x, + // objectComponent.scaleOffset.y, + // objectComponent.scaleOffset.z + // ), + // objectComponent.sensorLength, + // objectComponent.spline, + // objectComponent.accelleration, + // objectComponent.maxSpeed, + // new GameLib.D3.API.Vector3( + // objectComponent.baseOffset.x, + // objectComponent.baseOffset.y, + // objectComponent.baseOffset.z + // ), + // new GameLib.D3.API.Vector3( + // objectComponent.maxOffset.x, + // objectComponent.maxOffset.y, + // objectComponent.maxOffset.z + // ), + // objectComponent.steeringSpeed, + // objectComponent.frontLWheelIndex, + // objectComponent.frontRWheelIndex, + // objectComponent.backLWheelIndex, + // objectComponent.backRWheelIndex, + // objectComponent.maxForce, + // objectComponent.steering, + // objectComponent.entitiesToCheck.map(function(objectEntity){ + // return GameLib.D3.Entity.FromObjectEntity(graphics, objectEntity); + // }), + // objectComponent.onInside, + // objectComponent.onEnter, + // objectComponent.onLeave, + // objectComponent.onSetParent, + // objectComponent.sphereRadius, + // GameLib.D3.World.FromObjectWorld(objectComponent.world), + // objectComponent.maxSteerAngle + // ); + // + // var component = null; + // + // switch (apiComponent.componentType) { + // + // } + +}; // will be not used GameLib.D3.ComponentInterface.prototype.setParentEntity = function( @@ -101,4 +415,24 @@ GameLib.D3.ComponentInterface.prototype.onSetParentEntity = function( GameLib.D3.ComponentInterface.prototype.clone = function() { return _.cloneDeep(this); +}; + +/** + * links component ids to actual objects + * @param idToObject Object linking object ids to objects + */ +GameLib.D3.ComponentInterface.prototype.linkObjects = function(idToObject) { + /** + * @param camera GameLib.D3.Camera + * @param parentEntity GameLib.D3.Entity + * @param targetEntity GameLib.D3.Entity + * @param spline GameLib.D3.Spline + * @param mesh GameLib.D3.Mesh + */ + + GameLib.D3.Utils.Link('camera', idToObject, this, this.camera); + GameLib.D3.Utils.Link('parentEntity', idToObject, this, this.parentEntity); + GameLib.D3.Utils.Link('targetEntity', idToObject, this, this.targetEntity); + GameLib.D3.Utils.Link('spline', idToObject, this, this.spline); + GameLib.D3.Utils.Link('mesh', idToObject, this, this.mesh); }; \ No newline at end of file diff --git a/src/game-lib-component-look-at.js b/src/game-lib-component-look-at.js index b4962cc..1bdc052 100644 --- a/src/game-lib-component-look-at.js +++ b/src/game-lib-component-look-at.js @@ -5,9 +5,9 @@ * @param graphics GameLib.D3.Graphics * @param parentEntity GameLib.D3.Entity * @param targetEntity GameLib.D3.Entity - * @param targetOffset GameLib.D3.Vector3 + * @param targetOffset GameLib.D3.API.Vector3 * @param rotationSpeed - * @param lookAtMatrix GameLib.D3.Matrix4 + * @param lookAtMatrix GameLib.D3.API.Matrix4 * @param up GameLib.D3.Vector3 * @constructor */ @@ -46,12 +46,13 @@ GameLib.D3.ComponentLookAt = function ComponentLookAt( this.targetEntity = targetEntity; if(GameLib.D3.Utils.UndefinedOrNull(targetOffset)) { - targetOffset = new GameLib.D3.Vector3( - this.graphics, - this, - new GameLib.D3.API.Vector3(0, 0, 0) - ); + targetOffset = new GameLib.D3.API.Vector3(0, 0, 0); } + targetOffset = new GameLib.D3.Vector3( + this.graphics, + this, + targetOffset + ); this.targetOffset = targetOffset; if (GameLib.D3.Utils.UndefinedOrNull(rotationSpeed)) { @@ -60,21 +61,23 @@ GameLib.D3.ComponentLookAt = function ComponentLookAt( this.rotationSpeed = rotationSpeed; if (GameLib.D3.Utils.UndefinedOrNull(lookAtMatrix)) { - lookAtMatrix = new GameLib.D3.Matrix4( - this.graphics, - this, - new GameLib.D3.API.Matrix4() - ); + lookAtMatrix = new GameLib.D3.API.Matrix4(); } + lookAtMatrix = new GameLib.D3.Matrix4( + this.graphics, + this, + lookAtMatrix + ); this.lookAtMatrix = lookAtMatrix; if(GameLib.D3.Utils.UndefinedOrNull(up)) { - up = new GameLib.D3.Vector3( - this.graphics, - this, - new GameLib.D3.API.Vector3(0, 1, 0) - ); + up = new GameLib.D3.API.Vector3(0, 1, 0); } + up = new GameLib.D3.Vector3( + this.graphics, + this, + up + ); this.up = up; this.targetPosition = new GameLib.D3.Vector3( @@ -83,10 +86,10 @@ GameLib.D3.ComponentLookAt = function ComponentLookAt( new GameLib.D3.API.Vector3(0, 0, 0) ); - this.currentRotation = new GameLib.D3.Vector4( + this.currentRotation = new GameLib.D3.Quaternion( this.graphics, this, - new GameLib.D3.API.Vector4() + new GameLib.D3.API.Quaternion() ); GameLib.D3.Utils.Extend(GameLib.D3.ComponentLookAt, GameLib.D3.ComponentInterface); diff --git a/src/game-lib-component-mesh-permutation.js b/src/game-lib-component-mesh-permutation.js index 4b34645..fe508b0 100644 --- a/src/game-lib-component-mesh-permutation.js +++ b/src/game-lib-component-mesh-permutation.js @@ -24,7 +24,7 @@ GameLib.D3.ComponentMeshPermutation = function ComponentMeshPermutation( this.parentEntity = null; this.positionOffset = positionOffset || new GameLib.D3.API.Vector3(0, 0, 0); - this.quaternionOffset = quaternionOffset || new GameLib.D3.API.Vector4(0, 0, 0, 1); + this.quaternionOffset = quaternionOffset || new GameLib.D3.API.Quaternion(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. diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index 3131dff..fce37bb 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -11,7 +11,7 @@ * @param baseOffset GameLib.D3.Vector * @param maxOffset GameLib.D3.Vector * @param steeringSpeed Number - * @param offset GameLib.D3.Vector3 + * @param targetOffset GameLib.D3.Vector3 * @param currentOffset GameLib.D3.Vector3 * @param currentPathValue Number * @param currentSpeed Number @@ -24,7 +24,7 @@ * @param futurePosition GameLib.D3.Vector3 * @param up GameLib.D3.Vector3 * @param rotationMatrix GameLib.D3.Matrix4 - * @param rotationVector GameLib.D3.Vector4 + * @param rotationVector GameLib.D3.Quaternion * @constructor */ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( @@ -39,7 +39,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( baseOffset, maxOffset, steeringSpeed, - offset, + targetOffset, currentOffset, currentPathValue, currentSpeed, @@ -64,6 +64,9 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( } this.name = name; + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + if (GameLib.D3.Utils.UndefinedOrNull(parentEntity)) { parentEntity = null; } @@ -90,13 +93,23 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.accelleration = accelleration; if (GameLib.D3.Utils.UndefinedOrNull(baseOffset)) { - baseOffset = new GameLib.D3.Vector3(graphics, this, new GameLib.D3.API.Vector3(), 0.1); + baseOffset = new GameLib.D3.API.Vector3(); } + baseOffset = new GameLib.D3.Vector3( + graphics, + this, + baseOffset + ); this.baseOffset = baseOffset; if (GameLib.D3.Utils.UndefinedOrNull(maxOffset)) { - maxOffset = new GameLib.D3.Vector3(graphics, this, new GameLib.D3.API.Vector3(10, 10, 10), 0.1); + maxOffset = new GameLib.D3.API.Vector3(); } + maxOffset = new GameLib.D3.Vector3( + graphics, + this, + maxOffset + ); this.maxOffset = maxOffset; if (GameLib.D3.Utils.UndefinedOrNull(steeringSpeed)) { @@ -104,14 +117,24 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( } this.steeringSpeed = steeringSpeed; - if (GameLib.D3.Utils.UndefinedOrNull(offset)) { - offset = new GameLib.D3.Vector3(graphics, this, new GameLib.D3.API.Vector3(), 0.1); + if (GameLib.D3.Utils.UndefinedOrNull(targetOffset)) { + targetOffset = new GameLib.D3.API.Vector3(); } - this.offset = offset; + targetOffset = new GameLib.D3.Vector3( + graphics, + this, + targetOffset + ); + this.targetOffset = targetOffset; if (GameLib.D3.Utils.UndefinedOrNull(currentOffset)) { - currentOffset = new GameLib.D3.Vector3(graphics, this, new GameLib.D3.API.Vector3(), 0.1); + currentOffset = new GameLib.D3.API.Vector3(); } + currentOffset = new GameLib.D3.Vector3( + graphics, + this, + currentOffset + ); this.currentOffset = currentOffset; if (GameLib.D3.Utils.UndefinedOrNull(currentPathValue)) { @@ -162,18 +185,33 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.raycaster = raycaster; if (GameLib.D3.Utils.UndefinedOrNull(currentPosition)) { - currentPosition = new GameLib.D3.Vector3(graphics, this, new GameLib.D3.API.Vector3(), 0.1); + currentPosition = new GameLib.D3.API.Vector3(); } + currentPosition = new GameLib.D3.Vector3( + graphics, + this, + currentPosition + ); this.currentPosition = currentPosition; if (GameLib.D3.Utils.UndefinedOrNull(futurePosition)) { - futurePosition = new GameLib.D3.Vector3(graphics, this, new GameLib.D3.API.Vector3(), 0.1); + futurePosition = new GameLib.D3.API.Vector3(); } + futurePosition = new GameLib.D3.Vector3( + graphics, + this, + futurePosition + ); this.futurePosition = futurePosition; - if (GameLib.D3.Utils.UndefinedOrNull(up)) { - up = new GameLib.D3.Vector3(graphics, this, new GameLib.D3.API.Vector3(0, 1, 0), 0.1); + if(GameLib.D3.Utils.UndefinedOrNull(up)) { + up = new GameLib.D3.API.Vector3(0, 1, 0); } + up = new GameLib.D3.Vector3( + this.graphics, + this, + up + ); this.up = up; if (GameLib.D3.Utils.UndefinedOrNull(rotationMatrix)) { @@ -186,8 +224,14 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.rotationMatrix = rotationMatrix; if (GameLib.D3.Utils.UndefinedOrNull(rotationVector)) { - rotationVector = new GameLib.D3.Vector4(graphics, this, new GameLib.D3.API.Vector4(), 0.1); + rotationVector = new GameLib.D3.API.Quaternion(); } + rotationVector = new GameLib.D3.Quaternion( + graphics, + this, + rotationVector + ); + //rotationVector.normalize(); this.rotationVector = rotationVector; GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathFollowing, GameLib.D3.ComponentInterface); @@ -220,58 +264,19 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( this.futurePosition = this.spline.getPointAt(this.currentPathValue); - // - - - - - - - - - - - - - - // Ray trace from the future position. - // - - - - - - - - -- - - - - - this.raycaster.setPosition( this.futurePosition ); - // var futureNormal = new THREE.Vector3(0, 1, 0); - // var futurePositionRayCasted = new THREE.Vector3( - // futurePosition.x, - // futurePosition.y, - // futurePosition.z - // ); - var normal = this.raycaster.getFaceNormal(this.mesh); - this.up.x = this.mx(normal.x); - this.up.y = this.my(normal.y); - this.up.z = this.mz(normal.z); - - this.up.updateInstance(); - - // - - - - - - - - - - - - - - // Ray trace from the current position. - // - - - - - - - - -- - - - - - // - // ComponentPathFollowing_Three_Raycaster.set( - // currentPosition, - // new THREE.Vector3( - // 0, - // -1, - // 0 - // ) - // ); - - // var currentNormal = new THREE.Vector3(0, 1, 0); - // for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) { - // var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject( - // this.meshes[m].instance - // ); - // - // if(intersect && intersect.length > 0) { - // currentNormal = intersect[0].face.normal; - // break; - // } - // } - - //var avgNormal = currentNormal.add(this.pastNormal).add(futureNormal).normalize(); - // var avgNormal = (this.pastNormal).add(futureNormal).normalize(); - // this.pastNormal = futureNormal; + if (normal) { + this.up.x = this.mx(normal.x); + this.up.y = this.my(normal.y); + this.up.z = this.mz(normal.z); + this.up.updateInstance(); + } this.rotationMatrix.lookAt( this.currentPosition, @@ -281,37 +286,6 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( this.rotationVector.setFromRotationMatrix(this.rotationMatrix); - // var targetVector = new THREE.Vector3( - // this.maxOffset.x * this.offset.x, - // this.maxOffset.y * this.offset.y, - // this.maxOffset.z * this.offset.z - // ); - // - // var lerpedOffset = new THREE.Vector3( - // this.currentOffset.x, - // this.currentOffset.y, - // this.currentOffset.z - // ).lerp( - // targetVector, - // (this.grain * this.steeringSpeed) - // ); - // - // this.currentOffset.x = lerpedOffset.x; - // this.currentOffset.y = lerpedOffset.y; - // this.currentOffset.z = lerpedOffset.z; - // - // var transformedOffset = new THREE.Vector3( - // this.baseOffset.x + lerpedOffset.x, - // this.baseOffset.y + lerpedOffset.y, - // this.baseOffset.z + lerpedOffset.z - // ).applyQuaternion(quaternion); - - // apply to parent rigidbody instead of direclty to the mesh. - /* parentEntity.position.x = futurePositionRayCasted.x + transformedOffset.x; - parentEntity.position.y = futurePositionRayCasted.y + transformedOffset.y + this.parentEntity.mesh.geometry.boundingBox.y; - parentEntity.position.z = futurePositionRayCasted.z + transformedOffset.z; - */ - /** * Update Position */ @@ -327,39 +301,6 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( this.parentEntity.quaternion.z = this.rotationVector.z; this.parentEntity.quaternion.w = this.rotationVector.w; - // // original code - // - // this.step += this.grain; - // - // if (this.step > 1) { - // this.step = 0; - // } - // - // var lookAtStep = this.step + this.grain * 2; - // - // if (lookAtStep > 1) { - // lookAtStep = 0; - // } - // - // var position = this.scene.splines[0].instance.getPoint(this.step); - // var futurePosition = this.scene.splines[0].instance.getPoint(lookAtStep); - // - // this.raycaster.set(position, new THREE.Vector3(0, -1, 0)); - // - // var intersect = this.raycaster.intersectObject(this.scene.meshes[6].instance); - // - // this.scene.entities[1].position.x = position.x; - // this.scene.entities[1].position.y = position.y; - // this.scene.entities[1].position.z = position.z; - // - // var matrix = new THREE.Matrix4().lookAt(position, futurePosition, intersect[0].face.normal); - // - // var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); - // - // this.scene.entities[1].quaternion.x = quaternion.x; - // this.scene.entities[1].quaternion.y = quaternion.y; - // this.scene.entities[1].quaternion.z = quaternion.z; - // this.scene.entities[1].quaternion.w = quaternion.w; } }; \ No newline at end of file diff --git a/src/game-lib-engine.js b/src/game-lib-engine.js index f0d4479..dd80dfd 100644 --- a/src/game-lib-engine.js +++ b/src/game-lib-engine.js @@ -12,6 +12,14 @@ GameLib.D3.Engine = function Engine( this.instance = instance; }; +GameLib.D3.Engine.prototype.toApiEngine = function() { + + //TODO: create API.Engine sometime + return { + engineType : this.engineType + } +}; + /** * True if CANNON physics * @returns {boolean} diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index f3c5ac9..84b574e 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -1,22 +1,101 @@ /** * Entity Runtime - * @param apiEntity - * @param parentScene - * @param mesh + * @param graphics GameLib.D3.Graphics + * @param apiEntity GameLib.D3.API.Entity * @constructor */ GameLib.D3.Entity = function Entity( - apiEntity, - parentScene, - mesh + graphics, + apiEntity ) { - for (var property in apiEntity) { if (apiEntity.hasOwnProperty(property)) { this[property] = apiEntity[property]; } } + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + this.position = new GameLib.D3.Vector3( + this.graphics, + this, + this.position + ); + + this.quaternion = new GameLib.D3.Quaternion( + this.graphics, + this, + this.quaternion + ); + + this.scale = new GameLib.D3.Vector3( + this.graphics, + this, + this.scale + ); + +}; + +/** + * Converts a GameLib.D3.Entity to GameLib.D3.API.Entity + * @returns {GameLib.D3.API.Entity} + */ +GameLib.D3.Entity.prototype.toApiEntity = function() { + + return new GameLib.D3.API.Entity( + this.id, + this.name, + this.ids, + this.position.toApiVector(), + this.quaternion.toApiQuaternion(), + this.scale.toApiVector(), + GameLib.D3.Utils.IdOrNull(this.parentScene), + GameLib.D3.Utils.IdOrNull(this.mesh) + ); +}; + +/** + * + * @param graphics GameLib.D3.Graphics + * @param objectEntity Object + * @constructor + */ +GameLib.D3.Entity.FromObjectEntity = function(graphics, objectEntity) { + + var apiEntity = new GameLib.D3.API.Entity( + objectEntity.id, + objectEntity.name, + objectEntity.ids, + new GameLib.D3.API.Vector3( + objectEntity.position.x, + objectEntity.position.y, + objectEntity.position.z + ), + new GameLib.D3.API.Quaternion( + objectEntity.quaternion.x, + objectEntity.quaternion.y, + objectEntity.quaternion.z, + objectEntity.quaternion.w, + new GameLib.D3.API.Vector3( + objectEntity.quaternion.axis.x, + objectEntity.quaternion.axis.y, + objectEntity.quaternion.axis.z + ) + ), + new GameLib.D3.API.Vector3( + objectEntity.scale.x, + objectEntity.scale.y, + objectEntity.scale.z + ), + objectEntity.parentScene, + objectEntity.mesh + ); + + return new GameLib.D3.Entity( + graphics, + apiEntity + ); }; /** @@ -28,7 +107,7 @@ GameLib.D3.Entity.prototype.update = function( ) { for(var c in this.ids) { var id = this.ids[c]; - var component = this.parentScene.idToComponent[id]; + var component = this.parentScene.idToObject[id]; if(component && component.onUpdate) { component.onUpdate(deltaTime, this); } @@ -75,7 +154,7 @@ GameLib.D3.Entity.prototype.lateUpdate = function( ) { for(var c in this.ids) { var id = this.ids[c]; - var component = this.parentScene.idToComponent[id]; + var component = this.parentScene.idToObject[id]; if(component && component.onLateUpdate) { component.onLateUpdate(deltaTime, this); } @@ -130,9 +209,9 @@ GameLib.D3.Entity.prototype.addComponent = function( }; GameLib.D3.Entity.prototype.removeComponent = function(component) { - if(component && component.id && this.parentScene.idToComponent[component.id]) { + if(component && component.id && this.parentScene.idToObject[component.id]) { this.ids = this.ids.splice(this.ids.indexOf(component), 1); - delete this.parentScene.idToComponent[component.id]; + delete this.parentScene.idToObject[component.id]; } }; @@ -142,7 +221,7 @@ GameLib.D3.Entity.prototype.getComponent = function( for (var componentId in this.ids) { if (this.ids.hasOwnProperty(componentId)) { var id = this.ids[componentId]; - var component = this.parentScene.idToComponent[id]; + var component = this.parentScene.idToObject[id]; if (component instanceof componentType) { return component; } @@ -170,4 +249,14 @@ GameLib.D3.Entity.prototype.onRegistered = function( parentScene ) { +}; + +GameLib.D3.Entity.prototype.linkObjects = function(idToObject) { + /** + * @param scene GameLib.D3.Scene + * @param mesh GameLib.D3.Mesh + */ + GameLib.D3.Utils.Link('parentScene', idToObject, this, this.parentScene); + GameLib.D3.Utils.Link('mesh', idToObject, this, this.mesh); + }; \ No newline at end of file diff --git a/src/game-lib-heightmap.js b/src/game-lib-heightmap.js index 28c6b4a..619707f 100644 --- a/src/game-lib-heightmap.js +++ b/src/game-lib-heightmap.js @@ -39,6 +39,18 @@ GameLib.D3.Heightmap = function Heightmap( this.elementSize = heightScale; }; + +GameLib.D3.Heightmap.prototype.toApiHeightMap = function() { + //TODO - create API heightmap someday + return { + sizeX: this.sizeX, + sizeY: this.sizeY, + matrix: this.matrix, + elementSize: this.elementSize, + heightScale: this.heightScale + } +}; + /** * Creates a graphics instance mesh from the graphics, physics and physics shape * @param graphics GameLib.D3.Graphics diff --git a/src/game-lib-light.js b/src/game-lib-light.js index ee2d65f..d3e5b4d 100644 --- a/src/game-lib-light.js +++ b/src/game-lib-light.js @@ -42,7 +42,7 @@ GameLib.D3.Light = function Light( this.scale ); - this.quaternion = new GameLib.D3.Vector4( + this.quaternion = new GameLib.D3.Quaternion( graphics, this, this.quaternion @@ -154,3 +154,90 @@ GameLib.D3.Light.prototype.updateInstance = function() { GameLib.D3.Light.prototype.clone = function() { return _.cloneDeep(this); }; + +/** + * Converts a GameLib.D3.Light to a GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.prototype.toApiLight = function() { + return new GameLib.D3.API.Light( + this.id, + this.lightType, + this.name, + this.color.toApiColor(), + this.intensity, + this.position.toApiVector(), + this.targetPosition.toApiVector(), + this.quaternion.toApiQuaternion(), + this.rotation, + this.scale.toApiVector(), + this.distance, + this.decay, + this.power, + this.angle, + this.penumbra + ); +}; + +/** + * Returns a new GameLib.D3.Light from a GameLib.D3.API.Light + * @param graphics GameLib.D3.Graphics + * @param apiLight GameLib.D3.API.Light + * @returns {GameLib.D3.API.Light} + */ +GameLib.D3.Light.FromObjectLight = function(graphics, apiLight) { + + return new GameLib.D3.Light( + graphics, + new GameLib.D3.API.Light( + apiLight.id, + apiLight.lightType, + apiLight.name, + new GameLib.D3.API.Color( + apiLight.color.r, + apiLight.color.g, + apiLight.color.b, + apiLight.color.a + ), + apiLight.intensity, + new GameLib.D3.API.Vector3( + apiLight.position.x, + apiLight.position.y, + apiLight.position.z + ), + new GameLib.D3.API.Vector3( + apiLight.targetPosition.x, + apiLight.targetPosition.y, + apiLight.targetPosition.z + ), + new GameLib.D3.API.Quaternion( + apiLight.quaternion.x, + apiLight.quaternion.y, + apiLight.quaternion.z, + apiLight.quaternion.w, + new GameLib.D3.API.Vector3( + apiLight.quaternion.axis.x, + apiLight.quaternion.axis.y, + apiLight.quaternion.axis.z + ), + apiLight.quaternion.angle + ), + new GameLib.D3.API.Vector3( + apiLight.rotation.x, + apiLight.rotation.y, + apiLight.rotation.z + ), + new GameLib.D3.API.Vector3( + apiLight.scale.x, + apiLight.scale.y, + apiLight.scale.z + ), + apiLight.distance, + apiLight.decay, + apiLight.power, + apiLight.angle, + apiLight.penumbra + ) + ); + +}; \ No newline at end of file diff --git a/src/game-lib-material.js b/src/game-lib-material.js index b0f5287..6050cbd 100644 --- a/src/game-lib-material.js +++ b/src/game-lib-material.js @@ -341,4 +341,175 @@ GameLib.D3.Material.prototype.updateInstance = function() { GameLib.D3.Material.prototype.clone = function() { return _.cloneDeep(this); -}; \ No newline at end of file +}; + +/** + * Converts a GameLib.D3.Material to a GameLib.D3.API.Material + * @returns {GameLib.D3.API.Material} + */ +GameLib.D3.Material.prototype.toApiMaterial = function() { + + return new GameLib.D3.API.Material( + this.id, + this.materialType, + this.name, + this.opacity, + this.side, + this.transparent, + this.maps.toApiTextureMaps(), + this.specular.toApiColor(), + this.lightMapIntensity, + this.aoMapIntensity, + this.color.toApiColor(), + this.emissive.toApiColor(), + this.emissiveIntensity, + this.combine, + this.shininess, + this.reflectivity, + this.refractionRatio, + this.fog, + this.wireframe, + this.wireframeLineWidth, + this.wireframeLineCap, + this.wireframeLineJoin, + this.vertexColors, + this.skinning, + this.morphTargets, + this.morphNormals, + this.lineWidth, + this.lineCap, + this.lineJoin, + this.dashSize, + this.gapWidth, + this.blending, + this.blendSrc, + this.blendDst, + this.blendEquation, + this.depthTest, + this.depthFunc, + this.depthWrite, + this.polygonOffset, + this.polygonOffsetFactor, + this.polygonOffsetUnits, + this.alphaTest, + this.clippingPlanes, + this.clipShadows, + this.visible, + this.overdraw, + this.shading, + this.bumpScale, + this.normalScale, + this.displacementScale, + this.displacementBias, + this.roughness, + this.metalness, + this.pointSize, + this.pointSizeAttenuation, + this.spriteRotation, + this.envMapIntensity + ); +}; + +/** + * Creates a GameLib.D3.Material from a material Object + * @param graphics GameLib.D3.Graphics + * @param objectMaterial Object + * @param imageFactory GameLib.D3.ImageFactory + * @constructor + */ +GameLib.D3.Material.FromObjectMaterial = function(graphics, objectMaterial, imageFactory) { + + var gameLibMaterial = new GameLib.D3.Material( + graphics, + new GameLib.D3.API.Material( + objectMaterial.id, + objectMaterial.materialType, + objectMaterial.name, + objectMaterial.opacity, + objectMaterial.side, + objectMaterial.transparent, + GameLib.D3.API.TextureMapTemplate(), + new GameLib.D3.API.Color( + objectMaterial.specular.r, + objectMaterial.specular.g, + objectMaterial.specular.b, + objectMaterial.specular.a + ), + objectMaterial.lightMapIntensity, + objectMaterial.aoMapIntensity, + new GameLib.D3.API.Color( + objectMaterial.color.r, + objectMaterial.color.g, + objectMaterial.color.b, + objectMaterial.color.a + ), + new GameLib.D3.API.Color( + objectMaterial.emissive.r, + objectMaterial.emissive.g, + objectMaterial.emissive.b, + objectMaterial.emissive.a + ), + objectMaterial.emissiveIntensity, + objectMaterial.combine, + objectMaterial.shininess, + objectMaterial.reflectivity, + objectMaterial.refractionRatio, + objectMaterial.fog, + objectMaterial.wireframe, + objectMaterial.wireframeLineWidth, + objectMaterial.wireframeLineCap, + objectMaterial.wireframeLineJoin, + objectMaterial.vertexColors, + objectMaterial.skinning, + objectMaterial.morphTargets, + objectMaterial.morphNormals, + objectMaterial.lineWidth, + objectMaterial.lineCap, + objectMaterial.lineJoin, + objectMaterial.dashSize, + objectMaterial.gapWidth, + objectMaterial.blending, + objectMaterial.blendSrc, + objectMaterial.blendDst, + objectMaterial.blendEquation, + objectMaterial.depthTest, + objectMaterial.depthFunc, + objectMaterial.depthWrite, + objectMaterial.polygonOffset, + objectMaterial.polygonOffsetFactor, + objectMaterial.polygonOffsetUnits, + objectMaterial.alphaTest, + objectMaterial.clippingPlanes, + objectMaterial.clipShadows, + objectMaterial.visible, + objectMaterial.overdraw, + objectMaterial.shading, + objectMaterial.bumpScale, + objectMaterial.normalScale, + objectMaterial.displacementScale, + objectMaterial.displacementBias, + objectMaterial.roughness, + objectMaterial.metalness, + objectMaterial.pointSize, + objectMaterial.pointSizeAttenuation, + objectMaterial.spriteRotation, + objectMaterial.envMapIntensity + ) + ); + + var objectMaps = objectMaterial.maps; + + var gameLibTextureMap = new GameLib.D3.TextureMapTemplate(graphics); + + for (var map in gameLibTextureMap) { + + if (gameLibTextureMap.hasOwnProperty(map) && objectMaps[map] && objectMaps[map].texture && objectMaps[map].texture.imagePath) { + + gameLibTextureMap[map].texture = GameLib.D3.Texture.FromObjectTexture(graphics, objectMaps[map].texture, gameLibMaterial, map, gameLibTextureMap, imageFactory); + } + } + + gameLibMaterial.maps = gameLibTextureMap; + + return gameLibMaterial; +}; diff --git a/src/game-lib-matrix-3.js b/src/game-lib-matrix-3.js index 7ad2604..9bdf2f8 100644 --- a/src/game-lib-matrix-3.js +++ b/src/game-lib-matrix-3.js @@ -30,8 +30,8 @@ GameLib.D3.Matrix3 = function( */ GameLib.D3.Matrix3.prototype.identity = function () { this.rows = [ - new GameLib.D3.API.Vector4(1, 0, 0), - new GameLib.D3.API.Vector4(0, 1, 0), - new GameLib.D3.API.Vector4(0, 0, 1) + new GameLib.D3.API.Vector3(1, 0, 0), + new GameLib.D3.API.Vector3(0, 1, 0), + new GameLib.D3.API.Vector3(0, 0, 1) ]; }; \ No newline at end of file diff --git a/src/game-lib-matrix-4.js b/src/game-lib-matrix-4.js index fef1631..24c65fe 100644 --- a/src/game-lib-matrix-4.js +++ b/src/game-lib-matrix-4.js @@ -2,20 +2,20 @@ * Runtime Matrix4 * @param graphics * @param parentObject - * @param matrix4 + * @param apiMatrix4 * @param grain * @constructor */ GameLib.D3.Matrix4 = function( graphics, parentObject, - matrix4, + apiMatrix4, grain ) { - for (var property in matrix4) { - if (matrix4.hasOwnProperty(property)) { - this[property] = matrix4[property]; + for (var property in apiMatrix4) { + if (apiMatrix4.hasOwnProperty(property)) { + this[property] = apiMatrix4[property]; } } @@ -32,28 +32,28 @@ GameLib.D3.Matrix4 = function( } this.grain = grain; - this.rows[0] = new GameLib.D3.Vector4( + this.rows[0] = new GameLib.D3.Quaternion( this.graphics, this, this.rows[0], grain ); - this.rows[1] = new GameLib.D3.Vector4( + this.rows[1] = new GameLib.D3.Quaternion( this.graphics, this, this.rows[1], grain ); - this.rows[2] = new GameLib.D3.Vector4( + this.rows[2] = new GameLib.D3.Quaternion( this.graphics, this, this.rows[2], grain ); - this.rows[3] = new GameLib.D3.Vector4( + this.rows[3] = new GameLib.D3.Quaternion( this.graphics, this, this.rows[3], @@ -133,3 +133,59 @@ GameLib.D3.Matrix4.prototype.lookAt = function (position, target, up) { this.rows[3].w = this.instance.elements[15]; }; + +/** + * GameLib.D3.Matrix4 to GameLib.D3.API.Matrix4 + * @returns {*} + */ +GameLib.D3.Matrix4.prototype.toApiMatrix = function () { + return new GameLib.D3.API.Matrix4( + this.rows[0].toApiQuaternion(), + this.rows[1].toApiQuaternion(), + this.rows[2].toApiQuaternion(), + this.rows[3].toApiQuaternion() + ) +}; + +/** + * Creates a GameLib.D3.Matrix4 from an Object matrix + * @param graphics GameLib.D3.Graphics + * @param objectMatrix Object + * @param parentObject + * @returns {GameLib.D3.Matrix4} + * @constructor + */ +GameLib.D3.Matrix4.FromObjectMatrix = function(graphics, objectMatrix, parentObject) { + var apiMatrix = new GameLib.D3.API.Matrix4( + new GameLib.D3.API.Quaternion( + objectMatrix[0], + objectMatrix[1], + objectMatrix[2], + objectMatrix[3] + ), + new GameLib.D3.API.Quaternion( + objectMatrix[4], + objectMatrix[5], + objectMatrix[6], + objectMatrix[7] + ), + new GameLib.D3.API.Quaternion( + objectMatrix[8], + objectMatrix[9], + objectMatrix[10], + objectMatrix[11] + ), + new GameLib.D3.API.Quaternion( + objectMatrix[12], + objectMatrix[13], + objectMatrix[14], + objectMatrix[15] + ) + ); + + return new GameLib.D3.Matrix4( + graphics, + parentObject, + apiMatrix + ) +}; \ No newline at end of file diff --git a/src/game-lib-mesh.js b/src/game-lib-mesh.js index 86f7d79..c1c281a 100644 --- a/src/game-lib-mesh.js +++ b/src/game-lib-mesh.js @@ -45,14 +45,8 @@ GameLib.D3.Mesh = function Mesh( /** * We don't do a Runtime rotation since it interferes with the quaternion update */ - this.rotation = new GameLib.D3.Vector3( - graphics, - this, - this.rotation, - 0.001 - ); - this.quaternion = new GameLib.D3.Vector4( + this.quaternion = new GameLib.D3.Quaternion( graphics, this, this.quaternion @@ -266,7 +260,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { */ for (var si = 0; si < this.skinIndices.length; si++) { instanceGeometry.skinIndices.push( - new this.graphics.instance.Vector4( + new this.graphics.instance.Quaternion( this.skinIndices[si].x, this.skinIndices[si].y, this.skinIndices[si].z, @@ -280,7 +274,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { */ for (var sw = 0; sw < this.skinWeights.length; sw++) { instanceGeometry.skinWeights.push( - new this.graphics.instance.Vector4( + new this.graphics.instance.Quaternion( this.skinWeights[sw].x, this.skinWeights[sw].y, this.skinWeights[sw].z, @@ -331,21 +325,6 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.up.y = this.up.y; instance.up.z = this.up.z; - /** - * We don't do rotation for now - it should be dealt with by quaternion - */ - // instance.rotation.x = this.rotation.x; - // instance.rotation.y = this.rotation.y; - // instance.rotation.z = this.rotation.z; - // - // instance.quaternion.setFromEuler( - // new THREE.Euler( - // instance.rotation.x, - // instance.rotation.y, - // instance.rotation.z - // ) - // ); - // instance.quaternion.x = this.quaternion.x; instance.quaternion.y = this.quaternion.y; instance.quaternion.z = this.quaternion.z; @@ -354,10 +333,129 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { return instance; }; +/** + * Updates the mesh instance + */ GameLib.D3.Mesh.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; +/** + * Clones a mesh + * @returns {*} + */ GameLib.D3.Mesh.prototype.clone = function() { return _.cloneDeep(this); +}; + +/** + * Converts a GameLib.D3.Mesh to a GameLib.D3.API.Mesh + * @returns {GameLib.D3.API.Mesh} + */ +GameLib.D3.Mesh.prototype.toApiMesh = function() { + + return new GameLib.D3.API.Mesh( + this.id, + this.meshType, + this.name, + this.vertices, + this.faces, + this.faceVertexUvs, + this.materials.map(function(material){return material.toApiMaterial()}), + this.parentMeshId, + this.parentSceneId, + this.skeleton, + this.skinIndices, + this.skinWeights, + this.position.toApiVector(), + this.quaternion.toApiQuaternion(), + this.scale.toApiVector(), + this.up.toApiVector() + ); +}; + +/** + * Converts a standard object mesh to a GameLib.D3.Mesh + * @param graphics GameLib.D3.Graphics + * @param objectMesh {Object} + * @param computeNormals boolean to indicate whether or not to recalculate normals + * @param imageFactory GameLib.D3.ImageFactory + * @constructor + */ +GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh, computeNormals, imageFactory) { + + var apiMesh = new GameLib.D3.API.Mesh( + objectMesh.id, + objectMesh.meshType, + objectMesh.name, + objectMesh.vertices.map( + function (objectVertex) { + return new GameLib.D3.Vertex( + new GameLib.D3.API.Vector3( + objectVertex.position.x, + objectVertex.position.y, + objectVertex.position.z + ), + objectVertex.boneWeights.map( + function (boneWeight) { + return new GameLib.D3.API.BoneWeight( + boneWeight.boneIndex, + boneWeight.weight + ) + } + ) + ); + } + ), + objectMesh.faces, + objectMesh.faceVertexUvs, + objectMesh.materials.map( + function (objectMaterial) { + return GameLib.D3.Material.FromObjectMaterial( + graphics, + objectMaterial, + imageFactory + ); + } + ), + objectMesh.parentMeshId, + objectMesh.parentSceneId, + objectMesh.skeleton, + objectMesh.skinIndices, + objectMesh.skinWeights, + new GameLib.D3.API.Vector3( + objectMesh.position.x, + objectMesh.position.y, + objectMesh.position.z + ), + new GameLib.D3.API.Quaternion( + objectMesh.quaternion.x, + objectMesh.quaternion.y, + objectMesh.quaternion.z, + objectMesh.quaternion.w, + new GameLib.D3.API.Vector3( + objectMesh.quaternion.axis.x, + objectMesh.quaternion.axis.y, + objectMesh.quaternion.axis.z + ), + objectMesh.quaternion.angle + ), + new GameLib.D3.API.Vector3( + objectMesh.scale.x, + objectMesh.scale.y, + objectMesh.scale.z + ), + new GameLib.D3.API.Vector3( + objectMesh.up.x, + objectMesh.up.y, + objectMesh.up.z + ) + ); + + return new GameLib.D3.Mesh( + graphics, + computeNormals, + apiMesh + ); + }; \ No newline at end of file diff --git a/src/game-lib-vector4.js b/src/game-lib-quaternion.js similarity index 58% rename from src/game-lib-vector4.js rename to src/game-lib-quaternion.js index b305746..1f3dfc0 100644 --- a/src/game-lib-vector4.js +++ b/src/game-lib-quaternion.js @@ -1,27 +1,25 @@ /** - * Runtime vector4 for updating instance objects + * Runtime quaternion for updating instance objects * @param graphics GameLib.D3.Graphics * @param parentObject GameLib.D3.* - * @param vector4 GameLib.D3.API.Vector4 + * @param quaternion GameLib.D3.API.Quaternion * @param grain Number * @constructor */ -GameLib.D3.Vector4 = function RuntimeVector4( +GameLib.D3.Quaternion = function RuntimeQuaternion( graphics, parentObject, - vector4, + quaternion, grain ) { - for (var property in vector4) { - if (vector4.hasOwnProperty(property)) { - - - this[property] = vector4[property]; + for (var property in quaternion) { + if (quaternion.hasOwnProperty(property)) { + this[property] = quaternion[property]; } } - GameLib.D3.Utils.Extend(GameLib.D3.Vector4, GameLib.D3.API.Vector4); + GameLib.D3.Utils.Extend(GameLib.D3.Quaternion, GameLib.D3.API.Quaternion); this.graphics = graphics; @@ -34,15 +32,22 @@ GameLib.D3.Vector4 = function RuntimeVector4( } this.grain = grain; + this.axis = new GameLib.D3.Vector3( + this.graphics, + this, + this.axis, + 0.001 + ); + this.instance = this.createInstance(); }; /** - * Creates an instance vector4 + * Creates an instance quaternion * @param update * @returns {*} */ -GameLib.D3.Vector4.prototype.createInstance = function(update) { +GameLib.D3.Quaternion.prototype.createInstance = function(update) { var instance = null; @@ -62,7 +67,7 @@ GameLib.D3.Vector4.prototype.createInstance = function(update) { /** * Updates the instance vector, calls updateInstance on the parent object */ -GameLib.D3.Vector4.prototype.updateInstance = function() { +GameLib.D3.Quaternion.prototype.updateInstance = function() { this.createInstance(true); @@ -72,19 +77,21 @@ GameLib.D3.Vector4.prototype.updateInstance = function() { }; /** - * Converts runtime vector to API Vector - * @returns {GameLib.D3.API.Vector4} + * Converts runtime quaternion to API quaternion + * @returns {*} */ -GameLib.D3.Vector4.prototype.toApiVector = function() { - return new GameLib.D3.API.Vector4( +GameLib.D3.Quaternion.prototype.toApiQuaternion = function() { + return new GameLib.D3.API.Quaternion( this.x, this.y, this.z, - this.w + this.w, + this.axis.toApiVector(), + this.angle ); }; -GameLib.D3.Vector4.prototype.normalize = function () { +GameLib.D3.Quaternion.prototype.normalize = function () { this.updateInstance(); @@ -102,7 +109,7 @@ GameLib.D3.Vector4.prototype.normalize = function () { * * @param matrix4 GameLib.D3.Matrix4 */ -GameLib.D3.Vector4.prototype.setFromRotationMatrix = function(matrix4) { +GameLib.D3.Quaternion.prototype.setFromRotationMatrix = function(matrix4) { this.instance.setFromRotationMatrix(matrix4.instance); @@ -114,15 +121,15 @@ GameLib.D3.Vector4.prototype.setFromRotationMatrix = function(matrix4) { /** * - * @param vector4 GameLib.D3.Vector4 + * @param quaternion GameLib.D3.Quaternion * @param t - * @returns {GameLib.D3.Vector4} + * @returns {GameLib.D3.Quaternion} */ -GameLib.D3.Vector4.prototype.slerp = function (vector4, t) { +GameLib.D3.Quaternion.prototype.slerp = function (quaternion, t) { this.updateInstance(); - this.instance.slerp(vector4.instance, t); + this.instance.slerp(quaternion.instance, t); this.x = this.instance.x; this.y = this.instance.y; diff --git a/src/game-lib-rigid-body.js b/src/game-lib-rigid-body.js index b739e98..953f848 100644 --- a/src/game-lib-rigid-body.js +++ b/src/game-lib-rigid-body.js @@ -43,7 +43,7 @@ GameLib.D3.RigidBody = function( this.position = position || new GameLib.D3.API.Vector3(); this.velocity = velocity || new GameLib.D3.API.Vector3(); this.angularVelocity = angularVelocity || new GameLib.D3.API.Vector3(); - this.quaternion = quaternion || new GameLib.D3.API.Vector4(0, 0, 0, 1); + this.quaternion = quaternion || new GameLib.D3.API.Quaternion(0, 0, 0, 1); this.mass = typeof mass == "undefined" ? 0 : mass; this.friction = typeof friction == "undefined" ? 5 : friction; this.linearDamping = typeof linearDamping == "undefined" ? 0.01 : linearDamping; @@ -111,11 +111,15 @@ GameLib.D3.RigidBody.prototype.createInstance = function() { return instance; }; +GameLib.D3.RigidBody.prototype.toApiRigidBody = function() { + return null; +}; + /** * Adds a shape to this rigid body * @param shape GameLib.D3.Shape * @param offset GameLib.D3.API.Vector3 - * @param orientation GameLib.D3.API.Vector4 + * @param orientation GameLib.D3.API.Quaternion * @constructor */ GameLib.D3.RigidBody.prototype.addShape = function( @@ -128,7 +132,7 @@ GameLib.D3.RigidBody.prototype.addShape = function( } if (!orientation || typeof orientation == 'undefined') { - orientation = new GameLib.D3.API.Vector4(0,0,0,1); + orientation = new GameLib.D3.API.Quaternion(0,0,0,1); } this.instance.addShape( diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index 6929d27..097457c 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -5,20 +5,18 @@ * @param progressCallback * @param apiScene GameLib.D3.API.Scene * @param imageFactory - * @param meshIdToMesh - * @param idToComponent * @constructor */ GameLib.D3.Scene = function Scene( graphics, progressCallback, apiScene, - imageFactory, - meshIdToMesh, - idToComponent + imageFactory ) { - for (var property in apiScene) { + var property; + + for (property in apiScene) { if (apiScene.hasOwnProperty(property)) { this[property] = apiScene[property]; } @@ -39,34 +37,43 @@ GameLib.D3.Scene = function Scene( this.scale ); - /** - * We don't do a Runtime rotation since it interferes with the quaternion update - */ - - this.quaternion = new GameLib.D3.Vector4( + this.quaternion = new GameLib.D3.Quaternion( graphics, this, this.quaternion ); - if (GameLib.D3.Utils.UndefinedOrNull(meshIdToMesh)) { - meshIdToMesh = {}; - } - this.meshIdToMesh = meshIdToMesh; - - if (GameLib.D3.Utils.UndefinedOrNull(idToComponent)) { - idToComponent = {}; - } - this.idToComponent = idToComponent; - this.progressCallback = progressCallback; this.instance = this.createInstance(); this.imageFactory = imageFactory; -}; + this.interestingProperties = [ + "cameras", + "meshes", + "lights", + "components", + "entities", + "splines" + ]; + this.idToObject = {}; + + this.idToObject[this.id] = this; + + for (var p = 0; p < this.interestingProperties.length; p++) { + property = this.interestingProperties[p]; + if (this.hasOwnProperty(property)) { + for (var i = 0; i < this[property].length; i++) { + var object = this[property][i]; + this.idToObject[object.id] = object; + } + } + } + + this.linkObjects(); +}; /** * Creates an instance scene @@ -74,7 +81,7 @@ GameLib.D3.Scene = function Scene( */ GameLib.D3.Scene.prototype.createInstance = function() { - var instance = new this.graphics.instance.Scene(); + var instance = new THREE.Scene(); instance.name = this.name; @@ -97,9 +104,205 @@ GameLib.D3.Scene.prototype.createInstance = function() { return instance; }; +/** + * Converts a GameLib.D3.Scene to a GameLib.D3.API.Scene + * @returns {GameLib.D3.API.Scene} + */ +GameLib.D3.Scene.prototype.toApiScene = function() { + + var apiMeshes = this.meshes.map( + function(mesh) { + return mesh.toApiMesh(); + } + ); + + var apiLights = this.lights.map( + function(light) { + return light.toApiLight(); + } + ); + + var apiComponents = this.components.map( + function(component) { + return component.toApiComponent(); + } + ); + + var apiEntities = this.entities.map( + function(entity) { + return entity.toApiEntity(); + } + ); + + var apiCameras = this.cameras.map( + function(camera) { + return camera.toApiCamera(); + } + ); + + var apiSplines = this.splines.map( + function(spline) { + return spline.toApiSpline(); + } + ); + + var apiWorlds = this.worlds.map( + function(world) { + return world.toApiWorld(); + } + ); + + var apiShapes = this.shapes.map( + function(shape) { + return shape.toApiShape(); + } + ); + + return new GameLib.D3.API.Scene( + this.id, + this.path, + this.name, + apiMeshes, + this.quaternion.toApiQuaternion(), + this.position.toApiVector(), + this.scale.toApiVector(), + this.parentSceneId, + apiLights, + apiWorlds, + apiEntities, + apiComponents, + apiShapes, + apiCameras, + this.activeCameraIndex, + apiSplines + ); +}; + +/** + * Converts a scene Object to a GameLib.D3.Scene object + * @param graphics GameLib.D3.Graphics + * @param objectScene Object + * @param computeNormals boolean to indicate whether or not to recalculate normals + * @param imageFactory GameLib.D3.ImageFactory + * @param progressCallback callback + * @returns {GameLib.D3.Scene} + * @constructor + */ +GameLib.D3.Scene.FromObjectScene = function( + graphics, + objectScene, + computeNormals, + imageFactory, + progressCallback +) { + var apiScene = new GameLib.D3.API.Scene( + objectScene.id, + objectScene.path, + objectScene.name, + objectScene.meshes.map( + function (objectMesh) { + return GameLib.D3.Mesh.FromObjectMesh( + graphics, + objectMesh, + computeNormals, + imageFactory + ) + } + ), + new GameLib.D3.API.Quaternion( + objectScene.quaternion.x, + objectScene.quaternion.y, + objectScene.quaternion.z, + objectScene.quaternion.w, + new GameLib.D3.API.Vector3( + objectScene.quaternion.axis.x, + objectScene.quaternion.axis.y, + objectScene.quaternion.axis.z + ), + objectScene.quaternion.angle + ), + new GameLib.D3.API.Vector3( + objectScene.position.x, + objectScene.position.y, + objectScene.position.z + ), + new GameLib.D3.API.Vector3( + objectScene.scale.x, + objectScene.scale.y, + objectScene.scale.z + ), + objectScene.parentSceneId, + objectScene.lights.map( + function (objectLight) { + return GameLib.D3.Light.FromObjectLight( + graphics, + objectLight + ) + } + ), + objectScene.worlds.map( + function (objectWorld) { + return GameLib.D3.World.FromObjectWorld( + graphics, + objectWorld + ); + } + ), + objectScene.entities.map( + function (objectEntity) { + return GameLib.D3.Entity.FromObjectEntity( + graphics, + objectEntity + ); + } + ), + objectScene.components.map( + function (objectComponent) { + return GameLib.D3.ComponentInterface.FromObjectComponent( + graphics, + objectComponent + ); + } + ), + objectScene.shapes.map( + function (objectShape) { + return GameLib.D3.Shape.FromObjectShape( + graphics, + objectShape + ); + } + ), + objectScene.cameras.map( + function (objectCamera) { + return GameLib.D3.Camera.FromObjectCamera( + graphics, + objectCamera + ); + } + ), + objectScene.activeCameraIndex, + objectScene.splines.map( + function (objectSpline) { + return GameLib.D3.Spline.FromObjectSpline( + graphics, + objectSpline + ) + } + ) + ); + + return new GameLib.D3.Scene( + graphics, + progressCallback, + apiScene, + imageFactory + ); + +}; + /** * Transforms raw scene data into a GameLib.D3.Scene - * @param scene Object (as it comes from the API) + * @param objectScene Object (as it comes from the API) * @param onLoaded * @param graphics * @param uploadUrl @@ -108,404 +311,25 @@ GameLib.D3.Scene.prototype.createInstance = function() { * @constructor */ GameLib.D3.Scene.LoadScene = function( - scene, + objectScene, onLoaded, graphics, uploadUrl, progressCallback, computeNormals ) { - var entities = []; - - var worlds = []; - - var components = []; - - var shapes = []; - - var imageFactory = GameLib.D3.ImageFactory(graphics, uploadUrl); - - if (scene.worlds && scene.worlds.length > 0) { - console.warn('Implement physics worlds code here'); - } - - var lights = []; - - if (scene.lights && scene.lights.length > 0) { - for (var l = 0; l < scene.lights.length; l++) { - - var apiLight = scene.lights[l]; - - var light = new GameLib.D3.Light( - graphics, - new GameLib.D3.API.Light( - apiLight.id, - apiLight.lightType, - apiLight.name, - new GameLib.D3.API.Color( - apiLight.color.r, - apiLight.color.g, - apiLight.color.b, - apiLight.color.a - ), - apiLight.intensity, - new GameLib.D3.API.Vector3( - apiLight.position.x, - apiLight.position.y, - apiLight.position.z - ), - new GameLib.D3.API.Vector3( - apiLight.targetPosition.x, - apiLight.targetPosition.y, - apiLight.targetPosition.z - ), - new GameLib.D3.API.Vector4( - apiLight.quaternion.x, - apiLight.quaternion.y, - apiLight.quaternion.z, - apiLight.quaternion.w - ), - new GameLib.D3.API.Vector3( - apiLight.rotation.x, - apiLight.rotation.y, - apiLight.rotation.z - ), - new GameLib.D3.API.Vector3( - apiLight.scale.x, - apiLight.scale.y, - apiLight.scale.z - ), - apiLight.distance, - apiLight.decay, - apiLight.power, - apiLight.angle, - apiLight.penumbra - ) - ); - - lights.push(light); - } - } - - var gameLibMeshes = []; - - var meshIdToMesh = {}; - - for (var m = 0; m < scene.meshes.length; m++) { - - var apiMesh = scene.meshes[m]; - - var gameLibMaterials = []; - - for (var mat = 0; mat < apiMesh.materials.length; mat++) { - - var apiMaterial = apiMesh.materials[mat]; - - var gameLibMaterial = new GameLib.D3.Material( - graphics, - new GameLib.D3.API.Material( - apiMaterial.id, - apiMaterial.materialType, - apiMaterial.name, - apiMaterial.opacity, - apiMaterial.side, - apiMaterial.transparent, - GameLib.D3.API.TextureMapTemplate(), - new GameLib.D3.API.Color( - apiMaterial.specular.r, - apiMaterial.specular.g, - apiMaterial.specular.b, - apiMaterial.specular.a - ), - apiMaterial.lightMapIntensity, - apiMaterial.aoMapIntensity, - new GameLib.D3.API.Color( - apiMaterial.color.r, - apiMaterial.color.g, - apiMaterial.color.b, - apiMaterial.color.a - ), - new GameLib.D3.API.Color( - apiMaterial.emissive.r, - apiMaterial.emissive.g, - apiMaterial.emissive.b, - apiMaterial.emissive.a - ), - apiMaterial.emissiveIntensity, - apiMaterial.combine, - apiMaterial.shininess, - apiMaterial.reflectivity, - apiMaterial.refractionRatio, - apiMaterial.fog, - apiMaterial.wireframe, - apiMaterial.wireframeLineWidth, - apiMaterial.wireframeLineCap, - apiMaterial.wireframeLineJoin, - apiMaterial.vertexColors, - apiMaterial.skinning, - apiMaterial.morphTargets, - apiMaterial.morphNormals, - apiMaterial.lineWidth, - apiMaterial.lineCap, - apiMaterial.lineJoin, - apiMaterial.dashSize, - apiMaterial.gapWidth, - apiMaterial.blending, - apiMaterial.blendSrc, - apiMaterial.blendDst, - apiMaterial.blendEquation, - apiMaterial.depthTest, - apiMaterial.depthFunc, - apiMaterial.depthWrite, - apiMaterial.polygonOffset, - apiMaterial.polygonOffsetFactor, - apiMaterial.polygonOffsetUnits, - apiMaterial.alphaTest, - apiMaterial.clippingPlanes, - apiMaterial.clipShadows, - apiMaterial.visible, - apiMaterial.overdraw, - apiMaterial.shading, - apiMaterial.bumpScale, - apiMaterial.normalScale, - apiMaterial.displacementScale, - apiMaterial.displacementBias, - apiMaterial.roughness, - apiMaterial.metalness, - apiMaterial.pointSize, - apiMaterial.pointSizeAttenuation, - apiMaterial.spriteRotation, - apiMaterial.envMapIntensity - ) - ); - - var apiMaps = apiMaterial.maps; - - var gameLibTextureMap = new GameLib.D3.TextureMapTemplate(graphics); - - for (var map in gameLibTextureMap) { - - if (gameLibTextureMap.hasOwnProperty(map) && apiMaps[map] && apiMaps[map].texture && apiMaps[map].texture.imagePath) { - - var apiTexture = apiMaps[map].texture; - - var texture = new GameLib.D3.Texture( - new GameLib.D3.API.Texture( - apiTexture.id, - map, - apiTexture.name, - apiTexture.imagePath, - apiTexture.wrapS, - apiTexture.wrapT, - new GameLib.D3.API.Vector2( - apiTexture.repeat.x, - apiTexture.repeat.y - ), - apiTexture.data, - apiTexture.format, - apiTexture.mapping, - apiTexture.magFilter, - apiTexture.minFilter, - apiTexture.textureType, - apiTexture.anisotropy, - new GameLib.D3.API.Vector2( - apiTexture.offset.x, - apiTexture.offset.y - ), - apiTexture.generateMipmaps, - apiTexture.flipY, - apiTexture.mipmaps, - apiTexture.unpackAlignment, - apiTexture.premultiplyAlpha, - apiTexture.encoding - ), - graphics, - gameLibMaterial, - gameLibTextureMap[map].instanceMapId, - imageFactory - ); - - - gameLibTextureMap[map].texture = texture; - } - } - - gameLibMaterial.maps = gameLibTextureMap; - - gameLibMaterials.push(gameLibMaterial); - } - - var gameLibVertices = []; - - for (var v = 0; v < apiMesh.vertices.length; v++) { - - var boneWeights = []; - - for (var bw = 0; bw < apiMesh.vertices[v].boneWeights.length; bw++) { - boneWeights.push( - new GameLib.D3.API.BoneWeight( - apiMesh.vertices[v].boneWeights[bw].boneIndex, - apiMesh.vertices[v].boneWeights[bw].weight - ) - ) - } - - gameLibVertices.push( - new GameLib.D3.Vertex( - new GameLib.D3.API.Vector3( - apiMesh.vertices[v].position.x, - apiMesh.vertices[v].position.y, - apiMesh.vertices[v].position.z - ), - boneWeights - ) - ) - } - - var gameLibMesh = new GameLib.D3.Mesh( + onLoaded( + GameLib.D3.Scene.FromObjectScene( graphics, + objectScene, computeNormals, - new GameLib.D3.API.Mesh( - apiMesh.id, - apiMesh.meshType, - apiMesh.name, - gameLibVertices, - apiMesh.faces, - apiMesh.faceVertexUvs, - gameLibMaterials, - apiMesh.parentMeshId, - apiMesh.parentSceneId, - apiMesh.skeleton, - apiMesh.skinIndices, - apiMesh.skinWeights, - new GameLib.D3.API.Vector3( - apiMesh.position.x, - apiMesh.position.y, - apiMesh.position.z - ), - new GameLib.D3.API.Vector4( - apiMesh.quaternion.x, - apiMesh.quaternion.y, - apiMesh.quaternion.z, - apiMesh.quaternion.w - ), - new GameLib.D3.API.Vector3( - apiMesh.rotation.x, - apiMesh.rotation.y, - apiMesh.rotation.z - ), - new GameLib.D3.API.Vector3( - apiMesh.scale.x, - apiMesh.scale.y, - apiMesh.scale.z - ), - new GameLib.D3.API.Vector3( - apiMesh.up.x, - apiMesh.up.y, - apiMesh.up.z - ) - ) - ); - - gameLibMeshes.push(gameLibMesh); - - meshIdToMesh[gameLibMesh.id] = gameLibMesh; - } - - var cameras = []; - - for (var c = 0; scene.cameras && c < scene.cameras.length; c++) { - - var apiCamera = scene.cameras[c]; - - var camera = new GameLib.D3.Camera( - graphics, - new GameLib.D3.API.Camera( - apiCamera.id, - apiCamera.cameraType, - apiCamera.name, - apiCamera.fov, - apiCamera.aspect, - apiCamera.near, - apiCamera.far, - apiCamera.position, - apiCamera.lookAt, - apiCamera.minX, - apiCamera.maxX, - apiCamera.minY, - apiCamera.maxY, - apiCamera.minZ, - apiCamera.maxZ - ) - ); - - cameras.push(camera); - } - - var activeCameraIndex = scene.activeCameraIndex; - - var splines = []; - - for (var s = 0; scene.splines && s < scene.splines.length; s++) { - - var apiSpline = scene.spline[s]; - - var spline = new GameLib.D3.Spline( - graphics, - new GameLib.D3.API.Spline( - apiSpline.id, - apiSpline.name, - apiSpline.vertices - ) - ); - - splines.push(spline); - } - - var scene3d = new GameLib.D3.Scene( - graphics, - progressCallback, - new GameLib.D3.API.Scene( - scene.id, - scene.path, - scene.name, - gameLibMeshes, - new GameLib.D3.API.Vector4( - scene.quaternion.x, - scene.quaternion.y, - scene.quaternion.z, - scene.quaternion.w + GameLib.D3.ImageFactory( + graphics, + uploadUrl ), - new GameLib.D3.API.Vector3( - scene.position.x, - scene.position.y, - scene.position.z - ), - new GameLib.D3.API.Vector3( - scene.rotation.x, - scene.rotation.y, - scene.rotation.z - ), - new GameLib.D3.API.Vector3( - scene.scale.x, - scene.scale.y, - scene.scale.z - ), - scene.parentSceneId, - lights, - worlds, - entities, - components, - shapes, - cameras, - activeCameraIndex, - splines - ), - imageFactory, - meshIdToMesh + progressCallback + ) ); - - onLoaded(scene3d); }; /** @@ -626,7 +450,13 @@ GameLib.D3.Scene.prototype.render = function( GameLib.D3.Scene.prototype.registerEntity = function( entity ) { - this.entities.push(entity); + if (!this.idToObject.hasOwnProperty(entity.id)) { + this.idToObject[entity.id] = entity; + } + + if (this.entities.indexOf(entity) == -1) { + this.entities.push(entity); + } entity.register(this); @@ -634,9 +464,9 @@ GameLib.D3.Scene.prototype.registerEntity = function( var id = entity.ids[c]; - var component = this.idToComponent[id]; + var component = this.idToObject[id]; - if(component) { + if (component) { component.setParentEntity(this, entity); } } @@ -650,8 +480,27 @@ GameLib.D3.Scene.prototype.registerEntity = function( GameLib.D3.Scene.prototype.registerComponent = function( component ) { - this.idToComponent[component.id] = component; - if(component.onRegistered && typeof component.onRegistered == 'function') { + this.components.push(component); + + this.idToObject[component.id] = component; + + if (component.onRegistered && typeof component.onRegistered == 'function') { component.onRegistered(this); } +}; + +/** + * After we created a scene and idToObject has been created, we should execute this function to map + * the components witht their ids to the proper objects + */ +GameLib.D3.Scene.prototype.linkObjects = function() { + + this.components.map(function(component) { + component.linkObjects(this.idToObject); + }.bind(this)); + + this.entities.map(function(entity) { + entity.linkObjects(this.idToObject); + }.bind(this)); + }; \ No newline at end of file diff --git a/src/game-lib-shape.js b/src/game-lib-shape.js index e91736a..bdb0e5c 100644 --- a/src/game-lib-shape.js +++ b/src/game-lib-shape.js @@ -181,3 +181,37 @@ GameLib.D3.Shape.prototype.update = function( this.instance.updateTree(); } }; + +/** + * Converts a GameLib.D3.Shape to a GameLib.D3.API.Shape + * @returns {GameLib.D3.API.Shape} + */ +GameLib.D3.Shape.prototype.toApiShape = function() { + + return new GameLib.D3.API.Shape( + this.engine.toApiEngine(), + this.shapeType, + this.scale.toApiVector(), + this.vertices, + this.indices, + this.radius, + this.halfExtensions, + this.radiusTop, + this.radiusBottom, + this.height, + this.numSegments, + this.heightmap.toApiHeightMap() + ) + +}; + +/** + * Converts from an Object to a Shape + * @param graphics GameLib.D3.Graphics + * @param objectShape Object + * @constructor + */ +GameLib.D3.Shape.FromObjectShape = function(graphics, objectShape) { + //todo: implement this still + return null; +}; \ No newline at end of file diff --git a/src/game-lib-solver.js b/src/game-lib-solver.js index b2e16b9..00e489d 100644 --- a/src/game-lib-solver.js +++ b/src/game-lib-solver.js @@ -73,6 +73,11 @@ GameLib.D3.Solver.prototype.createInstance = function(){ return instance; }; +GameLib.D3.Solver.prototype.toApiSolver = function() { + return null; +}; + + /** * Solver Types * @type {number} diff --git a/src/game-lib-spline.js b/src/game-lib-spline.js index afc0081..0d6f8f9 100644 --- a/src/game-lib-spline.js +++ b/src/game-lib-spline.js @@ -62,4 +62,39 @@ GameLib.D3.Spline.prototype.getPointAt = function(proper) { ); }; +/** + * Converts a GameLib.D3.Spline to GameLib.D3.API.Spline + * @returns {GameLib.D3.API.Spline} + */ +GameLib.D3.Spline.prototype.toApiSpline = function() { + return new GameLib.D3.API.Spline( + this.id, + this.name, + this.vertices + ); + +}; + +/** + * Returns a GameLib.D3.Spline from a spline Object + * @param graphics GameLib.D3.Graphics + * @param objectSpline Object + * @returns {GameLib.D3.Spline} + * @constructor + */ +GameLib.D3.Spline.FromObjectSpline = function( + graphics, + objectSpline +) { + + return new GameLib.D3.Spline( + graphics, + new GameLib.D3.API.Spline( + objectSpline.id, + objectSpline.name, + objectSpline.vertices + ) + ); + +}; diff --git a/src/game-lib-texture-map.js b/src/game-lib-texture-map.js index 4c09bfc..79a3ec3 100644 --- a/src/game-lib-texture-map.js +++ b/src/game-lib-texture-map.js @@ -18,4 +18,16 @@ GameLib.D3.TextureMap = function TextureMap( instanceMapId = null; } this.instanceMapId = instanceMapId; +}; + +GameLib.D3.TextureMap.prototype.toApiTextureMap = function() { + + var apiTexture = null; + + if (this.texture) { + apiTexture = this.texture.toApiTexture(); + } + + return new GameLib.D3.TextureMap(apiTexture, this.instanceMapId); + }; \ No newline at end of file diff --git a/src/game-lib-texture-maps.js b/src/game-lib-texture-maps.js index 9744bd7..0051b86 100644 --- a/src/game-lib-texture-maps.js +++ b/src/game-lib-texture-maps.js @@ -43,3 +43,24 @@ GameLib.D3.TextureMaps = function TextureMaps( this.roughness = roughness; this.specular = specular; }; + +/** + * Returns API textures from the Texture Maps + * @returns {GameLib.D3.TextureMaps} + */ +GameLib.D3.TextureMaps.prototype.toApiTextureMaps = function() { + return new GameLib.D3.TextureMaps( + this.alpha.toApiTextureMap(), + this.ao.toApiTextureMap(), + this.bump.toApiTextureMap(), + this.diffuse.toApiTextureMap(), + this.displacement.toApiTextureMap(), + this.emissive.toApiTextureMap(), + this.environment.toApiTextureMap(), + this.light.toApiTextureMap(), + this.metalness.toApiTextureMap(), + this.normal.toApiTextureMap(), + this.roughness.toApiTextureMap(), + this.specular.toApiTextureMap() + ); +}; \ No newline at end of file diff --git a/src/game-lib-texture.js b/src/game-lib-texture.js index da5ecff..3fa9b7e 100644 --- a/src/game-lib-texture.js +++ b/src/game-lib-texture.js @@ -198,6 +198,98 @@ GameLib.D3.Texture.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; +/** + * Clones this texture object + * @returns {*} + */ GameLib.D3.Texture.prototype.clone = function() { return _.cloneDeep(this); +}; + +/** + * Converts a GameLib.D3.Texture to a GameLib.D3.API.Texture + * @returns {GameLib.D3.API.Texture} + */ +GameLib.D3.Texture.prototype.toApiTexture = function() { + + return new GameLib.D3.API.Texture( + this.id, + this.typeId, + this.name, + this.imagePath, + this.wrapS, + this.wrapT, + this.repeat.toApiVector(), + this.data, + this.format, + this.mapping, + this.magFilter, + this.minFilter, + this.textureType, + this.anisotropy, + this.offset.toApiVector(), + this.generateMipmaps, + this.flipY, + this.mipmaps, + this.unpackAlignment, + this.premultiplyAlpha, + this.encoding + ) + +}; + + +/** + * Converts from an Object texture to a GameLib.D3.Texture + * @param graphics GameLib.D3.Graphics + * @param objectTexture Object + * @param gameLibMaterial GameLib.D3.Material + * @param map GameLib.D3.TextureMap + * @param gameLibTextureMap GameLib.D3.TextureMapTemplate + * @param imageFactory GameLib.D3.ImageFactory + * @constructor + */ +GameLib.D3.Texture.FromObjectTexture = function( + graphics, + objectTexture, + gameLibMaterial, + map, + gameLibTextureMap, + imageFactory +) { + return new GameLib.D3.Texture( + new GameLib.D3.API.Texture( + objectTexture.id, + map, + objectTexture.name, + objectTexture.imagePath, + objectTexture.wrapS, + objectTexture.wrapT, + new GameLib.D3.API.Vector2( + objectTexture.repeat.x, + objectTexture.repeat.y + ), + objectTexture.data, + objectTexture.format, + objectTexture.mapping, + objectTexture.magFilter, + objectTexture.minFilter, + objectTexture.textureType, + objectTexture.anisotropy, + new GameLib.D3.API.Vector2( + objectTexture.offset.x, + objectTexture.offset.y + ), + objectTexture.generateMipmaps, + objectTexture.flipY, + objectTexture.mipmaps, + objectTexture.unpackAlignment, + objectTexture.premultiplyAlpha, + objectTexture.encoding + ), + graphics, + gameLibMaterial, + gameLibTextureMap[map].instanceMapId, + imageFactory + ); }; \ No newline at end of file diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index e0e6957..fe703e7 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -16,6 +16,44 @@ GameLib.D3.Utils.UndefinedOrNull = function ( return typeof variable == 'undefined' || variable == null; }; +/** + * Returns either an ID of the object or Null + * @param object + * @returns {null} + * @constructor + */ +GameLib.D3.Utils.IdOrNull = function (object) { + if (GameLib.D3.Utils.UndefinedOrNull(object)) { + return null; + } else { + if (GameLib.D3.Utils.UndefinedOrNull(object.id)) { + console.warn('saving an object reference with no ID : ', object); + return null; + } + return object.id; + } +}; + +/** + * Links an object to its parent through idToObject array + * @param propertyString + * @param idToObject + * @param parentObject + * @param id + * @constructor + */ +GameLib.D3.Utils.Link = function(propertyString, idToObject, parentObject, id) { + + if (!GameLib.D3.Utils.UndefinedOrNull(parentObject[propertyString])) { + + if (!idToObject.hasOwnProperty(id)) { + console.warn('camera linking failed for object:' + parentObject.name); + } + + parentObject[propertyString] = idToObject[id]; + } +}; + GameLib.D3.Utils.Raycast = function ( from, to, @@ -303,7 +341,7 @@ GameLib.D3.Utils.FixPolyZPlane = function(verticesFlat, grain) { var vertices = []; - var points = new GameLib.D3.API.Vector4.Points(); + var points = new GameLib.D3.API.Quaternion.Points(); for (var i = 0; i < verticesFlat.length; i += 3) { points.add(new GameLib.D3.API.Vector3( diff --git a/src/game-lib-world.js b/src/game-lib-world.js index eb0c88c..da18456 100644 --- a/src/game-lib-world.js +++ b/src/game-lib-world.js @@ -80,6 +80,34 @@ GameLib.D3.World.prototype.createInstance = function() { return instance; }; +GameLib.D3.World.prototype.toApiWorld = function() { + //TODO: create API.World someday + return { + id: this.id, + name: this.name, + engine: this.engine.toApiEngine(), + gravity: this.gravity, + broadphase: this.broadphase.toApiBroadphase(), + solver: this.solver.toApiSolver(), + rigidBodies: this.rigidBodies.map(function(rigidBody){return rigidBody.toApiRigidBody()}) + } +}; + + +GameLib.D3.World.FromObjectWorld = function(engine, objectWorld) { + + //todo implement this fully + return new GameLib.D3.World( + objectWorld.id, + objectWorld.name, + engine, + objectWorld.gravity, + null, + null, + null + ); +}; + /** * * @param rigidBody GameLib.D3.RigidBody