Jump to content

mergeMeshes Automatic Optimization


Rodrix3
 Share

Recommended Posts

Hi guys :)

I will be spending the next few hours (attempting to) creating a code snippet that optimizes meshes, by doing automatic merging.

My objective is to:

  • 1) Automatically identify meshes that share same materials, and merge them into a single mesh.
  • 2) Merge all meshes that don't share material into one single mesh all meshes, by using submeshes and multimaterial, automatically.  

The idea is to have an automatic optimization based I have spent a long time researching on forum and reading docs. However, if something like this has already been done
and I didn't find it.. please let me know ;)

Meanwhile.. I'll be coding and making tests the next hours :)

Link to comment
Share on other sites

I am done. Result => SUCCESS :)

@Deltakosh I think  you will enjoy this (and you may also share it with the guys working on Scene Optimizer, or optimization on Babylon, and tag them - I haven't met everyone yet - if you find this interesting ..  which I hope you do!).

I have created a function called optimizeSceneByMergingMeshesAutomatically(scene) which you can see at the top of the playground,
which analysis all the meshes on scene and automatically:

  • merges all meshes with the same material.
  • merges all merge of meshes with same material, into one big multi material mesh.

The result:

  • BEFORE: 152 meshes; 152 draw calls 
  • AFTER: 1 mesh; 4 draw calls

    ...with exactly the same render results. 

Playground and demo:  https://playground.babylonjs.com/#S7E1DT#1

My idea would be to be able to integrate this script into current functionality so others can use it; and have the community chip in and add any extra optimizations to my code. Hope you all like it!

I have been working the last two hours on this, so please let me all know what you think! :)

Link to comment
Share on other sites

25 minutes ago, Deltakosh said:

Thanks  @Deltakosh for the reply. 

 

Yes! I see you have something similar and that you and @Sebavan are the main contributors on the mesh merging optimization. Good! Too bad I missed it..
However, I also see that 
currently implemented code at Scene Optimizer passed does not implement multi material as further optimization, as I did on my quick proof of concept function.

Using a multi-material reduces the mesh count, which could lead to a performance increase.

Don't you think it would be a nice addition to merge all merged meshes into one single multi-material (or several of them) as I did as an extra step of the optimization algorithm? What do you think? :)
If this is a bad idea, please let me know - I am just trying to contribute to make the library better :)

IMPORTANT IDEA: Also I would like to take this even further. I think that once the optimization is done, we should serialize the result of all merged meshes and export as babylon file. In this way, the optimization will be done ONLY once (by the developer) and not every time (by the end user).

We could make this into a webpage where our users load the main babylon file, the scene gets optimized by using merge mesh, and returned as a new babylon optimized file they can use on their production projects (or we could make it part of the Inspector, under a new tab called "Optimize")

Link to comment
Share on other sites

Awesome :)

Let me get into it. Before I do the PR I am doing tests on my side to make the code more robust.

@Deltakosh, I need  one thing: how can I get the unique indentifier of a material?
Not the material name, but a string or number that can be used to uniquely identify this material.

Does there exist such thing? material.id is unique? (If not, not a problem I can handle this otherwise)

UPDATE: I have run into a much more important issue. There seems to be a bug with BABYLON.Mesh.MergeMeshes() function when the meshes to merge have more than 65536 vertices (even though the flag  allow32BitsIndices is set to true).
I have isolated a test case for this, but I don't how to have a sandbox with external gltf, bin, and multiple texture files. 

The error happens only with particular meshes of the same material, evidently those with more than 65536 vertices:
 
  var disposeSource = true;
  var allow32BitsIndices = true;
  var meshSubclass = undefined;
  var subdivideWithSubMeshes = false;
 BABYLON.Mesh.MergeMeshes(meshList,  disposeSource, allow32BitsIndices, meshSubclass, subdivideWithSubMeshes);


TypeError: this.indices.push is not a function
    at q.merge (babylon.js:1)
    at Function.p.MergeMeshes (babylon.js:1)


I am thinking it might be appropriate to open an issue on Babylon source code. We need this fixed before I can proceed.
Shall I open an issue on git and attach all files?

 

UPDATE 2: Done! I have created a completely isolated full test suite showing that by merging multiple meshes with more than 65536 and allow32BitsIndices on, Babylon crashes.

Here is the issue with full test suite attachedhttps://github.com/BabylonJS/Babylon.js/issues/4859

Link to comment
Share on other sites

  • 4 weeks later...

Hey guys! :)

I've been extremely busy with other non-3d urgent projects that unexpectedly got into the way. Hopefully, I will be able to plunge again into the loved 3d world in the next two weeks or so - as soon as the urgent project is done -
and I will resume this mergeMeshes implementation and share my discoveries of optimization :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...