# Need to call resetDrawCache before setMaterialForRendering?

**URL:** <https://forum.babylonjs.com/t/need-to-call-resetdrawcache-before-setmaterialforrendering/30642>\
**Category:** Questions\
**Tags:** material\
**Created:** [May 23, 2022, 3:20pm UTC](https://forum.babylonjs.com/t/need-to-call-resetdrawcache-before-setmaterialforrendering/30642 "2022-05-23T15:20:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [May 23, 2022, 3:20pm UTC](https://forum.babylonjs.com/t/need-to-call-resetdrawcache-before-setmaterialforrendering/30642/1 "2022-05-23T15:20:18Z")

</div>

When calling setMaterialForRendering() on an effect, if changing e.g. from a StandardMaterial to a NodeMaterial (or vice versa) then a runtime error crashes the page…

And from checking the source and debugging I eventually found that a workaround is to first call resetDrawCache() on the mesh like below (comment this reset out on line 23 to see the error in the console).

So I wonder if setMaterialForRendering() should call resetDrawCache() on the mesh that we pass in for us to prevent this error? Or some other solution?

PS the node material in the playground is just the default saved from NME, so just sets the color to white. 🙂

> **[Babylon.js Playground](https://playground.babylonjs.com/#ANJPH0%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 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:** [May 23, 2022, 4:49pm UTC](https://forum.babylonjs.com/t/need-to-call-resetdrawcache-before-setmaterialforrendering/30642/2 "2022-05-23T16:49:12Z")

</div>

Yes, I think this call should be done. It had already been fixed in the glow layer when calling `referenceMeshToUseItsOwnMaterial` and `unReferenceMeshFromUsingItsOwnMaterial` for this very reason:

> <https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/Layers/glowLayer.ts#L580-L601>

Fancy doing a PR for that? (note that you should reset the draw cache only for `this._mainTexture.renderPassId`).

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [May 23, 2022, 5:09pm UTC](https://forum.babylonjs.com/t/need-to-call-resetdrawcache-before-setmaterialforrendering/30642/3 "2022-05-23T17:09:00Z")

</div>

OK here it is, I actually ended up adding it to setMaterialForRenderPass to make the fix more general. 🙂

> <https://github.com/BabylonJS/Babylon.js/pull/12561>
>
> Make setMaterialForRenderPass call resetDrawCache to prevent the error when swit…ching e.g. between StandardMaterial and NodeMaterial.
> 
> Forum: https://forum.babylonjs.com/t/need-to-call-resetdrawcache-before-setmaterialforrendering/30642
