2016-02-12 08:47:08 +00:00
|
|
|
import Layer from '../Layer';
|
2016-02-24 20:42:32 +00:00
|
|
|
import extend from 'lodash.assign';
|
2016-02-12 08:47:08 +00:00
|
|
|
import THREE from 'three';
|
2016-02-23 23:11:49 +00:00
|
|
|
import Skybox from './Skybox';
|
2016-02-12 08:47:08 +00:00
|
|
|
|
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 EnvironmentLayer extends Layer {
|
2016-02-24 20:42:32 +00:00
|
|
|
constructor(options) {
|
2016-02-12 08:47:08 +00:00
|
|
|
super();
|
2016-02-24 20:42:32 +00:00
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
skybox: false
|
|
|
|
};
|
|
|
|
|
|
|
|
this._options = extend(defaults, options);
|
2016-02-23 23:11:49 +00:00
|
|
|
}
|
2016-02-12 08:47:08 +00:00
|
|
|
|
2016-02-23 23:11:49 +00:00
|
|
|
_onAdd() {
|
2016-02-12 08:47:08 +00:00
|
|
|
this._initLights();
|
2016-02-24 20:42:32 +00:00
|
|
|
|
|
|
|
if (this._options.skybox) {
|
|
|
|
this._initSkybox();
|
|
|
|
}
|
|
|
|
|
2016-02-16 13:32:31 +00:00
|
|
|
// this._initGrid();
|
2016-02-12 08:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not fleshed out or thought through yet
|
|
|
|
//
|
|
|
|
// Lights could potentially be put it their own 'layer' to keep this class
|
|
|
|
// much simpler and less messy
|
|
|
|
_initLights() {
|
|
|
|
// Position doesn't really matter (the angle is important), however it's
|
|
|
|
// used here so the helpers look more natural.
|
|
|
|
|
2016-02-24 20:42:32 +00:00
|
|
|
if (!this._options.skybox) {
|
2016-03-02 11:34:10 +00:00
|
|
|
var directionalLight = new THREE.DirectionalLight(0xffffff, 1);
|
|
|
|
directionalLight.position.x = 1000;
|
|
|
|
directionalLight.position.y = 1000;
|
|
|
|
directionalLight.position.z = 1000;
|
|
|
|
|
|
|
|
var directionalLight2 = new THREE.DirectionalLight(0xffffff, 0.5);
|
|
|
|
directionalLight2.position.x = -1000;
|
|
|
|
directionalLight2.position.y = 1000;
|
|
|
|
directionalLight2.position.z = -1000;
|
2016-02-24 20:42:32 +00:00
|
|
|
|
2016-03-02 08:49:35 +00:00
|
|
|
// var helper = new THREE.DirectionalLightHelper(directionalLight, 10);
|
|
|
|
// var helper2 = new THREE.DirectionalLightHelper(directionalLight2, 10);
|
2016-02-24 20:42:32 +00:00
|
|
|
|
2016-02-25 23:00:16 +00:00
|
|
|
this.add(directionalLight);
|
|
|
|
this.add(directionalLight2);
|
2016-02-24 20:42:32 +00:00
|
|
|
|
2016-03-02 08:49:35 +00:00
|
|
|
// this.add(helper);
|
|
|
|
// this.add(helper2);
|
2016-02-24 20:42:32 +00:00
|
|
|
} else {
|
|
|
|
// Directional light that will be projected from the sun
|
|
|
|
this._skyboxLight = new THREE.DirectionalLight(0xffffff, 1);
|
2016-02-25 22:32:06 +00:00
|
|
|
|
|
|
|
this._skyboxLight.castShadow = true;
|
|
|
|
|
|
|
|
var d = 1000;
|
|
|
|
this._skyboxLight.shadow.camera.left = -d;
|
|
|
|
this._skyboxLight.shadow.camera.right = d;
|
|
|
|
this._skyboxLight.shadow.camera.top = d;
|
|
|
|
this._skyboxLight.shadow.camera.bottom = -d;
|
|
|
|
|
|
|
|
this._skyboxLight.shadow.camera.near = 10000;
|
|
|
|
this._skyboxLight.shadow.camera.far = 70000;
|
|
|
|
|
|
|
|
// TODO: Need to dial in on a good shadowmap size
|
|
|
|
this._skyboxLight.shadow.mapSize.width = 2048;
|
|
|
|
this._skyboxLight.shadow.mapSize.height = 2048;
|
|
|
|
|
|
|
|
// this._skyboxLight.shadowBias = -0.0010;
|
|
|
|
// this._skyboxLight.shadow.darkness = 0.15;
|
|
|
|
|
|
|
|
// this._layer.add(new THREE.CameraHelper(this._skyboxLight.shadow.camera));
|
|
|
|
|
2016-02-25 23:00:16 +00:00
|
|
|
this.add(this._skyboxLight);
|
2016-02-24 20:42:32 +00:00
|
|
|
}
|
2016-02-23 23:11:49 +00:00
|
|
|
}
|
2016-02-12 08:47:08 +00:00
|
|
|
|
2016-02-23 23:11:49 +00:00
|
|
|
_initSkybox() {
|
2016-03-01 20:34:53 +00:00
|
|
|
this._skybox = new Skybox(this._world, this._skyboxLight);
|
2016-02-25 23:00:16 +00:00
|
|
|
this.add(this._skybox._mesh);
|
2016-02-12 08:47:08 +00:00
|
|
|
}
|
2016-02-12 21:17:40 +00:00
|
|
|
|
|
|
|
// Add grid helper for context during initial development
|
|
|
|
_initGrid() {
|
|
|
|
var size = 4000;
|
|
|
|
var step = 100;
|
|
|
|
|
|
|
|
var gridHelper = new THREE.GridHelper(size, step);
|
2016-02-25 23:00:16 +00:00
|
|
|
this.add(gridHelper);
|
2016-02-12 21:17:40 +00:00
|
|
|
}
|
2016-02-24 20:42:32 +00:00
|
|
|
|
|
|
|
// Clean up environment
|
|
|
|
destroy() {
|
|
|
|
this._skyboxLight = null;
|
|
|
|
|
2016-02-25 23:00:16 +00:00
|
|
|
this.remove(this._skybox._mesh);
|
2016-02-24 20:42:32 +00:00
|
|
|
this._skybox.destroy();
|
|
|
|
this._skybox = null;
|
|
|
|
|
|
|
|
super.destroy();
|
|
|
|
}
|
2016-02-12 08:47:08 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 20:34:53 +00:00
|
|
|
export default EnvironmentLayer;
|
|
|
|
|
|
|
|
var noNew = function(options) {
|
2016-02-24 20:42:32 +00:00
|
|
|
return new EnvironmentLayer(options);
|
2016-02-12 08:47:08 +00:00
|
|
|
};
|
2016-03-01 20:34:53 +00:00
|
|
|
|
|
|
|
// Initialise without requiring new keyword
|
|
|
|
export {noNew as environmentLayer};
|