/** * Scene Superset - The apiScene properties get moved into the Scene object itself, and then the instance is * created * @param graphics * @param apiScene GameLib.D3.API.Scene * @param computeNormals * @constructor */ GameLib.D3.Scene = function ( graphics, apiScene, computeNormals ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiScene)) { apiScene = {}; } if (apiScene instanceof GameLib.D3.Scene) { 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, apiScene.scale, apiScene.parentGameId, apiScene.lights, apiScene.textures, apiScene.materials, 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) { if (apiMesh instanceof GameLib.D3.API.Mesh) { return new GameLib.D3.Mesh( this.graphics, apiMesh, this.imageFactory, this.computeNormals ); } else { console.warn('apiMesh not an instance of API.Mesh'); throw new Error('apiMesh not an instance of API.Mesh'); } }.bind(this) ); this.position = new GameLib.Vector3( this.graphics, this.position, this ); this.quaternion = new GameLib.Quaternion( this.graphics, this.quaternion, this ); this.scale = new GameLib.Vector3( this.graphics, this.scale, this ); this.lights = this.lights.map( function(apiLight) { if (apiLight instanceof GameLib.D3.API.Light) { return new GameLib.D3.Light( this.graphics, apiLight ); } else { console.warn('apiLight not an instance of API.Light'); throw new Error('apiLight not an instance of API.Light'); } }.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.idToObject[this.id] = this; this.linkObjects(this.idToObject); this.meshes.map( function(mesh) { mesh.updateInstance(); mesh.materials.map( function(material) { material.updateInstance(); } ) } ); this.buildIdToObject(); this.instance = this.createInstance(); }; GameLib.D3.Scene.prototype = Object.create(GameLib.D3.API.Scene.prototype); GameLib.D3.Scene.prototype.constructor = GameLib.D3.Scene; /** * Creates an instance scene * @returns {THREE.Scene} */ GameLib.D3.Scene.prototype.createInstance = function() { var instance = new THREE.Scene(); instance.name = this.name; instance.position = this.position.instance; instance.scale = this.scale.instance; instance.quaternion = this.quaternion.instance; for (var im = 0; im < this.meshes.length; im++) { instance.add(this.meshes[im].instance); } for (var l = 0; l < this.lights.length; l++) { instance.add(this.lights[l].instance); } return instance; }; /** * Converts a GameLib.D3.Scene to a GameLib.D3.API.Scene * @returns {GameLib.D3.API.Scene} */ 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(); } ); var apiLights = this.lights.map( function(light) { return light.toApiObject(); } ); var apiTextures = this.textures.map( function(texture) { return texture.toApiObject(); } ); var apiMaterials = this.materials.map( function(material) { return material.toApiObject(); } ); return new GameLib.D3.API.Scene( this.id, this.name, apiImageFactory, apiMeshes, this.position.toApiObject(), this.quaternion.toApiObject(), this.scale.toApiObject(), this.parentGameId, apiLights, apiTextures, apiMaterials, GameLib.Utils.IdOrNull(this.parentEntity) ); }; /** * 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 ) { var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene, imageFactory); return new GameLib.D3.Scene( graphics, apiScene, computeNormals ); };