diff --git a/src/game-lib-d3-api-input-drive.js b/src/game-lib-d3-api-input-drive.js index fbdfd6b..9b1d452 100644 --- a/src/game-lib-d3-api-input-drive.js +++ b/src/game-lib-d3-api-input-drive.js @@ -5,6 +5,13 @@ * @param domElementId * @param pathFollowingComponent GameLib.D3.Mesh * @param parentEntity + * @param wheelFL + * @param wheelFR + * @param wheelRL + * @param wheelRR + * @param heightOffset + * @param distance + * @param distanceGrain * @constructor */ GameLib.D3.API.Input.Drive = function ( @@ -12,13 +19,25 @@ GameLib.D3.API.Input.Drive = function ( name, domElementId, pathFollowingComponent, - parentEntity + parentEntity, + wheelFL, + wheelFR, + wheelRL, + wheelRR, + heightOffset, + distance, + distanceGrain, + rotationFactor ) { GameLib.Component.call( this, GameLib.Component.COMPONENT_INPUT_DRIVE, { - 'pathFollowingComponent' : GameLib.D3.PathFollowing + 'pathFollowingComponent' : GameLib.D3.PathFollowing, + 'wheelFL' : GameLib.D3.Mesh, + 'wheelFR' : GameLib.D3.Mesh, + 'wheelRL' : GameLib.D3.Mesh, + 'wheelRR' : GameLib.D3.Mesh }, null, parentEntity @@ -44,6 +63,46 @@ GameLib.D3.API.Input.Drive = function ( } this.pathFollowingComponent = pathFollowingComponent; + if (GameLib.Utils.UndefinedOrNull(wheelFL)) { + wheelFL = null; + } + this.wheelFL = wheelFL; + + if (GameLib.Utils.UndefinedOrNull(wheelFR)) { + wheelFR = null; + } + this.wheelFR = wheelFR; + + if (GameLib.Utils.UndefinedOrNull(wheelRL)) { + wheelRL = null; + } + this.wheelRL = wheelRL; + + if (GameLib.Utils.UndefinedOrNull(wheelRR)) { + wheelRR = null; + } + this.wheelRR = wheelRR; + + if (GameLib.Utils.UndefinedOrNull(heightOffset)) { + heightOffset = 0; + } + this.heightOffset = heightOffset; + + if (GameLib.Utils.UndefinedOrNull(distance)) { + distance = 0; + } + this.distance = distance; + + if (GameLib.Utils.UndefinedOrNull(distanceGrain)) { + distanceGrain = 0.1; + } + this.distanceGrain = distanceGrain; + + if (GameLib.Utils.UndefinedOrNull(rotationFactor)) { + rotationFactor = 0.1; + } + this.rotationFactor = rotationFactor; + }; GameLib.D3.API.Input.Drive.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index edf8ae5..d2859d3 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -21,6 +21,7 @@ * @param up * @param modelMatrix GameLib.API.Matrix4 * @param parentEntity + * @param renderOrder * @constructor */ GameLib.D3.API.Mesh = function( @@ -44,7 +45,8 @@ GameLib.D3.API.Mesh = function( localScale, up, modelMatrix, - parentEntity + parentEntity, + renderOrder ) { GameLib.Component.call( this, @@ -156,6 +158,11 @@ GameLib.D3.API.Mesh = function( modelMatrix = new GameLib.API.Matrix4(); } this.modelMatrix = modelMatrix; + + if (GameLib.Utils.UndefinedOrNull(renderOrder)) { + renderOrder = 0; + } + this.renderOrder = renderOrder; }; GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-helper.js b/src/game-lib-d3-helper.js index de866dd..578386a 100644 --- a/src/game-lib-d3-helper.js +++ b/src/game-lib-d3-helper.js @@ -61,7 +61,7 @@ GameLib.D3.Helper.prototype.createInstance = function(update) { var instance = null; if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_EDGES) { - instance = new THREE.EdgesHelper(this.object.instance, 0x007700); + instance = new THREE.WireframeHelper(this.object.instance, 0x007700); } if (this.helperType == GameLib.D3.Helper.HELPER_TYPE_DIRECTIONAL_LIGHT) { diff --git a/src/game-lib-d3-input-drive.js b/src/game-lib-d3-input-drive.js index 847e8a5..ef686a5 100644 --- a/src/game-lib-d3-input-drive.js +++ b/src/game-lib-d3-input-drive.js @@ -25,15 +25,21 @@ GameLib.D3.Input.Drive = function RuntimeInputDrive( apiInputDrive.name, apiInputDrive.domElementId, apiInputDrive.pathFollowingComponent, - apiInputDrive.parentEntity + apiInputDrive.parentEntity, + apiInputDrive.wheelFL, + apiInputDrive.wheelFR, + apiInputDrive.wheelRL, + apiInputDrive.wheelRR, + apiInputDrive.heightOffset, + apiInputDrive.distance, + apiInputDrive.distanceGrain, + apiInputDrive.rotationFactor ); this.keyLeft = false; this.keyRight = false; - this.distance = 0; - this.instance = this.createInstance(); }; @@ -91,7 +97,15 @@ GameLib.D3.Input.Drive.prototype.toApiComponent = function() { this.name, this.domElementId, GameLib.Utils.IdOrNull(this.pathFollowingComponent), - GameLib.Utils.IdOrNull(this.parentEntity) + GameLib.Utils.IdOrNull(this.parentEntity), + GameLib.Utils.IdOrNull(this.wheelFL), + GameLib.Utils.IdOrNull(this.wheelFR), + GameLib.Utils.IdOrNull(this.wheelRL), + GameLib.Utils.IdOrNull(this.wheelRR), + this.heightOffset, + this.distance, + this.distanceGrain, + this.rotationFactor ); return apiInputDrive; @@ -104,7 +118,15 @@ GameLib.D3.Input.Drive.FromObjectComponent = function(graphics, objectComponent) objectComponent.name, objectComponent.domElementId, objectComponent.pathFollowingComponent, - objectComponent.parentEntity + objectComponent.parentEntity, + objectComponent.wheelFL, + objectComponent.wheelFR, + objectComponent.wheelRL, + objectComponent.wheelRR, + objectComponent.heightOffset, + objectComponent.distance, + objectComponent.distanceGrain, + objectComponent.rotationFactor ); return new GameLib.D3.Input.Drive( @@ -117,20 +139,29 @@ GameLib.D3.Input.Drive.FromObjectComponent = function(graphics, objectComponent) GameLib.D3.Input.Drive.prototype.update = function(deltaTime) { if (this.pathFollowingComponent) { - this.pathFollowingComponent.mesh.localPosition.x = this.pathFollowingComponent.rotationMatrix.up.x; - this.pathFollowingComponent.mesh.localPosition.y = this.pathFollowingComponent.rotationMatrix.up.y; - this.pathFollowingComponent.mesh.localPosition.z = this.pathFollowingComponent.rotationMatrix.up.z; + this.pathFollowingComponent.mesh.localPosition.x = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.x); + this.pathFollowingComponent.mesh.localPosition.y = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.y); + this.pathFollowingComponent.mesh.localPosition.z = (this.heightOffset * this.pathFollowingComponent.rotationMatrix.up.z); if (this.keyLeft) { - this.distance -= 0.05; + this.distance -= this.distanceGrain; } if (this.keyRight) { - this.distance += 0.05; + this.distance += this.distanceGrain; } this.pathFollowingComponent.mesh.localPosition.x += (this.distance * this.pathFollowingComponent.rotationMatrix.left.x); this.pathFollowingComponent.mesh.localPosition.y += (this.distance * this.pathFollowingComponent.rotationMatrix.left.y); this.pathFollowingComponent.mesh.localPosition.z += (this.distance * this.pathFollowingComponent.rotationMatrix.left.z); + + this.wheelFL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + this.wheelFR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + + this.wheelFL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + this.wheelFR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + + this.wheelRL.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; + this.wheelRR.localRotation.x += this.rotationFactor * this.pathFollowingComponent.currentSpeed; } }; \ No newline at end of file diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index ef5e663..e20f014 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -35,7 +35,8 @@ GameLib.D3.Mesh = function RuntimeMesh( apiMesh.localScale, apiMesh.up, apiMesh.modelMatrix, - apiMesh.parentEntity + apiMesh.parentEntity, + apiMesh.renderOrder ); this.position = new GameLib.Vector3( @@ -243,6 +244,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { if (this.computeNormals) { instanceGeometry.computeFaceNormals(); instanceGeometry.computeVertexNormals(); + this.computeNormals = false; } var instanceMaterial = this.materials[0].instance; @@ -354,6 +356,8 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.rotateZ(this.localRotation.z); } + instance.renderOrder = this.renderOrder; + return instance; }; @@ -409,7 +413,8 @@ GameLib.D3.Mesh.prototype.toApiMesh = function() { this.localScale.toApiVector(), this.up.toApiVector(), this.modelMatrix.toApiMatrix(), - GameLib.Utils.IdOrNull(this.parentEntity) + GameLib.Utils.IdOrNull(this.parentEntity), + this.renderOrder ); }; @@ -522,7 +527,8 @@ GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh, computeNormals, objectMesh.modelMatrix.rows[3].w ) ), - objectMesh.parentEntity + objectMesh.parentEntity, + objectMesh.renderOrder ); return new GameLib.D3.Mesh( diff --git a/src/game-lib-d3-path-following.js b/src/game-lib-d3-path-following.js index fd8aca9..ad5ebfe 100644 --- a/src/game-lib-d3-path-following.js +++ b/src/game-lib-d3-path-following.js @@ -107,11 +107,13 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing( this.mx = new GameLib.Utils.MovingAverage(10); - this.my = new GameLib.Utils.MovingAverage(10); - this.mz = new GameLib.Utils.MovingAverage(10); + this.posx = new GameLib.Utils.MovingAverage(10); + this.posy = new GameLib.Utils.MovingAverage(10); + this.posz = new GameLib.Utils.MovingAverage(10); + }; GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype); @@ -268,21 +270,15 @@ GameLib.D3.PathFollowing.prototype.update = function(deltaTime) { this.futurePosition.z = futurePosition.z; this.raycaster.setPosition( - this.futurePosition - ); - - var direction = new GameLib.Vector3( - this.graphics, - this, - new GameLib.API.Vector3( - this.up.x * -1, - this.up.y * -1, - this.up.z * -1 - ) + this.currentPosition ); this.raycaster.setDirection( - direction + { + x : -this.up.x, + y : -this.up.y, + z : -this.up.z + } ); var normal = this.raycaster.getFaceNormal(this.raytraceMesh); @@ -291,8 +287,6 @@ GameLib.D3.PathFollowing.prototype.update = function(deltaTime) { 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( @@ -303,16 +297,9 @@ GameLib.D3.PathFollowing.prototype.update = function(deltaTime) { this.rotationVector.setFromRotationMatrix(this.rotationMatrix); - /** - * Update Position - */ - // this.mesh.position.x = this.futurePosition.x + (this.rotationMatrix.up.x * (this.mesh.height - (2 * this.rotationMatrix.up.x)) / 2);// + transformedOffset.x; - // this.mesh.position.y = this.futurePosition.y + (this.rotationMatrix.up.y * (this.mesh.height - (2 * this.rotationMatrix.up.y)) / 2); - // this.mesh.position.z = this.futurePosition.z + (this.rotationMatrix.up.z * (this.mesh.height - (2 * this.rotationMatrix.up.z)) / 2);// + transformedOffset.z; - - this.mesh.position.x = this.futurePosition.x;// + transformedOffset.x; - this.mesh.position.y = this.futurePosition.y; - this.mesh.position.z = this.futurePosition.z;// + transformedOffset.z; + this.mesh.position.x = this.posx(this.futurePosition.x); + this.mesh.position.y = this.posy(this.futurePosition.y); + this.mesh.position.z = this.posz(this.futurePosition.z); /** * Update Rotation diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index 2f4c969..77d98d9 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -81,8 +81,13 @@ GameLib.D3.Raycaster.prototype.set = function( position, direction ) { - this.position = position; - this.direction = direction; + this.position.x = position.x; + this.position.y = position.y; + this.position.z = position.z; + + this.direction.x = direction.x; + this.direction.y = direction.y; + this.direction.z = direction.z; this.position.updateInstance(); this.direction.updateInstance(); @@ -97,7 +102,9 @@ GameLib.D3.Raycaster.prototype.set = function( GameLib.D3.Raycaster.prototype.setDirection = function( direction ) { - this.direction = direction; + this.direction.x = direction.x; + this.direction.y = direction.y; + this.direction.z = direction.z; this.direction.updateInstance(); diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index ea006ce..fe195b3 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -134,6 +134,32 @@ GameLib.Utils.InvertWindingOrder = function(triangles) { return triangles; }; +/** + * Inverts a mesh winding order (and its instance) + * @param mesh GameLib.D3.Mesh + * @returns {*} + * @constructor + */ +GameLib.Utils.InvertMeshWindingOrder = function(mesh) { + + mesh.faces.forEach( + function (face) { + + var tmpV1 = face.v1; + face.v1 = face.v2; + face.v2 = tmpV1; + + var tmpV1uv = face.v1uv; + face.v1uv = face.v2uv; + face.v2uv = tmpV1uv; + + }.bind(this) + ); + + mesh.computeNormals = true; + mesh.instance = mesh.createInstance(false); +}; + /** * This function resets a the winding order of a mesh from a reference point V (the average center of the mesh) */ diff --git a/src/game-lib-vector3.js b/src/game-lib-vector3.js index 6a8c6b2..89129ba 100644 --- a/src/game-lib-vector3.js +++ b/src/game-lib-vector3.js @@ -73,3 +73,19 @@ GameLib.Vector3.prototype.toApiVector = function() { this.z ); }; + +/** + * Converts runtime vector to API Vector + */ +GameLib.Vector3.prototype.copy = function() { + return new GameLib.Vector3( + this.graphics, + this.parentObject, + new GameLib.API.Vector3( + this.x, + this.y, + this.z + ), + this.grain + ) +};