Creating a Mesh without adding to the scene

what kind of mesh you wanna make?
like new mesh or box …

Boxes, Cylinders and Spheres

i have way for make geometry without any webgl dependency and you quickly can use
and attach the mesh when you want in the any scene is that what you want?

I’m looking for something like this:

let plate = BABYLON.MeshBuilder.CreateBox("plate1", { width: 30, depth: 30,height: 5});
.
.
.
scene.addChild(plate);

https://www.babylonjs-playground.com/#2LEYZZ

look may be that help
i make the sphere sample for you too

https://www.babylonjs-playground.com/#2LEYZZ#1

How about this Babylon.js Playground

The current scene is used as the default if none is given. When working with multiple scenes it is necessary to specify which scene an object has to be placed in, for example https://www.babylonjs-playground.com/#L0IMUD#1

1 Like

Is it possible to prevent the automatic adding to the scene?

https://www.babylonjs-playground.com/#XKMWDG#1

3 Likes

Is there any difference to the MeshBuilder method except the adding to the scene?

export class MeshBuilder {
    public static CreateBox(name: string, options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, wrap?: boolean, topBaseAt?: number, bottomBaseAt?: number, updatable?: boolean }, scene: Nullable<Scene> = null): Mesh {
        return BoxBuilder.CreateBox(name, options, scene);
    }

export class BoxBuilder {
    public static CreateBox(name: string, options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, wrap?: boolean, topBaseAt?: number, bottomBaseAt?: number, updatable?: boolean }, scene: Nullable<Scene> = null): Mesh {
        var box = new Mesh(name, scene);
        options.sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
        box._originalBuilderSideOrientation = options.sideOrientation;
        var vertexData = VertexData.CreateBox(options);
        vertexData.applyToMesh(box, options.updatable);
        return box;
    }
}

No.

1 Like

Does that mean that using MeshBuilder I cannot prevent rendering the mesh? I want to use the mesh as template and clone it. To hide and completely remove it from the screen, I have a workaround right now by making it invisible by setting mesh.isVisible = false and set a very large value for coordinate: mesh.position.x = 9999

In following PG uncomment line 11 to see the difference.

On the first created box you could setEnabled(false) after all your clones where cloned, this way you would not have to enable each one you clone.

I have tried to use setEnabled as well, but seems like it will still be considered by the physic engine. Should I set the PhysicImpostor for all the clone instead of the original one(but I would prefer setting it once for the original one)?

You didn’t mention that you were using physics. Is there an issue with using the original plus clones rather than all clones? Perhaps @Cedric can help.

If you don’t create a physics impostor for a mesh, it won’t be physicalized.
So, I would suggest to clone a mesh, then create its impostor. Then, call setEnabled(false); to remove it from rendering… or dispose it if you won’t have to spawn any new clone.

1 Like

I guess I will create its impostor after cloning, and that will do the trick. Thanks!

1 Like

This seems to no longer be the solution as the method you’re calling is marked as deprecated.

The deprecated method says to use CreateBoxVertexData:

    var scenelessBox = new BABYLON.BoxBuidler.CreateBoxVertexData({width: 2, height: 1, depth: 3});
    var box = new BABYLON.Mesh("box");
    scenelessBox.applyToMesh(box);

Is this correct? Also, how do you then retroactively add a mesh to the scene? There doesn’t seem to be a scene.addChild method. Is it mesh.addParent(scene) ?

This mesh API doesn’t really make sense. Instantiating an object should not mutate the scene.

scene.addMesh and scene.removeMesh are responsible for it.

This change would be such a breaking change it is impossible to do now.

1 Like

The accepted answer seems not valid anymore : VertexData is deprecated, documentation explains we just have to call CreateBox directly. But when we do that, the playground does not work anymore : the box is added on the scene.

From a lambda user’s point of view, since the scene is an optional parameter, we expect something simple : the mesh is added to the scene only if the scene is given. I don’t really understand why it has to be more complicated.

As the message mentions, you can now use https://www.babylonjs-playground.com/#XKMWDG#23

About the second point I totally agree but it would break back compat a lot.