Hello wonderful BJS community,
I have project where the scene is made up of meshes which can change material properties dynamically at runtime (alpha & color + shader specific properties like scrolling uvs etc), often fading in or out, and we use a variety of shaders made via the NME that are implemented as NodeMaterials at runtime.
To get suitable rendering & alpha sorting order we’ve been swapping between 2 material variants (regular and fade) on each mesh, where if the object has a final alpha value less than 1, we switch to the fade material variant for the mesh. And we perform the opposite if the alpha value reaches 1/opaque.
Here’s an example of the material settings we have set differently between the regular and fade variants.
const transparentMaterial =
loadingFadeVariant ||
isFlowShader ||
shaderFilePath.includes('Glass')
if (transparentMaterial) {
nodeMaterial.transparencyMode = BABYLON.Material.MATERIAL_ALPHATESTANDBLEND
nodeMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE
nodeMaterial.needDepthPrePass = !isFlowShader
nodeMaterial.separateCullingPass = true
nodeMaterial.useAlphaFromDiffuseTexture = true
} else {
nodeMaterial.transparencyMode = BABYLON.Material.MATERIAL_OPAQUE
nodeMaterial.alphaMode = BABYLON.Engine.ALPHA_DISABLE
nodeMaterial.needDepthPrePass = false
nodeMaterial.separateCullingPass = false
nodeMaterial.disableDepthWrite = false
}
What we’re finding is that we suffer poor FPS drops whenever we trigger the dynamic changes to the scene and material swapping takes place, and then once everything is recalculated/changed the FPS speeds up again.
We already during a loading screen, call material.forceCompilationAsync(...)
on each material used in the scene to help avoid run-time stalls. Our meshes and material properties are fairly unique per use case so my understanding is that material and/or mesh instancing isn’t useful. As is material freezing because of the alpha/color property changes.
So my question is, for a fairly dynamic scene with fading in/out of meshes, and glass/transparency usage on various meshes, what would you recommend as the best approach to managing materials and scene state to achieve optimum performance out of BJS?