Generate and write on textures

Hello,
I’m trying to generate a new texture, passing UV coordinates collected with ray tracing and colouring the texture only where the ray-trace hit the object.
Is there a way to do so?

have you already solved the problem?

Kind of, but I don’t think is the best way to do it. It ended up to be slow and heavy.