UNPKG

17.4 kBJavaScriptView Raw
1import Constants from 'expo-constants';
2import { Dimensions, NativeEventEmitter, NativeModules, Platform, findNodeHandle, } from 'react-native';
3const ExponentAR = NativeModules.ExponentAR || {};
4const emitter = new NativeEventEmitter(ExponentAR);
5/**
6 * Tracking Configuration
7 * Options for how ARKit constructs a scene coordinate system based on real-world device motion.
8 * https://developer.apple.com/documentation/arkit/arconfiguration
9 */
10export var TrackingConfiguration;
11(function (TrackingConfiguration) {
12 /**
13 * Provides high-quality AR experiences that use the rear-facing camera precisely track a device's
14 * position and orientation and allow plane detection and hit testing.
15 */
16 TrackingConfiguration["World"] = "ARWorldTrackingConfiguration";
17 /**
18 * Provides basic AR experiences that use the rear-facing camera and track only a device's
19 * orientation.
20 */
21 TrackingConfiguration["Orientation"] = "AROrientationTrackingConfiguration";
22 /**
23 * Provides AR experiences that use the front-facing camera and track the movement and expressions
24 * of the user's face.
25 */
26 TrackingConfiguration["Face"] = "ARFaceTrackingConfiguration";
27})(TrackingConfiguration || (TrackingConfiguration = {}));
28export var DepthDataQuality;
29(function (DepthDataQuality) {
30 DepthDataQuality["Low"] = "AVDepthDataQualityLow";
31 DepthDataQuality["High"] = "AVDepthDataQualityHigh";
32})(DepthDataQuality || (DepthDataQuality = {}));
33export var DepthDataAccuracy;
34(function (DepthDataAccuracy) {
35 DepthDataAccuracy["Absolute"] = "AVDepthDataAccuracyAbsolute";
36 DepthDataAccuracy["Relative"] = "AVDepthDataAccuracyRelative";
37})(DepthDataAccuracy || (DepthDataAccuracy = {}));
38export var BlendShape;
39(function (BlendShape) {
40 BlendShape["BrowDownL"] = "browDown_L";
41 BlendShape["BrowDownR"] = "browDown_R";
42 BlendShape["BrowInnerUp"] = "browInnerUp";
43 BlendShape["BrowOuterUpL"] = "browOuterUp_L";
44 BlendShape["BrowOuterUpR"] = "browOuterUp_R";
45 BlendShape["CheekPuff"] = "cheekPuff";
46 BlendShape["CheekSquintL"] = "cheekSquint_L";
47 BlendShape["CheekSquintR"] = "cheekSquint_R";
48 BlendShape["EyeBlinkL"] = "eyeBlink_L";
49 BlendShape["EyeBlinkR"] = "eyeBlink_R";
50 BlendShape["EyeLookDownL"] = "eyeLookDown_L";
51 BlendShape["EyeLookDownR"] = "eyeLookDown_R";
52 BlendShape["EyeLookInL"] = "eyeLookIn_L";
53 BlendShape["EyeLookInR"] = "eyeLookIn_R";
54 BlendShape["EyeLookOutL"] = "eyeLookOut_L";
55 BlendShape["EyeLookOutR"] = "eyeLookOut_R";
56 BlendShape["EyeLookUpL"] = "eyeLookUp_L";
57 BlendShape["EyeLookUpR"] = "eyeLookUp_R";
58 BlendShape["EyeSquintL"] = "eyeSquint_L";
59 BlendShape["EyeSquintR"] = "eyeSquint_R";
60 BlendShape["EyeWideL"] = "eyeWide_L";
61 BlendShape["EyeWideR"] = "eyeWide_R";
62 BlendShape["JawForward"] = "jawForward";
63 BlendShape["JawLeft"] = "jawLeft";
64 BlendShape["JawOpen"] = "jawOpen";
65 BlendShape["JawRight"] = "jawRight";
66 BlendShape["MouthClose"] = "mouthClose";
67 BlendShape["MouthDimpleL"] = "mouthDimple_L";
68 BlendShape["MouthDimpleR"] = "mouthDimple_R";
69 BlendShape["MouthFrownL"] = "mouthFrown_L";
70 BlendShape["MouthFrownR"] = "mouthFrown_R";
71 BlendShape["MouthFunnel"] = "mouthFunnel";
72 BlendShape["MouthLeft"] = "mouthLeft";
73 BlendShape["MouthLowerDownL"] = "mouthLowerDown_L";
74 BlendShape["MouthLowerDownR"] = "mouthLowerDown_R";
75 BlendShape["MouthPressL"] = "mouthPress_L";
76 BlendShape["MouthPressR"] = "mouthPress_R";
77 BlendShape["MouthPucker"] = "mouthPucker";
78 BlendShape["MouthRight"] = "mouthRight";
79 BlendShape["MouthRollLower"] = "mouthRollLower";
80 BlendShape["MouthRollUpper"] = "mouthRollUpper";
81 BlendShape["MouthShrugLower"] = "mouthShrugLower";
82 BlendShape["MouthShrugUpper"] = "mouthShrugUpper";
83 BlendShape["MouthSmileL"] = "mouthSmile_L";
84 BlendShape["MouthSmileR"] = "mouthSmile_R";
85 BlendShape["MouthStretchL"] = "mouthStretch_L";
86 BlendShape["MouthStretchR"] = "mouthStretch_R";
87 BlendShape["MouthUpperUpL"] = "mouthUpperUp_L";
88 BlendShape["MouthUpperUpR"] = "mouthUpperUp_R";
89 BlendShape["NoseSneerL"] = "noseSneer_L";
90 BlendShape["NoseSneerR"] = "noseSneer_R";
91})(BlendShape || (BlendShape = {}));
92export var FaceAnchorProp;
93(function (FaceAnchorProp) {
94 FaceAnchorProp["Geometry"] = "geometry";
95 FaceAnchorProp["BlendShapes"] = "blendShapes";
96})(FaceAnchorProp || (FaceAnchorProp = {}));
97/**
98 * Plane Detection
99 * Options for whether and how ARKit detects flat surfaces in captured images.
100 * https://developer.apple.com/documentation/arkit/arplanedetection
101 */
102export var PlaneDetection;
103(function (PlaneDetection) {
104 /**
105 * No plane detection is run
106 */
107 PlaneDetection["None"] = "none";
108 /**
109 * Plane detection determines horizontal planes in the scene
110 */
111 PlaneDetection["Horizontal"] = "horizontal";
112 /**
113 * Plane detection determines vertical planes in the scene
114 */
115 PlaneDetection["Vertical"] = "vertical";
116})(PlaneDetection || (PlaneDetection = {}));
117/**
118 * Hit-Test Result Types
119 * Possible types for specifying a hit-test search, or for the result of a hit-test search.
120 * https://developer.apple.com/documentation/arkit/arhittestresulttype
121 */
122export var HitTestResultTypes;
123(function (HitTestResultTypes) {
124 /**
125 * Result type from intersecting the nearest feature point.
126 */
127 HitTestResultTypes["FeaturePoint"] = "featurePoint";
128 /**
129 * Result type from intersecting a horizontal plane estimate, determined for the current frame.
130 */
131 HitTestResultTypes["HorizontalPlane"] = "horizontalPlane";
132 /**
133 * Result type from intersecting a vertical plane estimate, determined for the current frame.
134 */
135 HitTestResultTypes["VerticalPlane"] = "verticalPlane";
136 /**
137 * Result type from intersecting with an existing plane anchor.
138 */
139 HitTestResultTypes["ExistingPlane"] = "existingPlane";
140 /**
141 * Result type from intersecting with an existing plane anchor, taking into account the plane’s
142 * extent.
143 */
144 HitTestResultTypes["ExistingPlaneUsingExtent"] = "existingPlaneUsingExtent";
145 /**
146 * Result type from intersecting with an existing plane anchor, taking into account the plane’s
147 * geometry.
148 */
149 HitTestResultTypes["ExistingPlaneUsingGeometry"] = "existingPlaneUsingGeometry";
150})(HitTestResultTypes || (HitTestResultTypes = {}));
151/**
152 * World Alignment
153 * Options for how ARKit constructs a scene coordinate system based on real-world device motion.
154 * https://developer.apple.com/documentation/arkit/arworldalignment
155 */
156export var WorldAlignment;
157(function (WorldAlignment) {
158 /**
159 * Aligns the world with gravity that is defined by vector (0, -1, 0).
160 */
161 WorldAlignment["Gravity"] = "gravity";
162 /**
163 * Aligns the world with gravity that is defined by the vector (0, -1, 0) and heading (w.r.t. true
164 * north) that is given by the vector (0, 0, -1).
165 */
166 WorldAlignment["GravityAndHeading"] = "gravityAndHeading";
167 /**
168 * Aligns the world with the camera’s orientation.
169 */
170 WorldAlignment["AlignmentCamera"] = "alignmentCamera";
171})(WorldAlignment || (WorldAlignment = {}));
172export var EventType;
173(function (EventType) {
174 EventType[EventType["FrameDidUpdate"] = ExponentAR.frameDidUpdate] = "FrameDidUpdate";
175 EventType[EventType["DidFailWithError"] = ExponentAR.didFailWithError] = "DidFailWithError";
176 EventType[EventType["AnchorsDidUpdate"] = ExponentAR.anchorsDidUpdate] = "AnchorsDidUpdate";
177 EventType[EventType["CameraDidChangeTrackingState"] = ExponentAR.cameraDidChangeTrackingState] = "CameraDidChangeTrackingState";
178 EventType[EventType["SessionWasInterrupted"] = ExponentAR.sessionWasInterrupted] = "SessionWasInterrupted";
179 EventType[EventType["SessionInterruptionEnded"] = ExponentAR.sessionInterruptionEnded] = "SessionInterruptionEnded";
180})(EventType || (EventType = {}));
181export var AnchorType;
182(function (AnchorType) {
183 AnchorType["Face"] = "ARFaceAnchor";
184 AnchorType["Image"] = "ARImageAnchor";
185 AnchorType["Plane"] = "ARPlaneAnchor";
186 AnchorType["Anchor"] = "ARAnchor";
187})(AnchorType || (AnchorType = {}));
188export var AnchorEventType;
189(function (AnchorEventType) {
190 AnchorEventType["Add"] = "add";
191 AnchorEventType["Update"] = "update";
192 AnchorEventType["Remove"] = "remove";
193})(AnchorEventType || (AnchorEventType = {}));
194export var FrameAttribute;
195(function (FrameAttribute) {
196 FrameAttribute["Anchors"] = "anchors";
197 FrameAttribute["RawFeaturePoints"] = "rawFeaturePoints";
198 FrameAttribute["LightEstimation"] = "lightEstimation";
199 FrameAttribute["CapturedDepthData"] = "capturedDepthData";
200})(FrameAttribute || (FrameAttribute = {}));
201export var TrackingState;
202(function (TrackingState) {
203 /** Tracking is not available. */
204 TrackingState["NotAvailable"] = "ARTrackingStateNotAvailable";
205 /** Tracking is limited. See tracking reason for details. */
206 TrackingState["Limited"] = "ARTrackingStateLimited";
207 /** Tracking is Normal. */
208 TrackingState["Normal"] = "ARTrackingStateNormal";
209})(TrackingState || (TrackingState = {}));
210export var TrackingStateReason;
211(function (TrackingStateReason) {
212 /** Tracking is not limited. */
213 TrackingStateReason["None"] = "ARTrackingStateReasonNone";
214 /** Tracking is limited due to initialization in progress. */
215 TrackingStateReason["Initializing"] = "ARTrackingStateReasonInitializing";
216 /** Tracking is limited due to a excessive motion of the camera. */
217 TrackingStateReason["ExcessiveMotion"] = "ARTrackingStateReasonExcessiveMotion";
218 /** Tracking is limited due to a lack of features visible to the camera. */
219 TrackingStateReason["InsufficientFeatures"] = "ARTrackingStateReasonInsufficientFeatures";
220 /** Tracking is limited due to a relocalization in progress. */
221 TrackingStateReason["Relocalizing"] = "ARTrackingStateReasonRelocalizing";
222})(TrackingStateReason || (TrackingStateReason = {}));
223export function getVersion() {
224 return ExponentAR.ARKitVersion;
225}
226const AvailabilityErrorMessages = {
227 Simulator: `Cannot run EXGL in a simulator`,
228 ANineChip: `ARKit can only run on iOS devices with A9 (2015) or greater chips! This is a`,
229 ARKitOnlyOnIOS: `ARKit can only run on an iOS device! This is a`,
230};
231export function isAvailable() {
232 // Device has A9 chip
233 const hasA9Chip = Constants.deviceYearClass && Constants.deviceYearClass > 2014;
234 if (!Constants.isDevice || // Prevent Simulators
235 // @ts-ignore
236 Platform.isTVOS ||
237 Platform.OS !== 'ios' || // Device is iOS
238 !hasA9Chip ||
239 !ExponentAR.isSupported || // ARKit is included in the build
240 !ExponentAR.startAsync // Older SDK versions (27 and lower) that are fully compatible
241 ) {
242 return false;
243 }
244 return true;
245}
246export function getUnavailabilityReason() {
247 if (!Constants.isDevice) {
248 return AvailabilityErrorMessages.Simulator;
249 }
250 else if (Platform.OS !== 'ios') {
251 return `${AvailabilityErrorMessages.ARKitOnlyOnIOS} ${Platform.OS} device`;
252 }
253 else if (Constants.deviceYearClass == null || Constants.deviceYearClass < 2015) {
254 return `${AvailabilityErrorMessages.ANineChip} ${Constants.deviceYearClass} device`;
255 }
256 return 'Unknown Reason';
257}
258export function onFrameDidUpdate(listener) {
259 return _addListener(EventType.FrameDidUpdate, listener);
260}
261export function onDidFailWithError(listener) {
262 return _addListener(EventType.DidFailWithError, listener);
263}
264export function onAnchorsDidUpdate(listener) {
265 return _addListener(EventType.AnchorsDidUpdate, listener);
266}
267export function onCameraDidChangeTrackingState(listener) {
268 return _addListener(EventType.CameraDidChangeTrackingState, listener);
269}
270export function onSessionWasInterrupted(listener) {
271 return _addListener(EventType.SessionWasInterrupted, listener);
272}
273export function onSessionInterruptionEnded(listener) {
274 return _addListener(EventType.SessionInterruptionEnded, listener);
275}
276function _addListener(eventType, event) {
277 return emitter.addListener(eventType, event);
278}
279export function removeAllListeners(eventType) {
280 emitter.removeAllListeners(eventType);
281}
282// TODO: support multiple types (take an array or bit flags)
283export function performHitTest(point, types) {
284 if (ExponentAR.performHitTest) {
285 return ExponentAR.performHitTest(point, types);
286 }
287 throw new Error(`AR hit testing is not supported on this device`);
288}
289export async function setDetectionImagesAsync(images) {
290 if (ExponentAR.setDetectionImagesAsync) {
291 await ExponentAR.setDetectionImagesAsync(images);
292 }
293}
294export function getCurrentFrame(attributes) {
295 if (ExponentAR.getCurrentFrame) {
296 return ExponentAR.getCurrentFrame(attributes);
297 }
298 throw new Error(`AR is not supported on this device`);
299}
300export function getARMatrices(near, far) {
301 if (ExponentAR.getARMatrices) {
302 return ExponentAR.getARMatrices(near, far);
303 }
304 throw new Error(`AR is not supported on this device`);
305}
306export async function stopAsync() {
307 if (ExponentAR.stopAsync) {
308 await ExponentAR.stopAsync();
309 }
310}
311export async function startAsync(node, configuration) {
312 let handle = typeof node === 'number' ? node : _getNodeHandle(node);
313 if (ExponentAR.startAsync) {
314 return await ExponentAR.startAsync(handle, configuration);
315 }
316 throw new Error(`AR is not supported on this device`);
317}
318function _getNodeHandle(component) {
319 let handle = findNodeHandle(component);
320 if (handle === null) {
321 throw new Error(`Could not find the React node handle for the AR component: ${component}`);
322 }
323 return handle;
324}
325export function reset() {
326 if (ExponentAR.reset) {
327 ExponentAR.reset();
328 }
329}
330export function resume() {
331 if (ExponentAR.resume) {
332 ExponentAR.resume();
333 }
334}
335export function pause() {
336 if (ExponentAR.pause) {
337 ExponentAR.pause();
338 }
339}
340export async function setConfigurationAsync(configuration) {
341 if (ExponentAR.setConfigurationAsync) {
342 await ExponentAR.setConfigurationAsync(configuration);
343 }
344}
345export function getProvidesAudioData() {
346 if (ExponentAR.getProvidesAudioData) {
347 return ExponentAR.getProvidesAudioData();
348 }
349 return false;
350}
351export function setProvidesAudioData(providesAudioData) {
352 if (ExponentAR.setProvidesAudioData) {
353 ExponentAR.setProvidesAudioData(providesAudioData);
354 }
355}
356export function setPlaneDetection(planeDetection) {
357 if (ExponentAR.setPlaneDetection) {
358 ExponentAR.setPlaneDetection(planeDetection);
359 }
360}
361export function getPlaneDetection() {
362 if (ExponentAR.getPlaneDetection) {
363 return ExponentAR.getPlaneDetection();
364 }
365 throw new Error(`AR plane detection is not supported on this device`);
366}
367export function getCameraTexture() {
368 if (ExponentAR.getCameraTexture) {
369 return ExponentAR.getCameraTexture();
370 }
371 throw new Error(`AR camera textures are not supported on this device`);
372}
373export async function setWorldOriginAsync(matrix_float4x4) {
374 if (ExponentAR.setWorldOriginAsync) {
375 await ExponentAR.setWorldOriginAsync(matrix_float4x4);
376 }
377}
378export function setLightEstimationEnabled(isLightEstimationEnabled) {
379 if (ExponentAR.setLightEstimationEnabled) {
380 ExponentAR.setLightEstimationEnabled(isLightEstimationEnabled);
381 }
382}
383export function getLightEstimationEnabled() {
384 if (ExponentAR.getLightEstimationEnabled) {
385 return ExponentAR.getLightEstimationEnabled();
386 }
387 return false;
388}
389export function setAutoFocusEnabled(isAutoFocusEnabled) {
390 if (ExponentAR.setAutoFocusEnabled) {
391 ExponentAR.setAutoFocusEnabled(isAutoFocusEnabled);
392 }
393}
394export function getAutoFocusEnabled() {
395 if (ExponentAR.getAutoFocusEnabled) {
396 return ExponentAR.getAutoFocusEnabled();
397 }
398 return false;
399}
400export function setWorldAlignment(worldAlignment) {
401 if (ExponentAR.setWorldAlignment) {
402 ExponentAR.setWorldAlignment(worldAlignment);
403 }
404}
405export function getWorldAlignment() {
406 if (ExponentAR.getWorldAlignment) {
407 return ExponentAR.getWorldAlignment();
408 }
409 throw new Error(`AR world alignment is not supported on this device`);
410}
411export function isConfigurationAvailable(configuration) {
412 const { width, height } = Dimensions.get('window');
413 // @ts-ignore: re-evaluate this for the new iPhones (2018)
414 const isX = (width === 812 || height === 812) && !Platform.isTVOS && !Platform.isPad;
415 if (configuration === TrackingConfiguration.Face && isX && isAvailable()) {
416 return true;
417 }
418 return !!ExponentAR[configuration];
419}
420export function getSupportedVideoFormats(configuration) {
421 const videoFormats = {
422 [TrackingConfiguration.World]: 'WorldTrackingVideoFormats',
423 [TrackingConfiguration.Orientation]: 'OrientationTrackingVideoFormats',
424 [TrackingConfiguration.Face]: 'FaceTrackingVideoFormats',
425 };
426 const videoFormat = videoFormats[configuration];
427 return ExponentAR[videoFormat] || [];
428}
429export function isFrontCameraAvailable() {
430 return isConfigurationAvailable(TrackingConfiguration.Face);
431}
432export function isRearCameraAvailable() {
433 return isConfigurationAvailable(TrackingConfiguration.World);
434}
435//# sourceMappingURL=AR.js.map
\No newline at end of file