rt.onBeforeRenderObservable.add(() => {
// Apply custom material before rendering
for (let mesh of rt.renderList) {
mesh._savedMaterial = mesh.material; // Save the original material
mesh.material = helperMaterial; // Apply the helper material or any material
}
});
This is a side effect of this PR, which added reference counting for the Effect
class, which automatically deletes an instance when it is not referenced anymore.
When you do rt.renderList[index]._savedMaterial = rt.renderList[index].material
, because of the new behavior, you should also refcount the effects associated to rt.renderList[index].material
used by the mesh.
It’s not that easy. The two simplest solutions are:
- sets
BABYLON.Effect.PersistentMode = true;
at the beginning of your program. This way, it retains the old behavior where the effects were never deleted => https://playground.babylonjs.com/#71DKKJ#86 - uses this more performant way of setting a specific material for a mesh/a list of meshes rendered by a
RenderTargetTexture
:
rt.setMaterialForRendering(rt.renderList, helperMaterial);
=> https://playground.babylonjs.com/#71DKKJ#87
By doing this, you can get rid of the code that adds observers to rt.onBeforeRenderObservable
and rt.onAfterRenderObservable
, and you don’t incur a performance penalty on every frame because you no longer modify the mesh.material
property.
2 Likes