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

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