Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

WebCamTexture -> Texture2D?

Discussion in 'iOS and tvOS' started by DeepShader, Oct 8, 2012.

  1. DeepShader

    DeepShader

    Joined:
    May 29, 2009
    Posts:
    682
    Hi there,

    if I apply a WebCamTexture to such as a Cube via
    Code (csharp):
    1. cube.renderer.material.mainTexture = wct;
    Why I can't read out this texture via
    Code (csharp):
    1. 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!!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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.
     
  3. DeepShader

    DeepShader

    Joined:
    May 29, 2009
    Posts:
    682
    Thank you for this information. I thought .mainTexture would be automatically convert to Texture2D with
    Code (csharp):
    1. [...].mainTexture as Texture2D).GetPixels[...]
    So, you said I've to use ReadPixels. This is what I've tried before I've posted this thread:

    Code (csharp):
    1. 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?
     
  4. DeepShader

    DeepShader

    Joined:
    May 29, 2009
    Posts:
    682
    Another user in another post find the right solution for this problem:

    Code (csharp):
    1. tx2d.SetPixels((go.renderer.material.mainTexture as WebCamTexture).GetPixels());

    Special thanks to mgear!!!
     
    Fenikkel likes this.
  5. pixel_fiend

    pixel_fiend

    Joined:
    Jun 16, 2014
    Posts:
    4
    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().
     
    sergei201613, Fenikkel and bariscigal like this.
  6. Marald

    Marald

    Joined:
    Jan 16, 2015
    Posts:
    42
    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);
     
  7. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    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!
    :D
     
  8. bariscigal

    bariscigal

    Joined:
    Oct 26, 2009
    Posts:
    46
    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
     
    Fenikkel, talyh and JonasLuz like this.
  9. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Tested on windows 7 unity 4.6 and dx9 and is just instantly crashing (even the whole editor);
     
  10. bariscigal

    bariscigal

    Joined:
    Oct 26, 2009
    Posts:
    46
    I have been working this code with mac, android and ios. I currently don't have a windows system. Anyone else tried it?
     
  11. martinhodler

    martinhodler

    Joined:
    Feb 10, 2014
    Posts:
    3
    After setting the Pixels with SetPixel() you need to call the Apply() on the destination Texture.
     
  12. sudha

    sudha

    Joined:
    Nov 19, 2012
    Posts:
    2
    Hi
    I


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

    Lapsapnow

    Joined:
    Jul 4, 2019
    Posts:
    51
    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.

    Code (CSharp):
    1.  
    2. public Texture2D Convert_WebCamTexture_To_Texture2d(WebCamTexture _webCamTexture)
    3.         {
    4.         Texture2D _texture2D = new Texture2D(_webCamTexture.width, _webCamTexture.height);
    5.         _texture2D.SetPixels32(_webCamTexture.GetPixels32());
    6.  
    7.         return _texture2D;
    8.         }
    9.  
    This can further be converted into a byte array, using :
    byte[] texture_bytes = _texture2D.GetRawTextureData();
     
  14. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    This code is working for me.

    Code (csharp):
    1.  
    2. public Texture2D GetTexture2DFromWebcamTexture(WebCamTexture webCamTexture)
    3.         {
    4.             // Create new texture2d
    5.             Texture2D tx2d = new Texture2D(webCamTexture.width, webCamTexture.height);
    6.             // Gets all color data from web cam texture and then Sets that color data in texture2d
    7.             tx2d.SetPixels(webCamTexture.GetPixels());
    8.             // Applying new changes to texture2d
    9.             tx2d.Apply();
    10.             return tx2d;
    11.         }
    12.  
    This is how you can free texture memory after using it.
    Code (csharp):
    1.  
    2.  Texture2D tx2d = GetTexture2DFromWebcamTexture(webCamTexture);
    3.   // Once you are done with tx2d destory it to release memory
    4.   Destroy(tx2d);
    5.  
     
    Last edited: Oct 25, 2021
    Mr-Citizen, DMTSource and Fenikkel like this.
  15. Fenikkel

    Fenikkel

    Joined:
    Sep 24, 2019
    Posts:
    20
    APPLY the changes to see the result and CLEAR the memory to evade get your pc crash:

    Code (CSharp):
    1.         public Texture2D GetTexture2dFromWebcam()
    2.         {
    3.             _CamTexture2D = new Texture2D(_WebcamTexture.width, _WebcamTexture.height);
    4.             _CamTexture2D.SetPixels32(_WebcamTexture.GetPixels32());
    5.             _CamTexture2D.Apply(); //If you want to see the result, you need to apply the changes
    6.  
    7.             Resources.UnloadUnusedAssets(); //Clean the memory or you will make your pc crash
    8.  
    9.             return _CamTexture2D;
    10.         }
    But it's not working on Android devices for me :(
     
  16. smasson99

    smasson99

    Joined:
    Oct 1, 2017
    Posts:
    10
    Yeah same thing for me on Android. Destroy seems to not work properly on the 2D texture.