2016-02-12 08:47:08 +00:00
|
|
|
import EventEmitter from 'eventemitter3';
|
|
|
|
import THREE from 'three';
|
|
|
|
import Scene from '../engine/Scene';
|
|
|
|
|
2016-02-28 18:40:51 +00:00
|
|
|
// TODO: Make sure nothing is left behind in the heap after calling destroy()
|
|
|
|
|
2016-02-12 08:47:08 +00:00
|
|
|
class Layer extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this._layer = new THREE.Object3D();
|
|
|
|
}
|
|
|
|
|
2016-02-25 23:00:16 +00:00
|
|
|
// Add THREE object directly to layer
|
|
|
|
add(object) {
|
|
|
|
this._layer.add(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove THREE object from to layer
|
|
|
|
remove(object) {
|
|
|
|
this._layer.remove(object);
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:47:08 +00:00
|
|
|
// Add layer to world instance and store world reference
|
|
|
|
addTo(world) {
|
|
|
|
world.addLayer(this);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal method called by World.addLayer to actually add the layer
|
|
|
|
_addToWorld(world) {
|
|
|
|
this._world = world;
|
2016-02-14 19:10:28 +00:00
|
|
|
this._onAdd(world);
|
2016-02-12 08:47:08 +00:00
|
|
|
this.emit('added');
|
|
|
|
}
|
2016-02-18 19:33:12 +00:00
|
|
|
|
|
|
|
// Destroys the layer and removes it from the scene and memory
|
|
|
|
destroy() {
|
2016-02-25 23:00:16 +00:00
|
|
|
// Remove everything else in the layer
|
|
|
|
var child;
|
|
|
|
for (i = this._layer.children.length - 1; i >= 0; i--) {
|
|
|
|
child = this._layer.children[i];
|
|
|
|
|
|
|
|
if (!child) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remove(child);
|
|
|
|
|
|
|
|
if (child.geometry) {
|
|
|
|
// Dispose of mesh and materials
|
|
|
|
child.geometry.dispose();
|
|
|
|
child.geometry = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.material) {
|
|
|
|
if (child.material.map) {
|
|
|
|
child.material.map.dispose();
|
|
|
|
child.material.map = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
child.material.dispose();
|
|
|
|
child.material = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 19:33:12 +00:00
|
|
|
this._world = null;
|
|
|
|
this._layer = null;
|
|
|
|
}
|
2016-02-12 08:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Layer;
|