/** * 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.images, 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.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.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.images = this.images.map( function(apiImage) { if (apiImage instanceof GameLib.D3.API.Image) { var image = new GameLib.D3.Image( this.graphics, apiImage ); // this.idToObject[image.id] = image; return image; } else { console.warn('apiImage not an instance of API.Image'); throw new Error('apiImage not an instance of API.Image'); } }.bind(this) ); if (this.activeCamera instanceof GameLib.D3.API.Camera) { this.activeCamera = new GameLib.D3.Camera( this.graphics, this.activeCamera ); } /** * TODO : Refactor (linking ?) * @type {GameLib.D3.Scene} */ // this.idToObject[this.id] = this; // // this.linkObjects(this.idToObject); GameLib.Component.call( this, GameLib.Component.COMPONENT_SCENE, { 'meshes' : [GameLib.D3.Mesh], 'lights' : [GameLib.D3.Light], 'textures' : [GameLib.D3.Texture], 'materials' : [GameLib.D3.Material], 'images' : [GameLib.D3.Image], 'activeCamera' : GameLib.D3.Camera } ); }; 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(save) { 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; } ); 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.FromObject = function( graphics, objectScene ) { var apiScene = GameLib.D3.API.Scene.FromObject(objectScene); return new GameLib.D3.Scene( graphics, apiScene ); }; /** * Adds a mesh to the scene * @param object GameLib.D3.Mesh */ GameLib.D3.Scene.prototype.addObject = function(object) { if (object instanceof GameLib.D3.Mesh) { this.meshes.push(object); } else if (object instanceof GameLib.D3.API.Mesh) { object = new GameLib.D3.Mesh( this.graphics, object ); this.meshes.push(object); } if (object instanceof GameLib.D3.Light) { this.lights.push(object); } else if (object instanceof GameLib.D3.API.Light) { object = new GameLib.D3.Light( this.graphics, object ); this.lights.push(object); } object.parentScene = this; this.instance.add(object.instance); this.buildIdToObject(); }; /** * * @param object */ GameLib.D3.Scene.prototype.removeObject = function(object) { var index = -1; if (object instanceof GameLib.D3.Mesh) { index = this.meshes.indexOf(object); if (index !== -1) { this.meshes.splice(index, 1); } } else if (object instanceof GameLib.D3.Light) { index = this.lights.indexOf(object); if (index !== -1) { this.lights.splice(index, 1); } } else { console.warn('Cannot remove this mesh - what is this ?' + object.toString()) return; } this.instance.remove(object.instance); if (object.parentScene === this) { object.parentScene = null; } this.buildIdToObject(); };