I’m working on using a custom shader to perform certain computation on an RTT and then read the RTT to see the result of the computation.
I understand that I basically have to wait for the shader to compile before the mesh is drawn on the RTT.
The problem is that I have a problem with the method to wait for all the resources needed for rendering. If I read RTT into readPixels on the initial run, I get a byte array of just zeros.
This seems to be some kind of complex timing issue - it only reproduces in PG, and even then, once it happens, it doesn’t reproduce on page reload. (I think the browser is caching the compiled shaders).
Follow the link to see the full code that uses RTT.
I’ve read through the relevant Babylon.js sources several times, but I can’t figure out what’s causing this and need help. Thank you.
This response leaves out the context of babylon.js and my code.
looks like it was generated by an LLM.
Try to use scene.executeWhenReady(callback, true)
instead and see if that helps. You said it reproduces in a PG: are you able to provide a link? It would be much easier to help.
Babylon.js Playground
Here’s a organized PG.
For a little context, i use RTT to determine the rendering mode of a mesh
The TextureAlphaChecker
takes a mesh and a diffuse texture and renders the geometry once in UV space.
And when you read the result, you can see that if the alpha value is not 255, then the mesh should be drawn as transparent.
Here’s the result when PG is doing something wrong (it doesn’t seem to reproduce well in Firefox).
And here’s the result when PG works well.
Each time you press the Run button, it will randomly give you one of two outcomes
it will often work well, and a very high chance it will work badly the first time.
Here’s a better reproduction. This is also reproduced well in Firefox.
If you load the same mesh twice, and the first time you load it, RTT rendering will always fail. The second mesh’s evaluation will succeed probabilistically.
Babylon.js Playground
Babylon.js Playground
Here is a test that saves the RTT result as a png to make it debuggable. Note that it overwrites the rtt render code part of the library.
Sorry for sending so many PGs. This is the best one to analyze.
The root mesh is disabled when RTT.isReadyForRendering
is called, so the material of the “q302” mesh won’t be tested for readiness. You should move the code lines 42-45 before the while
loop (though I don’t know why you set these flags by hand, you could call setEnabled
on the mesh / parent mesh?):
1 Like
Maybe this was too simple a question to ask in the forum, I apologize for my laziness.
(though I don’t know why you set these flags by hand, you could call setEnabled
on the mesh / parent mesh?):
Like the GLTF loader, my loader basically creates a single root node, disables it, creates meshes and materials asynchronously and adds them to the child.
Then, when all resources are loaded, it enables the root node.
The reason for this is that rendering an incomplete model that is not yet fully loaded can cause more shaders to be compiled than are actually used in the model, and can also slow down the loading process.
During this process, I use RTT(TextureAlphaChecker) to determine the rendering mode of the material when building the material.
The pain is that Babylon.js is not friendly to one-time renders like this.
I run this rendering mode evaluation only once for every mesh, but it creates GPU resources like VAOs that are cached and left unused.
The loader needs to make sure that resources are loaded with the least amount of resources possible given the wide range of device specs, so I use Babylon.js internal members to optimize for that.
1 Like