instanceIndex
Provides a read-only, zero-based integer in TSL that represents the unique ID of the currently rendering geometry instance.
Core Advantages
It perfectly abstracts the underlying built-in variable (like GLSL's gl_InstanceID), offering a unified, portable, and semantically clear interface. As the starting point for all per-instance variations, it's a safe and fundamental building block.
Common Uses
Procedural layout (e.g., creating a grid)
Algorithmic visual variations (e.g., generating random seeds or patterns)
Animation phase offsetting (e.g., schools of fish, crowds)
Debugging and visualization (converting the index to a color)
How to adjust
instanceIndex itself cannot be adjusted, but applying different mathematical operations to it creates a vast range of effects. For example, using it directly creates a linear arrangement, processing it with a sin() function creates a wave, and combining it with cos() and sin() can create a circular layout.
Code Examples
1// Define the grid width
2const gridWidth = float( 20.0 );
3
4// Calculate 2D grid coordinates from the index
5const gridX = instanceIndex.mod( gridWidth );
6const gridY = instanceIndex.div( gridWidth ).floor();
7
8// Apply the calculated grid coordinates as a position offset
9const gridPosition = vec3( gridX, gridY, 0 );