From 14da18f2e85fdfbacc41f99ab67f487503505460 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Fri, 9 Jun 2017 16:03:05 +0200 Subject: [PATCH] texture and material loading revamped --- src/game-lib-a-1-event.js | 5 +- src/game-lib-d3-api-material.js | 24 ++-- src/game-lib-d3-api-scene.js | 18 +++ src/game-lib-d3-api-texture.js | 4 +- src/game-lib-d3-image-factory.js | 2 +- src/game-lib-d3-image.js | 46 ++++++- src/game-lib-d3-material.js | 107 ++++++++++++--- src/game-lib-d3-mesh-0.js | 226 +++++++++++++++++-------------- src/game-lib-d3-scene.js | 110 +++++++++------ src/game-lib-d3-texture.js | 93 ++++++++++--- src/game-lib-entity-manager.js | 73 ---------- 11 files changed, 435 insertions(+), 273 deletions(-) diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index e3299fd..584a0a9 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -17,7 +17,10 @@ GameLib.Event.Subscriptions = {}; GameLib.Event.WINDOW_RESIZE = 0x1; GameLib.Event.PARENT_SCENE_CHANGE = 0x2; GameLib.Event.PARENT_ENTITY_CHANGE = 0x3; -GameLib.Event.TEXTURE_LOADED = 0x4; +GameLib.Event.IMAGE_LOADED = 0x5; +GameLib.Event.TEXTURE_LOADED = 0x6; +GameLib.Event.LOAD_IMAGE = 0x7; +GameLib.Event.MATERIAL_LOADED = 0x8; /** * Subscribe to some events diff --git a/src/game-lib-d3-api-material.js b/src/game-lib-d3-api-material.js index 07926c6..7a4d1b9 100644 --- a/src/game-lib-d3-api-material.js +++ b/src/game-lib-d3-api-material.js @@ -539,51 +539,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-scene.js b/src/game-lib-d3-api-scene.js index a67e293..f890ac8 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -10,6 +10,7 @@ * @param lights [GameLib.D3.API.Light] * @param textures [GameLib.D3.API.Texture] * @param materials [GameLib.D3.API.Material] + * @param images * @param activeCamera [GameLib.D3.Camera] * @param parentEntity * @constructor @@ -25,6 +26,7 @@ GameLib.D3.API.Scene = function( lights, textures, materials, + images, activeCamera, parentEntity ) { @@ -36,6 +38,7 @@ GameLib.D3.API.Scene = function( 'lights' : [GameLib.D3.Light], 'textures' : [GameLib.D3.Texture], 'materials' : [GameLib.D3.Material], + 'images' : [GameLib.D3.Image], 'activeCamera' : GameLib.D3.Camera }, false, @@ -92,6 +95,11 @@ GameLib.D3.API.Scene = function( } this.materials = materials; + if (GameLib.Utils.UndefinedOrNull(images)) { + images = []; + } + this.images = images; + if (GameLib.Utils.UndefinedOrNull(activeCamera)) { activeCamera = null; } @@ -113,6 +121,7 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { var apiLights = []; var apiTextures = []; var apiMaterials = []; + var apiImages = []; var apiPosition = new GameLib.API.Vector3(); var apiQuaternion = new GameLib.API.Quaternion(); @@ -152,6 +161,14 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { ) } + if (objectScene.images) { + apiImages = objectScene.images.map( + function(objectImage) { + return GameLib.D3.API.Image.FromObject(objectImage) + } + ) + } + if (objectScene.position) { apiPosition = GameLib.API.Vector3.FromObjectVector(objectScene.position); } @@ -179,6 +196,7 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { apiLights, apiTextures, apiMaterials, + apiImages, apiActiveCamera, objectScene.parentEntity ); diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 4022bf5..d451efb 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -56,7 +56,9 @@ GameLib.D3.API.Texture = function( GameLib.Component.call( this, GameLib.Component.COMPONENT_TEXTURE, - null, + { + 'image' : GameLib.D3.Image + }, null, parentEntity ); diff --git a/src/game-lib-d3-image-factory.js b/src/game-lib-d3-image-factory.js index 69ebc2a..b4fad82 100644 --- a/src/game-lib-d3-image-factory.js +++ b/src/game-lib-d3-image-factory.js @@ -99,7 +99,7 @@ GameLib.D3.ImageFactory.prototype.loadImage = function( function (image) { GameLib.Event.Emit( - GameLib.Event.TEXTURE_LOADED, + GameLib.Event.IMAGE_LOADED, { imagePath : imagePath, imageInstance : image diff --git a/src/game-lib-d3-image.js b/src/game-lib-d3-image.js index 1ca2db3..fdafe19 100644 --- a/src/game-lib-d3-image.js +++ b/src/game-lib-d3-image.js @@ -39,16 +39,54 @@ GameLib.D3.Image.prototype.createInstance = function(update) { if (update) { console.log('why the fuck are you updating an image instance?'); + if (!this.instance) { + console.warn('no image instance'); + return; + } this.instance.id = this.id; this.instance.name = this.name; this.instance.path = this.path; this.instance.contentType = this.contentType; } else { - var instance = new THREE.TextureLoader().load(this.path); - return instance; - } - return instance; + this.subscribe( + GameLib.Event.LOAD_IMAGE, + function(data) { + if ( + data.id === this.id || + data.path === this.path + ) { + var loader = new THREE.ImageLoader(); + + loader.crossOrigin = true; + + loader.path = data.baseUrl; + + loader.load( + this.path, + function ( image ) { + this.instance = image; + this.publish( + GameLib.Event.IMAGE_LOADED, + { + imagePath : this.path, + imageInstance : this.instance + } + ); + }.bind(this), + function ( xhr ) { + console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); + }, + function () { + console.log('An image load error happened'); + } + ); + } + } + ); + + return null; + } }; /** diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index d40ed11..5338f7c 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -21,10 +21,10 @@ GameLib.D3.Material = function( if (apiMaterial instanceof GameLib.D3.Material) { return apiMaterial; } - + GameLib.D3.API.Material.call( this, - apiMaterial.id, + apiMaterial.id, apiMaterial.materialType, apiMaterial.name, apiMaterial.opacity, @@ -225,6 +225,7 @@ GameLib.D3.Material = function( this.instance = this.createInstance(); + this.updateTextures(); }; GameLib.D3.Material.prototype = Object.create(GameLib.D3.API.Material.prototype); @@ -477,62 +478,62 @@ GameLib.D3.Material.prototype.createMeshBasicMaterialInstance = function() { * updates textures */ GameLib.D3.Material.prototype.updateTextures = function() { - if (this.alphaMap) { + if (this.alphaMap && this.alphaMap.instance) { this.instance.alphaMap = this.alphaMap.instance; } else { this.instance.alphaMap = null; } - if (this.aoMap) { + if (this.aoMap && this.aoMap.instance) { this.instance.aoMap = this.aoMap.instance; } else { this.instance.aoMap = null; } - if (this.bumpMap) { + if (this.bumpMap && this.bumpMap.instance) { this.instance.bumpMap = this.bumpMap.instance; } else { this.instance.bumpMap = null; } - if (this.diffuseMap) { + if (this.diffuseMap && this.diffuseMap.instance) { this.instance.map = this.diffuseMap.instance; } else { this.instance.map = null; } - if (this.displacementMap) { + if (this.displacementMap && this.displacementMap.instance) { this.instance.displacementMap = this.displacementMap.instance; } else { this.instance.displacementMap = null; } - if (this.emissiveMap) { + if (this.emissiveMap && this.emissiveMap.instance) { this.instance.emissiveMap = this.emissiveMap.instance; } else { this.instance.emissiveMap = null; } - if (this.environmentMap) { + if (this.environmentMap && this.environmentMap.instance) { this.instance.envMap = this.environmentMap.instance; } else { this.instance.envMap = null; } - if (this.lightMap) { + if (this.lightMap && this.lightMap.instance) { this.instance.lightMap = this.lightMap.instance; } else { this.instance.lightMap = null; } - if (this.metalnessMap) { + if (this.metalnessMap && this.metalnessMap.instance) { this.instance.metalnessMap = this.metalnessMap.instance; } else { this.instance.metalnessMap = null; } - if (this.normalMap) { + if (this.normalMap && this.normalMap.instance) { this.instance.normalMap = this.normalMap.instance; } else { this.instance.normalMap = null; } - if (this.roughnessMap) { + if (this.roughnessMap && this.roughnessMap.instance) { this.instance.roughnessMap = this.roughnessMap.instance; } else { this.instance.roughnessMap = null; } - if (this.specularMap) { + if (this.specularMap && this.specularMap.instance) { this.instance.specularMap = this.specularMap.instance; } else { this.instance.specularMap = null; @@ -581,7 +582,6 @@ GameLib.D3.Material.prototype.updateStandardMaterialInstance = function() { this.instance.skinning = this.skinning; this.instance.morphTargets = this.morphTargets; this.instance.morphNormals = this.morphNormals; - this.updateTextures(); }; GameLib.D3.Material.prototype.updatePointsMaterialInstance = function() { @@ -609,7 +609,6 @@ GameLib.D3.Material.prototype.updatePointsMaterialInstance = function() { this.instance.sizeAttenuation = this.pointSizeAttenuation; this.instance.vertexColors = GameLib.D3.Material.TYPE_NO_COLORS; this.instance.fog = this.fog; - this.updateTextures(); }; GameLib.D3.Material.prototype.updatePhongMaterialInstance = function() { @@ -654,7 +653,6 @@ GameLib.D3.Material.prototype.updatePhongMaterialInstance = function() { this.instance.skinning = this.skinning; this.instance.morphTargets = this.morphTargets; this.instance.morphNormals = this.morphNormals; - this.updateTextures(); }; GameLib.D3.Material.prototype.updateMeshBasicMaterialInstance = function() { @@ -680,7 +678,6 @@ GameLib.D3.Material.prototype.updateMeshBasicMaterialInstance = function() { this.instance.color = this.color.instance; this.instance.vertexColors = GameLib.D3.Material.TYPE_NO_COLORS; this.instance.fog = this.fog; - this.updateTextures(); }; /** @@ -719,6 +716,8 @@ GameLib.D3.Material.prototype.createInstance = function(update) { console.warn('not yet implemented (material type = ' + this.materialType + ')'); } + this.updateTextures(); + this.instance.needsUpdate = true; } else { @@ -747,6 +746,78 @@ GameLib.D3.Material.prototype.createInstance = function(update) { instance.needsUpdate = true; + this.subscribe( + GameLib.Event.TEXTURE_LOADED, + function (data) { + + var modified = false; + + if (this.alphaMap === data.texture) { + this.instance.alphaMap = data.texture.instance; + modified = true; + } + if (this.aoMap === data.texture) { + this.instance.aoMap = data.texture.instance; + modified = true; + } + if (this.bumpMap === data.texture) { + this.instance.bumpMap = data.texture.instance; + modified = true; + } + if (this.diffuseMap === data.texture) { + this.instance.map = data.texture.instance; + modified = true; + } + if (this.displacementMap === data.texture) { + this.instance.displacementMap = data.texture.instance; + modified = true; + } + if (this.emissiveMap === data.texture) { + this.instance.emissiveMap = data.texture.instance; + modified = true; + } + if (this.environmentMap === data.texture) { + this.instance.envMap = data.texture.instance; + modified = true; + } + if (this.lightMap === data.texture) { + this.instance.lightMap = data.texture.instance; + modified = true; + } + if (this.metalnessMap === data.texture) { + this.instance.metalnessMap = data.texture.instance; + modified = true; + } + if (this.normalMap === data.texture) { + this.instance.normalMap = data.texture.instance; + modified = true; + } + if (this.roughnessMap === data.texture) { + this.instance.roughnessMap = data.texture.instance; + modified = true; + } + if (this.specularMap === data.texture) { + this.instance.specularMap = data.texture.instance; + modified = true; + } + + if (modified) { + + data.texture.instance.needsUpdate = true; + + this.publish( + GameLib.Event.MATERIAL_LOADED, + { + material: this + } + ); + + this.instance.needsUpdate = true; + } + + } + ); + return instance; } diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index d32b592..fcb360d 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -45,27 +45,18 @@ GameLib.D3.Mesh = function ( apiMesh.renderOrder ); - apiMesh.materials = apiMesh.materials.map(function(apiMaterial) { - if (apiMaterial instanceof GameLib.D3.API.Material) { - /** - * Do Nothing - */ - if (apiMaterial instanceof GameLib.D3.Material) { - return apiMaterial; - } else { + this.materials = this.materials.map( + function(material) { + if (material instanceof GameLib.D3.API.Material) { return new GameLib.D3.Material( this.graphics, - apiMaterial - ); + material + ) + } else { + return material; } - } else { - /** - * Do Nothing - */ - } - }.bind(this)); - - this.materials = apiMesh.materials; + }.bind(this) + ); if (this.skeleton) { this.skeleton = new GameLib.D3.Skeleton( @@ -133,7 +124,7 @@ GameLib.D3.Mesh = function ( this.buildIdToObject(); - this.instance = this.createInstance(false); + this.instance = this.createInstance(); this.instance.geometry.computeBoundingBox(); @@ -288,14 +279,58 @@ GameLib.D3.Mesh.prototype.customGeometry = function(){ */ GameLib.D3.Mesh.prototype.createInstance = function(update) { - var instance = null; - if (update) { - instance = this.instance; - } - if (!update) { + if (this.parentMesh && this.parentMesh.loaded) { + this.instance.parent = this.parentMesh.instance; + + this.instance.position.x = this.localPosition.x; + this.instance.position.y = this.localPosition.y; + this.instance.position.z = this.localPosition.z; + + this.instance.rotation.x = this.localRotation.x; + this.instance.rotation.y = this.localRotation.y; + this.instance.rotation.z = this.localRotation.z; + + this.instance.scale.x = this.localScale.x; + this.instance.scale.y = this.localScale.y; + this.instance.scale.z = this.localScale.z; + + } else { + this.instance.quaternion.x = this.quaternion.x; + this.instance.quaternion.y = this.quaternion.y; + this.instance.quaternion.z = this.quaternion.z; + this.instance.quaternion.w = this.quaternion.w; + + this.instance.position.x = this.position.x + this.localPosition.x; + this.instance.position.y = this.position.y + this.localPosition.y; + this.instance.position.z = this.position.z + this.localPosition.z; + + this.instance.scale.x = this.scale.x * this.localScale.x; + this.instance.scale.y = this.scale.y * this.localScale.y; + this.instance.scale.z = this.scale.z * this.localScale.z; + + this.instance.up.x = this.up.x; + this.instance.up.y = this.up.y; + this.instance.up.z = this.up.z; + + this.instance.rotateX(this.localRotation.x); + this.instance.rotateY(this.localRotation.y); + this.instance.rotateZ(this.localRotation.z); + } + + this.instance.name = this.name; + + if (this.materials.length === 1 && this.materials[0].instance) { + this.instance.material = this.materials[0].instance; + } + + this.instance.renderOrder = this.renderOrder; + + } else { + + var instance = null; var geometry = null; if (this instanceof GameLib.D3.Mesh.Sphere) { @@ -309,13 +344,9 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { if (this.meshType === GameLib.D3.Mesh.TYPE_NORMAL) { instance = new THREE.Mesh(geometry); - } - - if (this.meshType === GameLib.D3.Mesh.TYPE_CURVE) { + } else if (this.meshType === GameLib.D3.Mesh.TYPE_CURVE) { instance = new THREE.Points(geometry); - } - - if (this.meshType === GameLib.D3.Mesh.TYPE_SKINNED) { + } else if (this.meshType === GameLib.D3.Mesh.TYPE_SKINNED) { /** * Setup bones (indexes) @@ -350,84 +381,81 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.add(this.skeleton.rootBoneInstance); instance.bind(this.skeleton.instance); - } - - if (instance === null) { + } else { console.log('cannot handle meshes of type ' + this.meshType + ' yet.'); } + + instance.name = this.name; + + if (this.parentMesh && this.parentMesh.loaded) { + + instance.parent = this.parentMesh.instance; + + instance.position.x = this.localPosition.x; + instance.position.y = this.localPosition.y; + instance.position.z = this.localPosition.z; + + instance.rotation.x = this.localRotation.x; + instance.rotation.y = this.localRotation.y; + instance.rotation.z = this.localRotation.z; + + instance.scale.x = this.localScale.x; + instance.scale.y = this.localScale.y; + instance.scale.z = this.localScale.z; + + } else { + instance.quaternion.x = this.quaternion.x; + instance.quaternion.y = this.quaternion.y; + instance.quaternion.z = this.quaternion.z; + instance.quaternion.w = this.quaternion.w; + + instance.position.x = this.position.x + this.localPosition.x; + instance.position.y = this.position.y + this.localPosition.y; + instance.position.z = this.position.z + this.localPosition.z; + + instance.scale.x = this.scale.x * this.localScale.x; + instance.scale.y = this.scale.y * this.localScale.y; + instance.scale.z = this.scale.z * this.localScale.z; + + instance.up.x = this.up.x; + instance.up.y = this.up.y; + instance.up.z = this.up.z; + + instance.rotateX(this.localRotation.x); + instance.rotateY(this.localRotation.y); + instance.rotateZ(this.localRotation.z); + } + + if (this.materials.length === 1 && this.materials[0].instance) { + instance.material = this.materials[0].instance; + } + + instance.renderOrder = this.renderOrder; + + this.subscribe( + GameLib.Event.MATERIAL_LOADED, + function(data) { + + if (this.instance.material === data.material.instance) { + return; + } + + if (this.materials[0] === data.material) { + this.instance.material = data.material.instance; + this.instance.geometry.uvsNeedUpdate = true; + } + } + ); + + return instance; } - - instance.name = this.name; - - instance.gameLibObject = this; - - if (this.parentMesh && this.parentMesh.loaded) { - - instance.parent = this.parentMesh.instance; - - instance.position.x = this.localPosition.x; - instance.position.y = this.localPosition.y; - instance.position.z = this.localPosition.z; - - instance.rotation.x = this.localRotation.x; - instance.rotation.y = this.localRotation.y; - instance.rotation.z = this.localRotation.z; - - instance.scale.x = this.localScale.x; - instance.scale.y = this.localScale.y; - instance.scale.z = this.localScale.z; - - // this.position.x = this.parentMesh.position.x; - // this.position.y = this.parentMesh.position.y; - // this.position.z = this.parentMesh.position.z; - - // var euler = new THREE.Euler(); - // - // var worldRotation = this.parentMesh.instance.getWorldRotation(); - // - // this.instance.setRotationFromEuler(euler); - - // this.quaternion.x = this.parentMesh.quaternion.x; - // this.quaternion.y = this.parentMesh.quaternion.y; - // this.quaternion.z = this.parentMesh.quaternion.z; - // this.quaternion.w = this.parentMesh.quaternion.w; - } else { - instance.quaternion.x = this.quaternion.x; - instance.quaternion.y = this.quaternion.y; - instance.quaternion.z = this.quaternion.z; - instance.quaternion.w = this.quaternion.w; - - instance.position.x = this.position.x + this.localPosition.x; - instance.position.y = this.position.y + this.localPosition.y; - instance.position.z = this.position.z + this.localPosition.z; - - instance.scale.x = this.scale.x * this.localScale.x; - instance.scale.y = this.scale.y * this.localScale.y; - instance.scale.z = this.scale.z * this.localScale.z; - - instance.up.x = this.up.x; - instance.up.y = this.up.y; - instance.up.z = this.up.z; - - instance.rotateX(this.localRotation.x); - instance.rotateY(this.localRotation.y); - instance.rotateZ(this.localRotation.z); - } - - if (this.materials.length === 1 && this.materials[0].instance) { - instance.material = this.materials[0].instance; - } - - instance.renderOrder = this.renderOrder; - - return instance; }; /** * Updates the mesh instance */ GameLib.D3.Mesh.prototype.updateInstance = function() { - this.instance = this.createInstance(true); + this.createInstance(true); }; /** diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index aa56634..3cd24a5 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -32,6 +32,7 @@ GameLib.D3.Scene = function ( apiScene.lights, apiScene.textures, apiScene.materials, + apiScene.images, apiScene.activeCamera, apiScene.parentEntity ); @@ -90,49 +91,69 @@ GameLib.D3.Scene = function ( }.bind(this) ); - // this.textures = this.textures.map( - // function(apiTexture) { - // - // if (apiTexture instanceof GameLib.D3.API.Texture) { - // var texture = new GameLib.D3.Texture( - // this.graphics, - // apiTexture, - // this.imageFactory - // ); - // - // this.idToObject[texture.id] = texture; - // - // return texture; - // } else { - // console.warn('apiTexture not an instance of API.Texture'); - // throw new Error('apiTexture not an instance of API.Texture'); - // } - // - // }.bind(this) - // ); - // - // this.materials = this.materials.map( - // function(apiMaterial) { - // - // if (apiMaterial instanceof GameLib.D3.API.Material) { - // - // var material = new GameLib.D3.Material( - // this.graphics, - // apiMaterial, - // this.imageFactory - // ); - // - // this.idToObject[material.id] = material; - // - // return material; - // - // } else { - // console.warn('apiMaterial not an instance of API.Material'); - // throw new Error('apiMaterial not an instance of API.Material'); - // } - // - // }.bind(this) - // ); + this.textures = this.textures.map( + function(apiTexture) { + + if (apiTexture instanceof GameLib.D3.API.Texture) { + var texture = new GameLib.D3.Texture( + this.graphics, + apiTexture + ); + + this.idToObject[texture.id] = texture; + + return texture; + } else { + console.warn('apiTexture not an instance of API.Texture'); + throw new Error('apiTexture not an instance of API.Texture'); + } + + }.bind(this) + ); + + this.materials = this.materials.map( + function(apiMaterial) { + + if (apiMaterial instanceof GameLib.D3.API.Material) { + + var material = new GameLib.D3.Material( + this.graphics, + apiMaterial + ); + + this.idToObject[material.id] = material; + + return material; + + } else { + console.warn('apiMaterial not an instance of API.Material'); + throw new Error('apiMaterial not an instance of API.Material'); + } + + }.bind(this) + ); + + this.images = this.images.map( + function(apiImage) { + + if (apiImage instanceof GameLib.D3.API.Image) { + + var image = new GameLib.D3.Image( + this.graphics, + apiImage + ); + + this.idToObject[image.id] = image; + + return image; + + } else { + console.warn('apiImage not an instance of API.Image'); + throw new Error('apiImage not an instance of API.Image'); + } + + }.bind(this) + ); if (this.activeCamera instanceof GameLib.D3.API.Camera) { this.activeCamera = new GameLib.D3.Camera( @@ -258,7 +279,8 @@ GameLib.D3.Scene.prototype.addMesh = function(mesh) { mesh = new GameLib.D3.Mesh( this.graphics, mesh - ) + ); + this.meshes.push(mesh); } mesh.parentScene = this; diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 8956890..d68832b 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -58,10 +58,12 @@ GameLib.D3.Texture = function( this ); - this.image = new GameLib.D3.Image( - this.graphics, - this.image - ); + if (this.image instanceof GameLib.D3.API.Image) { + this.image = new GameLib.D3.Image( + this.graphics, + this.image + ); + } this.instance = this.createInstance(); }; @@ -157,7 +159,13 @@ GameLib.D3.Texture.TEXTURE_TYPE_SPECULAR = 'specular'; * @returns {*} */ GameLib.D3.Texture.prototype.createInstance = function(update) { + if (update) { + + if (!this.instance) { + console.warn('trying to update a non-existent texture instance'); + return; + } this.instance.name = this.name; this.instance.flipY = this.flipY; this.instance.encoding = this.encoding; @@ -165,18 +173,68 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { this.instance.offset.y = this.offset.y; this.instance.repeat.x = this.repeat.x; this.instance.repeat.y = this.repeat.y; + } else { - var instance = new THREE.Texture( - this.image.instance + + if (this.image instanceof GameLib.D3.Image) { + + if (this.image.instance) { + + var instance = new THREE.Texture( + this.image.instance + ); + instance.name = this.name; + instance.flipY = this.flipY; + instance.encoding = this.encoding; + instance.offset.x = this.offset.x; + instance.offset.y = this.offset.y; + instance.repeat.x = this.repeat.x; + instance.repeat.y = this.repeat.y; + + return instance; + } + } + + this.subscribe( + GameLib.Event.IMAGE_LOADED, + function (data) { + + if (!(this.image instanceof GameLib.D3.Image)) { + console.warn('The image associated with this texture has not been setup properly - did you link the objects?') + return; + } + + if (this.instance) { + /** + * The texture has already been loaded + */ + return; + } + + if (data.imagePath === this.image.path) { + var instance = new THREE.Texture( + data.imageInstance + ); + instance.name = this.name; + instance.flipY = this.flipY; + instance.encoding = this.encoding; + instance.offset.x = this.offset.x; + instance.offset.y = this.offset.y; + instance.repeat.x = this.repeat.x; + instance.repeat.y = this.repeat.y; + this.instance = instance; + + this.publish( + GameLib.Event.TEXTURE_LOADED, + { + texture : this + } + ) + } + } ); - instance.name = this.name; - instance.flipY = this.flipY; - instance.encoding = this.encoding; - instance.offset.x = this.offset.x; - instance.offset.y = this.offset.y; - instance.repeat.x = this.repeat.x; - instance.repeat.y = this.repeat.y; - return instance; + + return null; } }; @@ -193,16 +251,11 @@ GameLib.D3.Texture.prototype.updateInstance = function() { */ GameLib.D3.Texture.prototype.toApiObject = function() { - var apiImage = null; - if (this.image) { - apiImage = this.image.toApiObject(); - } - return new GameLib.D3.API.Texture( this.id, this.typeId, this.name, - apiImage, + this.image.id, this.wrapS, this.wrapT, this.repeat.toApiObject(), diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 7d05039..f762cf8 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -394,74 +394,6 @@ GameLib.EntityManager.prototype.onParentEntityChange = function(data) { data.newEntity.addComponent(data.object); }; -GameLib.EntityManager.prototype.onTextureLoaded = function(data) { - - var allGuis = []; - - this.entities.map( - - function(entity) { - - var materials = entity.getComponents(GameLib.D3.Material); - - materials.map( - - function(material) { - - for (var property in material.linkedObjects) { - if ( - material.linkedObjects.hasOwnProperty(property) && - material.linkedObjects[property] === GameLib.D3.Texture && - material[property] - ) { - var texture = material[property]; - - var dataImagePath = GameLib.Utils.StripImageExtension(data.imagePath); - var textureImagePath = GameLib.Utils.StripImageExtension(texture.imagePath); - - if (dataImagePath === textureImagePath) { - - /** - * Update the image path in case the file extension changed - * @type {*} - */ - texture.imagePath = data.imagePath; - - texture.imageInstance = data.imageInstance; - - if (texture.instance) { - texture.updateInstance(); - } else { - texture.instance = texture.createInstance(); - } - - texture.instance.needsUpdate = true; - - material.updateInstance(); - - material.instance.needsUpdate = true; - } - } - } - } - ); - - var guis = entity.getComponents(GameLib.GUI); - - guis.map(function(gui) { - allGuis.push(gui); - }) - } - - - ); - - allGuis.map(function(gui){ - gui.build(this); - }.bind(this)) - -}; - /** * */ @@ -477,11 +409,6 @@ GameLib.EntityManager.prototype.registerCallbacks = function() { this.onParentEntityChange ); - this.subscribe( - GameLib.Event.TEXTURE_LOADED, - this.onTextureLoaded - ) - }; /**