aframe-aabb-collider-component.min.js 5.1 KB

1
  1. !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var s=t();for(var i in s)("object"==typeof exports?exports:e)[i]=s[i]}}("undefined"!=typeof self?self:this,function(){return function(e){function t(i){if(s[i])return s[i].exports;var o=s[i]={i:i,l:!1,exports:{}};return e[i].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var s={};return t.m=e,t.c=s,t.d=function(e,s,i){t.o(e,s)||Object.defineProperty(e,s,{configurable:!1,enumerable:!0,get:i})},t.n=function(e){var s=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(s,"a",s),s},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,s){"use strict";function i(e,t){e.length=0;for(var s=0;s<t.length;s++)e[s]=t[s]}if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");var o={childList:!0,attributes:!0,subtree:!0};AFRAME.registerComponent("aabb-collider",{schema:{collideNonVisible:{default:!1},debug:{default:!1},enabled:{default:!0},interval:{default:80},objects:{default:""}},init:function(){this.centerDifferenceVec3=new THREE.Vector3,this.clearedIntersectedEls=[],this.closestIntersectedEl=null,this.boundingBox=new THREE.Box3,this.boxCenter=new THREE.Vector3,this.boxHelper=new THREE.BoxHelper,this.boxMax=new THREE.Vector3,this.boxMin=new THREE.Vector3,this.hitClosestClearEventDetail={},this.hitClosestEventDetail={},this.intersectedEls=[],this.objectEls=[],this.newIntersectedEls=[],this.prevCheckTime=void 0,this.previousIntersectedEls=[],this.setDirty=this.setDirty.bind(this),this.observer=new MutationObserver(this.setDirty),this.dirty=!0,this.hitStartEventDetail={intersectedEls:this.newIntersectedEls}},play:function(){this.observer.observe(this.el.sceneEl,o),this.el.sceneEl.addEventListener("object3dset",this.setDirty),this.el.sceneEl.addEventListener("object3dremove",this.setDirty)},remove:function(){this.observer.disconnect(),this.el.sceneEl.removeEventListener("object3dset",this.setDirty),this.el.sceneEl.removeEventListener("object3dremove",this.setDirty)},tick:function(e){var t=this.boundingBox,s=this.centerDifferenceVec3,o=this.clearedIntersectedEls,n=this.el,r=this.intersectedEls,l=this.newIntersectedEls,c=this.objectEls,h=this.prevCheckTime,a=this.previousIntersectedEls,b=void 0,d=void 0,E=void 0;if(this.data.enabled&&!(h&&e-h<this.data.interval)){for(this.prevCheckTime=e,this.dirty&&this.refreshObjects(),t.setFromObject(n.object3D),this.boxMin.copy(t.min),this.boxMax.copy(t.max),t.getCenter(this.boxCenter),this.data.debug&&(this.boxHelper.setFromObject(n.object3D),this.boxHelper.parent||n.sceneEl.object3D.add(this.boxHelper)),i(a,r),r.length=0,E=0;E<c.length;E++)if(c[E]!==this.el)if(this.data.collideNonVisible||c[E].getAttribute("visible"))this.isIntersecting(c[E])&&r.push(c[E]);else if(this.data.debug){var u=c[E].object3D.boxHelper;u&&(n.sceneEl.object3D.remove(u),c[E].object3D.boxHelper=null)}for(l.length=0,E=0;E<r.length;E++)-1===a.indexOf(r[E])&&l.push(r[E]);for(o.length=0,E=0;E<a.length;E++)-1===r.indexOf(a[E])&&(a[E].hasAttribute("aabb-collider")||a[E].emit("hitend"),o.push(a[E]));for(E=0;E<l.length;E++)l[E]!==this.el&&(l[E].hasAttribute("aabb-collider")||l[E].emit("hitstart"));for(E=0;E<r.length;E++)r[E]!==this.el&&(s.copy(r[E].object3D.boundingBoxCenter).sub(this.boxCenter),(void 0===b||s.length()<b)&&(b=s.length(),d=r[E]));!r.length&&this.closestIntersectedEl?(this.hitClosestClearEventDetail.el=this.closestIntersectedEl,this.closestIntersectedEl.emit("hitclosestclear"),this.closestIntersectedEl=null,n.emit("hitclosestclear",this.hitClosestClearEventDetail)):d!==this.closestIntersectedEl&&(this.closestIntersectedEl&&(this.hitClosestClearEventDetail.el=this.closestIntersectedEl,this.closestIntersectedEl.emit("hitclosestclear",this.hitClosestClearEventDetail)),d&&(d.emit("hitclosest"),this.closestIntersectedEl=d,this.hitClosestEventDetail.el=d,n.emit("hitclosest",this.hitClosestEventDetail))),o.length&&n.emit("hitend"),l.length&&n.emit("hitstart",this.hitStartEventDetail)}},isIntersecting:function(){var e=new THREE.Box3;return function(t){var s=void 0;t.dataset.aabbColliderDynamic&&(e.setFromObject(t.object3D),s=e,t.object3D.boundingBoxCenter=t.object3D.boundingBoxCenter||new THREE.Vector3,s.getCenter(t.object3D.boundingBoxCenter)),t.dataset.aabbColliderDynamic||(t.object3D.aabbBox||(t.object3D.aabbBox=(new THREE.Box3).setFromObject(t.object3D),t.object3D.boundingBoxCenter=new THREE.Vector3,t.object3D.aabbBox.getCenter(t.object3D.boundingBoxCenter)),s=t.object3D.aabbBox),this.data.debug&&(t.object3D.boxHelper||(t.object3D.boxHelper=new THREE.BoxHelper(t.object3D,new THREE.Color(Math.random(),Math.random(),Math.random())),t.sceneEl.object3D.add(t.object3D.boxHelper)),t.object3D.boxHelper.setFromObject(t.object3D));var i=s.min,o=s.max;return this.boxMin.x<=o.x&&this.boxMax.x>=i.x&&this.boxMin.y<=o.y&&this.boxMax.y>=i.y&&this.boxMin.z<=o.z&&this.boxMax.z>=i.z}}(),setDirty:function(){this.dirty=!0},refreshObjects:function(){var e=this.data;this.objectEls=e.objects?this.el.sceneEl.querySelectorAll(e.objects):this.el.sceneEl.children,this.dirty=!1}})}])});