参考 instanceIndex

instanceIndex

此 API 是实验性的

其行为可能在 p5.js 的未来版本中发生变化。

Returns the index of the current instance when drawing multiple copies of a shape with model(count). The first instance has an index of 0, the second has 1, and so on.

This lets each copy of a shape behave differently. For example, you can use the index to place instances at different positions, give them different colors, or animate them at different speeds.

instanceIndex can only be used inside a p5.strands shader callback.

(Note: instanceID() is also available as a function for compatibility.)

If you are using WebGPU mode, a common pattern is to use instanceIndex to look up data made with createStorage(). This lets you give each instance different properties.

This can be paired with buildComputeShader to update the data being read.


相关参考