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