/** * R3.D3.Scene * @param apiComponent * @constructor */ R3.D3.Scene = function( apiComponent ) { __RUNTIME_COMPONENT__; this.linkedComponents.meshes = [R3.D3.Mesh]; this.linkedComponents.lights = [R3.D3.Light]; this.linkedComponents.textures = [R3.D3.Texture]; this.linkedComponents.materials = [R3.D3.Material]; this.linkedComponents.fog = R3.D3.Fog; this.helpers = []; this.clones = []; this.grid = []; this.axis = []; this.storeClones = false; __UPGRADE_TO_RUNTIME__; }; R3.D3.Scene.prototype = Object.create(R3.Component.prototype); R3.D3.Scene.prototype.constructor = R3.D3.Scene; /** * Creates an instance scene * @returns {THREE.Scene} */ R3.D3.Scene.prototype.createInstance = function() { this.instance = this.graphics.Scene(this); if (this.showGrid) { this.drawGrid(); } if (this.showAxis) { this.drawAxis(); } __CREATE_INSTANCE__; }; R3.D3.Scene.prototype.updateInstance = function(property) { if (property === 'name') { this.instance.name = this.name; return; } if (property === 'fog') { if (R3.Utils.Instance(this.fog)) { this.instance.fog = this.fog.instance; } else { this.instance.fog = null; } } if (property === 'meshes') { /** * Add missing meshes */ this.meshes.map( function(mesh) { if (this.instance.children.indexOf(mesh.instance === -1)) { this.instance.add(mesh.instance); } }.bind(this) ); } if (property === 'lights') { /** * Add missing lights */ this.lights.map( function(light) { if (this.instance.children.indexOf(light.instance) === -1) { this.instance.add(light.instance); } }.bind(this) ); } if ( property === 'meshes' || property === 'lights' ) { /** * Remove extra meshes and lights */ this.instance.children.map( function(instanceObject) { var instanceMeshes = this.meshes.map( function(mesh) { return mesh.instance; } ); var instanceLights = this.lights.map( function(light) { return light.instance; } ); if ( ( instanceObject instanceof THREE.Mesh || instanceObject instanceof THREE.Light ) && ( instanceLights.indexOf(instanceObject) === -1 && instanceMeshes.indexOf(instanceObject) === -1 ) ) { this.instance.remove(instanceObject); } }.bind(this) ); return; } if ( property === 'showGrid' || property === 'gridSize' || property === 'gridColor' ) { if (this.showGrid) { this.drawGrid(); } else { this.removeGrid(); } } if (property === 'showAxis') { if (this.showAxis) { this.drawAxis(); } else { this.removeAxis(); } } __UPDATE_INSTANCE__; }; /** * Adds a mesh to the scene * @param object R3.D3.Mesh */ R3.D3.Scene.prototype.addObject = function(object) { if (object instanceof R3.D3.Mesh) { if (this.meshes.indexOf(object.id) === -1) { R3.Utils.PushUnique(this.meshes, object); } } if (object instanceof R3.D3.Light) { if (this.lights.indexOf(object.id) === -1) { R3.Utils.PushUnique(this.lights, object); } } object.parentScene = this; if ( this.instance && object.instance ) { if (this.instance.children.indexOf(object.instance) === -1) { this.instance.add(object.instance); } } else { // console.warn('either scene or mesh instance not ready'); } // if (this.parent) { // this.parent.addComponent(object); // } }; R3.D3.Scene.prototype.addClone = function(component) { if (component instanceof R3.D3.Mesh || component instanceof R3.D3.Light ) { if (this.instance && component.instance) { if (this.instance.children.indexOf(component.instance) === -1) { this.instance.add(component.instance); } } component.isClone = true; R3.Utils.PushUnique(this.clones, component); var index = this.meshes.indexOf(component); if (index !== -1) { this.meshes.splice(index, 1); } component.parentScene = this; } }; /** * * @param object */ R3.D3.Scene.prototype.removeObject = function(object) { var index = -1; if (object instanceof R3.D3.Mesh) { index = this.meshes.indexOf(object); if (index !== -1) { this.meshes.splice(index, 1); } index = this.clones.indexOf(object); if (index !== -1) { this.clones.splice(index, 1); } } else if (object instanceof R3.D3.Light) { index = this.lights.indexOf(object); if (index !== -1) { this.lights.splice(index, 1); } index = this.clones.indexOf(object); if (index !== -1) { this.clones.splice(index, 1); } } else { console.warn('Cannot remove this object - what is this ?' + object.toString()); return; } if (this.instance.children.indexOf(object.instance) !== -1) { this.instance.remove(object.instance); } else { console.warn('no scene instance'); } if (object.parentScene === this) { object.parentScene = null; } // // if (this.parent) { // this.parent.removeComponent(object); // } // this.buildIdToObject(); }; R3.D3.Scene.prototype.drawGrid = function() { this.removeGrid(); var lineMaterial = new THREE.LineBasicMaterial({ color: this.gridColor.toHex(), linewidth: 1 }); for (var y = -this.gridSize; y <= this.gridSize; y += 1) { var Xgeometry = new THREE.Geometry(); Xgeometry.vertices.push( new THREE.Vector3( y, 0, this.gridSize * -1 ), new THREE.Vector3( y, 0, this.gridSize ) ); var lineX = new THREE.Line(Xgeometry, lineMaterial); this.instance.add(lineX); this.grid.push(lineX); var Ygeometry = new THREE.Geometry(); Ygeometry.vertices.push( new THREE.Vector3( this.gridSize * -1 , 0, y ), new THREE.Vector3( this.gridSize, 0, y ) ); var lineY = new THREE.Line(Ygeometry, lineMaterial); this.instance.add(lineY); this.grid.push(lineY); } }; R3.D3.Scene.prototype.removeGrid = function() { this.grid.map( function(object) { this.instance.remove(object); }.bind(this) ); }; R3.D3.Scene.prototype.drawAxis = function() { this.removeAxis(); var Xmaterial = new THREE.LineBasicMaterial({ color: 0xff0000, linewidth: 2 }); var Xgeometry = new THREE.Geometry(); Xgeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 100, 0, 0 ) ); var lineX = new THREE.Line(Xgeometry, Xmaterial); this.instance.add(lineX); this.axis.push(lineX); var Ymaterial = new THREE.LineBasicMaterial({ color: 0x00ff00, linewidth: 2 }); var Ygeometry = new THREE.Geometry(); Ygeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 100, 0 ) ); var lineY = new THREE.Line(Ygeometry, Ymaterial); this.instance.add(lineY); this.axis.push(lineY); var Zmaterial = new THREE.LineBasicMaterial({ color: 0x0000ff, linewidth: 2 }); var Zgeometry = new THREE.Geometry(); Zgeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 100 ) ); var lineZ = new THREE.Line(Zgeometry, Zmaterial); this.instance.add(lineZ); this.axis.push(lineZ); }; R3.D3.Scene.prototype.removeAxis = function() { this.axis.map( function(object) { this.instance.remove(object); }.bind(this) ); };