ami-cjs.js
Version:
<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>
148 lines (126 loc) • 6.07 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
* @module helpers/x/renderer3d
*/
var _controls = require('../../controls/controls.trackball');
var _controls2 = _interopRequireDefault(_controls);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _class = function () {
function _class() {
var containerId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'r3d';
_classCallCheck(this, _class);
this._container = null;
this._renderer = null;
this._camera = null;
this._controls = null;
this._scene = null;
this._initRenderer(containerId);
this._initCamera();
this._initScene();
this._initControls();
// setup event listeners
this._onWindowResize = this._onWindowResize.bind(this);
this.addEventListeners();
}
_createClass(_class, [{
key: 'add',
value: function add(obj) {
this._scene.add(obj);
}
}, {
key: 'addEventListeners',
value: function addEventListeners() {
window.addEventListener('resize', this._onWindowResize, false);
}
}, {
key: 'removeEventListeners',
value: function removeEventListeners() {
window.removeEventListener('resize', this._onWindowResize, false);
}
}, {
key: 'center',
value: function center(worldPosition) {
// update camrea's and control's target
this._camera.lookAt(worldPosition.x, worldPosition.y, worldPosition.z);
this._camera.updateProjectionMatrix();
this._controls.target.set(worldPosition.x, worldPosition.y, worldPosition.z);
}
}, {
key: 'animate',
value: function animate() {
this._controls.update();
this._renderer.render(this._scene, this._camera);
// request new frame
requestAnimationFrame(this.animate.bind(this));
}
// private methods
}, {
key: '_onWindowResize',
value: function _onWindowResize() {
this._camera.aspect = this._container.clientWidth / this._container.clientHeight;
this._camera.updateProjectionMatrix();
this._renderer.setSize(this._container.clientWidth, this._container.clientHeight);
}
}, {
key: '_initRenderer',
value: function _initRenderer(containerId) {
// renderer
this._container = document.getElementById(containerId);
this._renderer = new THREE.WebGLRenderer({
antialias: true
});
this._renderer.setSize(this._container.clientWidth, this._container.clientHeight);
this._renderer.setClearColor(0x424242, 1);
this._renderer.setPixelRatio(window.devicePixelRatio);
this._container.appendChild(this._renderer.domElement);
}
}, {
key: '_initCamera',
value: function _initCamera() {
this._camera = new THREE.PerspectiveCamera(45, this._container.clientWidth / this._container.clientHeight, 1, 10000000);
this._camera.position.x = 250;
this._camera.position.y = 250;
this._camera.position.z = 250;
}
}, {
key: '_initScene',
value: function _initScene() {
// add some lights to the scene by default
this._scene = new THREE.Scene();
// ambient
this._scene.add(new THREE.AmbientLight(0x353535));
// directional 1
var directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(200, 200, 1000).normalize();
this._scene.add(directionalLight);
// directional 2
var directionalLight2 = new THREE.DirectionalLight(0xffffff, 1);
directionalLight2.position.set(-200, -200, -1000).normalize();
this._scene.add(directionalLight2);
}
}, {
key: '_initControls',
value: function _initControls() {
// controls
this._controls = new _controls2.default(this._camera, this._container);
this._controls.rotateSpeed = 1.4;
this._controls.zoomSpeed = 1.2;
this._controls.panSpeed = 0.8;
}
}, {
key: 'container',
set: function set(container) {
this._container = container;
},
get: function get() {
return this._container;
}
}]);
return _class;
}();
exports.default = _class;
module.exports = exports['default'];