getTextureIndex
A CPU-side helper function that finds and returns the integer index of a texture object within an array by its name.
Core Advantages
Its core value is decoupling a texture's logical identity (name) from its physical position (index). This makes the code robustly handle changes in the texture array's order and significantly improves code readability and maintainability.
Common Uses
In terrain materials, to get textures for different layers like grass or rock by name.
For customizable character materials, to dynamically find and apply the correct textures.
When working with texture atlases or arrays, to locate a specific layer or slice by name.
How to adjust
This function cannot be adjusted directly. However, changing its inputs affects the outcome: changing the `name` parameter (e.g., from 'fire' to 'water') will switch the final sampled texture, causing a direct visual change. In contrast, reordering the `textures` array will cause no visual change, as the function will always find the correct new index, which demonstrates its power.
Code Examples
1// Get the index of the texture named 'normalMap' from the textures array
2const normalMapIndex = getTextureIndex( textures, 'normalMap' );