-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
114 lines (92 loc) · 3.04 KB
/
Copy pathapp.js
File metadata and controls
114 lines (92 loc) · 3.04 KB
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
import React from 'react';
import { View, Text } from 'magic-script-components';
import AnchorCube from './anchor-cube.js';
import { authorize } from 'react-native-app-auth';
import { NativeModules } from 'react-native';
const { XrClientBridge } = NativeModules;
const oAuthConfig = {
cacheKey: 'auth/prod',
issuer: 'https://auth.magicleap.com',
clientId: undefined, // Generate from https://developer.magicleap.com --> Publish tab --> Clients tab
redirectUrl: undefined, // Generated along with client ID
scopes: [
'openid',
'profile',
'email'
],
serviceConfiguration: {
authorizationEndpoint: 'https://oauth.magicleap.com/auth',
tokenEndpoint: 'https://oauth.magicleap.com/token'
}
};
const getUUID = (id, pose) => `${id}#[${pose}]`;
class MyApp extends React.Component {
constructor (props) {
super(props);
this.state = {
scenes: {},
anchorCount: 0
};
}
async componentDidMount () {
const oauth = await this.authorizeToXrServer(oAuthConfig);
const status = await this.connectToXrServer(oauth);
this._updateInterval = setInterval(() => this.updateAnchors(), 1000);
}
componentWillUnmount () {
if (Object.keys(this.state.scenes).length > 0) {
XrClientBridge.removeAllAnchors();
}
}
async authorizeToXrServer (config) {
console.log('MyXrDemoApp: authorizing');
const result = await authorize(config);
console.log('MyXrDemoApp: oAuthData', result);
return result;
}
async connectToXrServer (config) {
console.log('MyXrDemoApp: XrClientBridge.connecting');
const result = await XrClientBridge.connect(config.accessToken);
console.log('MyXrDemoApp: XrClientBridge.connect result', result);
return result;
}
async updateAnchors () {
const status = await XrClientBridge.getLocalizationStatus();
console.log('MyXrDemoApp: localization status', status);
if (status === 'localized' && this.state.anchorCount === 0) {
const pcfList = await XrClientBridge.getAllPCFs();
console.log(`MyXrDemoApp: received ${pcfList.length} PCFs`);
if (pcfList.length > 0) {
clearInterval(this._updateInterval);
}
var scenes = {};
pcfList.forEach(pcfData => this.updateScenes(scenes, pcfData));
Object.values(scenes).forEach(scene => {
XrClientBridge.createAnchor(scene.uuid, scene.pcfPose);
});
this.setState({ scenes: scenes, anchorCount: pcfList.length });
}
}
updateScenes (scenes, pcfData) {
const uuid = getUUID(pcfData.anchorId, pcfData.pose);
if (scenes[pcfData.anchorId] === undefined) {
scenes[pcfData.anchorId] = {
uuid: uuid,
pcfId: pcfData.anchorId,
pcfPose: pcfData.pose
};
}
}
render () {
const scenes = Object.values(this.state.scenes);
return (
<View name='main-view'>
{ scenes.length === 0
? (<Text text='Initializing ...' />)
: scenes.map( scene => <AnchorCube key={scene.uuid} uuid={scene.uuid} id={scene.pcfId} />)
}
</View>
);
}
}
export default MyApp;