Texture creation is slow on huge scenes

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.

Hello and welcome!

You can block the dirty mechanism with
‘scene. blockMaterialDirtyMechanism = false’

Do not forget to restore it to true at the end to update the system

2 Likes

@Deltakosh just beats me to it! However, it’s the other way around, set it to true to block the dirty mechanism, and reset it to false to re-enable it.

1 Like

You are right, i have to set it to true to block the behavior

It works perfectly but as @Evgeni_Popov pointed out i have to set the value to true for behavior to got blocked.

Yes sorry I was on my phone :slight_smile: