How to paint on a 3D object without raycast in Unity (from shader)?

I have spent weeks to find a way to paint on a 3D Object with a brush in Unity. I have find solutions with raycasting and RenderTexrures, but it’s not fast enough.

Currently I am passing the cameras MVP matrix to my shader to create a circle on the 3D object’s surface, but I am unable to “burn it” on the texture from the shader, since there is no way(?) to make actual changes from a shader.

I have also tried to do something with ZBuffer, but it only works if nothing moves. As soon as I move something, the draw will “flow”.

I am not an expert in shader coding, but I see no option for drawing anything from there.

Please if you have any suggestions, do not hesitate to share with me.

Hi seltonnn,

this comment on reddit from DrunkMc helped me a lot:
https://www.reddit.com/r/Unity3D/comments/8haf6m/painting_a_surface_and_tracking_how_much_has_been/

and especially this video from the Snowtracks Shader series:

You still use raycast, but the trick is to create a second shader to draw into the texture of your 3D object, preventing the slow transmission of a rendertexture bewteen CPU and GPU.

Hope it helps.

It’s not good. I have a bunch of little meshes with a bunch of (few hundred) mesh colliders because of the raycast, and it’s too much. The result is around 2-3 FPS with my scene.

Currently I am able to color the vertices at a given point (e.g. at the mouse position using the cameras view matrix in the shader), but I am unable to “save on vertex colors to the actual mesh” from shader. So the previous vertex coloring is gone at the next rendering iteration. :frowning:

Dont use mesh colliders then? is there a specific reason you cant do this with compounds of primitive colliders?

EDIT: and you save the colors by putting that data into a buffer somewhere. Compute shader with structured buffers would be a nice way and you would have buffer as a render texture / texture.

1 Like

3D painting is easy and pleasant :slight_smile:

1 Like

Wow, that’s cool. Can you share the code as well?