TransparencyTool.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // River Modeler
  2. // Staggart Creations (http://staggart.xyz)
  3. // Copyright protected under Unity Asset Store EULA
  4. // Copying or referencing source code for the production of new asset store content is strictly prohibited.
  5. using UnityEngine;
  6. using UnityEditor;
  7. using sc.modeling.river.runtime;
  8. using UnityEditor.EditorTools;
  9. #if SPLINES
  10. using UnityEditor.Splines;
  11. using UnityEngine.Splines;
  12. #endif
  13. #if MATHEMATICS
  14. using Unity.Mathematics;
  15. #endif
  16. namespace sc.modeling.river.editor
  17. {
  18. #if SPLINES && MATHEMATICS
  19. [EditorTool(NAME, typeof(RiverModeler))]
  20. public class TransparencyTool : RiverAttributeTool<float>, IDrawSelectedHandles
  21. {
  22. private const string NAME = "River Transparency Tool";
  23. public const string IconName = "river-transparency-tool-64px";
  24. void OnEnable()
  25. {
  26. name = NAME;
  27. m_IconContent = new GUIContent()
  28. {
  29. image = Resources.Load<Texture2D>(IconName),
  30. text = "Transparency Tool",
  31. tooltip = "Adjust the opacity/transparency data of the created river mesh."
  32. };
  33. changeFlag = RiverModeler.ChangeFlags.Transparency | RiverModeler.ChangeFlags.Data;
  34. }
  35. public override void OnToolGUI(EditorWindow window)
  36. {
  37. RiverModeler modeler = target as RiverModeler;
  38. if (modeler == null || modeler.splineContainer == null)
  39. return;
  40. base.OnToolGUI(window);
  41. Handles.color = Color.gray;
  42. m_DisableHandles = false;
  43. var splines = modeler.splineContainer.Splines;
  44. for (var i = 0; i < splines.Count; i++)
  45. {
  46. if (i < modeler.TransparencyData.Count)
  47. {
  48. var nativeSpline = new NativeSpline(splines[i], modeler.splineContainer.transform.localToWorldMatrix);
  49. Undo.RecordObject(modeler, "Modifying River Transparency");
  50. // User defined handles to manipulate width
  51. DrawDataPoints(nativeSpline, modeler.TransparencyData[i]);
  52. nativeSpline.DataPointHandles(modeler.TransparencyData[i], true, i);
  53. if (GUI.changed)
  54. {
  55. modeler.Rebuild(changeFlag);
  56. }
  57. }
  58. }
  59. }
  60. public void OnDrawHandles()
  61. {
  62. if (Asset.Preferences.ShowToolPoints == false) return;
  63. RiverModeler modeler = target as RiverModeler;
  64. if (ToolManager.IsActiveTool(this) || modeler.splineContainer == null)
  65. return;
  66. Color color = Color.gray;
  67. color.a = 0.5f;
  68. Handles.color = color;
  69. m_DisableHandles = true;
  70. var splines = modeler.splineContainer.Splines;
  71. for (var i = 0; i < splines.Count; i++)
  72. {
  73. if (i < modeler.TransparencyData.Count)
  74. {
  75. var nativeSpline = new NativeSpline(splines[i], modeler.splineContainer.transform.localToWorldMatrix);
  76. DrawDataPoints(nativeSpline, modeler.TransparencyData[i]);
  77. }
  78. }
  79. }
  80. protected override bool DrawDataPoint(Vector3 position, Vector3 tangent, Vector3 up, float inValue, out float outValue)
  81. {
  82. int id = m_DisableHandles ? -1 : GUIUtility.GetControlID(FocusType.Passive);
  83. outValue = -1f;
  84. if (tangent == Vector3.zero) return false;
  85. if (Event.current.type == EventType.MouseUp && Event.current.button != 0 && (GUIUtility.hotControl == id))
  86. {
  87. Event.current.Use();
  88. return false;
  89. }
  90. var handleColor = Handles.color;
  91. if (GUIUtility.hotControl == id)
  92. handleColor = Handles.selectedColor;
  93. else if (GUIUtility.hotControl == 0 && (HandleUtility.nearestControl == id))
  94. handleColor = Handles.preselectionColor;
  95. EditorGUI.BeginChangeCheck();
  96. //if (GUIUtility.hotControl == id)
  97. {
  98. using (new Handles.DrawingScope(handleColor))
  99. {
  100. //Handles.ScaleValueHandle(inValue, position, Quaternion.LookRotation(tangent, up), HandleUtility.GetHandleSize(position) * 10f, Handles.ArrowHandleCap, 0.01f);
  101. Handles.BeginGUI();
  102. Vector2 screenPos = HandleUtility.WorldToGUIPoint(position);
  103. Rect sliderRect = new Rect(screenPos.x -(SLIDER_WIDTH * 0.5f), screenPos.y - 50f, SLIDER_WIDTH, 22f);
  104. EditorGUI.DrawRect(sliderRect, headerBackground);
  105. outValue = GUI.HorizontalSlider(sliderRect, inValue, 0f, 1f);
  106. outValue = math.clamp(outValue, 0f, 1f);
  107. Handles.EndGUI();
  108. }
  109. }
  110. if (inValue != outValue)
  111. {
  112. return true;
  113. }
  114. if (EditorGUI.EndChangeCheck()) return true;
  115. return false;
  116. }
  117. }
  118. #endif
  119. }