sphere-collider.min.js 2.3 KB

1
  1. !function t(e,i,n){function r(o,l){if(!i[o]){if(!e[o]){var a="function"==typeof require&&require;if(!l&&a)return a(o,!0);if(s)return s(o,!0);var h=new Error("Cannot find module '"+o+"'");throw h.code="MODULE_NOT_FOUND",h}var c=i[o]={exports:{}};e[o][0].call(c.exports,function(t){var i=e[o][1][t];return r(i||t)},c,c.exports,t,e,i,n)}return i[o].exports}for(var s="function"==typeof require&&require,o=0;o<n.length;o++)r(n[o]);return r}({1:[function(t,e,i){"use strict";t("./src/misc/sphere-collider")},{"./src/misc/sphere-collider":2}],2:[function(t,e,i){"use strict";e.exports=AFRAME.registerComponent("sphere-collider",{schema:{objects:{default:""},state:{default:"collided"},radius:{default:.05},watch:{default:!0}},init:function(){this.observer=null,this.els=[],this.collisions=[],this.handleHit=this.handleHit.bind(this),this.handleHitEnd=this.handleHitEnd.bind(this)},remove:function(){this.pause()},play:function(){var t=this.el.sceneEl;this.data.watch&&(this.observer=new MutationObserver(this.update.bind(this,null)),this.observer.observe(t,{childList:!0,subtree:!0}))},pause:function(){this.observer&&(this.observer.disconnect(),this.observer=null)},update:function(){var t=this.data,e=void 0;e=t.objects?this.el.sceneEl.querySelectorAll(t.objects):this.el.sceneEl.children,this.els=Array.prototype.slice.call(e)},tick:function(){var t=new THREE.Vector3,e=new THREE.Vector3,i=new THREE.Vector3,n=new THREE.Vector3,r=new THREE.Box3,s=new Map;return function(){var o=this.el,l=this.data,a=[],h=void 0;o.getObject3D("mesh")&&(s.clear(),o.object3D.getWorldPosition(t),o.object3D.getWorldScale(i),h=l.radius*function(t){return Math.max.apply(null,t.toArray())}(i),this.els.forEach(function(i){var o=void 0,l=void 0,c=void 0,u=void 0;i.isEntity&&(l=i.getObject3D("mesh"))&&(r.setFromObject(l).getSize(n),u=Math.max(n.x,n.y,n.z)/2,o=Math.sqrt(2*u*u),r.getCenter(e),o&&(c=t.distanceTo(e))<o+h&&(a.push(i),s.set(i,c)))}),a.sort(function(t,e){return s.get(t)>s.get(e)?1:-1}).forEach(this.handleHit),0===a.length&&o.emit("hit",{el:null}),this.collisions.filter(function(t){return!s.has(t)}).forEach(this.handleHitEnd),this.collisions=a)}}(),handleHit:function(t){t.emit("hit"),t.addState(this.data.state),this.el.emit("hit",{el:t})},handleHitEnd:function(t){t.emit("hitend"),t.removeState(this.data.state),this.el.emit("hitend",{el:t})}})},{}]},{},[1]);