Loading files
// https://doc.babylonjs.com/communityExtensions/Babylon.js+ExternalLibraries/BabylonJS_and_ReactJS
import { Engine, Scene } from '@babylonjs/core';
import { useEffect, useRef } from 'react';
export default function BabylonJS({
antialias,
engineOptions,
adaptToDeviceRatio,
sceneOptions,
onRender,
onSceneReady,
...rest
}: any) {
const reactCanvas = useRef(null);
// set up basic engine and scene
useEffect(() => {
const { current: canvas } = reactCanvas;
if (!canvas) return;
const engine = new Engine(canvas, antialias, engineOptions, adaptToDeviceRatio);
const scene = new Scene(engine, sceneOptions);
if (scene.isReady()) {
onSceneReady(scene);
} else {
scene.onReadyObservable.addOnce((scene) => onSceneReady(scene));
}
engine.runRenderLoop(() => {
if (typeof onRender === 'function') onRender(scene);
scene.render();
});
const resize = () => {
scene.getEngine().resize();
};
if (window) {
window.addEventListener('resize', resize);
window.addEventListener('any-panel-resize', resize);
}
return () => {
scene.getEngine().dispose();
if (window) {
window.removeEventListener('resize', resize);
window.removeEventListener('any-panel-resize', resize);
}
};
}, [antialias, engineOptions, adaptToDeviceRatio, sceneOptions, onRender, onSceneReady]);
return <canvas ref={reactCanvas} {...rest} />;
}
7e293118aThis file is pinned to the selected release. Download the original to open it in your design tool.