2016-02-29 23:04:07 +00:00
|
|
|
import EventEmitter from 'eventemitter3';
|
|
|
|
import THREE from 'three';
|
|
|
|
import Scene from './Scene';
|
2016-03-03 17:05:06 +00:00
|
|
|
import DOMScene3D from './DOMScene3D';
|
|
|
|
import DOMScene2D from './DOMScene2D';
|
2016-02-29 23:04:07 +00:00
|
|
|
import Renderer from './Renderer';
|
2016-03-03 17:05:06 +00:00
|
|
|
import DOMRenderer3D from './DOMRenderer3D';
|
|
|
|
import DOMRenderer2D from './DOMRenderer2D';
|
2016-02-29 23:04:07 +00:00
|
|
|
import Camera from './Camera';
|
|
|
|
import Picking from './Picking';
|
2016-03-11 11:38:08 +00:00
|
|
|
import EffectComposer from '../vendor/EffectComposer';
|
|
|
|
import RenderPass from '../vendor/RenderPass';
|
|
|
|
import BokehPass from '../vendor/BokehPass';
|
2016-02-29 23:04:07 +00:00
|
|
|
|
|
|
|
class Engine extends EventEmitter {
|
|
|
|
constructor(container, world) {
|
|
|
|
console.log('Init Engine');
|
|
|
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
this._world = world;
|
2016-03-03 17:05:06 +00:00
|
|
|
|
2016-02-29 23:04:07 +00:00
|
|
|
this._scene = Scene;
|
2016-03-03 17:05:06 +00:00
|
|
|
this._domScene3D = DOMScene3D;
|
|
|
|
this._domScene2D = DOMScene2D;
|
|
|
|
|
2016-02-29 23:04:07 +00:00
|
|
|
this._renderer = Renderer(container);
|
2016-03-03 17:05:06 +00:00
|
|
|
this._domRenderer3D = DOMRenderer3D(container);
|
|
|
|
this._domRenderer2D = DOMRenderer2D(container);
|
|
|
|
|
2016-02-29 23:04:07 +00:00
|
|
|
this._camera = Camera(container);
|
|
|
|
|
|
|
|
// TODO: Make this optional
|
|
|
|
this._picking = Picking(this._world, this._renderer, this._camera);
|
|
|
|
|
|
|
|
this.clock = new THREE.Clock();
|
|
|
|
|
|
|
|
this._frustum = new THREE.Frustum();
|
2016-03-11 11:38:08 +00:00
|
|
|
|
|
|
|
this._initPostProcessing();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Set up composer to automatically resize on viewport change
|
|
|
|
_initPostProcessing() {
|
|
|
|
var renderPass = new RenderPass(this._scene, this._camera);
|
|
|
|
|
|
|
|
var bokehPass = new BokehPass(this._scene, this._camera, {
|
|
|
|
focus: 1,
|
|
|
|
aperture: 0.6,
|
|
|
|
// maxblur: 1.0,
|
|
|
|
width: this._renderer.getSize().width,
|
|
|
|
height: this._renderer.getSize().height
|
|
|
|
});
|
|
|
|
|
|
|
|
bokehPass.renderToScreen = true;
|
|
|
|
|
|
|
|
this._composer = new EffectComposer(this._renderer);
|
|
|
|
|
|
|
|
this._composer.addPass(renderPass);
|
|
|
|
this._composer.addPass(bokehPass);
|
2016-02-29 23:04:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update(delta) {
|
|
|
|
this.emit('preRender');
|
|
|
|
|
2016-03-11 11:38:08 +00:00
|
|
|
// this._renderer.render(this._scene, this._camera);
|
|
|
|
this._composer.render();
|
2016-02-29 23:04:07 +00:00
|
|
|
|
|
|
|
// Render picking scene
|
|
|
|
// this._renderer.render(this._picking._pickingScene, this._camera);
|
|
|
|
|
2016-03-03 17:05:06 +00:00
|
|
|
// Render DOM scenes
|
|
|
|
this._domRenderer3D.render(this._domScene3D, this._camera);
|
|
|
|
this._domRenderer2D.render(this._domScene2D, this._camera);
|
|
|
|
|
2016-02-29 23:04:07 +00:00
|
|
|
this.emit('postRender');
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
// Remove any remaining objects from scene
|
|
|
|
var child;
|
2016-03-03 17:05:06 +00:00
|
|
|
for (var i = this._scene.children.length - 1; i >= 0; i--) {
|
2016-02-29 23:04:07 +00:00
|
|
|
child = this._scene.children[i];
|
|
|
|
|
|
|
|
if (!child) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._scene.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-03-03 17:05:06 +00:00
|
|
|
for (var i = this._domScene3D.children.length - 1; i >= 0; i--) {
|
|
|
|
child = this._domScene3D.children[i];
|
|
|
|
|
|
|
|
if (!child) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._domScene3D.remove(child);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = this._domScene2D.children.length - 1; i >= 0; i--) {
|
|
|
|
child = this._domScene2D.children[i];
|
|
|
|
|
|
|
|
if (!child) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._domScene2D.remove(child);
|
|
|
|
};
|
|
|
|
|
2016-02-29 23:04:07 +00:00
|
|
|
this._picking.destroy();
|
|
|
|
this._picking = null;
|
|
|
|
|
|
|
|
this._world = null;
|
|
|
|
this._scene = null;
|
2016-03-03 17:05:06 +00:00
|
|
|
this._domScene3D = null;
|
|
|
|
this._domScene2D = null;
|
2016-02-29 23:04:07 +00:00
|
|
|
this._renderer = null;
|
2016-03-03 17:05:06 +00:00
|
|
|
this._domRenderer3D = null;
|
|
|
|
this._domRenderer2D = null;
|
2016-02-29 23:04:07 +00:00
|
|
|
this._camera = null;
|
|
|
|
this._clock = null;
|
|
|
|
this._frustum = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 20:34:53 +00:00
|
|
|
export default Engine;
|
|
|
|
|
|
|
|
// // Initialise without requiring new keyword
|
|
|
|
// export default function(container, world) {
|
|
|
|
// return new Engine(container, world);
|
|
|
|
// };
|