열차 목업의 내부 확인용 프로젝트
smchoi
2024-07-31 9fe33c1ae076b0f6501619388e6b4cc872b76f80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using UnityEngine.Events;
using UnityEngine.XR.Interaction.Toolkit;
 
namespace UnityEngine.XR.Content.Interaction
{
    /// <summary>
    /// An interactable lever that snaps into an on or off position by a direct interactor
    /// </summary>
    public class XRLever : XRBaseInteractable
    {
        public enum LeverAxis
        {
            X,
            Y,
            Z
        }
 
        const float k_LeverDeadZone = 0.1f; // Prevents rapid switching between on and off states when right in the middle
 
        [SerializeField]
        [Tooltip("The object that is visually grabbed and manipulated")]
        Transform m_Handle = null;
 
        [SerializeField]
        [Tooltip("The value of the lever")]
        bool m_Value = false;
 
        [SerializeField]
        [Tooltip("If enabled, the lever will snap to the value position when released")]
        bool m_LockToValue;
 
        [SerializeField]
        [Tooltip("Angle of the lever in the 'on' position")]
        [Range(-180.0f, 180.0f)]
        float m_MaxAngle = 90.0f;
 
        [SerializeField]
        [Tooltip("Angle of the lever in the 'off' position")]
        [Range(-180.0f, 180.0f)]
        float m_MinAngle = -90.0f;
 
        [SerializeField]
        [Tooltip("Events to trigger when the lever activates")]
        UnityEvent m_OnLeverActivate = new UnityEvent();
 
        [SerializeField]
        [Tooltip("Events to trigger when the lever deactivates")]
        UnityEvent m_OnLeverDeactivate = new UnityEvent();
 
        [SerializeField]
        LeverAxis m_Axis = LeverAxis.X;
 
        [SerializeField]
        Vector3 m_BaseDirection = new Vector3(0, 90, -90);
 
 
        IXRSelectInteractor m_Interactor;
 
        /// <summary>
        /// The object that is visually grabbed and manipulated
        /// </summary>
        public Transform handle
        {
            get => m_Handle;
            set => m_Handle = value;
        }
 
        /// <summary>
        /// The value of the lever
        /// </summary>
        public bool value
        {
            get => m_Value;
            set => SetValue(value, true);
        }
 
        /// <summary>
        /// If enabled, the lever will snap to the value position when released
        /// </summary>
        public bool lockToValue { get; set; }
 
        /// <summary>
        /// Angle of the lever in the 'on' position
        /// </summary>
        public float maxAngle
        {
            get => m_MaxAngle;
            set => m_MaxAngle = value;
        }
 
        /// <summary>
        /// Angle of the lever in the 'off' position
        /// </summary>
        public float minAngle
        {
            get => m_MinAngle;
            set => m_MinAngle = value;
        }
 
        /// <summary>
        /// Events to trigger when the lever activates
        /// </summary>
        public UnityEvent onLeverActivate => m_OnLeverActivate;
 
        /// <summary>
        /// Events to trigger when the lever deactivates
        /// </summary>
        public UnityEvent onLeverDeactivate => m_OnLeverDeactivate;
 
        void Start()
        {
            //if(!SceneTypeManager.Inst.IsVRMode)
            //{
            //    if(colliders.Count > 0)
            //    {
            //        foreach(var collider in colliders)
            //        {
            //            collider.enabled = false;
            //        }
            //    }
            //} 
            SetValue(m_Value, true);
        }
 
        protected override void OnEnable()
        {
            base.OnEnable();
            selectEntered.AddListener(StartGrab);
            selectExited.AddListener(EndGrab);
        }
 
        protected override void OnDisable()
        {
            selectEntered.RemoveListener(StartGrab);
            selectExited.RemoveListener(EndGrab);
            base.OnDisable();
        }
 
        void StartGrab(SelectEnterEventArgs args)
        {
            m_Interactor = args.interactorObject;
        }
 
        void EndGrab(SelectExitEventArgs args)
        {
            SetValue(m_Value, true);
            m_Interactor = null;
        }
 
        public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
        {
            base.ProcessInteractable(updatePhase);
 
            if (updatePhase == XRInteractionUpdateOrder.UpdatePhase.Dynamic)
            {
                if (isSelected)
                {
                    UpdateValue();
                }
            }
        }
 
        Vector3 GetLookDirection()
        {
            Vector3 direction = m_Interactor.GetAttachTransform(this).position - m_Handle.position;
            direction = transform.InverseTransformDirection(direction);
            direction.z = 0;
            Debug.Log(direction);
            return direction.normalized;
        }
 
        void UpdateValue()
        {
            var lookDirection = GetLookDirection();
            var lookAngle = Mathf.Atan2(lookDirection.x, lookDirection.y) * Mathf.Rad2Deg;
            Debug.Log("lookAngle : " + lookAngle);
            if (m_MinAngle < m_MaxAngle)
                lookAngle = Mathf.Clamp(lookAngle, m_MinAngle, m_MaxAngle);
            else
                lookAngle = Mathf.Clamp(lookAngle, m_MaxAngle, m_MinAngle);
 
            var maxAngleDistance = Mathf.Abs(m_MaxAngle - lookAngle);
            var minAngleDistance = Mathf.Abs(m_MinAngle - lookAngle);
 
            if (m_Value)
                maxAngleDistance *= (1.0f - k_LeverDeadZone);
            else
                minAngleDistance *= (1.0f - k_LeverDeadZone);
 
            var newValue = (maxAngleDistance < minAngleDistance);
 
            Debug.Log("Look Angle : " + lookAngle);
 
            SetHandleAngle(lookAngle);
 
            SetValue(newValue);
        }
 
        void SetValue(bool isOn, bool forceRotation = false)
        {
            if (m_Value == isOn)
            {
                if (forceRotation)
                    SetHandleAngle(m_Value ? m_MaxAngle : m_MinAngle);
 
                return;
            }
 
            m_Value = isOn;
 
            if (m_Value)
            {
                m_OnLeverActivate.Invoke();
            }
            else
            {
                m_OnLeverDeactivate.Invoke();
            }
 
            if (!isSelected && (m_LockToValue || forceRotation))
                SetHandleAngle(m_Value ? m_MaxAngle : m_MinAngle);
        }
 
        void SetHandleAngle(float angle)
        {
 
            if (m_Handle == null) return;
 
            if (m_Axis == LeverAxis.X)
            {
                m_Handle.localRotation = Quaternion.Euler(angle, m_BaseDirection.y, m_BaseDirection.z);
            }
            else if (m_Axis == LeverAxis.Y)
            {
                m_Handle.localRotation = Quaternion.Euler(m_BaseDirection.x, angle, m_BaseDirection.z);
            }
            else
            {
                m_Handle.localRotation = Quaternion.Euler(m_BaseDirection.x, m_BaseDirection.y, angle);
            }
 
 
        }
 
        void OnDrawGizmosSelected()
        {
            var angleStartPoint = transform.position;
 
            if (m_Handle != null)
                angleStartPoint = m_Handle.position;
 
            const float k_AngleLength = 0.25f;
 
            var angleMaxPoint = angleStartPoint + transform.TransformDirection(Quaternion.Euler(m_MaxAngle, 0.0f, 0.0f) * Vector3.up) * k_AngleLength;
            var angleMinPoint = angleStartPoint + transform.TransformDirection(Quaternion.Euler(m_MinAngle, 0.0f, 0.0f) * Vector3.up) * k_AngleLength;
 
            Gizmos.color = Color.green;
            Gizmos.DrawLine(angleStartPoint, angleMaxPoint);
 
            Gizmos.color = Color.red;
            Gizmos.DrawLine(angleStartPoint, angleMinPoint);
        }
 
        void OnValidate()
        {
            SetHandleAngle(m_Value ? m_MaxAngle : m_MinAngle);
        }
    }
}