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!)?
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.
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().
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…
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!
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();
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);
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;
}