diff --git a/src/game-lib-api-quaternion-a.js b/src/game-lib-api-quaternion-a.js index 939742d..8d4a853 100644 --- a/src/game-lib-api-quaternion-a.js +++ b/src/game-lib-api-quaternion-a.js @@ -202,16 +202,18 @@ GameLib.API.Quaternion.prototype.slerp = function (quaternion, t) { * @constructor */ GameLib.API.Quaternion.FromObjectQuaternion = function (objectQuaternion) { + + var apiAxis = null; + if (objectQuaternion.axis) { + apiAxis = GameLib.API.Vector3.FromObjectVector(objectQuaternion.axis); + } + return new GameLib.API.Quaternion( objectQuaternion.x, objectQuaternion.y, objectQuaternion.z, objectQuaternion.w, - new GameLib.API.Vector3( - objectQuaternion.axis.x, - objectQuaternion.axis.y, - objectQuaternion.axis.z - ), + apiAxis, objectQuaternion.angle ) }; \ No newline at end of file diff --git a/src/game-lib-d3-api-material.js b/src/game-lib-d3-api-material.js index 967b15a..5008e25 100644 --- a/src/game-lib-d3-api-material.js +++ b/src/game-lib-d3-api-material.js @@ -528,51 +528,51 @@ GameLib.D3.API.Material.FromObjectMaterial = function(objectMaterial) { var apiSpecularMap = null; if (objectMaterial.alphaMap) { - apiAlphaMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.alphaMap); + apiAlphaMap = objectMaterial.alphaMap; } if (objectMaterial.aoMap) { - apiAoMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.aoMap); + apiAoMap = objectMaterial.aoMap; } if (objectMaterial.bumpMap) { - apiBumpMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.bumpMap); + apiBumpMap = objectMaterial.bumpMap; } if (objectMaterial.diffuseMap) { - apiDiffuseMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.diffuseMap); + apiDiffuseMap = objectMaterial.diffuseMap; } if (objectMaterial.displacementMap) { - apiDisplacementMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.displacementMap); + apiDisplacementMap = objectMaterial.displacementMap; } if (objectMaterial.emissiveMap) { - apiEmissiveMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.emissiveMap); + apiEmissiveMap = objectMaterial.emissiveMap; } if (objectMaterial.environmentMap) { - apiEnvironmentMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.environmentMap); + apiEnvironmentMap = objectMaterial.environmentMap; } if (objectMaterial.lightMap) { - apiLightMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.lightMap); + apiLightMap = objectMaterial.lightMap; } if (objectMaterial.metalnessMap) { - apiMetalnessMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.metalnessMap); + apiMetalnessMap = objectMaterial.metalnessMap; } if (objectMaterial.normalMap) { - apiNormalMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.normalMap); + apiNormalMap = objectMaterial.normalMap; } if (objectMaterial.roughnessMap) { - apiRoughnessMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.roughnessMap); + apiRoughnessMap = objectMaterial.roughnessMap; } if (objectMaterial.specularMap) { - apiSpecularMap = GameLib.D3.API.Texture.FromObjectTexture(objectMaterial.specularMap); + apiSpecularMap = objectMaterial.specularMap; } return new GameLib.D3.API.Material( diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index 514750d..2fdd1bf 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -53,7 +53,8 @@ GameLib.D3.API.Mesh = function( GameLib.Component.COMPONENT_MESH, { 'parentMesh' : GameLib.D3.Mesh, - 'parentScene' : GameLib.D3.Scene + 'parentScene' : GameLib.D3.Scene, + 'materials' : [GameLib.D3.Material] }, null, parentEntity @@ -176,11 +177,51 @@ GameLib.D3.API.Mesh.prototype.constructor = GameLib.D3.API.Mesh; GameLib.D3.API.Mesh.FromObjectMesh = function (objectMesh){ var apiSkeleton = null; - + var apiPosition = null; + var apiQuaternion = null; + var apiScale = null; + var apiLocalPosition = null; + var apiLocalRotation = null; + var apiLocalScale = null; + var apiUp = null; + var apiModelMatrix = null; + if (objectMesh.skeleton) { apiSkeleton = GameLib.D3.API.Skeleton.FromObjectSkeleton(objectMesh.skeleton); } + if (objectMesh.position) { + apiPosition = GameLib.API.Vector3.FromObjectVector(objectMesh.position); + } + + if (objectMesh.quaternion) { + apiQuaternion = GameLib.API.Quaternion.FromObjectQuaternion(objectMesh.quaternion); + } + + if (objectMesh.scale) { + apiScale = GameLib.API.Vector3.FromObjectVector(objectMesh.scale); + } + + if (objectMesh.localPosition) { + apiLocalPosition = GameLib.API.Vector3.FromObjectVector(objectMesh.localPosition); + } + + if (objectMesh.localRotation) { + apiLocalRotation = GameLib.API.Vector3.FromObjectVector(objectMesh.localRotation); + } + + if (objectMesh.localScale) { + apiLocalScale = GameLib.API.Vector3.FromObjectVector(objectMesh.localScale); + } + + if (objectMesh.up) { + apiUp = GameLib.API.Vector3.FromObjectVector(objectMesh.up); + } + + if (objectMesh.modelMatrix) { + apiModelMatrix = GameLib.API.Matrix4.FromObjectMatrix(objectMesh.modelMatrix); + } + return new GameLib.D3.API.Mesh( objectMesh.id, objectMesh.meshType, @@ -192,24 +233,20 @@ GameLib.D3.API.Mesh.FromObjectMesh = function (objectMesh){ ), objectMesh.faces, objectMesh.faceVertexUvs, - objectMesh.materials.map( - function (objectMaterial) { - return GameLib.D3.API.Material.FromObjectMaterial(objectMaterial); - } - ), + objectMesh.materials, objectMesh.parentMesh, objectMesh.parentScene, apiSkeleton, objectMesh.skinIndices, objectMesh.skinWeights, - GameLib.API.Vector3.FromObjectVector(objectMesh.position), - GameLib.API.Quaternion.FromObjectQuaternion(objectMesh.quaternion), - GameLib.API.Vector3.FromObjectVector(objectMesh.scale), - GameLib.API.Vector3.FromObjectVector(objectMesh.localPosition), - GameLib.API.Vector3.FromObjectVector(objectMesh.localRotation), - GameLib.API.Vector3.FromObjectVector(objectMesh.localScale), - GameLib.API.Vector3.FromObjectVector(objectMesh.up), - GameLib.API.Matrix4.FromObjectMatrix(objectMesh.modelMatrix), + apiPosition, + apiQuaternion, + apiScale, + apiLocalPosition, + apiLocalRotation, + apiLocalScale, + apiUp, + apiModelMatrix, objectMesh.parentEntity, objectMesh.renderOrder ); diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index 7cab28f..423cd8a 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -32,7 +32,8 @@ GameLib.D3.API.Scene = function( shapes, cameras, activeCameraIndex, - textures + textures, + materials ) { if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -108,6 +109,11 @@ GameLib.D3.API.Scene = function( textures = []; } this.textures = textures; + + if (GameLib.Utils.UndefinedOrNull(materials)) { + materials = []; + } + this.materials = materials; }; /** @@ -118,8 +124,15 @@ GameLib.D3.API.Scene = function( GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { var apiEntityManager = null; + var apiPosition = null; + var apiQuaternion = null; + var apiScale = null; - var apiTextures = null; + var apiTextures = []; + var apiMaterials = []; + var apiMeshes = []; + var apiLights = []; + var apiCameras = []; if (objectScene.entityManager) { apiEntityManager = GameLib.API.EntityManager.FromObjectEntityManager(objectScene.entityManager); @@ -133,34 +146,67 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { ) } + if (objectScene.materials) { + apiMaterials = objectScene.materials.map( + function(objectMaterial) { + return GameLib.D3.API.Material.FromObjectMaterial(objectMaterial) + } + ) + } + + if (objectScene.meshes) { + apiMeshes = objectScene.meshes.map( + function(objectMesh) { + return GameLib.D3.API.Mesh.FromObjectMesh(objectMesh); + } + ) + } + + if (objectScene.lights) { + apiLights = objectScene.lights.map( + function(objectLight) { + return GameLib.D3.API.Light.FromObjectLight(objectLight); + } + ) + } + + if (objectScene.cameras) { + apiCameras = objectScene.cameras.map( + function(objectCamera) { + return GameLib.D3.API.Camera.FromObjectCamera(objectCamera); + } + ) + } + + if (objectScene.position) { + apiPosition = GameLib.API.Vector3.FromObjectVector(objectScene.position); + } + + if (objectScene.quaternion) { + apiQuaternion = GameLib.API.Quaternion.FromObjectQuaternion(objectScene.quaternion); + } + + if (objectScene.scale) { + apiScale = GameLib.API.Vector3.FromObjectVector(objectScene.scale); + } + return new GameLib.D3.API.Scene( objectScene.id, objectScene.path, objectScene.name, - objectScene.meshes.map( - function (objectMesh) { - return GameLib.D3.API.Mesh.FromObjectMesh(objectMesh) - } - ), - GameLib.API.Vector3.FromObjectVector(objectScene.position), - GameLib.API.Quaternion.FromObjectQuaternion(objectScene.quaternion), - GameLib.API.Vector3.FromObjectVector(objectScene.scale), + apiMeshes, + apiPosition, + apiQuaternion, + apiScale, objectScene.parentSceneId, - objectScene.lights.map( - function (objectLight) { - return GameLib.D3.API.Light.FromObjectLight(objectLight) - } - ), + apiLights, [], //TODO : implement worlds here apiEntityManager, [], //TODO : implement shapes here - objectScene.cameras.map( - function (objectCamera) { - return GameLib.D3.API.Camera.FromObjectCamera(objectCamera); - } - ), + apiCameras, objectScene.activeCameraIndex, - apiTextures + apiTextures, + apiMaterials ); }; diff --git a/src/game-lib-d3-light.js b/src/game-lib-d3-light.js index 7e3956b..8ffb536 100644 --- a/src/game-lib-d3-light.js +++ b/src/game-lib-d3-light.js @@ -83,6 +83,7 @@ GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4; */ GameLib.D3.Light.prototype.createInstance = function(update) { + var instance = null; if (update) { @@ -91,22 +92,31 @@ GameLib.D3.Light.prototype.createInstance = function(update) { if (!instance) { - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) { - instance = new this.graphics.instance.AmbientLight( + if ( + this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT || + this.lightType == 'AmbientLight' + ) { + instance = new THREE.AmbientLight( this.color.instance, this.intensity ); } - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) { - instance = new this.graphics.instance.DirectionalLight( + if ( + this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL || + this.lightType == 'DirectionalLight' + ) { + instance = new THREE.DirectionalLight( this.color.instance, this.intensity ); } - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) { - instance = new this.graphics.instance.PointLight( + if ( + this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT || + this.lightType == 'PointLight' + ) { + instance = new THREE.PointLight( this.color.instance, this.intensity ); @@ -114,8 +124,11 @@ GameLib.D3.Light.prototype.createInstance = function(update) { instance.decay = this.decay; } - if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) { - instance = new this.graphics.instance.SpotLight( + if ( + this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT || + this.lightType == 'SpotLight' + ) { + instance = new THREE.SpotLight( this.color.instance, this.intensity ); @@ -143,11 +156,11 @@ GameLib.D3.Light.prototype.createInstance = function(update) { instance.scale.y = this.scale.y; instance.scale.z = this.scale.z; - if (instance.target) { - instance.target.position.x = this.targetPosition.x; - instance.target.position.y = this.targetPosition.y; - instance.target.position.z = this.targetPosition.z; - } + if (instance.target) { + instance.target.position.x = this.targetPosition.x; + instance.target.position.y = this.targetPosition.y; + instance.target.position.z = this.targetPosition.z; + } instance.quaternion.x = this.quaternion.x; instance.quaternion.y = this.quaternion.y; diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index 01ad7ad..8962cb3 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -108,123 +108,159 @@ GameLib.D3.Material = function Material( ); if (this.alphaMap) { - this.alphaMap = new GameLib.D3.Texture( - this.graphics, - this.alphaMap, - this, - 'alphaMap', - imageFactory - ); + if (this.alphaMap instanceof GameLib.D3.API.Texture) { + this.alphaMap = new GameLib.D3.Texture( + this.graphics, + this.alphaMap, + this.imageFactory + ); + } else { + console.warn('this.alphaMap is not an instance of API.Texture'); + // throw new Error('this.alphaMap is not an instance of API.Texture'); + } } if (this.aoMap) { - this.aoMap = new GameLib.D3.Texture( - this.graphics, - this.aoMap, - this, - 'aoMap', - imageFactory - ); + if (this.aoMap instanceof GameLib.D3.API.Texture) { + this.aoMap = new GameLib.D3.Texture( + this.graphics, + this.aoMap, + this.imageFactory + ); + } else { + console.warn('this.aoMap is not an instance of API.Texture'); + // throw new Error('this.aoMap is not an instance of API.Texture'); + } } if (this.bumpMap) { - this.bumpMap = new GameLib.D3.Texture( - this.graphics, - this.bumpMap, - this, - 'bumpMap', - imageFactory - ); + if (this.bumpMap instanceof GameLib.D3.API.Texture) { + this.bumpMap = new GameLib.D3.Texture( + this.graphics, + this.bumpMap, + this.imageFactory + ); + } else { + console.warn('this.bumpMap is not an instance of API.Texture'); + // throw new Error('this.bumpMap is not an instance of API.Texture'); + } } if (this.diffuseMap) { - this.diffuseMap = new GameLib.D3.Texture( - this.graphics, - this.diffuseMap, - this, - 'map', - imageFactory - ); + if (this.diffuseMap instanceof GameLib.D3.API.Texture) { + this.diffuseMap = new GameLib.D3.Texture( + this.graphics, + this.diffuseMap, + this.imageFactory + ); + } else { + console.warn('this.diffuseMap is not an instance of API.Texture'); + // throw new Error('this.diffuseMap is not an instance of API.Texture'); + } } if (this.displacementMap) { - this.displacementMap = new GameLib.D3.Texture( - this.graphics, - this.displacementMap, - this, - 'displacementMap', - imageFactory - ); + if (this.displacementMap instanceof GameLib.D3.API.Texture) { + this.displacementMap = new GameLib.D3.Texture( + this.graphics, + this.displacementMap, + this.imageFactory + ); + } else { + console.warn('this.displacementMap is not an instance of API.Texture'); + // throw new Error('this.displacementMap is not an instance of API.Texture'); + } } if (this.emissiveMap) { - this.emissiveMap = new GameLib.D3.Texture( - this.graphics, - this.emissiveMap, - this, - 'emissiveMap', - imageFactory - ); + if (this.emissiveMap instanceof GameLib.D3.API.Texture) { + this.emissiveMap = new GameLib.D3.Texture( + this.graphics, + this.emissiveMap, + this.imageFactory + ); + } else { + console.warn('this.emissiveMap is not an instance of API.Texture'); + // throw new Error('this.emissiveMap is not an instance of API.Texture'); + } } if (this.environmentMap) { - this.environmentMap = new GameLib.D3.Texture( - this.graphics, - this.environmentMap, - this, - 'envMap', - imageFactory - ); + if (this.environmentMap instanceof GameLib.D3.API.Texture) { + this.environmentMap = new GameLib.D3.Texture( + this.graphics, + this.environmentMap, + this.imageFactory + ); + } else { + console.warn('this.environmentMap is not an instance of API.Texture'); + // throw new Error('this.environmentMap is not an instance of API.Texture'); + } } if (this.lightMap) { - this.lightMap = new GameLib.D3.Texture( - this.graphics, - this.lightMap, - this, - 'lightMap', - imageFactory - ); + if (this.lightMap instanceof GameLib.D3.API.Texture) { + this.lightMap = new GameLib.D3.Texture( + this.graphics, + this.lightMap, + this.imageFactory + ); + } else { + console.warn('this.lightMap is not an instance of API.Texture'); + // throw new Error('this.lightMap is not an instance of API.Texture'); + } } if (this.metalnessMap) { - this.metalnessMap = new GameLib.D3.Texture( - this.graphics, - this.metalnessMap, - this, - 'metalnessMap', - imageFactory - ); + if (this.metalnessMap instanceof GameLib.D3.API.Texture) { + this.metalnessMap = new GameLib.D3.Texture( + this.graphics, + this.metalnessMap, + this.imageFactory + ); + } else { + console.warn('this.metalnessMap is not an instance of API.Texture'); + // throw new Error('this.metalnessMap is not an instance of API.Texture'); + } } if (this.normalMap) { - this.normalMap = new GameLib.D3.Texture( - this.graphics, - this.normalMap, - this, - 'normalMap', - imageFactory - ); + if (this.normalMap instanceof GameLib.D3.API.Texture) { + this.normalMap = new GameLib.D3.Texture( + this.graphics, + this.normalMap, + this.imageFactory + ); + } else { + console.warn('this.normalMap is not an instance of API.Texture'); + // throw new Error('this.normalMap is not an instance of API.Texture'); + } } if (this.roughnessMap) { - this.roughnessMap = new GameLib.D3.Texture( - this.graphics, - this.roughnessMap, - this, - 'roughnessMap', - imageFactory - ); + if (this.roughnessMap instanceof GameLib.D3.API.Texture) { + this.roughnessMap = new GameLib.D3.Texture( + this.graphics, + this.roughnessMap, + this.imageFactory + ); + } else { + console.warn('this.roughnessMap is not an instance of API.Texture'); + // throw new Error('this.roughnessMap is not an instance of API.Texture'); + } } if (this.specularMap) { - this.specularMap = new GameLib.D3.Texture( - this.graphics, - this.specularMap, - this, - 'specularMap', - imageFactory - ); + if (this.specularMap instanceof GameLib.D3.API.Texture) { + this.specularMap = new GameLib.D3.Texture( + this.graphics, + this.specularMap, + this.imageFactory + ); + } else { + console.warn('this.specularMap is not an instance of API.Texture'); + // throw new Error('this.specularMap is not an instance of API.Texture'); + } } this.instance = this.createInstance(); diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index fcf4bb5..c4fe4c7 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -41,15 +41,15 @@ GameLib.D3.Mesh = function ( apiMesh.renderOrder ); - this.materials = this.materials.map( - function (apiMaterial) { - return new GameLib.D3.Material( - this.graphics, - apiMaterial, - imageFactory - ) - }.bind(this) - ); + // this.materials = this.materials.map( + // function (apiMaterial) { + // return new GameLib.D3.Material( + // this.graphics, + // apiMaterial, + // imageFactory + // ) + // }.bind(this) + // ); if (this.skeleton) { this.skeleton = new GameLib.D3.Skeleton( @@ -426,7 +426,7 @@ GameLib.D3.Mesh.prototype.toApiMesh = function() { ), this.faces, this.faceVertexUvs, - this.materials.map(function(material){return material.toApiMaterial()}), + this.materials.map(function(material){return material.id}), GameLib.Utils.IdOrNull(this.parentMesh), GameLib.Utils.IdOrNull(this.parentScene), apiSkeleton, diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 01c42d9..13f39a4 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -36,7 +36,8 @@ GameLib.D3.Scene = function Scene( apiScene.shapes, apiScene.cameras, apiScene.activeCameraIndex, - apiScene.textures + apiScene.textures, + apiScene.materials ); this.meshes = this.meshes.map( @@ -87,6 +88,16 @@ GameLib.D3.Scene = function Scene( }.bind(this) ); + this.materials = this.materials.map( + function(apiMaterial) { + return new GameLib.D3.Material( + this.graphics, + apiMaterial, + this.imageFactory + ) + }.bind(this) + ); + this.position = new GameLib.Vector3( graphics, this, @@ -113,14 +124,15 @@ GameLib.D3.Scene = function Scene( "cameras", "meshes", "lights", - "textures" + "textures", + "materials" ]; this.idToObject = {}; this.idToObject[this.id] = this; - var material = null; + // var material = null; for (var p = 0; p < this.interestingProperties.length; p++) { property = this.interestingProperties[p]; @@ -144,14 +156,8 @@ GameLib.D3.Scene = function Scene( } 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.meshes[m].materials[0] = this.idToObject[this.meshes[m].materials[0]]; + this.meshes[m].materials[0].diffuseMap = this.idToObject[this.meshes[m].materials[0].diffuseMap]; } } diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index fe195b3..6caf11e 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -10,6 +10,32 @@ GameLib.Utils.Extend = function( } }; +/** + * Returns id of object with the name if it exists in the array, otherwise null + * @param name + * @param array + * @returns {*} + * @constructor + */ +GameLib.Utils.ObjectIdWithNameInArray = function(name, array) { + + return array.reduce( + function(result, object) { + + if (result) { + return result; + } + + if (name == object.name) { + return object.id; + } + + return null; + }, + null + ); +}; + // GameLib.Utils.ObjectFactory = function() { // // var promiseList = {};