Search Unity

How to render surface shader data to render texture?

Discussion in 'Shaders' started by carcasanchez, Jun 2, 2020.

  1. carcasanchez

    carcasanchez

    Joined:
    Jul 15, 2018
    Posts:
    177
    Hallo,
    I am working on a surface shader that needs to store data (just a float3) to make an operation during next frame. As far as I know, this is not easy.

    I also have the necessity to relegate everything to GPU, since I will instantiate the material like 2000 times in scene, and each material will have to perform the data storage.

    The best-sounding way I have found, it is to use a render texture.
    This will go like:

    -I get the float3 in a pass
    -Using the instanceID, I write the float3 values to a specific pixel of the texture (since it is a normalized float3, I guess I will have no trouble on storing it on rgb channels, if I move it from -1/1 space to 0/1)
    -Using the instanceID in the next frame, I read the same pixel from the renderTexture.

    I am still not sure if this workflow is possible using only one texture, keeping it at GPU at all times, since I haven't found any examples on the web. The ones I have found use compute shaders, C# code and RWBuffers, something I cannot afford due to performance/design reasons.

    Can somebody tell me how the syntaxis of this could be?