SteamVR_LaserPointer.cs
5.75 KB
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
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
using UnityEngine;
using System.Collections;
namespace Valve.VR.Extras
{
public class SteamVR_LaserPointer : MonoBehaviour
{
public SteamVR_Behaviour_Pose pose;
//public SteamVR_Action_Boolean interactWithUI = SteamVR_Input.__actions_default_in_InteractUI;
public SteamVR_Action_Boolean interactWithUI = SteamVR_Input.GetBooleanAction("InteractUI");
public bool active = true;
public Color color;
public float thickness = 0.002f;
public Color clickColor = Color.green;
public GameObject holder;
public GameObject pointer;
bool isActive = false;
public bool addRigidBody = false;
public Transform reference;
public event PointerEventHandler PointerIn;
public event PointerEventHandler PointerOut;
public event PointerEventHandler PointerClick;
Transform previousContact = null;
private void Start()
{
if (pose == null)
pose = this.GetComponent<SteamVR_Behaviour_Pose>();
if (pose == null)
Debug.LogError("No SteamVR_Behaviour_Pose component found on this object", this);
if (interactWithUI == null)
Debug.LogError("No ui interaction action has been set on this component.", this);
holder = new GameObject();
holder.transform.parent = this.transform;
holder.transform.localPosition = Vector3.zero;
holder.transform.localRotation = Quaternion.identity;
pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
pointer.transform.parent = holder.transform;
pointer.transform.localScale = new Vector3(thickness, thickness, 100f);
pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
pointer.transform.localRotation = Quaternion.identity;
BoxCollider collider = pointer.GetComponent<BoxCollider>();
if (addRigidBody)
{
if (collider)
{
collider.isTrigger = true;
}
Rigidbody rigidBody = pointer.AddComponent<Rigidbody>();
rigidBody.isKinematic = true;
}
else
{
if (collider)
{
Object.Destroy(collider);
}
}
Material newMaterial = new Material(Shader.Find("Unlit/Color"));
newMaterial.SetColor("_Color", color);
pointer.GetComponent<MeshRenderer>().material = newMaterial;
}
public virtual void OnPointerIn(PointerEventArgs e)
{
if (PointerIn != null)
PointerIn(this, e);
}
public virtual void OnPointerClick(PointerEventArgs e)
{
if (PointerClick != null)
PointerClick(this, e);
}
public virtual void OnPointerOut(PointerEventArgs e)
{
if (PointerOut != null)
PointerOut(this, e);
}
private void Update()
{
if (!isActive)
{
isActive = true;
this.transform.GetChild(0).gameObject.SetActive(true);
}
float dist = 100f;
Ray raycast = new Ray(transform.position, transform.forward);
RaycastHit hit;
bool bHit = Physics.Raycast(raycast, out hit);
if (previousContact && previousContact != hit.transform)
{
PointerEventArgs args = new PointerEventArgs();
args.fromInputSource = pose.inputSource;
args.distance = 0f;
args.flags = 0;
args.target = previousContact;
OnPointerOut(args);
previousContact = null;
}
if (bHit && previousContact != hit.transform)
{
PointerEventArgs argsIn = new PointerEventArgs();
argsIn.fromInputSource = pose.inputSource;
argsIn.distance = hit.distance;
argsIn.flags = 0;
argsIn.target = hit.transform;
OnPointerIn(argsIn);
previousContact = hit.transform;
}
if (!bHit)
{
previousContact = null;
}
if (bHit && hit.distance < 100f)
{
dist = hit.distance;
}
if (bHit && interactWithUI.GetStateUp(pose.inputSource))
{
PointerEventArgs argsClick = new PointerEventArgs();
argsClick.fromInputSource = pose.inputSource;
argsClick.distance = hit.distance;
argsClick.flags = 0;
argsClick.target = hit.transform;
OnPointerClick(argsClick);
}
if (interactWithUI != null && interactWithUI.GetState(pose.inputSource))
{
pointer.transform.localScale = new Vector3(thickness * 5f, thickness * 5f, dist);
pointer.GetComponent<MeshRenderer>().material.color = clickColor;
}
else
{
pointer.transform.localScale = new Vector3(thickness, thickness, dist);
pointer.GetComponent<MeshRenderer>().material.color = color;
}
pointer.transform.localPosition = new Vector3(0f, 0f, dist / 2f);
}
}
public struct PointerEventArgs
{
public SteamVR_Input_Sources fromInputSource;
public uint flags;
public float distance;
public Transform target;
}
public delegate void PointerEventHandler(object sender, PointerEventArgs e);
}