IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SteamVR_LaserPointer.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 using UnityEngine;
3 using System.Collections;
4 
5 public struct PointerEventArgs
6 {
7  public uint controllerIndex;
8  public uint flags;
9  public float distance;
10  public Transform target;
11 }
12 
13 public delegate void PointerEventHandler(object sender, PointerEventArgs e);
14 
15 
16 public class SteamVR_LaserPointer : MonoBehaviour
17 {
18  public bool active = true;
19  public Color color;
20  public float thickness = 0.002f;
21  public GameObject holder;
22  public GameObject pointer;
23  bool isActive = false;
24  public bool addRigidBody = false;
25  public Transform reference;
26  public event PointerEventHandler PointerIn;
27  public event PointerEventHandler PointerOut;
28 
29  Transform previousContact = null;
30 
31  // Use this for initialization
32  void Start ()
33  {
34  holder = new GameObject();
35  holder.transform.parent = this.transform;
36  holder.transform.localPosition = Vector3.zero;
37  holder.transform.localRotation = Quaternion.identity;
38 
39  pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
40  pointer.transform.parent = holder.transform;
41  pointer.transform.localScale = new Vector3(thickness, thickness, 100f);
42  pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
43  pointer.transform.localRotation = Quaternion.identity;
44  BoxCollider collider = pointer.GetComponent<BoxCollider>();
45  if (addRigidBody)
46  {
47  if (collider)
48  {
49  collider.isTrigger = true;
50  }
51  Rigidbody rigidBody = pointer.AddComponent<Rigidbody>();
52  rigidBody.isKinematic = true;
53  }
54  else
55  {
56  if(collider)
57  {
58  Object.Destroy(collider);
59  }
60  }
61  Material newMaterial = new Material(Shader.Find("Unlit/Color"));
62  newMaterial.SetColor("_Color", color);
63  pointer.GetComponent<MeshRenderer>().material = newMaterial;
64  }
65 
66  public virtual void OnPointerIn(PointerEventArgs e)
67  {
68  if (PointerIn != null)
69  PointerIn(this, e);
70  }
71 
72  public virtual void OnPointerOut(PointerEventArgs e)
73  {
74  if (PointerOut != null)
75  PointerOut(this, e);
76  }
77 
78 
79  // Update is called once per frame
80  void Update ()
81  {
82  if (!isActive)
83  {
84  isActive = true;
85  this.transform.GetChild(0).gameObject.SetActive(true);
86  }
87 
88  float dist = 100f;
89 
90  SteamVR_TrackedController controller = GetComponent<SteamVR_TrackedController>();
91 
92  Ray raycast = new Ray(transform.position, transform.forward);
93  RaycastHit hit;
94  bool bHit = Physics.Raycast(raycast, out hit);
95 
96  if(previousContact && previousContact != hit.transform)
97  {
99  if (controller != null)
100  {
101  args.controllerIndex = controller.controllerIndex;
102  }
103  args.distance = 0f;
104  args.flags = 0;
105  args.target = previousContact;
106  OnPointerOut(args);
107  previousContact = null;
108  }
109  if(bHit && previousContact != hit.transform)
110  {
111  PointerEventArgs argsIn = new PointerEventArgs();
112  if (controller != null)
113  {
114  argsIn.controllerIndex = controller.controllerIndex;
115  }
116  argsIn.distance = hit.distance;
117  argsIn.flags = 0;
118  argsIn.target = hit.transform;
119  OnPointerIn(argsIn);
120  previousContact = hit.transform;
121  }
122  if(!bHit)
123  {
124  previousContact = null;
125  }
126  if (bHit && hit.distance < 100f)
127  {
128  dist = hit.distance;
129  }
130 
131  if (controller != null && controller.triggerPressed)
132  {
133  pointer.transform.localScale = new Vector3(thickness * 5f, thickness * 5f, dist);
134  }
135  else
136  {
137  pointer.transform.localScale = new Vector3(thickness, thickness, dist);
138  }
139  pointer.transform.localPosition = new Vector3(0f, 0f, dist/2f);
140  }
141 }