Multi scene sharing object (meshes, materials)

I work with Babylon 4.0.0-alpha.19
I’m trying to duplicate objects between multiple scenes. I use clone on the mesh of scene 1 and add it to scene 2. I made several attempts often materials do not move from one scene to another. Now I have a WebGL error GL ERROR: GL_INVALID_OPERATION: glDrawElements: uniform buffer: no buffer bound at index 1. Code example:

_canvas = document.getElementById("renderCanvas") as HTMLCanvasElement;
_engine = new BABYLON.Engine(_canvas, true);
console.log(BABYLON.Engine.Version);
_scene1 = new BABYLON.Scene(_engine);
_scene2 = new BABYLON.Scene(_engine);
// Scene 1    
let _camera1 = new BABYLON.UniversalCamera("UniversalCamera", new BABYLON.Vector3(0, 5, -10), _scene1); // need camera unless error of global position
_camera1.setTarget(BABYLON.Vector3.Zero());
// _camera1.attachControl(_canvas, false);
new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), _scene1);
let sphere = BABYLON.MeshBuilder.CreateSphere('sphere',
    { segments: 16, diameter: 2 }, _scene1);
let texture = new BABYLON.Texture("3DTheater/sand.jpg", _scene1);
let sphereMaterial = new BABYLON.StandardMaterial("sphereMaterial", _scene1);
sphereMaterial.diffuseColor = new BABYLON.Color3(1, 0, 0);
sphereMaterial.diffuseTexture = texture;
sphere.material = sphereMaterial;
sphere.position.y = 1;
let ground = BABYLON.MeshBuilder.CreateGround('ground', { width: 6, height: 6, subdivisions: 2 }, _scene1);
// Scene 2
let _camera2 = new BABYLON.UniversalCamera("UniversalCamera", new BABYLON.Vector3(0, 5, -10), _scene2);
_camera2.setTarget(BABYLON.Vector3.Zero());
_camera2.attachControl(_canvas, false);
let _light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), _scene2);
let sphere2 = sphere.clone("sphere2");
sphere2._scene = _scene2;
_scene2.addMesh(sphere2);
_scene2.addMaterial(sphere2.material);
_engine.runRenderLoop(() => {
        _scene2.render();
    });

Is it possible with BabylonJs to do that? Do I forget something?

PS: If there is no camera in Scene 1, an error is raised :

babylon.custom.js:16 Uncaught TypeError: Cannot read property 'globalPosition' of undefined
at Function.e.BindEyePosition (babylon.custom.js:16)...

Unfortunately in this case this is not possible. Scenes are top level containers used to isolate resources.

You have them multiple options:

1 Like

Thank you for the answer.
I suspected that it was not possible :slight_smile:
I will use something similare to AssetContainer.

1 Like