# 🏎️ 8. LODs & Performance Optimization

Rendering 10,000 animated units at 60+ FPS requires more than just moving calculations to the GPU. If every single vertex of those 10,000 units calculates 4-bone influences and interpolates between frames, you will eventually hit the GPU's ALU (Arithmetic Logic Unit) and memory bandwidth limits.

**GPU Animation Entities PRO** includes a highly aggressive, automated optimization pipeline. It scales down both **GPU rendering cost** (via LODs) and **CPU logic cost** (via Tick-Rate scaling) based on camera distance.

***

#### 📉 1. Visual Degradation (The LOD Tab)

Our system perfectly integrates with Unity's standard `LODGroup` component. When you drag a prefab with an `LODGroup` into the **Animator Baker**, it automatically imports your LOD levels into the `Lods` tab.

For each LOD level, you can configure how the shader degrades to save performance:

**🦴 Skinning Quality (Bone Influences)**

Standard Unity animation uses up to 4 bones per vertex. We allow you to reduce this per LOD:

* **4 Bones:** Maximum quality. Essential for LOD 0 (close-up camera).
* **2 Bones:** Great for mid-distance (LOD 1). Visually identical to 4 bones in most cases, but skips half the math.
* **1 Bone:** The ultimate performance saver for distant swarms (LOD 2+). The mesh becomes completely rigid (like a Minecraft character), but at a distance, the player cannot tell the difference. It requires only a single matrix multiplication per vertex.

**🎞️ Interpolation (Stepped Animation)**

By default, our shader reads two animation frames from the `GraphicsBuffer` and interpolates (Lerps) between them to create buttery-smooth motion.

* If you **disable Interpolation**, the shader activates the `ANIM_STEP_ONLY` keyword.
* The shader will now only read one frame and snap to it.
* **Why do this?** It cuts memory bandwidth reads in half and eliminates the Lerp math. For distant units, the lack of interpolation is practically invisible, but the performance gain is massive.

**🌑 The Shadow Caster Trick (Zero Setup)**

Shadow passes are notoriously expensive. You might be wondering how to optimize them.\
**You don't have to.**\
Under the hood, our `SnivelerSGWrapper.hlsl` automatically detects if Unity is rendering a `ShadowCaster` or `DepthOnly` pass. If it is, the shader **forces 1-Bone Skinning and disables Interpolation**, regardless of your LOD settings. You get ultra-cheap shadows completely for free.

***

#### ⏱️ 2. Logic Degradation (Tick-Rate Optimization)

Even though the GPU handles the vertices, the CPU still has to calculate the current animation time, check transition conditions, and update the `_SnivelerRenderFrames` data for the shader.

To prevent the CPU from bottlenecking when you have 10,000+ units, we use **Distance-Based Tick-Rate Optimization**.

You configure this on the `AnimatorRendererAuthoring` component (the script you placed in your `SubScene` to upload the prefabs to the GPU).

* **Half Tick Distance (e.g., 50 meters):**\
  If an entity is further than this distance from the camera, the `AnimatorProcessSystem` will only update its animation logic every 2nd frame (effectively running its logic at 30 FPS).
* **Quarter Tick Distance (e.g., 100 meters):**\
  If an entity is further than this distance, its logic updates every 4th frame (running at 15 FPS).

Because the GPU continues to render the last known state, the units don't disappear—they just update their animation frames slightly less frequently. This drastically reduces the execution time of the Burst-compiled jobs.

***

#### 👁️ 3. Automatic Frustum Culling

You do not need to write custom culling logic for your animations.

The `AnimatorProcessSystem` automatically reads the `AnimatorCameraData` (updated by the Main Camera) and checks it against the `WorldRenderBounds` of your entities.

If an entity is completely off-screen (outside the camera frustum):

1. The system flags it as invisible.
2. It **skips** updating the `GraphicsBuffer` indices for that entity.
3. It **skips** calculating complex transition blending.
4. If the entity has `Root Motion`, it snaps the root motion delta to the final frame instead of calculating frame-by-frame interpolation.

This ensures that if you have 10,000 units in the level, but only 2,000 are on screen, your CPU is only doing the heavy lifting for those 2,000.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sniveler-code.gitbook.io/dots/gpu-animation-entities-pro/8.-lods-and-performance-optimization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
