Rodrix3 Posted July 29, 2018 Share Posted July 29, 2018 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 Quote Link to comment Share on other sites More sharing options...
Rodrix3 Posted July 30, 2018 Author Share Posted July 30, 2018 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! Dad72 1 Quote Link to comment Share on other sites More sharing options...
Dad72 Posted July 30, 2018 Share Posted July 30, 2018 Very interesting. it would indeed be nice to have that in Babylon. and I think this can be a part for the sceneOptimizer and more. Rodrix3 1 Quote Link to comment Share on other sites More sharing options...
Guest Posted July 30, 2018 Share Posted July 30, 2018 Well this is the goal of this guy already: ) https://github.com/BabylonJS/Babylon.js/blob/4eaf63b9f3a97819ae6c523ad9c211d28b2784df/src/Tools/babylon.sceneOptimizer.ts#L333 Dad72 1 Quote Link to comment Share on other sites More sharing options...
Rodrix3 Posted July 30, 2018 Author Share Posted July 30, 2018 25 minutes ago, Deltakosh said: Well this is the goal of this guy already: ) https://github.com/BabylonJS/Babylon.js/blob/4eaf63b9f3a97819ae6c523ad9c211d28b2784df/src/Tools/babylon.sceneOptimizer.ts#L333 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") Quote Link to comment Share on other sites More sharing options...
Guest Posted July 30, 2018 Share Posted July 30, 2018 Nope it is a really good idea! I encourage you to do a PR so we can even merge it in the SceneOptimizer! Rodrix3 1 Quote Link to comment Share on other sites More sharing options...
Guest Posted July 30, 2018 Share Posted July 30, 2018 (And I love the optimize tab A LOT) Rodrix3 1 Quote Link to comment Share on other sites More sharing options...
Rodrix3 Posted July 31, 2018 Author Share Posted July 31, 2018 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 attached: https://github.com/BabylonJS/Babylon.js/issues/4859 Quote Link to comment Share on other sites More sharing options...
Dad72 Posted July 31, 2018 Share Posted July 31, 2018 I'm not sure but I think there is a property material.uniqId or one function material.uniqId() But maybe I'm wrong and confused with something else. Rodrix3 1 Quote Link to comment Share on other sites More sharing options...
Guest Posted July 31, 2018 Share Posted July 31, 2018 uniqueId should be the right property. I will look into the issue asap Rodrix3 1 Quote Link to comment Share on other sites More sharing options...
Rodrix3 Posted August 28, 2018 Author Share Posted August 28, 2018 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 MinsuKim and GameMonetize 1 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.