|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using UnityEngine; |
| 6 | +using System.IO; |
| 7 | +using System.Reflection; |
| 8 | +#if CPP |
| 9 | +using Explorer.Unstrip.ImageConversion; |
| 10 | +#endif |
| 11 | + |
| 12 | +namespace Explorer.Helpers |
| 13 | +{ |
| 14 | + public static class Texture2DHelpers |
| 15 | + { |
| 16 | +#if CPP |
| 17 | +#else |
| 18 | + private static bool isNewEncodeMethod = false; |
| 19 | + private static MethodInfo EncodeToPNGMethod => m_encodeToPNGMethod ?? GetEncodeToPNGMethod(); |
| 20 | + private static MethodInfo m_encodeToPNGMethod; |
| 21 | + |
| 22 | + private static MethodInfo GetEncodeToPNGMethod() |
| 23 | + { |
| 24 | + if (ReflectionHelpers.GetTypeByName("UnityEngine.ImageConversion") is Type imageConversion) |
| 25 | + { |
| 26 | + isNewEncodeMethod = true; |
| 27 | + return m_encodeToPNGMethod = imageConversion.GetMethod("EncodeToPNG", ReflectionHelpers.CommonFlags); |
| 28 | + } |
| 29 | + |
| 30 | + var method = typeof(Texture2D).GetMethod("EncodeToPNG", ReflectionHelpers.CommonFlags); |
| 31 | + if (method != null) |
| 32 | + { |
| 33 | + return m_encodeToPNGMethod = method; |
| 34 | + } |
| 35 | + |
| 36 | + ExplorerCore.Log("ERROR: Cannot get any EncodeToPNG method!"); |
| 37 | + return null; |
| 38 | + } |
| 39 | +#endif |
| 40 | + |
| 41 | + |
| 42 | + public static bool IsReadable(this Texture2D tex) |
| 43 | + { |
| 44 | + try |
| 45 | + { |
| 46 | + // This will cause an exception if it's not readable. |
| 47 | + // Reason for doing it this way is not all Unity versions |
| 48 | + // ship with the 'Texture.isReadable' property. |
| 49 | + |
| 50 | + tex.GetPixel(0, 0); |
| 51 | + return true; |
| 52 | + } |
| 53 | + catch |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static Texture2D Copy(Texture2D other, Rect rect, bool isDTXnmNormal = false) |
| 60 | + { |
| 61 | + Color[] pixels; |
| 62 | + |
| 63 | + if (!other.IsReadable()) |
| 64 | + { |
| 65 | + other = ForceReadTexture(other, isDTXnmNormal); |
| 66 | + } |
| 67 | + |
| 68 | + pixels = other.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height); |
| 69 | + |
| 70 | + var _newTex = new Texture2D((int)rect.width, (int)rect.height); |
| 71 | + _newTex.SetPixels(pixels); |
| 72 | + |
| 73 | + return _newTex; |
| 74 | + } |
| 75 | + |
| 76 | + public static Texture2D ForceReadTexture(Texture2D tex, bool isDTXnmNormal = false) |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + var origFilter = tex.filterMode; |
| 81 | + tex.filterMode = FilterMode.Point; |
| 82 | + |
| 83 | + RenderTexture rt = RenderTexture.GetTemporary(tex.width, tex.height, 0, RenderTextureFormat.ARGB32); |
| 84 | + rt.filterMode = FilterMode.Point; |
| 85 | + RenderTexture.active = rt; |
| 86 | + Graphics.Blit(tex, rt); |
| 87 | + |
| 88 | + Texture2D _newTex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); |
| 89 | + _newTex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); |
| 90 | + |
| 91 | + if (isDTXnmNormal) |
| 92 | + { |
| 93 | + _newTex = DTXnmToRGBA(_newTex); |
| 94 | + } |
| 95 | + |
| 96 | + _newTex.Apply(false, false); |
| 97 | + |
| 98 | + RenderTexture.active = null; |
| 99 | + tex.filterMode = origFilter; |
| 100 | + |
| 101 | + return _newTex; |
| 102 | + } |
| 103 | + catch (Exception e) |
| 104 | + { |
| 105 | + ExplorerCore.Log("Exception on ForceReadTexture: " + e.ToString()); |
| 106 | + return default; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static void SaveTextureAsPNG(Texture2D tex, string dir, string name, bool isDTXnmNormal = false) |
| 111 | + { |
| 112 | + if (!Directory.Exists(dir)) |
| 113 | + { |
| 114 | + Directory.CreateDirectory(dir); |
| 115 | + } |
| 116 | + |
| 117 | + byte[] data; |
| 118 | + var savepath = dir + @"\" + name + ".png"; |
| 119 | + |
| 120 | + // Fix for non-Readable or Compressed textures. |
| 121 | + tex = ForceReadTexture(tex, isDTXnmNormal); |
| 122 | + |
| 123 | + if (isDTXnmNormal) |
| 124 | + { |
| 125 | + tex = DTXnmToRGBA(tex); |
| 126 | + tex.Apply(false, false); |
| 127 | + } |
| 128 | + |
| 129 | +#if CPP |
| 130 | + data = tex.EncodeToPNG(); |
| 131 | +#else |
| 132 | + var method = EncodeToPNGMethod; |
| 133 | + |
| 134 | + if (isNewEncodeMethod) |
| 135 | + { |
| 136 | + data = (byte[])method.Invoke(null, new object[] { tex }); |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + data = (byte[])method.Invoke(tex, new object[0]); |
| 141 | + } |
| 142 | +#endif |
| 143 | + |
| 144 | + if (data == null || data.Length < 1) |
| 145 | + { |
| 146 | + ExplorerCore.LogWarning("Couldn't get any data for the texture!"); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | +#if CPP |
| 151 | + // The IL2CPP method will return invalid byte data. |
| 152 | + // However, we can just iterate into safe C# byte[] array. |
| 153 | + byte[] safeData = new byte[data.Length]; |
| 154 | + for (int i = 0; i < data.Length; i++) |
| 155 | + { |
| 156 | + safeData[i] = (byte)data[i]; // not sure if cast is needed |
| 157 | + } |
| 158 | + |
| 159 | + File.WriteAllBytes(savepath, safeData); |
| 160 | +#else |
| 161 | + File.WriteAllBytes(savepath, data); |
| 162 | +#endif |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Converts DTXnm-format Normal Map to RGBA-format Normal Map. |
| 167 | + public static Texture2D DTXnmToRGBA(Texture2D tex) |
| 168 | + { |
| 169 | + Color[] colors = tex.GetPixels(); |
| 170 | + |
| 171 | + for (int i = 0; i < colors.Length; i++) |
| 172 | + { |
| 173 | + Color c = colors[i]; |
| 174 | + |
| 175 | + c.r = c.a * 2 - 1; // red <- alpha |
| 176 | + c.g = c.g * 2 - 1; // green is always the same |
| 177 | + |
| 178 | + Vector2 rg = new Vector2(c.r, c.g); //this is the red-green vector |
| 179 | + c.b = Mathf.Sqrt(1 - Mathf.Clamp01(Vector2.Dot(rg, rg))); //recalculate the blue channel |
| 180 | + |
| 181 | + colors[i] = new Color( |
| 182 | + (c.r * 0.5f) + 0.5f, |
| 183 | + (c.g * 0.5f) + 0.25f, |
| 184 | + (c.b * 0.5f) + 0.5f |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + var newtex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); |
| 189 | + newtex.SetPixels(colors); |
| 190 | + |
| 191 | + return newtex; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments