UNPKG

awv3

Version:
1 lines 17.2 kB
"use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function updateReferences(e,t){return t.parent===e&&e instanceof _three2.default.Scene||e.view?t.traverse(function(t){t.scene=e instanceof _three2.default.Scene?e:e.scene,t.view=e.view,t.canvas=e.canvas,t.emit(_three2.default.Object3D.Events.Lifecycle.ViewFound,t.view)}):t.traverse(function(e){e.scene=void 0,e.view=void 0,e.canvas=void 0}),t}function traverse(e,t){e.material&&t(e);for(var r=e.children,n=Array.isArray(r),i=0,r=n?r:(0,_getIterator3.default)(r);;){var a;if(n){if(i>=r.length)break;a=r[i++]}else{if(i=r.next(),i.done)break;a=i.value}var s=a;s.updateParentMaterials&&traverse(s,t)}}exports.__esModule=!0;var _getIterator2=require("babel-runtime/core-js/get-iterator"),_getIterator3=_interopRequireDefault(_getIterator2),_from=require("babel-runtime/core-js/array/from"),_from2=_interopRequireDefault(_from),_regenerator=require("babel-runtime/regenerator"),_regenerator2=_interopRequireDefault(_regenerator),_asyncToGenerator2=require("babel-runtime/helpers/asyncToGenerator"),_asyncToGenerator3=_interopRequireDefault(_asyncToGenerator2),_promise=require("babel-runtime/core-js/promise"),_promise2=_interopRequireDefault(_promise),_keys=require("babel-runtime/core-js/object/keys"),_keys2=_interopRequireDefault(_keys),_extends2=require("babel-runtime/helpers/extends"),_extends3=_interopRequireDefault(_extends2),_classCallCheck2=require("babel-runtime/helpers/classCallCheck"),_classCallCheck3=_interopRequireDefault(_classCallCheck2),_possibleConstructorReturn2=require("babel-runtime/helpers/possibleConstructorReturn"),_possibleConstructorReturn3=_interopRequireDefault(_possibleConstructorReturn2),_inherits2=require("babel-runtime/helpers/inherits"),_inherits3=_interopRequireDefault(_inherits2),_three=require("three"),_three2=_interopRequireDefault(_three),_defaults=require("../core/defaults"),_defaults2=_interopRequireDefault(_defaults),_events=require("../core/events"),_events2=_interopRequireDefault(_events),_tween=require("../animation/tween"),_tween2=_interopRequireDefault(_tween),_raycaster=require("../three/raycaster"),_raycaster2=_interopRequireDefault(_raycaster),Object3=function(e){function t(){var r=arguments.length<=0||void 0===arguments[0]?void 0:arguments[0],n=arguments.length<=1||void 0===arguments[1]?void 0:arguments[1];(0,_classCallCheck3.default)(this,t);var i=(0,_possibleConstructorReturn3.default)(this,e.call(this));return n&&i.setValues(n),r&&i.add(r),i}return(0,_inherits3.default)(t,e),t}(_three2.default.Object3D);exports.default=Object3,_three2.default.Object3D.Events={Lifecycle:{Loaded:"Loaded",Added:"Added",ViewFound:"ViewFound",ChildAdded:"ChildAdded",Removed:"Removed",ChildRemoved:"ChildRemoved",Destroyed:"Destroyed",Rendered:"Rendered"},Interaction:{Clicked:"Clicked",Picked:"Picked",Dropped:"Dropped",Dragged:"Dragged",Hovered:"Hovered",Unhovered:"Unhovered",Missed:"Missed"}},_three2.default.Object3D.prototype.canvas=void 0,_three2.default.Object3D.prototype.view=void 0,_three2.default.Object3D.prototype.bounds=void 0,_three2.default.Object3D.prototype.materials=void 0,_three2.default.Object3D.prototype.updateParentMaterials=!0,_three2.default.Object3D.prototype.keep=!1,_three2.default.Object3D.prototype.tweens=!0,_three2.default.Object3D.prototype.interactive=!0,_three2.default.Object3D.prototype.interaction=void 0,_events2.default.mixin(_three2.default.Object3D.prototype),_three2.default.Object3D.prototype.createInteraction=function(){var e=this,t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];if(this.interaction)return this;this.interaction=(0,_extends3.default)({first:!0,faces:!1,priority:0,enabled:!0,recursive:!1,approach:_raycaster2.default.Approach.Default,types:["Mesh"]},t);var r=this;return this.viewFound().then(function(e){e.interaction.targets.indexOf(r)==-1&&!function(){e.interaction.targets.push(r);var t=0;e.scene.traverse(function(e){return e.depthIndex=t++}),e.interaction.targets.sort(function(e,t){return e.depthIndex-t.depthIndex})}()}),this.inspect(function(t){return e.interaction._active=!!(0,_keys2.default)(e._callbacks).find(function(e){return e in _three2.default.Object3D.Events.Interaction})}),this},_three2.default.Object3D.prototype.removeInteraction=function(){this.view&&this.interaction&&(this.interaction=void 0,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]))},_three2.default.Object3D.prototype.viewFound=function(){var e=this;return this.view?_promise2.default.resolve(this.view):new _promise2.default(function(t){return e.once(_three2.default.Object3D.Events.Lifecycle.ViewFound,t)})},_three2.default.Object3D.prototype._destroyRecursive=function(e){if(this.geometry&&this.geometry.dispose(),this.material&&this.material.dispose(),this.materials&&(this.materials.all.forEach(function(e){return e.dispose()}),this.materials.all=[],this.materials.meshes=[],this.materials.lines=[],this.bounds=void 0),this.userData={},e.async)return _promise2.default.all(this.children.map(function(t){return t.destroyAsync(e)}));for(var t=this.children.length-1;t>=0;t--)this.children[t].destroy(e)},_three2.default.Object3D.prototype.destroyAsync=function(){var e=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(){var t=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t=(0,_extends3.default)({keep:!0,data:!0,interaction:!0,listeners:!0},t),!t.keep||!this.keep){e.next=3;break}return e.abrupt("return");case 3:return t.interaction&&this.removeInteraction(),e.next=6,this.emit(Object3.Events.Lifecycle.Destroyed);case 6:if(e.t0=this.parent,!e.t0){e.next=10;break}return e.next=10,this.parent.removeAsync(this);case 10:if(t.listeners&&this.removeListeners(),this.removeObjectTweens(),e.t1=t.data,!e.t1){e.next=16;break}return e.next=16,this._destroyRecursive((0,_extends3.default)({},t,{async:!0}));case 16:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}(),_three2.default.Object3D.prototype.destroy=function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];e=(0,_extends3.default)({keep:!0,data:!0,interaction:!0,listeners:!0},e),e.keep&&this.keep||(this.emit(Object3.Events.Lifecycle.Destroyed),this.parent&&this.parent.remove(this),e.interaction&&this.removeInteraction(),e.listeners&&this.removeListeners(),this.removeObjectTweens(),e.data&&this._destroyRecursive(e))},_three2.default.Object3D.prototype.addAsync=function(){var e=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){var r=this,n=arguments;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t=n.length>1?(0,_from2.default)(n):t,!Array.isArray(t)){e.next=3;break}return e.abrupt("return",_promise2.default.all(t.map(function(e){return r.addAsync(e)})));case 3:if(!(t!==this&&t instanceof _three2.default.Object3D)){e.next=14;break}if(!t.parent){e.next=7;break}return e.next=7,t.parent.removeAsync(t);case 7:return t.parent=this,this.children.push(updateReferences(this,t)),e.next=11,this.emit(Object3.Events.Lifecycle.ChildAdded,{child:t});case 11:return e.next=13,t.emit(Object3.Events.Lifecycle.Added,{parent:this});case 13:!!this.view&&this.view.invalidate();case 14:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}(),_three2.default.Object3D.prototype.add=function(e){var t=this;return e=arguments.length>1?(0,_from2.default)(arguments):e,Array.isArray(e)?_promise2.default.all(e.map(function(e){return t.add(e)})):void(e!==this&&e instanceof _three2.default.Object3D&&(e.parent&&e.parent.remove(e),e.parent=this,this.children.push(updateReferences(this,e)),this.emit(Object3.Events.Lifecycle.ChildAdded,{child:e}),e.emit(Object3.Events.Lifecycle.Added,{parent:this}),!!this.view&&this.view.invalidate()))},_three2.default.Object3D.prototype.removeAsync=function(){var e=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){var r,n=this,i=arguments;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t=i.length>1?(0,_from2.default)(i):t,!Array.isArray(t)){e.next=3;break}return e.abrupt("return",_promise2.default.all(t.map(function(e){return n.removeAsync(e)})));case 3:if(!(t instanceof _three2.default.Object3D)){e.next=14;break}if(r=this.children.indexOf(t),r===-1){e.next=14;break}return e.next=8,this.emit(Object3.Events.Lifecycle.ChildRemoved,{child:t});case 8:return e.next=10,t.emit(Object3.Events.Lifecycle.Removed,{parent:this});case 10:this.children.splice(r,1),t.parent=null,updateReferences(this,t),!!this.view&&this.view.invalidate();case 14:case"end":return e.stop()}},e,this)}));return function(t){return e.apply(this,arguments)}}(),_three2.default.Object3D.prototype.remove=function(e){var t=this;if(e=arguments.length>1?(0,_from2.default)(arguments):e,Array.isArray(e))return _promise2.default.all(e.map(function(e){return t.remove(e)}));if(e instanceof _three2.default.Object3D){var r=this.children.indexOf(e);r!==-1&&(this.emit(Object3.Events.Lifecycle.ChildRemoved,{child:e}),e.emit(Object3.Events.Lifecycle.Removed,{parent:this}),this.children.splice(r,1),e.parent=null,updateReferences(this,e),!!this.view&&this.view.invalidate())}},_three2.default.Object3D.prototype.removeObjectTweens=function(){return _tween2.default.removeObjectTweens(this),this},_three2.default.Object3D.prototype.updateMaterials=function(){var e=this,t={};return this.materials=this.materials||{updateParent:!0,all:[],meshes:[],lines:[]},this.materials.all=[],this.materials.meshes=[],this.materials.lines=[],traverse(this,function(e){return t[e.material.uuid]=e.material}),(0,_keys2.default)(t).forEach(function(r){var n=t[r];e.materials.all.push(n),n.type.indexOf("Mesh")>-1?e.materials.meshes.push(n):n.type.indexOf("Line")>-1&&e.materials.lines.push(n)}),this},_three2.default.Object3D.prototype.compress=function(){var e=this;if(this.materials){for(var t=((0,_keys2.default)(this.materials.all),{}),r=function(){a=e.materials.all[n],s="#"+a.color.getHexString()+"_"+a.opacity.toFixed(2)+"_"+a.type;var r=t[s];r?e.traverse(function(e){e.material===a&&(e.material.dispose(),e.material=r)}):t[s]=a},n=0,i=this.materials.all.length;n<i;n++){var a,s;r()}return this.updateMaterials(),this}},_three2.default.Object3D.prototype.animate=function(e){return this.tweens?new _tween2.default(this,e):new _tween2.default({},{})},_three2.default.Material.prototype.tweens=!0,_three2.default.Material.prototype.animate=function(e){return this.tweens?new _tween2.default(this,e):new _tween2.default({},{})},_three2.default.Object3D.prototype.animateMaterials=function(e){return this.tweens?new _tween2.default(this,{materials:e}):new _tween2.default({},{})},_three2.default.Object3D.prototype.animateAllMaterials=function(e){return this.tweens?new _tween2.default(this,{materials:{all:[e]}}):new _tween2.default({},{})},_three2.default.Object3D.prototype.animateMeshes=function(e){return this.tweens?new _tween2.default(this,{materials:{meshes:[e]}}):new _tween2.default({},{})},_three2.default.Object3D.prototype.animateLines=function(e){return this.tweens?new _tween2.default(this,{materials:{lines:[e]}}):new _tween2.default({},{})},_three2.default.Object3D.prototype.fadeOut=function(e){return this.animate({materials:{meshes:[{opacity:0}],lines:[{opacity:0}]}}).start(e||0),this},_three2.default.Object3D.prototype.fadeIn=function(e,t){return this.animate({materials:{meshes:[{opacity:t||1}],lines:[{opacity:t||1}]}}).start(e||0),this},_three2.default.Object3D.prototype.setValues=function(e){return new _tween2.default(this,e).start(0)},_three2.default.Object3D.prototype.root=function(){for(var e=this;e;){if(!e.parent||e.parent instanceof _three2.default.Scene)return e;e=e.parent}},_three2.default.Object3D.prototype.pwtl=function(e){return this.parent.worldToLocal(e.clone())},_three2.default.Object3D.prototype.pltw=function(e){return this.parent.localToWorld(e.clone())},_three2.default.Object3D.prototype.isChildOf=function(e){for(var t=this;t;){if(t==e)return!0;t=t.parent}return!1},_three2.default.Object3D.prototype.isVisible=function(){return!(!this.visible||this instanceof _three2.default.Mesh&&0==this.material.opacity)&&(null==this.parent||this.parent instanceof _three2.default.Scene?this.visible:!!this.visible&&this.parent.isVisible())},_three2.default.Object3D.prototype.find=function(e){if(e(this))return this;for(var t=this.children,r=Array.isArray(t),n=0,t=r?t:(0,_getIterator3.default)(t);;){var i;if(r){if(n>=t.length)break;i=t[n++]}else{if(n=t.next(),n.done)break;i=n.value}var a=i,s=a.find(e);if(s)return s}},_three2.default.Object3D.prototype.getObjectByMatch=function(e){for(var t=0,r=this.children.length;t<r;t++){var n=this.children[t];if(n.name.indexOf(e)>-1)return n}},_three2.default.Object3D.prototype.getObjectByUserId=function(e){if(this.userData&&this.userData.id===e)return this;for(var t=0,r=this.children.length;t<r;t++){var n=this.children[t],i=n.getObjectByUserId(e);if(void 0!==i)return i}},_three2.default.Object3D.prototype.lastChild=function(){return this.children.length?this.children[this.children.length-1]:void 0},_three2.default.Object3D.prototype.setPosition=function(e){return e=arguments.length>1?new(Function.prototype.bind.apply(_three2.default.Vector3,[null].concat(Array.prototype.slice.call(arguments)))):e,this.position.copy(e),this},_three2.default.Object3D.prototype.setRotation=function(e){return e=arguments.length>1?new(Function.prototype.bind.apply(_three2.default.Euler,[null].concat(Array.prototype.slice.call(arguments)))):e,this.rotation.copy(e),this},_three2.default.Object3D.prototype.setScale=function(e){return e=arguments.length>1?new(Function.prototype.bind.apply(_three2.default.Vector3,[null].concat(Array.prototype.slice.call(arguments)))):e,this.scale.copy(e),this},_three2.default.Object3D.prototype.setRenderOrder=function(e,t){return this.traverse(function(r){(r instanceof _three2.default.Mesh||t&&r instanceof _three2.default.Line)&&(r.renderOrder=e)}),this},_three2.default.Object3D.prototype.updateBounds=function(){var e=arguments.length<=0||void 0===arguments[0]?void 0:arguments[0];return this.bounds=this.bounds||{box:new _three2.default.Box3,sphere:new _three2.default.Sphere},this.bounds.box=e?this.bounds.box.union(e):(new _three2.default.Box3).setFromObject(this),this.bounds.sphere=this.bounds.box.getBoundingSphere(),this.view&&this.view.updateBounds(e?this.bounds.box:void 0),this},_three2.default.Object3D.prototype.getCenter=function(e){return!e&&this.bounds||this.updateBounds(),this.bounds.sphere.center.clone()},_three2.default.Object3D.prototype.getRadius=function(e){return!e&&this.bounds||this.updateBounds(),this.bounds.sphere.radius},_three2.default.Object3D.prototype.centerGeometry=function(){var e=arguments.length<=0||void 0===arguments[0]?this.getCenter(!0):arguments[0],t=arguments.length<=1||void 0===arguments[1]||arguments[1];t&&this.position.copy(e),this.geometry&&(this.geometry instanceof _three2.default.Geometry||this.geometry instanceof _three2.default.BufferGeometry)&&(this.geometry.applyMatrix((new _three2.default.Matrix4).makeTranslation(-e.x,-e.y,-e.z)),this.geometry.computeBoundingBox(),this.geometry.computeBoundingSphere());for(var r=this.children,n=Array.isArray(r),i=0,r=n?r:(0,_getIterator3.default)(r);;){var a;if(n){if(i>=r.length)break;a=r[i++]}else{if(i=r.next(),i.done)break;a=i.value}var s=a;s.centerGeometry(e,!1)}return this},_three2.default.Object3D.prototype.centerChildGeometry=function(){for(var e=this.children,t=Array.isArray(e),r=0,e=t?e:(0,_getIterator3.default)(e);;){var n;if(t){if(r>=e.length)break;n=e[r++]}else{if(r=e.next(),r.done)break;n=r.value}var i=n;i.centerGeometry()}return this},_three2.default.Object3D.prototype.addEventListener=function(e,t){return this.on(e,t)},_three2.default.Object3D.prototype.hasEventListener=function(e,t){return this.hasListener(e,t)},_three2.default.Object3D.prototype.removeEventListener=function(e,t){return this.removeListener(e,t)},_three2.default.Object3D.prototype.dispatchEvent=function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;n<t;n++)r[n-1]=arguments[n];return this.emit.apply(this,[e].concat(r))},_three2.default.Object3D.prototype.setRenderOrder=function(){var e=arguments.length<=0||void 0===arguments[0]?_defaults2.default.renderOrder:arguments[0];this.traverse(function(t){var r=e[t.type];void 0!==r&&(t.renderOrder=r)})},_three2.default.Object3D.RenderOrder={Default:{Mesh:0,LineSegments:100},LinesFirst:{Mesh:0,LineSegments:100},MeshesFirst:{Mesh:100,LineSegments:0}},_three2.default.Object3D.prototype.traverseConditional=function(e){var t=e(this);if(t)for(var r=this.children,n=0,i=r.length;n<i;n++)r[n].traverseConditional(e)},_three2.default.Object3D.prototype.traverseMaterials=function(e){this.traverse(function(t){t.material&&(t.material instanceof _three2.default.MultiMaterial?t.material.materials.forEach(function(r){return e(r,t)}):e(t.material,t))})};