WebCamTexture -> Texture2D?

Hi there,

if I apply a WebCamTexture to such as a Cube via cube.renderer.material.mainTexture = wct; Why I can’t read out this texture via tx2d.SetPixels((cube.renderer.material.mainTexture as Texture2D).GetPixels());?

Is this because the WebCamTexture is only assigned to this material, but not really ‘in’ it?

Do you know another way to pick the texture out of the cube later (I’ve no access to wct at this time, only to the cube!)?

Thank you!!

No its because WebCamTexture is no Texture2D, its a Texture (Texture2D, WebCamTexture, RenderTexture all extend from Texture but they aren’t exchangeable)

If you want to read from it you would likely draw it and use ReadPixels.

Thank you for this information. I thought .mainTexture would be automatically convert to Texture2D with [...].mainTexture as Texture2D).GetPixels[...]

So, you said I’ve to use ReadPixels. This is what I’ve tried before I’ve posted this thread:

tx2d.ReadPixels(new Rect(0,0,cube.renderer.material.mainTexture.width,cube.renderer.material.mainTexture.height),0,0);

This brings me another error:
ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.

What did I wrong?

Another user in another post find the right solution for this problem:

tx2d.SetPixels((go.renderer.material.mainTexture as WebCamTexture).GetPixels());

Special thanks to mgear!!!

1 Like

This might not apply to you, but for future visitors…

If you’re trying to do this with a video texture, you shouldn’t be using setPixels/getPixels because it’s much slower than assigning an actual texture.

Type conversion is possible from WebCamTexture to Texture2D by grabbing the native texture pointer and assigning it to the Texture2D by using Texture2D.UpdateExternalTexture().

4 Likes

Could you give an example? I have a webcamtexture on a plane called livedisplay, and would like to grab the webcamtexture as texture2d to do other stuff. Can’t use read pixels as the camera already has effects applied, which I don’t want in my texture2d…

I tried everything but so far no luck…

for example :

Texture2D texture;
texture.UpdateExternalTexture(renderer.material.mainTexture, GetNativeTexturePtr());

Debug.log(texture);

Did you ever get this working?
Can you not use something like…
Texture2D texture;
texture = webcamMaterial.Value.mainTexture as Texture2D;
Let me know, as I am trying to get something similar working myself!
:smile:

Thank you very much for the heads up @pixel_fiend

Here is how you can convert it. (working code on mac. Will try on android soon.)

IntPtr pointer = _myWebcamTexture.GetNativeTexturePtr();
_myTexture.UpdateExternalTexture(pointer);

You need using System; on top of your .cs

3 Likes

Tested on windows 7 unity 4.6 and dx9 and is just instantly crashing (even the whole editor);

I have been working this code with mac, android and ios. I currently don’t have a windows system. Anyone else tried it?

After setting the Pixels with SetPixel() you need to call the Apply() on the destination Texture.

Hi
I

Hi
I have tried as u suggested but image is not getting saved…can u pls explain in detail…
Thanks,
Sudra

8 years later. Another poor lad comes looking for a solution to this very problem.
Never gonna give you up :

Seriously, however.
Here is a solution to converting a webCamTexture over to a texture2D with ease.
I’m not sure on it’s performance speed, but it will function.

public Texture2D Convert_WebCamTexture_To_Texture2d(WebCamTexture _webCamTexture)
        {
        Texture2D _texture2D = new Texture2D(_webCamTexture.width, _webCamTexture.height);
        _texture2D.SetPixels32(_webCamTexture.GetPixels32());

        return _texture2D;
        }

This can further be converted into a byte array, using :
byte[ ] texture_bytes = _texture2D.GetRawTextureData();

5 Likes

This code is working for me.

public Texture2D GetTexture2DFromWebcamTexture(WebCamTexture webCamTexture)
        {
            // Create new texture2d
            Texture2D tx2d = new Texture2D(webCamTexture.width, webCamTexture.height);
            // Gets all color data from web cam texture and then Sets that color data in texture2d
            tx2d.SetPixels(webCamTexture.GetPixels());
            // Applying new changes to texture2d
            tx2d.Apply();
            return tx2d;
        }

This is how you can free texture memory after using it.

 Texture2D tx2d = GetTexture2DFromWebcamTexture(webCamTexture);
  // Once you are done with tx2d destory it to release memory
  Destroy(tx2d);
3 Likes

APPLY the changes to see the result and CLEAR the memory to evade get your pc crash:

        public Texture2D GetTexture2dFromWebcam()
        {
            _CamTexture2D = new Texture2D(_WebcamTexture.width, _WebcamTexture.height);
            _CamTexture2D.SetPixels32(_WebcamTexture.GetPixels32());
            _CamTexture2D.Apply(); //If you want to see the result, you need to apply the changes

            Resources.UnloadUnusedAssets(); //Clean the memory or you will make your pc crash

            return _CamTexture2D;
        }

But it’s not working on Android devices for me :frowning:

Yeah same thing for me on Android. Destroy seems to not work properly on the 2D texture.