Hey Babylon team,
The problem
I encountered an issue recently when creating a render texture per mesh on a huge scene (more or less 3000 meshes and 600 materials), and this is taking a considerable amount of time (1 minute on average).
I tracked down the problem and it seems like every time we create a new texture it assigns it a coordinates mode.
Babylon.js/src/Materials/Textures/renderTargetTexture.ts
Line 118 in 224af4f
public coordinatesMode = Texture.PROJECTION_MODE;
)
This assignment triggers this property’s setter which marks all materials as dirty.
Babylon.js/src/Materials/Textures/baseTexture.ts
Line 111 in 224af4f
public set coordinatesMode(value: number) {
So of course we have to assign coordinates mode a value, but i really need to get rid of the call to markAllMaterialsAsDirty
, as it is slow down things a lot.
Without this call everything works well in my case and it take on average 1 second to process all the mesh and create a render texture for each of them.
Repro
As you might expect i can’t bring you a repro of the complete scene so here is one with just spheres and a lot of materials created.
If you profile the scene you will see that most of the time is spend in markAllMaterialsAsDirty
.
Expectations
So i expect you to help me think of clean solution that would fix this.
For now i changed the _coordinatesMode
to a public property that is clearly a hot fix that must not figure in release code.