using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; namespace ZIM.Unity { public class Texture2DZIM { public Texture2D Texture; public int Width, Height; public Vector2 Size => new Vector2(Width, Height); public Texture2DZIM(int width, int height, TextureFormat textureFormat = TextureFormat.RGB24, bool mipChain = false) { Texture = new Texture2D(width, height, textureFormat, mipChain); Width = width; Height = height; } public Texture2DZIM(Texture2D texture) { Texture = texture; Width = texture.width; Height = texture.height; } public void ReadPixels(RenderTexture rTex) { var old_rt = RenderTexture.active; RenderTexture.active = rTex; Texture.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0); Texture.Apply(); RenderTexture.active = old_rt; } ~Texture2DZIM() { GameObject.Destroy(Texture); } public Color[] GetPixels() => Texture.GetPixels(); public Vector2Int IndexToCoord(int i) { var y = i / Width; var x = i % Width; return new Vector2Int(x, y); } public int CoordToIndex(int x, int y) { return y * Width + x; } public static implicit operator Texture2D(Texture2DZIM i) => i?.Texture; public static implicit operator bool(Texture2DZIM i) => i?.Texture != null; } }