Playground: https://playground.babylonjs.com/#71RRJM#0
When a streamed world changes the level of its textures, it swaps the images of many textures at once. Texture.updateURL does two things that cost us there:
- It releases the old image right away, so every mesh using the texture is not drawn until the new one has loaded.
- For each texture, it marks dirty every material of the scene that has it:
Scene.markAllMaterialsAsDirtywalks all the scene’s materials, so swapping N textures makes N × materialshasTexturecalls, and each of those materials is evaluated again.
What we do instead: load the new image in a separate Texture, and once it has loaded, point the old texture at its image, the way the engine’s texture cache shares one InternalTexture among textures (incrementReferences(), then dispose() on the old image). The material keeps the same texture, no material is marked dirty, and the meshes stay drawn with the old image until the new one is there.
The Playground swaps the textures of 600 materials both ways, in alternate rounds. Measured on 9.32.0 (Chromium 147, RTX 3080 Laptop GPU, Windows 11), the medians of 3 rounds, in two runs:
| Swap calls | hasTexture calls |
Frames with boxes missing | Frames until the new images show | |
|---|---|---|---|---|
updateURL, WebGPU |
161 and 179 ms | 360,000 | 2, with none of the 600 drawn | 3 |
| Pointed at the loaded image, WebGPU | 136 and 151 ms | 0 | 0 | 3 |
updateURL, WebGL2 |
96 and 89 ms | 360,000 | 0 | 1 |
| Pointed at the loaded image, WebGL2 | 52 and 49 ms | 0 | 0 | 1 |
The swap calls include starting the loads of the 600 images, both ways. On WebGL2, these small images load within a frame, so no box goes missing there.
In our world, this took the dirty marking from about 1 ms per frame to nothing and the 95th percentile of the frame time down by 16.5 %, and the hitch we had every half second went away.
The Playground does it with internals (_texture), which an application should not rely on. Would you welcome a public way to do it, for example an option of updateURL that keeps the current image until the new one has loaded and swaps it without marking the materials, or a method that points a texture at the image of another one? We can prepare the PR either way.
Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.