Its possible to create texture (by custom shader) for each instance object?

Hello. sorry for the nooby question but going to ask it anyway :smiley:

my idea:

  • create N amount of objects using InstancedMesh.

  • for each object, we pass to custom shader specific traits:
    position,color…

  • in shader, according to the position of each object (which are different) , we draw simple shapes for each object using the passed colors.

  • so essentially without loading texture, we create different textures inside the shader itself for each object. it doesnt matter how many objects out there.

  • for example in the picture:
    we have 3 instance boxes using instancedMesh.
    each box got its own texture (simple lines) according to their position.

== its possible? and if so, its efficient? what if i create a lots and lots of objects and for each object its own texture. maybe its not worth it after all because of performance?

You can pass the position of each instance from vertex to fragment shader and then use some drawing formula that will produce some colors depending on the pass position, so yes, it’s possible.

Alternatively, the same way colors are passed for individual instances, you can create your own attribute, say instance id, and pass it to vertex/fragment shader and base drawing on that id.

It will be as efficient as the formula for the drawing and will slow down rendering of course as the number of instances grow, so in a particular situation it might be worth it or not.

You can also pass a texture to the shader code and use instance id to generate an offset on the texture and apply different patches from it to texturize instances, so it’ll work as an atlas.

1 Like

Thanks for the reply :+1:. here an example with instances which what i was looking for Raycast Highlight with InstancedMesh, missed that one.

Yep, that’s pretty much the idea