Where does anything actually get rendered to the RenderTexture? (You’re setting it as active and immediately reading from it.)
Set it as target for a Camera and then do the ReadPixels part after the Camera is done rendering. (OnPostRender with the script being on the same GameObject as the Camera.)
@Branxord You need to copy the texture back from GPU to the CPU side of the things. You can do that with ReadPixels. Looks like the post above is almost correct?
Only thing I can see missing is the texture.Apply(). If you don’t apply a texture when you have set the pixels, it won’t get stored/saved. You should call it right after the texture.ReadPixels.
Here are a few links about this topic:
It’s not very fast and is said to cause some stalling so use it with consideration. i.e. probably not wise to try get a copy of some big texture every frame.
I’ve used it quite often when I’ve been testing if my compute shader experiments function at all. I’ve used very small test textures then (like 32x32 or 64x64) to read data back.
Oh ok, yeah i kinda got it working now, except i need to know the coordinates of the pixels i retrieved (i’m doing a for loop and checking the GetPixels array) but i’m not finding a way to get the coords so far
If you get the RenderTexture copied to a texture, just get the coordinates?
I mean, it’s just an image? If you point to coordinate x=32, y=32, it’s that pixel.
Did you check the examples I linked? When you copy the RT to same resolution image, it’s a copy. I’m not sure what you are missing.
Assuming you have a RenderTexture rendered somewhere, like a compute shader that you dispatched just a moment before:
Was there a best rule of thumb for this?
I’m about to need exactly that using a CustomRenderTexture that’s revealing terrain/gameplay stuff that’s double buffered (accumulates over time), most of the suggested samples are RenderTextures assigned to the camera (which isn’t what Custom Render Texture are meant to be used as).
Will be trial and error-ing and come back if I find something that works.
Any luck? I’m trying to use a similar method to read pixel color from a shadergraph material with multiple textures, to color dust particles correctly. Having a hard time finding relevant examples.
No, excuse me that, the whole feature I was doing where I was going to use that got put on the side potentially forever. Got sidetracked to other things higher in priority. If I ever get back to that I’ll be sure to post back here.