@babylonjs/react-native 0.4.0-alpha.4 → 0.4.0-alpha.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/BabylonModule.d.ts +2 -0
  2. package/BabylonModule.js +21 -0
  3. package/BabylonModule.js.map +1 -0
  4. package/EngineHook.d.ts +3 -0
  5. package/EngineHook.js +179 -0
  6. package/EngineHook.js.map +1 -0
  7. package/EngineView.d.ts +12 -0
  8. package/EngineView.js +117 -0
  9. package/EngineView.js.map +1 -0
  10. package/FontFace.d.ts +12 -0
  11. package/FontFace.js +35 -0
  12. package/FontFace.js.map +1 -0
  13. package/NativeCapture.d.ts +14 -0
  14. package/NativeCapture.js +14 -0
  15. package/NativeCapture.js.map +1 -0
  16. package/README.md +2 -6
  17. package/ReactNativeEngine.d.ts +7 -0
  18. package/ReactNativeEngine.js +33 -0
  19. package/ReactNativeEngine.js.map +1 -0
  20. package/VersionValidation.d.ts +1 -0
  21. package/VersionValidation.js +13 -0
  22. package/VersionValidation.js.map +1 -0
  23. package/android/build.gradle +13 -1
  24. package/android/include/IXrContextARCore.h +10 -0
  25. package/android/src/main/java/com/babylonreactnative/BabylonModule.java +7 -20
  26. package/android/src/main/java/com/babylonreactnative/BabylonNativeInterop.java +89 -140
  27. package/android/src/main/java/com/babylonreactnative/EngineView.java +60 -11
  28. package/android/src/main/jniLibs/arm64-v8a/libBabylonNative.so +0 -0
  29. package/android/src/main/jniLibs/arm64-v8a/libturbomodulejsijni.so +0 -0
  30. package/android/src/main/jniLibs/armeabi-v7a/libBabylonNative.so +0 -0
  31. package/android/src/main/jniLibs/armeabi-v7a/libturbomodulejsijni.so +0 -0
  32. package/android/src/main/jniLibs/x86/libBabylonNative.so +0 -0
  33. package/android/src/main/jniLibs/x86/libturbomodulejsijni.so +0 -0
  34. package/index.d.ts +4 -0
  35. package/index.js +5 -0
  36. package/index.js.map +1 -0
  37. package/ios/BabylonModule.mm +7 -8
  38. package/ios/BabylonNativeInterop.h +7 -4
  39. package/ios/BabylonNativeInterop.mm +84 -124
  40. package/ios/EngineViewManager.mm +34 -19
  41. package/ios/ReactNativeBabylon.xcodeproj/project.pbxproj +9810 -8605
  42. package/ios/include/IXrContextARKit.h +10 -0
  43. package/ios/libs/libBabylonNative.a +0 -0
  44. package/ios/libs/libCanvas.a +0 -0
  45. package/ios/libs/libGenericCodeGen.a +0 -0
  46. package/ios/libs/libGraphics.a +0 -0
  47. package/ios/libs/libJsRuntime.a +0 -0
  48. package/ios/libs/libMachineIndependent.a +0 -0
  49. package/ios/libs/libNativeCapture.a +0 -0
  50. package/ios/libs/libNativeEngine.a +0 -0
  51. package/ios/libs/libNativeInput.a +0 -0
  52. package/ios/libs/libNativeOptimizations.a +0 -0
  53. package/ios/libs/libNativeTracing.a +0 -0
  54. package/ios/libs/libNativeXr.a +0 -0
  55. package/ios/libs/libOGLCompiler.a +0 -0
  56. package/ios/libs/libOSDependent.a +0 -0
  57. package/ios/libs/libSPIRV.a +0 -0
  58. package/ios/libs/libUrlLib.a +0 -0
  59. package/ios/libs/libWindow.a +0 -0
  60. package/ios/libs/libXMLHttpRequest.a +0 -0
  61. package/ios/libs/libastc-codec.a +0 -0
  62. package/ios/libs/libastc.a +0 -0
  63. package/ios/libs/libbgfx.a +0 -0
  64. package/ios/libs/libbimg.a +0 -0
  65. package/ios/libs/libbx.a +0 -0
  66. package/ios/libs/libglslang.a +0 -0
  67. package/ios/libs/libnapi.a +0 -0
  68. package/ios/libs/libspirv-cross-core.a +0 -0
  69. package/ios/libs/libspirv-cross-glsl.a +0 -0
  70. package/ios/libs/libspirv-cross-hlsl.a +0 -0
  71. package/ios/libs/libspirv-cross-msl.a +0 -0
  72. package/ios/libs/libxr.a +0 -0
  73. package/package.json +54 -42
  74. package/shared/BabylonNative.h +24 -0
  75. package/shared/XrAnchorHelper.h +229 -0
  76. package/shared/XrContextHelper.h +179 -0
  77. package/BabylonModule.ts +0 -33
  78. package/EngineHelpers.ts +0 -14
  79. package/EngineHook.ts +0 -97
  80. package/EngineView.tsx +0 -168
  81. package/index.ts +0 -2
  82. package/ios/BabylonNative.h +0 -24
package/EngineHook.ts DELETED
@@ -1,97 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
- import { Platform } from 'react-native';
3
- import { PERMISSIONS, check, request } from 'react-native-permissions';
4
- import { Engine, NativeEngine, WebXRSessionManager } from '@babylonjs/core';
5
- import { BabylonModule } from './BabylonModule';
6
- import { DisposeEngine } from './EngineHelpers';
7
- import * as base64 from 'base-64';
8
-
9
- // These are errors that are normally thrown by WebXR's requestSession, so we should throw the same errors under similar circumstances so app code can be written the same for browser or native.
10
- // https://developer.mozilla.org/en-US/docs/Web/API/XRSystem/requestSession
11
- // https://developer.mozilla.org/en-US/docs/Web/API/DOMException#Error_names
12
- enum DOMError {
13
- NotSupportedError = 9,
14
- InvalidStateError = 11,
15
- SecurityError = 18,
16
- }
17
-
18
- class DOMException {
19
- public constructor(private readonly error: DOMError) { }
20
- get code(): number { return this.error; }
21
- get name(): string { return DOMError[this.error]; }
22
- }
23
-
24
- // Override the WebXRSessionManager.initializeSessionAsync to insert a camera permissions request. It would be cleaner to do this directly in the native XR implementation, but there are a couple problems with that:
25
- // 1. React Native does not provide a way to hook into the permissions request result (at least on Android).
26
- // 2. If it is done on the native side, then we need one implementation per platform.
27
- {
28
- const originalInitializeSessionAsync: (...args: any[]) => Promise<any> = WebXRSessionManager.prototype.initializeSessionAsync;
29
- WebXRSessionManager.prototype.initializeSessionAsync = async function (...args: any[]): Promise<any> {
30
- const cameraPermission = Platform.select({
31
- android: PERMISSIONS.ANDROID.CAMERA,
32
- ios: PERMISSIONS.IOS.CAMERA,
33
- });
34
-
35
- // Only Android and iOS are supported.
36
- if (cameraPermission === undefined) {
37
- throw new DOMException(DOMError.NotSupportedError);
38
- }
39
-
40
- // If the permission has not been granted yet, but also not been blocked, then request permission.
41
- let permissionStatus = await check(cameraPermission);
42
- if (permissionStatus == "denied")
43
- {
44
- permissionStatus = await request(cameraPermission);
45
- }
46
-
47
- // If the permission has still not been granted, then throw an appropriate exception, otherwise continue with the actual XR session initialization.
48
- switch(permissionStatus) {
49
- case "unavailable":
50
- throw new DOMException(DOMError.NotSupportedError);
51
- case "denied":
52
- case "blocked":
53
- throw new DOMException(DOMError.SecurityError);
54
- case "granted":
55
- return originalInitializeSessionAsync.apply(this, args);
56
- }
57
- }
58
- }
59
-
60
- // Babylon Native includes a native atob polyfill, but it relies JSI to deal with the strings, and JSI has a bug where it assumes strings are null terminated, and a base 64 string can contain one of these.
61
- // So for now, provide a JavaScript based atob polyfill.
62
- declare const global: any;
63
- global.atob = base64.decode;
64
-
65
- export function useEngine(): Engine | undefined {
66
- const [engine, setEngine] = useState<Engine>();
67
-
68
- useEffect(() => {
69
- let disposed = false;
70
- let engine: Engine | undefined = undefined;
71
-
72
- (async () => {
73
- if (await BabylonModule.initialize() && !disposed)
74
- {
75
- engine = BabylonModule.createEngine();
76
- setEngine(engine);
77
- }
78
- })();
79
-
80
- return () => {
81
- disposed = true;
82
- // NOTE: Do not use setEngine with a callback to dispose the engine instance as that callback does not get called during component unmount when compiled in release.
83
- if (engine) {
84
- DisposeEngine(engine);
85
- }
86
- // Ideally we would always do a reset here as we don't want different behavior between debug and release. Unfortunately, fast refresh has some strange behavior that
87
- // makes it quite difficult to get this to work correctly (e.g. it re-runs previous useEffect instances, which means it can try to use Babylon Native in a de-initialized state).
88
- // TODO: https://github.com/BabylonJS/BabylonReactNative/issues/125
89
- if (!__DEV__) {
90
- BabylonModule.reset();
91
- }
92
- setEngine(undefined);
93
- };
94
- }, []);
95
-
96
- return engine;
97
- }
package/EngineView.tsx DELETED
@@ -1,168 +0,0 @@
1
- import React, { Component, FunctionComponent, SyntheticEvent, useCallback, useEffect, useState, useRef } from 'react';
2
- import { requireNativeComponent, NativeModules, ViewProps, AppState, AppStateStatus, View, Text, findNodeHandle, UIManager } from 'react-native';
3
- import { Camera } from '@babylonjs/core';
4
- import { IsEngineDisposed } from './EngineHelpers';
5
- import { BabylonModule } from './BabylonModule';
6
-
7
- declare const global: any;
8
- const isRemoteDebuggingEnabled = !global['nativeCallSyncHook'];
9
-
10
- const EngineViewManager: {
11
- setJSThread(): void;
12
- } = NativeModules.EngineViewManager;
13
-
14
- // Not all platforms need this, but for those that do, this is intended to be a synchronous call to boostrap the ability to run native code on the JavaScript thread.
15
- if (EngineViewManager && EngineViewManager.setJSThread && !isRemoteDebuggingEnabled) {
16
- EngineViewManager.setJSThread();
17
- }
18
-
19
- interface NativeEngineViewProps extends ViewProps {
20
- onSnapshotDataReturned: (event: SyntheticEvent) => void;
21
- }
22
-
23
- const NativeEngineView: {
24
- prototype: Component<NativeEngineViewProps>;
25
- new(props: Readonly<NativeEngineViewProps>): Component<NativeEngineViewProps>;
26
- } = requireNativeComponent('EngineView');
27
-
28
- export interface EngineViewProps extends ViewProps {
29
- camera?: Camera;
30
- displayFrameRate?: boolean;
31
- onInitialized?: (view: EngineViewCallbacks) => void;
32
- }
33
-
34
- export interface EngineViewCallbacks {
35
- takeSnapshot: () => Promise<string>;
36
- }
37
-
38
- export const EngineView: FunctionComponent<EngineViewProps> = (props: EngineViewProps) => {
39
- const [failedInitialization, setFailedInitialization] = useState(false);
40
- const [appState, setAppState] = useState(AppState.currentState);
41
- const [fps, setFps] = useState<number>();
42
- const engineViewRef = useRef<Component<NativeEngineViewProps>>(null);
43
- const snapshotPromise = useRef<{ promise: Promise<string>, resolve: (data: string) => void }>();
44
-
45
- useEffect(() => {
46
- (async () => {
47
- if (!await BabylonModule.whenInitialized()) {
48
- setFailedInitialization(true);
49
- }
50
- })();
51
- }, []);
52
-
53
- useEffect(() => {
54
- const onAppStateChanged = (appState: AppStateStatus) => {
55
- setAppState(appState);
56
- };
57
-
58
- AppState.addEventListener("change", onAppStateChanged);
59
-
60
- return () => {
61
- AppState.removeEventListener("change", onAppStateChanged);
62
- }
63
- }, []);
64
-
65
- useEffect(() => {
66
- if (props.camera && appState === "active") {
67
- const engine = props.camera.getScene().getEngine();
68
-
69
- if (!IsEngineDisposed(engine)) {
70
- engine.runRenderLoop(() => {
71
- for (let scene of engine.scenes) {
72
- scene.render();
73
- }
74
- });
75
-
76
- return () => {
77
- if (!IsEngineDisposed(engine)) {
78
- engine.stopRenderLoop();
79
- }
80
- };
81
- }
82
- }
83
-
84
- return undefined;
85
- }, [props.camera, appState]);
86
-
87
- useEffect(() => {
88
- if (props.camera && (props.displayFrameRate ?? __DEV__)) {
89
- const engine = props.camera.getScene().getEngine();
90
-
91
- if (!IsEngineDisposed(engine)) {
92
- setFps(engine.getFps());
93
- const timerHandle = setInterval(() => {
94
- setFps(engine.getFps());
95
- }, 1000);
96
-
97
- return () => {
98
- clearInterval(timerHandle);
99
- };
100
- }
101
- }
102
-
103
- setFps(undefined);
104
- return undefined;
105
- }, [props.camera, props.displayFrameRate]);
106
-
107
- // Call onInitialized if provided, and include the callback for takeSnapshot.
108
- useEffect(() => {
109
- if (props.onInitialized) {
110
- props.onInitialized({
111
- takeSnapshot: (): Promise<string> => {
112
- if (!snapshotPromise.current) {
113
- let resolveFunction: ((data: string) => void) | undefined;
114
- const promise = new Promise<string>((resolutionFunc) => {
115
- resolveFunction = resolutionFunc;
116
- });
117
-
118
- // Resolution functions should always be initialized.
119
- if (resolveFunction) {
120
- snapshotPromise.current = { promise: promise, resolve: resolveFunction };
121
- }
122
- else {
123
- throw new Error("Resolution functions not initialized after snapshot promise creation.");
124
- }
125
-
126
- UIManager.dispatchViewManagerCommand(
127
- findNodeHandle(engineViewRef.current),
128
- "takeSnapshot",
129
- []);
130
- }
131
-
132
- return snapshotPromise.current.promise;
133
- }
134
- });
135
- }
136
- }, [props.onInitialized]);
137
-
138
- // Handle snapshot data returned.
139
- const snapshotDataReturnedHandler = useCallback((event: SyntheticEvent) => {
140
- // The nativeEvent is a DOMEvent which doesn't have a typescript definition. Cast it to an Event object with a data property.
141
- const { data } = event.nativeEvent as Event & { data: string };
142
- if (snapshotPromise.current) {
143
- snapshotPromise.current.resolve(data);
144
- snapshotPromise.current = undefined;
145
- }
146
- }, []);
147
-
148
- if (!failedInitialization) {
149
- return (
150
- <View style={[props.style, { overflow: "hidden" }]}>
151
- <NativeEngineView ref={engineViewRef} style={{ flex: 1 }} onSnapshotDataReturned={snapshotDataReturnedHandler} />
152
- { fps && <Text style={{ color: 'yellow', position: 'absolute', margin: 10, right: 0, top: 0 }}>FPS: {Math.round(fps)}</Text>}
153
- </View>
154
- );
155
- } else {
156
- const message = "Could not initialize Babylon Native.";
157
- if (!__DEV__) {
158
- throw new Error(message);
159
- }
160
-
161
- return (
162
- <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
163
- <Text style={{ fontSize: 24 }}>{message}</Text>
164
- { isRemoteDebuggingEnabled && <Text style={{ fontSize: 12 }}>React Native remote debugging does not work with Babylon Native.</Text>}
165
- </View>
166
- );
167
- }
168
- }
package/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './EngineView';
2
- export * from './EngineHook';
@@ -1,24 +0,0 @@
1
- #pragma once
2
-
3
- #include <jsi/jsi.h>
4
- #include <ReactCommon/CallInvoker.h>
5
-
6
- namespace Babylon
7
- {
8
- class Native final
9
- {
10
- public:
11
- // This class must be constructed from the JavaScript thread
12
- Native(facebook::jsi::Runtime& jsiRuntime, std::shared_ptr<facebook::react::CallInvoker> callInvoker, void* windowPtr, size_t width, size_t height);
13
- ~Native();
14
- void Refresh(void* windowPtr, size_t width, size_t height);
15
- void Resize(size_t width, size_t height);
16
- void Reset();
17
- void SetPointerButtonState(uint32_t pointerId, uint32_t buttonId, bool isDown, uint32_t x, uint32_t y);
18
- void SetPointerPosition(uint32_t pointerId, uint32_t x, uint32_t y);
19
-
20
- private:
21
- class Impl;
22
- std::unique_ptr<Impl> m_impl{};
23
- };
24
- }