jangbi

create mesh before render in babylon.js

Recommended Posts

I want to create new mesh before each render, here's my code:

let snake = [
    sphere1,
    sphere2,
    sphere3
];

(function(){
    let counter = 4;
    scene.registerBeforeRender(function(){
        let newOne = BABYLON.MeshBuilder.CreateSphere(
            "sphere" + counter,
            {

            },
            scene
        );
        let head = snake[0];
        newOne.position = head.position;
        newOne.position.x += 0.02;
        snake.unshift(newOne);
        ++counter;
    });
})();

My expecting behavior is to create a series of spheres, each position.x is slightly higher than the previous one. However, there are only three meshes in the scene after rendering, like this:

enter image description here

I want to know what is wrong with my code, and how to implement it properly?

By the way, what is the difference between scene.removeMesh(mesh) and mesh.dispose()?

Here's the relative link in stackoverflow:

https://stackoverflow.com/questions/50235364/create-mesh-before-render-in-babylon-js

Share this post


Link to post
Share on other sites

scene.removeMesh will just remove the mesh from the scene but the mesh still exists

with dispose, the mesh will be disposed AND removed from the scene

 

In your case I suggest this slightly change:

newOne.position = head.position.clone();

 

Share this post


Link to post
Share on other sites

@Deltakosh Thanks for your answer, but as for my first question, I expected that will produce a series of spheres, but there're only three spheres existed all the time, I want to know what's wrong with my implementation.

Share this post


Link to post
Share on other sites

You should provide a repro in the PG: it helps a lot to get a fast answer :)

Just by running your code I think it will crash after 3 tries as the snake array will be empty so on the 4th call snake[0] will be undefined and then it will crash. You should definitely check your console to see if there is an exception

Share this post


Link to post
Share on other sites
    let canvas,
        engine,
        camera,
        scene;

    function initEngine(){
        canvas = document.getElementById("renderCanvas");
        engine = new BABYLON.Engine(canvas, true);
    }

    function createScene(){
        initEngine();

        let scene = new BABYLON.Scene(engine);

        camera = new BABYLON.ArcRotateCamera("camera", Math.PI / 2, Math.PI / 2, 4, BABYLON.Vector3.Zero(), scene);
        camera.attachControl(canvas, true);
        

        let light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0,1,0), scene);

        let ground = BABYLON.MeshBuilder.CreateGround(
            "ground",
            {
                width:30,
                height:30
            },
            scene
        );

        ground.position.y -= 0.5;

        let sphere1 = BABYLON.MeshBuilder.CreateSphere(
            "sphere1",
            {

            },
            scene
        );

        let sphere2 = sphere1.clone("sphere2");
        let sphere3 = sphere1.clone("sphere3");

        sphere1.position.z = -18;
        sphere2.position.z = -19;
        sphere3.position.z = -20;

        let snake = [
            sphere1,
            sphere2,
            sphere3
        ];

        (function(){
            let counter = 4;
            scene.registerBeforeRender(function(){
                let newOne = BABYLON.MeshBuilder.CreateSphere(
                    "sphere" + counter,
                    {

                    },
                    scene
                );
                let head = snake[0];
                newOne.position = head.position;
                newOne.position.x += 0.02;
                snake.unshift(newOne);
                ++counter;
            });
        })();

        window.addEventListener("resize", function(){
            engine.resize();
        });

        return scene;
    }

    scene = createScene();
    
    engine.runRenderLoop(function(){
        // box.position.z += 0.01;
        scene.render();
    });

@DeltakoshHere's the minimal demo.

Share this post


Link to post
Share on other sites

I tried to start putting debugging statements into that playground example, but it runs so poorly, it keeps crashing my browser.

Share this post


Link to post
Share on other sites

mmmhh.. I didn't test your PG but it looks like you create a new mesh each frame with no stop condition. This means, every 16 ms, a new instance is allocated in memory. This would crash very quickly any browser imho.

Share this post


Link to post
Share on other sites

@jerome @Magilla I know what you guys mean. But my point is, my expecting code behavior is to generate a series of spheres instead of just 3. And if you add snake.pop().dispose() before each render, you will see there's only one sphere, not three.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.