RealWorldTerrainScalerWindow.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace InfinityCode.RealWorldTerrain.Tools
  7. {
  8. public class RealWorldTerrainScalerWindow : EditorWindow
  9. {
  10. private static RealWorldTerrainScalerWindow wnd;
  11. private RealWorldTerrainContainer item;
  12. private Vector3 scale = Vector3.one;
  13. private Vector3 size = Vector3.one;
  14. private SizeType sizeType = SizeType.relative;
  15. #if !RTP
  16. private bool scaleFirstSP = true;
  17. #endif
  18. private void OnGUI()
  19. {
  20. item = (RealWorldTerrainContainer)EditorGUILayout.ObjectField("Real World Terrain", item, typeof(RealWorldTerrainContainer), true);
  21. if (item == null) return;
  22. sizeType = (SizeType)EditorGUILayout.EnumPopup("Size Type", sizeType);
  23. if (sizeType == SizeType.relative) scale = EditorGUILayout.Vector3Field("Scale", scale);
  24. else size = EditorGUILayout.Vector3Field("Size", size);
  25. if (item.prefs.resultType == RealWorldTerrainResultType.terrain)
  26. {
  27. #if !RTP
  28. scaleFirstSP = EditorGUILayout.Toggle("Scale first SplatPrototype", scaleFirstSP);
  29. #endif
  30. }
  31. if (GUILayout.Button("Apply")) Scale();
  32. }
  33. [MenuItem("Window/Infinity Code/Real World Terrain/Tools/Scaler")]
  34. public static void OpenWindow()
  35. {
  36. OpenWindow(null);
  37. }
  38. public static void OpenWindow(RealWorldTerrainContainer item)
  39. {
  40. wnd = GetWindow<RealWorldTerrainScalerWindow>("Scaler", true);
  41. wnd.item = item;
  42. if (item != null) wnd.size = item.size;
  43. }
  44. private void Scale()
  45. {
  46. if (sizeType == SizeType.relative)
  47. {
  48. if (!ValidateValue(scale, "Scale")) return;
  49. }
  50. else
  51. {
  52. if (!ValidateValue(size, "Size")) return;
  53. }
  54. Vector3 center = item.bounds.center;
  55. Vector3 originalSize = item.bounds.size;
  56. Vector3 newSize = originalSize;
  57. Vector3 pscale;
  58. if (sizeType == SizeType.relative)
  59. {
  60. pscale = scale;
  61. center.Scale(pscale);
  62. newSize.Scale(pscale);
  63. }
  64. else
  65. {
  66. center = item.bounds.min + size / 2;
  67. newSize = size;
  68. pscale = new Vector3(newSize.x / originalSize.x, newSize.y / originalSize.y, newSize.z / originalSize.z);
  69. }
  70. item.bounds = new Bounds(center, newSize);
  71. item.size = newSize;
  72. if (item.prefs.resultType == RealWorldTerrainResultType.mesh)
  73. {
  74. Vector3 localScale = item.transform.localScale;
  75. localScale.Scale(pscale);
  76. item.transform.localScale = localScale;
  77. item.size = newSize;
  78. }
  79. else
  80. {
  81. foreach (RealWorldTerrainItem terrain in item.terrains)
  82. {
  83. Vector3 p = terrain.transform.position;
  84. p.Scale(pscale);
  85. terrain.transform.position = p;
  86. Vector3 s = terrain.terrainData.size;
  87. s.Scale(pscale);
  88. terrain.terrainData.size = s;
  89. terrain.size = s;
  90. terrain.bounds = new Bounds(terrain.transform.position + s / 2, s);
  91. #if !RTP
  92. if (scaleFirstSP && terrain.terrainData.terrainLayers.Length > 0)
  93. {
  94. TerrainLayer[] tls = terrain.terrainData.terrainLayers;
  95. TerrainLayer l = tls[0];
  96. l.tileSize = new Vector2(l.tileSize.x * pscale.x, l.tileSize.y * pscale.z);
  97. l.tileOffset = new Vector2(l.tileOffset.x * pscale.x, l.tileOffset.y * pscale.z);
  98. l.diffuseTexture = l.diffuseTexture;
  99. terrain.terrainData.terrainLayers = tls;
  100. }
  101. #endif
  102. }
  103. }
  104. Close();
  105. }
  106. private bool ValidateValue(Vector3 value, string variableName)
  107. {
  108. if (Math.Abs(value.x) < float.Epsilon || Math.Abs(value.y) < float.Epsilon || Math.Abs(value.z) < float.Epsilon)
  109. {
  110. Debug.LogError(variableName + " failed!!! Value can not be zero.");
  111. return false;
  112. }
  113. if (scale.x < 0 || scale.y < 0 || scale.z < 0)
  114. {
  115. Debug.LogError(variableName + " failed!!! Value can not be lower zero.");
  116. return false;
  117. }
  118. return true;
  119. }
  120. public enum SizeType
  121. {
  122. absolute,
  123. relative
  124. }
  125. }
  126. }