Hello,
I think problem comes from the fact that you are using multiple canvases :
- If your goal is just to create a split screen, then you can use 1 full screen canvas, 1 engine, and 4 scenes. Then you would use
camera.viewport
to render on only part of the canvas.
Example with 2 cameras :
const camera1 = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene1);
camera1.viewport = new BABYLON.Viewport(0, 0, 0.5, 1); // Left half
const camera2 = new BABYLON.FreeCamera("camera2", new BABYLON.Vector3(0, 5, 10), scene2);
camera2.viewport = new BABYLON.Viewport(0.5, 0, 0.5, 1); // Right half
scene1.activeCamera = camera1;
scene2.activeCamera = camera2;
engine.runRenderLoop(() => {
scene1.render();
scene2.render();
});
- Now if for some reason you absolutely need 4 canvas, I think that 1 engine is strictly linked to 1 canvas. I might be wrong, but to me the fact that the engine constructor takes the canvas as 1st parameter, is because it needs to bind to a WebGL (or WebGPU) context which is provided by the canvas itself.
So it means you would need to create 4 engines :
function createScene(engine, canvas) {
// Scene & Camera
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.ArcRotateCamera("camera", 0.25 * Math.PI, 1, 8, new BABYLON.Vector3(0, 0, 0), scene);
camera.attachControl(canvas, true);
// Light
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
// Ground & Sphere
var ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 6, height: 6 }, scene);
var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { diameter: 2, segments: 32 }, scene);
sphere.position.y = 1;
return scene;
}
// Create 4 scenes on 4 canvases
for (let i = 0; i < 4; i++) {
// Canvas
const canvas = document.getElementById("renderCanvas" + i);
// Engine
const engine = new BABYLON.Engine(canvas, true);
// Resize
window.addEventListener("resize", function () {
engine.resize();
});
// Scene
const scene = createScene(engine, canvas);
// Render loop
engine.runRenderLoop(function () {
scene.render();
});
}
And this is OK with attaching independently to mouse :
