atteneder / glTFast

Efficient glTF 3D import / export package for Unity
Other
1.25k stars 251 forks source link

Improve SkinnedMeshRenderer culling by providing a better AABB #302

Open atteneder opened 2 years ago

atteneder commented 2 years ago

At the moment culling problems are avoided by enabling Update When Offscreen setting of SkinnedMeshRenderers (SMR), which comes at a performance cost. This issue is about finding a better solution.

Solution 1: Enable Update When Offscreen (status quo)

Solution 2: Assign a particular Transform as root bone

Figure out which bone/Transform has the most influence on a SMR and assign it as root bone (plus adjust/offset the bounds accordingly)

This works correctly if one (1) bone only influences the SMR. In glTF bone weights come in sets of 4, so the importer would have to iterate all influences to check it.

This won't scale well with vertex count.

Solution 3: Calculate AABBs that cover all movement over time

Given that each vertex's position can be influenced by many transforms that animate over time, calculating the AABB that covers all frames and positions is not trivial. Usually this is done on the GPU, so there's no way of retrieving that information easily.

Still, one could put in the work and calculate it, but I assume it will be:

These ideas initially came up in #301

in0finite commented 1 year ago

One idea would be to use SkinnedMeshRenderer.BakeMesh() API, which creates snapshot of the skinned mesh on the CPU. So, someone can compute AABB per frame, or per anim, and then assign it manually.

This could be used both at runtime (compute once and cache AABBs) or in edit-mode (prebuilding AABBs).