awv3
Version:
⚡ AWV3 embedded CAD
294 lines (232 loc) • 8.55 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import * as THREE from 'three';
import Object3 from '../three/object3';
import { Pool } from '../session/helpers';
import { exponential } from '../animation/easing';
var Presentation =
/*#__PURE__*/
function (_Pool) {
_inheritsLoose(Presentation, _Pool);
function Presentation(args) {
var _this;
_this = _Pool.call(this, args) || this;
_this.options = _extends({
shadows: true,
lights: true,
ambient: 0,
light: 0.95,
shadowHeight: 0.9,
shadowFactor: 1.2
}, args);
_this._objects = new Objects({
pool: _assertThisInitialized(_this)
});
_Pool.prototype.add.call(_this, _this._objects);
if (_this.options.shadows) {
_this._stage = new Stage({
pool: _assertThisInitialized(_this)
});
_Pool.prototype.add.call(_this, _this._stage);
}
if (_this.options.lights) {
_this._lights = new Lights({
pool: _assertThisInitialized(_this)
});
_Pool.prototype.add.call(_this, _this._lights);
}
return _this;
}
var _proto = Presentation.prototype;
_proto.add = function add(args) {
this._objects.add(args);
};
_proto.addAsync = function addAsync(args) {
return this._objects.addAsync(args);
};
_proto.remove = function remove(args) {
this._objects.remove(args);
};
_proto.removeAsync = function removeAsync(args) {
return this._objects.removeAsync(args);
};
_proto.update = function update() {
if (this.view) {
this._objects.update();
this.options.lights && this._lights.update();
this.options.shadows && this._stage.update();
}
};
_proto.getObjects = function getObjects() {
return this._objects.children;
};
return Presentation;
}(Pool);
export { Presentation as default };
function addLights(target, radius) {
if (radius === void 0) {
radius = 1000;
}
;
[[1000 * 1.3, radius, 0], [-(1000 * 1.3), radius, 0], [0, radius, 1000 * 1.3], [0, radius, -(1000 * 1.3)]].forEach(function (_ref, index) {
var x = _ref[0],
y = _ref[1],
z = _ref[2];
// Four spotlights, one for each direction
var spot = target["light" + index] = new THREE.SpotLight(0xffffff, 1.5, radius * 2, Math.PI / 6, 1);
spot.position.set(x, y, z);
spot.castShadow = false;
target.add(spot); //var spotLightHelper = new THREE.SpotLightHelper( spot );
//target.add( spotLightHelper );
}); // One spotlight above, one below...
var spotAbove = target['spotAbove'] = new THREE.SpotLight(0xffffff, 0.6, 1000 * 2.5, Math.PI / 2, 1);
spotAbove.position.set(0, radius, 0);
target.add(spotAbove);
var spotBelow = target['spotBelow'] = new THREE.SpotLight(0xffffff, 0.5, 1000 * 2.5, Math.PI / 2, 1);
spotBelow.position.set(0, -radius, 0);
target.add(spotBelow);
}
function updateLights(target, radius) {
if (radius === void 0) {
radius = 1000;
}
;
[[1000 * 1.3, radius, 0], [-(1000 * 1.3), radius, 0], [0, radius, 1000 * 1.3], [0, radius, -(1000 * 1.3)]].forEach(function (_ref2, index) {
var x = _ref2[0],
y = _ref2[1],
z = _ref2[2];
// Four spotlights, one for each direction
// SpotLight( color, intensity, distance, angle, penumbra, decay )
var spot = target["light" + index];
spot.distance = radius * 2;
spot.position.set(x, y, z);
}); // One spotlight above, one below...
var spotAbove = target['spotAbove'];
spotAbove.position.set(0, radius, 0);
var spotBelow = target['spotBelow'];
spotBelow.position.set(0, -radius, 0);
}
var Lights =
/*#__PURE__*/
function (_THREE$Group) {
_inheritsLoose(Lights, _THREE$Group);
function Lights(_ref3) {
var _this2;
var pool = _ref3.pool;
_this2 = _THREE$Group.call(this) || 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);
}
addLights(_assertThisInitialized(_this2)); //this.spot1 = new THREE.SpotLight(0xffffff)
//this.add(this.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;
}
var _proto2 = Lights.prototype;
_proto2.update = function update() {
var _this3 = this;
this.pool.viewFound().then(function () {
_this3.quaternion.setFromUnitVectors(_this3.up, _this3.pool.view.camera.up);
if (_this3.pool.options.ambient) {
_this3.ambient.intensity = _this3.pool.options.ambient;
}
var radius = _this3.pool._objects.radius;
updateLights(_this3, radius);
var intensity = _this3.pool.options.light;
var vec = _this3.pool.parent.worldToLocal(new THREE.Vector3(-radius / 2, radius, radius / 2)); //this.spot1.intensity = intensity / 2
//this.spot1.position.set(0, -radius, -radius * 2)
_this3.spot2.intensity = intensity * 2;
_this3.spot2.position.copy(vec);
_this3.spot2.shadow.camera.far = radius * 1.5;
_this3.spot2.shadow.camera.updateProjectionMatrix();
_this3.spot2.insensity = intensity;
_this3.spot2.distance = radius * 1.5;
_this3.spot2.angle = Math.PI / 6;
});
};
return Lights;
}(THREE.Group);
var Stage =
/*#__PURE__*/
function (_THREE$Group2) {
_inheritsLoose(Stage, _THREE$Group2);
function Stage(_ref4) {
var _this4;
var pool = _ref4.pool;
_this4 = _THREE$Group2.call(this) || this;
_this4.name = 'presentation.stage';
_this4.pool = pool;
_this4.interactive = false;
_this4.tweens = false;
_this4.measurable = false;
return _this4;
}
var _proto3 = Stage.prototype;
_proto3.update = 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 =
/*#__PURE__*/
function (_THREE$Group3) {
_inheritsLoose(Objects, _THREE$Group3);
function Objects(_ref5) {
var _this5;
var pool = _ref5.pool;
_this5 = _THREE$Group3.call(this) || this;
_this5.name = 'presentation.objects';
_this5.pool = pool;
return _this5;
}
var _proto4 = Objects.prototype;
_proto4.update = 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.3)');
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();