You are browsing as a guest. Sign up (or log in) to start making projects!

3h 52m 46s logged

INSANE tree optimisations

Went from 60-80 fps @ 4K to 400-450 (in game). Yes a 6 to 7x increase.

How you ask?

GPU Mesh Instance drawing offloading the CPU work of handling thousands of game objects.

In English, I’m no longer spawning thousands of tree objects, but instead telling the GPU to draw the tree instances every frame. This seems quite unintuitive because you’re now drawing thousands of things every second. BUT this means that there are no game objects, so the CPU doesn’t even know that these exist. Also the GPU draws it thousands of times anyways.

A game object handles transforms, rotations, etc. that the CPU has to keep track of whether it is active or not. This had the added benefit of the trees having no overhead when the trees are disabled, so you regain all of your framerate!

Also I used some neat little script that combined the meshes all together in the GameObject prefab.

Now it really should be able to run on your potato PC/Laptop.

Combine script

using UnityEngine;

public class MeshCombineUtility : MonoBehaviour
{
    [ContextMenu("Combine Children Meshes")]
    public void Combine()
    {
        MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];

        Matrix4x4 matrix = transform.worldToLocalMatrix;

        for (int i = 0; i < meshFilters.Length; i++)
        {
            if (meshFilters[i].gameObject == gameObject) continue;
            combine[i].mesh = meshFilters[i].sharedMesh;
            combine[i].transform = matrix * meshFilters[i].transform.localToWorldMatrix;
        }

        Mesh combinedMesh = new Mesh();
        combinedMesh.CombineMeshes(combine, true, true);

        #if UNITY_EDITOR
        string directory = $"Assets/Assets/Mesh/Tree/{gameObject.name}.asset";
        UnityEditor.AssetDatabase.CreateAsset(combinedMesh, directory);
        UnityEditor.AssetDatabase.SaveAssets();
        Debug.Log($"Combined mesh saved to {directory}");
        #endif
    }
}

Draw section of GPU Mesh Instancing

    private void DrawTreeMeshes()
    {
        if (treeMesh == null || treeMaterial == null) return;

        foreach (var kvp in generatedChunks)
        {
            ChunkData chunk = kvp.Value;
            if (chunk.spawnedTrees == null || chunk.spawnedTrees.Count == 0) continue;

            int batchSize = 1023;
            int totalTrees = chunk.spawnedTrees.Count;

            for(int i = 0; i < totalTrees; i+=batchSize)
            {
                int length = Mathf.Min(batchSize, totalTrees - i);
                Matrix4x4[] subMatrices = new Matrix4x4[length];

                for (int j = 0; j < length; j++)
                {
                    subMatrices[j] = chunk.spawnedTrees[i + j].matrix;
                }

                int meshIndex = Mathf.Abs(chunk.chunkCord.GetHashCode() + i) % treeMesh.Length;

                Graphics.DrawMeshInstanced(treeMesh[meshIndex], 0, treeMaterial, subMatrices, length);
            }
        }
    }
4
1189

Comments 9

@obedot

Yeah!!! Optimization!

@sol4r_on_hackclub

@obedot YEAHHHH

@Shreyansh

Dang, thats insanely useful especially for macs. my mac would get fried if it had to render that many trees in 3d. This method is like 10 times better

@sol4r_on_hackclub

@Shreyansh don’t worry you can turn the trees of again and i reach a stable 800 fps, sometimes reaching into the 1000s

@Danial

Capital Chaos most commonly refers to Capital Chaos TV, a long-running heavy metal and punk music media outlet and YouTube channel founded in Sacramento, California, in 1998. It also refers to a live music event series held in venues like Edinburgh’s La Belle Angele, a 2023 short comedy film about visiting US state capitals, and a former map featured in Brawl Stars.

won’t this count as a copyright thing too?

@sol4r_on_hackclub

@Danial I think we’re fine because those are different medium pieces, and the copyright covers the content not really the name. I could’ve kept Pocket City if I REALLY wanted to but it’d be bad SEO and possibly cause issues with STEAM not really copyright.
This is a movie vs a video game, and worst case scenario i’ll name it Capital Chaos: The Name.
and live music is whatever, and i think this is way different to a Brawl Stars so fingers crossed we’re good to go.

@aayanalikhan09

would be nice if my potato laptop didnt have integrated jraphics 😢

@fAILedKnight

Beautiful game!

@sol4r_on_hackclub

@aayanalikhan09 the whole point of this game is that you should be able to run it!
@fAILedKnight thank you bro