|
| 1 | +import * as THREE from 'three' |
| 2 | +import * as React from 'react' |
| 3 | +import { useFrame, useThree } from '@react-three/fiber' |
| 4 | +import { Interactive, type XRInteractionEvent } from './Interactions' |
| 5 | + |
| 6 | +const _q = /* @__PURE__ */ new THREE.Quaternion() |
| 7 | + |
| 8 | +/** |
| 9 | + * Teleport callback, accepting a world-space target position to teleport to. |
| 10 | + */ |
| 11 | +export type TeleportCallback = (target: THREE.Vector3 | THREE.Vector3Tuple) => void |
| 12 | + |
| 13 | +/** |
| 14 | + * Returns a {@link TeleportCallback} to teleport the player to a position. |
| 15 | + */ |
| 16 | +export function useTeleportation(): TeleportCallback { |
| 17 | + const frame = React.useRef<XRFrame>() |
| 18 | + const baseReferenceSpace = React.useRef<XRReferenceSpace | null>(null) |
| 19 | + const teleportReferenceSpace = React.useRef<XRReferenceSpace | null>(null) |
| 20 | + |
| 21 | + useFrame((state, _, xrFrame) => { |
| 22 | + frame.current = xrFrame |
| 23 | + |
| 24 | + const referenceSpace = state.gl.xr.getReferenceSpace() |
| 25 | + baseReferenceSpace.current ??= referenceSpace |
| 26 | + |
| 27 | + const teleportOffset = teleportReferenceSpace.current |
| 28 | + if (teleportOffset && referenceSpace !== teleportOffset) { |
| 29 | + state.gl.xr.setReferenceSpace(teleportOffset) |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + return React.useCallback((target) => { |
| 34 | + const base = baseReferenceSpace.current |
| 35 | + if (base) { |
| 36 | + const [x, y, z] = Array.from(target as THREE.Vector3Tuple) |
| 37 | + const offsetFromBase = { x: -x, y: -y, z: -z } |
| 38 | + |
| 39 | + const pose = frame.current?.getViewerPose(base) |
| 40 | + if (pose) { |
| 41 | + offsetFromBase.x += pose.transform.position.x |
| 42 | + offsetFromBase.z += pose.transform.position.z |
| 43 | + } |
| 44 | + |
| 45 | + const teleportOffset = new XRRigidTransform(offsetFromBase, _q) |
| 46 | + teleportReferenceSpace.current = base.getOffsetReferenceSpace(teleportOffset) |
| 47 | + } |
| 48 | + }, []) |
| 49 | +} |
| 50 | + |
| 51 | +export interface TeleportationPlaneProps extends Partial<JSX.IntrinsicElements['group']> { |
| 52 | + /** Whether to allow teleportation from left controller. Default is `false` */ |
| 53 | + leftHand?: boolean |
| 54 | + /** Whether to allow teleportation from right controller. Default is `false` */ |
| 55 | + rightHand?: boolean |
| 56 | + /** The maximum distance from the camera to the teleportation point. Default is `10` */ |
| 57 | + maxDistance?: number |
| 58 | + /** The radial size of the teleportation marker. Default is `0.25` */ |
| 59 | + size?: number |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Creates a teleportation plane with a marker that will teleport on interaction. |
| 64 | + */ |
| 65 | +export const TeleportationPlane = React.forwardRef<THREE.Group, TeleportationPlaneProps>(function TeleportationPlane( |
| 66 | + { leftHand = false, rightHand = false, maxDistance = 10, size = 0.25, ...props }, |
| 67 | + ref |
| 68 | +) { |
| 69 | + const teleport = useTeleportation() |
| 70 | + const marker = React.useRef<THREE.Mesh>(null!) |
| 71 | + const intersection = React.useRef<THREE.Vector3>() |
| 72 | + const camera = useThree((state) => state.camera) |
| 73 | + |
| 74 | + const isInteractive = React.useCallback( |
| 75 | + (e: XRInteractionEvent): boolean => { |
| 76 | + const { handedness } = e.target.inputSource |
| 77 | + return !!((handedness !== 'left' || leftHand) && (handedness !== 'right' || rightHand)) |
| 78 | + }, |
| 79 | + [leftHand, rightHand] |
| 80 | + ) |
| 81 | + |
| 82 | + return ( |
| 83 | + <group ref={ref} {...props}> |
| 84 | + <mesh ref={marker} visible={false} rotation-x={-Math.PI / 2}> |
| 85 | + <circleGeometry args={[size, 32]} /> |
| 86 | + <meshBasicMaterial color="white" /> |
| 87 | + </mesh> |
| 88 | + <Interactive |
| 89 | + onMove={(e) => { |
| 90 | + if (!isInteractive(e) || !e.intersection) return |
| 91 | + |
| 92 | + const distanceFromCamera = e.intersection.point.distanceTo(camera.position) |
| 93 | + marker.current.visible = distanceFromCamera <= maxDistance |
| 94 | + marker.current.scale.setScalar(1) |
| 95 | + |
| 96 | + intersection.current = e.intersection.point |
| 97 | + marker.current.position.copy(intersection.current) |
| 98 | + }} |
| 99 | + onHover={(e) => { |
| 100 | + if (!isInteractive(e) || !e.intersection) return |
| 101 | + |
| 102 | + const distanceFromCamera = e.intersection.point.distanceTo(camera.position) |
| 103 | + marker.current.visible = distanceFromCamera <= maxDistance |
| 104 | + marker.current.scale.setScalar(1) |
| 105 | + }} |
| 106 | + onBlur={(e) => { |
| 107 | + if (!isInteractive(e)) return |
| 108 | + marker.current.visible = false |
| 109 | + }} |
| 110 | + onSelectStart={(e) => { |
| 111 | + if (!isInteractive(e) || !e.intersection) return |
| 112 | + |
| 113 | + const distanceFromCamera = e.intersection.point.distanceTo(camera.position) |
| 114 | + marker.current.visible = distanceFromCamera <= maxDistance |
| 115 | + marker.current.scale.setScalar(1.1) |
| 116 | + }} |
| 117 | + onSelectEnd={(e) => { |
| 118 | + if (!isInteractive(e) || !intersection.current) return |
| 119 | + |
| 120 | + marker.current.visible = true |
| 121 | + marker.current.scale.setScalar(1) |
| 122 | + |
| 123 | + const distanceFromCamera = intersection.current.distanceTo(camera.position) |
| 124 | + if (distanceFromCamera <= maxDistance) { |
| 125 | + teleport(intersection.current) |
| 126 | + } |
| 127 | + }} |
| 128 | + > |
| 129 | + <mesh rotation-x={-Math.PI / 2} visible={false} scale={1000}> |
| 130 | + <planeGeometry /> |
| 131 | + </mesh> |
| 132 | + </Interactive> |
| 133 | + </group> |
| 134 | + ) |
| 135 | +}) |
0 commit comments