Hello all, I’ve been researching this for days now and the consensus seems to be that it is possible but can be very slow, however in all my attempts I have been unable to read any data at all from the CPU side of things.
My end goal is to return a float either per instance or per pixel of an instance which indicated whether or not it is in shadow from a particular light, I have the calculation working from running AdditionalLightRealtimeShadow() which returns 0 when in light and 1 when in shadow.
This works perfectly on the GPU and I am able to adjust say the color of the material based on whether or not the certain pixel is in light, but I want to pass this data to the CPU to apply gameplay logic when a certain percentage of the object is hit by light.
I have tried creating a StructuredBuffer in the shader and setting a ComputeBuffer in c#, setting it with a Material.SetBuffer() call, after more research I found that Material.SetBuffer actually does not read from the GPU. I then looked into ComputeShaders which are be able to read from the GPU however I would need to pass my data from my fragment shader to the compute shader, and maybe I am not understanding correctly but my research has led me to think that in order to pass that data it would have to go through the CPU anyways. (Which I am struggling to do in the first place)
Another work around I have heard of is to create a custom rendertexture and draw the frag shader to it, and then read the rendertexture from the cpu with .GetPixels(), I’d like to avoid this as my shader is coupled with my custom lighting shader to access the lighting data, and I would like to keep it on the main renderer.
Does anyone know of the right way to reference a buffer in frag shaders from C#?
Or any other work around to pass data from the GPU to CPU? I have also of the idea of using
Stream Output Compatibility and I’m curious if that is a rabbit hole worth going down.