From f701370f5d12dbf9c72ad1dacd5f189358f1e1e1 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Tue, 31 Jan 2017 11:37:55 +0100 Subject: [PATCH] textures and materials stored to scene --- src/game-lib-d3-api-scene.js | 34 +++++++++++++++++++++++ src/game-lib-d3-scene.js | 53 ++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index 424ad7e..0963e22 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -20,6 +20,8 @@ GameLib.D3.API.Scene = function( scale, parentGameId, lights, + textures, + materials, parentEntity ) { GameLib.Component.call( @@ -28,6 +30,8 @@ GameLib.D3.API.Scene = function( { 'meshes' : [GameLib.D3.Mesh], 'lights' : [GameLib.D3.Light], + 'textures' : [GameLib.D3.Texture], + 'materials' : [GameLib.D3.Material], 'imageFactory' : GameLib.D3.ImageFactory }, false, @@ -74,6 +78,16 @@ GameLib.D3.API.Scene = function( } this.lights = lights; + if (GameLib.Utils.UndefinedOrNull(textures)) { + textures = []; + } + this.textures = textures; + + if (GameLib.Utils.UndefinedOrNull(materials)) { + materials = []; + } + this.materials = materials; + }; GameLib.D3.API.Scene.prototype = Object.create(GameLib.Component.prototype); @@ -88,6 +102,8 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { var apiMeshes = []; var apiLights = []; + var apiTextures = []; + var apiMaterials = []; var apiPosition = new GameLib.API.Vector3(); var apiQuaternion = new GameLib.API.Quaternion(); @@ -109,6 +125,22 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { ) } + if (objectScene.textures) { + apiTextures = objectScene.textures.map( + function(objectTexture) { + return GameLib.D3.API.Texture.FromObjectTexture(objectTexture) + } + ) + } + + if (objectScene.materials) { + apiMaterials = objectScene.materials.map( + function(objectMaterial) { + return GameLib.D3.API.Material.FromObjectMaterial(objectMaterial) + } + ) + } + if (objectScene.position) { apiPosition = GameLib.API.Vector3.FromObjectVector(objectScene.position); } @@ -130,6 +162,8 @@ GameLib.D3.API.Scene.FromObjectScene = function(objectScene) { apiScale, objectScene.parentGameId, apiLights, + apiTextures, + apiMaterials, objectScene.parentEntity ); diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 0b76d05..87a89a7 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -35,8 +35,8 @@ GameLib.D3.Scene = function ( apiScene.scale, apiScene.parentGameId, apiScene.lights, + apiScene.textures, apiScene.materials, - apiScene.textures, apiScene.parentEntity ); @@ -102,6 +102,42 @@ GameLib.D3.Scene = function ( }.bind(this) ); + this.textures = this.textures.map( + function(apiTexture) { + + if (apiTexture instanceof GameLib.D3.API.Texture) { + return new GameLib.D3.Texture( + this.graphics, + apiTexture, + null, + null, + this.imageFactory + ); + } 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) { + return new GameLib.D3.Material( + this.graphics, + apiMaterial, + this.imageFactory + ); + } else { + console.warn('apiMaterial not an instance of API.Material'); + throw new Error('apiMaterial not an instance of API.Material'); + } + + }.bind(this) + ); + this.buildIdToObject(); this.instance = this.createInstance(); @@ -155,6 +191,18 @@ GameLib.D3.Scene.prototype.toApiScene = function() { } ); + var apiTextures = this.textures.map( + function(texture) { + return texture.toApiTexture(); + } + ); + + var apiMaterials = this.materials.map( + function(material) { + return material.toApiMaterial(); + } + ); + return new GameLib.D3.API.Scene( this.id, this.name, @@ -164,6 +212,8 @@ GameLib.D3.Scene.prototype.toApiScene = function() { this.scale.toApiVector(), this.parentGameId, apiLights, + apiTextures, + apiMaterials, GameLib.Utils.IdOrNull(this.parentEntity) ); }; @@ -191,7 +241,6 @@ GameLib.D3.Scene.FromObjectScene = function( imageFactory, computeNormals ); - }; /**