UNPKG

awv3

Version:
892 lines (745 loc) 23.9 kB
import _regeneratorRuntime from "@babel/runtime/regenerator"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _extends from "@babel/runtime/helpers/extends"; import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import * as THREE from 'three'; import merge from 'lodash/merge'; import Defaults from '../core/defaults'; import Events from '../core/events'; import Tween from '../animation/tween'; import Raycaster from './raycaster'; var Object3 = /*#__PURE__*/ function (_THREE$Object3D) { _inheritsLoose(Object3, _THREE$Object3D); function Object3(objects, options) { var _this; if (objects === void 0) { objects = undefined; } if (options === void 0) { options = undefined; } _this = _THREE$Object3D.call(this) || this; if (!!options) _this.setValues(options); if (!!objects) _this.add(objects); return _this; } return Object3; }(THREE.Object3D); export { Object3 as default }; THREE.Object3D.Events = { Lifecycle: { Loaded: 'Loaded', Added: 'Added', ViewFound: 'ViewFound', ChildAdded: 'ChildAdded', Removed: 'Removed', ChildRemoved: 'ChildRemoved', Destroyed: 'Destroyed', Rendered: 'Rendered', Updated: 'Updated' }, Interaction: { Clicked: 'Clicked', Picked: 'Picked', Dropped: 'Dropped', Dragged: 'Dragged', Hovered: 'Hovered', Unhovered: 'Unhovered', Missed: 'Missed' } }; THREE.Object3D.RenderOrder = { Default: { Mesh: 0, LineSegments: 100 }, LinesFirst: { Mesh: 0, LineSegments: 100 }, MeshesFirst: { Mesh: 100, LineSegments: 0 } }; THREE.Object3D.prototype.canvas = undefined; THREE.Object3D.prototype.view = undefined; THREE.Object3D.prototype.bounds = undefined; THREE.Object3D.prototype.materials = undefined; THREE.Object3D.prototype.updateParentMaterials = true; THREE.Object3D.prototype.keep = false; THREE.Object3D.prototype.measurable = true; THREE.Object3D.prototype.tweens = true; THREE.Object3D.prototype.interactive = true; THREE.Object3D.prototype.interaction = undefined; THREE.Object3D.prototype.interactionPriority = 0; // Extend Object3D with event emitter Events.mixin(THREE.Object3D.prototype); var copyObject3D = THREE.Object3D.prototype.copy; THREE.Object3D.prototype.copy = function (source) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } var copy = copyObject3D.bind(this).apply(void 0, [source].concat(args)); copy.updateParentMaterials = source.updateParentMaterials; copy.keep = source.keep; copy.measurable = source.measurable; copy.tweens = source.tweens; copy.interactive = source.interactive; copy.interactionPriority = source.interactionPriority; return copy; }; THREE.Object3D.prototype.createInteraction = function (options) { var _this2 = this; if (options === void 0) { options = {}; } if (this.interaction) return this; this.interaction = _extends({ first: true, faces: false, priority: 0, enabled: true, recursive: false, approach: Raycaster.Approach.Default, pierce: ['Assembly', 'Part'], types: ['Mesh'] }, options); var scope = this; this.viewFound().then(function (view) { if (view.interaction.targets.indexOf(scope) == -1) { view.interaction.targets.push(scope); var count = 0; view.scene.traverse(function (item) { return item.depthIndex = count++; }); view.interaction.targets.sort(function (a, b) { return a.depthIndex - b.depthIndex; }); } }); // _active controls if the interaction layer considers the object, depending if interaction-related events are registered this.inspect(function (context) { return _this2.interaction._active = !!Object.keys(_this2._callbacks).find(function (item) { return item in THREE.Object3D.Events.Interaction; }); }); return this; }; THREE.Object3D.prototype.removeInteraction = function () { if (this.view && this.interaction) { this.interaction = undefined; this.view.interaction.removeTarget(this); this.removeInspectors(); this.removeListener([Object3.Events.Interaction.Clicked, Object3.Events.Interaction.Picked, Object3.Events.Interaction.Dropped, Object3.Events.Interaction.Dragged, Object3.Events.Interaction.Hovered, Object3.Events.Interaction.Unhovered, Object3.Events.Interaction.Missed, Object3.Events.Lifecycle.Rendered, Object3.Events.Lifecycle.Updated]); } }; THREE.Object3D.prototype.viewFound = function () { var scope = this; return !!this.view ? Promise.resolve(this.view) : new Promise(function (resolve) { return scope.once(THREE.Object3D.Events.Lifecycle.ViewFound, resolve); }); }; THREE.Object3D.prototype._destroyRecursive = function (options) { this.canvas = undefined; this.view = undefined; this.scene = undefined; this.bounds = undefined; this.userData = {}; if (!!this.geometry) { this.geometry.dispose(); } if (!!this.material) { var isMultiMaterial = Array.isArray(this.material); if (isMultiMaterial) { this.material.forEach(function (material) { material.dispose(); material.needsUpdate = true; }); this.material = undefined; } else { this.material.dispose && this.material.dispose(); this.material.needsUpdate = true; this.material = undefined; } } if (!!this.materials) { //this.materials.all.forEach(material => material.dispose()); this.materials.all = []; this.materials.meshes = []; this.materials.lines = []; } // Remove children if (!options.async) { for (var i = this.children.length - 1; i >= 0; i--) { this.children[i].destroy(options); } } else { return Promise.all(this.children.map(function (child) { return child.destroyAsync(options); })); } }; THREE.Object3D.prototype.destroyAsync = /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee(options) { var view; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (options === void 0) { options = {}; } view = this.view; options = _extends({ keep: true, data: true, interaction: true, listeners: true }, options); if (!(options.keep && this.keep)) { _context.next = 5; break; } return _context.abrupt("return"); case 5: _context.next = 7; return this.emit(Object3.Events.Lifecycle.Destroyed); case 7: _context.t0 = options.data; if (!_context.t0) { _context.next = 11; break; } _context.next = 11; return this._destroyRecursive(_extends({}, options, { async: true })); case 11: options.interaction && this.removeInteraction(); _context.t1 = this.parent; if (!_context.t1) { _context.next = 16; break; } _context.next = 16; return this.parent.removeAsync(this); case 16: options.listeners && this.removeListeners(); this.removeObjectTweens(); view && view.invalidate(); case 19: case "end": return _context.stop(); } } }, _callee, this); })); return function (_x) { return _ref.apply(this, arguments); }; }(); THREE.Object3D.prototype.destroy = function (options) { if (options === void 0) { options = {}; } var view = this.view; options = _extends({ keep: true, data: true, interaction: true, listeners: true }, options); if (options.keep && this.keep) return; this.emit(Object3.Events.Lifecycle.Destroyed); this.parent && this.parent.remove(this); options.interaction && this.removeInteraction(); options.listeners && this.removeListeners(); this.removeObjectTweens(); options.data && this._destroyRecursive(options); view && view.invalidate(); }; function updateReferences(parent, object) { if (object.parent && (object.parent === parent && parent instanceof THREE.Scene || parent.view)) { object.traverse(function (child) { child.scene = parent instanceof THREE.Scene ? parent : parent.scene; child.view = parent.view; child.canvas = parent.canvas; child.emit(THREE.Object3D.Events.Lifecycle.ViewFound, child.view); }); } else { object.traverse(function (child) { child.scene = undefined; child.view = undefined; child.canvas = undefined; }); } return object; } THREE.Object3D.prototype.addAsync = /*#__PURE__*/ function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee2(object) { var _this3 = this; var _args2 = arguments; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: object = _args2.length > 1 ? Array.from(_args2) : object; if (!Array.isArray(object)) { _context2.next = 3; break; } return _context2.abrupt("return", Promise.all(object.map(function (item) { return _this3.addAsync(item); }))); case 3: if (!(object !== this && object instanceof THREE.Object3D)) { _context2.next = 14; break; } if (!object.parent) { _context2.next = 7; break; } _context2.next = 7; return object.parent.removeAsync(object); case 7: object.parent = this; this.children.push(updateReferences(this, object)); _context2.next = 11; return this.emit(Object3.Events.Lifecycle.ChildAdded, { child: object }); case 11: _context2.next = 13; return object.emit(Object3.Events.Lifecycle.Added, { parent: this }); case 13: !!this.view && this.view.invalidate(); case 14: case "end": return _context2.stop(); } } }, _callee2, this); })); return function (_x2) { return _ref2.apply(this, arguments); }; }(); THREE.Object3D.prototype.add = function (object) { var _this4 = this; object = arguments.length > 1 ? Array.from(arguments) : object; if (Array.isArray(object)) { return Promise.all(object.map(function (item) { return _this4.add(item); })); } if (object !== this && object instanceof THREE.Object3D) { if (!!object.parent) object.parent.remove(object); object.parent = this; this.children.push(updateReferences(this, object)); this.emit(Object3.Events.Lifecycle.ChildAdded, { child: object }); object.emit(Object3.Events.Lifecycle.Added, { parent: this }); !!this.view && this.view.invalidate(); } }; THREE.Object3D.prototype.removeAsync = /*#__PURE__*/ function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee3(object) { var _this5 = this; var index, _args3 = arguments; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: object = _args3.length > 1 ? Array.from(_args3) : object; if (!Array.isArray(object)) { _context3.next = 3; break; } return _context3.abrupt("return", Promise.all(object.map(function (item) { return _this5.removeAsync(item); }))); case 3: if (!(object instanceof THREE.Object3D)) { _context3.next = 14; break; } index = this.children.indexOf(object); if (!(index !== -1)) { _context3.next = 14; break; } _context3.next = 8; return this.emit(Object3.Events.Lifecycle.ChildRemoved, { child: object }); case 8: _context3.next = 10; return object.emit(Object3.Events.Lifecycle.Removed, { parent: this }); case 10: this.children.splice(this.children.indexOf(object), 1); object.parent = null; updateReferences(this, object); !!this.view && this.view.invalidate(); case 14: case "end": return _context3.stop(); } } }, _callee3, this); })); return function (_x3) { return _ref3.apply(this, arguments); }; }(); THREE.Object3D.prototype.remove = function (object) { var _this6 = this; object = arguments.length > 1 ? Array.from(arguments) : object; if (Array.isArray(object)) { return Promise.all(object.map(function (item) { return _this6.remove(item); })); } if (object instanceof THREE.Object3D) { var index = this.children.indexOf(object); if (index !== -1) { this.emit(Object3.Events.Lifecycle.ChildRemoved, { child: object }); object.emit(Object3.Events.Lifecycle.Removed, { parent: this }); this.children.splice(index, 1); object.parent = null; updateReferences(this, object); !!this.view && this.view.invalidate(); } } }; THREE.Object3D.prototype.removeObjectTweens = function () { Tween.removeObjectTweens(this); return this; }; function traverse(obj, cb) { if (!!obj.material) cb(obj); for (var _iterator = obj.children, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref4; if (_isArray) { if (_i >= _iterator.length) break; _ref4 = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref4 = _i.value; } var _child = _ref4; _child.updateParentMaterials && traverse(_child, cb); } } THREE.Object3D.prototype.animate = function (properties) { return this.tweens ? new Tween(this, properties) : new Tween({}, {}); }; THREE.Material.prototype.tweens = true; THREE.Material.prototype.animate = function (properties) { return this.tweens ? new Tween(this, properties) : new Tween({}, {}); }; // This creates a property map for the animate function targeted to the material property // It uses material meta data to restore defaults THREE.Object3D.prototype.mapMaterial = function (props) { if (props === void 0) { props = {}; } var getProps = function getProps(material) { var overrides = props; if (typeof props === 'function') { overrides = props(material); if (overrides === null) return {}; } return _extends({}, (material.meta || {}).material, overrides); }; if (this.material) { return { material: Array.isArray(this.material) ? this.material.map(getProps) : getProps(this.material) }; } else { return {}; } }; THREE.Object3D.prototype.setValues = function (properties) { return new Tween(this, properties).start(0); }; THREE.Object3D.prototype.root = function () { var result, current = this; while (current) { if (!current.parent || current.parent instanceof THREE.Scene) return current; current = current.parent; } }; THREE.Object3D.prototype.updateBounds = function (box) { if (box === void 0) { box = undefined; } this.bounds = this.bounds || { box: new THREE.Box3(), sphere: new THREE.Sphere() }; this.bounds.box = !!box ? this.bounds.box.union(box) : new THREE.Box3().setFromObject(this); var boundingSphere = new THREE.Sphere(); this.bounds.box.getBoundingSphere(boundingSphere); this.bounds.sphere = boundingSphere; return this; }; THREE.Box3.prototype.expandByObject = function () { var v1 = new THREE.Vector3(); return function expandByObject(object) { var scope = this; object.updateMatrixWorld(true); object.traverseConditional(function (node) { var i, l; var geometry = node.geometry; var keepGoing = node.measurable && node.visible; if (geometry !== undefined && keepGoing) { if (geometry.isGeometry) { var vertices = geometry.vertices; for (i = 0, l = vertices.length; i < l; i++) { v1.copy(vertices[i]); v1.applyMatrix4(node.matrixWorld); scope.expandByPoint(v1); } } else if (geometry.isBufferGeometry) { var attribute = geometry.attributes.position; if (attribute !== undefined) { for (i = 0, l = attribute.count; i < l; i++) { v1.fromBufferAttribute(attribute, i).applyMatrix4(node.matrixWorld); scope.expandByPoint(v1); } } } } return keepGoing; }); return this; }; }(); THREE.Object3D.prototype.getCenter = function (force) { if (force || !this.bounds) this.updateBounds(); return this.bounds.sphere.center.clone(); }; THREE.Object3D.prototype.getRadius = function (force) { if (force || !this.bounds) this.updateBounds(); return this.bounds.sphere.radius; }; THREE.Object3D.prototype.centerGeometry = function (center, setPosition) { if (center === void 0) { center = this.getCenter(true); } if (setPosition === void 0) { setPosition = true; } setPosition && this.position.copy(center); if (!!this.geometry && (this.geometry instanceof THREE.Geometry || this.geometry instanceof THREE.BufferGeometry)) { this.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-center.x, -center.y, -center.z)); this.geometry.computeBoundingBox(); this.geometry.computeBoundingSphere(); } for (var _iterator2 = this.children, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { var _ref5; if (_isArray2) { if (_i2 >= _iterator2.length) break; _ref5 = _iterator2[_i2++]; } else { _i2 = _iterator2.next(); if (_i2.done) break; _ref5 = _i2.value; } var _child2 = _ref5; _child2.centerGeometry(center, false); } return this; }; THREE.Object3D.prototype.centerChildGeometry = function () { for (var _iterator3 = this.children, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { var _ref6; if (_isArray3) { if (_i3 >= _iterator3.length) break; _ref6 = _iterator3[_i3++]; } else { _i3 = _iterator3.next(); if (_i3.done) break; _ref6 = _i3.value; } var _child3 = _ref6; _child3.centerGeometry(); } return this; }; THREE.Object3D.prototype.addEventListener = function (type, listener) { return this.on(type, listener); }; THREE.Object3D.prototype.hasEventListener = function (type, listener) { return this.hasListener(type, listener); }; THREE.Object3D.prototype.removeEventListener = function (type, listener) { return this.removeListener(type, listener); }; THREE.Object3D.prototype.dispatchEvent = function (event) { for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } return this.emit.apply(this, [event].concat(args)); }; THREE.Object3D.prototype.setRenderOrder = function (pairs) { if (pairs === void 0) { pairs = Defaults.renderOrder; } this.traverse(function (item) { var value = pairs[item.type]; if (value !== undefined) { item.renderOrder = value; } }); return this; }; THREE.Object3D.prototype.find = function (condition) { if (condition(this)) return this; for (var _iterator4 = this.children, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) { var _ref7; if (_isArray4) { if (_i4 >= _iterator4.length) break; _ref7 = _iterator4[_i4++]; } else { _i4 = _iterator4.next(); if (_i4.done) break; _ref7 = _i4.value; } var _child4 = _ref7; var test = _child4.find(condition); if (test) return test; } }; THREE.Object3D.prototype.findParent = function (condition) { if (this.parent) { var result = condition(this.parent); if (result) return this.parent;else return this.parent.findParent(condition); } }; THREE.Object3D.prototype.findReverse = function (condition) { var result = condition(this); if (result) return this;else return this.parent && this.parent.findReverse(condition); }; THREE.Object3D.prototype.findMaterial = function (condition) { var material = undefined; var object = this.find(function (item) { if (item.material) { var materials = Array.isArray(item.material) ? item.material : [item.material]; for (var _iterator5 = materials, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) { var _ref8; if (_isArray5) { if (_i5 >= _iterator5.length) break; _ref8 = _iterator5[_i5++]; } else { _i5 = _iterator5.next(); if (_i5.done) break; _ref8 = _i5.value; } var _materialItem = _ref8; if (condition(_materialItem, item)) { material = _materialItem; return item; } } } }); return material ? { object: object, material: material } : undefined; }; THREE.Object3D.prototype.traverseConditional = function (callback) { if (callback(this)) { var children = this.children; for (var i = 0, l = children.length; i < l; i++) { children[i].traverseConditional(callback); } } }; THREE.Object3D.prototype.traverseMaterials = function (callback) { this.traverse(function (item) { if (item.material) { var isMultiMaterial = Array.isArray(item.material); if (isMultiMaterial) { item.material.forEach(function (multi) { return callback(multi, item); }); } else callback(item.material, item); } }); }; THREE.Object3D.prototype.getMaterials = function () { return this.material ? Array.isArray(this.material) ? this.material : [this.material] : []; }; THREE.Object3D.prototype.fadeIn = function (from) { if (from === void 0) { from = undefined; } this.traverse(function (item) { return item.material && item.animate(item.mapMaterial()).from(from && item.mapMaterial(from)).start(Defaults.fadeDuration); }); return this; }; THREE.Object3D.prototype.fadeOut = function (to) { if (to === void 0) { to = { opacity: 0.3 }; } this.traverse(function (item) { return item.material && item.animate(item.mapMaterial(to)).start(Defaults.fadeDuration); }); return this; }; THREE.Object3D.prototype.updateMaterials = function (cb, recursive) { if (recursive === void 0) { recursive = false; } if (recursive) { this.traverse(function (item) { return item.material && merge(item, item.mapMaterial(function (m) { return { meta: { material: cb(m) } }; })); }); } else { merge(this, this.mapMaterial(function (m) { return { meta: { material: cb(m) } }; })); } return this.fadeIn(); };