@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
@@ -1,16 +1,13 @@
1
1
  #import "BabylonNativeInterop.h"
2
- #import "BabylonNative.h"
2
+ #import "../shared/BabylonNative.h"
3
3
 
4
4
  #import <React/RCTBridge+Private.h>
5
5
  #import <jsi/jsi.h>
6
+ #include <ReactCommon/CallInvoker.h>
6
7
 
7
8
  #import <Foundation/Foundation.h>
8
9
 
9
- #import <functional>
10
10
  #import <memory>
11
- #import <vector>
12
- #import <unordered_map>
13
- #import <mutex>
14
11
 
15
12
  using namespace facebook;
16
13
 
@@ -27,144 +24,107 @@ namespace {
27
24
 
28
25
  @implementation BabylonNativeInterop
29
26
 
30
- static RCTBridge* currentBridge;
31
- static MTKView* currentView;
32
- static std::unique_ptr<Babylon::Native> currentNativeInstance;
33
- static std::unordered_map<void*, std::vector<RCTPromiseResolveBlock>> initializationPromises;
34
- static std::mutex mapMutex;
35
- static NSMutableArray* activeTouches;
27
+ static NSMutableArray* activeTouches = [NSMutableArray new];
36
28
 
37
- + (void)setView:(RCTBridge*)bridge jsRunLoop:(NSRunLoop*)jsRunLoop mktView:(MTKView*)mtkView {
38
- const int width = static_cast<int>(mtkView.bounds.size.width * UIScreen.mainScreen.scale);
39
- const int height = static_cast<int>(mtkView.bounds.size.height * UIScreen.mainScreen.scale);
40
- if (width != 0 && height != 0) {
41
- // NOTE: jsRunLoop should only be null when remote debugging is enabled.
42
- // In this case, we can just use the main loop, because we are only
43
- // going to set an error state (which can happen on any thread).
44
- if (!jsRunLoop) {
45
- jsRunLoop = NSRunLoop.mainRunLoop;
46
- }
47
-
48
- [jsRunLoop performBlock:^{
49
- if (bridge != currentBridge) {
50
- [BabylonNativeInterop setCurrentNativeInstance:bridge mtkView:mtkView width:width height:height];
51
- } else if (currentNativeInstance) {
52
- if (mtkView != currentView) {
53
- [BabylonNativeInterop setCurrentView:mtkView];
54
- currentNativeInstance->Refresh((__bridge void*)currentView, width, height);
55
- } else {
56
- // NOTE: This will cause Metal API Validation to fail if it is enabled when the debugger is attached, which stops app execution.
57
- // For now, be sure to disable Metal API Validation under Product->Scheme->Edit Scheme.
58
- currentNativeInstance->Resize(width, height);
59
- }
60
- }
61
- }];
62
- }
29
+ + (void)initialize:(RCTBridge*)bridge {
30
+ auto jsCallInvoker{ bridge.jsCallInvoker };
31
+ auto jsDispatcher{ [jsCallInvoker{ std::move(jsCallInvoker) }](std::function<void()> func)
32
+ {
33
+ jsCallInvoker->invokeAsync([func{ std::move(func) }]
34
+ {
35
+ func();
36
+ });
37
+ } };
38
+
39
+ Babylon::Initialize(*GetJSIRuntime(bridge), std::move(jsDispatcher));
40
+
41
+ [[NSNotificationCenter defaultCenter] removeObserver:self
42
+ name:RCTBridgeWillInvalidateModulesNotification
43
+ object:bridge.parentBridge];
44
+
45
+ [[NSNotificationCenter defaultCenter] addObserver:self
46
+ selector:@selector(onBridgeWillInvalidate:)
47
+ name:RCTBridgeWillInvalidateModulesNotification
48
+ object:bridge.parentBridge];
63
49
  }
64
50
 
65
- + (void)reportTouchEvent:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
66
- if (currentNativeInstance) {
67
- for (UITouch* touch in touches) {
68
- if (touch.view == currentView) {
69
- const CGFloat scale = UIScreen.mainScreen.scale;
70
- const CGPoint pointerPosition = [touch locationInView:currentView];
71
- const uint32_t x = static_cast<uint32_t>(pointerPosition.x * scale);
72
- const uint32_t y = static_cast<uint32_t>(pointerPosition.y * scale);
73
-
74
- switch (touch.phase) {
75
- case UITouchPhaseBegan: {
76
- NSUInteger pointerId = [activeTouches indexOfObject:[NSNull null]];
77
- if (pointerId == NSNotFound) {
78
- pointerId = [activeTouches count];
79
- [activeTouches addObject:touch];
80
- } else {
81
- [activeTouches replaceObjectAtIndex:pointerId withObject:touch];
82
- }
83
- currentNativeInstance->SetPointerButtonState(static_cast<uint32_t>(pointerId), 0, true, x, y);
84
- break;
85
- }
86
-
87
- case UITouchPhaseMoved: {
88
- NSUInteger pointerId = [activeTouches indexOfObject:touch];
89
- currentNativeInstance->SetPointerPosition(static_cast<uint32_t>(pointerId), x, y);
90
- break;
91
- }
92
-
93
- case UITouchPhaseEnded:
94
- case UITouchPhaseCancelled: {
95
- NSUInteger pointerId = [activeTouches indexOfObject:touch];
96
- [activeTouches replaceObjectAtIndex:pointerId withObject:[NSNull null]];
97
- currentNativeInstance->SetPointerButtonState(static_cast<uint32_t>(pointerId), 0, false, x, y);
98
- break;
99
- }
100
-
101
- default:
102
- break;
103
- }
104
- }
105
- }
106
- }
51
+ // NOTE: This happens during dev mode reload, when the JS engine is being shutdown and restarted.
52
+ + (void)onBridgeWillInvalidate:(NSNotification*)notification
53
+ {
54
+ Babylon::Deinitialize();
107
55
  }
108
56
 
109
- + (void)whenInitialized:(RCTBridge*)bridge resolve:(RCTPromiseResolveBlock)resolve {
110
- const std::lock_guard<std::mutex> lock(mapMutex);
111
- if (bridge == currentBridge) {
112
- resolve([NSNumber numberWithUnsignedLong:reinterpret_cast<uintptr_t>(currentNativeInstance.get())]);
113
- } else {
114
- initializationPromises[(__bridge void*)bridge].push_back(resolve);
57
+ + (void)updateView:(MTKView*)mtkView {
58
+ const CGFloat scale = mtkView.contentScaleFactor;
59
+ const int width = static_cast<int>(mtkView.bounds.size.width * scale);
60
+ const int height = static_cast<int>(mtkView.bounds.size.height * scale);
61
+ if (width != 0 && height != 0) {
62
+ Babylon::UpdateView((__bridge void*)mtkView, width, height);
115
63
  }
116
64
  }
117
65
 
118
- + (void)reset {
119
- if (currentNativeInstance) {
120
- currentNativeInstance->Reset();
121
- }
66
+ + (void)renderView {
67
+ Babylon::RenderView();
122
68
  }
123
69
 
124
- + (void)setCurrentView:(MTKView*)mtkView {
125
- currentView = mtkView;
126
- activeTouches = [NSMutableArray new];
70
+ + (void)resetView {
71
+ Babylon::ResetView();
127
72
  }
128
73
 
129
- + (void)setCurrentNativeInstance:(RCTBridge*)bridge mtkView:(MTKView*)mtkView width:(int)width height:(int)height {
130
- [BabylonNativeInterop setCurrentView:mtkView];
74
+ + (void)updateXRView:(MTKView*)mtkView {
75
+ Babylon::UpdateXRView((__bridge void*)mtkView);
76
+ }
131
77
 
132
- {
133
- const std::lock_guard<std::mutex> lock(mapMutex);
134
-
135
- if (bridge != currentBridge) {
136
- if (currentBridge == nil || currentBridge.parentBridge != bridge.parentBridge) {
137
- [[NSNotificationCenter defaultCenter] addObserver:self
138
- selector:@selector(onBridgeWillInvalidate:)
139
- name:RCTBridgeWillInvalidateModulesNotification
140
- object:bridge.parentBridge];
141
- }
78
+ + (bool)isXRActive {
79
+ return Babylon::IsXRActive();
80
+ }
142
81
 
143
- currentBridge = bridge;
144
- }
82
+ + (void)reportTouchEvent:(MTKView*)mtkView touches:(NSSet<UITouch*>*)touches event:(UIEvent*)event {
83
+ for (UITouch* touch in touches) {
84
+ if (touch.view == mtkView) {
85
+ const CGFloat scale = mtkView.contentScaleFactor;
86
+ const CGPoint pointerPosition = [touch locationInView:mtkView];
87
+ const uint32_t x = static_cast<uint32_t>(pointerPosition.x * scale);
88
+ const uint32_t y = static_cast<uint32_t>(pointerPosition.y * scale);
89
+
90
+ switch (touch.phase) {
91
+ case UITouchPhaseBegan: {
92
+ // The activeTouches array only grows, it does not shrink (to keep indices constant since they are used as pointer ids),
93
+ // so look for an unused (null) array element and reuse it if found. Otherwise, add a new entry to the array.
94
+ NSUInteger pointerId = [activeTouches indexOfObject:[NSNull null]];
95
+ if (pointerId != NSNotFound) {
96
+ [activeTouches replaceObjectAtIndex:pointerId withObject:touch];
97
+ } else {
98
+ pointerId = [activeTouches count];
99
+ [activeTouches addObject:touch];
100
+ }
101
+ Babylon::SetTouchButtonState(static_cast<uint32_t>(pointerId), true, x, y);
102
+ break;
103
+ }
145
104
 
146
- currentNativeInstance.reset();
105
+ case UITouchPhaseMoved: {
106
+ NSUInteger pointerId = [activeTouches indexOfObject:touch];
107
+ if (pointerId != NSNotFound) {
108
+ Babylon::SetTouchPosition(static_cast<uint32_t>(pointerId), x, y);
109
+ }
110
+ break;
111
+ }
147
112
 
148
- jsi::Runtime* jsiRuntime = GetJSIRuntime(currentBridge);
149
- if (jsiRuntime) {
150
- currentNativeInstance = std::make_unique<Babylon::Native>(*jsiRuntime, currentBridge.jsCallInvoker, (__bridge void*)mtkView, width, height);
151
- }
152
- }
113
+ case UITouchPhaseEnded:
114
+ case UITouchPhaseCancelled: {
115
+ NSUInteger pointerId = [activeTouches indexOfObject:touch];
116
+ if (pointerId != NSNotFound) {
117
+ [activeTouches replaceObjectAtIndex:pointerId withObject:[NSNull null]];
118
+ Babylon::SetTouchButtonState(static_cast<uint32_t>(pointerId), false, x, y);
119
+ }
120
+ break;
121
+ }
153
122
 
154
- auto initializationPromisesIterator = initializationPromises.find((__bridge void*)currentBridge);
155
- if (initializationPromisesIterator != initializationPromises.end()) {
156
- for (RCTPromiseResolveBlock resolve : initializationPromisesIterator->second) {
157
- resolve([NSNumber numberWithUnsignedLong:reinterpret_cast<uintptr_t>(currentNativeInstance.get())]);
123
+ default:
124
+ break;
125
+ }
158
126
  }
159
-
160
- initializationPromises.erase(initializationPromisesIterator);
161
127
  }
162
128
  }
163
129
 
164
- // NOTE: This happens during dev mode reload, when the JS engine is being shutdown and restarted.
165
- + (void)onBridgeWillInvalidate:(NSNotification*)notification
166
- {
167
- currentNativeInstance.reset();
168
- }
169
-
170
130
  @end
@@ -2,6 +2,7 @@
2
2
 
3
3
  #import <React/RCTViewManager.h>
4
4
  #import <React/RCTUIManager.h>
5
+ #import <ReactCommon/CallInvoker.h>
5
6
 
6
7
  #import <Foundation/Foundation.h>
7
8
  #import <UIKit/UIKit.h>
@@ -14,14 +15,13 @@
14
15
  @end
15
16
 
16
17
  @implementation EngineView {
17
- RCTBridge* bridge;
18
- NSRunLoop* runLoop;
18
+ const RCTBridge* bridge;
19
+ MTKView* xrView;
19
20
  }
20
21
 
21
- - (instancetype)init:(RCTBridge*)_bridge runLoop:(NSRunLoop*)_runLoop {
22
+ - (instancetype)init:(RCTBridge*)_bridge {
22
23
  if (self = [super initWithFrame:CGRectZero device:MTLCreateSystemDefaultDevice()]) {
23
24
  bridge = _bridge;
24
- runLoop = _runLoop;
25
25
 
26
26
  super.translatesAutoresizingMaskIntoConstraints = false;
27
27
  super.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
@@ -32,23 +32,45 @@
32
32
 
33
33
  - (void)setBounds:(CGRect)bounds {
34
34
  [super setBounds:bounds];
35
- [BabylonNativeInterop setView:bridge jsRunLoop:runLoop mktView:self];
35
+ [BabylonNativeInterop updateView:self];
36
36
  }
37
37
 
38
38
  - (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
39
- [BabylonNativeInterop reportTouchEvent:touches withEvent:event];
39
+ [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
40
40
  }
41
41
 
42
42
  - (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
43
- [BabylonNativeInterop reportTouchEvent:touches withEvent:event];
43
+ [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
44
44
  }
45
45
 
46
46
  - (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
47
- [BabylonNativeInterop reportTouchEvent:touches withEvent:event];
47
+ [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
48
48
  }
49
49
 
50
50
  - (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
51
- [BabylonNativeInterop reportTouchEvent:touches withEvent:event];
51
+ [BabylonNativeInterop reportTouchEvent:self touches:touches event:event];
52
+ }
53
+
54
+ - (void)drawRect:(CGRect)rect {
55
+ if ([BabylonNativeInterop isXRActive]) {
56
+ if (!xrView) {
57
+ xrView = [[MTKView alloc] initWithFrame:self.bounds device:self.device];
58
+ xrView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
59
+ xrView.userInteractionEnabled = false;
60
+ [self addSubview:xrView];
61
+ [BabylonNativeInterop updateXRView:xrView];
62
+ }
63
+ } else if (xrView) {
64
+ [BabylonNativeInterop updateXRView:nil];
65
+ [xrView removeFromSuperview];
66
+ xrView = nil;
67
+ }
68
+
69
+ [BabylonNativeInterop renderView];
70
+ }
71
+
72
+ -(void)dealloc {
73
+ [BabylonNativeInterop updateXRView:nil];
52
74
  }
53
75
 
54
76
  - (void)takeSnapshot {
@@ -63,7 +85,7 @@
63
85
  // Grab the image from the graphics context, and convert into a base64 encoded JPG.
64
86
  UIImage* capturedImage = UIGraphicsGetImageFromCurrentImageContext();
65
87
  UIGraphicsEndImageContext();
66
- NSData* jpgData = UIImageJPEGRepresentation(capturedImage, 1.0f);
88
+ NSData* jpgData = UIImageJPEGRepresentation(capturedImage, .8f);
67
89
  NSString* encodedData = [jpgData base64EncodedStringWithOptions:0];
68
90
 
69
91
  // Fire the onSnapshotDataReturned event if hooked up.
@@ -79,9 +101,7 @@
79
101
  @interface EngineViewManager : RCTViewManager
80
102
  @end
81
103
 
82
- @implementation EngineViewManager {
83
- NSRunLoop* runLoop;
84
- }
104
+ @implementation EngineViewManager
85
105
 
86
106
  RCT_EXPORT_MODULE(EngineViewManager)
87
107
 
@@ -98,13 +118,8 @@ RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber*) reactTag) {
98
118
  }];
99
119
  }
100
120
 
101
- RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(setJSThread) {
102
- runLoop = [NSRunLoop currentRunLoop];
103
- return nil;
104
- }
105
-
106
121
  - (UIView*)view {
107
- return [[EngineView alloc] init:self.bridge runLoop:runLoop];
122
+ return [[EngineView alloc] init:self.bridge];
108
123
  }
109
124
 
110
125
  @end