awv3
Version:
⚡ AWV3 embedded CAD
281 lines (224 loc) • 10.6 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _get2 = require('babel-runtime/helpers/get');
var _get3 = _interopRequireDefault(_get2);
var _three = require('three');
var THREE = _interopRequireWildcard(_three);
var _object = require('../three/object3');
var _object2 = _interopRequireDefault(_object);
var _helpers = require('../session/helpers');
var _easing = require('../animation/easing');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Presentation = function (_Pool) {
(0, _inherits3.default)(Presentation, _Pool);
function Presentation(args) {
(0, _classCallCheck3.default)(this, Presentation);
var _this = (0, _possibleConstructorReturn3.default)(this, (Presentation.__proto__ || (0, _getPrototypeOf2.default)(Presentation)).call(this, args));
_this.options = (0, _extends3.default)({
shadows: true,
lights: true,
ambient: 0,
light: 0.95,
shadowHeight: 0.9,
shadowFactor: 1.2
}, args);
_this._objects = new Objects({ pool: _this });
(0, _get3.default)(Presentation.prototype.__proto__ || (0, _getPrototypeOf2.default)(Presentation.prototype), 'add', _this).call(_this, _this._objects);
if (_this.options.shadows) {
_this._stage = new Stage({ pool: _this });
(0, _get3.default)(Presentation.prototype.__proto__ || (0, _getPrototypeOf2.default)(Presentation.prototype), 'add', _this).call(_this, _this._stage);
}
if (_this.options.lights) {
_this._lights = new Lights({ pool: _this });
(0, _get3.default)(Presentation.prototype.__proto__ || (0, _getPrototypeOf2.default)(Presentation.prototype), 'add', _this).call(_this, _this._lights);
}
return _this;
}
(0, _createClass3.default)(Presentation, [{
key: 'add',
value: function add(args) {
this._objects.add(args);
}
}, {
key: 'addAsync',
value: function addAsync(args) {
return this._objects.addAsync(args);
}
}, {
key: 'remove',
value: function remove(args) {
this._objects.remove(args);
}
}, {
key: 'removeAsync',
value: function removeAsync(args) {
return this._objects.removeAsync(args);
}
}, {
key: 'update',
value: function update() {
if (this.view) {
this._objects.update();
this.options.lights && this._lights.update();
this.options.shadows && this._stage.update();
}
}
}]);
return Presentation;
}(_helpers.Pool);
exports.default = Presentation;
var Lights = function (_THREE$Group) {
(0, _inherits3.default)(Lights, _THREE$Group);
function Lights(_ref) {
var pool = _ref.pool;
(0, _classCallCheck3.default)(this, Lights);
var _this2 = (0, _possibleConstructorReturn3.default)(this, (Lights.__proto__ || (0, _getPrototypeOf2.default)(Lights)).call(this));
_this2.name = 'presentation.lights';
_this2.pool = pool;
_this2.interactive = false;
_this2.tweens = false;
_this2.measurable = false;
if (_this2.pool.options.ambient) {
_this2.ambient = new THREE.AmbientLight(0xffffff, _this2.pool.options.ambient);
_this2.add(_this2.ambient);
}
_this2.point1 = new THREE.DirectionalLight(0xffffff);
_this2.point1.target = pool._objects;
_this2.add(_this2.point1);
_this2.point2 = new THREE.DirectionalLight(0xffffff);
_this2.point2.target = pool._objects;
_this2.add(_this2.point2);
_this2.point3 = new THREE.DirectionalLight(0xffffff);
_this2.point3.target = pool._objects;
_this2.add(_this2.point3);
_this2.point4 = new THREE.DirectionalLight(0xffffff);
_this2.point4.target = pool._objects;
_this2.add(_this2.point4);
_this2.spot1 = new THREE.SpotLight(0xffffff);
_this2.add(_this2.spot1);
_this2.spot2 = new THREE.SpotLight(0xffffff);
_this2.spot2.target = pool._objects;
_this2.spot2.castShadow = true;
_this2.spot2.penumbra = 1;
_this2.spot2.shadow = new THREE.LightShadow(new THREE.PerspectiveCamera(20, 1, 100, 1000));
_this2.spot2.shadow.mapSize.width = 2048;
_this2.spot2.shadow.mapSize.height = 2048;
_this2.add(_this2.spot2);
return _this2;
}
(0, _createClass3.default)(Lights, [{
key: 'update',
value: function update() {
if (this.pool.parent) {
this.quaternion.setFromUnitVectors(this.up, this.pool.view.camera.up);
if (this.pool.options.ambient) {
this.ambient.intensity = this.pool.options.ambient;
}
var radius = this.pool._objects.radius;
var intensity = this.pool.options.light;
var vec = this.pool.parent.worldToLocal(new THREE.Vector3(-radius / 2, radius, radius / 2));
this.point1.intensity = intensity / 2;
this.point1.setPosition(-radius, 0, 0);
this.point2.intensity = intensity / 2;
this.point2.setPosition(radius, 0, 0);
this.point3.intensity = intensity / 2;
this.point3.setPosition(0, radius, 0);
this.point4.intensity = intensity / 2;
this.point4.setPosition(0, 0, radius);
this.spot1.intensity = intensity / 2;
this.spot1.setPosition(0, -radius, -radius * 2);
this.spot2.intensity = intensity * 3;
this.spot2.position.copy(vec);
this.spot2.shadow.camera.far = radius * 1.5;
this.spot2.shadow.camera.updateProjectionMatrix();
this.spot2.insensity = intensity;
this.spot2.distance = radius * 1.5;
this.spot2.angle = Math.PI / 6;
}
}
}]);
return Lights;
}(THREE.Group);
var Stage = function (_THREE$Group2) {
(0, _inherits3.default)(Stage, _THREE$Group2);
function Stage(_ref2) {
var pool = _ref2.pool;
(0, _classCallCheck3.default)(this, Stage);
var _this3 = (0, _possibleConstructorReturn3.default)(this, (Stage.__proto__ || (0, _getPrototypeOf2.default)(Stage)).call(this));
_this3.name = 'presentation.stage';
_this3.pool = pool;
_this3.interactive = false;
_this3.tweens = false;
_this3.measurable = false;
return _this3;
}
(0, _createClass3.default)(Stage, [{
key: 'update',
value: function update() {
this.destroy();
if (!isNaN(this.pool._objects.width) && !isNaN(this.pool._objects.depth)) {
this.quaternion.setFromUnitVectors(new THREE.Vector3(0, 0, 1), this.pool.view.camera.up);
this.position.copy(this.pool._objects.getCenter());
this.position.addScaledVector(this.pool.view.camera.up, -this.pool._objects.height * this.pool.options.shadowHeight);
var shadowGeo = new THREE.PlaneBufferGeometry(this.pool._objects.width * this.pool.options.shadowFactor, this.pool._objects.depth * this.pool.options.shadowFactor, 1, 1);
var mesh = new THREE.Mesh(shadowGeo, shadowMaterial);
this.add(mesh);
}
}
}]);
return Stage;
}(THREE.Group);
var Objects = function (_THREE$Group3) {
(0, _inherits3.default)(Objects, _THREE$Group3);
function Objects(_ref3) {
var pool = _ref3.pool;
(0, _classCallCheck3.default)(this, Objects);
var _this4 = (0, _possibleConstructorReturn3.default)(this, (Objects.__proto__ || (0, _getPrototypeOf2.default)(Objects)).call(this));
_this4.name = 'presentation.objects';
_this4.pool = pool;
return _this4;
}
(0, _createClass3.default)(Objects, [{
key: 'update',
value: function update() {
this.updateBounds();
this.min = this.bounds.box.min;
this.max = this.bounds.box.max;
this.width = this.max.distanceTo(new THREE.Vector3(this.min.x, this.max.y, this.max.z));
this.height = this.max.distanceTo(new THREE.Vector3(this.max.x, this.min.y, this.max.z));
this.depth = this.max.distanceTo(new THREE.Vector3(this.max.x, this.max.y, this.min.z));
this.radius = this.getRadius() * 6;
}
}]);
return Objects;
}(THREE.Group);
function createShadowMaterial() {
var canvas2d = document.createElement('canvas');
canvas2d.width = 128;
canvas2d.height = 128;
var context = canvas2d.getContext('2d');
var gradient = context.createRadialGradient(canvas2d.width / 2, canvas2d.height / 2, 0, canvas2d.width / 2, canvas2d.height / 2, canvas2d.width / 2);
gradient.addColorStop(0.1, 'rgba(0,0,0,0.15)');
gradient.addColorStop(1, 'rgba(0,0,0,0)');
context.fillStyle = gradient;
context.fillRect(0, 0, canvas2d.width, canvas2d.height);
var shadowTexture = new THREE.CanvasTexture(canvas2d);
return new THREE.MeshBasicMaterial({ map: shadowTexture, transparent: true });
}
var shadowMaterial = createShadowMaterial();