Skip to content

Commit 61a3a8f

Browse files
authored
Update to latest Babylon JS/Native and fix iOS flushedQueue issue (#92)
Directly calling flushedQueue on iOS has some side effects that I didn't notice in my first test. With some UI transitions, the internal UI state becomes broken. I'm not sure what is going on, but see the comments in the code for more details. We have an active thread on Discord with some Facebook React Native folks, so we'll wait and see if they have a better solution. I also removed the call to reinitialize graphics right after initialization on Android as this no longer seems to be an issue. Finally, I'm updating both Babylon.js and Babylon Native to the latest. This brings in Babylon.js changes that expose the XR tracking state, and also bring in the Babylon Native XR render loop fix.
1 parent 694f3b3 commit 61a3a8f

File tree

9 files changed

+36
-27
lines changed

9 files changed

+36
-27
lines changed

Apps/PackageTest/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Apps/PackageTest/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
1111
},
1212
"dependencies": {
13-
"@babylonjs/core": "^4.2.0-beta.6",
13+
"@babylonjs/core": "^4.2.0-beta.10",
1414
"@babylonjs/react-native": "file:../../Package/Assembled/babylonjs-react-native-0.0.1.tgz",
1515
"react": "16.13.1",
1616
"react-native": "0.63.1",

Apps/Playground/package-lock.json

+11-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Apps/Playground/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
1111
},
1212
"dependencies": {
13-
"@babylonjs/core": "^4.2.0-beta.6",
14-
"@babylonjs/loaders": "^4.2.0-beta.6",
13+
"@babylonjs/core": "^4.2.0-beta.10",
14+
"@babylonjs/loaders": "^4.2.0-beta.10",
1515
"@babylonjs/react-native": "file:../../Modules/@babylonjs/react-native",
1616
"@react-native-community/slider": "^2.0.9",
1717
"logkitty": "^0.7.1",

Modules/@babylonjs/react-native/android/src/main/cpp/BabylonNativeInterop.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ namespace Babylon
8080
Polyfills::Window::Initialize(m_env);
8181

8282
m_nativeInput = &Babylon::Plugins::NativeInput::CreateForJavaScript(m_env);
83-
84-
// TODO: This shouldn't be necessary, but for some reason results in a significant increase in frame rate. Need to figure this out.
85-
m_graphics->ReinitializeFromWindow<void*>(windowPtr, width, height);
8683
}
8784

8885
~Native()

Modules/@babylonjs/react-native/ios/BabylonNative.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ namespace Babylon
4848
struct DispatchData
4949
{
5050
arcana::run_loop_scheduler scheduler;
51-
Napi::FunctionReference flushedQueue;
51+
std::shared_ptr<facebook::jsi::Function> setTimeout;
5252

53-
DispatchData(Napi::Env env)
53+
DispatchData(facebook::jsi::Runtime& rt)
5454
: scheduler{ arcana::run_loop_scheduler::get_for_current_thread() }
55-
, flushedQueue{ GetFlushedQueue(env) }
55+
, setTimeout{ GetSetTimeout(rt) }
5656
{
5757
}
5858
};
5959

6060
JsRuntime::DispatchFunctionT dispatchFunction =
61-
[env = m_impl->env, data = std::make_shared<DispatchData>(m_impl->env)](std::function<void(Napi::Env)> func)
61+
[env = m_impl->env, data = std::make_shared<DispatchData>(*jsiRuntime)](std::function<void(Napi::Env)> func)
6262
{
6363
(data->scheduler)([env, func = std::move(func), &data]()
6464
{
6565
func(env);
66-
data->flushedQueue.Call({});
66+
data->setTimeout->call((static_cast<napi_env>(env))->rt, {});
6767
});
6868
};
6969

Modules/@babylonjs/react-native/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"base-64": "^0.1.0"
2828
},
2929
"peerDependencies": {
30-
"@babylonjs/core": "^4.2.0-beta.6",
30+
"@babylonjs/core": "^4.2.0-beta.10",
3131
"react": "^16.13.1",
3232
"react-native": "^0.63.1",
3333
"react-native-permissions": "^2.1.4"

Modules/@babylonjs/react-native/shared/Shared.h

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <napi/napi.h>
4+
#include <jsi/jsi.h>
45

56
// See https://github.com/BabylonJS/BabylonReactNative/issues/60 for original issue.
67
// This is a work around to poke the React Native message queue to run setImmediate callbacks.
@@ -16,3 +17,14 @@ inline Napi::FunctionReference GetFlushedQueue(Napi::Env env)
1617
auto flushedQueue{ batchedBridge.Get("flushedQueue").As<Napi::Function>() };
1718
return Napi::Persistent(flushedQueue);
1819
}
20+
21+
// On iOS, directly calling flushedQueue breaks some kind of internal UI state and we start getting errors like:
22+
// *** Assertion failure in -[RCTNativeAnimatedNodesManager disconnectAnimatedNodes:childTag:](), node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m:138
23+
// [native] Exception thrown while executing UI block: 'parentNode' is a required parameter
24+
// However, calling setTimeout eventually also calls flushedQueue, but doesn't result in the above error,
25+
// so we'll go this route until we (hopefully) get a better resolution from the React Native team at Facebook.
26+
inline std::shared_ptr<facebook::jsi::Function> GetSetTimeout(facebook::jsi::Runtime& rt)
27+
{
28+
auto code{std::make_shared<const facebook::jsi::StringBuffer>("() => setTimeout(() => {})")};
29+
return std::make_shared<facebook::jsi::Function>(rt.evaluateJavaScript(std::move(code), "").asObject(rt).asFunction(rt));
30+
}

0 commit comments

Comments
 (0)