/** * 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 * @constructor */ GameLib.D3.Scene = function ( graphics, apiScene ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiScene)) { apiScene = {}; } if (apiScene instanceof GameLib.D3.Scene) { return apiScene; } GameLib.D3.API.Scene.call( this, apiScene.id, apiScene.name, apiScene.meshes, apiScene.position, apiScene.quaternion, apiScene.scale, apiScene.parentGameId, apiScene.lights, apiScene.textures, apiScene.materials, apiScene.activeCamera, apiScene.parentEntity ); this.meshes = this.meshes.map( function(apiMesh) { if (apiMesh instanceof GameLib.D3.API.Mesh) { return new GameLib.D3.Mesh( this.graphics, apiMesh ); } 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) // ); if (this.activeCamera instanceof GameLib.D3.API.Camera) { this.activeCamera = new GameLib.D3.Camera( this.graphics, this.activeCamera ); } this.idToObject[this.id] = this; this.linkObjects(this.idToObject); 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 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, apiMeshes, this.position.toApiObject(), this.quaternion.toApiObject(), this.scale.toApiObject(), this.parentGameId, apiLights, apiTextures, apiMaterials, GameLib.Utils.IdOrNull(this.activeCamera), GameLib.Utils.IdOrNull(this.parentEntity) ); }; /** * Converts a scene Object to a GameLib.D3.Scene object * @param graphics GameLib.D3.Graphics * @param objectScene Object * @returns {GameLib.D3.Scene} * @constructor */ GameLib.D3.Scene.FromObjectScene = function( graphics, objectScene ) { var apiScene = GameLib.D3.API.Scene.FromObjectScene(objectScene); return new GameLib.D3.Scene( graphics, apiScene ); }; /** * Adds a mesh to the scene * @param mesh GameLib.D3.Mesh */ GameLib.D3.Scene.prototype.addMesh = function(mesh) { if (mesh instanceof GameLib.D3.Mesh) { this.meshes.push(mesh); } else if (mesh instanceof GameLib.D3.API.Mesh) { mesh = new GameLib.D3.Mesh( this.graphics, mesh ) } mesh.parentScene = this; this.instance.add(mesh.instance); this.buildIdToObject(); }; /** * * @param mesh */ GameLib.D3.Scene.prototype.removeMesh = function(mesh) { var index = -1; if (mesh instanceof GameLib.D3.Mesh) { index = this.meshes.indexOf(mesh); } else { console.warn('Cannot remove this mesh - what is this ?' + mesh.toString()); throw new Error('Cannot remove this mesh - what is this ?' + mesh.toString()) } this.instance.remove(mesh.instance); if (index !== -1) { this.meshes.splice(index, 1); } if (mesh.parentScene === this) { mesh.parentScene = null; } this.buildIdToObject(); };