- Home /
What is the best way to GetPixel out of a RenderTexture?
I'm trying to get a fluid simulation working on Android.
Running in C# is too slow.
Making a C++ plugin could potentially be faster. I've never written such a plugin, but I do know C++.
Compute Shaders are out of the question because I wouldn't even be able to test on my phone (Nexus 5).
I have a working simulation that uses regular Shaders and RenderTextures, but they don't have a GetPixel function which I need to be able to have the fluid push my GameObjects around.
The best solution I've been able to find so far is to make a Texture2D and use ReadPixels to fill it, then GetPixel out of it. The code I'm using is simply:
RenderTexture.active = m_velocityTex;// The RenderTexture.
velocityReader.ReadPixels(new Rect(0, 0, m_width, m_height), 0, 0);// The Texture2D.
I've tried having that code at the end of Update, in LateUpdate, and in a coroutine that uses WaitForEndOfFrame, as well as a coroutine that manually tells the camera to render then waits a frame before the ReadPixels call.
But no matter what I try, ReadPixels kills the frame rate. Is there some other way of reading from a RenderTexture or setting it up so that ReadPixels doesn't cock block the main thread?
Also, can anyone comment on whether it would be worth trying to port the sim into a C++ plugin?
I ran into the same problem on desktop. If you find a better / faster solution than compute shaders, I'd love to hear about it.
Anythin new here? I am also trying to iter through all pixels of a Texture2D; regards