TC_MeasureTerrainsEditor.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using UnityEngine;
  2. using System;
  3. using UnityEditor;
  4. namespace TerrainComposer2
  5. {
  6. [CustomEditor(typeof(TC_MeasureTerrains), true)]
  7. public class TC_MeasureTerrainsEditor : Editor
  8. {
  9. public TC_MeasureTerrains mt;
  10. Ray ray;
  11. Event key;
  12. RaycastHit hit;
  13. void OnEnable()
  14. {
  15. Transform t = ((MonoBehaviour)target).transform;
  16. t.hideFlags = HideFlags.NotEditable | HideFlags.HideInInspector;
  17. }
  18. public void OnSceneGUI()
  19. {
  20. mt = (TC_MeasureTerrains)target;
  21. key = Event.current;
  22. ray = HandleUtility.GUIPointToWorldRay(key.mousePosition);
  23. if (Physics.Raycast(ray, out mt.hit))
  24. {
  25. hit = mt.hit;
  26. mt.terrain = hit.collider.GetComponent<Terrain>();
  27. mt.mr = hit.collider.GetComponent<MeshRenderer>();
  28. Terrain terrain = mt.terrain;
  29. // if (mt.terrain != null) Debug.Log(mt.terrain.name);
  30. if (terrain != null)
  31. {
  32. mt.ReadTerrain();
  33. }
  34. else if (mt.mr != null)
  35. {
  36. mt.localPos = new Vector2(hit.point.x - mt.mr.transform.position.x, hit.point.z - mt.mr.transform.position.z);
  37. }
  38. mt.angle = hit.normal.y * 90;
  39. }
  40. }
  41. public override void OnInspectorGUI()
  42. {
  43. mt = (TC_MeasureTerrains)target;
  44. TD.DrawSpacer();
  45. GUILayout.Space(5.0f);
  46. TD.DrawLabelWidthUnderline("Terrain", 14);
  47. EditorGUILayout.BeginVertical("Box");
  48. GUI.changed = false;
  49. EditorGUILayout.BeginHorizontal();
  50. if (mt.terrain != null)
  51. {
  52. EditorGUILayout.PrefixLabel("Terrain name");
  53. EditorGUILayout.LabelField(mt.terrain.name);
  54. }
  55. else
  56. {
  57. if (mt.mr != null)
  58. {
  59. EditorGUILayout.PrefixLabel("Mesh");
  60. EditorGUILayout.LabelField(mt.mr.name);
  61. }
  62. else
  63. {
  64. EditorGUILayout.LabelField("-", GUILayout.Width(70.0f));
  65. }
  66. }
  67. EditorGUILayout.EndHorizontal();
  68. GUILayout.Space(5.0f);
  69. EditorGUILayout.BeginHorizontal();
  70. EditorGUILayout.PrefixLabel("World Position");
  71. EditorGUILayout.LabelField("X " + hit.point.x.ToString("F2") + " Y " + hit.point.y.ToString("F2") + " Z " + hit.point.z.ToString("F2"));
  72. EditorGUILayout.EndHorizontal();
  73. // GUILayout.Space(5.0f);
  74. EditorGUILayout.BeginHorizontal();
  75. EditorGUILayout.PrefixLabel("Local Position");
  76. EditorGUILayout.LabelField("X " + mt.localPos.x.ToString("F2") + " Y " + mt.localPos.y.ToString("F2"));
  77. EditorGUILayout.EndHorizontal();
  78. GUILayout.Space(5.0f);
  79. // ! inbed locked!
  80. EditorGUILayout.BeginHorizontal();
  81. EditorGUILayout.PrefixLabel("Height");
  82. EditorGUILayout.LabelField(mt.height.ToString("F2") + " (" + mt.normalizedHeight.ToString("F3") + ")");
  83. EditorGUILayout.EndHorizontal();
  84. EditorGUILayout.BeginHorizontal();
  85. EditorGUILayout.PrefixLabel("Angle");
  86. EditorGUILayout.LabelField(mt.angle.ToString("F2") + " (" + hit.normal.y.ToString("F3") + ")");
  87. EditorGUILayout.EndHorizontal();
  88. EditorGUILayout.BeginHorizontal();
  89. EditorGUILayout.PrefixLabel("Normal");
  90. EditorGUILayout.LabelField("X " + hit.normal.x.ToString("F2") + " Z " + hit.normal.z.ToString("F2"));
  91. EditorGUILayout.EndHorizontal();
  92. GUILayout.Space(10.0f);
  93. EditorGUILayout.EndVertical();
  94. GUILayout.Space(5.0f);
  95. EditorGUILayout.BeginHorizontal();
  96. if (mt.drawSplat) GUI.backgroundColor = Color.green;
  97. if (GUILayout.Button("Splat", GUILayout.Width(55))) mt.drawSplat = !mt.drawSplat;
  98. if (mt.drawGrass) GUI.backgroundColor = Color.green; else GUI.backgroundColor = Color.white;
  99. if (GUILayout.Button("Grass", GUILayout.Width(55))) mt.drawGrass = !mt.drawGrass;
  100. GUI.backgroundColor = Color.white;
  101. EditorGUILayout.EndHorizontal();
  102. GUILayout.Space(5);
  103. // TD.DrawSpacer();
  104. EditorGUILayout.BeginHorizontal();
  105. EditorGUILayout.LabelField("Zoom", GUILayout.Width(50));
  106. mt.textureSize = (int)EditorGUILayout.Slider(mt.textureSize, 25, 150);
  107. EditorGUILayout.EndHorizontal();
  108. //GUILayout.Space(5);
  109. TD.DrawSpacer();
  110. if (mt.drawSplat) { DrawSplat(); GUILayout.Space(15); }
  111. if (mt.drawGrass) DrawGrass();
  112. TD.DrawSpacer();
  113. Repaint();
  114. //GUILayout.Space(5.0f);
  115. //float detail_total = 0.0f;
  116. //for (int count_detail = 0; count_detail < detail_length; ++count_detail)
  117. //{
  118. // EditorGUILayout.BeginHorizontal();
  119. // GUILayout.Space(15.0f);
  120. // EditorGUILayout.LabelField("Detail" + count_detail + "", GUILayout.Width(100.0f));
  121. // EditorGUILayout.LabelField("" + detail[count_detail], GUILayout.Width(50.0f));
  122. // detail_total += (float)detail[count_detail];
  123. // GUILayout.Space(74.0f);
  124. // EditorGUILayout.LabelField(locked);
  125. // EditorGUILayout.EndHorizontal();
  126. //}
  127. //GUILayout.Space(5.0f);
  128. //EditorGUILayout.BeginHorizontal();
  129. //GUILayout.Space(15.0f);
  130. //if (global_script.settings.color_scheme) { GUI.color = Color.green; }
  131. //EditorGUILayout.LabelField("Grass Total", GUILayout.Width(100.0f));
  132. //EditorGUILayout.LabelField("" + detail_total.ToString("F2"), GUILayout.Width(50.0f));
  133. //GUILayout.Space(74.0f);
  134. //EditorGUILayout.LabelField(locked);
  135. //EditorGUILayout.EndHorizontal();
  136. //GUILayout.Space(5.0f);
  137. //if (global_script.settings.color_scheme) { GUI.color = Color.white; }
  138. //EditorGUILayout.BeginHorizontal();
  139. //GUILayout.Space(15.0f);
  140. //EditorGUILayout.LabelField("Min Height", GUILayout.Width(100.0f));
  141. //EditorGUILayout.LabelField(script.settings.terrainMinHeight.ToString("F2"), GUILayout.Width(50.0f));
  142. //EditorGUILayout.LabelField("(" + (script.settings.terrainMinHeight / heightmap_scale.y).ToString("F3") + ")", GUILayout.Width(70.0f));
  143. //if (GUILayout.Button("Calc", GUILayout.Width(55.0f)))
  144. //{
  145. // if (script.settings.showTerrains) script.get_terrains_minmax();
  146. // else script.get_meshes_minmax_height();
  147. //}
  148. //EditorGUILayout.EndHorizontal();
  149. //EditorGUILayout.BeginHorizontal();
  150. //GUILayout.Space(15.0f);
  151. //EditorGUILayout.LabelField("Max Height", GUILayout.Width(100.0f));
  152. //EditorGUILayout.LabelField(script.settings.terrainMaxHeight.ToString("F2"), GUILayout.Width(50.0f));
  153. //EditorGUILayout.LabelField("(" + (script.settings.terrainMaxHeight / heightmap_scale.y).ToString("F3") + ")", GUILayout.Width(70.0f));
  154. //EditorGUILayout.EndHorizontal();
  155. //EditorGUILayout.BeginHorizontal();
  156. //GUILayout.Space(15.0f);
  157. //EditorGUILayout.LabelField("Min Steepness", GUILayout.Width(100.0f));
  158. //EditorGUILayout.LabelField(script.settings.terrainMinDegree.ToString("F2"), GUILayout.Width(50.0f));
  159. //EditorGUILayout.LabelField("(" + (script.settings.terrainMinDegree / heightmap_scale.y).ToString("F3") + ")", GUILayout.Width(70.0f));
  160. //EditorGUILayout.EndHorizontal();
  161. //EditorGUILayout.BeginHorizontal();
  162. //GUILayout.Space(15.0f);
  163. //EditorGUILayout.LabelField("Max Steepness", GUILayout.Width(100.0f));
  164. //EditorGUILayout.LabelField(script.settings.terrainMaxDegree.ToString("F2"), GUILayout.Width(50.0f));
  165. //EditorGUILayout.LabelField("(" + (script.settings.terrainMaxDegree / heightmap_scale.y).ToString("F3") + ")", GUILayout.Width(70.0f));
  166. //EditorGUILayout.EndHorizontal();
  167. //GUILayout.Space(5.0f);
  168. //EditorGUILayout.BeginHorizontal();
  169. //GUILayout.Space(15.0f);
  170. //string range_text = null;
  171. //if (script.measure_tool_inrange) { range_text = "*"; } else { range_text = "?"; }
  172. //EditorGUILayout.LabelField("Measure Range", GUILayout.Width(100.0f));
  173. //script.measure_tool_range = EditorGUILayout.Slider(script.measure_tool_range, 1.0f, 100000.0f, GUILayout.Width(300.0f));
  174. //EditorGUILayout.LabelField(range_text, GUILayout.Width(100.0f));
  175. //EditorGUILayout.EndHorizontal();
  176. //EditorGUILayout.BeginHorizontal();
  177. //GUILayout.Space(15.0f);
  178. //EditorGUILayout.LabelField("Sphere Radius", GUILayout.Width(100.0f));
  179. //script.sphere_radius = EditorGUILayout.Slider(script.sphere_radius, 0.1f, 50.0f, GUILayout.Width(300.0f));
  180. //EditorGUILayout.EndHorizontal();
  181. //GUILayout.Space(5.0f);
  182. //EditorGUILayout.BeginHorizontal();
  183. //GUILayout.Space(15.0f);
  184. //EditorGUILayout.LabelField("Sphere Gizmos", GUILayout.Width(100.0f));
  185. //script.sphere_draw = EditorGUILayout.Toggle(script.sphere_draw, GUILayout.Width(25.0f));
  186. //EditorGUILayout.EndHorizontal();
  187. //GUILayout.Space(5.0f);
  188. //EditorGUILayout.BeginHorizontal();
  189. //GUILayout.Space(15.0f);
  190. //EditorGUILayout.LabelField("Converter Calculator");
  191. //EditorGUILayout.EndHorizontal();
  192. //EditorGUILayout.BeginHorizontal();
  193. //GUILayout.Space(15.0f);
  194. //EditorGUILayout.LabelField("Height", GUILayout.Width(100.0f));
  195. //GUI.changed = false;
  196. //script.measure_tool_converter_height_input = EditorGUILayout.FloatField(script.measure_tool_converter_height_input, GUILayout.Width(80.0f));
  197. //if (GUI.changed)
  198. //{
  199. // if (terrain_measure != null) { script.measure_tool_converter_height = script.measure_tool_converter_height_input / terrain_measure.terrainData.size.y; }
  200. //}
  201. //EditorGUILayout.LabelField("-> " + script.measure_tool_converter_height.ToString("f3"));
  202. //EditorGUILayout.EndHorizontal();
  203. //EditorGUILayout.BeginHorizontal();
  204. //GUILayout.Space(15.0f);
  205. //EditorGUILayout.LabelField("Angle", GUILayout.Width(100.0f));
  206. //GUI.changed = false;
  207. //script.measure_tool_converter_angle_input = EditorGUILayout.FloatField(script.measure_tool_converter_angle_input, GUILayout.Width(80.0f));
  208. //if (GUI.changed)
  209. //{
  210. // script.measure_tool_converter_angle = script.measure_tool_converter_angle_input / 90;
  211. //}
  212. //EditorGUILayout.LabelField("-> " + script.measure_tool_converter_angle.ToString("f3"));
  213. //EditorGUILayout.EndHorizontal();
  214. //if (script.measure_tool_active && !script.measure_tool_undock) { this.Repaint(); }
  215. }
  216. void DrawSplat()
  217. {
  218. if (mt.terrain != null && mt.splat != null)
  219. {
  220. TD.DrawLabelWidthUnderline("Splat", 14);
  221. GUI.color = Color.red * TD.editorSkinMulti;
  222. EditorGUILayout.BeginVertical("Box");
  223. GUI.color = Color.white;
  224. float splatTotal = 0;
  225. for (int i = 0; i < mt.splat.Length; ++i)
  226. {
  227. EditorGUILayout.BeginHorizontal();
  228. TC_ItemBehaviourEditor.DrawPreviewTexture(mt.terrain.terrainData.splatPrototypes[i].texture, Color.white, Color.white, mt.textureSize, mt.textureSize);
  229. float splatValue = mt.splat[0, 0, i];
  230. EditorGUILayout.LabelField("");
  231. Rect rect = GUILayoutUtility.GetLastRect();
  232. EditorGUI.ProgressBar(rect, splatValue, splatValue.ToString("F2"));
  233. splatTotal += splatValue;
  234. EditorGUILayout.EndHorizontal();
  235. }
  236. GUILayout.Space(5.0f);
  237. GUI.color = Color.yellow * TD.editorSkinMulti;
  238. EditorGUILayout.BeginVertical("Box");
  239. GUI.color = Color.white;
  240. EditorGUILayout.BeginHorizontal();
  241. EditorGUILayout.PrefixLabel("Splat Total");
  242. EditorGUILayout.LabelField(splatTotal.ToString("F3"));
  243. EditorGUILayout.EndHorizontal();
  244. EditorGUILayout.EndVertical();
  245. EditorGUILayout.EndVertical();
  246. }
  247. }
  248. void DrawGrass()
  249. {
  250. if (mt.terrain != null && mt.grassLayer != null)
  251. {
  252. TD.DrawLabelWidthUnderline("Grass", 14);
  253. GUI.color = Color.green * TD.editorSkinMulti;
  254. EditorGUILayout.BeginVertical("Box");
  255. GUI.color = Color.white;
  256. float grassTotal = 0;
  257. for (int i = 0; i < mt.grassLayer.Length; ++i)
  258. {
  259. EditorGUILayout.BeginHorizontal();
  260. TC_ItemBehaviourEditor.DrawPreviewTexture(mt.terrain.terrainData.detailPrototypes[i].prototypeTexture, Color.white, Color.white, mt.textureSize, mt.textureSize);
  261. float grassValue = mt.grassLayer[i].grass[0, 0];
  262. EditorGUILayout.LabelField("");
  263. Rect rect = GUILayoutUtility.GetLastRect();
  264. EditorGUI.ProgressBar(rect, grassValue / 16, grassValue.ToString());
  265. grassTotal += grassValue;
  266. EditorGUILayout.EndHorizontal();
  267. }
  268. GUILayout.Space(5.0f);
  269. GUI.color = Color.yellow * TD.editorSkinMulti;
  270. EditorGUILayout.BeginVertical("Box");
  271. GUI.color = Color.white;
  272. EditorGUILayout.BeginHorizontal();
  273. EditorGUILayout.PrefixLabel("Grass Total");
  274. EditorGUILayout.LabelField(grassTotal.ToString("F3"));
  275. EditorGUILayout.EndHorizontal();
  276. EditorGUILayout.EndVertical();
  277. EditorGUILayout.EndVertical();
  278. }
  279. }
  280. }
  281. }