|
1 |
| -import { |
2 |
| - ReactNode, |
3 |
| - forwardRef, |
4 |
| - useImperativeHandle, |
5 |
| - useMemo, |
6 |
| - useRef, |
7 |
| - useState, |
8 |
| - useEffect, |
9 |
| - FC, |
10 |
| - createContext, |
11 |
| - useContext, |
12 |
| -} from 'react' |
| 1 | +import { ReactNode, forwardRef, useImperativeHandle, useMemo, useRef, useState, useEffect } from 'react' |
13 | 2 | import { suspend } from 'suspend-react'
|
14 | 3 | import {
|
15 |
| - XRControllerGamepadComponentId, |
16 |
| - XRControllerGamepadComponentState, |
17 |
| - XRControllerModelOptions, |
18 |
| - XRControllerState, |
19 | 4 | configureXRControllerModel,
|
20 | 5 | createUpdateXRControllerVisuals,
|
21 | 6 | loadXRControllerModel,
|
| 7 | + XRControllerGamepadComponentId, |
| 8 | + XRControllerGamepadComponentState, |
22 | 9 | XRControllerLayout,
|
23 | 10 | XRControllerLayoutLoader,
|
24 | 11 | XRControllerLayoutLoaderOptions,
|
| 12 | + XRControllerModelOptions, |
| 13 | + XRControllerState, |
25 | 14 | } from '@pmndrs/xr/internals'
|
26 | 15 | import { createPortal, useFrame } from '@react-three/fiber'
|
27 | 16 | import { Object3D } from 'three'
|
28 |
| -import { useXRInputSourceState, useXRInputSourceStateContext } from './input.js' |
| 17 | +import { useXRInputSourceStateContext } from './input.js' |
29 | 18 |
|
30 | 19 | /**
|
31 | 20 | * component for placing content in the controller anchored at a specific component such as the Thumbstick
|
@@ -90,7 +79,7 @@ const LoadXRControllerModelSymbol = Symbol('loadXRControllerModel')
|
90 | 79 | */
|
91 | 80 | export const XRControllerModel = forwardRef<Object3D, XRControllerModelOptions>((options, ref) => {
|
92 | 81 | const state = useXRInputSourceStateContext('controller')
|
93 |
| - const model = useLoadXRControllerModel(state.layout) |
| 82 | + const model = suspend(loadXRControllerModel, [state.layout, undefined, LoadXRControllerModelSymbol]) |
94 | 83 | configureXRControllerModel(model, options)
|
95 | 84 | state.object = model
|
96 | 85 | useImperativeHandle(ref, () => model, [model])
|
@@ -127,46 +116,3 @@ export function useLoadXRControllerModel(layout: XRControllerLayout) {
|
127 | 116 | [layout, undefined, LoadXRControllerModelSymbol],
|
128 | 117 | )
|
129 | 118 | }
|
130 |
| - |
131 |
| -export function getXRControllerComponentObject( |
132 |
| - model: Object3D, |
133 |
| - layout: XRControllerLayout, |
134 |
| - componentId: XRControllerGamepadComponentId, |
135 |
| -) { |
136 |
| - const component = layout.components[componentId] |
137 |
| - // TODO: Add support for providing gamepad state |
138 |
| - const firstVisualResponse = component.visualResponses[Object.keys(component.visualResponses)[0]] |
139 |
| - if (!firstVisualResponse) return |
140 |
| - const valueNode = model.getObjectByName(firstVisualResponse.valueNodeName) |
141 |
| - |
142 |
| - return { object: valueNode } |
143 |
| -} |
144 |
| - |
145 |
| -const unboundControllerContext = createContext<XRControllerState | undefined>(undefined) |
146 |
| - |
147 |
| -export const UnboundController: FC<{ profileIds: string[] }> = ({ profileIds, children }) => { |
148 |
| - const layout = useLoadXRControllerLayout(['meta-quest-touch-plus'], 'right') |
149 |
| - const model = useLoadXRControllerModel(layout) |
150 |
| - |
151 |
| - return model ? ( |
152 |
| - <unboundControllerContext.Provider value={{ model, layout }}> |
153 |
| - <primitive object={model} /> |
154 |
| - {children} |
155 |
| - </unboundControllerContext.Provider> |
156 |
| - ) : null |
157 |
| -} |
158 |
| - |
159 |
| -export const UnboundControllerComponent: FC = ({ id, children }) => { |
160 |
| - const [object, setObject] = useState<Object3D | undefined>(undefined) |
161 |
| - const { model, layout } = useContext(unboundControllerContext) |
162 |
| - |
163 |
| - useEffect(() => { |
164 |
| - if (!model) { |
165 |
| - return |
166 |
| - } |
167 |
| - const component = getXRControllerComponentObject(model, layout, id) |
168 |
| - |
169 |
| - setObject(component?.object) |
170 |
| - }, [model, layout, id]) |
171 |
| - return object ? createPortal(children, object) : null |
172 |
| -} |
0 commit comments