Skip to content

Commit 7620be2

Browse files
authored
Add workaround for xr transition rendering wonkyness (#211)
1 parent 60a4211 commit 7620be2

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Modules/@babylonjs/react-native/EngineHook.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,32 @@ class DOMException {
6464
}
6565
}
6666

67-
if (Platform.OS == "windows") {
68-
const originalEnterXRAsync: (...args: any[]) => Promise<any> = WebXRExperienceHelper.prototype.enterXRAsync;
69-
WebXRExperienceHelper.prototype.enterXRAsync = async function (...args: any[]): Promise<any> {
67+
if (Platform.OS === "android" || Platform.OS === "ios") {
68+
const originalEnterXRAsync: (...args: any[]) => Promise<WebXRSessionManager> = WebXRExperienceHelper.prototype.enterXRAsync;
69+
WebXRExperienceHelper.prototype.enterXRAsync = async function (...args: any[]): Promise<WebXRSessionManager> {
70+
// TODO: https://github.com/BabylonJS/BabylonNative/issues/649
71+
// Android/iOS require manually clearing the default frame buffer to prevent garbage from being rendered for a few frames during the XR transition
72+
const sessionManager = await originalEnterXRAsync.apply(this, args);
73+
const scene = sessionManager.scene;
74+
const beforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
75+
scene.getEngine().unBindFramebuffer(undefined!);
76+
scene.getEngine().clear(scene.clearColor, true, false);
77+
});
78+
sessionManager.onXRSessionEnded.add(() => {
79+
scene.onBeforeRenderObservable.remove(beforeRenderObserver);
80+
});
81+
return sessionManager;
82+
};
83+
} else if (Platform.OS === "windows") {
84+
const originalEnterXRAsync: (...args: any[]) => Promise<WebXRSessionManager> = WebXRExperienceHelper.prototype.enterXRAsync;
85+
WebXRExperienceHelper.prototype.enterXRAsync = async function (...args: any[]): Promise<WebXRSessionManager> {
7086
// TODO: https://github.com/BabylonJS/BabylonNative/issues/577
7187
// Windows HMDs require different rendering behaviors than default xr rendering for mobile devices
7288
const sessionManager = await originalEnterXRAsync.apply(this, args);
7389
sessionManager.scene.clearColor = Color3.Black().toColor4();
7490
sessionManager.scene.autoClear = true;
7591
return sessionManager;
76-
}
92+
};
7793
}
7894

7995
// 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.

0 commit comments

Comments
 (0)