-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add basic XR navigation ESM script #7171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* eslint-disable-next-line import/no-unresolved */ | ||
import { Color, Script, Vec3 } from 'playcanvas'; | ||
|
||
/** @import { XrInputSource } from 'playcanvas' */ | ||
|
||
/** | ||
* Handles VR teleportation navigation by allowing users to point and teleport using either | ||
* hands or tracked controllers. Shows a visual ray and target indicator when the user holds | ||
* the select button (trigger) or makes a pinch gesture with hand tracking, and teleports to | ||
* the target location when released. | ||
* | ||
* This script should be attached to a parent entity of the camera entity used for the XR | ||
* session. Use it in conjunction with the `XrControllers` script to handle the rendering of | ||
* the controllers. | ||
*/ | ||
export class XrNavigation extends Script { | ||
/** @type {Set<XrInputSource>} */ | ||
inputSources = new Set(); | ||
|
||
/** @type {Map<XrInputSource, boolean>} */ | ||
activePointers = new Map(); | ||
|
||
validColor = new Color(0, 1, 0); // Green for valid teleport | ||
|
||
invalidColor = new Color(1, 0, 0); // Red for invalid teleport | ||
|
||
/** @type {Map<XrInputSource, { handleSelectStart: Function, handleSelectEnd: Function }>} */ | ||
inputHandlers = new Map(); | ||
|
||
initialize() { | ||
this.app.xr.input.on('add', (inputSource) => { | ||
const handleSelectStart = () => { | ||
this.activePointers.set(inputSource, true); | ||
}; | ||
|
||
const handleSelectEnd = () => { | ||
this.activePointers.set(inputSource, false); | ||
this.tryTeleport(inputSource); | ||
}; | ||
|
||
// Attach the handlers | ||
inputSource.on('selectstart', handleSelectStart); | ||
inputSource.on('selectend', handleSelectEnd); | ||
|
||
// Store the handlers in the map | ||
this.inputHandlers.set(inputSource, { handleSelectStart, handleSelectEnd }); | ||
this.inputSources.add(inputSource); | ||
}); | ||
|
||
this.app.xr.input.on('remove', (inputSource) => { | ||
const handlers = this.inputHandlers.get(inputSource); | ||
if (handlers) { | ||
inputSource.off('selectstart', handlers.handleSelectStart); | ||
inputSource.off('selectend', handlers.handleSelectEnd); | ||
this.inputHandlers.delete(inputSource); | ||
} | ||
this.activePointers.delete(inputSource); | ||
this.inputSources.delete(inputSource); | ||
}); | ||
} | ||
|
||
findPlaneIntersection(origin, direction) { | ||
// Find intersection with y=0 plane | ||
if (Math.abs(direction.y) < 0.00001) return null; // Ray is parallel to plane | ||
|
||
const t = -origin.y / direction.y; | ||
if (t < 0) return null; // Intersection is behind the ray | ||
|
||
return new Vec3( | ||
origin.x + direction.x * t, | ||
0, | ||
origin.z + direction.z * t | ||
); | ||
} | ||
|
||
tryTeleport(inputSource) { | ||
const origin = inputSource.getOrigin(); | ||
const direction = inputSource.getDirection(); | ||
|
||
const hitPoint = this.findPlaneIntersection(origin, direction); | ||
willeastcott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (hitPoint) { | ||
const cameraY = this.entity.getPosition().y; | ||
hitPoint.y = cameraY; | ||
this.entity.setPosition(hitPoint); | ||
} | ||
} | ||
|
||
update() { | ||
for (const inputSource of this.inputSources) { | ||
// Only show ray when trigger is pressed | ||
if (!this.activePointers.get(inputSource)) continue; | ||
|
||
const start = inputSource.getOrigin(); | ||
const direction = inputSource.getDirection(); | ||
|
||
const hitPoint = this.findPlaneIntersection(start, direction); | ||
|
||
if (hitPoint) { | ||
// Draw line to intersection point | ||
this.app.drawLine(start, hitPoint, this.validColor); | ||
willeastcott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.drawTeleportIndicator(hitPoint); | ||
} else { | ||
// Draw full length ray if no intersection | ||
const end = start.clone().add( | ||
direction.clone().mulScalar(100) | ||
); | ||
this.app.drawLine(start, end, this.invalidColor); | ||
} | ||
} | ||
} | ||
|
||
drawTeleportIndicator(point) { | ||
// Draw a circle at the teleport point | ||
const segments = 32; | ||
const radius = 0.2; | ||
|
||
for (let i = 0; i < segments; i++) { | ||
const angle1 = (i / segments) * Math.PI * 2; | ||
const angle2 = ((i + 1) / segments) * Math.PI * 2; | ||
|
||
const x1 = point.x + Math.cos(angle1) * radius; | ||
const z1 = point.z + Math.sin(angle1) * radius; | ||
const x2 = point.x + Math.cos(angle2) * radius; | ||
const z2 = point.z + Math.sin(angle2) * radius; | ||
|
||
this.app.drawLine( | ||
new Vec3(x1, 0.01, z1), // Slightly above ground to avoid z-fighting | ||
new Vec3(x2, 0.01, z2), | ||
this.validColor | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.