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 Map;return function(){var r=this.el,s=this.data,o=[],l=void 0;r.getObject3D("mesh")&&(n.clear(),t.copy(r.object3D.getWorldPosition()),r.object3D.getWorldScale(i),l=s.radius*function(t){return Math.max.apply(null,t.toArray())}(i),this.els.forEach(function(i){var r=void 0,s=void 0,a=void 0,h=void 0,c=void 0,u=void 0;i.isEntity&&(s=i.getObject3D("mesh"))&&(u=(h=(new THREE.Box3).setFromObject(s)).getSize(),c=Math.max(u.x,u.y,u.z)/2,r=Math.sqrt(2*c*c),h.getCenter(e),r&&(a=t.distanceTo(e))<r+l&&(o.push(i),n.set(i,a)))}),o.sort(function(t,e){return n.get(t)>n.get(e)?1:-1}).forEach(this.handleHit),0===o.length&&r.emit("hit",{el:null}),this.collisions.filter(function(t){return!n.has(t)}).forEach(this.handleHitEnd),this.collisions=o)}}(),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]);