drawIndex
Provides a unique integer ID within a TSL shader to distinguish between individual objects or groups rendered in a single, batched draw call.
Core Advantages
It enables per-instance variation while maintaining the extreme performance of a single draw call, eliminating the need for extra vertex data or complex CPU-side uniform management, thus greatly simplifying high-performance batch rendering.
Common Uses
Procedurally generating different colors or properties for each object in a batch.
Selecting different sub-textures from a Texture Atlas based on the object's ID.
Creating staggered animations with unique phases for large numbers of similar objects (e.g., grass, crowds).
In data visualization, using it as an index to read data for a specific object from a uniform array.
How to adjust
This node's value is not adjustable; it is a read-only integer ID provided by the renderer. Its value is determined by how you configure the geometry's draw groups (using `geometry.addGroup()`) or utilize MultiDraw features in JavaScript. Each group receives a sequential ID starting from 0.
Code Examples
1// Use drawIndex to create a unique animation phase offset for each instance
2const animationPhase = time.mul( speed ).add( drawIndex.mul( offsetFactor ) );
3
4// Apply the phased animation to create a natural swaying effect
5const sway = sin( animationPhase );