diff --git a/src/game-lib-d3-image-factory.js b/bak/game-lib-d3-image-factory.js similarity index 100% rename from src/game-lib-d3-image-factory.js rename to bak/game-lib-d3-image-factory.js diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 47636e7..6ded6ce 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -18,26 +18,30 @@ GameLib.Event.OnceSubscriptions = {}; GameLib.Event.WINDOW_RESIZE = 0x1; GameLib.Event.PARENT_SCENE_CHANGE = 0x2; GameLib.Event.PARENT_ENTITY_CHANGE = 0x3; -GameLib.Event.IMAGE_LOADED = 0x5; -GameLib.Event.TEXTURE_LOADED = 0x6; -GameLib.Event.LOAD_IMAGE = 0x7; -GameLib.Event.MATERIAL_LOADED = 0x8; -GameLib.Event.IMAGE_CHANGE = 0x9; -GameLib.Event.TEXTURE_TYPE_CHANGE = 0xa; -GameLib.Event.NEW_ENTITY = 0xb; -GameLib.Event.MATERIAL_TYPE_CHANGED = 0xc; -GameLib.Event.SAVE_COMPONENT = 0xd; -GameLib.Event.SAVE_COMPONENT_ERROR = 0xe; -GameLib.Event.COMPONENT_SAVED = 0xf; -GameLib.Event.LOAD_COMPONENT = 0x10; -GameLib.Event.LOAD_COMPONENT_ERROR = 0x11; -GameLib.Event.COMPONENT_LOADED = 0x12; -GameLib.Event.LOGGED_IN = 0x13; -GameLib.Event.COMPONENT_CREATED = 0x14; -GameLib.Event.SCENE_INSTANCE_CREATED = 0x15; -GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x16; -GameLib.Event.WORLD_INSTANCE_CREATED = 0x17; -GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x18; +GameLib.Event.IMAGE_INSTANCE_CREATED = 0x4; +GameLib.Event.LOAD_IMAGE = 0x5; +GameLib.Event.NEW_ENTITY = 0x7; +GameLib.Event.MATERIAL_TYPE_CHANGED = 0x8; +GameLib.Event.SAVE_COMPONENT = 0x9; +GameLib.Event.SAVE_COMPONENT_ERROR = 0xa; +GameLib.Event.COMPONENT_SAVED = 0xb; +GameLib.Event.LOAD_COMPONENT = 0xc; +GameLib.Event.LOAD_COMPONENT_ERROR = 0xd; +GameLib.Event.COMPONENT_LOADED = 0xe; +GameLib.Event.LOGGED_IN = 0xf; +GameLib.Event.COMPONENT_CREATED = 0x10; +GameLib.Event.SCENE_INSTANCE_CREATED = 0x11; +GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED = 0x12; +GameLib.Event.WORLD_INSTANCE_CREATED = 0x13; +GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x14; +GameLib.Event.TEXTURE_INSTANCE_CREATED = 0x15; +GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x16; +GameLib.Event.MATERIAL_INSTANCE_CREATED = 0x17; +GameLib.Event.MATERIAL_INSTANCE_UPDATED = 0x18; +GameLib.Event.MESH_INSTANCE_CREATED = 0x19; +GameLib.Event.MESH_INSTANCE_UPDATED = 0x1a; +GameLib.Event.LIGHT_INSTANCE_CREATED = 0x1b; +GameLib.Event.LIGHT_INSTANCE_UPDATED = 0x1c; /** * Subscribe to some events diff --git a/src/game-lib-d3-api-image.js b/src/game-lib-d3-api-image.js index 7a17122..08055a0 100644 --- a/src/game-lib-d3-api-image.js +++ b/src/game-lib-d3-api-image.js @@ -5,6 +5,7 @@ * @param path * @param contentType * @param size + * @param data * @param parentEntity GameLib.Entity * @constructor */ @@ -14,6 +15,7 @@ GameLib.D3.API.Image = function( path, contentType, size, + data, parentEntity ) { if (GameLib.Utils.UndefinedOrNull(id)) { @@ -54,6 +56,11 @@ GameLib.D3.API.Image = function( } this.size = size; + if (GameLib.Utils.UndefinedOrNull(data)) { + data = null; + } + this.data = data; + if (GameLib.Utils.UndefinedOrNull(parentEntity)) { parentEntity = null; } @@ -75,6 +82,7 @@ GameLib.D3.API.Image.FromObject = function(objectImage) { objectImage.path, objectImage.contentType, objectImage.size, + objectImage.data, objectImage.parentEntity ); }; diff --git a/src/game-lib-d3-image.js b/src/game-lib-d3-image.js index 0185e8b..70f91f6 100644 --- a/src/game-lib-d3-image.js +++ b/src/game-lib-d3-image.js @@ -23,6 +23,7 @@ GameLib.D3.Image = function( apiImage.path, apiImage.contentType, apiImage.size, + apiImage.data, apiImage.parentEntity ); @@ -40,131 +41,23 @@ GameLib.D3.Image.prototype.constructor = GameLib.D3.Image; * Creates a light instance * @returns {*} */ -GameLib.D3.Image.prototype.createInstance = function(update) { - - if (!this.loaded) { - console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); - return null; - } - - 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 { - - // this.subscribe( - // GameLib.Event.TEXTURE_TYPE_CHANGE, - // function(data) { - // } - // ); - - this.subscribe( - GameLib.Event.LOAD_IMAGE, - function(data) { - if ( - data.id === this.id || - data.path === this.path - ) { - var loader = null; - - if (data.textureType === GameLib.D3.Texture.TEXTURE_TYPE_NORMAL) { - - console.log('loading normal image'); - - loader = new THREE.ImageLoader(); - - loader.crossOrigin = ''; - - loader.path = data.baseUrl; - - loader.load( - this.path + '?ts=' + Date.now(), - 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 of ' + this.name); - this.size = xhr.total; - }.bind(this), - function(error) { - console.log('An image load error happened: ' + error); - } - ); - - } else { - - console.log('loading cube map'); - - loader = new THREE.CubeTextureLoader(); - - loader.crossOrigin = true; - - loader.path = data.baseUrl; - - loader.load( - [ - this.path + '?ts=' + Date.now(), - this.path + '?ts=' + Date.now(), - this.path + '?ts=' + Date.now(), - this.path + '?ts=' + Date.now(), - this.path + '?ts=' + Date.now(), - this.path + '?ts=' + Date.now() - ], - function (texture) { - - this.instance = texture.image; - this.publish( - GameLib.Event.IMAGE_LOADED, - { - imagePath: this.path, - imageInstance: this.instance, - cubeTexture : texture - } - ); - }.bind(this) - ); - } - } - } - ); - - return null; - } +GameLib.D3.Image.prototype.createInstance = function() { + console.log('create image instance'); + return null; }; /** * Updates the instance with the current state */ GameLib.D3.Image.prototype.updateInstance = function() { - this.createInstance(true); + console.log('update image instance'); }; /** * * @returns {GameLib.D3.API.Image} */ -GameLib.D3.Image.prototype.toApiObject = function(save) { - - if (GameLib.Utils.UndefinedOrNull(save)) { - save = false; - } +GameLib.D3.Image.prototype.toApiObject = function() { var apiImage = new GameLib.D3.API.Image( this.id, @@ -172,6 +65,7 @@ GameLib.D3.Image.prototype.toApiObject = function(save) { this.path, this.contentType, this.size, + this.data, GameLib.Utils.IdOrNull(this.parentEntity) ); @@ -180,25 +74,13 @@ GameLib.D3.Image.prototype.toApiObject = function(save) { /** * @param graphics - * @param objectLight + * @param objectImage * @returns {GameLib.D3.Image} * @constructor */ -GameLib.D3.Image.FromObject = function(graphics, objectLight) { +GameLib.D3.Image.FromObject = function(graphics, objectImage) { return new GameLib.D3.Image( graphics, - GameLib.D3.API.Image.FromObject(objectLight) + GameLib.D3.API.Image.FromObject(objectImage) ); }; - - -GameLib.D3.Image.prototype.load = function(baseUrl, path, textureType) { - GameLib.Event.Emit( - GameLib.Event.LOAD_IMAGE, - { - baseUrl : baseUrl, - path : path, - textureType : textureType - } - ); -}; \ No newline at end of file diff --git a/src/game-lib-d3-material.js b/src/game-lib-d3-material.js index aeef8c2..d8e0df9 100644 --- a/src/game-lib-d3-material.js +++ b/src/game-lib-d3-material.js @@ -729,146 +729,12 @@ GameLib.D3.Material.prototype.createInstance = function(update) { instance.needsUpdate = true; - this.subscribe( - GameLib.Event.TEXTURE_LOADED, - function (data) { - - var modified = false; - - /** - * We also need to check if the image of the texture is assigned - - * if not we should disable the map - */ - if (this.alphaMap === data.texture) { - - if (data.texture.image) { - this.instance.alphaMap = data.texture.instance; - } else { - this.instance.alphaMap = null; - } - - modified = true; - } - if (this.aoMap === data.texture) { - - if (data.texture.image) { - this.instance.aoMap = data.texture.instance; - } else { - this.instance.aoMap = null; - } - modified = true; - } - if (this.bumpMap === data.texture) { - - if (data.texture.image) { - this.instance.bumpMap = data.texture.instance; - } else { - this.instance.bumpMap = null; - } - modified = true; - } - if (this.diffuseMap === data.texture) { - - if (data.texture.image) { - this.instance.map = data.texture.instance; - } else { - this.instance.map = null; - } - modified = true; - } - if (this.displacementMap === data.texture) { - - if (data.texture.image) { - this.instance.displacementMap = data.texture.instance; - } else { - this.instance.displacementMap = null; - } - modified = true; - } - if (this.emissiveMap === data.texture) { - - if (data.texture.image) { - this.instance.emissiveMap = data.texture.instance; - } else { - this.instance.emissiveMap = null; - } - modified = true; - } - if (this.environmentMap === data.texture) { - - if (data.texture.image) { - this.instance.envMap = data.texture.instance; - } else { - this.instance.envMap = null; - } - modified = true; - } - if (this.lightMap === data.texture) { - - if (data.texture.image) { - this.instance.lightMap = data.texture.instance; - } else { - this.instance.lightMap = null; - } - modified = true; - } - if (this.metalnessMap === data.texture) { - - if (data.texture.image) { - this.instance.metalnessMap = data.texture.instance; - } else { - this.instance.metalnessMap = null; - } - modified = true; - } - if (this.normalMap === data.texture) { - - if (data.texture.image) { - this.instance.normalMap = data.texture.instance; - } else { - this.instance.normalMap = null; - } - modified = true; - } - if (this.roughnessMap === data.texture) { - - if (data.texture.image) { - this.instance.roughnessMap = data.texture.instance; - } else { - this.instance.roughnessMap = null; - } - modified = true; - } - if (this.specularMap === data.texture) { - - if (data.texture.image) { - this.instance.specularMap = data.texture.instance; - } else { - this.instance.specularMap = null; - } - modified = true; - } - - if (modified) { - - this.publish( - GameLib.Event.MATERIAL_LOADED, - { - material: this - } - ); - - this.instance.needsUpdate = true; - } - } - ); - this.instance = instance; this.updateTextures(); return instance; -} +}; /** * Updates the instance with the current state diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 9424c5a..071c7c3 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -408,30 +408,10 @@ GameLib.D3.Mesh.prototype.createInstanceDefaults = function(instance) { instance.renderOrder = this.renderOrder; - this.subscribe( - GameLib.Event.MATERIAL_LOADED, - function (data) { - - if (this.instance.material === data.material.instance) { - this.instance.geometry.uvsNeedUpdate = true; - return; - } - - if (this.materials[0] === data.material) { - - if (this.instance.material !== data.material.instance) { - this.instance.material = data.material.instance; - } - - this.instance.geometry.uvsNeedUpdate = true; - } - } - ); - this.subscribe( GameLib.Event.MATERIAL_TYPE_CHANGED, function (data) { - if (this.materials[0].id === data.material.id) { + if (this.materials[0] === data.material) { this.instance.material = data.material.instance; this.instance.geometry.uvsNeedUpdate = true; } diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 49db6ad..f713a1b 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -186,39 +186,14 @@ GameLib.D3.Scene.prototype.createInstance = function() { var instance = new THREE.Scene(); - instance.name = this.name; + instance.name = this.name; - instance.position = this.position.instance; + instance.position = this.position.instance; - instance.scale = this.scale.instance; + instance.scale = this.scale.instance; instance.quaternion = this.quaternion.instance; - // this.meshes.map( - // function(mesh){ - // if (mesh instanceof GameLib.D3.Mesh) { - // if (GameLib.Utils.UndefinedOrNull(mesh.instance)){ - // this.subscribe() - // } - // instance.add(mesh.instance); - // } else { - // console.log('Unable to add mesh which should be loaded at this point'); - // } - // }.bind(this) - // ); - // - // this.lights.map(function(light){ - // if (light instanceof GameLib.D3.Light) { - // if (GameLib.Utils.UndefinedOrNull(light.instance)){ - // console.warn('No light instance'); - // throw new Error('No light instance'); - // } - // instance.add(light.instance) - // } else { - // console.log('Unable to add light which should be loaded at this point'); - // } - // }); - return instance; }; @@ -226,40 +201,28 @@ GameLib.D3.Scene.prototype.createInstance = function() { * Converts a GameLib.D3.Scene to a GameLib.D3.API.Scene * @returns {GameLib.D3.API.Scene} */ -GameLib.D3.Scene.prototype.toApiObject = function(save) { +GameLib.D3.Scene.prototype.toApiObject = function() { var apiMeshes = this.meshes.map( function(mesh) { - if (save) { - mesh.save(); - } return mesh.id; } ); var apiLights = this.lights.map( function(light) { - if (save) { - light.save(); - } return light.id; } ); var apiTextures = this.textures.map( function(texture) { - if (save) { - texture.save(); - } return texture.id; } ); var apiMaterials = this.materials.map( function(material) { - if (save) { - material.save(); - } return material.id; } ); @@ -307,7 +270,9 @@ GameLib.D3.Scene.FromObject = function( GameLib.D3.Scene.prototype.addObject = function(object) { if (object instanceof GameLib.D3.Mesh) { - this.meshes.push(object); + if (this.meshes.indexOf(object) === -1) { + this.meshes.push(object); + } } else if (object instanceof GameLib.D3.API.Mesh) { object = new GameLib.D3.Mesh( this.graphics, @@ -317,7 +282,9 @@ GameLib.D3.Scene.prototype.addObject = function(object) { } if (object instanceof GameLib.D3.Light) { - this.lights.push(object); + if (this.lights.indexOf(object) === -1) { + this.lights.push(object); + } } else if (object instanceof GameLib.D3.API.Light) { object = new GameLib.D3.Light( this.graphics, @@ -328,89 +295,16 @@ GameLib.D3.Scene.prototype.addObject = function(object) { object.parentScene = this; - if (!this.instance) { - console.warn('No Scene Instance'); - - var index = this.sceneInstanceSubscriptions.length; - - this.sceneInstanceSubscriptions.push( - this.subscribe( - GameLib.Event.SCENE_INSTANCE_CREATED, - function(__index, __object, __scene) { - return function(data) { - - if (data.scene.id === __scene.id) { - - if (!data.scene.instance) { - console.warn('Invalid Scene ' + data.scene); - throw new Error('Invalid Scene' + data.scene); - } - - if (__object.instance) { - data.scene.instance.add(__object.instance); - } else { - /** - * We should alread have a subscription for a non-loaded object instance below, - * it will fire soon - */ - } - - /** - * Now remove the subscription to this event - */ - __scene.sceneInstanceSubscriptions[__index].remove(); - } - } - }(index, object, this) - ) - ); - - } - - if (!object.instance) { - - console.warn('No Object Instance'); - - var o = this.objectInstanceSubscriptions.length; - - this.objectInstanceSubscriptions.push( - this.subscribe( - GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED, - function(__index, __scene) { - return function (data) { - - if (!data.object.instance) { - console.warn('Invalid Object or wrong Event Emitted'); - throw new Error('Invalid Object or wrong Event Emitted'); - } - - if (!__scene.instance) { - /** - * This is ok, we will have the opportunity to add a new object once the scene is created - */ - } else { - /** - * Add the object to the scene - */ - __scene.instance.add(data.object.instance); - } - - /** - * Remove the listener for this event - */ - __scene.objectInstanceSubscriptions[__index].remove(); - } - }(o, this) - ) - ); - } - if ( this.instance && object.instance ) { - this.instance.add(object.instance); + if (this.instance.children.indexOf(object.instance) === -1) { + this.instance.add(object.instance); + } } + + }; /** @@ -432,11 +326,11 @@ GameLib.D3.Scene.prototype.removeObject = function(object) { this.lights.splice(index, 1); } } else { - console.warn('Cannot remove this mesh - what is this ?' + object.toString()) - return; + console.warn('Cannot remove this object - what is this ?' + object.toString()); + return; } - if (this.instance) { + if (this.instance.children.indexOf(object.instance) !== -1) { this.instance.remove(object.instance); } else { console.warn('no scene instance'); diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index e6ffd19..7bec341 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -149,215 +149,135 @@ GameLib.D3.Texture.TYPE_RGBD_ENCODING = 3006; // MAXRANGE IS 256. GameLib.D3.Texture.TEXTURE_TYPE_NORMAL = 0x1; GameLib.D3.Texture.TEXTURE_TYPE_CUBE = 0x2; - -// GameLib.D3.Texture.TEXTURE_TYPE_ALPHA = 'alpha'; -// GameLib.D3.Texture.TEXTURE_TYPE_AO = 'ao'; -// GameLib.D3.Texture.TEXTURE_TYPE_BUMP = 'bump'; -// GameLib.D3.Texture.TEXTURE_TYPE_DIFFUSE = 'diffuse'; -// GameLib.D3.Texture.TEXTURE_TYPE_DISPLACEMENT = 'displacement'; -// GameLib.D3.Texture.TEXTURE_TYPE_EMISSIVE = 'emissive'; -// GameLib.D3.Texture.TEXTURE_TYPE_ENVIRONMENT = 'environment'; -// GameLib.D3.Texture.TEXTURE_TYPE_LIGHT = 'light'; -// GameLib.D3.Texture.TEXTURE_TYPE_METALNESS = 'metalness'; -// GameLib.D3.Texture.TEXTURE_TYPE_NORMAL = 'normal'; -// GameLib.D3.Texture.TEXTURE_TYPE_ROUGHNESS = 'roughness'; -// GameLib.D3.Texture.TEXTURE_TYPE_SPECULAR = 'specular'; - /** * Creates an instance of our texture object - * @param update * @returns {*} */ -GameLib.D3.Texture.prototype.createInstance = function(update) { +GameLib.D3.Texture.prototype.createInstance = function() { - if (!this.loaded) { - console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name); - return null; - } - - if (update) { - - if (!this.instance) { - console.warn('trying to update a non-existent texture instance'); - return; + if (!(this.image instanceof GameLib.D3.Image)) { + console.warn('The image associated with this texture has not been setup properly - objects linked?'); + return null; } - if (GameLib.Utils.UndefinedOrNull(this.image.instance)) { - console.warn('trying to update while no image instance'); - return; - } + var instance = null; - 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; - this.instance.mapping = this.mapping; - this.instance.format = this.format; - this.instance.wrapS = this.wrapS; - this.instance.wrapT = this.wrapT; + if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CUBE) { - this.publish( - GameLib.Event.TEXTURE_LOADED, - { - texture : this - } - ); + if (this.image.instance) { - this.instance.needsUpdate = true; - - } else { - - // 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; - // instance.mapping = this.mapping; - // instance.format = this.format; - // instance.wrapS = this.wrapS; - // instance.wrapT = this.wrapT; - // - // this.instance = instance; - // - // this.publish( - // GameLib.Event.TEXTURE_LOADED, - // { - // texture : this - // } - // ); - // - // this.instance.needsUpdate = true; - // return instance; - // } - // } - - this.subscribe( - GameLib.Event.IMAGE_LOADED, - function (data) { - - /** - * Only work with images that belong to this texture - */ - if (data.imagePath === this.image.path) { - - if (!(this.image instanceof GameLib.D3.Image)) { - if (!data.cubeTexture) { - console.warn('The image associated with this texture has not been setup properly - objects linked?'); - return; - } - } - - var instance = null; - - if (data.cubeTexture) { - - instance = data.cubeTexture; - - if (this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING && - this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING - ) { - this.mapping = GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING; - } - - } else { - - instance = new THREE.Texture( - data.imageInstance - ); - - if (this.mapping !== GameLib.D3.Texture.TYPE_UV_MAPPING) { - this.mapping = GameLib.D3.Texture.TYPE_UV_MAPPING; - } - } - - 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.mapping = this.mapping; - instance.format = this.format; - instance.wrapS = this.wrapS; - instance.wrapT = this.wrapT; - - this.instance = instance; - - this.publish( - GameLib.Event.TEXTURE_LOADED, - { - texture : this - } - ); - - this.instance.needsUpdate = true; - } - } - ); - - this.subscribe( - GameLib.Event.IMAGE_CHANGE, - function (data) { - - var instance = null; - - if (this.image === null) { - instance = new THREE.Texture(); - } else { - instance = new THREE.Texture(this.image.instance); - } - - this.publish( - GameLib.Event.TEXTURE_LOADED, - { - texture : this - } + instance = new THREE.CubeTexture( + [ + this.image.instance, + this.image.instance, + this.image.instance, + this.image.instance, + this.image.instance, + this.image.instance + ] ); - instance.needsUpdate = true; - this.instance = instance; + } else { + return null; } - ); - return null; - } + if (this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING && + this.mapping !== GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING + ) { + this.mapping = GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING; + } + + } else { + + if (this.image.instance) { + instance = new THREE.Texture( + this.image.instance + ); + instance.needsUpdate = true; + } else { + return null; + } + + if (this.mapping !== GameLib.D3.Texture.TYPE_UV_MAPPING) { + this.mapping = GameLib.D3.Texture.TYPE_UV_MAPPING; + } + } + + 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.mapping = this.mapping; + instance.format = this.format; + instance.wrapS = this.wrapS; + instance.wrapT = this.wrapT; + + return instance; }; /** * Updates the instance with the current state */ GameLib.D3.Texture.prototype.updateInstance = function() { - this.createInstance(true); + + if (!this.instance) { + console.error('no texture instance'); + return; + } + + if (!this.image.instance) { + console.error('no image instance'); + return; + } + + if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_NORMAL) { + if (this.instance.image !== this.image.instance) { + this.instance = new THREE.Texture( + this.image.instance + ); + this.mapping = this.instance.mapping; + } + } else if (this.typeId === GameLib.D3.Texture.TEXTURE_TYPE_CUBE) { + if (this.instance.image[0] !== this.image.instance) { + this.instance = new THREE.CubeTexture( + [ + this.image.instance, + this.image.instance, + this.image.instance, + this.image.instance, + this.image.instance, + this.image.instance + ] + ); + this.mapping = this.instance.mapping; + } + } + + 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; + this.instance.mapping = this.mapping; + this.instance.format = this.format; + this.instance.wrapS = this.wrapS; + this.instance.wrapT = this.wrapT; + this.instance.needsUpdate = true; }; /** * Converts a GameLib.D3.Texture to a GameLib.D3.API.Texture * @returns {GameLib.D3.API.Texture} */ -GameLib.D3.Texture.prototype.toApiObject = function(save) { - - if (GameLib.Utils.UndefinedOrNull(save)) { - save = false; - } +GameLib.D3.Texture.prototype.toApiObject = function() { var apiImage = null; if (this.image) { - if (save) { - this.image.save(); - } apiImage = this.image.id } diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index 2384fa7..90a5638 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -16,8 +16,6 @@ GameLib.EntityManager = function() { this.subscriptions = []; - this.register = {}; - this.checkRegister = []; this.registerCallbacks(); @@ -434,7 +432,18 @@ GameLib.EntityManager.prototype.componentCreated = function() { /** * Check if we already processed a component on which this component is dependent */ - if (this.register.hasOwnProperty(id)) { + + var processedComponent = this.checkRegister.reduce( + function(result, component){ + if (component.id === id){ + result = component; + } + return result; + }, + null + ); + + if (processedComponent) { /** * Remove this dependency from the dependency list @@ -448,7 +457,7 @@ GameLib.EntityManager.prototype.componentCreated = function() { /** * Now link the component */ - this.link(data.component, {component: this.register[id]}); + this.link(data.component, {component: processedComponent}); } else { @@ -473,9 +482,8 @@ GameLib.EntityManager.prototype.componentCreated = function() { /** * We don't know about any dependencies on this object - but maybe a component still - * has to load which has dependencies to this object + * has to load which has dependencies to this object - this object is in the 'checkRegister' */ - this.register[data.component.id] = data.component; } else { @@ -555,13 +563,23 @@ GameLib.EntityManager.prototype.componentCreated = function() { GameLib.EntityManager.prototype.emitInstanceEvents = function (component) { if ( - component instanceof GameLib.D3.Mesh || + component instanceof GameLib.D3.Mesh + ) { + GameLib.Event.Emit( + GameLib.Event.MESH_INSTANCE_CREATED, + { + mesh: component + } + ) + } + + if ( component instanceof GameLib.D3.Light ) { GameLib.Event.Emit( - GameLib.Event.SCENE_OBJECT_INSTANCE_CREATED, + GameLib.Event.LIGHT_INSTANCE_CREATED, { - object: component + light: component } ) } @@ -581,12 +599,23 @@ GameLib.EntityManager.prototype.emitInstanceEvents = function (component) { component instanceof GameLib.D3.Material ) { GameLib.Event.Emit( - GameLib.Event.MATERIAL_LOADED, + GameLib.Event.MATERIAL_INSTANCE_CREATED, { material: component } ); } + + if ( + component instanceof GameLib.D3.Texture + ) { + GameLib.Event.Emit( + GameLib.Event.TEXTURE_INSTANCE_CREATED, + { + texture: component + } + ); + } }; /** diff --git a/src/game-lib-entity.js b/src/game-lib-entity.js index b346c4e..4284f8d 100644 --- a/src/game-lib-entity.js +++ b/src/game-lib-entity.js @@ -163,20 +163,12 @@ GameLib.Entity.prototype.removeComponent = function(component) { } } - /** + /** * Now we remove the boss entity - * @type {null} - */ - component.parentEntity = null; + * @type {null} + */ + component.parentEntity = null; - var index = this.components.indexOf(component); - - if (index === -1) { - console.log('failed to remove component : ', component); - return false; - } - - this.components.splice(index, 1); return true; }; diff --git a/src/game-lib-gui.js b/src/game-lib-gui.js index 0bb92ed..c6e7079 100644 --- a/src/game-lib-gui.js +++ b/src/game-lib-gui.js @@ -775,28 +775,7 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property, entityMa } else { object[property] = value; } - - if (object instanceof GameLib.D3.Texture && property === 'typeId') { - - var imageFactory = entityManager.queryComponents(GameLib.D3.ImageFactory)[0]; - - if (object.image) { - object.publish( - GameLib.Event.LOAD_IMAGE, - { - id : object.image.id, - textureType : object[property], - baseUrl : imageFactory.baseUrl - } - ) - } else { - console.log('changing texture type but no image specified') - } - - } - object.updateInstance(); - // object.needsUpdate = true; } ); } @@ -915,19 +894,8 @@ GameLib.GUI.prototype.buildSelectControl = function(folder, object, property, en } ); - } else if (property === 'image') { - - GameLib.Event.Emit( - GameLib.Event.IMAGE_CHANGE, - { - originalImage: this.initialValue, - newImage: object[property], - object: object, - entityManager: entityManager - } - ); - - } else { + } + else { /** * Old way of doing things */ diff --git a/src/game-lib-system-storage.js b/src/game-lib-system-storage.js index fe0ebae..c3093ac 100644 --- a/src/game-lib-system-storage.js +++ b/src/game-lib-system-storage.js @@ -1,35 +1,71 @@ /** - * System takes care of updating all the entities (based on their component data) - * @param graphics + * Storage System takes care loading and linking components and dependencies * @param apiSystem GameLib.API.System * @param apiUrl * @param token + * @param apiUploadUrl + * @param onImageLoaded + * @param onImageProgress + * @param onImageError * @constructor */ GameLib.System.Storage = function( apiSystem, apiUrl, - token + token, + apiUploadUrl, + onImageLoaded, + onImageProgress, + onImageError ) { - - if (GameLib.Utils.UndefinedOrNull(apiUrl)) { - console.warn('Need an API URL for a storage system'); - } - this.apiUrl = apiUrl; - GameLib.System.call( this, apiSystem ); + if (GameLib.Utils.UndefinedOrNull(apiUrl)) { + console.warn('Need an API URL for a storage system'); + apiUrl = ''; + } + this.apiUrl = apiUrl; + if (GameLib.Utils.UndefinedOrNull(token)) { token = null; } this.token = token; + if (GameLib.Utils.UndefinedOrNull(apiUploadUrl)) { + console.warn('Need an API Upload URL for a storage system'); + apiUploadUrl = ''; + } + this.apiUploadUrl = apiUploadUrl; + + if (GameLib.Utils.UndefinedOrNull(onImageLoaded)) { + onImageLoaded = null; + } + this.onImageLoaded = onImageLoaded; + + if (GameLib.Utils.UndefinedOrNull(onImageProgress)) { + onImageProgress = null; + } + this.onImageProgress = onImageProgress; + + if (GameLib.Utils.UndefinedOrNull(onImageError)) { + onImageError = null; + } + this.onImageError = onImageError; + this.loginSubscription = null; - this.saveSubscription = null; - this.loadSubscription = null; + this.saveSubscription = null; + this.loadSubscription = null; + this.loadImageSubscription = null; + this.meshInstanceCreatedSubscription = null; + this.lightInstanceCreatedSubscription = null; + this.sceneInstanceCreatedSubscription = null; + this.imageInstanceCreatedSubscription = null; + this.textureInstanceCreatedSubscription = null; + this.materialInstanceCreatedSubscription = null; + }; GameLib.System.Storage.prototype = Object.create(GameLib.System.prototype); @@ -52,7 +88,42 @@ GameLib.System.Storage.prototype.start = function() { this.loadSubscription = this.subscribe( GameLib.Event.LOAD_COMPONENT, this.load - ) + ); + + this.loadImageSubscription = this.subscribe( + GameLib.Event.LOAD_IMAGE, + this.loadImage + ); + + this.sceneInstanceCreatedSubscription = this.subscribe( + GameLib.Event.SCENE_INSTANCE_CREATED, + this.sceneInstanceCreated + ); + + this.meshInstanceCreatedSubscription = this.subscribe( + GameLib.Event.MESH_INSTANCE_CREATED, + this.meshInstanceCreated + ); + + this.lightInstanceCreatedSubscription = this.subscribe( + GameLib.Event.LIGHT_INSTANCE_CREATED, + this.lightInstanceCreated + ); + + this.imageInstanceCreatedSubscription = this.subscribe( + GameLib.Event.IMAGE_INSTANCE_CREATED, + this.imageInstanceCreated + ); + + this.textureInstanceCreatedSubscription = this.subscribe( + GameLib.Event.TEXTURE_INSTANCE_CREATED, + this.textureInstanceCreated + ); + + this.materialInstanceCreatedSubscription = this.subscribe( + GameLib.Event.MATERIAL_INSTANCE_CREATED, + this.materialInstanceCreated + ); }; /** @@ -170,9 +241,415 @@ GameLib.System.Storage.prototype.load = function(data) { xhr.send(); }; +GameLib.System.Storage.prototype.loadImage = function(data) { + + console.log('loading image : ' + data.image.name); + + var onLoaded = this.onImageLoaded; + + var onProgress = this.onImageProgress;; + + var onError = this.onImageError; + + var image = data.image; + + var url = this.apiUploadUrl + image.path + '?ts=' + Date.now(); + + var preflight = new XMLHttpRequest(); + + preflight.withCredentials = true; + + preflight.open( + 'OPTIONS', + url + ); + + preflight.setRequestHeader('Content-Type', 'application/json'); + + preflight.onload = function() { + + var xhr = new XMLHttpRequest(); + + xhr.withCredentials = true; + + xhr.open('GET', url); + + xhr.setRequestHeader('Content-Type', image.contentType); + + xhr.responseType = 'blob'; + + xhr.onload = function() { + + try { + if (this.response.type !== 'application/json') { + var url = window.URL.createObjectURL(this.response); + } else { + if (onError) { + onError(image, {message:'Image not found'}); + return; + } + } + } catch (error) { + if (onError) { + onError(image, {message:'Image not found'}); + return; + } + } + var img = document.createElement('img'); + + img.onload = function() { + + window.URL.revokeObjectURL(url); + + image.instance = img; + image.publish( + GameLib.Event.IMAGE_INSTANCE_CREATED, + { + image: image + } + ); + + if (onLoaded) { + onLoaded(image); + } + }; + + img.src = url; + }; + + xhr.onprogress = function(progressEvent) { + + var progress = 0; + + if (progressEvent.total !== 0) { + progress = Number(progressEvent.loaded / progressEvent.total); + progress *= 100; + } + + if (onProgress) { + onProgress(image, progress); + } + + image.size = progressEvent.total; + }; + + xhr.onerror = function(error) { + console.warn('image load failed for image ' + image.name); + if (onError) { + onError(image, error) + } + }; + + xhr.send(); + }; + + preflight.onerror = function(error) { + console.warn('image pre-flight request failed for image ' + image.name); + if (onError) { + onError(image, error); + } + }; + + preflight.send(); +}; + +GameLib.System.Storage.prototype.meshInstanceCreated = function(data) { + + var scenes = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Scene]); + + scenes.map(function(scene){ + if (data.mesh.parentScene === scene) { + scene.addObject(data.mesh); + } + }); + +}; + +GameLib.System.Storage.prototype.lightInstanceCreated = function(data) { + + var scenes = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Scene]); + + scenes.map(function(scene){ + if (data.light.parentScene === scene) { + scene.addObject(data.light); + } + }); + +}; + +GameLib.System.Storage.prototype.sceneInstanceCreated = function(data) { + + var scene = data.scene; + + scene.images.map( + function(image){ + GameLib.Event.Emit( + GameLib.Event.LOAD_IMAGE, + { + onLoaded : function(image) { + if (this.onImageLoaded) { + this.onImageLoaded(image); + } + }, + onProgress : function(image, progress) { + if (this.onImageProgress) { + this.onImageProgress(image, progress); + } + }, + onError : function(image, error) { + if (this.onImageError) { + this.onImageError(image, error); + } + }, + image : image + } + ); + }.bind(this) + ); + + /** + * Add all meshes and lights + */ + var object = GameLib.EntityManager.Instance.queryComponents([ + GameLib.D3.Mesh, + GameLib.D3.Light + ]); + object.map(function(object){ + if ( + object.parentScene === scene + ) { + scene.addObject(object); + } + }); + +}; + +GameLib.System.Storage.prototype.imageInstanceCreated = function(data) { + + var textures = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Texture]); + + textures.map( + function(texture) { + /** + * Only work with images that belong to this texture + */ + if ( + texture.image === data.image + ) { + /** + * Update instance, if already an instance + */ + if (texture.instance) { + texture.updateInstance(); + GameLib.Event.Emit( + GameLib.Event.TEXTURE_INSTANCE_UPDATED, + { + texture : texture + } + ) + } else { + /** + * Create a new instance + */ + texture.instance = texture.createInstance(); + GameLib.Event.Emit( + GameLib.Event.TEXTURE_INSTANCE_CREATED, + { + texture : texture + } + ) + } + } + } + ); +}; + +GameLib.System.Storage.prototype.textureInstanceCreated = function(data) { + + var materials = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Material]); + + materials.map( + + function(material) { + + if (!material.instance) { + console.log('No material instance for ' + material.name); + return; + } + + var modified = false; + + /** + * We also need to check if the image of the texture is assigned - + * if not we should disable the map + */ + if (material.alphaMap === data.texture) { + + if (data.texture.image) { + material.instance.alphaMap = data.texture.instance; + } else { + material.instance.alphaMap = null; + } + + modified = true; + } + if (material.aoMap === data.texture) { + + if (data.texture.image) { + material.instance.aoMap = data.texture.instance; + } else { + material.instance.aoMap = null; + } + modified = true; + } + if (material.bumpMap === data.texture) { + + if (data.texture.image) { + material.instance.bumpMap = data.texture.instance; + } else { + material.instance.bumpMap = null; + } + modified = true; + } + if (material.diffuseMap === data.texture) { + + if (data.texture.image) { + material.instance.map = data.texture.instance; + } else { + material.instance.map = null; + } + modified = true; + } + if (material.displacementMap === data.texture) { + + if (data.texture.image) { + material.instance.displacementMap = data.texture.instance; + } else { + material.instance.displacementMap = null; + } + modified = true; + } + if (material.emissiveMap === data.texture) { + + if (data.texture.image) { + material.instance.emissiveMap = data.texture.instance; + } else { + material.instance.emissiveMap = null; + } + modified = true; + } + if (material.environmentMap === data.texture) { + + if (data.texture.image) { + material.instance.envMap = data.texture.instance; + } else { + material.instance.envMap = null; + } + modified = true; + } + if (material.lightMap === data.texture) { + + if (data.texture.image) { + material.instance.lightMap = data.texture.instance; + } else { + material.instance.lightMap = null; + } + modified = true; + } + if (material.metalnessMap === data.texture) { + + if (data.texture.image) { + material.instance.metalnessMap = data.texture.instance; + } else { + material.instance.metalnessMap = null; + } + modified = true; + } + if (material.normalMap === data.texture) { + + if (data.texture.image) { + material.instance.normalMap = data.texture.instance; + } else { + material.instance.normalMap = null; + } + modified = true; + } + if (material.roughnessMap === data.texture) { + + if (data.texture.image) { + material.instance.roughnessMap = data.texture.instance; + } else { + material.instance.roughnessMap = null; + } + modified = true; + } + if (material.specularMap === data.texture) { + + if (data.texture.image) { + material.instance.specularMap = data.texture.instance; + } else { + material.instance.specularMap = null; + } + modified = true; + } + + if (modified) { + material.updateInstance(); + GameLib.Event.Emit( + GameLib.Event.MATERIAL_INSTANCE_UPDATED, + { + material : material + } + ) + } + } + ); +}; + +GameLib.System.Storage.prototype.materialInstanceCreated = function(data) { + + var meshes = GameLib.EntityManager.Instance.queryComponents([GameLib.D3.Mesh]); + + meshes.map(function(mesh){ + + if (!mesh.instance) { + return; + } + + /** + * Only work with materials assigned to us + */ + if (mesh.materials[0] !== data.material) { + return; + } + + if (mesh.instance.material === data.material.instance) { + //mesh.instance.geometry.uvsNeedUpdate = true; + return; + } + + if (mesh.materials[0] === data.material) { + + if (mesh.instance.material !== data.material.instance) { + mesh.instance.material = data.material.instance; + } + + //mesh.instance.geometry.uvsNeedUpdate = true; + } + }); + +}; + GameLib.System.Storage.prototype.stop = function() { this.loginSubscription.remove(); this.loadSubscription.remove(); this.saveSubscription.remove(); + this.loadImageSubscription.remove(); + this.meshInstanceCreatedSubscription.remove(); + this.lightInstanceCreatedSubscription.remove(); + this.sceneInstanceCreatedSubscription.remove(); + this.imageInstanceCreatedSubscription.remove(); + this.textureInstanceCreatedSubscription.remove(); + this.materialInstanceCreatedSubscription.remove(); };