ComparisonGizmoState.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using UnityEditor.AnimatedValues;
  3. using UnityEngine;
  4. namespace UnityEditor.Rendering.LookDev
  5. {
  6. /// <summary>state of the comparison gizmo of the LookDev</summary>
  7. [Serializable]
  8. public class ComparisonGizmoState
  9. {
  10. internal const float thickness = 0.0028f;
  11. internal const float thicknessSelected = 0.015f;
  12. internal const float circleRadius = 0.014f;
  13. internal const float circleRadiusSelected = 0.03f;
  14. internal const float blendFactorCircleRadius = 0.01f;
  15. internal const float blendFactorCircleRadiusSelected = 0.03f;
  16. /// <summary>Position of the first extremity</summary>
  17. public Vector2 point1 { get; private set; }
  18. /// <summary>Position of the second extremity</summary>
  19. public Vector2 point2 { get; private set; }
  20. /// <summary>Position of the center</summary>
  21. [field: SerializeField]
  22. public Vector2 center { get; private set; } = Vector2.zero;
  23. /// <summary>Angle from vertical in radian</summary>
  24. [field: SerializeField]
  25. public float angle { get; private set; }
  26. /// <summary>Length between extremity</summary>
  27. [field: SerializeField]
  28. public float length { get; private set; } = 0.2f;
  29. internal Vector4 plane { get; private set; }
  30. internal Vector4 planeOrtho { get; private set; }
  31. /// <summary>
  32. /// The position of the blending slider.
  33. /// From value -1 on first extremity to value 1 on second extremity.
  34. /// </summary>
  35. [field: SerializeField]
  36. public float blendFactor { get; set; }
  37. internal float blendFactorMaxGizmoDistance
  38. => length - circleRadius - blendFactorCircleRadius;
  39. internal float blendFactorMinGizmoDistance
  40. => length - circleRadius - blendFactorCircleRadiusSelected;
  41. internal void Init()
  42. => Update(center, length, angle);
  43. //TODO: optimize
  44. private Vector4 Get2DPlane(Vector2 firstPoint, float angle)
  45. {
  46. Vector4 result = new Vector4();
  47. angle = angle % (2.0f * (float)Math.PI);
  48. Vector2 secondPoint = new Vector2(firstPoint.x + Mathf.Sin(angle), firstPoint.y + Mathf.Cos(angle));
  49. Vector2 diff = secondPoint - firstPoint;
  50. if (Mathf.Abs(diff.x) < 1e-5)
  51. {
  52. result.Set(-1.0f, 0.0f, firstPoint.x, 0.0f);
  53. float sign = Mathf.Cos(angle) > 0.0f ? 1.0f : -1.0f;
  54. result *= sign;
  55. }
  56. else
  57. {
  58. float slope = diff.y / diff.x;
  59. result.Set(-slope, 1.0f, -(firstPoint.y - slope * firstPoint.x), 0.0f);
  60. }
  61. if (angle > Mathf.PI)
  62. result = -result;
  63. float length = Mathf.Sqrt(result.x * result.x + result.y * result.y);
  64. result = result / length;
  65. return result;
  66. }
  67. /// <summary>
  68. /// Update all fields while moving one extremity
  69. /// </summary>
  70. /// <param name="point1">The new first extremity position</param>
  71. /// <param name="point2">The new second extremity position</param>
  72. public void Update(Vector2 point1, Vector2 point2)
  73. {
  74. this.point1 = point1;
  75. this.point2 = point2;
  76. center = (point1 + point2) * 0.5f;
  77. length = (point2 - point1).magnitude * 0.5f;
  78. Vector3 verticalPlane = Get2DPlane(center, 0.0f);
  79. float side = Vector3.Dot(new Vector3(point1.x, point1.y, 1.0f), verticalPlane);
  80. angle = (Mathf.Deg2Rad * Vector2.Angle(new Vector2(0.0f, 1.0f), (point1 - point2).normalized));
  81. if (side > 0.0f)
  82. angle = 2.0f * Mathf.PI - angle;
  83. plane = Get2DPlane(center, angle);
  84. planeOrtho = Get2DPlane(center, angle + 0.5f * (float)Mathf.PI);
  85. }
  86. /// <summary>
  87. /// Update all fields while moving the bar
  88. /// </summary>
  89. /// <param name="center">The new center position</param>
  90. /// <param name="length">Tne new length of this gizmo</param>
  91. /// <param name="angle">The new angle of this gizmo</param>
  92. public void Update(Vector2 center, float length, float angle)
  93. {
  94. this.center = center;
  95. this.length = length;
  96. this.angle = angle;
  97. plane = Get2DPlane(center, angle);
  98. planeOrtho = Get2DPlane(center, angle + 0.5f * (float)Mathf.PI);
  99. Vector2 dir = new Vector2(planeOrtho.x, planeOrtho.y);
  100. point1 = center + dir * length;
  101. point2 = center - dir * length;
  102. }
  103. }
  104. }