forked from react-native-ar/react-native-arkit
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ARKit.js
255 lines (228 loc) · 5.99 KB
/
ARKit.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// index.js
//
// Created by HippoAR on 7/9/17.
// Copyright © 2017 HippoAR. All rights reserved.
//
import {
StyleSheet,
View,
Text,
NativeModules,
requireNativeComponent,
} from 'react-native';
import { keyBy, mapValues, isBoolean } from 'lodash';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import {
deprecated,
detectionImages,
planeDetection,
position,
transition,
} from './components/lib/propTypes';
import { pickColors, pickColorsFromFile } from './lib/pickColors';
import generateId from './components/lib/generateId';
const ARKitManager = NativeModules.ARKitManager;
const TRACKING_STATES = ['NOT_AVAILABLE', 'LIMITED', 'NORMAL'];
const TRACKING_REASONS = [
'NONE',
'INITIALIZING',
'EXCESSIVE_MOTION',
'INSUFFICIENT_FEATURES',
];
const TRACKING_STATES_COLOR = ['red', 'orange', 'green'];
class ARKit extends Component {
state = {
state: 0,
reason: 0,
floor: null,
};
componentDidMount() {
ARKitManager.resume();
}
componentWillUnmount() {
ARKitManager.pause();
}
getCallbackProps() {
return mapValues(
keyBy([
'onTapOnPlaneUsingExtent',
'onTapOnPlaneNoExtent',
'onPlaneDetected',
'onPlaneRemoved',
'onPlaneUpdated',
'onAnchorDetected',
'onAnchorUpdated',
'onAnchorRemoved',
'onTrackingState',
'onARKitError',
]),
name => this.callback(name),
);
}
render(AR = RCTARKit) {
let state = null;
if (this.props.debug) {
state = (
<View style={styles.statePanel}>
<View
style={[
styles.stateIcon,
{ backgroundColor: TRACKING_STATES_COLOR[this.state.state] },
]}
/>
<Text style={styles.stateText}>
{TRACKING_REASONS[this.state.reason] || this.state.reason}
</Text>
</View>
);
}
return (
<View style={this.props.style}>
<AR
{...this.props}
{...this.getCallbackProps()}
// fallback to old prop type (Was boolean, now is enum)
planeDetection={
/* eslint no-nested-ternary: 0 */
isBoolean(this.props.planeDetection)
? this.props.planeDetection
? ARKitManager.ARPlaneDetection.Horizontal
: ARKitManager.ARPlaneDetection.None
: this.props.planeDetection
}
onEvent={this._onEvent}
/>
{state}
</View>
);
}
_onTrackingState = ({
state = this.state.state,
reason = this.state.reason,
}) => {
console.log('tracking in arkit.js')
if (this.props.onTrackingState) {
this.props.onTrackingState({
state: TRACKING_STATES[state] || state,
reason: TRACKING_REASONS[reason] || reason,
});
}
// TODO: check if we can remove this
if (this.props.debug) {
this.setState({
state,
reason,
});
}
};
_onEvent = event => {
let eventName = event.nativeEvent.event;
if (!eventName) {
return;
}
eventName = eventName.charAt(0).toUpperCase() + eventName.slice(1);
const eventListener = this.props[`on${eventName}`];
if (eventListener) {
eventListener(event.nativeEvent);
}
};
// handle deprecated alias
_onPlaneUpdated = nativeEvent => {
if (this.props.onPlaneUpdate) {
this.props.onPlaneUpdate(nativeEvent);
}
if (this.props.onPlaneUpdated) {
this.props.onPlaneUpdated(nativeEvent);
}
};
callback(name) {
return event => {
if (this[`_${name}`]) {
this[`_${name}`](event.nativeEvent);
return;
}
if (!this.props[name]) {
return;
}
this.props[name](event.nativeEvent);
};
}
}
const styles = StyleSheet.create({
statePanel: {
position: 'absolute',
top: 30,
left: 10,
height: 20,
borderRadius: 10,
padding: 4,
backgroundColor: 'black',
flexDirection: 'row',
},
stateIcon: {
width: 12,
height: 12,
borderRadius: 6,
marginRight: 4,
},
stateText: {
color: 'white',
fontSize: 10,
height: 12,
},
});
// copy all ARKitManager properties to ARKit
Object.keys(ARKitManager).forEach(key => {
ARKit[key] = ARKitManager[key];
});
const addDefaultsToSnapShotFunc = funcName => (
{ target = 'cameraRoll', format = 'png' } = {},
) => ARKitManager[funcName]({ target, format });
ARKit.snapshot = addDefaultsToSnapShotFunc('snapshot');
ARKit.snapshotCamera = addDefaultsToSnapShotFunc('snapshotCamera');
ARKit.exportModel = presetId => {
const id = presetId || generateId();
const property = { id };
return ARKitManager.exportModel(property).then(result => ({ ...result, id })).catch((err)=>{
console.log(err)
});
};
ARKit.TRACKING_STATES = TRACKING_STATES
ARKit.TRACKING_REASONS = TRACKING_REASONS
ARKit.TRACKING_STATES_COLOR = TRACKING_STATES_COLOR
ARKit.pickColors = pickColors;
ARKit.pickColorsFromFile = pickColorsFromFile;
ARKit.propTypes = {
debug: PropTypes.bool,
planeDetection,
origin: PropTypes.shape({
position,
transition,
}),
lightEstimationEnabled: PropTypes.bool,
autoenablesDefaultLighting: PropTypes.bool,
worldAlignment: PropTypes.number,
detectionImages,
onARKitError: PropTypes.func,
onFeaturesDetected: PropTypes.func,
// onLightEstimation is called rapidly, better poll with
// ARKit.getCurrentLightEstimation()
onLightEstimation: PropTypes.func,
onPlaneDetected: PropTypes.func,
onPlaneRemoved: PropTypes.func,
onPlaneUpdated: PropTypes.func,
onPlaneUpdate: deprecated(PropTypes.func, 'Use `onPlaneUpdated` instead'),
onAnchorDetected: PropTypes.func,
onAnchorRemoved: PropTypes.func,
onAnchorUpdated: PropTypes.func,
onTrackingState: PropTypes.func,
onTapOnPlaneUsingExtent: PropTypes.func,
onTapOnPlaneNoExtent: PropTypes.func,
onEvent: PropTypes.func,
isMounted: PropTypes.func,
isInitialized: PropTypes.func,
};
const RCTARKit = requireNativeComponent('RCTARKit', ARKit);
export default ARKit;