I have an instance using Drei
library with a plane geometry.
According to a state, I would like to display different materials for each instance:
- state is false: display a standard material with a simple color
- state is true: apply a different texture (an image) Each instance has its own state (for example 20% can be true and 80% can be false, or another ratio)
What would be the best way to do that?
Retained solution: Instanced Attributes
As mentionned by @Bhushan Wagh, the idea is to store the texture state inside a geometry attribute. Then, you can access this data in the shaders in order to apply a custom logic. You can follow his codesandbox for the R3F solution, or this one for the drei instance solution.
Investigated solutions
- Having 2 distinct instances (one for the colored material, one for the image texture):
it's ok when we have only 2 states, but what if we have like 10 possible states? (What would be the best solution in this case?)
- Represent a plane with a
boxGeometry
and display the proper face according to the state
It's also only working when we only have 2 states and are in 2D