Tanoda
VivePointer.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.Linq;
3using UnityEngine;
6#if !UNITY_WEBGL
7using Valve.VR;
8using Valve.VR.InteractionSystem;
9
10[RequireComponent (typeof(Hand))]
11#endif
12public class VivePointer : MonoBehaviour {
13
14 public LineRenderer line;
15 public float maxDistance;
16 public LayerMask mask;
17 public UnityAction<Vector3, bool> OnPointerUpdate = null;
18 private GameObject currentObject = null;
19 public EventSystem eventSystem = null;
20 public float defaultLength = 3.0f;
21 public StandaloneInputModule inputModule = null;
23 public GameObject RawImage;
24 internal int xPosScreen;
25 internal int yPosScreen;
26 private Queue<Vector3> fromQueue;
27 private int i;
28 private Vector3 avgHitPoint;
29 private Vector3 transformedHitPoint;
30 internal RaycastHit hitVC;
31 public void Start () {
32 if(!line) {
33 enabled = false;
34 return;
35 }
36 line.positionCount = 2;
37 line.enabled = false;
38 fromQueue = new Queue<Vector3>();
39 i = 0;
40 }
41 public void Update ()
42 {
43#if !UNITY_WEBGL
44
45 RaycastHit hit;
46
47 float xRate;
48 float xRate2;
49 float xRate3;
50 float yRate;
51 int xWidth, yWidth;
52
53 if (!Physics.Raycast (transform.position, transform.forward, out hit, maxDistance, mask)) {
54 if (!line) return;
55 line.enabled = false;
56 line.gameObject.SetActive(false);
57 reticleVive.GetComponent<SpriteRenderer>().enabled = false;
58 return;
59 }
60 hitVC = hit;
61 if (hit.collider.gameObject.GetComponent<VivePointerTarget>())
62 {
63 line.enabled = true;
64 line.gameObject.SetActive(true);
65 reticleVive.GetComponent<SpriteRenderer>().enabled = true;
66 reticleVive.GetComponent<SpriteRenderer>().gameObject.SetActive(true);
67
68 line.SetPosition (0, transform.position);
69 line.SetPosition (1, hit.point);
70 if (SteamVR_Actions._default.GrabGrip.GetStateDown(SteamVR_Input_Sources.Any)) {
71 hit.collider.gameObject.GetComponent<VivePointerTarget> ().OnPointerClick ();
72 }
73 UpdatePointerStatus(hit.point);
74
75 fromQueue.Enqueue(hit.transform.GetChild(0).InverseTransformPoint(hit.point));
76 i++;
77
78 if (fromQueue.Count == 10)
79 {
80 var avg = default(Vector3);
81 foreach (var v in fromQueue)
82 {
83 avg += v;
84 }
85
86 avg /= fromQueue.Count;
87 avgHitPoint = avg;
88 while (fromQueue.Count == 10)
89 {
90 fromQueue.Dequeue();
91 }
92 xWidth = (int)RawImage.GetComponent<ScreneCapture>().xWidth;
93 yWidth = (int)RawImage.GetComponent <ScreneCapture>().yWidth;
94 xRate = avgHitPoint.x / RawImage.GetComponent<RectTransform>().sizeDelta.x;
95 xRate2 = hit.transform.InverseTransformPoint(avgHitPoint).x / xWidth;
96 yRate = avgHitPoint.y / RawImage.GetComponent<RectTransform>().sizeDelta.y;
97
98 xPosScreen = (int)(xRate * (Screen.width - RawImage.GetComponent<ScreneCapture>().LeftCorr - RawImage.GetComponent<ScreneCapture>().RightCorr)) + RawImage.GetComponent<ScreneCapture>().LeftCorr;
99 yPosScreen = (int)(Screen.height- RawImage.GetComponent<ScreneCapture>().TopCorr-RawImage.GetComponent<ScreneCapture>().BottomCorr) - (int)(yRate * (Screen.height - RawImage.GetComponent<ScreneCapture>().TopCorr - RawImage.GetComponent<ScreneCapture>().BottomCorr)) + RawImage.GetComponent<ScreneCapture>().TopCorr;
100
101 if (hit.collider.gameObject.GetComponent<ScreneCapture>())
102 {
103 MouseEvent.MoveMouse(xPosScreen, yPosScreen);
104 }
105
106 }
107
108
109
110 }
111
112#endif
113 }
114 #if UNITY_EDITOR
115 private void OnGUI()
116 {
117 GUI.Label(new Rect(10, 120, 100, 100), $"X: {avgHitPoint.x.ToString("F3")} Y: {avgHitPoint.y.ToString("F3")}");
118 }
119
120#endif
121
122
123 private void UpdatePointerStatus(Vector3 hitPoint)
124 {
125 bool hit = false;
126
127 if (currentObject != null)
128 hit = mask == (mask | (1 << currentObject.layer));
129
130 if (OnPointerUpdate != null)
131 OnPointerUpdate(hitPoint, hit);
132 }
133
134}
static void MoveMouse(int x, int y)
Definition: MouseEvent.cs:69
EventSystem eventSystem
Definition: VivePointer.cs:19
GameObject RawImage
Definition: VivePointer.cs:23
void Update()
Definition: VivePointer.cs:41
void Start()
Definition: VivePointer.cs:31
float maxDistance
Definition: VivePointer.cs:15
UnityAction< Vector3, bool > OnPointerUpdate
Definition: VivePointer.cs:17
ReticleVive reticleVive
Definition: VivePointer.cs:22
LayerMask mask
Definition: VivePointer.cs:16
StandaloneInputModule inputModule
Definition: VivePointer.cs:21
LineRenderer line
Definition: VivePointer.cs:14
float defaultLength
Definition: VivePointer.cs:20