- Home /
How to get pixel color from MovieTexture?
Dear game developers,
I am creating a TV asset that needs to light the room based on the footage running on its screen.
Renderer r = GetComponent<Renderer>();
MovieTexture movie = (MovieTexture)r.material.GetTexture("_EmissionMap");
movie.Play();
Playing the movie (*.mov) is not a problem. However, there seems to be no way to access the current frame's Texture2D to use its GetPixel function and get some Colors out of there (updating a Light's Color). I tried to pull out a Texture, which did not throw an error, but I could not convert this Texture to a Texture2D. Trying to directly get a Texture2D is not possible either (returns Null):
Texture2D tex = r.material.GetTexture("_EmissionMap") as Texture2D;
Any ideas? Thanks a lot!
Answer by yelman · Jan 05, 2016 at 08:16 AM
Do you have to get pixels every frame? It's very performance intensive.
A possible solution is to render current camera to rendertexture and than call Texture2D.ReadPixels Something like this should work:
// Render movie from camera to render texture
RenderTexture rt = RenderTexture.GetTemporary(256, 256, 24);
Camera.main.targetTexture = rt;
Camera.main.Render();
RenderTexture.active = rt;
// Copy pixels from active render texture to texture2d
targetTexture.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
targetTexture.Apply();
Thanks @yelman! It worked out perfectly! I placed a camera in front of the TV that only sees the TV screen to get its output. I made the RenderTexture and Texture2D targetTexture both 16x16 and calculated the average Color for my Light.
You can create a quad, set it's material to the one with the movie texture. Change camera to orographic and set it size to match the quad size. Than when you render from camera to render texture it will render only the quad wich has your movie texture.
Answer by oburacodacara · Aug 14, 2020 at 06:40 PM
Could you post your code here? i am realy trying to make this work with just what we have here but i cant see through Thanks!
This thread could be of help with retrieving pixel information from a video:
Your answer
![](https://koobas.hobune.stream/wayback/20220612054818im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I set the color of a light with a script? 1 Answer
How can I use GetPixel to create a color picker? 1 Answer
light being colored 1 Answer
Texture2D.GetPixel returning wrong colours 3 Answers
Popular Color/Light Bleeding Effect 0 Answers