# Stop materials from unnecessary duplicating

**URL:** <https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860>\
**Category:** Questions\
**Created:** [May 17, 2021, 12:53pm UTC](https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860 "2021-05-17T12:53:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Regit](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@Regit](https://forum.babylonjs.com/u/Regit)\
**Post date:** [May 17, 2021, 12:53pm UTC](https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860/1 "2021-05-17T12:53:18Z")

</div>

Hi,

I doubt it’s too impactful on performance, but everytime I’m building a Point Cloud System a new point cloud material gets added to the scene. I don’t think this is necessary, since I’m using pretty much the same properties everytime I build it.

I also noticed that everytime I create a line a material is added. They don’t need a material each, is there a way to have them share a material instance?

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [May 17, 2021, 1:48pm UTC](https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860/2 "2021-05-17T13:48:40Z")

</div>

Right now, I have the same sort of issue importing a mesh with created and assigned in BJS materials (pbr, std, multimat). Each time I dispose of a mesh and next reimport it assigning the same material, I get a duplicate (of the material and texture). I don’t know if it only shows as such in the inspector (because of the delta or anything) or if it can eventually impact performance. Now, I’m pretty sure this topic already had its answer somewhere in this forum, but it’s faster and easier for me to just soak up the input that will come for your case 😉 Following…

Edit: @shaderbytes , sry just found this post of yours:

> [@How to avoid duplicate materials with multi asset loading](https://forum.babylonjs.com/t/how-to-avoid-duplicate-materials-with-multi-asset-loading/10748):
>
> Hi All So I have a scene that loads many different assets into it. Many assets have same named materials and it is meant to be this way. So if I change the material texture , it shouldd update all assets using that material. Of coarse I noticed this was not happening and so logged the materials array of the scene and noticed many instances of the said single material , instead of one. So my question is how do I load an asset and when it loads it does not create a duplicate material , but rath…

Do you have any further input/advise on this? Thanks in advance. Cheers,

---

<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 17, 2021, 2:19pm UTC](https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860/3 "2021-05-17T14:19:47Z")

</div>

Here’s a PR that will let you reuse an existing material for `LinesMesh` and `PointsCloudSystem`:

> <https://github.com/BabylonJS/Babylon.js/pull/10379>
>
> See https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20…860

There’s a new `material` option in the `LinesMesh` constructor and you can pass a material when calling `PointsCloudSystem.buildMeshAsync()`.

---

<div class="post-metadata">

**Author:** ![shaderbytes](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/shaderbytes/32/174_2.png) [@shaderbytes](https://forum.babylonjs.com/u/shaderbytes)\
**Post date:** [May 17, 2021, 6:16pm UTC](https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860/4 "2021-05-17T18:16:09Z")

</div>

> [@mawa](#):
>
> Do you have any further input/advise on this? Thanks in advance. Cheers,

Hey there!

In that thread I did post a code solution that made sure the scene always only had one version of a same named material. Basically on every asset load I run this function , the function in a nut shell :

• all materials of loaded asset are evaluated  
• if a material with name x is not found in cache , it is cached.  
• if a material with name x is found , The duplicate is flagged for deletion and replaced with the cached material  
• once recursion of nodes is complete all flagged duplicates are deleted properly.

I wrote this a while back and have not visited the code again. It is possible it could be optimized , refactored or done different. At the time it worked a treat 😉

Currently im working on a complex Laravel and VUE project and have not touched babylon development for months.

Some clever developer should think of making something with this logic part of the native engine code , I mean this kind of material duplication is a problem in other applications as well ( blender im looking at you , i have a python snippet to get rid of those as well )

cheers

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [May 19, 2021, 9:06am UTC](https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860/5 "2021-05-19T09:06:45Z")

</div>

Thanks a lot for your reply. I have saved your (old;) code snippet and will shortly try it. I believe it still works.  
Let me thank you for taking time to respond on the side of your current project. GL with it.

For others and for my understanding, I’d like to bounce on this

> [@shaderbytes](#):
>
> Some clever developer should think of making something with this logic part of the native engine code

@Deltakosh @Evgeni_Popov , any thoughts or later input on this?  
Thanks,

---

<div class="post-metadata">

**Author:** ![shaderbytes](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/shaderbytes/32/174_2.png) [@shaderbytes](https://forum.babylonjs.com/u/shaderbytes)\
**Post date:** [May 19, 2021, 1:08pm UTC](https://forum.babylonjs.com/t/stop-materials-from-unnecessary-duplicating/20860/6 "2021-05-19T13:08:52Z")

</div>

yeah it needs to be an optional argument when loading . so you can choose a behaviour type :

• allow duplicates  
• use if exists  
• replace

Make it an optional argument which defaults to the current behavior , this makes the change backward compatible , if you do nothing … you project will still just work as it always did.

Yet opens the door for two more scenarios , use from the existing materials , or replace the existing material

😉
