-
Notifications
You must be signed in to change notification settings - Fork 13
/
App.tsx
107 lines (97 loc) · 3.15 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* Generated with the TypeScript template
* https://github.com/react-native-community/react-native-template-typescript
*
* @format
*/
import React, {
FunctionComponent,
useEffect,
useCallback,
useState,
} from 'react';
import {SafeAreaView, View, Button, ViewProps, StatusBar} from 'react-native';
import {EngineView, useEngine} from '@babylonjs/react-native';
import {SceneLoader} from '@babylonjs/core/Loading/sceneLoader';
import {Camera} from '@babylonjs/core/Cameras/camera';
import {ArcRotateCamera} from '@babylonjs/core/Cameras/arcRotateCamera';
import '@babylonjs/loaders/glTF';
import {Scene} from '@babylonjs/core/scene';
import {WebXRSessionManager, WebXRTrackingState} from '@babylonjs/core/XR';
const EngineScreen: FunctionComponent<ViewProps> = (props: ViewProps) => {
const engine = useEngine();
const [camera, setCamera] = useState<Camera>();
const [xrSession, setXrSession] = useState<WebXRSessionManager>();
const [trackingState, setTrackingState] = useState<WebXRTrackingState>();
const [scene, setScene] = useState<Scene>();
const onToggleXr = useCallback(() => {
(async () => {
if (xrSession) {
await xrSession.exitXRAsync();
} else {
if (scene !== undefined) {
const xr = await scene.createDefaultXRExperienceAsync({
disableDefaultUI: true,
disableTeleportation: true,
});
const session = await xr.baseExperience.enterXRAsync(
'immersive-ar',
'unbounded',
xr.renderTarget,
);
setXrSession(session);
session.onXRSessionEnded.add(() => {
setXrSession(undefined);
setTrackingState(undefined);
});
setTrackingState(xr.baseExperience.camera.trackingState);
xr.baseExperience.camera.onTrackingStateChanged.add(
newTrackingState => {
setTrackingState(newTrackingState);
},
);
}
}
})();
}, [scene, xrSession]);
useEffect(() => {
if (engine) {
const url =
'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/BoxAnimated/glTF/BoxAnimated.gltf';
SceneLoader.LoadAsync(url, undefined, engine).then(loadScene => {
setScene(loadScene);
loadScene.createDefaultCameraOrLight(true, undefined, true);
(loadScene.activeCamera as ArcRotateCamera).alpha += Math.PI;
(loadScene.activeCamera as ArcRotateCamera).radius = 10;
setCamera(loadScene.activeCamera!);
});
}
}, [engine]);
return (
<>
<View style={props.style}>
<Button
title={xrSession ? 'Stop XR' : 'Start XR'}
onPress={onToggleXr}
/>
<View style={{flex: 1}}>
<EngineView camera={camera} displayFrameRate={true} />
</View>
</View>
</>
);
};
const App = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={{flex: 1, backgroundColor: 'white'}}>
<EngineScreen style={{flex: 1}} />
</SafeAreaView>
</>
);
};
export default App;