RealWorldTerrainHUEWindow.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace InfinityCode.RealWorldTerrain.Tools
  7. {
  8. public class RealWorldTerrainHUEWindow : Base.RealWorldTerrainBaseContainerTool
  9. {
  10. private int brightness;
  11. private int contrast;
  12. private int tone;
  13. private int saturation;
  14. protected override void Apply()
  15. {
  16. int tick = 0;
  17. int sx = countX;
  18. int sy = countY;
  19. for (int ty = 0; ty < sy; ty++)
  20. {
  21. for (int tx = 0; tx < sx; tx++)
  22. {
  23. int tIndex = ty * sx + tx;
  24. Texture2D texture = terrains[tIndex].texture;
  25. if (texture == null) continue;
  26. Color[] colors = texture.GetPixels();
  27. int cl = colors.Length;
  28. for (int i = 0; i < cl; i++)
  29. {
  30. tick++;
  31. if (tick >= 1000)
  32. {
  33. float progress = ((ty * sx + tx) * cl + i) / (float)(sx * sy * cl);
  34. EditorUtility.DisplayProgressBar("Apply Brightness, Contrast and HUE", Mathf.RoundToInt(progress * 100) + "%", progress);
  35. tick = 0;
  36. }
  37. colors[i] = ApplyFilters(colors[i]);
  38. }
  39. Texture2D newTexture = new Texture2D(texture.width, texture.height);
  40. newTexture.SetPixels(colors);
  41. newTexture.Apply();
  42. string path = AssetDatabase.GetAssetPath(texture);
  43. File.WriteAllBytes(path, newTexture.EncodeToPNG());
  44. }
  45. }
  46. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  47. Close();
  48. EditorUtility.ClearProgressBar();
  49. }
  50. protected override Color ApplyFilters(Color color)
  51. {
  52. if (brightness < 0) color = Color.Lerp(color, Color.black, (float)brightness / -150);
  53. else if (brightness > 0) color = Color.Lerp(color, Color.white, (float)brightness / 150);
  54. if (contrast != 0)
  55. {
  56. float c = Mathf.Pow((contrast + 100f) / 100, 2);
  57. float r = (color.r - 0.5f) * c + 0.5f;
  58. float g = (color.g - 0.5f) * c + 0.5f;
  59. float b = (color.b - 0.5f) * c + 0.5f;
  60. color.r = Mathf.Clamp01(r);
  61. color.g = Mathf.Clamp01(g);
  62. color.b = Mathf.Clamp01(b);
  63. }
  64. if (tone != 0)
  65. {
  66. float t = tone >= 0 ? tone : tone + 360;
  67. float rt = t / 120;
  68. rt = rt - (int)rt;
  69. float r = color.r;
  70. float g = color.g;
  71. float b = color.b;
  72. float nr, ng, nb;
  73. if (t < 120)
  74. {
  75. nr = Mathf.Lerp(r, b, rt);
  76. ng = Mathf.Lerp(g, r, rt);
  77. nb = Mathf.Lerp(b, g, rt);
  78. }
  79. else if (t < 240)
  80. {
  81. nr = Mathf.Lerp(b, g, rt);
  82. ng = Mathf.Lerp(r, b, rt);
  83. nb = Mathf.Lerp(g, r, rt);
  84. }
  85. else
  86. {
  87. nr = Mathf.Lerp(g, r, rt);
  88. ng = Mathf.Lerp(b, g, rt);
  89. nb = Mathf.Lerp(r, b, rt);
  90. }
  91. color.r = nr;
  92. color.g = ng;
  93. color.b = nb;
  94. }
  95. if (saturation != 0)
  96. {
  97. float s = Mathf.Pow((saturation + 100f) / 100, 2);
  98. float averageColor = color.grayscale;
  99. float difR = color.r - averageColor;
  100. float difG = color.g - averageColor;
  101. float difB = color.b - averageColor;
  102. color.r = averageColor + difR * s;
  103. color.g = averageColor + difG * s;
  104. color.b = averageColor + difB * s;
  105. }
  106. return color;
  107. }
  108. protected override void OnContentGUI()
  109. {
  110. EditorGUI.BeginChangeCheck();
  111. bool reset = false;
  112. EditorGUILayout.BeginHorizontal();
  113. brightness = EditorGUILayout.IntSlider("Brightness", brightness, -150, 150);
  114. if (GUILayout.Button("Reset", GUILayout.ExpandWidth(false)))
  115. {
  116. brightness = 0;
  117. reset = true;
  118. }
  119. EditorGUILayout.EndHorizontal();
  120. EditorGUILayout.BeginHorizontal();
  121. contrast = EditorGUILayout.IntSlider("Contrast", contrast, -100, 100);
  122. if (GUILayout.Button("Reset", GUILayout.ExpandWidth(false)))
  123. {
  124. contrast = 0;
  125. reset = true;
  126. }
  127. EditorGUILayout.EndHorizontal();
  128. EditorGUILayout.BeginHorizontal();
  129. tone = EditorGUILayout.IntSlider("Tone", tone, -180, 180);
  130. if (GUILayout.Button("Reset", GUILayout.ExpandWidth(false)))
  131. {
  132. tone = 0;
  133. reset = true;
  134. }
  135. EditorGUILayout.EndHorizontal();
  136. EditorGUILayout.BeginHorizontal();
  137. saturation = EditorGUILayout.IntSlider("Saturation", saturation, -100, 100);
  138. if (GUILayout.Button("Reset", GUILayout.ExpandWidth(false)))
  139. {
  140. saturation = 0;
  141. reset = true;
  142. }
  143. EditorGUILayout.EndHorizontal();
  144. if (EditorGUI.EndChangeCheck() || reset) UpdatePreview();
  145. if (image != null && preview != null)
  146. {
  147. int width = (int)position.width;
  148. GUILayout.Box("", GUILayout.Height(width / 2), GUILayout.ExpandWidth(true));
  149. Rect lastRect = GUILayoutUtility.GetLastRect();
  150. if (Event.current.type != EventType.Layout)
  151. {
  152. float imgWidth = lastRect.width / 2 - 10;
  153. EditorGUI.DrawPreviewTexture(new Rect(lastRect.x + 5, lastRect.y + 5, imgWidth, lastRect.height - 10), image, null, ScaleMode.ScaleToFit);
  154. EditorGUI.DrawPreviewTexture(new Rect(lastRect.x + 5 + lastRect.width / 2, lastRect.y + 5, imgWidth, lastRect.height - 10), preview, null, ScaleMode.ScaleToFit);
  155. }
  156. }
  157. }
  158. protected override void UpdatePreview()
  159. {
  160. for (int i = 0; i < originalColors.Length; i++) previewColors[i] = ApplyFilters(originalColors[i]);
  161. preview.SetPixels(previewColors);
  162. preview.Apply();
  163. }
  164. public static void OpenWindow(RealWorldTerrainMonoBase item)
  165. {
  166. if (wnd != null) wnd.Close();
  167. wnd = GetWindow<RealWorldTerrainHUEWindow>(true, "Brightness, Contrast and HUE", true);
  168. wnd.item = item;
  169. wnd.GetImage();
  170. }
  171. }
  172. }