Skip to content

Commit

Permalink
Merge pull request #7 from simonedevit/6-finalize-custom-loading-scre…
Browse files Browse the repository at this point in the history
…en-and-multiple-canvases

Finalize custom loading screen + implement multiple canvases with single scene
  • Loading branch information
simonedevit authored Dec 27, 2024
2 parents 68cb7b5 + 922ed47 commit ca33c3f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
35 changes: 24 additions & 11 deletions packages/library/src/core/Scene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SceneContext, EngineContextType } from './hooks';
import { RootContainer } from '@types';
import Reactylon from '../reconciler';
import { type ContextBridge, useContextBridge } from 'its-fine';
import { type CameraProps } from '@props';

type SceneProps = React.PropsWithChildren<{
/**
Expand Down Expand Up @@ -58,15 +59,15 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea
scene.enablePhysics(
physicsOptions?.gravity || new Vector3(0, -9.8, 0),
physicsOptions?.plugin ||
new HavokPlugin(
true,
await HavokPhysics({
// TODO: serve .wasm file from your own server
locateFile: file => {
return `https://preview.babylonjs.com/havok/${file}`;
},
}),
),
new HavokPlugin(
true,
await HavokPhysics({
// TODO: serve .wasm file from your own server
locateFile: file => {
return `https://preview.babylonjs.com/havok/${file}`;
},
}),
),
);
}
let xrExperience = null;
Expand Down Expand Up @@ -104,8 +105,20 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea
}
};
} else {
//TODO: implement logic for multiple canvases but single scene (for each camera -> engine.registerView(canvas, camera))
//need linking camera and canvas (what about creating a new prop called "canvas" for camera elements?)
scene.onNewCameraAddedObservable.add((camera) => {
const augmentedCamera = camera as Camera & CameraProps;
const canvas = augmentedCamera.canvas!;
engine.registerView(canvas, camera);

canvas.onclick = () => {
if (scene.activeCamera !== camera) {
scene.activeCamera?.detachControl();
engine.inputElement = canvas;
scene.activeCamera = camera;
camera.attachControl();
}
}
})
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/library/src/web/CustomLoadingScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CustomLoadingScreen implements ILoadingScreen {
* @param renderingCanvas defines the canvas used to render the scene
*/
constructor(renderingCanvas: HTMLCanvasElement, Loader: React.FC) {
this._resizeLoadingUI = this._resizeLoadingUI.bind(this);
this._renderingCanvas = renderingCanvas;
/* if (this._loadingDiv) {
// Do not add a loading screen if there is already one
Expand All @@ -34,8 +35,10 @@ class CustomLoadingScreen implements ILoadingScreen {
return;
}
this._loadingDiv.style.position = canvasPositioning === 'fixed' ? 'fixed' : 'absolute';
this._loadingDiv.style.left = canvasRect.left + 'px';
this._loadingDiv.style.top = canvasRect.top + 'px';
const scrollLeft = document.documentElement.scrollLeft;
const scrollTop = document.documentElement.scrollTop;
this._loadingDiv.style.left = canvasRect.left + scrollLeft + 'px';
this._loadingDiv.style.top = canvasRect.top + scrollTop + 'px';
this._loadingDiv.style.width = canvasRect.width + 'px';
this._loadingDiv.style.height = canvasRect.height + 'px';
}
Expand All @@ -52,7 +55,7 @@ class CustomLoadingScreen implements ILoadingScreen {
/**
* Function called to hide the loading screen
*/
public hideLoadingUI() {
hideLoadingUI() {
window.removeEventListener('resize', this._resizeLoadingUI);
this._loadingDiv.style.display = 'none';
}
Expand Down

0 comments on commit ca33c3f

Please sign in to comment.