Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Speeding up reading from a RenderTexture

$
0
0
I'm trying to read from a render texture every frame. In my scene, I have a camera that's rendering to a RenderTexture. I need to determine the value of a pixel at a known location in that image. I tried setting up some code to read a pixel out of the render texture into a 1x1 Texture2D, like so: Texture2D texture = new Texture2D(1,1); Rect rect = new Rect(Mathf.FloorToInt(pixel.x), Mathf.FloorToInt(pixel.y), 1, 1); testTexture.ReadPixels(rect, 0, 0); //testTexture.Apply(); Color32 pixelSample = testTexture.GetPixel(0,0); This code, specifically ReadPixels(), is taking 3-5 ms each frame on the GPU. That's actually quite a big deal for us, and I'm trying to figure out why it's so large. The code is copying from the GPU to RAM, so I get that there's going to be a delay, but this seems large. I'm wondering if I'm blocking execution of Unity's main thread or doing something else to gum up the works. I'm calling this code from OnRenderImage - I've also tried Update() with no apparent change. Is there a specific point in the rendering I should be doing this, or perhaps a different way of going about it that's faster? Is there any other way to speed up the read of a single pixel from a RenderTexture? FYI, this has to work in the web player, so I can't use texture pointers. (P.S.: I don't believe it's possible to get information directly out of a shader, at least pre-DX11, but if I'm wrong that's another avenue I could try)

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>