Skip to content

ncgreco1440/MeshBakery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mesh Bakery

Mesh batch tool for Unity3D

Intro


Mesh Bakery is an open source tool for optimizing draw calls within Unity3D by combining similar meshes into one.

Version


Latest Release: 1.0.0

Date: 8/19/2017

Documentation


Mesh Bakery is a tool that attempts to optimize draw calls within Unity3D. Once the MeshBakery is attached and started it will run around and scoop up arbitrary meshes that have been specified by the application's author from within the Unity Editor. It will then create Batches based on unique mesh names and materials of each Batch Ingredient, and as each one is created it is also baked. As the baking completes all original MeshRenderers contained with the batch are disabled.

In order to use MeshBakery simply attach it to a GameObject which parents several identical meshes with matching materials. Use the Batch Ingredient data member to add to the list of Batch Indegredients. Each Batch Ingredient will contain a single mesh and a single material. Hit the play button and upon Start() the MeshBakery will create brand new GameObjects located at 0, 0, 0 in world space that contain the newly baked meshes which have the appropriate materials assigned.

MeshBakery Component Setup

Note that there is a checkbox "Launch From Editor". If you set up MeshBakery through the Unity Editor you must check that checkbox or the initialization won't happen properly. Setting up MeshBakery through code automatically overrides that checkbox.

To set up MeshBakery through code...

yourGameObj = (GameObject)Instantiate(Resources.Load("Path/To/Prefab"));
yourGameObj.AddComponent<MeshBakery>();
yourGameObj.GetComponent<MeshBakery>().m_BatchIngredients = new List<BatchIngredient> {
    new BatchIngredient(yourGameObj.GetComponentInChildren<MeshFilter>().sharedMesh, yourGameObj.GetComponentInChildren<MeshRenderer>().sharedMaterial)
};
yourGameObj.GetComponent<MeshBakery>().Init();

Alternatively you can set up your own arbitrary look up function like the one that exists within MeshBakery.cs SearchForArbitraryMeshToBatch()...and pass that into the BatchIngredient constructor.

Important Notes


There is a chance that scene load times will noticeably increase depending on how many meshes you wish to bake however during testing it was shown that a total list of over 3,400 meshes could be parsed, batched, and baked in 399 ms. The result was 1.1 million triangles rendering on screen in a total of 44 batches and 20 SetPass calls. One important note is that shadow distance was turned down to 20 in order to maximize total performance, however shadow distance should always be tuned appropriately for your game.

VERY IMPORTANT NOTES


One Extremely important note is the increase in Triangles that occurs with MeshBakery. This is inevitable to happen as you are giving up both Frustum and Occlusion Culling, which is why MeshBakery shines only under a very particular set of circumstances.

  1. You lack LODs for certain 3D Models
  2. The 3D Models are fairly close together in proximity within the scene

Best Practices


It is best practice to place a MeshBakery component on a GameObject that encapsulates objects within a specific area in your scene that has nearly a guaranteed chance that all of its children will share screen space rather than to bundle everything everywhere into one GameObject. This is because the biggest trade off is the limited use of Frustum Culling. It makes no sense to bundle 2 or more meshes into one if there is little to no chance of them ever all sharing the screen at the same time even though they may be the same mesh and material.

At the end of the day, be smart about where you place objects in your scene. It is both an art and a science.

Benchmarks


All benchmarks were completed using a single MeshBakery parenting 1,152 GameObjects containing 3 more GameObjects each with it's own MeshRenderer constructing a modular fence. In total, 3,456 meshes were batched in the scene.

Batch Mode Batches SetPass Calls Render Thread Image
Dynamic Batching 1327 187 3.0 ms link
Static Batching 420 372 1.4 ms link
GPU Instancing 192 187 2.9 ms link
MeshBakery 44 23 0.5 ms link

About

Mesh batch tool for Unity3D

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages