|
| 1 | +/** |
| 2 | + * Generated with the TypeScript template |
| 3 | + * https://github.com/react-native-community/react-native-template-typescript |
| 4 | + * |
| 5 | + * @format |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { |
| 9 | + useState, |
| 10 | + FunctionComponent, |
| 11 | + useEffect, |
| 12 | + useCallback, |
| 13 | +} from "react"; |
| 14 | +import { |
| 15 | + SafeAreaView, |
| 16 | + StatusBar, |
| 17 | + Button, |
| 18 | + View, |
| 19 | + Text, |
| 20 | + ViewProps, |
| 21 | + Image, |
| 22 | +} from "react-native"; |
| 23 | + |
| 24 | +import { |
| 25 | + EngineView, |
| 26 | + useEngine, |
| 27 | + EngineViewCallbacks, |
| 28 | +} from "@babylonjs/react-native"; |
| 29 | +import { |
| 30 | + Scene, |
| 31 | + Vector3, |
| 32 | + ArcRotateCamera, |
| 33 | + Camera, |
| 34 | + WebXRSessionManager, |
| 35 | + SceneLoader, |
| 36 | + TransformNode, |
| 37 | + DeviceSourceManager, |
| 38 | + DeviceType, |
| 39 | + PointerInput, |
| 40 | + WebXRTrackingState, |
| 41 | + IMouseEvent, |
| 42 | +} from "@babylonjs/core"; |
| 43 | +import "@babylonjs/loaders"; |
| 44 | +import Slider from "@react-native-community/slider"; |
| 45 | + |
| 46 | +const EngineScreen: FunctionComponent<ViewProps> = (props: ViewProps) => { |
| 47 | + const defaultScale = 1; |
| 48 | + const enableSnapshots = false; |
| 49 | + |
| 50 | + const engine = useEngine(); |
| 51 | + const [toggleView, setToggleView] = useState(false); |
| 52 | + const [camera, setCamera] = useState<Camera>(); |
| 53 | + const [rootNode, setRootNode] = useState<TransformNode>(); |
| 54 | + const [scene, setScene] = useState<Scene>(); |
| 55 | + const [xrSession, setXrSession] = useState<WebXRSessionManager>(); |
| 56 | + const [scale, setScale] = useState<number>(defaultScale); |
| 57 | + const [snapshotData, setSnapshotData] = useState<string>(); |
| 58 | + const [engineViewCallbacks, setEngineViewCallbacks] = |
| 59 | + useState<EngineViewCallbacks>(); |
| 60 | + const [trackingState, setTrackingState] = useState<WebXRTrackingState>(); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (engine) { |
| 64 | + const scene = new Scene(engine); |
| 65 | + setScene(scene); |
| 66 | + scene.createDefaultCamera(true); |
| 67 | + (scene.activeCamera as ArcRotateCamera).beta -= Math.PI / 8; |
| 68 | + setCamera(scene.activeCamera!); |
| 69 | + scene.createDefaultLight(true); |
| 70 | + const rootNode = new TransformNode("Root Container", scene); |
| 71 | + setRootNode(rootNode); |
| 72 | + |
| 73 | + const deviceSourceManager = new DeviceSourceManager(engine); |
| 74 | + const handlePointerInput = (event: IMouseEvent) => { |
| 75 | + if (event.inputIndex === PointerInput.Move && event.movementX) { |
| 76 | + rootNode.rotate(Vector3.Down(), event.movementX * 0.005); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + deviceSourceManager.onDeviceConnectedObservable.add((device) => { |
| 81 | + if (device.deviceType === DeviceType.Touch) { |
| 82 | + const touch = deviceSourceManager.getDeviceSource( |
| 83 | + device.deviceType, |
| 84 | + device.deviceSlot |
| 85 | + )!; |
| 86 | + touch.onInputChangedObservable.add((touchEvent) => { |
| 87 | + handlePointerInput(touchEvent); |
| 88 | + }); |
| 89 | + } else if (device.deviceType === DeviceType.Mouse) { |
| 90 | + const mouse = deviceSourceManager.getDeviceSource( |
| 91 | + device.deviceType, |
| 92 | + device.deviceSlot |
| 93 | + )!; |
| 94 | + mouse.onInputChangedObservable.add((mouseEvent) => { |
| 95 | + if (mouse.getInput(PointerInput.LeftClick)) { |
| 96 | + handlePointerInput(mouseEvent); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + const transformContainer = new TransformNode( |
| 103 | + "Transform Container", |
| 104 | + scene |
| 105 | + ); |
| 106 | + transformContainer.parent = rootNode; |
| 107 | + transformContainer.scaling.scaleInPlace(0.2); |
| 108 | + transformContainer.position.y -= 0.2; |
| 109 | + |
| 110 | + scene.beforeRender = function () { |
| 111 | + transformContainer.rotate( |
| 112 | + Vector3.Up(), |
| 113 | + 0.005 * scene.getAnimationRatio() |
| 114 | + ); |
| 115 | + }; |
| 116 | + |
| 117 | + SceneLoader.ImportMeshAsync( |
| 118 | + "", |
| 119 | + "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/BoxAnimated/glTF-Binary/BoxAnimated.glb" |
| 120 | + ).then((result) => { |
| 121 | + const mesh = result.meshes[0]; |
| 122 | + mesh.parent = transformContainer; |
| 123 | + }); |
| 124 | + } |
| 125 | + }, [engine]); |
| 126 | + |
| 127 | + useEffect(() => { |
| 128 | + if (rootNode) { |
| 129 | + rootNode.scaling = new Vector3(scale, scale, scale); |
| 130 | + } |
| 131 | + }, [rootNode, scale]); |
| 132 | + |
| 133 | + const trackingStateToString = ( |
| 134 | + trackingState: WebXRTrackingState | undefined |
| 135 | + ): string => { |
| 136 | + return trackingState === undefined ? "" : WebXRTrackingState[trackingState]; |
| 137 | + }; |
| 138 | + |
| 139 | + const onToggleXr = useCallback(() => { |
| 140 | + (async () => { |
| 141 | + if (xrSession) { |
| 142 | + await xrSession.exitXRAsync(); |
| 143 | + } else { |
| 144 | + if (rootNode !== undefined && scene !== undefined) { |
| 145 | + const xr = await scene.createDefaultXRExperienceAsync({ |
| 146 | + disableDefaultUI: true, |
| 147 | + disableTeleportation: true, |
| 148 | + }); |
| 149 | + const session = await xr.baseExperience.enterXRAsync( |
| 150 | + "immersive-ar", |
| 151 | + "unbounded", |
| 152 | + xr.renderTarget |
| 153 | + ); |
| 154 | + setXrSession(session); |
| 155 | + session.onXRSessionEnded.add(() => { |
| 156 | + setXrSession(undefined); |
| 157 | + setTrackingState(undefined); |
| 158 | + }); |
| 159 | + |
| 160 | + setTrackingState(xr.baseExperience.camera.trackingState); |
| 161 | + xr.baseExperience.camera.onTrackingStateChanged.add( |
| 162 | + (newTrackingState) => { |
| 163 | + setTrackingState(newTrackingState); |
| 164 | + } |
| 165 | + ); |
| 166 | + |
| 167 | + // TODO: Figure out why getFrontPosition stopped working |
| 168 | + //box.position = (scene.activeCamera as TargetCamera).getFrontPosition(2); |
| 169 | + const cameraRay = scene.activeCamera!.getForwardRay(1); |
| 170 | + rootNode.position = cameraRay.origin.add( |
| 171 | + cameraRay.direction.scale(cameraRay.length) |
| 172 | + ); |
| 173 | + rootNode.rotate(Vector3.Up(), 3.14159); |
| 174 | + } |
| 175 | + } |
| 176 | + })(); |
| 177 | + }, [rootNode, scene, xrSession]); |
| 178 | + |
| 179 | + const onInitialized = useCallback( |
| 180 | + async (engineViewCallbacks: EngineViewCallbacks) => { |
| 181 | + setEngineViewCallbacks(engineViewCallbacks); |
| 182 | + }, |
| 183 | + [engine] |
| 184 | + ); |
| 185 | + |
| 186 | + const onSnapshot = useCallback(async () => { |
| 187 | + if (engineViewCallbacks) { |
| 188 | + setSnapshotData( |
| 189 | + "data:image/jpeg;base64," + (await engineViewCallbacks.takeSnapshot()) |
| 190 | + ); |
| 191 | + } |
| 192 | + }, [engineViewCallbacks]); |
| 193 | + |
| 194 | + return ( |
| 195 | + <> |
| 196 | + <View style={props.style}> |
| 197 | + <Button |
| 198 | + title="Toggle EngineView" |
| 199 | + onPress={() => { |
| 200 | + setToggleView(!toggleView); |
| 201 | + }} |
| 202 | + /> |
| 203 | + <Button |
| 204 | + title={xrSession ? "Stop XR" : "Start XR"} |
| 205 | + onPress={onToggleXr} |
| 206 | + /> |
| 207 | + {!toggleView && ( |
| 208 | + <View style={{ flex: 1 }}> |
| 209 | + {enableSnapshots && ( |
| 210 | + <View style={{ flex: 1 }}> |
| 211 | + <Button title={"Take Snapshot"} onPress={onSnapshot} /> |
| 212 | + <Image style={{ flex: 1 }} source={{ uri: snapshotData }} /> |
| 213 | + </View> |
| 214 | + )} |
| 215 | + <EngineView |
| 216 | + camera={camera} |
| 217 | + onInitialized={onInitialized} |
| 218 | + displayFrameRate={true} |
| 219 | + antiAliasing={2} |
| 220 | + /> |
| 221 | + <Slider |
| 222 | + style={{ |
| 223 | + position: "absolute", |
| 224 | + minHeight: 50, |
| 225 | + margin: 10, |
| 226 | + left: 0, |
| 227 | + right: 0, |
| 228 | + bottom: 0, |
| 229 | + }} |
| 230 | + minimumValue={0.2} |
| 231 | + maximumValue={2} |
| 232 | + step={0.01} |
| 233 | + value={defaultScale} |
| 234 | + onValueChange={setScale} |
| 235 | + /> |
| 236 | + <Text style={{ color: "yellow", position: "absolute", margin: 3 }}> |
| 237 | + {trackingStateToString(trackingState)} |
| 238 | + </Text> |
| 239 | + </View> |
| 240 | + )} |
| 241 | + {toggleView && ( |
| 242 | + <View |
| 243 | + style={{ flex: 1, justifyContent: "center", alignItems: "center" }} |
| 244 | + > |
| 245 | + <Text style={{ fontSize: 24 }}>EngineView has been removed.</Text> |
| 246 | + <Text style={{ fontSize: 12 }}> |
| 247 | + Render loop stopped, but engine is still alive. |
| 248 | + </Text> |
| 249 | + </View> |
| 250 | + )} |
| 251 | + </View> |
| 252 | + </> |
| 253 | + ); |
| 254 | +}; |
| 255 | + |
| 256 | +const App = () => { |
| 257 | + const [toggleScreen, setToggleScreen] = useState(false); |
| 258 | + |
| 259 | + return ( |
| 260 | + <> |
| 261 | + <StatusBar barStyle="dark-content" /> |
| 262 | + <SafeAreaView style={{ flex: 1, backgroundColor: "white" }}> |
| 263 | + {!toggleScreen && <EngineScreen style={{ flex: 1 }} />} |
| 264 | + {toggleScreen && ( |
| 265 | + <View |
| 266 | + style={{ flex: 1, justifyContent: "center", alignItems: "center" }} |
| 267 | + > |
| 268 | + <Text style={{ fontSize: 24 }}>EngineScreen has been removed.</Text> |
| 269 | + <Text style={{ fontSize: 12 }}> |
| 270 | + Engine has been disposed, and will be recreated. |
| 271 | + </Text> |
| 272 | + </View> |
| 273 | + )} |
| 274 | + <Button |
| 275 | + title="Toggle EngineScreen" |
| 276 | + onPress={() => { |
| 277 | + setToggleScreen(!toggleScreen); |
| 278 | + }} |
| 279 | + /> |
| 280 | + </SafeAreaView> |
| 281 | + </> |
| 282 | + ); |
| 283 | +}; |
| 284 | + |
| 285 | +export default App; |
0 commit comments