RTPMethods.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace WorldComposer
  4. {
  5. public class RTPMethods
  6. {
  7. public static Texture2D PrepareHeights(int num, int numLayers, Texture2D[] Heights)
  8. {
  9. // ReliefTerrain _targetRT=(ReliefTerrain)target;
  10. // ReliefTerrainGlobalSettingsHolder _target=_targetRT.globalSettingsHolder;
  11. // Texture2D[] Heights=_target.Heights;
  12. if (Heights == null) return null;
  13. for (int n = 0; n < Heights.Length; n++)
  14. {
  15. int min_size = 9999;
  16. for (int m = (n / 4) * 4; (m < ((n / 4) * 4 + 4)) && (m < Heights.Length); m++)
  17. {
  18. if (Heights[m])
  19. {
  20. if (Heights[m].width < min_size) min_size = Heights[m].width;
  21. }
  22. }
  23. /*
  24. AssetImporter _importer=AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(Heights[n]));
  25. if (_importer) {
  26. TextureImporter tex_importer=(TextureImporter)_importer;
  27. bool reimport_flag=false;
  28. if (!tex_importer.isReadable) {
  29. Debug.LogWarning("Height texture "+n+" ("+Heights[n].name+") has been reimported as readable.");
  30. tex_importer.isReadable=true;
  31. reimport_flag=true;
  32. }
  33. if (!tex_importer.DoesSourceTextureHaveAlpha() && !tex_importer.grayscaleToAlpha) {
  34. Debug.LogWarning("Height texture "+n+" ("+Heights[n].name+") has been reimported to have alpha channel.");
  35. tex_importer.grayscaleToAlpha=true;
  36. tex_importer.textureFormat=TextureImporterFormat.Alpha8;
  37. reimport_flag=true;
  38. }
  39. if (Heights[n] && Heights[n].width>min_size) {
  40. Debug.LogWarning("Height texture "+n+" ("+Heights[n].name+") has been reimported with "+min_size+" size.");
  41. tex_importer.maxTextureSize=min_size;
  42. reimport_flag=true;
  43. }
  44. if (reimport_flag) {
  45. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Heights[n]), ImportAssetOptions.ForceUpdate);
  46. }
  47. }
  48. */
  49. }
  50. Texture2D[] heights = new Texture2D[4];
  51. int i;
  52. int _w = 256;
  53. // int _w_idx=0;
  54. int len = numLayers < 12 ? numLayers : 12;
  55. if (num >= len) return null;
  56. num = (num >> 2) * 4;
  57. for (i = num; i < num + 4; i++)
  58. {
  59. if (num < 4)
  60. {
  61. heights[i] = i < Heights.Length ? Heights[i] : null;
  62. if (heights[i])
  63. {
  64. _w = heights[i].width;
  65. // _w_idx=i;
  66. }
  67. }
  68. else if (num < 8)
  69. {
  70. heights[i - 4] = i < Heights.Length ? Heights[i] : null;
  71. if (heights[i - 4])
  72. {
  73. _w = heights[i - 4].width;
  74. // _w_idx=i;
  75. }
  76. }
  77. else
  78. {
  79. heights[i - 8] = i < Heights.Length ? Heights[i] : null;
  80. if (heights[i - 8])
  81. {
  82. _w = heights[i - 8].width;
  83. // _w_idx=i;
  84. }
  85. }
  86. }
  87. for (i = 0; i < 4; i++)
  88. {
  89. if (!heights[i])
  90. {
  91. heights[i] = new Texture2D(_w, _w);
  92. FillTex(heights[i], new Color32(255, 255, 255, 255));
  93. }
  94. }
  95. /*
  96. if (heights[i]) {
  97. try {
  98. heights[i].GetPixels(0,0,4,4,0);
  99. } catch (Exception e) {
  100. Debug.LogError("Height texture "+i+" has to be marked as isReadable...");
  101. Debug.LogError(e.Message);
  102. // _target.activateObject=heights[i];
  103. return false;
  104. }
  105. if (heights[i].width!=_w) {
  106. Debug.LogError("Height textures should have the same size ! (check layer "+_w_idx+" and "+(num+i)+")");
  107. // _target.activateObject=heights[i];
  108. return false;
  109. }
  110. }
  111. // else {
  112. if (heights[i] == null) {
  113. heights[i]=new Texture2D(_w, _w);
  114. FillTex(heights[i], new Color32(255,255,255,255));
  115. }
  116. //}
  117. }
  118. */
  119. return CombineHeights(heights[0], heights[1], heights[2], heights[3]);
  120. /*
  121. if (num<4) {
  122. _target.HeightMap=CombineHeights(heights[0], heights[1], heights[2], heights[3]);
  123. } else if (num<8) {
  124. _target.HeightMap2=CombineHeights(heights[0], heights[1], heights[2], heights[3]);
  125. } else if (num<12) {
  126. _target.HeightMap3=CombineHeights(heights[0], heights[1], heights[2], heights[3]);
  127. }
  128. return true;
  129. */
  130. }
  131. public static Texture2D CombineHeights(Texture2D source_tex0, Texture2D source_tex1, Texture2D source_tex2, Texture2D source_tex3)
  132. {
  133. Texture2D rendered_tex = new Texture2D(source_tex0.width, source_tex0.height, TextureFormat.ARGB32, true, true);
  134. byte[] colsR = get_alpha_channel(source_tex0);
  135. byte[] colsG = get_alpha_channel(source_tex1);
  136. byte[] colsB = get_alpha_channel(source_tex2);
  137. byte[] colsA = get_alpha_channel(source_tex3);
  138. Color32[] cols = rendered_tex.GetPixels32();
  139. for (int i = 0; i < cols.Length; i++)
  140. {
  141. cols[i].r = colsR[i];
  142. cols[i].g = colsG[i];
  143. cols[i].b = colsB[i];
  144. cols[i].a = colsA[i];
  145. }
  146. rendered_tex.SetPixels32(cols);
  147. rendered_tex.Apply(true, false);
  148. rendered_tex.Compress(true);
  149. //rendered_tex.Apply(true, true); // (non readable robione przy publishingu)
  150. rendered_tex.filterMode = FilterMode.Trilinear;
  151. return rendered_tex;
  152. }
  153. static byte[] get_alpha_channel(Texture2D source_tex)
  154. {
  155. Color32[] cols = source_tex.GetPixels32();
  156. byte[] ret = new byte[cols.Length];
  157. for (int i = 0; i < cols.Length; i++) ret[i] = cols[i].a;
  158. return ret;
  159. }
  160. static void FillTex(Texture2D tex, Color32 col)
  161. {
  162. Color32[] cols = tex.GetPixels32();
  163. for (int i = 0; i < cols.Length; i++)
  164. {
  165. cols[i].r = col.r;
  166. cols[i].g = col.g;
  167. cols[i].b = col.b;
  168. cols[i].a = col.a;
  169. }
  170. tex.SetPixels32(cols);
  171. }
  172. }
  173. }