- Home /
getting texture from shader into c# script. Also: best way to get values from GPU to CPU?
Dear unity community
as I am pretty new to unity I would be happy for some guidance into the right direction.
I use a shader to modify an image sequence in runtime. This shader is applied as a texture to a material. Now I need to access the color values of this texture in a c# script. my first practical question would be: How do I get the texture from my shader into my c# script? From what I know I could use .GetPixel from there.
Since I am aware of the fact that getting these values from the gpu to the cpu is always a bottleneck, I was wondering whether there is a best practice to do this? I was thinking about passing the texture to a compute shader and fill a RWStructuredBuffer there and somehow getting the data back from there? Would that be faster?
Dunno. Try both and find out? ;)
You're right - reading pixels from GPU rendertexture to the CPU is slow. As a result, I try to avoid, by (where possible) continuing all remaining processing on the GPU side. But sometimes it's unavoidably necessary to combine shader output with other CPU code. If you find a better method to do so than ReadPixels(), let us know!