From 9a12e4e3c3b0b098858f8fc65433ad1ed98338b7 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Thu, 8 Jun 2017 18:17:03 +0200 Subject: [PATCH] no shortcuts - images to api --- src/game-lib-a-1-event.js | 1 + src/game-lib-a-component-a.js | 1 + src/game-lib-d3-api-image.js | 77 ++++ src/game-lib-d3-api-material.js | 2 + src/game-lib-d3-api-renderer.js | 2 +- src/game-lib-d3-api-scene.js | 23 +- src/game-lib-d3-api-texture.js | 12 +- src/game-lib-d3-image-factory.js | 9 + src/game-lib-d3-image.js | 113 +++-- src/game-lib-d3-material.js | 752 +++++++++++++++++-------------- src/game-lib-d3-mesh-0.js | 68 +-- src/game-lib-d3-scene.js | 139 ++---- src/game-lib-d3-texture.js | 134 ++---- src/game-lib-entity-manager.js | 85 +++- src/game-lib-gui.js | 2 +- src/game-lib-system.js | 142 ++---- src/game-lib-utils.js | 9 + 17 files changed, 842 insertions(+), 729 deletions(-) create mode 100644 src/game-lib-d3-api-image.js diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 304a0f5..e3299fd 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -17,6 +17,7 @@ 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; /** * Subscribe to some events diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index e875cd9..13a9b10 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -76,6 +76,7 @@ GameLib.Component.COMPONENT_DOM_ELEMENT = 0x1c; GameLib.Component.COMPONENT_IMAGE_FACTORY = 0x1d; GameLib.Component.COMPONENT_STATS = 0x1e; GameLib.Component.COMPONENT_GUI = 0x1f; +GameLib.Component.COMPONENT_IMAGE = 0x20; /** * Components are linked at runtime - for storing, we just store the ID diff --git a/src/game-lib-d3-api-image.js b/src/game-lib-d3-api-image.js new file mode 100644 index 0000000..d3aa40c --- /dev/null +++ b/src/game-lib-d3-api-image.js @@ -0,0 +1,77 @@ +/** + * Image + * @param id + * @param name + * @param path + * @param contentType + * @param parentEntity GameLib.Entity + * @constructor + */ +GameLib.D3.API.Image = function( + id, + name, + path, + contentType, + parentEntity +) { + + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_IMAGE, + null, + null, + parentEntity + ); + + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(name)) { + name = 'Image ' + GameLib.Utils.RandomId() + '.png'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(path)) { + path = '/' + this.name.toLowerCase().replace(' ', '_'); + } + this.path = path; + + if (GameLib.Utils.UndefinedOrNull(contentType)) { + + contentType = 'application/octet-stream'; + + if (this.name.match(/(png)$/i)) { + contentType = 'image/png'; + } + + if (this.name.match(/(jpg|jpeg)$/i)) { + contentType = 'image/jpeg'; + } + + if (this.name.match(/(gif)$/i)) { + contentType = 'image/gif'; + } + } + this.contentType = contentType; +}; + +GameLib.D3.API.Image.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Image.prototype.constructor = GameLib.D3.API.Image; + +/** + * Returns an API light from an Object light + * @constructor + * @param objectImage + */ +GameLib.D3.API.Image.FromObject = function(objectImage) { + return new GameLib.D3.API.Image( + objectImage.id, + objectImage.name, + objectImage.path, + objectImage.contentType, + objectImage.parentEntity + ); +}; + diff --git a/src/game-lib-d3-api-material.js b/src/game-lib-d3-api-material.js index 951bc22..07926c6 100644 --- a/src/game-lib-d3-api-material.js +++ b/src/game-lib-d3-api-material.js @@ -511,6 +511,8 @@ GameLib.D3.API.Material = function( selected = false; } this.selected = selected; + + this.needsUpdate = false; }; GameLib.D3.API.Material.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-renderer.js b/src/game-lib-d3-api-renderer.js index 2c84d75..126b8c5 100644 --- a/src/game-lib-d3-api-renderer.js +++ b/src/game-lib-d3-api-renderer.js @@ -76,7 +76,7 @@ GameLib.D3.API.Renderer = function ( this.domElement = domElement; if (GameLib.Utils.UndefinedOrNull(clearColor)) { - clearColor = new GameLib.API.Color(); + clearColor = new GameLib.API.Color(0.58, 0.58, 0.58); } this.clearColor = clearColor; }; diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index 7627b64..a67e293 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -2,7 +2,6 @@ * Raw Scene API object - should always correspond with the Scene Schema * @param id String * @param name String - * @param imageFactory [GameLib.D3.API.ImageFactory] * @param meshes [GameLib.D3.API.Mesh] * @param position GameLib.API.Vector3 * @param quaternion GameLib.API.Quaternion @@ -18,7 +17,6 @@ GameLib.D3.API.Scene = function( id, name, - imageFactory, meshes, position, quaternion, @@ -34,7 +32,6 @@ GameLib.D3.API.Scene = function( this, GameLib.Component.COMPONENT_SCENE, { - 'imageFactory' : GameLib.D3.ImageFactory, 'meshes' : [GameLib.D3.Mesh], 'lights' : [GameLib.D3.Light], 'textures' : [GameLib.D3.Texture], @@ -55,12 +52,6 @@ GameLib.D3.API.Scene = function( } this.name = name; - if (GameLib.Utils.UndefinedOrNull(imageFactory)) { - imageFactory = null; - console.warn('Constructing an API Scene with no Image Factory') - } - this.imageFactory = imageFactory; - if (GameLib.Utils.UndefinedOrNull(meshes)) { meshes = []; } @@ -114,12 +105,10 @@ GameLib.D3.API.Scene.prototype.constructor = GameLib.D3.API.Scene; /** * Returns an API scene from an Object scene * @param objectScene - * @param imageFactory GameLib.D3.ImageFactory * @constructor */ -GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) { +GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { - var apiImageFactory = null; var apiMeshes = []; var apiLights = []; var apiTextures = []; @@ -131,15 +120,6 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) { var apiActiveCamera = null; - /** - * Passed in ImageFactory overrides API image factory - */ - if (imageFactory) { - apiImageFactory = imageFactory; - } else if (objectScene.imageFactory) { - apiImageFactory = GameLib.D3.API.ImageFactory.FromObjectImageFactory(objectScene.imageFactory); - } - if (objectScene.meshes) { apiMeshes = objectScene.meshes.map( function(objectMesh) { @@ -191,7 +171,6 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene, imageFactory) { return new GameLib.D3.API.Scene( objectScene.id, objectScene.name, - apiImageFactory, apiMeshes, apiPosition, apiQuaternion, diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 3dd02a2..4022bf5 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -3,7 +3,7 @@ * @param id * @param typeId * @param name - * @param imagePath + * @param image * @param wrapS * @param wrapT * @param repeat @@ -28,7 +28,7 @@ GameLib.D3.API.Texture = function( id, typeId, name, - imagePath, + image, wrapS, wrapT, repeat, @@ -76,10 +76,10 @@ GameLib.D3.API.Texture = function( } this.name = name; - if (GameLib.Utils.UndefinedOrNull(imagePath)) { - imagePath = null; + if (GameLib.Utils.UndefinedOrNull(image)) { + image = null; } - this.imagePath = imagePath; + this.image = image; if (GameLib.Utils.UndefinedOrNull(wrapS)) { wrapS = GameLib.D3.Texture.TYPE_REPEAT_WRAPPING; @@ -182,7 +182,7 @@ GameLib.D3.API.Texture.FromObjectTexture = function(objectTexture) { objectTexture.id, objectTexture.textureType, objectTexture.name, - objectTexture.imagePath, + objectTexture.image, objectTexture.wrapS, objectTexture.wrapT, GameLib.API.Vector2.FromObjectVector(objectTexture.repeat), diff --git a/src/game-lib-d3-image-factory.js b/src/game-lib-d3-image-factory.js index 450fb32..69ebc2a 100644 --- a/src/game-lib-d3-image-factory.js +++ b/src/game-lib-d3-image-factory.js @@ -97,6 +97,15 @@ GameLib.D3.ImageFactory.prototype.loadImage = function( this.instance.load( this.baseUrl + imagePath + '?ts=' + Date.now(), function (image) { + + GameLib.Event.Emit( + GameLib.Event.TEXTURE_LOADED, + { + imagePath : imagePath, + imageInstance : image + } + ); + defer.resolve(image); }.bind(this), function onProgress(xhr) { diff --git a/src/game-lib-d3-image.js b/src/game-lib-d3-image.js index b164654..1ca2db3 100644 --- a/src/game-lib-d3-image.js +++ b/src/game-lib-d3-image.js @@ -1,48 +1,89 @@ /** * Image - * @param id - * @param filename - * @param size - * @param contentType - * @param textureLink * @constructor + * @param graphics + * @param apiImage */ GameLib.D3.Image = function( - id, - filename, - size, - contentType, - textureLink + graphics, + apiImage ) { - if (GameLib.Utils.UndefinedOrNull(id)) { - id = GameLib.Utils.RandomId(); + if (GameLib.Utils.UndefinedOrNull(apiImage)) { + apiImage = {}; } - this.id = id; - this.filename = filename; - - if (GameLib.Utils.UndefinedOrNull(size)) { - size = 0; + if (apiImage instanceof GameLib.D3.Image) { + return apiImage; } - this.size = size; - if (GameLib.Utils.UndefinedOrNull(contentType)) { + GameLib.D3.API.Image.call( + this, + apiImage.id, + apiImage.name, + apiImage.path, + apiImage.contentType, + apiImage.parentEntity + ); - contentType = 'application/octet-stream'; - - if (this.filename.match(/(png)$/i)) { - contentType = 'image/png'; - } - - if (this.filename.match(/(jpg|jpeg)$/i)) { - contentType = 'image/jpeg'; - } - - if (this.filename.match(/(gif)$/i)) { - contentType = 'image/gif'; - } - } - this.contentType = contentType; - - this.textureLink = textureLink; + this.instance = this.createInstance(); +}; + +GameLib.D3.Image.prototype = Object.create(GameLib.D3.API.Image.prototype); +GameLib.D3.Image.prototype.constructor = GameLib.D3.Image; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.Image.prototype.createInstance = function(update) { + + if (update) { + console.log('why the fuck are you updating an image instance?'); + 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; +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.Image.prototype.updateInstance = function() { + this.createInstance(true); +}; + +/** + * + * @returns {GameLib.D3.API.Image} + */ +GameLib.D3.Image.prototype.toApiObject = function() { + + var apiImage = new GameLib.D3.API.Image( + this.id, + this.name, + this.path, + this.contentType, + GameLib.Utils.IdOrNull(this.parentEntity) + ); + + return apiImage; +}; + +/** + * @param graphics + * @param objectLight + * @returns {GameLib.D3.Image} + * @constructor + */ +GameLib.D3.Image.FromObject = function(graphics, objectLight) { + return new GameLib.D3.Image( + graphics, + GameLib.D3.API.Image.FromObject(objectLight) + ); }; diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index c5cdc64..d40ed11 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -3,14 +3,12 @@ * created * @param graphics GameLib.D3.Graphics * @param apiMaterial GameLib.D3.API.Material - * @param imageFactory GameLib.D3.ImageFactory * @constructor * @returns {GameLib.D3.Material | GameLib.D3.API.Material} */ GameLib.D3.Material = function( graphics, - apiMaterial, - imageFactory + apiMaterial ) { this.graphics = graphics; @@ -23,13 +21,7 @@ GameLib.D3.Material = function( if (apiMaterial instanceof GameLib.D3.Material) { return apiMaterial; } - - if (GameLib.Utils.UndefinedOrNull(imageFactory)) { - console.warn('Cannot create a Material fully without specifying an ImageFactory'); - imageFactory = null; - } - this.imageFactory = imageFactory; - + GameLib.D3.API.Material.call( this, apiMaterial.id, @@ -125,12 +117,8 @@ GameLib.D3.Material = function( if (this.alphaMap instanceof GameLib.D3.API.Texture) { this.alphaMap = new GameLib.D3.Texture( this.graphics, - this.alphaMap, - this.imageFactory + this.alphaMap ); - } else { - console.warn('this.alphaMap is not an instance of API.Texture'); - // throw new Error('this.alphaMap is not an instance of API.Texture'); } } @@ -138,12 +126,8 @@ GameLib.D3.Material = function( if (this.aoMap instanceof GameLib.D3.API.Texture) { this.aoMap = new GameLib.D3.Texture( this.graphics, - this.aoMap, - this.imageFactory + this.aoMap ); - } else { - console.warn('this.aoMap is not an instance of API.Texture'); - // throw new Error('this.aoMap is not an instance of API.Texture'); } } @@ -151,12 +135,8 @@ GameLib.D3.Material = function( if (this.bumpMap instanceof GameLib.D3.API.Texture) { this.bumpMap = new GameLib.D3.Texture( this.graphics, - this.bumpMap, - this.imageFactory + this.bumpMap ); - } else { - console.warn('this.bumpMap is not an instance of API.Texture'); - // throw new Error('this.bumpMap is not an instance of API.Texture'); } } @@ -164,12 +144,8 @@ GameLib.D3.Material = function( if (this.diffuseMap instanceof GameLib.D3.API.Texture) { this.diffuseMap = new GameLib.D3.Texture( this.graphics, - this.diffuseMap, - this.imageFactory + this.diffuseMap ); - } else { - console.warn('this.diffuseMap is not an instance of API.Texture'); - // throw new Error('this.diffuseMap is not an instance of API.Texture'); } } @@ -177,12 +153,8 @@ GameLib.D3.Material = function( if (this.displacementMap instanceof GameLib.D3.API.Texture) { this.displacementMap = new GameLib.D3.Texture( this.graphics, - this.displacementMap, - this.imageFactory + this.displacementMap ); - } else { - console.warn('this.displacementMap is not an instance of API.Texture'); - // throw new Error('this.displacementMap is not an instance of API.Texture'); } } @@ -190,12 +162,8 @@ GameLib.D3.Material = function( if (this.emissiveMap instanceof GameLib.D3.API.Texture) { this.emissiveMap = new GameLib.D3.Texture( this.graphics, - this.emissiveMap, - this.imageFactory + this.emissiveMap ); - } else { - console.warn('this.emissiveMap is not an instance of API.Texture'); - // throw new Error('this.emissiveMap is not an instance of API.Texture'); } } @@ -203,12 +171,8 @@ GameLib.D3.Material = function( if (this.environmentMap instanceof GameLib.D3.API.Texture) { this.environmentMap = new GameLib.D3.Texture( this.graphics, - this.environmentMap, - this.imageFactory + this.environmentMap ); - } else { - console.warn('this.environmentMap is not an instance of API.Texture'); - // throw new Error('this.environmentMap is not an instance of API.Texture'); } } @@ -216,12 +180,8 @@ GameLib.D3.Material = function( if (this.lightMap instanceof GameLib.D3.API.Texture) { this.lightMap = new GameLib.D3.Texture( this.graphics, - this.lightMap, - this.imageFactory + this.lightMap ); - } else { - console.warn('this.lightMap is not an instance of API.Texture'); - // throw new Error('this.lightMap is not an instance of API.Texture'); } } @@ -229,12 +189,8 @@ GameLib.D3.Material = function( if (this.metalnessMap instanceof GameLib.D3.API.Texture) { this.metalnessMap = new GameLib.D3.Texture( this.graphics, - this.metalnessMap, - this.imageFactory + this.metalnessMap ); - } else { - console.warn('this.metalnessMap is not an instance of API.Texture'); - // throw new Error('this.metalnessMap is not an instance of API.Texture'); } } @@ -242,12 +198,8 @@ GameLib.D3.Material = function( if (this.normalMap instanceof GameLib.D3.API.Texture) { this.normalMap = new GameLib.D3.Texture( this.graphics, - this.normalMap, - this.imageFactory + this.normalMap ); - } else { - console.warn('this.normalMap is not an instance of API.Texture'); - // throw new Error('this.normalMap is not an instance of API.Texture'); } } @@ -255,12 +207,8 @@ GameLib.D3.Material = function( if (this.roughnessMap instanceof GameLib.D3.API.Texture) { this.roughnessMap = new GameLib.D3.Texture( this.graphics, - this.roughnessMap, - this.imageFactory + this.roughnessMap ); - } else { - console.warn('this.roughnessMap is not an instance of API.Texture'); - // throw new Error('this.roughnessMap is not an instance of API.Texture'); } } @@ -268,17 +216,11 @@ GameLib.D3.Material = function( if (this.specularMap instanceof GameLib.D3.API.Texture) { this.specularMap = new GameLib.D3.Texture( this.graphics, - this.specularMap, - this.imageFactory + this.specularMap ); - } 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.needsUpdate = false; - this.buildIdToObject(); this.instance = this.createInstance(); @@ -384,289 +326,437 @@ GameLib.D3.Material.MATERIAL_TYPE_POINTS = "PointsMaterial"; GameLib.D3.Material.MATERIAL_TYPE_SPRITE = "SpriteMaterial"; GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial"; +GameLib.D3.Material.prototype.createStandardMaterialInstance = function() { + return new THREE.MeshStandardMaterial({ + name: this.name, + opacity: this.opacity, + transparent: this.transparent, + blending: this.blending, + blendSrc: this.blendSrc, + blendDst: this.blendDst, + blendEquation: this.blendEquation, + depthTest: this.depthTest, + depthFunc: this.depthFunc, + depthWrite: this.depthWrite, + polygonOffset: this.polygonOffset, + polygonOffsetFactor: this.polygonOffsetFactor, + polygonOffsetUnits: this.polygonOffsetUnits, + alphaTest: this.alphaTest, + clippingPlanes: this.clippingPlanes, + clipShadows: this.clipShadows, + overdraw: this.overdraw, + visible: this.visible, + side: this.side, + color: this.color.instance, + roughness: this.roughness, + metalness: this.metalness, + lightMapIntensity: this.lightMapIntensity, + aoMapIntensity: this.aoMapIntensity, + emissive: this.emissive.instance, + emissiveIntensity: this.emissiveIntensity, + bumpScale: this.bumpScale, + normalScale: this.normalScale, + displacementScale: this.displacementScale, + refractionRatio: this.refractionRatio, + fog: this.fog, + shading: this.shading, + wireframe: this.wireframe, + wireframeLinewidth: this.wireframeLineWidth, + wireframeLinecap: this.wireframeLineCap, + wireframeLinejoin: this.wireframeLineJoin, + vertexColors: this.vertexColors, + skinning: this.skinning, + morphTargets: this.morphTargets, + morphNormals: this.morphNormals + }); +}; + +GameLib.D3.Material.prototype.createPointsMaterialInstance = function() { + return new THREE.PointsMaterial({ + name: this.name, + opacity: this.opacity, + transparent: this.transparent, + blending: this.blending, + blendSrc: this.blendSrc, + blendDst: this.blendDst, + blendEquation: this.blendEquation, + depthTest: this.depthTest, + depthFunc: this.depthFunc, + depthWrite: this.depthWrite, + polygonOffset: this.polygonOffset, + polygonOffsetFactor: this.polygonOffsetFactor, + polygonOffsetUnits: this.polygonOffsetUnits, + alphaTest: this.alphaTest, + clippingPlanes: this.clippingPlanes, + clipShadows: this.clipShadows, + overdraw: this.overdraw, + visible: this.visible, + side: this.side, + color: this.color.instance, + size: this.pointSize, + sizeAttenuation: this.pointSizeAttenuation, + vertexColors: GameLib.D3.Material.TYPE_NO_COLORS, + fog: this.fog + }); +}; + +GameLib.D3.Material.prototype.createPhongMaterialInstance = function() { + return new THREE.MeshPhongMaterial({ + name: this.name, + opacity: this.opacity, + transparent: this.transparent, + blending: this.blending, + blendSrc: this.blendSrc, + blendDst: this.blendDst, + blendEquation: this.blendEquation, + depthTest: this.depthTest, + depthFunc: this.depthFunc, + depthWrite: this.depthWrite, + polygonOffset: this.polygonOffset, + polygonOffsetFactor: this.polygonOffsetFactor, + polygonOffsetUnits: this.polygonOffsetUnits, + alphaTest: this.alphaTest, + clippingPlanes: this.clippingPlanes, + clipShadows: this.clipShadows, + overdraw: this.overdraw, + visible: this.visible, + side: this.side, + color: this.color.instance, + specular: this.specular.instance, + shininess: this.shininess, + lightMapIntensity: this.lightMapIntensity, + aoMapIntensity: this.aoMapIntensity, + emissive: this.emissive.instance, + emissiveIntensity: this.emissiveIntensity, + bumpScale: this.bumpScale, + normalScale: this.normalScale, + displacementScale: this.displacementScale, + combine: this.combine, + refractionRatio: this.refractionRatio, + fog: this.fog, + shading: this.shading, + wireframe: this.wireframe, + wireframeLinewidth: this.wireframeLineWidth, + wireframeLinecap: this.wireframeLineCap, + wireframeLinejoin: this.wireframeLineJoin, + vertexColors: this.vertexColors, + skinning: this.skinning, + morphTargets: this.morphTargets, + morphNormals: this.morphNormals + }); +}; + +GameLib.D3.Material.prototype.createMeshBasicMaterialInstance = function() { + return new THREE.MeshBasicMaterial({ + name: this.name, + opacity: this.opacity, + transparent: this.transparent, + blending: this.blending, + blendSrc: this.blendSrc, + blendDst: this.blendDst, + blendEquation: this.blendEquation, + depthTest: this.depthTest, + depthFunc: this.depthFunc, + depthWrite: this.depthWrite, + polygonOffset: this.polygonOffset, + polygonOffsetFactor: this.polygonOffsetFactor, + polygonOffsetUnits: this.polygonOffsetUnits, + alphaTest: this.alphaTest, + clippingPlanes: this.clippingPlanes, + clipShadows: this.clipShadows, + overdraw: this.overdraw, + visible: this.visible, + side: this.side, + color: this.color.instance, + vertexColors: GameLib.D3.Material.TYPE_NO_COLORS, + fog: this.fog + }); +}; + +/** + * updates textures + */ +GameLib.D3.Material.prototype.updateTextures = function() { + if (this.alphaMap) { + this.instance.alphaMap = this.alphaMap.instance; + } else { + this.instance.alphaMap = null; + } + if (this.aoMap) { + this.instance.aoMap = this.aoMap.instance; + } else { + this.instance.aoMap = null; + } + if (this.bumpMap) { + this.instance.bumpMap = this.bumpMap.instance; + } else { + this.instance.bumpMap = null; + } + if (this.diffuseMap) { + this.instance.map = this.diffuseMap.instance; + } else { + this.instance.map = null; + } + if (this.displacementMap) { + this.instance.displacementMap = this.displacementMap.instance; + } else { + this.instance.displacementMap = null; + } + if (this.emissiveMap) { + this.instance.emissiveMap = this.emissiveMap.instance; + } else { + this.instance.emissiveMap = null; + } + if (this.environmentMap) { + this.instance.envMap = this.environmentMap.instance; + } else { + this.instance.envMap = null; + } + if (this.lightMap) { + this.instance.lightMap = this.lightMap.instance; + } else { + this.instance.lightMap = null; + } + if (this.metalnessMap) { + this.instance.metalnessMap = this.metalnessMap.instance; + } else { + this.instance.metalnessMap = null; + } + if (this.normalMap) { + this.instance.normalMap = this.normalMap.instance; + } else { + this.instance.normalMap = null; + } + if (this.roughnessMap) { + this.instance.roughnessMap = this.roughnessMap.instance; + } else { + this.instance.roughnessMap = null; + } + if (this.specularMap) { + this.instance.specularMap = this.specularMap.instance; + } else { + this.instance.specularMap = null; + } +}; + + +GameLib.D3.Material.prototype.updateStandardMaterialInstance = function() { + this.instance.name = this.name; + this.instance.opacity = this.opacity; + this.instance.transparent = this.transparent; + this.instance.blending = this.blending; + this.instance.blendSrc = this.blendSrc; + this.instance.blendDst = this.blendDst; + this.instance.blendEquation = this.blendEquation; + this.instance.depthTest = this.depthTest; + this.instance.depthFunc = this.depthFunc; + this.instance.depthWrite = this.depthWrite; + this.instance.polygonOffset = this.polygonOffset; + this.instance.polygonOffsetFactor = this.polygonOffsetFactor; + this.instance.polygonOffsetUnits = this.polygonOffsetUnits; + this.instance.alphaTest = this.alphaTest; + this.instance.clippingPlanes = this.clippingPlanes; + this.instance.clipShadows = this.clipShadows; + this.instance.overdraw = this.overdraw; + this.instance.visible = this.visible; + this.instance.side = this.side; + this.instance.color = this.color.instance; + this.instance.roughness = this.roughness; + this.instance.metalness = this.metalness; + this.instance.lightMapIntensity = this.lightMapIntensity; + this.instance.aoMapIntensity = this.aoMapIntensity; + this.instance.emissive = this.emissive.instance; + this.instance.emissiveIntensity = this.emissiveIntensity; + this.instance.bumpScale = this.bumpScale; + this.instance.normalScale = this.normalScale; + this.instance.displacementScale = this.displacementScale; + this.instance.refractionRatio = this.refractionRatio; + this.instance.fog = this.fog; + this.instance.shading = this.shading; + this.instance.wireframe = this.wireframe; + this.instance.wireframeLinewidth = this.wireframeLineWidth; + this.instance.wireframeLinecap = this.wireframeLineCap; + this.instance.wireframeLinejoin = this.wireframeLineJoin; + this.instance.vertexColors = this.vertexColors; + this.instance.skinning = this.skinning; + this.instance.morphTargets = this.morphTargets; + this.instance.morphNormals = this.morphNormals; + this.updateTextures(); +}; + +GameLib.D3.Material.prototype.updatePointsMaterialInstance = function() { + this.instance.name = this.name; + this.instance.opacity = this.opacity; + this.instance.transparent = this.transparent; + this.instance.blending = this.blending; + this.instance.blendSrc = this.blendSrc; + this.instance.blendDst = this.blendDst; + this.instance.blendEquation = this.blendEquation; + this.instance.depthTest = this.depthTest; + this.instance.depthFunc = this.depthFunc; + this.instance.depthWrite = this.depthWrite; + this.instance.polygonOffset = this.polygonOffset; + this.instance.polygonOffsetFactor = this.polygonOffsetFactor; + this.instance.polygonOffsetUnits = this.polygonOffsetUnits; + this.instance.alphaTest = this.alphaTest; + this.instance.clippingPlanes = this.clippingPlanes; + this.instance.clipShadows = this.clipShadows; + this.instance.overdraw = this.overdraw; + this.instance.visible = this.visible; + this.instance.side = this.side; + this.instance.color = this.color.instance; + this.instance.size = this.pointSize; + 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() { + this.instance.name = this.name; + this.instance.opacity = this.opacity; + this.instance.transparent = this.transparent; + this.instance.blending = this.blending; + this.instance.blendSrc = this.blendSrc; + this.instance.blendDst = this.blendDst; + this.instance.blendEquation = this.blendEquation; + this.instance.depthTest = this.depthTest; + this.instance.depthFunc = this.depthFunc; + this.instance.depthWrite = this.depthWrite; + this.instance.polygonOffset = this.polygonOffset; + this.instance.polygonOffsetFactor = this.polygonOffsetFactor; + this.instance.polygonOffsetUnits = this.polygonOffsetUnits; + this.instance.alphaTest = this.alphaTest; + this.instance.clippingPlanes = this.clippingPlanes; + this.instance.clipShadows = this.clipShadows; + this.instance.overdraw = this.overdraw; + this.instance.visible = this.visible; + this.instance.side = this.side; + this.instance.color = this.color.instance; + this.instance.specular = this.specular.instance; + this.instance.shininess = this.shininess; + this.instance.lightMapIntensity = this.lightMapIntensity; + this.instance.aoMapIntensity = this.aoMapIntensity; + this.instance.emissive = this.emissive.instance; + this.instance.emissiveIntensity = this.emissiveIntensity; + this.instance.bumpScale = this.bumpScale; + this.instance.normalScale = this.normalScale; + this.instance.displacementScale = this.displacementScale; + this.instance.combine = this.combine; + this.instance.refractionRatio = this.refractionRatio; + this.instance.fog = this.fog; + this.instance.shading = this.shading; + this.instance.wireframe = this.wireframe; + this.instance.wireframeLinewidth = this.wireframeLineWidth; + this.instance.wireframeLinecap = this.wireframeLineCap; + this.instance.wireframeLinejoin = this.wireframeLineJoin; + this.instance.vertexColors = this.vertexColors; + this.instance.skinning = this.skinning; + this.instance.morphTargets = this.morphTargets; + this.instance.morphNormals = this.morphNormals; + this.updateTextures(); +}; + +GameLib.D3.Material.prototype.updateMeshBasicMaterialInstance = function() { + this.instance.name = this.name; + this.instance.opacity = this.opacity; + this.instance.transparent = this.transparent; + this.instance.blending = this.blending; + this.instance.blendSrc = this.blendSrc; + this.instance.blendDst = this.blendDst; + this.instance.blendEquation = this.blendEquation; + this.instance.depthTest = this.depthTest; + this.instance.depthFunc = this.depthFunc; + this.instance.depthWrite = this.depthWrite; + this.instance.polygonOffset = this.polygonOffset; + this.instance.polygonOffsetFactor = this.polygonOffsetFactor; + this.instance.polygonOffsetUnits = this.polygonOffsetUnits; + this.instance.alphaTest = this.alphaTest; + this.instance.clippingPlanes = this.clippingPlanes; + this.instance.clipShadows = this.clipShadows; + this.instance.overdraw = this.overdraw; + this.instance.visible = this.visible; + this.instance.side = this.side; + this.instance.color = this.color.instance; + this.instance.vertexColors = GameLib.D3.Material.TYPE_NO_COLORS; + this.instance.fog = this.fog; + this.updateTextures(); +}; + /** * Material instance * @returns {*} */ GameLib.D3.Material.prototype.createInstance = function(update) { - var instance = null; - if (update) { - instance = this.instance; - } + + if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { + if (!(this.instance instanceof THREE.MeshStandardMaterial)) { + this.instance = this.createStandardMaterialInstance(); + } else { + this.updateStandardMaterialInstance(); + } + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) { + if (!(this.instance instanceof THREE.PointsMaterial)) { + this.instance = this.createPointsMaterialInstance(); + } else { + this.updatePointsMaterialInstance(); + } + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) { + if (!(this.instance instanceof THREE.MeshPhongMaterial)) { + this.instance = this.createPhongMaterialInstance(); + } else { + this.updatePhongMaterialInstance(); + } + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) { + if (!(this.instance instanceof THREE.MeshBasicMaterial)) { + this.instance = this.createMeshBasicMaterialInstance(); + } else { + this.updateMeshBasicMaterialInstance(); + } + } else { + console.warn('not yet implemented (material type = ' + this.materialType + ')'); + } - if (!instance) { + this.instance.needsUpdate = true; - if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { + } else { - instance = new THREE.MeshStandardMaterial({ - name: this.name, - opacity: this.opacity, - transparent: this.transparent, - blending: this.blending, - blendSrc: this.blendSrc, - blendDst: this.blendDst, - blendEquation: this.blendEquation, - depthTest: this.depthTest, - depthFunc: this.depthFunc, - depthWrite: this.depthWrite, - polygonOffset: this.polygonOffset, - polygonOffsetFactor: this.polygonOffsetFactor, - polygonOffsetUnits: this.polygonOffsetUnits, - alphaTest: this.alphaTest, - clippingPlanes: this.clippingPlanes, - clipShadows: this.clipShadows, - overdraw: this.overdraw, - visible: this.visible, - side: this.side, - color: this.color.instance, - roughness: this.roughness, - metalness: this.metalness, - lightMapIntensity: this.lightMapIntensity, - aoMapIntensity: this.aoMapIntensity, - emissive: this.emissive.instance, - emissiveIntensity: this.emissiveIntensity, - bumpScale: this.bumpScale, - normalScale: this.normalScale, - displacementScale: this.displacementScale, - refractionRatio: this.refractionRatio, - fog: this.fog, - shading: this.shading, - wireframe: this.wireframe, - wireframeLinewidth: this.wireframeLineWidth, - wireframeLinecap: this.wireframeLineCap, - wireframeLinejoin: this.wireframeLineJoin, - vertexColors: this.vertexColors, - skinning: this.skinning, - morphTargets: this.morphTargets, - morphNormals: this.morphNormals - }); + var instance = null; - } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { + if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { - instance = new THREE.MeshPhongMaterial({ - name: this.name, - opacity: this.opacity, - transparent: this.transparent, - blending: this.blending, - blendSrc: this.blendSrc, - blendDst: this.blendDst, - blendEquation: this.blendEquation, - depthTest: this.depthTest, - depthFunc: this.depthFunc, - depthWrite: this.depthWrite, - polygonOffset: this.polygonOffset, - polygonOffsetFactor: this.polygonOffsetFactor, - polygonOffsetUnits: this.polygonOffsetUnits, - alphaTest: this.alphaTest, - clippingPlanes: this.clippingPlanes, - clipShadows: this.clipShadows, - overdraw: this.overdraw, - visible: this.visible, - side: this.side, - color: this.color.instance, - specular: this.specular.instance, - shininess: this.shininess, - lightMapIntensity: this.lightMapIntensity, - aoMapIntensity: this.aoMapIntensity, - emissive: this.emissive.instance, - emissiveIntensity: this.emissiveIntensity, - bumpScale: this.bumpScale, - normalScale: this.normalScale, - displacementScale: this.displacementScale, - combine: this.combine, - refractionRatio: this.refractionRatio, - fog: this.fog, - shading: this.shading, - wireframe: this.wireframe, - wireframeLinewidth: this.wireframeLineWidth, - wireframeLinecap: this.wireframeLineCap, - wireframeLinejoin: this.wireframeLineJoin, - vertexColors: this.vertexColors, - skinning: this.skinning, - morphTargets: this.morphTargets, - morphNormals: this.morphNormals - }); - } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_POINTS) { + instance = this.createStandardMaterialInstance(); - instance = new this.graphics.instance.PointsMaterial({ - name: this.name, - opacity: this.opacity, - transparent: this.transparent, - blending: this.blending, - blendSrc: this.blendSrc, - blendDst: this.blendDst, - blendEquation: this.blendEquation, - depthTest: this.depthTest, - depthFunc: this.depthFunc, - depthWrite: this.depthWrite, - polygonOffset: this.polygonOffset, - polygonOffsetFactor: this.polygonOffsetFactor, - polygonOffsetUnits: this.polygonOffsetUnits, - alphaTest: this.alphaTest, - clippingPlanes: this.clippingPlanes, - clipShadows: this.clipShadows, - overdraw: this.overdraw, - visible: this.visible, - side: this.side, - color: this.color.instance, - size: this.pointSize, - sizeAttenuation: this.pointSizeAttenuation, - vertexColors: GameLib.D3.Material.TYPE_NO_COLORS, - fog: this.fog - }); + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) { - } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_BASIC) { + instance = this.createPointsMaterialInstance(); - instance = new THREE.MeshBasicMaterial({ - name: this.name, - opacity: this.opacity, - transparent: this.transparent, - blending: this.blending, - blendSrc: this.blendSrc, - blendDst: this.blendDst, - blendEquation: this.blendEquation, - depthTest: this.depthTest, - depthFunc: this.depthFunc, - depthWrite: this.depthWrite, - polygonOffset: this.polygonOffset, - polygonOffsetFactor: this.polygonOffsetFactor, - polygonOffsetUnits: this.polygonOffsetUnits, - alphaTest: this.alphaTest, - clippingPlanes: this.clippingPlanes, - clipShadows: this.clipShadows, - overdraw: this.overdraw, - visible: this.visible, - side: this.side, - color: this.color.instance, - vertexColors: GameLib.D3.Material.TYPE_NO_COLORS, - fog: this.fog - }); + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) { + + instance = this.createPhongMaterialInstance(); + + } else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) { + + instance = this.createMeshBasicMaterialInstance(); } else { - console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up"); - } - } - - if (update) { - - for (var property in instance) { - if (instance.hasOwnProperty(property)) { - - if (property == 'alphaMap') { - if (this.alphaMap) { - instance.alphaMap = this.alphaMap.instance; - } else { - instance.alphaMap = null; - } - } - else if (property == 'aoMap') { - if (this.aoMap) { - instance.aoMap = this.aoMap.instance; - } else { - instance.aoMap = null; - } - } - else if (property == 'bumpMap') { - if (this.bumpMap) { - instance.bumpMap = this.bumpMap.instance; - } else { - instance.bumpMap = null; - } - } - else if (property == 'map') { - if (this.diffuseMap) { - instance.map = this.diffuseMap.instance; - } else { - instance.map = null; - } - } - else if (property == 'displacementMap') { - if (this.displacementMap) { - instance.displacementMap = this.displacementMap.instance; - } else { - instance.displacementMap = null; - } - } - else if (property == 'emissiveMap') { - if (this.emissiveMap) { - instance.emissiveMap = this.emissiveMap.instance; - } else { - instance.emissiveMap = null; - } - } - else if (property == 'envMap') { - if (this.environmentMap) { - instance.envMap = this.environmentMap.instance; - } else { - instance.envMap = null; - } - } - else if (property == 'lightMap') { - if (this.lightMap) { - instance.lightMap = this.lightMap.instance; - } else { - instance.lightMap = null; - } - } - else if (property == 'metalnessMap') { - if (this.metalnessMap) { - instance.metalnessMap = this.metalnessMap.instance; - } else { - instance.metalnessMap = null; - } - } - else if (property == 'normalMap') { - if (this.normalMap) { - instance.normalMap = this.normalMap.instance; - } else { - instance.normalMap = null; - } - } - else if (property == 'roughnessMap') { - if (this.roughnessMap) { - instance.roughnessMap = this.roughnessMap.instance; - } else { - instance.roughnessMap = null; - } - } - else if (property == 'specularMap') { - if (this.specularMap) { - instance.specularMap = this.specularMap.instance; - } else { - instance.specularMap = null; - } - } - else if (property == 'size') { - instance.size = this.pointSize; - } - else if (property == 'sizeAttenuation') { - instance.sizeAttenuation = this.pointSizeAttenuation; - } - else if (instance[property] instanceof THREE.Color) { - instance[property].copy(this[property]) - } else if (this.hasOwnProperty(property)) { - instance[property] = this[property]; - } - } + console.warn("material type is not implemented yet: " + this.materialType); } instance.needsUpdate = true; + + return instance; } - - return instance; + }; /** * Updates the instance with the current state */ GameLib.D3.Material.prototype.updateInstance = function() { - this.instance = this.createInstance(true); -}; - -GameLib.D3.Material.prototype.clone = function() { - return _.cloneDeep(this); + this.createInstance(true); }; /** @@ -812,15 +902,13 @@ GameLib.D3.Material.prototype.toApiObject = function() { * Creates a GameLib.D3.Material from a material Object * @param graphics GameLib.D3.Graphics * @param objectMaterial Object - * @param imageFactory GameLib.D3.ImageFactory * @constructor */ -GameLib.D3.Material.FromObjectMaterial = function(graphics, objectMaterial, imageFactory) { +GameLib.D3.Material.FromObjectMaterial = function(graphics, objectMaterial) { var gameLibMaterial = new GameLib.D3.Material( graphics, - GameLib.D3.API.Material.FromObjectMaterial(objectMaterial), - imageFactory + GameLib.D3.API.Material.FromObjectMaterial(objectMaterial) ); return gameLibMaterial; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index e0f2d92..d32b592 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -2,15 +2,11 @@ * Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created * @param graphics GameLib.D3.Graphics * @param apiMesh GameLib.D3.API.Mesh - * @param imageFactory GameLib.D3.ImageFactory - * @param computeNormals Boolean * @constructor */ GameLib.D3.Mesh = function ( graphics, - apiMesh, - imageFactory, - computeNormals + apiMesh ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); @@ -23,17 +19,6 @@ GameLib.D3.Mesh = function ( return apiMesh; } - if (GameLib.Utils.UndefinedOrNull(computeNormals)) { - computeNormals = true; - } - this.computeNormals = computeNormals; - - if (GameLib.Utils.UndefinedOrNull(imageFactory)) { - console.warn('Cannot create Meshes fully without specifying an ImageFactory for downloading Textures'); - imageFactory = null; - } - this.imageFactory = imageFactory; - GameLib.D3.API.Mesh.call( this, apiMesh.id, @@ -60,23 +45,27 @@ GameLib.D3.Mesh = function ( apiMesh.renderOrder ); - this.materials = this.materials.map( - function (apiMaterial) { - - if (apiMaterial instanceof GameLib.D3.API.Material) { + 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 { return new GameLib.D3.Material( this.graphics, - apiMaterial, - this.imageFactory - ) - } else { - console.warn('API material not of instance API.Material - should be linked at runtime'); - return apiMaterial; - // throw new Error('API material not of instance API.Material'); + apiMaterial + ); } + } else { + /** + * Do Nothing + */ + } + }.bind(this)); - }.bind(this) - ); + this.materials = apiMesh.materials; if (this.skeleton) { this.skeleton = new GameLib.D3.Skeleton( @@ -315,15 +304,8 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { geometry = this.customGeometry(); } - /** - * Re-calculate normals (if we have to) - * @type {Array} - */ - if (this.computeNormals) { - geometry.computeFaceNormals(); - geometry.computeVertexNormals(); - this.computeNormals = false; - } + geometry.computeFaceNormals(); + geometry.computeVertexNormals(); if (this.meshType === GameLib.D3.Mesh.TYPE_NORMAL) { instance = new THREE.Mesh(geometry); @@ -432,7 +414,7 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.rotateZ(this.localRotation.z); } - if (this.materials.length == 1 && this.materials[0].instance) { + if (this.materials.length === 1 && this.materials[0].instance) { instance.material = this.materials[0].instance; } @@ -502,19 +484,15 @@ GameLib.D3.Mesh.prototype.toApiObject = function() { * Converts a standard object mesh to a GameLib.D3.Mesh * @param graphics GameLib.D3.Graphics * @param objectMesh {Object} - * @param computeNormals boolean to indicate whether or not to recalculate normals - * @param imageFactory GameLib.D3.ImageFactory * @constructor */ -GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh, computeNormals, imageFactory) { +GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh) { var apiMesh = GameLib.D3.API.Mesh.FromObjectMesh(objectMesh); return new GameLib.D3.Mesh( graphics, - apiMesh, - computeNormals, - imageFactory + apiMesh ); }; diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index f86b05a..aa56634 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -3,13 +3,11 @@ * created * @param graphics * @param apiScene GameLib.D3.API.Scene - * @param computeNormals * @constructor */ GameLib.D3.Scene = function ( graphics, - apiScene, - computeNormals + apiScene ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); @@ -22,16 +20,10 @@ GameLib.D3.Scene = function ( return apiScene; } - if (GameLib.Utils.UndefinedOrNull(computeNormals)) { - computeNormals = true; - } - this.computeNormals = computeNormals; - GameLib.D3.API.Scene.call( this, apiScene.id, apiScene.name, - apiScene.imageFactory, apiScene.meshes, apiScene.position, apiScene.quaternion, @@ -44,13 +36,6 @@ GameLib.D3.Scene = function ( apiScene.parentEntity ); - if (this.imageFactory instanceof GameLib.D3.API.ImageFactory) { - this.imageFactory = new GameLib.D3.ImageFactory( - this.graphics, - this.imageFactory - ); - } - this.meshes = this.meshes.map( function(apiMesh) { @@ -58,9 +43,7 @@ GameLib.D3.Scene = function ( return new GameLib.D3.Mesh( this.graphics, - apiMesh, - this.imageFactory, - this.computeNormals + apiMesh ); } else { @@ -107,49 +90,49 @@ 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.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) + // ); if (this.activeCamera instanceof GameLib.D3.API.Camera) { this.activeCamera = new GameLib.D3.Camera( @@ -162,17 +145,6 @@ GameLib.D3.Scene = function ( this.linkObjects(this.idToObject); - this.meshes.map( - function(mesh) { - mesh.updateInstance(); - mesh.materials.map( - function(material) { - material.updateInstance(); - } - ) - } - ); - this.buildIdToObject(); this.instance = this.createInstance(); @@ -214,11 +186,6 @@ GameLib.D3.Scene.prototype.createInstance = function() { */ GameLib.D3.Scene.prototype.toApiObject = function() { - var apiImageFactory = null; - if (this.imageFactory instanceof GameLib.D3.ImageFactory) { - apiImageFactory = this.imageFactory.toApiObject(); - } - var apiMeshes = this.meshes.map( function(mesh) { return mesh.toApiObject(); @@ -246,7 +213,6 @@ GameLib.D3.Scene.prototype.toApiObject = function() { return new GameLib.D3.API.Scene( this.id, this.name, - apiImageFactory, apiMeshes, this.position.toApiObject(), this.quaternion.toApiObject(), @@ -264,23 +230,18 @@ GameLib.D3.Scene.prototype.toApiObject = function() { * Converts a scene Object to a GameLib.D3.Scene object * @param graphics GameLib.D3.Graphics * @param objectScene Object - * @param computeNormals boolean to indicate whether or not to recalculate normals - * @param imageFactory GameLib.D3.ImageFactory * @returns {GameLib.D3.Scene} * @constructor */ GameLib.D3.Scene.FromObjectScene = function( graphics, - objectScene, - computeNormals, - imageFactory + objectScene ) { - var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene, imageFactory); + var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene); return new GameLib.D3.Scene( graphics, - apiScene, - computeNormals + apiScene ); }; @@ -296,9 +257,7 @@ GameLib.D3.Scene.prototype.addMesh = function(mesh) { } else if (mesh instanceof GameLib.D3.API.Mesh) { mesh = new GameLib.D3.Mesh( this.graphics, - mesh, - mesh.imageFactory, - mesh.computeNormals + mesh ) } diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 37d05c5..8956890 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -3,13 +3,11 @@ * created * @param apiTexture * @param graphics GameLib.D3.Graphics - * @param imageFactory GameLib.D3.ImageFactory * @constructor */ GameLib.D3.Texture = function( graphics, - apiTexture, - imageFactory + apiTexture ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); @@ -22,18 +20,12 @@ GameLib.D3.Texture = function( return apiTexture; } - if (GameLib.Utils.UndefinedOrNull(imageFactory)) { - console.warn('Cannot create a Texture without specifying an ImageFactory'); - imageFactory = null; - } - this.imageFactory = imageFactory; - GameLib.D3.API.Texture.call( this, apiTexture.id, apiTexture.typeId, apiTexture.name, - apiTexture.imagePath, + apiTexture.image, apiTexture.wrapS, apiTexture.wrapT, apiTexture.repeat, @@ -55,59 +47,28 @@ GameLib.D3.Texture = function( ); this.offset = new GameLib.Vector2( - graphics, + this.graphics, this.offset, this ); this.repeat = new GameLib.Vector2( - graphics, + this.graphics, this.repeat, this ); - this.imageInstance = null; + this.image = new GameLib.D3.Image( + this.graphics, + this.image + ); - this.instance = null; - - this.loadTexture(); + this.instance = this.createInstance(); }; GameLib.D3.Texture.prototype = Object.create(GameLib.D3.API.Texture.prototype); GameLib.D3.Texture.prototype.constructor = GameLib.D3.Texture; -/** - * Loads a texture from the image factory, it could already have downloaded, and then it updates the instance - * @param force boolean to force a reload - */ -GameLib.D3.Texture.prototype.loadTexture = function(force, onComplete) { - - this.imageData = this.imageFactory.loadImage(this.imagePath, force); - - this.imageData.then( - function (imageInstance){ - this.imageInstance = imageInstance; - this.instance = this.createInstance(); - if (onComplete) { - onComplete(); - } - // this.parentObjects.map( - // function(parentObject){ - // if (parentObject instanceof GameLib.D3.Material && parentObject.updateInstance) { - // parentObject.updateInstance(); - // } - // } - // ) - }.bind(this), - function onRejected(message) { - console.warn(message); - if (onComplete) { - onComplete(message); - } - } - ); -}; - /** * Texture Formats * @type {number} @@ -196,61 +157,34 @@ GameLib.D3.Texture.TEXTURE_TYPE_SPECULAR = 'specular'; * @returns {*} */ GameLib.D3.Texture.prototype.createInstance = function(update) { - - 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; + this.instance.name = this.name; + this.instance.flipY = this.flipY; + this.instance.encoding = this.encoding; + this.instance.offset.x = this.offset.x; + this.instance.offset.y = this.offset.y; + this.instance.repeat.x = this.repeat.x; + this.instance.repeat.y = this.repeat.y; } else { - 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 + 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; } - - 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; - // instance.mipmaps = this.mipmaps; - // instance.unpackAlignment = this.unpackAlignment; - // instance.premultiplyAlpha = this.premultiplyAlpha; - // instance.textureType = this.textureType; - - instance.needsUpdate = true; - - return instance; }; /** * Updates the instance with the current state */ GameLib.D3.Texture.prototype.updateInstance = function() { - this.instance = this.createInstance(true); -}; - -/** - * Clones this texture object - * @returns {*} - */ -GameLib.D3.Texture.prototype.clone = function() { - return _.cloneDeep(this); + this.createInstance(true); }; /** @@ -259,11 +193,16 @@ GameLib.D3.Texture.prototype.clone = 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, - this.imagePath, + apiImage, this.wrapS, this.wrapT, this.repeat.toApiObject(), @@ -290,19 +229,16 @@ GameLib.D3.Texture.prototype.toApiObject = function() { * Converts from an Object texture to a GameLib.D3.Texture * @param graphics GameLib.D3.Graphics * @param objectTexture Object - * @param imageFactory GameLib.D3.ImageFactory * @constructor */ GameLib.D3.Texture.FromObjectTexture = function( graphics, - objectTexture, - imageFactory + objectTexture ) { var apiTexture = GameLib.D3.API.Texture.FromObjectTexture(objectTexture); return new GameLib.D3.Texture( graphics, - apiTexture, - imageFactory + apiTexture ); }; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index ca38deb..7d05039 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -380,12 +380,88 @@ GameLib.EntityManager.prototype.onParentSceneChange = function(data) { } }; - +/** + * Change parent entity + * TODO: also change parent entity of children objects + * @param data + */ GameLib.EntityManager.prototype.onParentEntityChange = function(data) { - data.originalEntity.removeComponent(data.object); + + if (data.originalEntity) { + data.originalEntity.removeComponent(data.object); + } + 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)) + +}; + /** * */ @@ -401,6 +477,11 @@ GameLib.EntityManager.prototype.registerCallbacks = function() { this.onParentEntityChange ); + this.subscribe( + GameLib.Event.TEXTURE_LOADED, + this.onTextureLoaded + ) + }; /** diff --git a/src/game-lib-gui.js b/src/game-lib-gui.js index 50650d1..11ef588 100644 --- a/src/game-lib-gui.js +++ b/src/game-lib-gui.js @@ -678,7 +678,7 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property) { object[property] = value; } object.updateInstance(); - object.needsUpdate = true; + // object.needsUpdate = true; } ); } diff --git a/src/game-lib-system.js b/src/game-lib-system.js index a0353c0..76b7256 100644 --- a/src/game-lib-system.js +++ b/src/game-lib-system.js @@ -368,100 +368,52 @@ GameLib.System.prototype.notify = function(property, constructor, action) { }; -GameLib.System.prototype.load = function() { - - if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) { - - } - - /** - * First check if this is a client or server side request - */ - if (typeof XMLHttpRequest === 'undefined') { - console.warn('implement server side loading from API here'); - return onLoaded( - null, - new Error('not implemented') - ); - } - - var xhr = new XMLHttpRequest(); - xhr.open( - 'GET', - apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name - ); - - xhr.onreadystatechange = function(xhr) { - - return function() { - - if (xhr.readyState === 4) { - - try { - var response = JSON.parse(xhr.responseText); - } catch (e) { - return onLoaded(null, new Error('Could not load scene : ' + e.message)); - } - - if (!response.scene || response.scene.length === 0) { - return onLoaded(null, new Error('Could not load scene')); - } - - var objectScene = response.scene[0]; - - GameLib.D3.Scene.LoadScene( - graphics, - objectScene, - true, - onLoaded - ); - } - } - }(xhr); - - xhr.send(); - -}; - -/** - * Transforms raw scene data into a GameLib.D3.Scene - * @param graphics - * @param objectScene Object (as it comes from the API) - * @param computeNormals - * @param onLoaded - * @param imageFactory GameLib.D3.ImageFactory - * @constructor - */ -GameLib.D3.Scene.LoadScene = function( - graphics, - objectScene, - computeNormals, - onLoaded, - imageFactory -) { - var scene = GameLib.D3.Scene.FromObjectScene( - graphics, - objectScene, - computeNormals, - imageFactory - ); - - onLoaded(scene); -}; - -/** - * Loads a scene directly from the API - * @param graphics GameLib.D3.Graphics - * @param partialSceneObject Object {path: '', name: ''} - * @param apiUrl - * @param onLoaded - */ -GameLib.D3.Scene.LoadSceneFromApi = function( - graphics, - partialSceneObject, - apiUrl, - onLoaded -) { - +GameLib.System.prototype.load = function(onLoaded) { + // + // if (this.systemType === GameLib.System.SYSTEM_TYPE_STORAGE) { + // + // } + // + // /** + // * First check if this is a client or server side request + // */ + // if (typeof XMLHttpRequest === 'undefined') { + // console.warn('implement server side loading from API here'); + // return onLoaded( + // null, + // new Error('not implemented') + // ); + // } + // + // var xhr = new XMLHttpRequest(); + // xhr.open( + // 'GET', + // apiUrl + '/scene/load' + partialSceneObject.path + '/' + partialSceneObject.name + // ); + // + // xhr.onreadystatechange = function(xhr) { + // + // return function() { + // + // if (xhr.readyState === 4) { + // + // try { + // var response = JSON.parse(xhr.responseText); + // } catch (e) { + // return onLoaded(null, new Error('Could not load scene : ' + e.message)); + // } + // + // if (!response.scene || response.scene.length === 0) { + // return onLoaded(null, new Error('Could not load scene')); + // } + // + // var objectScene = response.scene[0]; + // + // onLoaded(GameLib.D3.Scene.FromObjectScene(graphics, objectScene)); + // } + // } + // }(xhr); + // + // xhr.send(); }; \ No newline at end of file diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index 4ed7e18..b2c343e 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -10,6 +10,15 @@ GameLib.Utils.Extend = function( } }; +/** + * Strips image extension from given path + * @param imagePath + * @constructor + */ +GameLib.Utils.StripImageExtension = function(imagePath) { + return imagePath.replace(/(\.png$|\.gif$|\.jpeg$|\.jpg$)/,'') +}; + /** * Returns id of object with the name if it exists in the array, otherwise null * @param name