|
| 1 | +import type { JSX, PropsWithChildren } from 'react' |
| 2 | +import type { Group } from 'three' |
| 3 | + |
| 4 | +import { useFrame, useThree } from '@react-three/fiber' |
| 5 | +import { Root } from '@react-three/uikit' |
| 6 | +import { colors } from '@react-three/uikit-default' |
| 7 | +import { useXRInputSourceState } from '@react-three/xr' |
| 8 | +import { useDebouncedState } from 'foxact/use-debounced-state' |
| 9 | +import { useMemo, useRef } from 'react' |
| 10 | +import { Vector3 } from 'three' |
| 11 | + |
| 12 | +import { Settings } from '~/components/ui/settings' |
| 13 | + |
| 14 | +export const FloatingMenu = ({ children, ...props }: PropsWithChildren<JSX.IntrinsicElements['group']>) => { |
| 15 | + const menuRef = useRef<Group>(null) |
| 16 | + const { camera } = useThree() |
| 17 | + |
| 18 | + const localPosition = useMemo(() => new Vector3(), []) |
| 19 | + |
| 20 | + useFrame(() => { |
| 21 | + if (!menuRef.current) |
| 22 | + return |
| 23 | + |
| 24 | + localPosition.set(0, 0, -1) |
| 25 | + menuRef.current.position.copy(localPosition.applyMatrix4(camera.matrixWorld)) |
| 26 | + menuRef.current.lookAt(camera.position) |
| 27 | + }) |
| 28 | + |
| 29 | + return ( |
| 30 | + <group ref={menuRef} {...props}> |
| 31 | + {children} |
| 32 | + </group> |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +const DebugMenu = () => { |
| 37 | + const [visible, setVisible] = useDebouncedState(true, 100) |
| 38 | + |
| 39 | + const controllerLeft = useXRInputSourceState('controller', 'left') |
| 40 | + const controllerRight = useXRInputSourceState('controller', 'right') |
| 41 | + |
| 42 | + useFrame(() => { |
| 43 | + if (controllerLeft?.gamepad['y-button']?.state === 'pressed' || controllerRight?.gamepad['b-button']?.state === 'pressed') |
| 44 | + setVisible(!visible) |
| 45 | + }) |
| 46 | + |
| 47 | + return ( |
| 48 | + <FloatingMenu visible={visible}> |
| 49 | + <Root |
| 50 | + alignItems="center" |
| 51 | + backgroundColor={colors.muted} |
| 52 | + borderRadius={12} |
| 53 | + height={768} |
| 54 | + justifyContent="center" |
| 55 | + pixelSize={0.0015} |
| 56 | + width={1024} |
| 57 | + > |
| 58 | + <Settings /> |
| 59 | + </Root> |
| 60 | + </FloatingMenu> |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +export default DebugMenu |
0 commit comments