forked from react-native-ar/react-native-arkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARKit.js
204 lines (183 loc) · 4.9 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
//
// 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 PropTypes from 'prop-types';
import React, { Component } from 'react';
import { pickColors, pickColorsFromFile } from './lib/pickColors';
import { position, transition } from './components/lib/propTypes';
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();
}
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}
{this.state.floor && ` (${this.state.floor})`}
</Text>
</View>
);
}
return (
<View style={this.props.style}>
<AR
{...this.props}
onTapOnPlaneUsingExtent={this.callback('onTapOnPlaneUsingExtent')}
onTapOnPlaneNoExtent={this.callback('onTapOnPlaneNoExtent')}
onPlaneDetected={this.callback('onPlaneDetected')}
onPlaneRemoved={this.callback('onPlaneRemoved')}
onPlaneUpdate={this.callback('onPlaneUpdate')}
onTrackingState={this.callback('onTrackingState')}
onARKitError={this.callback('onARKitError')}
onEvent={this._onEvent}
/>
{state}
</View>
);
}
_onTrackingState = ({
state = this.state.state,
reason = this.state.reason,
floor,
}) => {
if (this.props.onTrackingState) {
this.props.onTrackingState({
state: TRACKING_STATES[state] || state,
reason: TRACKING_REASONS[reason] || reason,
floor,
});
}
if (this.props.debug) {
this.setState({
state,
reason,
floor: floor ? floor.toFixed(2) : this.state.floor,
});
}
};
_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);
}
};
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 }));
};
ARKit.pickColors = pickColors;
ARKit.pickColorsFromFile = pickColorsFromFile;
ARKit.propTypes = {
debug: PropTypes.bool,
planeDetection: PropTypes.bool,
origin: PropTypes.shape({
position,
transition,
}),
lightEstimationEnabled: PropTypes.bool,
autoenablesDefaultLighting: PropTypes.bool,
worldAlignment: PropTypes.number,
onARKitError: PropTypes.func,
onPlaneDetected: PropTypes.func,
onPlaneRemoved: PropTypes.func,
onFeaturesDetected: PropTypes.func,
// onLightEstimation is called rapidly, better poll with
// ARKit.getCurrentLightEstimation()
onLightEstimation: PropTypes.func,
onPlaneUpdate: PropTypes.func,
onTrackingState: PropTypes.func,
onTapOnPlaneUsingExtent: PropTypes.func,
onTapOnPlaneNoExtent: PropTypes.func,
onEvent: PropTypes.func,
};
const RCTARKit = requireNativeComponent('RCTARKit', ARKit);
export default ARKit;