- Home /
How to get pixels from an external texture?
Hello,
I wanted to know how to get pixels from an external texture (plugin : EasyMovieTexture).
Currently, I'm using this function :
private Texture2D GetReadableTexture(Texture2D tex)
{
if (myTexture2D == null)
myTexture2D = new Texture2D(tex.width, tex.height);
// https://support.unity3d.com/hc/en-us/articles/206486626-How-can-I-get-pixels-from-unreadable-textures-
RenderTexture tmp = RenderTexture.GetTemporary(
tex.width,
tex.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(tex, tmp);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = tmp;
myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
//myTexture2D.Apply(); // no need
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(tmp);
return myTexture2D;
}
It returns a readable texture from the external texture. It works. But it is quite slow, and creates a lot of garbage with the GetPixels() that will be done after.
Is there a way to get the texture pixel for Editor/Android, we assume that we are using OpenGL. (has someone done it?) Is it possible without creating a native plugin (tried it, crashes everytime ^^')
My final goal is to get the average color of the image (video frame).
Thanks in advance,
Sylafrs
Since my goal is to get the average of the pixels, resizing the wanted texture into a small one will create an average (I think it's using an histogram). Since I don't want a histogram result, but a real average, Resizing the tmp and myTexture2D to 10x10 textures will give a small array (len=100) in which we can perform the average.
Perfs. are way better, but still, I know that I can do better... just can't manage to do it right now :-°
I know this is a very old problem. Reusing a Texture2D memory every time without re-applying will help reduce garbage, but I would love to know a better solution.
I tested it on the development board, and it took 52.3ms to execute such a conversion function once, which means I could not even guarantee an image of 20 frames per second (50ms per second). If I can directly read the external texture, it will be of great help!
Answer by lgarczyn · Mar 14, 2017 at 06:21 PM
Scaling down simply uses bilinear interpolation, so you will get the average of the 4 middle pixels, quite bad.
If you're dealing with the same video, the highest perf you'll ever get is to do any slow algorithm, store the result in a color array / texture, and use that as your data.
If inaccuracy is okay, just do a getpixels and sample 500 semi-random pixels on the map, that's how I did it in the Flash days.
If you want both accuracy and dynamic videos, this guy did something that works: http://stackoverflow.com/questions/20726556/xna-and-hlsl-sample-entire-texture-for-luminosity-average
Your answer
Follow this Question
Related Questions
How to access an Android native plugin in unity ? 0 Answers
Referencing a DLL in C# 0 Answers
GetNativeTexturePtr is not working in iOS plugin 1 Answer
Pass camera stream from unity and android plugin 1 Answer
Android native plugin question 1 Answer