# Rendertarget changematerial not work in babylon 7 and 8

**URL:** <https://forum.babylonjs.com/t/rendertarget-changematerial-not-work-in-babylon-7-and-8/57827>\
**Category:** Questions\
**Tags:** material, render-target\
**Created:** [April 14, 2025, 2:40pm UTC](https://forum.babylonjs.com/t/rendertarget-changematerial-not-work-in-babylon-7-and-8/57827 "2025-04-14T14:40:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nasimiasl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nasimiasl/32/34_2.png) [@nasimiasl](https://forum.babylonjs.com/u/nasimiasl)\
**Post date:** [April 14, 2025, 2:40pm UTC](https://forum.babylonjs.com/t/rendertarget-changematerial-not-work-in-babylon-7-and-8/57827/1 "2025-04-14T14:40:53Z")

</div>

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  
}  
});

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#71DKKJ%2382)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 14, 2025, 2:59pm UTC](https://forum.babylonjs.com/t/rendertarget-changematerial-not-work-in-babylon-7-and-8/57827/2 "2025-04-14T14:59:19Z")

</div>

Assigned @Evgeni\_Popov on April 14, 2025, 2:59pm UTC



---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 14, 2025, 4:53pm UTC](https://forum.babylonjs.com/t/rendertarget-changematerial-not-work-in-babylon-7-and-8/57827/3 "2025-04-14T16:53:14Z")

</div>

This is a side effect of [this PR](https://github.com/BabylonJS/Babylon.js/pull/15863), 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](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`:

```javascript
rt.setMaterialForRendering(rt.renderList, helperMaterial);

```

=\> [https://playground.babylonjs.com/#71DKKJ#87](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.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 14, 2025, 4:53pm UTC](https://forum.babylonjs.com/t/rendertarget-changematerial-not-work-in-babylon-7-and-8/57827/4 "2025-04-14T16:53:21Z")

</div>

Unassigned @Evgeni\_Popov on April 14, 2025, 4:53pm UTC


