@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 5.12 kB
JavaScript
import{EventHandler as t}from"../../core/event-handler.js";import{Mat4 as i}from"../../core/math/mat4.js";import{Quat as e}from"../../core/math/quat.js";import{Vec3 as s}from"../../core/math/vec3.js";import{Ray as r}from"../../core/shape/ray.js";import{XrHand as o}from"./xr-hand.js";import{now as n}from"../../core/time.js";const a=new s,h=new e;let l=0;class _ extends t{constructor(t,i){super(),this._id=void 0,this._manager=void 0,this._xrInputSource=void 0,this._ray=new r,this._rayLocal=new r,this._grip=!1,this._hand=null,this._velocitiesAvailable=!1,this._velocitiesTimestamp=n(),this._localTransform=null,this._worldTransform=null,this._position=new s,this._rotation=new e,this._localPosition=null,this._localPositionLast=null,this._localRotation=null,this._linearVelocity=null,this._dirtyLocal=!0,this._dirtyRay=!1,this._selecting=!1,this._squeezing=!1,this._elementInput=!0,this._elementEntity=null,this._hitTestSources=[],this._id=++l,this._manager=t,this._xrInputSource=i,i.hand&&(this._hand=new o(this))}get id(){return this._id}get inputSource(){return this._xrInputSource}get targetRayMode(){return this._xrInputSource.targetRayMode}get handedness(){return this._xrInputSource.handedness}get profiles(){return this._xrInputSource.profiles}get grip(){return this._grip}get hand(){return this._hand}get gamepad(){return this._xrInputSource.gamepad||null}get selecting(){return this._selecting}get squeezing(){return this._squeezing}set elementInput(t){this._elementInput!==t&&(this._elementInput=t,this._elementInput||(this._elementEntity=null))}get elementInput(){return this._elementInput}get elementEntity(){return this._elementEntity}get hitTestSources(){return this._hitTestSources}update(t){if(this._hand)this._hand.update(t);else{const r=this._xrInputSource.gripSpace;if(r){const o=t.getPose(r,this._manager._referenceSpace);if(o){this._grip||(this._grip=!0,this._localTransform=new i,this._worldTransform=new i,this._localPositionLast=new s,this._localPosition=new s,this._localRotation=new e,this._linearVelocity=new s);const t=n(),r=(t-this._velocitiesTimestamp)/1e3;this._velocitiesTimestamp=t,this._dirtyLocal=!0,this._localPositionLast.copy(this._localPosition),this._localPosition.copy(o.transform.position),this._localRotation.copy(o.transform.orientation),this._velocitiesAvailable=!0,this._manager.input.velocitiesSupported&&o.linearVelocity?this._linearVelocity.copy(o.linearVelocity):r>0&&(a.sub2(this._localPosition,this._localPositionLast).divScalar(r),this._linearVelocity.lerp(this._linearVelocity,a,.15))}else this._velocitiesAvailable=!1}const o=t.getPose(this._xrInputSource.targetRaySpace,this._manager._referenceSpace);o&&(this._dirtyRay=!0,this._rayLocal.origin.copy(o.transform.position),this._rayLocal.direction.set(0,0,-1),h.copy(o.transform.orientation),h.transformVector(this._rayLocal.direction,this._rayLocal.direction))}}_updateTransforms(){this._dirtyLocal&&(this._dirtyLocal=!1,this._localTransform.setTRS(this._localPosition,this._localRotation,s.ONE));const t=this._manager.camera.parent;t?this._worldTransform.mul2(t.getWorldTransform(),this._localTransform):this._worldTransform.copy(this._localTransform)}_updateRayTransforms(){const t=this._dirtyRay;this._dirtyRay=!1;if(this._manager.camera.parent){const t=this._manager.camera.parent.getWorldTransform();t.getTranslation(this._position),this._rotation.setFromMat4(t),this._rotation.transformVector(this._rayLocal.origin,this._ray.origin),this._ray.origin.add(this._position),this._rotation.transformVector(this._rayLocal.direction,this._ray.direction)}else t&&(this._ray.origin.copy(this._rayLocal.origin),this._ray.direction.copy(this._rayLocal.direction))}getPosition(){return this._position?(this._updateTransforms(),this._worldTransform.getTranslation(this._position),this._position):null}getLocalPosition(){return this._localPosition}getRotation(){return this._rotation?(this._updateTransforms(),this._rotation.setFromMat4(this._worldTransform),this._rotation):null}getLocalRotation(){return this._localRotation}getLinearVelocity(){return this._velocitiesAvailable?this._linearVelocity:null}getOrigin(){return this._updateRayTransforms(),this._ray.origin}getDirection(){return this._updateRayTransforms(),this._ray.direction}hitTestStart(t={}){t.inputSource=this,t.profile=this._xrInputSource.profiles[0];const i=t.callback;t.callback=(t,e)=>{e&&this.onHitTestSourceAdd(e),i&&i(t,e)},this._manager.hitTest.start(t)}onHitTestSourceAdd(t){this._hitTestSources.push(t),this.fire("hittest:add",t),t.on("result",((i,e,s,r)=>{s===this&&this.fire("hittest:result",t,i,e,r)})),t.once("remove",(()=>{this.onHitTestSourceRemove(t),this.fire("hittest:remove",t)}))}onHitTestSourceRemove(t){const i=this._hitTestSources.indexOf(t);-1!==i&&this._hitTestSources.splice(i,1)}}_.EVENT_REMOVE="remove",_.EVENT_SELECT="select",_.EVENT_SELECTSTART="selectstart",_.EVENT_SELECTEND="selectend",_.EVENT_SQUEEZE="squeeze",_.EVENT_SQUEEZESTART="squeezestart",_.EVENT_SQUEEZEEND="squeezeend",_.EVENT_HITTESTADD="hittest:add",_.EVENT_HITTESTREMOVE="hittest:remove",_.EVENT_HITTESTRESULT="hittest:result";export{_ as XrInputSource};