Hello,
I am trying to create a canvas shader that displays a different color for each 3d object in the scene! Is there any way to access some 3d model information that generates the current pixel in order to assign a different color for each object in the shader?
An example in the image of the result I would like to achieve.
There are several ways. You can use the set_shader_parameter function to pass a color value or any kind of value that then defines the color in the shader, or you can pass along the INSTANCE_ID of an instanced mode mesh, or you can set vertex color in your mesh, or you can make every vertex in the UV map 0.0,0.0 and then use an UV offset per entity on a gradient or noise texture. There are so many possible solutions!
@Rooilo0 I don’t think I can assign parent material to true on a 3D object (?)
I’ll share with you the structure of the project so maybe you can better understand what I want to do.
The canvas_item shader is applied to the SubViewportContainer:
@Efi : If I understand correctly, however, instance uniforms are only available in space (3D) shaders. And I’m using shader_type canvas_item. ref
Well, you could switch to a spatial shader, but you’re not using instanced meshes anyway, from what I see in your scene tree, but any of the other options work equally well.
Well, that’s my only idea.
I’m not really into 3D games but I think that the property Use Parent Material stays there.
If that doesn’t works, I don’t know what else you can do
create a material that writes a unique color. expose the color as an instance uniform.
for each mesh that need to render to that ID buffer, duplicate the mesh node (not the mesh!) and assign in override material the material you made in step 1.
those duplicate meshes should be on a different culling layer, add a camera that renders only that layer
Thank you. Following your advice I almost succeeded in accomplishing what I wanted to do. The only problem is that if I want to move an object now I have to move two nodes. Any ideas on how to get around this problem?