createScene, manual invocation of engine loop, bug hunt

I’ve got some strange bugs such as Engine setGlobalVolume not being invokable immediately after creating the engine as if the engine constructor or audio initialization was async.

I also have meshes disappearing when I alter them in scene.registerBeforeRender(() => { }), for example this code https://playground.babylonjs.com/index.html#AWWZYP moves the tube in the pg but causes the tube to disappear in my game. I can still console log the path vectors, but the there’s no tube being rendered in-game.

I can’t really reproduce either of those in the pg, which is perhaps a hint, though I’m not sure…

Between those two bugs, and the fact that all that works fine if I start fresh with Babylon.js Documentation 's HTML template, I suspect I may be doing some very basic thing incorrectly.

So here are some basic questions:

Do I need a createScene function? I have the equivalent code, but no function by that exact name. Changing ‘var createScene’ to ‘let createScene’ in the PG throws an error… but maybe that’s just something special about the PG.

How do I manually invoke the engine loop? Is scene.render() and engine runRenderLoop(()=>{}) everything? I noticed that scene.render() was all I needed until I added water reflections, which it didn’t render (fixed via runRenderLoop).

Does babylon have a global reference to scene? While looking for bugs I found a class where ‘this.scene’ was undefined, and yet all of the meshes created with that undefined scene are visible. Maybe I’ve accidentally made two scenes.

Sounds like something in your code is messing with you :slight_smile:

Do I need a createScene function?

You don’t need a “createScene” function, they’re just nice.

How do I manually invoke the engine loop?

Scene.render() renders a single frame.
Engine runRenderLoop(()=>{}) uses window.requestAnimationFrame to find the optimal framerate for a given device / browser. (Typically capped at 60 fps)

Does babylon have a global reference to scene?

Engine have a engine.scenes array.

While looking for bugs I found a class where ‘this.scene’ was undefined, and yet all of the meshes created with that undefined scene are visible. Maybe I’ve accidentally made two scenes.

Again it sounds like something in your code is messing with you, could be scope issue, could be reference has somehow been deleted, perhaps two scenes & reference was updated & then second scene deleted? Or maybe “this” just isn’t what you think it is.
Either way, when the scene variable is undefined when creating new meshes, the last scene created is used.
Hard to tell :slight_smile:

1 Like

I have finally found the disappearing mesh bug.

The bug appears to be that a camera cannot be moved via position/rotation without a call to setTarget – everything looks fine initially, but altering any ‘updatable’ meshes will make them invisible It can be reproduced by going to my PG above, setting version to stable, and commenting out ‘camera.setTarget’.

I think it’s fixed in the 4.0 preview. That said I can’t fully use the preview yet b/c i’m reliant on null engine. Does anyone happen to know how to get the 4.0-ish camera behavior in 3.3?

We will find why you are blocked with 4.0:slightly_smiling_face:

I answered on your other topic