Basic

September 9 2020

4 comments

Simplify Your Life With Unity Mesh Simplifier

By Rubén Torres Bonet

September 9, 2020


__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"6c4de":{"name":"Main Accent","parent":-1},"67ed2":{"name":"Accent Dark","parent":"6c4de","lock":{"saturation":1}}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"6c4de":{"val":"var(--tcb-skin-color-0)"},"67ed2":{"val":"rgb(59, 65, 63)","hsl_parent_dependency":{"h":160,"l":-0.05,"s":0.04}}},"gradients":[]},"original":{"colors":{"6c4de":{"val":"rgb(51, 190, 127)","hsl":{"h":152,"s":0.57,"l":0.47}},"67ed2":{"val":"rgb(59, 65, 63)","hsl_parent_dependency":{"h":160,"s":0.04,"l":0.24}}},"gradients":[]}}]}__CONFIG_colors_palette__

Trying to find if your game performance is GPU bound? Well, I have great news: your life is now easier with the Unity Mesh Simplifier package I am about to show you.

This neat API will let you reduce your assets poly count in matter of seconds.

Is Your Game Performance GPU Bound?

When optimizing your game, it is quite useful to find where your current bottleneck lies.

Sure enough, you might have different bottlenecks depending on the scene and the target device.

But in broader terms, your game tends to be bottlenecked in one main area.

Who's slowing you down?

Is it your CPU, your GPU, your Memory Bandwidth?

Today, I'll show you a quick way to find if your game performance is GPU bound. More precisely, you will learn a technique to determine if your game performance is vertex bound.

The plan is simple:

You are going to reduce the poly count of your meshes and see if that improves your framerate.

If removing vertices improves your framerate, then you were vertex bound.

Not only that, you'll also be able to use this very same tool to actually make the final optimizations.

Let's go!

Setting Up Unity Mesh Simplifier

Installation is simple enough.

Just open Window → Package Manager and add the following git URL:

https://github.com/Whinarn/UnityMeshSimplifier.git

You can do this by clicking on the plus (+) symbol and choosing Add Package From git URL.

Unity Package Manager Install git URL

That's it. Package imported into your project.

Unity Mesh Simplifier Package

With no more delays, you can now use the mesh simplifier API.

Reducing Unity Mesh Poly Count

To reduce the vertex count of a game object containing a mesh renderer and a mesh filter, you just need to invoke the Unity Mesh Simplifier API on the mesh itself.

Ideally, you can develop a custom inspector to make this poly count reduction.

You can do that.

But me?

I will merely prototype an OnGUI function so you can see how the mesh reduction works in run-time.

Here's my code*:

void OnGUI()
{
if (GUI.Button(Rect.MinMaxRect(0, 0, 200, 200),"Simplify Mesh"))
{
var originalMesh = GetComponent<MeshFilter>().sharedMesh;
float quality = 0.2f;
var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();
meshSimplifier.Initialize(originalMesh);
meshSimplifier.SimplifyMesh(quality);
var destMesh = meshSimplifier.ToMesh();
GetComponent<MeshFilter>().sharedMesh = destMesh;
}
}
void OnGUI() { if (GUI.Button(Rect.MinMaxRect(0, 0, 200, 200),"Simplify Mesh")) { var originalMesh = GetComponent<MeshFilter>().sharedMesh; float quality = 0.2f; var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier(); meshSimplifier.Initialize(originalMesh); meshSimplifier.SimplifyMesh(quality); var destMesh = meshSimplifier.ToMesh(); GetComponent<MeshFilter>().sharedMesh = destMesh; } }
void OnGUI()
{
    if (GUI.Button(Rect.MinMaxRect(0, 0, 200, 200),"Simplify Mesh"))
    {
        var originalMesh = GetComponent<MeshFilter>().sharedMesh;
        float quality = 0.2f;
        var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();
        meshSimplifier.Initialize(originalMesh);
        meshSimplifier.SimplifyMesh(quality);
        var destMesh = meshSimplifier.ToMesh();
        GetComponent<MeshFilter>().sharedMesh = destMesh;
    }
}

* Disclaimer: If using this snippet breaks your project, I would feel bad about it for a few seconds. Don't use it.

See what happens next:

Unity Mesh Simplifier Example

At quality 0.2, we reduced the vertex count of this sample mesh from 7k to 2.5k... in run-time.

Not bad for a few seconds of your life.

It goes without saying but:

lower quality = lower poly count = worse visuals = louder LOLs = higher performance

Indeed, this Unity mesh poly count reduction method is a great alternative to Unity with Simplygon when you care about faster iterations.

It works fairly well for reductions with quality above 0.5. Below that number, your mesh will start breaking apart.

But hey, if that goes well with your art style, go ahead. Won't judge you.

In any case, add this valuable tool to your tool shelf.

I certainly added it to my list of recommended tools and will stay there for a while.

Lots of credits to Mattias Edlund and his great work on this tool.

What's Next?

To see more optimization tips for your game, check out my Unity Performance Checklist.

Time to squeeze some performance juice from your game!

~ Ruben

  • Thanks Rubén. Would you say the tool is not only for quick iteration but also for those that don’t use a 3D modelling tool where they can do similar? I use Blender but if I can get not only quicker results in Unity but equal or better results, this definitely sounds worth a shot.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Rubén Torres Bonet

    About the author

    Born Game Developer, now ready to help you develop better games. Primary programmer on Star Trek Bridge Crew (Oculus Quest), Diamond Dash. Programmer on Time Stall, Catan Universe, Anne Frank House VR, Jelly Splash, Blackguards Definitive Edition. I also worked in minor XR experiences for HoloLens and Vive for clients such as Audi and Volkswagen.

    You might also like

    >