Instancing Mesh LOD
This section covers how to apply LOD to Instancing Mesh, which is used for rendering massive amounts of objects. LOD for InstancingMesh calculates the distance for each instance in parallel within the GPU Shader. This means there is virtually no CPU overhead, even when applying LOD to tens of thousands of objects.
1. How it Works
When LOD is registered to an InstancingMesh, Sub-Draw Calls equal to the number of LOD levels may be created internally. However, through rendering pipeline optimization, the GPU determines the camera distance for each instance and references the appropriate geometry buffer for rendering.
2. Usage
Use LODManager.addLOD just as you would for a regular Mesh.
import * as RedGPU from "https://redcamel.github.io/RedGPU/dist/index.js";
// 1. Create InstancingMesh (Base: Sphere High Poly)
const instancedMesh = new RedGPU.Display.InstancingMesh(
redGPUContext,
10000, 10000,
new RedGPU.Primitive.Sphere(redGPUContext, 2, 32, 32),
material
);
// 2. Add LOD Levels
// The GPU calculates the distance for each instance and applies the geometry accordingly.
instancedMesh.LODManager.addLOD(30, new RedGPU.Primitive.Sphere(redGPUContext, 2, 8, 8));
instancedMesh.LODManager.addLOD(60, new RedGPU.Primitive.Box(redGPUContext, 3, 3, 3));
scene.addChild(instancedMesh);3. Live Example: 2,000 LOD Objects
Observe how 2,000 objects change their shape depending on their distance from the camera. If you zoom in or out, distant objects appear as Boxes, while close objects appear as Spheres.
Key Summary
- GPU Acceleration: Since the GPU determines distances without CPU calculation, there is almost no performance degradation even with a massive number of objects.
- Memory Optimization: Using low-resolution models for distant objects dramatically reduces vertex processing costs.