Last active
May 9, 2024 20:04
-
-
Save jameskane05/e54e9ca42eb532b226f4bdf8a5c7c231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
update() { | |
let transientPointerFoundThisFrame = false; | |
for (let input of this.engine.xr.session.inputSources) { | |
if (input.targetRayMode !== 'transient-pointer') continue; | |
// If we made it this far, we're dealing w/ a transient-pointer | |
transientPointerFoundThisFrame = true; | |
// Get the pose from the session from using the targetRaySpace and the currentRefSpace | |
// https://developer.mozilla.org/en-US/docs/Web/API/XRFrame/getPose | |
this.controllerPose = this.engine.xr.frame.getPose( | |
input.targetRaySpace, | |
this.engine.xr.currentReferenceSpace | |
); | |
// Returns an XRPose containing an XRRigidTransform: | |
// https://developer.mozilla.org/en-US/docs/Web/API/XRPose | |
// https://developer.mozilla.org/en-US/docs/Web/API/XRRigidTransform | |
// Format data for gl-matrix types vec3 and quat | |
let posePosition = vec3.fromValues( | |
this.controllerPose.transform.position.x, | |
this.controllerPose.transform.position.y, | |
this.controllerPose.transform.position.z | |
) | |
let rotQuat = quat.fromValues( | |
this.controllerPose.transform.orientation.x, | |
this.controllerPose.transform.orientation.y, | |
this.controllerPose.transform.orientation.z, | |
this.controllerPose.transform.orientation.w, | |
) | |
// the returned pose is in reference space | |
// add the world position of its reference space to get the pose's world pos | |
let worldPos = vec3.create() | |
let refSpacePos = vec3.create() | |
this.refSpace.getPositionWorld(refSpacePos) | |
vec3.add(worldPos, posePosition, refSpacePos) | |
// Try to get directional vec | |
let dirVec = vec3.create(); | |
let forwardVec = vec3.fromValues(0,0,-1); // in ref space, z+ is "backwards" | |
vec3.transformQuat(dirVec, forwardVec, rotQuat) | |
// Next raycast from this transform to the static collision layer | |
const rayHit = this.engine.physics.rayCast(worldPos, dirVec, 1 << 0); | |
if (rayHit.hitCount > 0) { | |
this.isPinching = true; | |
console.log("hit something!", rayHit.locations[0]) | |
this.debugSphere.active = true; | |
this.debugSphere.setPositionWorld(rayHit.locations[0]) | |
// Re-use the controller pose but discard the Y | |
let gripPosInRefSpace = vec3.fromValues( | |
this.controllerPose.transform.position.x, | |
0.01, | |
this.controllerPose.transform.position.z | |
); | |
// add the world position of the reference space to get the pose's world pos | |
let gripWorldPos = vec3.create(); | |
vec3.add(gripWorldPos, gripPosInRefSpace, rayHit.locations[0]); | |
this.lastWorldPos = gripWorldPos; | |
this.debugSphere.setPositionWorld(gripWorldPos); | |
} | |
break; | |
} | |
if (!transientPointerFoundThisFrame) { | |
this.isPinching = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment