From 22e0e8828d5b0990f01a3ede5b7552af84dbe098 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Mon, 19 Jun 2017 12:42:15 +0200 Subject: [PATCH] loading, checkRegister and texture creation order --- src/game-lib-d3-api-texture.js | 2 +- src/game-lib-d3-image.js | 12 ++---- src/game-lib-d3-mesh-0.js | 6 ++- src/game-lib-d3-scene.js | 1 + src/game-lib-d3-texture.js | 74 ++++++++++++++++------------------ src/game-lib-entity-manager.js | 68 ++++++++++++++++++------------- src/game-lib-gui.js | 2 +- 7 files changed, 87 insertions(+), 78 deletions(-) diff --git a/src/game-lib-d3-api-texture.js b/src/game-lib-d3-api-texture.js index 0119465..a9f48f9 100644 --- a/src/game-lib-d3-api-texture.js +++ b/src/game-lib-d3-api-texture.js @@ -172,7 +172,7 @@ GameLib.D3.API.Texture.prototype.constructor = GameLib.D3.API.Texture; GameLib.D3.API.Texture.FromObject = function(objectTexture) { return new GameLib.D3.API.Texture( objectTexture.id, - objectTexture.textureType, + objectTexture.typeId, objectTexture.name, objectTexture.image, objectTexture.wrapS, diff --git a/src/game-lib-d3-image.js b/src/game-lib-d3-image.js index 64a4554..24e4e27 100644 --- a/src/game-lib-d3-image.js +++ b/src/game-lib-d3-image.js @@ -78,23 +78,18 @@ GameLib.D3.Image.prototype.createInstance = function(update) { console.log('loading normal image'); - loader = new THREE.XHRLoader(); + loader = new THREE.ImageLoader(); loader.crossOrigin = true; loader.path = data.baseUrl; - loader.responseType = 'blob'; - loader.load( this.path + '?ts=' + Date.now(), - function (blob) { - - var image = document.createElement('img'); - image.onload = function ( e ) { URL.revokeObjectURL( image.src ); }; - image.src = URL.createObjectURL( blob ); + function (image) { this.instance = image; + this.publish( GameLib.Event.IMAGE_LOADED, { @@ -102,6 +97,7 @@ GameLib.D3.Image.prototype.createInstance = function(update) { imageInstance: this.instance } ); + }.bind(this), function(xhr) { console.log( (xhr.loaded / xhr.total * 100) + '% loaded of ' + this.name); diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 889c2ef..6a04ffe 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -458,7 +458,11 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { } if (this.materials[0] === data.material) { - this.instance.material = data.material.instance; + + if (this.instance.material !== data.material.instance) { + 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 4fcf286..e425e2b 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -97,6 +97,7 @@ GameLib.D3.Scene = function ( function(apiTexture) { if (apiTexture instanceof GameLib.D3.API.Texture) { + var texture = new GameLib.D3.Texture( this.graphics, apiTexture diff --git a/src/game-lib-d3-texture.js b/src/game-lib-d3-texture.js index 9daf3a4..e6ffd19 100644 --- a/src/game-lib-d3-texture.js +++ b/src/game-lib-d3-texture.js @@ -210,35 +210,38 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { } 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.publish( - GameLib.Event.TEXTURE_LOADED, - { - texture : this - } - ); - - return instance; - } - } + // if (this.image instanceof GameLib.D3.Image) { + // + // if (this.image.instance) { + // + // var instance = new THREE.Texture( + // this.image.instance + // ); + // instance.name = this.name; + // instance.flipY = this.flipY; + // instance.encoding = this.encoding; + // instance.offset.x = this.offset.x; + // instance.offset.y = this.offset.y; + // instance.repeat.x = this.repeat.x; + // instance.repeat.y = this.repeat.y; + // 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, @@ -315,19 +318,8 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { instance = new THREE.Texture(); } else { instance = new THREE.Texture(this.image.instance); - // instance.needsUpdate = true; - // instance.name = this.name; - // instance.flipY = this.flipY; - // instance.encoding = this.encoding; - // instance.offset.x = this.offset.x; - // instance.offset.y = this.offset.y; - // instance.repeat.x = this.repeat.x; - // instance.repeat.y = this.repeat.y; - // this.format = instance.format; } - this.instance = instance; - this.publish( GameLib.Event.TEXTURE_LOADED, { @@ -335,6 +327,8 @@ GameLib.D3.Texture.prototype.createInstance = function(update) { } ); + instance.needsUpdate = true; + this.instance = instance; } ); diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index ad0618f..84ece06 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -10,6 +10,18 @@ GameLib.EntityManager = function() { this.entities = []; + this.loading = []; + + this.dependencies = {}; + + this.subscriptions = []; + + this.register = {}; + + this.checkRegister = []; + + this.registerCallbacks(); + GameLib.Component.call( this, GameLib.Component.COMPONENT_ENTITY_MANAGER, @@ -18,16 +30,6 @@ GameLib.EntityManager = function() { }, null ); - - this.loaded = []; - - this.dependencies = {}; - - this.subscriptions = []; - - this.register = {}; - - this.registerCallbacks(); }; GameLib.EntityManager.prototype = Object.create(GameLib.Component.prototype); @@ -203,24 +205,31 @@ GameLib.EntityManager.prototype.query = function(components) { /** * Returns all actual components of all entities that contain this component - * @param constructor + * @param constructors (array of constructor or just a single constructor) */ -GameLib.EntityManager.prototype.queryComponents = function(constructor) { +GameLib.EntityManager.prototype.queryComponents = function(constructors) { - var entities = this.query([constructor]); + return this.checkRegister.reduce( + function(result, object) { + + if (constructors instanceof Array) { + constructors.map( + function(constructor) { + if (object instanceof constructor) { + result.push(object); + } + } + ); + } else { + if (object instanceof constructors) { + result.push(object); + } + } - var components = entities.reduce( - function(result, entity){ - var ecs = entity.getComponents(constructor); - ecs.map(function(ec){ - result.push(ec); - }); return result; }, [] ); - - return components; }; /** @@ -405,7 +414,12 @@ GameLib.EntityManager.prototype.link = function(component, data) { GameLib.EntityManager.prototype.componentCreated = function(data) { - console.log('component created : ' + data.component.name); + //console.log('component created : ' + data.component.name); + + /** + * Register this component immediately + */ + this.checkRegister.push(data.component); /** * If we notify ourselves - ignore it @@ -417,7 +431,7 @@ GameLib.EntityManager.prototype.componentCreated = function(data) { /** * Store this component into our 'loaded' list */ - this.loaded.push(data.component); + this.loading.push(data.component); /** * Store the dependencies too @@ -533,8 +547,8 @@ GameLib.EntityManager.prototype.componentCreated = function(data) { * Now check if all components are loaded, i.e., no more dependencies - if so - create their instance objects */ var loaded = true; - for (var i = 0; i < this.loaded.length; i++) { - if (!this.loaded[i].loaded) { + for (var i = 0; i < this.loading.length; i++) { + if (!this.loading[i].loaded) { loaded = false; break } @@ -544,10 +558,10 @@ GameLib.EntityManager.prototype.componentCreated = function(data) { * All components loaded */ if (loaded) { - this.loaded.map(function(component){ + this.loading.map(function(component){ component.instance = component.createInstance(); }); - this.loaded = []; + this.loading = []; } }; diff --git a/src/game-lib-gui.js b/src/game-lib-gui.js index 8002967..37c461e 100644 --- a/src/game-lib-gui.js +++ b/src/game-lib-gui.js @@ -878,7 +878,7 @@ GameLib.GUI.prototype.buildSelectControl = function(folder, object, property, en /** * Properties changed - rebuild the object list in the parent */ - parentObject.buildIdToObject(); + console.log('parentObject.buildIdToObject();'); /** * Properties changed - rebuild GUI