열차 목업의 내부 확인용 프로젝트
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
 
 
 
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR;
 
using UnityEngine.Events;
 
 
public class VRInputManager : Singleton<VRInputManager>
{
 
    /// <summary>
    /// 왼손, 오른손 컨트롤러 변수
    /// </summary>
    private InputDevice rightHandDevice;
    private InputDevice leftHandDevice;
 
    /// <summary>
    /// 왼손, 오른손 물체 상호작용 관련 변수
    /// </summary>
    public XRRayInteractor leftRay;
    public XRRayInteractor rightRay;
 
 
 
 
    /// <summary>
    /// 메뉴 ui 오브젝트
    /// </summary>
    public GameObject Menu;
 
 
    //[SerializeField] private XRRayInteractor rayInteractor;
    //[SerializeField] private TeleportationProvider provider;
    //private InputAction _thumbstick;
    //private bool _isActive;
    //List<InputDevice> inputDevices = new List<InputDevice>();
 
 
 
    public bool trackPadIsTouch = false;
    private bool trackPadIsRunning = false;
    private bool leftTrackPadPressed = false;
    private bool rightTrackPadPressed = false;
    private bool primary2DAxisLeftSelect = false;
    private bool primary2DAxisRightSelect = false;
    private bool primary2DAxisUpSelect = false;
    private bool primary2DAxisDownSelect = false;
    private bool primary2DAxisLeftUpTouch = false;
    private bool primary2DAxisRightUpTouch = false;
    private Vector2 leftTrackPos = Vector2.zero;
    private Vector2 rightTrackPos = Vector2.zero;
 
 
    private bool triggerIsRunning = false;
    private bool leftTriggerPressed = false;
    private bool rightTriggerPressed = false;
 
 
    public bool gripIsRunning = false;
    private bool leftGripPressed = false;
    private bool rightGripPressed = false;
 
 
 
    private bool menuIsRunning = false;
    private bool leftMenuPressed = false;
    private bool rightMenuPressed = false;
 
 
    /// <summary>
    /// 각 버튼별 동작 시 이벤트 모음
    /// </summary>
    public UnityEvent Primary2DAxisUpSelect;
    public UnityEvent Primary2DAxisUpSelectExit;
    public UnityEvent Primary2DAxisDownSelect;
    public UnityEvent Primary2DAxisDownSelectExit;
    public UnityEvent Primary2DAxisLeftSelect;
    public UnityEvent Primary2DAxisLeftSelectExit;
    public UnityEvent Primary2DAxisRightSelect;
    public UnityEvent Primary2DAxisRightSelectExit;
    public UnityEvent TriggerSelect;
    public UnityEvent TriggerSelectExit;
 
 
 
    public override void Awake()
    {
 
    }
 
    void Start()
    {
        if (Menu != null)
        {
            Menu.SetActive(false);
        }
 
 
    }
 
 
 
    // Update is called once per frame
    void Update()
    {
        GetDevice();
        OnTrigger(leftHandDevice, rightHandDevice);
        OnTrackPad(leftHandDevice, rightHandDevice);
        OnMenuButton(leftHandDevice, rightHandDevice);
        OnGripButton(leftHandDevice, rightHandDevice);
 
 
 
    }
 
 
 
    private void GetDevice()
    {
 
 
 
        leftHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
 
 
        rightHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
 
    }
 
 
    private void OnTrigger(InputDevice leftHandDevice, InputDevice rightHandDevice)
    {
 
        leftHandDevice.TryGetFeatureValue(CommonUsages.triggerButton, out leftTriggerPressed);
        rightHandDevice.TryGetFeatureValue(CommonUsages.triggerButton, out rightTriggerPressed);
 
        if ((rightTriggerPressed || leftTriggerPressed) && !triggerIsRunning)
        {
 
            triggerIsRunning = true;
            TriggerSelect?.Invoke();
        }
        else if (!rightTriggerPressed && !leftTriggerPressed && triggerIsRunning)
        {
 
            triggerIsRunning = false;
            TriggerSelectExit?.Invoke();
        }
 
    }
 
 
    private void OnTrackPad(InputDevice leftHandDevice, InputDevice rightHandDevice)
    {
 
 
        leftHandDevice.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out leftTrackPadPressed);
        rightHandDevice.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out rightTrackPadPressed);
        leftHandDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out leftTrackPos);
        rightHandDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out rightTrackPos);
        float angle = 0;
 
        if (!primary2DAxisRightUpTouch)
        {
            angle = Mathf.Atan2(leftTrackPos.y, leftTrackPos.x) * Mathf.Rad2Deg;
            //primary2DAxisLeftUpTouch = ActivateTeleportation(angle);
        }
 
 
        if (!primary2DAxisLeftUpTouch)
        {
            angle = Mathf.Atan2(rightTrackPos.y, rightTrackPos.x) * Mathf.Rad2Deg;
            //primary2DAxisRightUpTouch =  ActivateTeleportation(angle);
        }
 
 
 
 
 
        if (leftTrackPadPressed && !trackPadIsRunning)
        {
            trackPadIsRunning = true;
            angle = Mathf.Atan2(leftTrackPos.y, leftTrackPos.x) * Mathf.Rad2Deg;
            ActionTrackPad(angle);
 
        }
        else if (rightTrackPadPressed && !trackPadIsRunning)
        {
            trackPadIsRunning = true;
            angle = Mathf.Atan2(rightTrackPos.y, rightTrackPos.x) * Mathf.Rad2Deg;
            ActionTrackPad(angle);
 
        }
        else if (!rightTrackPadPressed && !leftTrackPadPressed && trackPadIsRunning)
        {
            trackPadIsRunning = false;
 
            if (primary2DAxisUpSelect)
            {
                primary2DAxisUpSelect = false;
                Primary2DAxisUpSelectExit?.Invoke();
            }
            else if (primary2DAxisRightSelect)
            {
                primary2DAxisRightSelect = false;
                Primary2DAxisRightSelectExit?.Invoke();
            }
            else if (primary2DAxisLeftSelect)
            {
                primary2DAxisLeftSelect = false;
                Primary2DAxisLeftSelectExit?.Invoke();
            }
            else if (primary2DAxisDownSelect)
            {
                primary2DAxisDownSelect = false;
                Primary2DAxisDownSelectExit?.Invoke();
            }
        }
 
 
    }
 
    //private bool ActivateTeleportation(float angle)
    //{
    //    if ((angle < 135 && angle >= 45)) // top
    //    {
    //        if (!trackPadIsTouch)
    //        {
    //            trackPadIsTouch = true;
    //            AnchorManager.Inst.SetActiveVisibleAnchor(trackPadIsTouch);
 
    //        }
 
    //    }
    //    else if (trackPadIsTouch)
    //    {
    //        trackPadIsTouch = false;
    //        AnchorManager.Inst.SetActiveVisibleAnchor(trackPadIsTouch);
    //        SafeArea.Inst.ShowWarningAlert();
 
    //    }
    //    return trackPadIsTouch;
    //}
 
 
    private void ActionTrackPad(float angle)
    {
        if (angle < 135 && angle >= 45) // top
        {
            primary2DAxisUpSelect = true;
            Primary2DAxisUpSelect?.Invoke();
        }
        else if (angle < 45 && angle >= -45) // right
        {
            primary2DAxisRightSelect = true;
            Primary2DAxisRightSelect?.Invoke();
        }
        else if ((angle < -135 && angle >= -180) || (angle <= 180 && angle >= 135))// left 
        {
            primary2DAxisLeftSelect = true;
            Primary2DAxisLeftSelect?.Invoke();
        }
        else
        {
            primary2DAxisDownSelect = true;
            Primary2DAxisDownSelect?.Invoke();
        }
 
    }
 
 
 
    private void OnGripButton(InputDevice leftHandDevice, InputDevice rightHandDevice)
    {
        leftHandDevice.TryGetFeatureValue(CommonUsages.gripButton, out leftGripPressed);
        rightHandDevice.TryGetFeatureValue(CommonUsages.gripButton, out rightGripPressed);
 
        if (leftGripPressed)
        {
            gripIsRunning = true;
 
 
 
 
        }
        else if (rightGripPressed)
        {
            gripIsRunning = true;
 
        }
        else if (!leftGripPressed && !rightGripPressed && gripIsRunning)
        {
            gripIsRunning = false;
 
        }
 
    }
 
    public void ActionGripButton(HoverEnterEventArgs args)
    {
 
 
    }
 
 
    private void OnMenuButton(InputDevice leftHandDevice, InputDevice rightHandDevice)
    {
        leftHandDevice.TryGetFeatureValue(CommonUsages.menuButton, out leftMenuPressed);
        rightHandDevice.TryGetFeatureValue(CommonUsages.menuButton, out rightMenuPressed);
 
        if ((leftMenuPressed || rightMenuPressed) && !menuIsRunning)
        {
            menuIsRunning = true;
 
 
            Menu.SetActive(!Menu.activeSelf);
 
        }
        else if (!leftMenuPressed && !rightMenuPressed && menuIsRunning)
        {
            menuIsRunning = false;
        }
 
    }
 
 
 
 
 
 
 
    private int GetSubMeshIndex(Mesh mesh, int triangleIndex)
    {
        if (mesh.isReadable == false)
        {
            Debug.LogError("You need to mark model's mesh as Read/Write Enabled in Import Settings.", mesh);
            return 0;
        }
 
        int triangleCounter = 0;
        for (int subMeshIndex = 0; subMeshIndex < mesh.subMeshCount; subMeshIndex++)
        {
            var indexCount = mesh.GetSubMesh(subMeshIndex).indexCount;
            triangleCounter += indexCount / 3;
            if (triangleIndex < triangleCounter)
            {
                return subMeshIndex;
            }
        }
 
        Debug.LogError(
            $"Failed to find triangle with index {triangleIndex} in mesh '{mesh.name}'. Total triangle count: {triangleCounter}",
            mesh);
        return 0;
    }
    //private void HideLineVisual(XRInteractorLineVisual lineVisual, bool active)
    //{
 
 
    //    lineVisual.enabled = active;
    //}
    //private void OnTeleportActivate(InputAction.CallbackContext context)
 
 
    //{
 
    //    Debug.Log("����");
    //    rayInteractor.enabled = true;
    //    _isActive = true;
    //}
 
    //private void OnTeleportCancel(InputAction.CallbackContext context)
    //{
    //    rayInteractor.enabled = false;
    //    _isActive = false;
    //}
}