forked from BabylonJS/BabylonReactNative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
182 lines (161 loc) · 7.61 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Generated with the TypeScript template
* https://github.com/react-native-community/react-native-template-typescript
*
* @format
*/
import React, { useState, FunctionComponent, useEffect, useCallback } from 'react';
import { SafeAreaView, StatusBar, Button, View, Text, ViewProps, Image } from 'react-native';
import { EngineView, useEngine, EngineViewCallbacks } from '@babylonjs/react-native';
import { Scene, Vector3, ArcRotateCamera, Camera, WebXRSessionManager, SceneLoader, TransformNode, DeviceSourceManager, DeviceType, DeviceSource, PointerInput, WebXRTrackingState, Nullable } from '@babylonjs/core';
import '@babylonjs/loaders';
import Slider from '@react-native-community/slider';
const EngineScreen: FunctionComponent<ViewProps> = (props: ViewProps) => {
const defaultScale = 1;
const enableSnapshots = false;
const engine = useEngine();
const [toggleView, setToggleView] = useState(false);
const [camera, setCamera] = useState<Camera>();
const [rootNode, setRootNode] = useState<TransformNode>();
const [scene, setScene] = useState<Scene>();
const [xrSession, setXrSession] = useState<WebXRSessionManager>();
const [scale, setScale] = useState<number>(defaultScale);
const [snapshotData, setSnapshotData] = useState<string>();
const [engineViewCallbacks, setEngineViewCallbacks] = useState<EngineViewCallbacks>();
const [trackingState, setTrackingState] = useState<WebXRTrackingState>();
useEffect(() => {
if (engine) {
const scene = new Scene(engine);
setScene(scene);
scene.createDefaultCamera(true);
(scene.activeCamera as ArcRotateCamera).beta -= Math.PI / 8;
setCamera(scene.activeCamera!);
scene.createDefaultLight(true);
const rootNode = new TransformNode("Root Container", scene);
setRootNode(rootNode);
const deviceSourceManager = new DeviceSourceManager(engine);
const handlePointerInput = (inputIndex: PointerInput, previousState: Nullable<number>, currentState: Nullable<number>) => {
if (inputIndex === PointerInput.Horizontal &&
currentState && previousState) {
rootNode.rotate(Vector3.Down(), (currentState - previousState) * 0.005);
};
};
deviceSourceManager.onDeviceConnectedObservable.add(device => {
if (device.deviceType === DeviceType.Touch) {
const touch: DeviceSource<DeviceType.Touch> = deviceSourceManager.getDeviceSource(device.deviceType, device.deviceSlot)!;
touch.onInputChangedObservable.add(touchEvent => {
handlePointerInput(touchEvent.inputIndex, touchEvent.previousState, touchEvent.currentState);
});
} else if (device.deviceType === DeviceType.Mouse) {
const mouse: DeviceSource<DeviceType.Mouse> = deviceSourceManager.getDeviceSource(device.deviceType, device.deviceSlot)!;
mouse.onInputChangedObservable.add(mouseEvent => {
if (mouse.getInput(PointerInput.LeftClick)) {
handlePointerInput(mouseEvent.inputIndex, mouseEvent.previousState, mouseEvent.currentState);
}
});
}
});
const transformContainer = new TransformNode("Transform Container", scene);
transformContainer.parent = rootNode;
transformContainer.scaling.scaleInPlace(0.2);
transformContainer.position.y -= .2;
scene.beforeRender = function () {
transformContainer.rotate(Vector3.Up(), 0.005 * scene.getAnimationRatio());
};
SceneLoader.ImportMeshAsync("", "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/BoxAnimated/glTF-Binary/BoxAnimated.glb").then(result => {
const mesh = result.meshes[0];
mesh.parent = transformContainer;
});
}
}, [engine]);
useEffect(() => {
if (rootNode) {
rootNode.scaling = new Vector3(scale, scale, scale);
}
}, [rootNode, scale]);
const trackingStateToString = (trackingState: WebXRTrackingState | undefined) : string => {
return trackingState === undefined ? "" : WebXRTrackingState[trackingState];
};
const onToggleXr = useCallback(() => {
(async () => {
if (xrSession) {
await xrSession.exitXRAsync();
} else {
if (rootNode !== undefined && 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);
});
// TODO: Figure out why getFrontPosition stopped working
//box.position = (scene.activeCamera as TargetCamera).getFrontPosition(2);
const cameraRay = scene.activeCamera!.getForwardRay(1);
rootNode.position = cameraRay.origin.add(cameraRay.direction.scale(cameraRay.length));
rootNode.rotate(Vector3.Up(), 3.14159);
}
}
})();
}, [rootNode, scene, xrSession]);
const onInitialized = useCallback(async(engineViewCallbacks: EngineViewCallbacks) => {
setEngineViewCallbacks(engineViewCallbacks);
}, [engine]);
const onSnapshot = useCallback(async () => {
if (engineViewCallbacks) {
setSnapshotData("data:image/jpeg;base64," + await engineViewCallbacks.takeSnapshot());
}
}, [engineViewCallbacks]);
return (
<>
<View style={props.style}>
<Button title="Toggle EngineView" onPress={() => { setToggleView(!toggleView) }} />
<Button title={ xrSession ? "Stop XR" : "Start XR"} onPress={onToggleXr} />
{ !toggleView &&
<View style={{flex: 1}}>
{ enableSnapshots &&
<View style ={{flex: 1}}>
<Button title={"Take Snapshot"} onPress={onSnapshot}/>
<Image style={{flex: 1}} source={{uri: snapshotData }} />
</View>
}
<EngineView style={props.style} camera={camera} onInitialized={onInitialized} />
<Slider style={{position: 'absolute', minHeight: 50, margin: 10, left: 0, right: 0, bottom: 0}} minimumValue={0.2} maximumValue={2} step={0.01} value={defaultScale} onValueChange={setScale} />
<Text style={{fontSize: 12, color: 'yellow', position: 'absolute', margin: 10}}>{trackingStateToString(trackingState)}</Text>
</View>
}
{ toggleView &&
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text style={{fontSize: 24}}>EngineView has been removed.</Text>
<Text style={{fontSize: 12}}>Render loop stopped, but engine is still alive.</Text>
</View>
}
</View>
</>
);
};
const App = () => {
const [toggleScreen, setToggleScreen] = useState(false);
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={{flex: 1, backgroundColor: "white"}}>
{ !toggleScreen &&
<EngineScreen style={{flex: 1}} />
}
{ toggleScreen &&
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text style={{fontSize: 24}}>EngineScreen has been removed.</Text>
<Text style={{fontSize: 12}}>Engine has been disposed, and will be recreated.</Text>
</View>
}
<Button title="Toggle EngineScreen" onPress={() => { setToggleScreen(!toggleScreen) }} />
</SafeAreaView>
</>
);
};
export default App;