From ec79b039772863ef742e3af714113d04769dd603 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Fri, 6 Jan 2017 16:53:53 +0100 Subject: [PATCH] texture refactoring done --- src/game-lib-api-color.js | 110 ++++++ src/game-lib-api-component.js | 76 ++++ src/game-lib-api-entity-manager.js | 9 + src/game-lib-api-entity.js | 12 +- src/game-lib-color.js | 79 +++++ src/game-lib-component-a.js | 38 +- src/game-lib-d3-api-bone-weight.js | 15 +- src/game-lib-d3-api-color.js | 39 -- src/game-lib-d3-api-component.js | 227 ------------ src/game-lib-d3-api-follow.js | 19 + src/game-lib-d3-api-input-drive.js | 25 ++ ...or.js => game-lib-d3-api-input-editor.bak} | 0 ...t-fly.js => game-lib-d3-api-input-fly.bak} | 0 src/game-lib-d3-api-light.js | 4 +- src/game-lib-d3-api-look-at.js | 20 +- src/game-lib-d3-api-material.js | 41 ++- src/game-lib-d3-api-path-following.js | 39 ++ src/game-lib-d3-api-raycaster.js | 12 + src/game-lib-d3-api-renderable.js | 36 -- src/game-lib-d3-api-renderer.js | 19 + src/game-lib-d3-api-scene.js | 22 +- src/game-lib-d3-api-spline.js | 20 +- src/game-lib-d3-api-texture.js | 4 +- src/game-lib-d3-api-vertex.js | 6 +- src/game-lib-d3-bone-weight.js | 36 +- src/game-lib-d3-color.js | 142 -------- src/game-lib-d3-follow.js | 20 +- src/game-lib-d3-input-drive.js | 30 +- ...editor.js => game-lib-d3-input-editor.bak} | 0 ...input-fly.js => game-lib-d3-input-fly.bak} | 0 src/game-lib-d3-light.js | 2 +- src/game-lib-d3-look-at.js | 30 +- src/game-lib-d3-material.js | 334 ++++++++++++------ src/game-lib-d3-mesh.js | 11 +- src/game-lib-d3-path-following.js | 94 +---- src/game-lib-d3-raycaster.js | 19 +- src/game-lib-d3-renderable.js | 57 --- src/game-lib-d3-renderer.js | 23 +- src/game-lib-d3-scene.js | 62 +++- src/game-lib-d3-skeleton.js | 32 +- src/game-lib-d3-spline.js | 47 +-- src/game-lib-d3-texture.js | 55 +-- src/game-lib-d3-triangle-face.js | 8 +- src/game-lib-d3-vertex.js | 43 +-- src/game-lib-entity-manager.js | 35 +- src/game-lib-entity.js | 86 ++--- src/game-lib-quaternion.js | 2 +- src/game-lib-vector2.js | 2 +- src/game-lib-vector3.js | 2 +- src/game-lib-vector4.js | 2 +- 50 files changed, 978 insertions(+), 1068 deletions(-) create mode 100644 src/game-lib-api-color.js create mode 100644 src/game-lib-api-component.js create mode 100644 src/game-lib-color.js delete mode 100644 src/game-lib-d3-api-color.js delete mode 100644 src/game-lib-d3-api-component.js rename src/{game-lib-d3-api-input-editor.js => game-lib-d3-api-input-editor.bak} (100%) rename src/{game-lib-d3-api-input-fly.js => game-lib-d3-api-input-fly.bak} (100%) delete mode 100644 src/game-lib-d3-api-renderable.js delete mode 100644 src/game-lib-d3-color.js rename src/{game-lib-d3-input-editor.js => game-lib-d3-input-editor.bak} (100%) rename src/{game-lib-d3-input-fly.js => game-lib-d3-input-fly.bak} (100%) delete mode 100644 src/game-lib-d3-renderable.js diff --git a/src/game-lib-api-color.js b/src/game-lib-api-color.js new file mode 100644 index 0000000..98a3516 --- /dev/null +++ b/src/game-lib-api-color.js @@ -0,0 +1,110 @@ +/** + * API Color + * @param r + * @param g + * @param b + * @param a + * @constructor + */ +GameLib.API.Color = function (r, g, b, a) { + + if (GameLib.Utils.UndefinedOrNull(r)) { + r = 1; + } + this.r = r; + + if (GameLib.Utils.UndefinedOrNull(g)) { + g = 1; + } + this.g = g; + + if (GameLib.Utils.UndefinedOrNull(b)) { + b = 1; + } + this.b = b; + + if (GameLib.Utils.UndefinedOrNull(a)) { + a = 0; + } + this.a = a; + +}; + +/** + * Returns an API color from an Object color + * @param objectColor + * @constructor + */ +GameLib.API.Color.FromObjectColor = function(objectColor) { + + return new GameLib.API.Color( + objectColor.r, + objectColor.g, + objectColor.b, + objectColor.a + ); + +}; + +/** + * Converts the current color to HTML hex format (ex. #ffffff) + * @returns {string} + */ +GameLib.API.Color.prototype.toHex = function() { + + if (this.r < 0) { + this.r = 0; + } + + if (this.g < 0) { + this.g = 0; + } + + if (this.b < 0) { + this.b = 0; + } + + if (this.r > 1) { + this.r = 1; + } + + if (this.g > 1) { + this.g = 1; + } + + if (this.b > 1) { + this.b = 1; + } + + var rf = Math.floor(this.r >= 1? 255 : this.r * 256.0).toString(16); + var gf = Math.floor(this.g >= 1? 255 : this.g * 256.0).toString(16); + var bf = Math.floor(this.b >= 1? 255 : this.b * 256.0).toString(16); + + if (rf.length < 2) { + rf = '0' + rf; + } + + if (gf.length < 2) { + gf = '0' + gf; + } + + if (bf.length < 2) { + bf = '0' + bf; + } + + return '#' + rf + gf + bf; +}; + +/** + * Sets this object color to what the hex value is + * @param hex + * @returns {string} + */ +GameLib.API.Color.prototype.fromHex = function(hex) { + + var matches = hex.match(new RegExp('#+(..)(..)(..)')); + + this.r = parseInt(matches[1], 16) / 255.0; + this.g = parseInt(matches[2], 16) / 255.0; + this.b = parseInt(matches[3], 16) / 255.0; +}; diff --git a/src/game-lib-api-component.js b/src/game-lib-api-component.js new file mode 100644 index 0000000..e99aed4 --- /dev/null +++ b/src/game-lib-api-component.js @@ -0,0 +1,76 @@ +/** + * API Component Interface - Do not construct objects of this type directly + * @param componentType + * @param linkedObjects + * @param loaded (indicates whether the linked Objects for this component has been loaded) + * @param parentEntity + * @constructor + */ +GameLib.API.Component = function( + componentType, + linkedObjects, + loaded, + parentEntity +) { + this.componentType = componentType; + + if (GameLib.Utils.UndefinedOrNull(linkedObjects)) { + linkedObjects = {}; + } + this.linkedObjects = linkedObjects; + + if (GameLib.Utils.UndefinedOrNull(loaded)) { + loaded = false; + } + this.loaded = loaded; + + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { + parentEntity = null; + } + this.parentEntity = parentEntity; +}; + +/** + * Returns an API component from an object component + * @param objectComponent (should be an ID string - components get loaded and linked) + * @returns {GameLib.API.Component} + * @constructor + */ +GameLib.API.Component.FromObjectComponent = function(objectComponent) { + if (objectComponent instanceof Object) { + + if (objectComponent.componentType == GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) { + return GameLib.D3.API.PathFollowing.FromObjectComponent(objectComponent); + } + + if (objectComponent.componentType === GameLib.Component.COMPONENT_RENDERER) { + return GameLib.D3.API.Renderer.FromObjectComponent(objectComponent); + } + + if (objectComponent.componentType === GameLib.Component.COMPONENT_LOOK_AT) { + return GameLib.D3.API.LookAt.FromObjectComponent(objectComponent); + } + + if (objectComponent.componentType === GameLib.Component.COMPONENT_FOLLOW) { + return GameLib.D3.API.Follow.FromObjectComponent(objectComponent); + } + + if (objectComponent.componentType === GameLib.Component.COMPONENT_MESH) { + return GameLib.D3.API.Mesh.FromObjectComponent(objectComponent); + } + + if (objectComponent.componentType === GameLib.Component.COMPONENT_SPLINE) { + return GameLib.D3.API.Spline.FromObjectComponent(objectComponent); + } + + if (objectComponent.componentType === GameLib.Component.COMPONENT_INPUT_DRIVE) { + return GameLib.D3.API.Input.Drive.FromObjectComponent(objectComponent); + } + + console.warn('No API Component was associated with this Object'); + throw new Error('No API Component was associated with this Object'); + + } else { + return objectComponent; + } +}; diff --git a/src/game-lib-api-entity-manager.js b/src/game-lib-api-entity-manager.js index d4f6347..f193b78 100644 --- a/src/game-lib-api-entity-manager.js +++ b/src/game-lib-api-entity-manager.js @@ -19,4 +19,13 @@ GameLib.API.EntityManager = function( */ GameLib.API.EntityManager.FromObjectEntityManager = function(objectEntityManager) { + var apiEntities = objectEntityManager.entities.map( + function (objectEntity) { + return GameLib.API.Entity.FromObjectEntity(objectEntity); + } + ); + + return new GameLib.API.EntityManager( + apiEntities + ); }; diff --git a/src/game-lib-api-entity.js b/src/game-lib-api-entity.js index d05652f..b0624cf 100644 --- a/src/game-lib-api-entity.js +++ b/src/game-lib-api-entity.js @@ -33,7 +33,6 @@ GameLib.API.Entity = function( */ GameLib.API.Entity.prototype.addComponent = function(component) { this.components.push(component); - this.instance.addComponent(component) }; /** @@ -42,8 +41,6 @@ GameLib.API.Entity.prototype.addComponent = function(component) { */ GameLib.API.Entity.prototype.removeComponent = function(component) { - this.instance.removeComponent(component); - var index = this.components.indexOf(component); if (index == -1) { @@ -61,9 +58,16 @@ GameLib.API.Entity.prototype.removeComponent = function(component) { * @constructor */ GameLib.API.Entity.FromObjectEntity = function(objectEntity) { + + var apiComponents = objectEntity.components.map( + function (objectComponent) { + return GameLib.API.Component.FromObjectComponent(objectComponent); + } + ); + return new GameLib.API.Entity( objectEntity.id, objectEntity.name, - objectEntity.components + apiComponents ) }; diff --git a/src/game-lib-color.js b/src/game-lib-color.js new file mode 100644 index 0000000..e03aa39 --- /dev/null +++ b/src/game-lib-color.js @@ -0,0 +1,79 @@ +/** + * Runtime color for updating instance objects + * @param graphics GameLib.D3.Graphics + * @param parentObject GameLib.D3.* + * @param apiColor GameLib.API.Color + * @param grain Number + * @constructor + */ +GameLib.Color = function (graphics, parentObject, apiColor, grain) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.API.Color.call( + this, + apiColor.r, + apiColor.g, + apiColor.b, + apiColor.a + ); + + this.parentObject = parentObject; + + if (GameLib.Utils.UndefinedOrNull(grain)) { + grain = 0.001; + } + this.grain = grain; + + this.instance = this.createInstance(); +}; + +GameLib.Color.prototype = Object.create(GameLib.API.Color.prototype); +GameLib.Color.prototype.constructor = GameLib.Color; + +/** + * Creates an instance color + * @param update + * @returns {*} + */ +GameLib.Color.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + instance.r = this.r; + instance.g = this.g; + instance.b = this.b; + } else { + instance = new THREE.Color(this.r, this.g, this.b); + } + + return instance; +}; + +/** + * Updates the instance color, calls updateInstance on the parent object + */ +GameLib.Color.prototype.updateInstance = function() { + + this.createInstance(true); + + if (this.parentObject.updateInstance) { + this.parentObject.updateInstance(); + } +}; + +/** + * Converts runtime color to API Color + * @returns {GameLib.API.Color} + */ +GameLib.Color.prototype.toApiColor = function() { + return new GameLib.API.Color( + this.r, + this.g, + this.b, + this.a + ); +}; diff --git a/src/game-lib-component-a.js b/src/game-lib-component-a.js index 51dea84..30ac9f3 100644 --- a/src/game-lib-component-a.js +++ b/src/game-lib-component-a.js @@ -1,10 +1,10 @@ /** * Component Interface + * @constructor * @param componentType * @param linkedObjects - * @param loaded (indicates whether the linked Objects for this component has been loaded) + * @param loaded * @param parentEntity - * @constructor */ GameLib.Component = function( componentType, @@ -12,30 +12,23 @@ GameLib.Component = function( loaded, parentEntity ) { - this.componentType = componentType; - - if (GameLib.Utils.UndefinedOrNull(linkedObjects)) { - linkedObjects = {}; - } - this.linkedObjects = linkedObjects; - - if (GameLib.Utils.UndefinedOrNull(loaded)) { - loaded = false; - } - this.loaded = loaded; - - if (GameLib.Utils.UndefinedOrNull(parentEntity)) { - parentEntity = null; - } - this.parentEntity = parentEntity; + GameLib.API.Component.call( + this, + componentType, + linkedObjects, + loaded, + parentEntity + ); this.linkedObjects.parentEntity = GameLib.Entity; }; +GameLib.Component.prototype = Object.create(GameLib.API.Component.prototype); +GameLib.Component.prototype.constructor = GameLib.Component; + GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING = 0x1; -GameLib.Component.COMPONENT_RENDERABLE = 0x2; +GameLib.Component.COMPONENT_MATERIAL = 0x2; GameLib.Component.COMPONENT_RENDERER = 0x3; -GameLib.Component.COMPONENT_EDITOR_INPUT = 0x4; GameLib.Component.COMPONENT_LOOK_AT = 0x5; GameLib.Component.COMPONENT_CAMERA = 0x6; GameLib.Component.COMPONENT_FOLLOW = 0x7; @@ -43,8 +36,11 @@ GameLib.Component.COMPONENT_MESH = 0x8; GameLib.Component.COMPONENT_SPLINE = 0x9; GameLib.Component.COMPONENT_LIGHT = 0xa; GameLib.Component.COMPONENT_INPUT_DRIVE = 0xb; -GameLib.Component.COMPONENT_MATERIAL = 0xc; +/** + * Components are linked at runtime - for storing, we just store the ID + * @returns {*} + */ GameLib.Component.prototype.toApiComponent = function() { return this.id; }; \ No newline at end of file diff --git a/src/game-lib-d3-api-bone-weight.js b/src/game-lib-d3-api-bone-weight.js index 3d262f8..1be7148 100644 --- a/src/game-lib-d3-api-bone-weight.js +++ b/src/game-lib-d3-api-bone-weight.js @@ -10,4 +10,17 @@ GameLib.D3.API.BoneWeight = function ( ) { this.boneIndex = boneIndex; this.weight = weight; -}; \ No newline at end of file +}; + +/** + * Object to GameLib.D3.API.BoneWeight + * @param objectBoneWeight + * @returns {GameLib.D3.API.BoneWeight} + * @constructor + */ +GameLib.D3.API.BoneWeight.FromObjectBoneWeight = function(objectBoneWeight) { + return new GameLib.D3.API.BoneWeight( + objectBoneWeight.boneIndex, + objectBoneWeight.weight + ) +}; diff --git a/src/game-lib-d3-api-color.js b/src/game-lib-d3-api-color.js deleted file mode 100644 index 2fc89a9..0000000 --- a/src/game-lib-d3-api-color.js +++ /dev/null @@ -1,39 +0,0 @@ -GameLib.D3.API.Color = function (r, g, b, a) { - - if (GameLib.Utils.UndefinedOrNull(r)) { - r = 1; - } - this.r = r; - - if (GameLib.Utils.UndefinedOrNull(g)) { - g = 1; - } - this.g = g; - - if (GameLib.Utils.UndefinedOrNull(b)) { - b = 1; - } - this.b = b; - - if (GameLib.Utils.UndefinedOrNull(a)) { - a = 0; - } - this.a = a; - -}; - -/** - * Returns an API color from an Object color - * @param objectColor - * @constructor - */ -GameLib.D3.API.Color.FromObjectColor = function(objectColor) { - - return new GameLib.D3.API.Color( - objectColor.r, - objectColor.g, - objectColor.b, - objectColor.a - ); - -}; diff --git a/src/game-lib-d3-api-component.js b/src/game-lib-d3-api-component.js deleted file mode 100644 index 11385ab..0000000 --- a/src/game-lib-d3-api-component.js +++ /dev/null @@ -1,227 +0,0 @@ -/** - * Superset for storing Components to API, and generating them from componentType - * @param id - * @param name - * @param componentType - * @param camera GameLib.D3.Camera - * @param parentEntity GameLib.Entity - * @param targetEntity GameLib.Entity - * @param targetOffset - * @param minDistance - * @param moveSpeed - * @param target - * @param targetToParent - * @param rotatedTargetOffset - * @param rotated - * @param rotationSpeed - * @param lookAtMatrix - * @param spline GameLib.D3.Spline - * @param mesh GameLib.D3.Mesh - * @param accelleration - * @param maxSpeed - * @param baseOffset - * @param maxOffset - * @param steeringSpeed - * @param up - * @param currentOffset - * @param currentPathValue - * @param currentSpeed - * @param direction - * @param currentPosition - * @constructor - */ -GameLib.API.Component = function( - // General - id, - name, - componentType, - parentEntity, - - // Camera Component - camera, - - - // Follow Component - targetEntity, - targetOffset, - minDistance, - moveSpeed, - target, - targetToParent, - rotatedTargetOffset, - rotated, - - // LookAt Component - rotationSpeed, - lookAtMatrix, - up, - - // Path Following Component - spline, - mesh, - accelleration, - maxSpeed, - baseOffset, - maxOffset, - steeringSpeed, - currentOffset, - currentPathValue, - currentSpeed, - direction, - currentPosition -) { - /** - * General - */ - if (GameLib.Utils.UndefinedOrNull(id)) { - id = GameLib.Utils.RandomId(); - } - this.id = id; - - if (GameLib.Utils.UndefinedOrNull(name)) { - name = 'component-unnamed'; - } - this.name = name; - - if (GameLib.Utils.UndefinedOrNull(componentType)) { - componentType = GameLib.Component.COMPONENT_INTERFACE; - } - this.componentType = componentType; - - /** - * Camera Component - */ - if (GameLib.Utils.UndefinedOrNull(parentEntity)) { - parentEntity = null; - } - this.parentEntity = parentEntity; - - if (GameLib.Utils.UndefinedOrNull(camera)) { - camera = null; - } - this.camera = camera; - - /** - * Follow Component - */ - if (GameLib.Utils.UndefinedOrNull(targetEntity)) { - targetEntity = null; - } - this.targetEntity = targetEntity; - - if(GameLib.Utils.UndefinedOrNull(targetOffset)) { - targetOffset = new GameLib.API.Vector3(0, 0, 0); - } - this.targetOffset = targetOffset; - - if (GameLib.Utils.UndefinedOrNull(moveSpeed)) { - moveSpeed = 12.5; - } - this.moveSpeed = moveSpeed; - - if (GameLib.Utils.UndefinedOrNull(minDistance)) { - minDistance = 0; - } - this.minDistance = minDistance; - - if(GameLib.Utils.UndefinedOrNull(target)) { - target = new GameLib.API.Vector3(0, 0, 0); - } - this.target = target; - - if(GameLib.Utils.UndefinedOrNull(targetToParent)) { - targetToParent = new GameLib.API.Vector3(0, 0, 0); - } - this.targetToParent = targetToParent; - - if(GameLib.Utils.UndefinedOrNull(rotatedTargetOffset)) { - rotatedTargetOffset = new GameLib.API.Vector3(0, 0, 0); - } - this.rotatedTargetOffset = rotatedTargetOffset; - - if (GameLib.Utils.UndefinedOrNull(rotated)) { - rotated = new GameLib.API.Quaternion(); - } - this.rotated = rotated; - - /** - * LookAt Component - */ - if (GameLib.Utils.UndefinedOrNull(rotationSpeed)) { - rotationSpeed = 22.0; - } - this.rotationSpeed = rotationSpeed; - - if (GameLib.Utils.UndefinedOrNull(lookAtMatrix)) { - lookAtMatrix = new GameLib.API.Matrix4(); - } - this.lookAtMatrix = lookAtMatrix; - - if(GameLib.Utils.UndefinedOrNull(up)) { - up = new GameLib.API.Vector3(0, 1, 0); - } - this.up = up; - - /** - * Path Following Component - */ - if (GameLib.Utils.UndefinedOrNull(spline)) { - spline = null; - } - this.spline = spline; - - if (GameLib.Utils.UndefinedOrNull(mesh)) { - mesh = null; - } - this.mesh = mesh; - - if (GameLib.Utils.UndefinedOrNull(accelleration)) { - accelleration = 0.1; - } - this.accelleration = accelleration; - - if (GameLib.Utils.UndefinedOrNull(maxSpeed)) { - maxSpeed = 0.03; - } - this.maxSpeed = maxSpeed; - - if (GameLib.Utils.UndefinedOrNull(baseOffset)) { - baseOffset = new GameLib.API.Vector3(); - } - this.baseOffset = baseOffset; - - if (GameLib.Utils.UndefinedOrNull(maxOffset)) { - maxOffset = new GameLib.API.Vector3(10, 10, 10); - } - this.maxOffset = maxOffset; - - if (GameLib.Utils.UndefinedOrNull(steeringSpeed)) { - steeringSpeed = 1.0; - } - this.steeringSpeed = steeringSpeed; - - if (GameLib.Utils.UndefinedOrNull(currentOffset)) { - currentOffset = new GameLib.API.Vector3(); - } - this.currentOffset = currentOffset; - - if (GameLib.Utils.UndefinedOrNull(currentPathValue)) { - currentPathValue = 0; - } - this.currentPathValue = currentPathValue; - - if (GameLib.Utils.UndefinedOrNull(currentSpeed)) { - currentSpeed = 0; - } - this.currentSpeed = currentSpeed; - - if (GameLib.Utils.UndefinedOrNull(direction)) { - direction = 1; - } - this.direction = direction; - - if (GameLib.Utils.UndefinedOrNull(currentPosition)) { - currentPosition = new GameLib.API.Vector3(); - } - this.currentPosition = currentPosition; -}; \ No newline at end of file diff --git a/src/game-lib-d3-api-follow.js b/src/game-lib-d3-api-follow.js index 1204bd1..cc5a1fe 100644 --- a/src/game-lib-d3-api-follow.js +++ b/src/game-lib-d3-api-follow.js @@ -78,3 +78,22 @@ GameLib.D3.API.Follow = function ( GameLib.D3.API.Follow.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Follow.prototype.constructor = GameLib.D3.API.Follow; + +/** + * Object to GameLib.D3.API.Follow + * @param objectComponent + * @returns {GameLib.D3.API.Follow} + * @constructor + */ +GameLib.D3.API.Follow.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.Follow( + objectComponent.id, + objectComponent.name, + objectComponent.currentComponent, + objectComponent.targetComponent, + GameLib.API.Vector3.FromObjectVector(objectComponent.targetPositionOffset), + objectComponent.minDistance, + objectComponent.moveSpeed, + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-input-drive.js b/src/game-lib-d3-api-input-drive.js index 9b1d452..fc93a3c 100644 --- a/src/game-lib-d3-api-input-drive.js +++ b/src/game-lib-d3-api-input-drive.js @@ -12,6 +12,7 @@ * @param heightOffset * @param distance * @param distanceGrain + * @param rotationFactor * @constructor */ GameLib.D3.API.Input.Drive = function ( @@ -107,3 +108,27 @@ GameLib.D3.API.Input.Drive = function ( GameLib.D3.API.Input.Drive.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Input.Drive.prototype.constructor = GameLib.D3.API.Input.Drive; + +/** + * Object to GameLib.D3.API.Input.Drive + * @param objectComponent + * @returns {GameLib.D3.API.Input.Drive} + * @constructor + */ +GameLib.D3.API.Input.Drive.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.Input.Drive( + objectComponent.id, + objectComponent.name, + objectComponent.domElementId, + objectComponent.pathFollowingComponent, + objectComponent.parentEntity, + objectComponent.wheelFL, + objectComponent.wheelFR, + objectComponent.wheelRL, + objectComponent.wheelRR, + objectComponent.heightOffset, + objectComponent.distance, + objectComponent.distanceGrain, + objectComponent.rotationFactor + ); +}; diff --git a/src/game-lib-d3-api-input-editor.js b/src/game-lib-d3-api-input-editor.bak similarity index 100% rename from src/game-lib-d3-api-input-editor.js rename to src/game-lib-d3-api-input-editor.bak diff --git a/src/game-lib-d3-api-input-fly.js b/src/game-lib-d3-api-input-fly.bak similarity index 100% rename from src/game-lib-d3-api-input-fly.js rename to src/game-lib-d3-api-input-fly.bak diff --git a/src/game-lib-d3-api-light.js b/src/game-lib-d3-api-light.js index f9aa9ef..7525a50 100644 --- a/src/game-lib-d3-api-light.js +++ b/src/game-lib-d3-api-light.js @@ -60,7 +60,7 @@ GameLib.D3.API.Light = function( this.name = name; if (GameLib.Utils.UndefinedOrNull(color)) { - color = new GameLib.D3.API.Color(1,1,1,1); + color = new GameLib.API.Color(1,1,1,1); } this.color = color; @@ -133,7 +133,7 @@ GameLib.D3.API.Light.FromObjectLight = function(objectLight) { objectLight.id, objectLight.lightType, objectLight.name, - GameLib.D3.API.Color.FromObjectColor(objectLight.color), + GameLib.API.Color.FromObjectColor(objectLight.color), objectLight.intensity, GameLib.API.Vector3.FromObjectVector(objectLight.position), GameLib.API.Vector3.FromObjectVector(objectLight.targetPosition), diff --git a/src/game-lib-d3-api-look-at.js b/src/game-lib-d3-api-look-at.js index bfc2a68..8ea789c 100644 --- a/src/game-lib-d3-api-look-at.js +++ b/src/game-lib-d3-api-look-at.js @@ -70,4 +70,22 @@ GameLib.D3.API.LookAt = function ( }; GameLib.D3.API.LookAt.prototype = Object.create(GameLib.Component.prototype); -GameLib.D3.API.LookAt.prototype.constructor = GameLib.D3.API.LookAt; \ No newline at end of file +GameLib.D3.API.LookAt.prototype.constructor = GameLib.D3.API.LookAt; + +/** + * Object to GameLib.D3.API.LookAt + * @param objectComponent + * @returns {GameLib.D3.API.LookAt} + * @constructor + */ +GameLib.D3.API.LookAt.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.LookAt( + objectComponent.id, + objectComponent.name, + objectComponent.currentComponent, + objectComponent.targetComponent, + GameLib.API.Vector3.FromObjectVector(objectComponent.targetPositionOffset), + objectComponent.rotationSpeed, + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-material.js b/src/game-lib-d3-api-material.js index 225dfa6..967b15a 100644 --- a/src/game-lib-d3-api-material.js +++ b/src/game-lib-d3-api-material.js @@ -146,18 +146,18 @@ GameLib.D3.API.Material = function( this, GameLib.Component.COMPONENT_MATERIAL, { - alphaMap : GameLib.D3.Texture, - aoMap : GameLib.D3.Texture, - bumpMap : GameLib.D3.Texture, - diffuseMap : GameLib.D3.Texture, - displacementMap : GameLib.D3.Texture, - emissiveMap : GameLib.D3.Texture, - environmentMap : GameLib.D3.Texture, - lightMap : GameLib.D3.Texture, - metalnessMap : GameLib.D3.Texture, - normalMap : GameLib.D3.Texture, - roughnessMap : GameLib.D3.Texture, - specularMap : GameLib.D3.Texture + 'alphaMap' : GameLib.D3.Texture, + 'aoMap' : GameLib.D3.Texture, + 'bumpMap' : GameLib.D3.Texture, + 'diffuseMap' : GameLib.D3.Texture, + 'displacementMap' : GameLib.D3.Texture, + 'emissiveMap' : GameLib.D3.Texture, + 'environmentMap' : GameLib.D3.Texture, + 'lightMap' : GameLib.D3.Texture, + 'metalnessMap' : GameLib.D3.Texture, + 'normalMap' : GameLib.D3.Texture, + 'roughnessMap' : GameLib.D3.Texture, + 'specularMap' : GameLib.D3.Texture }, false, parentEntity @@ -194,7 +194,7 @@ GameLib.D3.API.Material = function( this.transparent = transparent; if (GameLib.Utils.UndefinedOrNull(specular)) { - specular = new GameLib.D3.API.Color(0.06, 0.06, 0.06, 0.06); + specular = new GameLib.API.Color(0.06, 0.06, 0.06, 0.06); } this.specular = specular; @@ -209,12 +209,12 @@ GameLib.D3.API.Material = function( this.aoMapIntensity = aoMapIntensity; if (GameLib.Utils.UndefinedOrNull(color)) { - color = new GameLib.D3.API.Color(1, 1, 1, 1) + color = new GameLib.API.Color(1, 1, 1, 1) } this.color = color; if (GameLib.Utils.UndefinedOrNull(emissive)) { - emissive = new GameLib.D3.API.Color(0, 0, 0, 0); + emissive = new GameLib.API.Color(0, 0, 0, 0); } this.emissive = emissive; @@ -473,6 +473,11 @@ GameLib.D3.API.Material = function( } this.emissiveMap = emissiveMap; + if (GameLib.Utils.UndefinedOrNull(environmentMap)) { + environmentMap = null; + } + this.environmentMap = environmentMap; + if (GameLib.Utils.UndefinedOrNull(lightMap)) { lightMap = null; } @@ -577,11 +582,11 @@ GameLib.D3.API.Material.FromObjectMaterial = function(objectMaterial) { objectMaterial.opacity, objectMaterial.side, objectMaterial.transparent, - GameLib.D3.API.Color.FromObjectColor(objectMaterial.specular), + GameLib.API.Color.FromObjectColor(objectMaterial.specular), objectMaterial.lightMapIntensity, objectMaterial.aoMapIntensity, - GameLib.D3.API.Color.FromObjectColor(objectMaterial.color), - GameLib.D3.API.Color.FromObjectColor(objectMaterial.emissive), + GameLib.API.Color.FromObjectColor(objectMaterial.color), + GameLib.API.Color.FromObjectColor(objectMaterial.emissive), objectMaterial.emissiveIntensity, objectMaterial.combine, objectMaterial.shininess, diff --git a/src/game-lib-d3-api-path-following.js b/src/game-lib-d3-api-path-following.js index e4a9a6f..8586a08 100644 --- a/src/game-lib-d3-api-path-following.js +++ b/src/game-lib-d3-api-path-following.js @@ -169,3 +169,42 @@ GameLib.D3.API.PathFollowing = function ( GameLib.D3.API.PathFollowing.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.PathFollowing.prototype.constructor = GameLib.D3.API.PathFollowing; + +/** + * Returns an API path following component from an Object path following component + * @param objectComponent + * @constructor + */ +GameLib.D3.API.PathFollowing.FromObjectComponent = function(objectComponent) { + + var apiRaycaster = null; + + if (objectComponent.raycaster) { + apiRaycaster = GameLib.D3.API.Raycaster.FromObjectRaycaster(objectComponent.raycaster); + } + + return new GameLib.D3.API.PathFollowing( + objectComponent.id, + objectComponent.name, + objectComponent.spline, + objectComponent.mesh, + objectComponent.raytraceMesh, + objectComponent.accelleration, + objectComponent.maxSpeed, + GameLib.API.Vector3.FromObjectVector(objectComponent.baseOffset), + GameLib.API.Vector3.FromObjectVector(objectComponent.maxOffset), + objectComponent.steeringSpeed, + GameLib.API.Vector3.FromObjectVector(objectComponent.targetOffset), + GameLib.API.Vector3.FromObjectVector(objectComponent.currentOffset), + objectComponent.currentPathValue, + objectComponent.currentSpeed, + objectComponent.direction, + apiRaycaster, + GameLib.API.Vector3.FromObjectVector(objectComponent.currentPosition), + GameLib.API.Vector3.FromObjectVector(objectComponent.futurePosition), + GameLib.API.Vector3.FromObjectVector(objectComponent.up), + GameLib.API.Matrix4.FromObjectMatrix(objectComponent.rotationMatrix), + GameLib.API.Quaternion.FromObjectQuaternion(objectComponent.rotationVector), + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-raycaster.js b/src/game-lib-d3-api-raycaster.js index ad10855..36d83c0 100644 --- a/src/game-lib-d3-api-raycaster.js +++ b/src/game-lib-d3-api-raycaster.js @@ -19,3 +19,15 @@ GameLib.D3.API.Raycaster = function( } this.direction = direction; }; + +/** + * Returns an API raycaster from an Object raycaster + * @param objectRaycaster + * @constructor + */ +GameLib.D3.API.Raycaster.FromObjectRaycaster = function(objectRaycaster) { + return new GameLib.D3.API.Raycaster( + GameLib.API.Vector3.FromObjectVector(objectRaycaster.position), + GameLib.API.Vector3.FromObjectVector(objectRaycaster.direction) + ); +}; diff --git a/src/game-lib-d3-api-renderable.js b/src/game-lib-d3-api-renderable.js deleted file mode 100644 index 7567897..0000000 --- a/src/game-lib-d3-api-renderable.js +++ /dev/null @@ -1,36 +0,0 @@ -/** - * This component makes the parentEntity (ex. car) follow the path provided by the spline - * @param id String - * @param name String - * @param visible - * @constructor - */ -GameLib.D3.API.Renderable = function ( - id, - name, - visible -) { - - GameLib.Component.call( - this, - GameLib.Component.COMPONENT_RENDERABLE - ); - - if (GameLib.Utils.UndefinedOrNull(id)) { - id = GameLib.Utils.RandomId(); - } - this.id = id; - - if (GameLib.Utils.UndefinedOrNull(name)) { - name = this.constructor.name; - } - this.name = name; - - if (GameLib.Utils.UndefinedOrNull(visible)) { - visible = true; - } - this.visible = visible; -}; - -GameLib.D3.API.Renderable.prototype = Object.create(GameLib.Component.prototype); -GameLib.D3.API.Renderable.prototype.constructor = GameLib.D3.API.Renderable; diff --git a/src/game-lib-d3-api-renderer.js b/src/game-lib-d3-api-renderer.js index 3eac4ae..2db8b41 100644 --- a/src/game-lib-d3-api-renderer.js +++ b/src/game-lib-d3-api-renderer.js @@ -78,3 +78,22 @@ GameLib.D3.API.Renderer = function ( GameLib.D3.API.Renderer.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.API.Renderer.prototype.constructor = GameLib.D3.API.Renderer; + +/** + * Object to GameLib.D3.API.Renderer + * @param objectComponent + * @constructor + */ +GameLib.D3.API.Renderer.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.Renderer( + objectComponent.id, + objectComponent.name, + objectComponent.scene, + objectComponent.camera, + objectComponent.autoClear, + objectComponent.localClipping, + objectComponent.width, + objectComponent.height, + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index b1004d6..7cab28f 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -14,6 +14,7 @@ * @param shapes GameLib.D3.API.Shape[] * @param cameras * @param activeCameraIndex + * @param textures GameLib.D3.Texture[] - additional textures * @constructor */ GameLib.D3.API.Scene = function( @@ -30,7 +31,8 @@ GameLib.D3.API.Scene = function( entityManager, shapes, cameras, - activeCameraIndex + activeCameraIndex, + textures ) { if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -101,6 +103,11 @@ GameLib.D3.API.Scene = function( activeCameraIndex = 0; } this.activeCameraIndex = activeCameraIndex; + + if (GameLib.Utils.UndefinedOrNull(textures)) { + textures = []; + } + this.textures = textures; }; /** @@ -112,10 +119,20 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { var apiEntityManager = null; + var apiTextures = null; + if (objectScene.entityManager) { apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectScene.entityManager); } + if (objectScene.textures) { + apiTextures = objectScene.textures.map( + function(objectTexture) { + return GameLib.D3.API.Texture.FromObjectTexture(objectTexture) + } + ) + } + return new GameLib.D3.API.Scene( objectScene.id, objectScene.path, @@ -142,7 +159,8 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { return GameLib.D3.API.Camera.FromObjectCamera(objectCamera); } ), - objectScene.activeCameraIndex + objectScene.activeCameraIndex, + apiTextures ); }; diff --git a/src/game-lib-d3-api-spline.js b/src/game-lib-d3-api-spline.js index 616d4b4..d803f46 100644 --- a/src/game-lib-d3-api-spline.js +++ b/src/game-lib-d3-api-spline.js @@ -37,4 +37,22 @@ GameLib.D3.API.Spline = function( }; GameLib.D3.API.Spline.prototype = Object.create(GameLib.Component.prototype); -GameLib.D3.API.Spline.prototype.constructor = GameLib.D3.API.Spline; \ No newline at end of file +GameLib.D3.API.Spline.prototype.constructor = GameLib.D3.API.Spline; + +/** + * Object to GameLib.D3.API.Spline + * @param objectComponent + * @constructor + */ +GameLib.D3.API.Spline.FromObjectComponent = function(objectComponent) { + return new GameLib.D3.API.Spline( + objectComponent.id, + objectComponent.name, + objectComponent.vertices.map( + function (objectVertex) { + return GameLib.API.Vector3.FromObjectVector(objectVertex); + } + ), + objectComponent.parentEntity + ); +}; diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 3760478..2c1548c 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -57,7 +57,7 @@ GameLib.D3.API.Texture = function( this.typeId = typeId; if (GameLib.Utils.UndefinedOrNull(name)) { - name = 'Texture (' + typeId + ')'; + name = 'Texture (' + id + ')'; } this.name = name; @@ -158,7 +158,7 @@ GameLib.D3.API.Texture = function( * @constructor */ GameLib.D3.API.Texture.FromObjectTexture = function(objectTexture) { - new GameLib.D3.API.Texture( + return new GameLib.D3.API.Texture( objectTexture.id, objectTexture.textureType, objectTexture.name, diff --git a/src/game-lib-d3-api-vertex.js b/src/game-lib-d3-api-vertex.js index 457e5c6..a1c5124 100644 --- a/src/game-lib-d3-api-vertex.js +++ b/src/game-lib-d3-api-vertex.js @@ -28,6 +28,10 @@ GameLib.D3.API.Vertex = function( GameLib.D3.API.Vertex.FromObjectVertex = function(objectVertex) { return new GameLib.D3.API.Vertex( GameLib.API.Vector3.FromObjectVector(objectVertex.position), - objectVertex.boneWeights + objectVertex.boneWeights.map( + function(boneWeight) { + return GameLib.D3.API.BoneWeight.FromObjectBoneWeight(boneWeight); + } + ) ); }; diff --git a/src/game-lib-d3-bone-weight.js b/src/game-lib-d3-bone-weight.js index dad6f93..0ed77bb 100644 --- a/src/game-lib-d3-bone-weight.js +++ b/src/game-lib-d3-bone-weight.js @@ -4,7 +4,7 @@ * @param graphics GameLib.D3.Graphics * @param apiBoneWeight GameLib.D3.API.BoneWeight */ -GameLib.D3.BoneWeight = function RuntimeBoneWeight( +GameLib.D3.BoneWeight = function ( graphics, apiBoneWeight ) { @@ -16,40 +16,11 @@ GameLib.D3.BoneWeight = function RuntimeBoneWeight( apiBoneWeight.boneIndex, apiBoneWeight.weight ); - - this.instance = this.createInstance(); }; GameLib.D3.BoneWeight.prototype = Object.create(GameLib.D3.API.BoneWeight.prototype); GameLib.D3.BoneWeight.prototype.constructor = GameLib.D3.BoneWeight; - -/** - * Creates an instance boneWeight - * @param update boolean - */ -GameLib.D3.BoneWeight.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - //TODO - update instance with boneWeight info - instance = this.instance; - } else { - //TODO - fix this - instance = new THREE.Quaternion(); - } - - return instance; -}; - -/** - * Updates the instance - */ -GameLib.D3.BoneWeight.prototype.updateInstance = function() { - this.instance = this.createInstance(true); -}; - /** * Converts a GameLib.D3.BoneWeight to GameLib.D3.API.BoneWeight * @returns {GameLib.D3.API.BoneWeight} @@ -75,10 +46,7 @@ GameLib.D3.BoneWeight.FromObjectBoneWeight = function( graphics, objectBoneWeight ) { - var apiBoneWeight = new GameLib.D3.API.BoneWeight( - objectBoneWeight.boneIndex, - objectBoneWeight.weight - ); + var apiBoneWeight = GameLib.D3.API.BoneWeight.FromObjectBoneWeight(objectBoneWeight); var boneWeight = new GameLib.D3.BoneWeight( graphics, diff --git a/src/game-lib-d3-color.js b/src/game-lib-d3-color.js deleted file mode 100644 index c0013b5..0000000 --- a/src/game-lib-d3-color.js +++ /dev/null @@ -1,142 +0,0 @@ -/** - * Runtime color for updating instance objects - * @param graphics GameLib.D3.Graphics - * @param parentObject GameLib.D3.* - * @param apiColor GameLib.D3.API.Color - * @param grain Number - * @constructor - */ -GameLib.D3.Color = function (graphics, parentObject, apiColor, grain) { - - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - - GameLib.D3.API.Color.call( - this, - apiColor.r, - apiColor.g, - apiColor.b, - apiColor.a - ); - - this.parentObject = parentObject; - - if (GameLib.Utils.UndefinedOrNull(grain)) { - grain = 0.001; - } - this.grain = grain; - - this.instance = this.createInstance(); -}; - -GameLib.D3.Color.prototype = Object.create(GameLib.D3.API.Color.prototype); -GameLib.D3.Color.prototype.constructor = GameLib.D3.Color; - -/** - * Creates an instance color - * @param update - * @returns {*} - */ -GameLib.D3.Color.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - instance.r = this.r; - instance.g = this.g; - instance.b = this.b; - } else { - instance = THREE.Color(this.r, this.g, this.b); - } - - return instance; -}; - -/** - * Updates the instance color, calls updateInstance on the parent object - */ -GameLib.D3.Color.prototype.updateInstance = function() { - - this.createInstance(true); - - if (this.parentObject.updateInstance) { - this.parentObject.updateInstance(); - } -}; - -/** - * Converts runtime color to API Color - * @returns {GameLib.D3.API.Color} - */ -GameLib.D3.Color.prototype.toApiColor = function() { - return new GameLib.D3.API.Color( - this.r, - this.g, - this.b, - this.a - ); -}; - -/** - * Converts the current color to HTML hex format (ex. #ffffff) - * @returns {string} - */ -GameLib.D3.Color.prototype.toHex = function() { - - if (this.r < 0) { - this.r = 0; - } - - if (this.g < 0) { - this.g = 0; - } - - if (this.b < 0) { - this.b = 0; - } - - if (this.r > 1) { - this.r = 1; - } - - if (this.g > 1) { - this.g = 1; - } - - if (this.b > 1) { - this.b = 1; - } - - var rf = Math.floor(this.r >= 1? 255 : this.r * 256.0).toString(16); - var gf = Math.floor(this.g >= 1? 255 : this.g * 256.0).toString(16); - var bf = Math.floor(this.b >= 1? 255 : this.b * 256.0).toString(16); - - if (rf.length < 2) { - rf = '0' + rf; - } - - if (gf.length < 2) { - gf = '0' + gf; - } - - if (bf.length < 2) { - bf = '0' + bf; - } - - return '#' + rf + gf + bf; -}; - -/** - * Sets this object color to what the hex value is - * @param hex - * @returns {string} - */ -GameLib.D3.Color.prototype.fromHex = function(hex) { - - var matches = hex.match(new RegExp('#+(..)(..)(..)')); - - this.r = parseInt(matches[1], 16) / 255.0; - this.g = parseInt(matches[2], 16) / 255.0; - this.b = parseInt(matches[3], 16) / 255.0; -}; \ No newline at end of file diff --git a/src/game-lib-d3-follow.js b/src/game-lib-d3-follow.js index 3221496..2af3080 100644 --- a/src/game-lib-d3-follow.js +++ b/src/game-lib-d3-follow.js @@ -1,13 +1,11 @@ /** * Runtime Follow Component * @param graphics GameLib.D3.Graphics - * @param parentObject * @param apiFollow * @constructor */ -GameLib.D3.Follow = function RuntimeFollow( +GameLib.D3.Follow = function ( graphics, - parentObject, apiFollow ) { this.graphics = graphics; @@ -77,24 +75,10 @@ GameLib.D3.Follow.prototype.toApiComponent = function() { GameLib.D3.Follow.FromObjectComponent = function(graphics, objectComponent) { - var apiFollow = new GameLib.D3.API.Follow( - objectComponent.id, - objectComponent.name, - objectComponent.currentComponent, - objectComponent.targetComponent, - new GameLib.API.Vector3( - objectComponent.targetPositionOffset.x, - objectComponent.targetPositionOffset.y, - objectComponent.targetPositionOffset.z - ), - objectComponent.minDistance, - objectComponent.moveSpeed, - objectComponent.parentEntity - ); + var apiFollow = GameLib.D3.API.Follow.FromObjectComponent(objectComponent); return new GameLib.D3.Follow( graphics, - this, apiFollow ); }; diff --git a/src/game-lib-d3-input-drive.js b/src/game-lib-d3-input-drive.js index ef686a5..b9492ef 100644 --- a/src/game-lib-d3-input-drive.js +++ b/src/game-lib-d3-input-drive.js @@ -5,20 +5,14 @@ * @param apiInputDrive GameLib.D3.API.Input.Drive * @constructor */ -GameLib.D3.Input.Drive = function RuntimeInputDrive( +GameLib.D3.Input.Drive = function ( graphics, - parentObject, apiInputDrive ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; - GameLib.D3.API.Input.Drive.call( this, apiInputDrive.id, @@ -89,7 +83,10 @@ GameLib.D3.Input.Drive.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; - +/** + * GameLib.D3.Input.Drive to GameLib.D3.API.Input.Drive + * @returns {GameLib.D3.API.Input.Drive} + */ GameLib.D3.Input.Drive.prototype.toApiComponent = function() { var apiInputDrive = new GameLib.D3.API.Input.Drive( @@ -113,25 +110,10 @@ GameLib.D3.Input.Drive.prototype.toApiComponent = function() { GameLib.D3.Input.Drive.FromObjectComponent = function(graphics, objectComponent) { - var apiInputDrive = new GameLib.D3.API.Input.Drive( - objectComponent.id, - objectComponent.name, - objectComponent.domElementId, - objectComponent.pathFollowingComponent, - objectComponent.parentEntity, - objectComponent.wheelFL, - objectComponent.wheelFR, - objectComponent.wheelRL, - objectComponent.wheelRR, - objectComponent.heightOffset, - objectComponent.distance, - objectComponent.distanceGrain, - objectComponent.rotationFactor - ); + var apiInputDrive = GameLib.D3.API.Input.Drive.FromObjectComponent(objectComponent); return new GameLib.D3.Input.Drive( graphics, - this, apiInputDrive ); }; diff --git a/src/game-lib-d3-input-editor.js b/src/game-lib-d3-input-editor.bak similarity index 100% rename from src/game-lib-d3-input-editor.js rename to src/game-lib-d3-input-editor.bak diff --git a/src/game-lib-d3-input-fly.js b/src/game-lib-d3-input-fly.bak similarity index 100% rename from src/game-lib-d3-input-fly.js rename to src/game-lib-d3-input-fly.bak diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index 8cb7718..7e3956b 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -31,7 +31,7 @@ GameLib.D3.Light = function Light( apiLight.parentEntity ); - this.color = new GameLib.D3.Color( + this.color = new GameLib.Color( graphics, this, this.color, diff --git a/src/game-lib-d3-look-at.js b/src/game-lib-d3-look-at.js index a54a7d8..e2048ce 100644 --- a/src/game-lib-d3-look-at.js +++ b/src/game-lib-d3-look-at.js @@ -1,23 +1,16 @@ /** * Entities with LookAt component looks to targetPosition (default up is 0,1,0) * @param graphics GameLib.D3.Graphics - * @param parentObject * @param apiLookAt GameLib.D3.API.LookAt * @constructor */ -GameLib.D3.LookAt = function RuntimeLookAt( +GameLib.D3.LookAt = function ( graphics, - parentObject, apiLookAt ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; - GameLib.D3.API.LookAt.call( this, apiLookAt.id, @@ -80,25 +73,16 @@ GameLib.D3.LookAt.prototype.toApiComponent = function() { GameLib.D3.LookAt.FromObjectComponent = function(graphics, objectComponent) { - var apiLookAt = new GameLib.D3.API.LookAt( - objectComponent.id, - objectComponent.name, - objectComponent.currentComponent, - objectComponent.targetComponent, - objectComponent.targetPositionOffset, - objectComponent.rotationSpeed, - objectComponent.parentEntity - ); + var apiLookAt = GameLib.D3.API.LookAt.FromObjectComponent(objectComponent); return new GameLib.D3.LookAt( graphics, - this, apiLookAt ); }; /** - * Updates the component + * Looks at using time * @param deltaTime */ GameLib.D3.LookAt.prototype.update = function(deltaTime) { @@ -111,14 +95,6 @@ GameLib.D3.LookAt.prototype.update = function(deltaTime) { this.targetPosition.updateInstance(); - // - // this.currentComponent.instance.lookAt(this.targetPosition); - // - // this.currentComponent.instance.updateProjectionMatrix(); - // - // // this.currentComponent.updateInstance(); - // return; - this.lookAtMatrix.lookAt( this.currentComponent.position, this.targetPosition, diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index ac6cf53..01ad7ad 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -89,120 +89,144 @@ GameLib.D3.Material = function Material( apiMaterial.parentEntity ); - this.specular = new GameLib.D3.Color( + this.specular = new GameLib.Color( graphics, this, this.specular ); - this.color = new GameLib.D3.Color( + this.color = new GameLib.Color( graphics, this, this.color ); - this.emissive = new GameLib.D3.Color( + this.emissive = new GameLib.Color( graphics, this, this.emissive ); - this.alphaMap = new GameLib.D3.Texture( - this.graphics, - this.alphaMap, - this, - 'alphaMap', - imageFactory - ); + if (this.alphaMap) { + this.alphaMap = new GameLib.D3.Texture( + this.graphics, + this.alphaMap, + this, + 'alphaMap', + imageFactory + ); + } - this.aoMap = new GameLib.D3.Texture( - this.graphics, - this.aoMap, - this, - 'aoMap', - imageFactory - ); + if (this.aoMap) { + this.aoMap = new GameLib.D3.Texture( + this.graphics, + this.aoMap, + this, + 'aoMap', + imageFactory + ); + } - this.bumpMap = new GameLib.D3.Texture( - this.graphics, - this.bumpMap, - this, - 'bumpMap', - imageFactory - ); + if (this.bumpMap) { + this.bumpMap = new GameLib.D3.Texture( + this.graphics, + this.bumpMap, + this, + 'bumpMap', + imageFactory + ); + } - this.diffuseMap = new GameLib.D3.Texture( - this.graphics, - this.diffuseMap, - this, - 'map', - imageFactory - ); + if (this.diffuseMap) { + this.diffuseMap = new GameLib.D3.Texture( + this.graphics, + this.diffuseMap, + this, + 'map', + imageFactory + ); + } - this.displacementMap = new GameLib.D3.Texture( - this.graphics, - this.displacementMap, - this, - 'displacementMap', - imageFactory - ); + if (this.displacementMap) { + this.displacementMap = new GameLib.D3.Texture( + this.graphics, + this.displacementMap, + this, + 'displacementMap', + imageFactory + ); + } - this.emissiveMap = new GameLib.D3.Texture( - this.graphics, - this.emissiveMap, - this, - 'emissiveMap', - imageFactory - ); + if (this.emissiveMap) { + this.emissiveMap = new GameLib.D3.Texture( + this.graphics, + this.emissiveMap, + this, + 'emissiveMap', + imageFactory + ); + } - this.environmentMap = new GameLib.D3.Texture( - this.graphics, - this.environmentMap, - this, - 'envMap', - imageFactory - ); + if (this.environmentMap) { + this.environmentMap = new GameLib.D3.Texture( + this.graphics, + this.environmentMap, + this, + 'envMap', + imageFactory + ); + } - this.lightMap = new GameLib.D3.Texture( - this.graphics, - this.lightMap, - this, - 'lightMap', - imageFactory - ); + if (this.lightMap) { + this.lightMap = new GameLib.D3.Texture( + this.graphics, + this.lightMap, + this, + 'lightMap', + imageFactory + ); + } - this.metalnessMap = new GameLib.D3.Texture( - this.graphics, - this.metalnessMap, - this, - 'metalnessMap', - imageFactory - ); + if (this.metalnessMap) { + this.metalnessMap = new GameLib.D3.Texture( + this.graphics, + this.metalnessMap, + this, + 'metalnessMap', + imageFactory + ); + } - this.normalMap = new GameLib.D3.Texture( - this.graphics, - this.normalMap, - this, - 'normalMap', - imageFactory - ); + if (this.normalMap) { + this.normalMap = new GameLib.D3.Texture( + this.graphics, + this.normalMap, + this, + 'normalMap', + imageFactory + ); + } - this.roughnessMap = new GameLib.D3.Texture( - this.graphics, - this.roughnessMap, - this, - 'roughnessMap', - imageFactory - ); + if (this.roughnessMap) { + this.roughnessMap = new GameLib.D3.Texture( + this.graphics, + this.roughnessMap, + this, + 'roughnessMap', + imageFactory + ); + } + + if (this.specularMap) { + this.specularMap = new GameLib.D3.Texture( + this.graphics, + this.specularMap, + this, + 'specularMap', + imageFactory + ); + } - this.specularMap = new GameLib.D3.Texture( - this.graphics, - this.specularMap, - this, - 'specularMap', - imageFactory - ); - this.instance = this.createInstance(); }; @@ -477,40 +501,64 @@ GameLib.D3.Material.prototype.createInstance = function(update) { if (instance.hasOwnProperty(property)) { if (property == 'alphaMap') { - instance.alphaMap = this.alphaMap.instance; + if (this.alphaMap) { + instance.alphaMap = this.alphaMap.instance; + } } else if (property == 'aoMap') { - instance.aoMap = this.aoMap.instance; + if (this.aoMap) { + instance.aoMap = this.aoMap.instance; + } } else if (property == 'bumpMap') { - instance.bumpMap = this.bumpMap.instance; + if (this.bumpMap) { + instance.bumpMap = this.bumpMap.instance; + } } else if (property == 'map') { - instance.map = this.diffuseMap.instance; + if (this.diffuseMap) { + instance.map = this.diffuseMap.instance; + } } else if (property == 'displacementMap') { - instance.displacementMap = this.displacementMap.instance; + if (this.displacementMap) { + instance.displacementMap = this.displacementMap.instance; + } } else if (property == 'emissiveMap') { - instance.emissiveMap = this.emissiveMap.instance; + if (this.emissiveMap) { + instance.emissiveMap = this.emissiveMap.instance; + } } else if (property == 'envMap') { - instance.envMap = this.environmentMap.instance; + if (this.environmentMap) { + instance.envMap = this.environmentMap.instance; + } } else if (property == 'lightMap') { - instance.lightMap = this.lightMap.instance; + if (this.lightMap) { + instance.lightMap = this.lightMap.instance; + } } else if (property == 'metalnessMap') { - instance.metalnessMap = this.metalnessMap.instance; + if (this.metalnessMap) { + instance.metalnessMap = this.metalnessMap.instance; + } } else if (property == 'normalMap') { - instance.normalMap = this.normalMap.instance; + if (this.normalMap) { + instance.normalMap = this.normalMap.instance; + } } else if (property == 'roughnessMap') { - instance.roughnessMap = this.roughnessMap.instance; + if (this.roughnessMap) { + instance.roughnessMap = this.roughnessMap.instance; + } } else if (property == 'specularMap') { - instance.specularMap = this.specularMap.instance; + if (this.specularMap) { + instance.specularMap = this.specularMap.instance; + } } else if (property == 'size') { instance.size = this.pointSize; @@ -520,7 +568,7 @@ GameLib.D3.Material.prototype.createInstance = function(update) { } else if (instance[property] instanceof THREE.Color) { instance[property].copy(this[property]) - } else { + } else if (this.hasOwnProperty(property)) { instance[property] = this[property]; } } @@ -549,6 +597,66 @@ GameLib.D3.Material.prototype.clone = function() { */ GameLib.D3.Material.prototype.toApiMaterial = function() { + var apiAlphaMap = null; + if (this.alphaMap) { + apiAlphaMap = this.alphaMap.toApiTexture(); + } + + var apiAoMap = null; + if (this.aoMap) { + apiAoMap = this.aoMap.toApiTexture(); + } + + var apiBumpMap = null; + if (this.bumpMap) { + apiBumpMap = this.bumpMap.toApiTexture(); + } + + var apiDiffuseMap = null; + if (this.diffuseMap) { + apiDiffuseMap = this.diffuseMap.toApiTexture(); + } + + var apiDisplacementMap = null; + if (this.displacementMap) { + apiDisplacementMap = this.displacementMap.toApiTexture(); + } + + var apiEmissiveMap = null; + if (this.emissiveMap) { + apiEmissiveMap = this.emissiveMap.toApiTexture(); + } + + var apiEnvironmentMap = null; + if (this.environmentMap) { + apiEnvironmentMap = this.environmentMap.toApiTexture(); + } + + var apiLightMap = null; + if (this.lightMap) { + apiLightMap = this.lightMap.toApiTexture(); + } + + var apiMetalnessMap = null; + if (this.metalnessMap) { + apiMetalnessMap = this.metalnessMap.toApiTexture(); + } + + var apiNormalMap = null; + if (this.normalMap) { + apiNormalMap = this.normalMap.toApiTexture(); + } + + var apiRoughnessMap = null; + if (this.roughnessMap) { + apiRoughnessMap = this.roughnessMap.toApiTexture(); + } + + var apiSpecularMap = null; + if (this.specularMap) { + apiSpecularMap = this.specularMap.toApiTexture(); + } + return new GameLib.D3.API.Material( this.id, this.materialType, @@ -606,18 +714,18 @@ GameLib.D3.Material.prototype.toApiMaterial = function() { this.pointSizeAttenuation, this.spriteRotation, this.envMapIntensity, - this.alphaMap.toApiTexture(), - this.aoMap.toApiTexture(), - this.bumpMap.toApiTexture(), - this.diffuseMap.toApiTexture(), - this.displacementMap.toApiTexture(), - this.emissiveMap.toApiTexture(), - this.environmentMap.toApiTexture(), - this.lightMap.toApiTexture(), - this.metalnessMap.toApiTexture(), - this.normalMap.toApiTexture(), - this.roughnessMap.toApiTexture(), - this.specularMap.toApiTexture(), + apiAlphaMap, + apiAoMap, + apiBumpMap, + apiDiffuseMap, + apiDisplacementMap, + apiEmissiveMap, + apiEnvironmentMap, + apiLightMap, + apiMetalnessMap, + apiNormalMap, + apiRoughnessMap, + apiSpecularMap, GameLib.Utils.IdOrNull(this.parentEntity) ); }; diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index 942ce4f..fcf4bb5 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -51,10 +51,13 @@ GameLib.D3.Mesh = function ( }.bind(this) ); - this.skeleton = new GameLib.D3.Skeleton( - this.graphics, - this.skeleton - ); + if (this.skeleton) { + this.skeleton = new GameLib.D3.Skeleton( + this.graphics, + this.skeleton + ); + + } this.vertices = this.vertices.map( function (apiVertex) { diff --git a/src/game-lib-d3-path-following.js b/src/game-lib-d3-path-following.js index ad5ebfe..be72939 100644 --- a/src/game-lib-d3-path-following.js +++ b/src/game-lib-d3-path-following.js @@ -1,24 +1,17 @@ /** * This component makes the parentEntity (ex. car) follow the path provided by the spline * @param graphics GameLib.D3.Graphics - * @param parentObject * @param apiPathFollowing GameLib.D3.API.PathFollowing * @constructor */ -GameLib.D3.PathFollowing = function RuntimePathFollowing( +GameLib.D3.PathFollowing = function ( graphics, - parentObject, apiPathFollowing ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; - GameLib.D3.API.PathFollowing.call( this, apiPathFollowing.id, @@ -71,7 +64,6 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing( this.raycaster = new GameLib.D3.Raycaster( this.graphics, - this, this.raycaster ); @@ -105,7 +97,6 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing( this.rotationVector ); - this.mx = new GameLib.Utils.MovingAverage(10); this.my = new GameLib.Utils.MovingAverage(10); this.mz = new GameLib.Utils.MovingAverage(10); @@ -149,86 +140,19 @@ GameLib.D3.PathFollowing.prototype.toApiComponent = function() { return apiPathFollowing; }; +/** + * Object path following to GameLib.D3.PathFollowing + * @param graphics + * @param objectComponent + * @returns {GameLib.D3.PathFollowing} + * @constructor + */ GameLib.D3.PathFollowing.FromObjectComponent = function(graphics, objectComponent) { - var apiPathFollowing = new GameLib.D3.API.PathFollowing( - objectComponent.id, - objectComponent.name, - objectComponent.spline, - objectComponent.mesh, - objectComponent.raytraceMesh, - objectComponent.accelleration, - objectComponent.maxSpeed, - new GameLib.API.Vector3( - objectComponent.baseOffset.x, - objectComponent.baseOffset.y, - objectComponent.baseOffset.z - ), - new GameLib.API.Vector3( - objectComponent.maxOffset.x, - objectComponent.maxOffset.y, - objectComponent.maxOffset.z - ), - objectComponent.steeringSpeed, - new GameLib.API.Vector3( - objectComponent.targetOffset.x, - objectComponent.targetOffset.y, - objectComponent.targetOffset.z - ), - new GameLib.API.Vector3( - objectComponent.currentOffset.x, - objectComponent.currentOffset.y, - objectComponent.currentOffset.z - ), - this.currentPathValue, - this.currentSpeed, - this.direction, - new GameLib.D3.API.Raycaster( - new GameLib.API.Vector3( - objectComponent.raycaster.position.x, - objectComponent.raycaster.position.y, - objectComponent.raycaster.position.z - ), - new GameLib.API.Vector3( - objectComponent.raycaster.direction.x, - objectComponent.raycaster.direction.y, - objectComponent.raycaster.direction.z - ) - ), - new GameLib.API.Vector3( - objectComponent.currentPosition.x, - objectComponent.currentPosition.y, - objectComponent.currentPosition.z - ), - new GameLib.API.Vector3( - objectComponent.futurePosition.x, - objectComponent.futurePosition.y, - objectComponent.futurePosition.z - ), - new GameLib.API.Vector3( - objectComponent.up.x, - objectComponent.up.y, - objectComponent.up.z - ), - //TODO : objectComponent rotationVector matrix4 - new GameLib.API.Matrix4( - new GameLib.API.Quaternion(), - new GameLib.API.Quaternion(), - new GameLib.API.Quaternion(), - new GameLib.API.Quaternion() - ), - new GameLib.API.Quaternion( - objectComponent.rotationVector.x, - objectComponent.rotationVector.y, - objectComponent.rotationVector.z, - objectComponent.rotationVector.w - ), - objectComponent.parentEntity - ); + var apiPathFollowing = GameLib.D3.API.PathFollowing.FromObjectComponent(objectComponent); return new GameLib.D3.PathFollowing( graphics, - this, apiPathFollowing ); }; diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index 77d98d9..9470062 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -7,18 +7,12 @@ */ GameLib.D3.Raycaster = function( graphics, - parentObject, apiRaycaster ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; - GameLib.D3.API.Raycaster.call( this, apiRaycaster.position, @@ -72,6 +66,19 @@ GameLib.D3.Raycaster.prototype.toApiRaycaster = function() { ) }; +GameLib.D3.Raycaster.FromObjectRaycaster = function(graphics, parentObject, objectRaycaster) { + + var apiRaycaster = GameLib.D3.API.Raycaster.FromObjectRaycaster(objectRaycaster); + + var raycaster = new GameLib.D3.Raycaster( + graphics, + parentObject, + apiRaycaster + ); + + return raycaster; +}; + /** * Sets the direction and position of this raycaster * @param position GameLib.Vector3 diff --git a/src/game-lib-d3-renderable.js b/src/game-lib-d3-renderable.js deleted file mode 100644 index 67725b5..0000000 --- a/src/game-lib-d3-renderable.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * This component makes the parentEntity (ex. car) follow the path provided by the spline - * @param graphics GameLib.D3.Graphics - * @param parentObject - * @param apiRenderable GameLib.D3.API.Renderable - * @constructor - */ -GameLib.D3.Renderable = function RuntimeRenderable( - graphics, - parentObject, - apiRenderable -) { - - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; - - GameLib.D3.API.Renderable.call( - this, - apiRenderable.id, - apiRenderable.name, - apiRenderable.visible - ); -}; - -GameLib.D3.Renderable.prototype = Object.create(GameLib.D3.API.Renderable.prototype); -GameLib.D3.Renderable.prototype.constructor = GameLib.D3.Renderable; - -GameLib.D3.Renderable.prototype.toApiComponent = function() { - - var apiRenderable = new GameLib.D3.API.Renderable( - this.id, - this.name, - this.visible - ); - - return apiRenderable; -}; - -GameLib.D3.Renderable.FromObjectComponent = function(graphics, objectComponent) { - - var apiRenderable = new GameLib.D3.API.Renderable( - objectComponent.id, - objectComponent.name, - objectComponent.visible - ); - - return new GameLib.D3.Renderable( - graphics, - this, - apiRenderable - ); -}; \ No newline at end of file diff --git a/src/game-lib-d3-renderer.js b/src/game-lib-d3-renderer.js index f4272bc..1140a8d 100644 --- a/src/game-lib-d3-renderer.js +++ b/src/game-lib-d3-renderer.js @@ -1,24 +1,17 @@ /** * Renders a scene with a camera * @param graphics GameLib.D3.Graphics - * @param parentObject * @param apiRenderer GameLib.D3.API.Renderer * @constructor */ -GameLib.D3.Renderer = function RuntimeRenderer( +GameLib.D3.Renderer = function ( graphics, - parentObject, apiRenderer ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; - GameLib.D3.API.Renderer.call( this, apiRenderer.id, @@ -85,17 +78,7 @@ GameLib.D3.Renderer.prototype.toApiComponent = function() { GameLib.D3.Renderer.FromObjectComponent = function(graphics, objectComponent) { - var apiRenderer = new GameLib.D3.API.Renderer( - objectComponent.id, - objectComponent.name, - objectComponent.scene, - objectComponent.camera, - objectComponent.autoClear, - objectComponent.localClipping, - objectComponent.width, - objectComponent.height, - objectComponent.parentEntity - ); + var apiRenderer = GameLib.D3.API.Renderer.FromObjectComponent(objectComponent); return new GameLib.D3.Renderer( graphics, @@ -104,6 +87,6 @@ GameLib.D3.Renderer.FromObjectComponent = function(graphics, objectComponent) { ); }; -GameLib.D3.Renderer.prototype.render = function(deltaTime) { +GameLib.D3.Renderer.prototype.render = function() { this.instance.render(this.scene.instance, this.camera.instance); }; \ No newline at end of file diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index da2d137..fc66535 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -18,6 +18,8 @@ GameLib.D3.Scene = function Scene( this.graphics = graphics; this.graphics.isNotThreeThrow(); + this.imageFactory = imageFactory; + GameLib.D3.API.Scene.call( this, apiScene.id, @@ -33,7 +35,8 @@ GameLib.D3.Scene = function Scene( apiScene.entityManager, apiScene.shapes, apiScene.cameras, - apiScene.activeCameraIndex + apiScene.activeCameraIndex, + apiScene.textures ); this.meshes = this.meshes.map( @@ -56,10 +59,12 @@ GameLib.D3.Scene = function Scene( }.bind(this) ); - this.entityManager = new GameLib.EntityManager( - this, - this.entityManager - ); + if (this.entityManager) { + this.entityManager = new GameLib.EntityManager( + this.graphics, + this.entityManager + ); + } this.cameras = this.cameras.map( function(apiCamera) { @@ -70,6 +75,18 @@ GameLib.D3.Scene = function Scene( }.bind(this) ); + this.textures = this.textures.map( + function(apiTexture) { + return new GameLib.D3.Texture( + this.graphics, + apiTexture, + null, + null, + this.imageFactory + ) + }.bind(this) + ); + this.position = new GameLib.Vector3( graphics, this, @@ -88,27 +105,23 @@ GameLib.D3.Scene = function Scene( this.scale ); - this.entityManager = new GameLib.EntityManager( - this, - this.entityManager - ); - this.progressCallback = progressCallback; this.instance = this.createInstance(); - this.imageFactory = imageFactory; - this.interestingProperties = [ "cameras", "meshes", - "lights" + "lights", + "textures" ]; this.idToObject = {}; this.idToObject[this.id] = this; + var material = null; + for (var p = 0; p < this.interestingProperties.length; p++) { property = this.interestingProperties[p]; if (this.hasOwnProperty(property)) { @@ -129,6 +142,17 @@ GameLib.D3.Scene = function Scene( this.idToObject[this.meshes[m].skeleton.id] = this.meshes[m].skeleton; } + + if (this.meshes[m].materials[0]) { + + material = this.meshes[m].materials[0]; + + for (var property in material) { + if (material.hasOwnProperty(property) && material[property] instanceof GameLib.D3.Texture) { + this.idToObject[material[property].id] = material[property]; + } + } + } } this.entityManager.entities.map( @@ -220,6 +244,12 @@ GameLib.D3.Scene.prototype.toApiScene = function() { } ); + var apiTextures = this.textures.map( + function(texture) { + return texture.toApiTexture(); + } + ); + return new GameLib.D3.API.Scene( this.id, this.path, @@ -234,7 +264,8 @@ GameLib.D3.Scene.prototype.toApiScene = function() { apiEntityManager, apiShapes, apiCameras, - this.activeCameraIndex + this.activeCameraIndex, + apiTextures ); }; @@ -261,7 +292,8 @@ GameLib.D3.Scene.FromObjectScene = function( graphics, progressCallback, apiScene, - imageFactory + imageFactory, + computeNormals ); }; diff --git a/src/game-lib-d3-skeleton.js b/src/game-lib-d3-skeleton.js index d6954fc..86edc73 100644 --- a/src/game-lib-d3-skeleton.js +++ b/src/game-lib-d3-skeleton.js @@ -23,7 +23,37 @@ GameLib.D3.Skeleton = function Skeleton( apiSkeleton.boneMatrices, apiSkeleton.boneTexture ); - + + this.bones = this.bones.map( + function(apiBone) { + return new GameLib.D3.Bone( + this.graphics, + apiBone + ) + }.bind(this) + ); + + this.boneInverses = this.boneInverses.map( + function(boneInverse) { + return new GameLib.Matrix4( + this.graphics, + this, + boneInverse + ); + }.bind(this) + ); + + + this.boneMatrices = this.boneMatrices.map( + function(boneMatrices) { + return new GameLib.Matrix4( + this.graphics, + this, + boneMatrices + ); + }.bind(this) + ); + this.instance = this.createInstance(); }; diff --git a/src/game-lib-d3-spline.js b/src/game-lib-d3-spline.js index 71cf719..c7a420c 100644 --- a/src/game-lib-d3-spline.js +++ b/src/game-lib-d3-spline.js @@ -4,7 +4,7 @@ * @param apiSpline GameLib.D3.API.Spline * @constructor */ -GameLib.D3.Spline = function RuntimeSpline( +GameLib.D3.Spline = function ( graphics, apiSpline ) { @@ -61,21 +61,6 @@ GameLib.D3.Spline.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; -/** - * Gets the current point from the spline at the proper value - * @param proper Number (fraction between 0 and 1 indicating position on spline) - * @returns {*} - */ -GameLib.D3.Spline.prototype.getPointAt = function(proper) { - var point = this.instance.getPointAt(proper); - return new GameLib.Vector3( - this.graphics, - this, - new GameLib.API.Vector3(point.x, point.y, point.z), - 0.1 - ); -}; - /** * Converts a GameLib.D3.Spline to GameLib.D3.API.Spline * @returns {GameLib.D3.API.Spline} @@ -106,23 +91,25 @@ GameLib.D3.Spline.FromObjectComponent = function( graphics, objectComponent ) { - var apiSpline = new GameLib.D3.API.Spline( - objectComponent.id, - objectComponent.name, - objectComponent.vertices.map( - function (objectVertex) { - return new GameLib.API.Vector3( - objectVertex.x, - objectVertex.y, - objectVertex.z - ) - } - ), - objectComponent.parentEntity - ); + var apiSpline = GameLib.D3.API.Spline.FromObjectComponent(objectComponent); return new GameLib.D3.Spline( graphics, apiSpline ); }; + +/** + * Gets the current point from the spline at the proper value + * @param proper Number (fraction between 0 and 1 indicating position on spline) + * @returns {*} + */ +GameLib.D3.Spline.prototype.getPointAt = function(proper) { + var point = this.instance.getPointAt(proper); + return new GameLib.Vector3( + this.graphics, + this, + new GameLib.API.Vector3(point.x, point.y, point.z), + 0.1 + ); +}; diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index d2705eb..6b5ed20 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -18,7 +18,7 @@ GameLib.D3.Texture = function Texture( this.graphics = graphics; this.graphics.isNotThreeThrow(); - GameLib.API.Texture.call( + GameLib.D3.API.Texture.call( this, apiTexture.id, apiTexture.typeId, @@ -177,29 +177,31 @@ GameLib.D3.Texture.TEXTURE_TYPE_SPECULAR = 'specular'; */ GameLib.D3.Texture.prototype.createInstance = function(update) { - var instance = null; + // var instance = null; + // + // if (update) { + // instance = this.instance; + // instance.mapping = this.mapping; + // instance.wrapS = this.wrapS; + // instance.wrapT = this.wrapT; + // instance.magFilter = this.magFilter; + // instance.minFilter = this.minFilter; + // instance.anisotropy = this.anisotropy; + // } else { + // + // } - if (update) { - instance = this.instance; - instance.mapping = this.mapping; - instance.wrapS = this.wrapS; - instance.wrapT = this.wrapT; - instance.magFilter = this.magFilter; - instance.minFilter = this.minFilter; - instance.anisotropy = this.anisotropy; - } else { - instance = new this.graphics.instance.Texture( - this.imageInstance, - this.mapping, - this.wrapS, - this.wrapT, - this.magFilter, - this.minFilter, - undefined, //format and textureType is different on different archs - undefined, - this.anisotropy - ); - } + var instance = new THREE.Texture( + this.imageInstance, + this.mapping, + this.wrapS, + this.wrapT, + this.magFilter, + this.minFilter, + undefined, //format and textureType is different on different archs + undefined, + this.anisotropy + ); instance.name = this.name; instance.flipY = this.flipY; @@ -212,6 +214,13 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { instance.unpackAlignment = this.unpackAlignment; instance.premultiplyAlpha = this.premultiplyAlpha; instance.textureType = this.textureType; + + if (this.parentMaterial && + this.parentMaterial.instance && + this.parentMaterialInstanceMapId) { + this.parentMaterial.instance[this.parentMaterialInstanceMapId] = instance; + } + instance.needsUpdate = true; return instance; diff --git a/src/game-lib-d3-triangle-face.js b/src/game-lib-d3-triangle-face.js index 834f61c..142beea 100644 --- a/src/game-lib-d3-triangle-face.js +++ b/src/game-lib-d3-triangle-face.js @@ -34,15 +34,15 @@ GameLib.D3.TriangleFace = function TriangleFace( this.v1uv = v1uv; this.v2uv = v2uv; if (!color) { - color = new GameLib.D3.API.Color(0xff, 0xff, 0xff, 0xff); + color = new GameLib.API.Color(0xff, 0xff, 0xff, 0xff); } this.color = color; if (!vertexColors) { vertexColors = [ - new GameLib.D3.API.Color(0xff, 0xff, 0xff, 0xff), - new GameLib.D3.API.Color(0xff, 0xff, 0xff, 0xff), - new GameLib.D3.API.Color(0xff, 0xff, 0xff, 0xff) + new GameLib.API.Color(0xff, 0xff, 0xff, 0xff), + new GameLib.API.Color(0xff, 0xff, 0xff, 0xff), + new GameLib.API.Color(0xff, 0xff, 0xff, 0xff) ]; } this.vertexColors = vertexColors; diff --git a/src/game-lib-d3-vertex.js b/src/game-lib-d3-vertex.js index 2fbdb50..821a2a8 100644 --- a/src/game-lib-d3-vertex.js +++ b/src/game-lib-d3-vertex.js @@ -23,42 +23,19 @@ GameLib.D3.Vertex = function Vertex( this.position ); - //TODO: GameLib.D3.BoneWeight implementation - - this.instance = this.createInstance(); + this.boneWeights = this.boneWeights.map( + function(apiBoneWeight) { + return new GameLib.D3.BoneWeight( + this.graphics, + apiBoneWeight + ) + }.bind(this) + ) }; GameLib.D3.Vertex.prototype = Object.create(GameLib.D3.API.Vertex.prototype); GameLib.D3.Vertex.prototype.constructor = GameLib.D3.Vertex; -/** - * Creates an instance vertex - * @param update boolean - */ -GameLib.D3.Vertex.prototype.createInstance = function(update) { - - var instance = null; - - if (update) { - instance = this.instance; - } else { - instance = new THREE.Vector3(); - } - - instance.x = this.position.x; - instance.y = this.position.y; - instance.z = this.position.z; - - return instance; -}; - -/** - * Updates the instance - */ -GameLib.D3.Vertex.prototype.updateInstance = function() { - this.instance = this.createInstance(true); -}; - /** * Converts a GameLib.D3.Vertex to GameLib.D3.API.Vertex * @returns {GameLib.D3.API.Vertex} @@ -67,7 +44,9 @@ GameLib.D3.Vertex.prototype.toApiVertex = function() { return new GameLib.D3.API.Vertex( this.position.toApiVector(), - this.boneWeights + this.boneWeights.map(function(boneWeight){ + return boneWeight.toApiBoneWeight(); + }) ); }; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 9673719..576f733 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -1,18 +1,16 @@ /** * GameLib.EntityManager - * @param parentObject + * @param graphics GameLib.D3.Graphics * @param apiEntityManager GameLib.API.EntityManager * @constructor */ GameLib.EntityManager = function( - parentObject, + graphics, apiEntityManager ) { - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; + this.graphics = graphics; + this.graphics.isNotThreeThrow(); GameLib.API.EntityManager.call( this, @@ -22,11 +20,10 @@ GameLib.EntityManager = function( this.entities = this.entities.map( function(apiEntity) { return new GameLib.Entity( - this, - this, + this.graphics, apiEntity ) - } + }.bind(this) ); this.instance = this.createInstance(); @@ -54,7 +51,7 @@ GameLib.EntityManager.prototype.createEntity = function(name) { var apiEntity = new GameLib.API.Entity(null, name); - var entity = new GameLib.Entity(this, this, apiEntity); + var entity = new GameLib.Entity(this.graphics, apiEntity); this.entities.push(entity); @@ -167,25 +164,17 @@ GameLib.EntityManager.prototype.toApiEntityManager = function() { }; /** - * + * Returns an EntityManager from an Object entity manager * @param graphics - * @param objectEntities Object + * @param objectEntityManager Object * @constructor */ -GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntities) { +GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntityManager) { - var apiEntities = objectEntities.entities.map( - function (objectEntity) { - return GameLib.API.Entity.FromObjectEntity(objectEntity); - } - ); - - var apiEntityManager = new GameLib.API.EntityManager( - apiEntities - ); + var apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectEntityManager); var entityManager = new GameLib.EntityManager( - this, + graphics, apiEntityManager ); diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index 789a2ad..74fd19b 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -1,28 +1,16 @@ /** - * Entity Runtime - * @constructor - */ -/** - * - * @param entityManager GameLib.D3.EntityManager - * @param parentObject + * Runtime Entity + * @param graphics GameLib.D3.Graphics * @param apiEntity GameLib.D3.API.Entity * @constructor */ -GameLib.Entity = function RuntimeEntity( - entityManager, - parentObject, +GameLib.Entity = function ( + graphics, apiEntity ) { - if (GameLib.Utils.UndefinedOrNull(entityManager)) { - throw new Error('You cannot create entities without an entity manager') - } - this.entityManager = entityManager; - if (GameLib.Utils.UndefinedOrNull(parentObject)) { - parentObject = null; - } - this.parentObject = parentObject; + this.graphics = graphics; + this.graphics.isNotThreeThrow(); GameLib.API.Entity.call( this, @@ -32,36 +20,35 @@ GameLib.Entity = function RuntimeEntity( ); this.components = this.components.map( - function (component) { - if (component instanceof Object) { - component.constructor.FromObjectComponent(entityManager.graphics, component); - // - // if (component.componentType === GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) { - // return GameLib.D3.PathFollowing.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_RENDERABLE) { - // return GameLib.D3.Renderable.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_RENDERER) { - // return GameLib.D3.Renderer.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_EDITOR_INPUT) { - // return GameLib.D3.Input.Editor.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_LOOK_AT) { - // return GameLib.D3.LookAt.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_FOLLOW) { - // return GameLib.D3.Follow.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_MESH) { - // return GameLib.D3.Mesh.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_SPLINE) { - // return GameLib.D3.Spline.FromObjectComponent(entityManager.graphics, component); - // } else if (component.componentType === GameLib.Component.COMPONENT_INPUT_DRIVE) { - // return GameLib.D3.Input.Drive.FromObjectComponent(entityManager.graphics, component); - // }else { - // console.warn('no component was associated with this object'); - // throw new Error('no component was associated with this object'); - // } - } else { - return component; + function (apiComponent) { + + if (apiComponent instanceof GameLib.D3.API.PathFollowing) { + return new GameLib.D3.PathFollowing(this.graphics, apiComponent); } + + if (apiComponent instanceof GameLib.D3.API.Renderer) { + return new GameLib.D3.Renderer(this.graphics, apiComponent); + } + + if (apiComponent instanceof GameLib.D3.API.LookAt) { + return new GameLib.D3.LookAt(this.graphics, apiComponent); + } + + if (apiComponent instanceof GameLib.D3.API.Follow) { + return new GameLib.D3.Follow(this.graphics, apiComponent); + } + + if (apiComponent instanceof GameLib.D3.API.Input.Drive) { + return new GameLib.D3.Input.Drive(this.graphics, apiComponent); + } + + if (apiComponent instanceof GameLib.D3.API.Spline) { + return new GameLib.D3.Spline(this.graphics, apiComponent); + } + + return apiComponent; + }.bind(this) ); @@ -138,17 +125,16 @@ GameLib.Entity.prototype.toApiEntity = function() { /** * - * @param entityManager GameLib.EntityManager + * @param graphics GameLib.D3.Graphics * @param objectEntity Object * @constructor */ -GameLib.Entity.FromObjectEntity = function(entityManager, objectEntity) { +GameLib.Entity.FromObjectEntity = function(graphics, objectEntity) { var apiEntity = GameLib.API.Entity.FromObjectEntity(objectEntity); return new GameLib.Entity( - entityManager, - this, + graphics, apiEntity ); }; diff --git a/src/game-lib-quaternion.js b/src/game-lib-quaternion.js index 74af21e..4708b03 100644 --- a/src/game-lib-quaternion.js +++ b/src/game-lib-quaternion.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.Quaternion = function RuntimeQuaternion( +GameLib.Quaternion = function ( graphics, parentObject, apiQuaternion, diff --git a/src/game-lib-vector2.js b/src/game-lib-vector2.js index 81392fa..87ed63b 100644 --- a/src/game-lib-vector2.js +++ b/src/game-lib-vector2.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.Vector2 = function RuntimeVector2(graphics, parentObject, apiVector2, grain) { +GameLib.Vector2 = function (graphics, parentObject, apiVector2, grain) { for (var property in apiVector2) { if (apiVector2.hasOwnProperty(property)) { diff --git a/src/game-lib-vector3.js b/src/game-lib-vector3.js index 89129ba..5f688e4 100644 --- a/src/game-lib-vector3.js +++ b/src/game-lib-vector3.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.Vector3 = function RuntimeVector3(graphics, parentObject, apiVector3, grain) { +GameLib.Vector3 = function (graphics, parentObject, apiVector3, grain) { for (var property in apiVector3) { if (apiVector3.hasOwnProperty(property)) { diff --git a/src/game-lib-vector4.js b/src/game-lib-vector4.js index be7e4ce..4a3f4d1 100644 --- a/src/game-lib-vector4.js +++ b/src/game-lib-vector4.js @@ -6,7 +6,7 @@ * @param grain Number * @constructor */ -GameLib.Vector4 = function RuntimeVector4(graphics, parentObject, apiVector4, grain) { +GameLib.Vector4 = function (graphics, parentObject, apiVector4, grain) { GameLib.API.Vector4.call( this,