Stop materials from unnecessary duplicating

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?

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 :wink: Following…

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

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

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

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

1 Like

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 :wink:

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

2 Likes

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

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

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

:wink: