Skip to content

Instantly share code, notes, and snippets.

@fenderrex
Created January 16, 2023 08:09
Show Gist options
  • Save fenderrex/3b0db75771a009d496cd622d01d41cd7 to your computer and use it in GitHub Desktop.
Save fenderrex/3b0db75771a009d496cd622d01d41cd7 to your computer and use it in GitHub Desktop.
turns a texture object into a 2DTexture object
/// <summary>
/// turns a texture object into a 2DTexture object
/// </summary>
/// <param name="texture"> inputTexture </param>
/// <returns>a 2DTexture from the texture object</returns>
///<example>
/// This shows why you might need it. https://github.com/fenderrex
/// <code>
/// RawTexture rawTexture = GetComponent<RawImage>();
/// texture2D valueOut= TextureToDepth(rawTexture.texture);
///
/// </code>
///</example>
public static Texture2D TextureToDepth(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height);
RenderTexture tempRT = new RenderTexture(texture.width, texture.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
RenderTexture.active = tempRT;
Graphics.Blit(texture, tempRT);
texture2D.ReadPixels(new Rect(0, 0, tempRT.width, tempRT.height), 0, 0);
texture2D.Apply();
//RenderTexture.active = null;
//tempRT.Release();
return texture2D;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment