- Home /
Get pixels from material displaying camera feed - LiveTexture Plugin
Hello,
I'm using the LiveTexture plugin for iOS in order to retrieve the camera feed of my device. The code for displaying my camera feed on a plane is the following - it works:
private Texture2D texture;
private GUIText textLog;
// Use this for initialization
void Start () {
texture = LiveTextureBinding.startCameraCapture(false, LTCapturePreset.Size1280x720);
renderer.sharedMaterial.mainTexture = texture;
LiveTextureBinding.setExposureMode(LTExposureMode.Locked);
textLog = GameObject.Find("LogText").GetComponent<GUIText>();
}
I would like to get the pixels of that video feed, I do the following however it doesn't seem to work as the output value doesn't change. What is wrong?
void Update (){
Color[] pixels = texture.GetPixels();
float averageLum = 0.0f;
foreach(Color pixel in pixels){
averageLum += (pixel.r * 0.299f) + (pixel.g * 0.587f) + (pixel.b * 0.114f);
}
averageLum /= pixels.Length;
textLog.text = "avg. luminosity: " + averageLum;
}
I also tried to replace texture.GetPixels by the following but it still doesn't work!
pixels = (renderer.sharedMaterial.mainTexture as Texture2D).GetPixels();
EDIT: Note that if I replace all that LiveTexture thing on my Start with a WebCamTexture and then retrieve the pixels in Update with code below, it is working fine.
pixels = (renderer.material.mainTexture as WebCamTexture).GetPixels();
Answer by Bunny83 · May 14, 2014 at 01:04 PM
Well, i don't know that "LiveTexture plugin" but it might be possible that the Texture the plugin generates isn't readable. You can only call GetPixels on textures which are readable. Have you tried using GetPixels and then using SetPixels on a new Texture to see if the data got transferred? I think the Texture2D you got back is just a wrapper for a native texture which probably can't be read since it doesn't exist in program memory.
Yes that was the problem. Solved it by creating a temp texture containing this unreadable texture (with ReadPixels). In case someone has the same problem...
I am having this same issue, but can't find an example anywhere to solve this. How do you create a texture from another one with the ReadPixels set?
Answer by pixel_fiend · Jun 20, 2014 at 04:54 PM
This Blit function worked better than ReadPixels. I'm pretty sure it's just an abstraction of ReadPixels, fullscreen quad, etc.
Anyway, this is how I took a snapshot of the webcam and copied it to another texture.
// temporary render texture to draw the webcam to
RenderTexture _rt = new RenderTexture(Screen.width, Screen.height, 24);
Texture2D webcamTexture = LiveTextureBinding.startCameraCapture (true, LTCapturePreset.Size640x480);
RenderTexture snapshot = Graphics.Blit(webcamTexture, _rt);
Your answer
![](https://koobas.hobune.stream/wayback/20220613145023im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Live screen share Unity3D 2 Answers
How can I save the Bild from a Camera, which not main camera is. 1 Answer
Get RenderTexture 1 Answer