Objects are not rendered the frame they are created

Objects seem to not be rendered on the frame they are created on if it has a fresh material, even if that material is identical to another material (not cached?), and even if async shader compilation is off.

I’ve created three test cases showing unexpected/undesirable behavior. For every demo, pressing the space key will recreate the object and demo the issue. The code is nearly identical for all 3 examples, only lines 3 and 23 are changed.

  1. Async shader compilation: disabled
    New object’s material: cloned
    Result: flash due to object not being rendered on the first frame (unexpected behavior)
    Babylon.js Playground
  2. Async shader compilation: disabled
    New object’s material: same material that already existed
    Result: no flash (this is the expected behavior)
    https://playground.babylonjs.com/#GQQGZQ#1
  3. Async shader compilation: enabled
    New object’s material: same material that already existed
    Result: flash due to object not being rendered on the first frame (unexpected behavior)
    https://playground.babylonjs.com/#GQQGZQ#2

I would expect that in all three cases, there should be no flash due to the object not being rendered on the frame it is created. I believe the current flash behavior used to not be present, as I encountered this by upgrading a very old project from Babylon.js v6.16.0 to v9.18.2, and there was a visual issue that I noticed. I think the reproduction I have here is a minimal reproduction of the issue I was seeing, but I’m not 100% sure.

Interesting find :slight_smile:

I managed to pinpoint the PR that made the change - Weak ref by deltakosh · Pull Request #15863 · BabylonJS/Babylon.js · GitHub

7.35.2 was the first version that had it. the short explanation is that this PR added this.effect.dispose() to DrawWrapper.dispose().

I am exploring a fix for that, let’s see if I can find something that doesn’t break rendering.

Please try this snapshot - https://playground.babylonjs.com/?snapshot=refs/pull/18755/merge#GQQGZQ#2

It comes from this PR - Fix one-frame render flash when recreating a mesh in the same tick by RaananW · Pull Request #18755 · BabylonJS/Babylon.js · GitHub

As far as I can tell, that looks like it works. Thanks for helping look into this!

I found another case where the flash occurs. Demo here:

Babylon.js Playground - #GQQGZQ#3

The difference being that a texture is assigned to the newly cloned material.