UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 1.72 kB
import{EventHandler as t}from"../../core/event-handler.js";import{Quat as e}from"../../core/math/quat.js";import{Vec3 as s}from"../../core/math/vec3.js";const i=[],r=[];class n extends t{constructor(t,e,s,i=null){super(),this.manager=void 0,this._xrHitTestSource=void 0,this._transient=void 0,this._inputSource=void 0,this.manager=t,this._xrHitTestSource=e,this._transient=s,this._inputSource=i}remove(){if(!this._xrHitTestSource)return;const t=this.manager.hitTest.sources,e=t.indexOf(this);-1!==e&&t.splice(e,1),this.onStop()}onStop(){this._xrHitTestSource.cancel(),this._xrHitTestSource=null,this.fire("remove"),this.manager.hitTest.fire("remove",this)}update(t){if(this._transient){const e=t.getHitTestResultsForTransientInput(this._xrHitTestSource);for(let t=0;t<e.length;t++){const s=e[t];if(!s.results.length)continue;let i;s.inputSource&&(i=this.manager.input._getByInputSource(s.inputSource)),this.updateHitResults(s.results,i)}}else{const e=t.getHitTestResults(this._xrHitTestSource);if(!e.length)return;this.updateHitResults(e)}}updateHitResults(t,n){var o,u,h;if(this._inputSource&&this._inputSource!==n)return;const c=null!=(o=i.pop())?o:new s;n?c.copy(n.getOrigin()):c.copy(this.manager.camera.getPosition());let a=1/0,p=null;const l=null!=(u=i.pop())?u:new s,m=null!=(h=r.pop())?h:new e;for(let e=0;e<t.length;e++){const s=t[e].getPose(this.manager._referenceSpace),i=c.distance(s.transform.position);i>=a||(a=i,p=t[e],l.copy(s.transform.position),m.copy(s.transform.orientation))}this.fire("result",l,m,n||this._inputSource,p),this.manager.hitTest.fire("result",this,l,m,n||this._inputSource,p),i.push(c),i.push(l),r.push(m)}}n.EVENT_REMOVE="remove",n.EVENT_RESULT="result";export{n as XrHitTestSource};