Texture2DUtils.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace TriLib
  5. {
  6. public static class Texture2DUtils
  7. {
  8. public static Texture2D LoadTextureFromFile(IntPtr scene, string path, string name, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, string basePath = null, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, string textureFileNameWithoutExtension = null, bool isNormalMap = false)
  9. {
  10. if (scene == IntPtr.Zero || string.IsNullOrEmpty(path))
  11. {
  12. return null;
  13. }
  14. int width = 0;
  15. int height = 0;
  16. string text;
  17. byte[] array;
  18. bool isRawData;
  19. if (!Texture2DUtils.LoadEmbeddedTextureData(scene, path, out text, out array, out isRawData, out width, out height))
  20. {
  21. string text2 = null;
  22. text = path;
  23. array = FileUtils.LoadFileData(text);
  24. if (array.Length == 0 && basePath != null)
  25. {
  26. text = Path.Combine(basePath, path);
  27. array = FileUtils.LoadFileData(text);
  28. }
  29. if (array.Length == 0)
  30. {
  31. text2 = FileUtils.GetFilename(path);
  32. text = text2;
  33. array = FileUtils.LoadFileData(text);
  34. }
  35. if (array.Length == 0 && basePath != null && text2 != null)
  36. {
  37. text = Path.Combine(basePath, text2);
  38. array = FileUtils.LoadFileData(text);
  39. }
  40. if (array.Length == 0)
  41. {
  42. return null;
  43. }
  44. }
  45. Texture2D tempTexture2D;
  46. if (Texture2DUtils.ApplyTextureData(array, isRawData, out tempTexture2D, width, height))
  47. {
  48. return Texture2DUtils.ProccessTextureData(tempTexture2D, name, material, propertyName, ref checkAlphaChannel, textureWrapMode, text, onTextureLoaded, textureCompression, isNormalMap);
  49. }
  50. return null;
  51. }
  52. public static Texture2D LoadTextureFromMemory(byte[] data, string path, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, bool isNormalMap = false, bool isRawData = false, int width = 0, int height = 0)
  53. {
  54. if (data.Length == 0 || string.IsNullOrEmpty(path))
  55. {
  56. return null;
  57. }
  58. Texture2D tempTexture2D;
  59. if (Texture2DUtils.ApplyTextureData(data, isRawData, out tempTexture2D, width, height))
  60. {
  61. return Texture2DUtils.ProccessTextureData(tempTexture2D, StringUtils.GenerateUniqueName(path.GetHashCode()), material, propertyName, ref checkAlphaChannel, textureWrapMode, null, onTextureLoaded, textureCompression, isNormalMap);
  62. }
  63. return null;
  64. }
  65. public static bool ApplyTextureData(byte[] data, bool isRawData, out Texture2D tempTexture2D, int width = 0, int height = 0)
  66. {
  67. if (data.Length == 0)
  68. {
  69. tempTexture2D = null;
  70. return false;
  71. }
  72. if (isRawData)
  73. {
  74. try
  75. {
  76. tempTexture2D = new Texture2D(width, height, TextureFormat.ARGB32, true);
  77. tempTexture2D.LoadRawTextureData(data);
  78. tempTexture2D.Apply();
  79. return true;
  80. }
  81. catch
  82. {
  83. }
  84. }
  85. tempTexture2D = new Texture2D(2, 2, TextureFormat.RGBA32, true);
  86. return tempTexture2D.LoadImage(data);
  87. }
  88. public static bool LoadEmbeddedTextureData(IntPtr scene, string path, out string finalPath, out byte[] data, out bool isRawData, out int width, out int height)
  89. {
  90. if (scene != IntPtr.Zero && !string.IsNullOrEmpty(path))
  91. {
  92. IntPtr intPtr = AssimpInterop.aiScene_GetEmbeddedTexture(scene, path);
  93. if (intPtr != IntPtr.Zero)
  94. {
  95. isRawData = !AssimpInterop.aiMaterial_IsEmbeddedTextureCompressed(scene, intPtr);
  96. uint uintSize = AssimpInterop.aiMaterial_GetEmbeddedTextureDataSize(scene, intPtr, !isRawData);
  97. data = AssimpInterop.aiMaterial_GetEmbeddedTextureData(scene, intPtr, uintSize);
  98. width = AssimpInterop.aiMaterial_GetEmbeddedTextureWidth(intPtr);
  99. height = AssimpInterop.aiMaterial_GetEmbeddedTextureHeight(intPtr);
  100. finalPath = Path.GetFileNameWithoutExtension(path);
  101. return true;
  102. }
  103. }
  104. finalPath = null;
  105. data = new byte[0];
  106. isRawData = false;
  107. width = 0;
  108. height = 0;
  109. return false;
  110. }
  111. public static Texture2D ProccessTextureData(Texture2D tempTexture2D, string name, Material material, string propertyName, ref bool checkAlphaChannel, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat, string finalPath = null, TextureLoadHandle onTextureLoaded = null, TextureCompression textureCompression = TextureCompression.None, bool isNormalMap = false)
  112. {
  113. if (tempTexture2D == null)
  114. {
  115. return null;
  116. }
  117. tempTexture2D.name = name;
  118. tempTexture2D.wrapMode = textureWrapMode;
  119. Color32[] pixels = tempTexture2D.GetPixels32();
  120. Texture2D texture2D;
  121. if (isNormalMap)
  122. {
  123. texture2D = new Texture2D(tempTexture2D.width, tempTexture2D.height, TextureFormat.ARGB32, true);
  124. for (int i = 0; i < pixels.Length; i++)
  125. {
  126. Color32 color = pixels[i];
  127. color.a = color.r;
  128. color.r = 0;
  129. color.b = 0;
  130. pixels[i] = color;
  131. }
  132. texture2D.SetPixels32(pixels);
  133. texture2D.Apply();
  134. }
  135. else
  136. {
  137. texture2D = new Texture2D(tempTexture2D.width, tempTexture2D.height, TextureFormat.ARGB32, true);
  138. texture2D.SetPixels32(pixels);
  139. texture2D.Apply();
  140. if (textureCompression != TextureCompression.None)
  141. {
  142. tempTexture2D.Compress(textureCompression == TextureCompression.HighQuality);
  143. }
  144. }
  145. if (checkAlphaChannel)
  146. {
  147. checkAlphaChannel = false;
  148. foreach (Color32 color2 in pixels)
  149. {
  150. if (color2.a != 255)
  151. {
  152. checkAlphaChannel = true;
  153. break;
  154. }
  155. }
  156. }
  157. if (material != null)
  158. {
  159. material.SetTexture(propertyName, texture2D);
  160. }
  161. if (onTextureLoaded != null)
  162. {
  163. onTextureLoaded(finalPath, material, propertyName, texture2D);
  164. }
  165. return texture2D;
  166. }
  167. public static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
  168. {
  169. Texture2D texture2D = new Texture2D(targetWidth, targetHeight, source.format, true);
  170. Color[] pixels = texture2D.GetPixels(0);
  171. float num = 1f / (float)targetWidth;
  172. float num2 = 1f / (float)targetHeight;
  173. for (int i = 0; i < pixels.Length; i++)
  174. {
  175. pixels[i] = source.GetPixelBilinear(num * ((float)i % (float)targetWidth), num2 * Mathf.Floor((float)(i / targetWidth)));
  176. }
  177. texture2D.SetPixels(pixels, 0);
  178. texture2D.Apply();
  179. return texture2D;
  180. }
  181. public static bool IsPowerOfTwo(float number)
  182. {
  183. float f = Mathf.Log(number, 2f);
  184. float num = Mathf.Pow(2f, Mathf.Round(f));
  185. return num == number;
  186. }
  187. }
  188. }