Skip to content

Commit ca33c3f

Browse files
authored
Merge pull request #7 from simonedevit/6-finalize-custom-loading-screen-and-multiple-canvases
Finalize custom loading screen + implement multiple canvases with single scene
2 parents 68cb7b5 + 922ed47 commit ca33c3f

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

packages/library/src/core/Scene.tsx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { SceneContext, EngineContextType } from './hooks';
66
import { RootContainer } from '@types';
77
import Reactylon from '../reconciler';
88
import { type ContextBridge, useContextBridge } from 'its-fine';
9+
import { type CameraProps } from '@props';
910

1011
type SceneProps = React.PropsWithChildren<{
1112
/**
@@ -58,15 +59,15 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea
5859
scene.enablePhysics(
5960
physicsOptions?.gravity || new Vector3(0, -9.8, 0),
6061
physicsOptions?.plugin ||
61-
new HavokPlugin(
62-
true,
63-
await HavokPhysics({
64-
// TODO: serve .wasm file from your own server
65-
locateFile: file => {
66-
return `https://preview.babylonjs.com/havok/${file}`;
67-
},
68-
}),
69-
),
62+
new HavokPlugin(
63+
true,
64+
await HavokPhysics({
65+
// TODO: serve .wasm file from your own server
66+
locateFile: file => {
67+
return `https://preview.babylonjs.com/havok/${file}`;
68+
},
69+
}),
70+
),
7071
);
7172
}
7273
let xrExperience = null;
@@ -104,8 +105,20 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea
104105
}
105106
};
106107
} else {
107-
//TODO: implement logic for multiple canvases but single scene (for each camera -> engine.registerView(canvas, camera))
108-
//need linking camera and canvas (what about creating a new prop called "canvas" for camera elements?)
108+
scene.onNewCameraAddedObservable.add((camera) => {
109+
const augmentedCamera = camera as Camera & CameraProps;
110+
const canvas = augmentedCamera.canvas!;
111+
engine.registerView(canvas, camera);
112+
113+
canvas.onclick = () => {
114+
if (scene.activeCamera !== camera) {
115+
scene.activeCamera?.detachControl();
116+
engine.inputElement = canvas;
117+
scene.activeCamera = camera;
118+
camera.attachControl();
119+
}
120+
}
121+
})
109122
}
110123
}
111124

packages/library/src/web/CustomLoadingScreen.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CustomLoadingScreen implements ILoadingScreen {
1313
* @param renderingCanvas defines the canvas used to render the scene
1414
*/
1515
constructor(renderingCanvas: HTMLCanvasElement, Loader: React.FC) {
16+
this._resizeLoadingUI = this._resizeLoadingUI.bind(this);
1617
this._renderingCanvas = renderingCanvas;
1718
/* if (this._loadingDiv) {
1819
// Do not add a loading screen if there is already one
@@ -34,8 +35,10 @@ class CustomLoadingScreen implements ILoadingScreen {
3435
return;
3536
}
3637
this._loadingDiv.style.position = canvasPositioning === 'fixed' ? 'fixed' : 'absolute';
37-
this._loadingDiv.style.left = canvasRect.left + 'px';
38-
this._loadingDiv.style.top = canvasRect.top + 'px';
38+
const scrollLeft = document.documentElement.scrollLeft;
39+
const scrollTop = document.documentElement.scrollTop;
40+
this._loadingDiv.style.left = canvasRect.left + scrollLeft + 'px';
41+
this._loadingDiv.style.top = canvasRect.top + scrollTop + 'px';
3942
this._loadingDiv.style.width = canvasRect.width + 'px';
4043
this._loadingDiv.style.height = canvasRect.height + 'px';
4144
}
@@ -52,7 +55,7 @@ class CustomLoadingScreen implements ILoadingScreen {
5255
/**
5356
* Function called to hide the loading screen
5457
*/
55-
public hideLoadingUI() {
58+
hideLoadingUI() {
5659
window.removeEventListener('resize', this._resizeLoadingUI);
5760
this._loadingDiv.style.display = 'none';
5861
}

0 commit comments

Comments
 (0)