123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Diagnostics;
- using UnityEngine;
- public class SteamVR_LaserPointer : MonoBehaviour
- {
- [DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event PointerEventHandler PointerIn;
- [DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event PointerEventHandler PointerOut;
- private void Start()
- {
- this.holder = new GameObject();
- this.holder.transform.parent = base.transform;
- this.holder.transform.localPosition = Vector3.zero;
- this.pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
- this.pointer.transform.parent = this.holder.transform;
- this.pointer.transform.localScale = new Vector3(this.thickness, this.thickness, 100f);
- this.pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
- BoxCollider component = this.pointer.GetComponent<BoxCollider>();
- if (this.addRigidBody)
- {
- if (component)
- {
- component.isTrigger = true;
- }
- Rigidbody rigidbody = this.pointer.AddComponent<Rigidbody>();
- rigidbody.isKinematic = true;
- }
- else if (component)
- {
- UnityEngine.Object.Destroy(component);
- }
- Material material = new Material(Shader.Find("Unlit/Color"));
- material.SetColor("_Color", this.color);
- this.pointer.GetComponent<MeshRenderer>().material = material;
- }
- public virtual void OnPointerIn(PointerEventArgs e)
- {
- if (this.PointerIn != null)
- {
- this.PointerIn(this, e);
- }
- }
- public virtual void OnPointerOut(PointerEventArgs e)
- {
- if (this.PointerOut != null)
- {
- this.PointerOut(this, e);
- }
- }
- private void Update()
- {
- if (!this.isActive)
- {
- this.isActive = true;
- base.transform.GetChild(0).gameObject.SetActive(true);
- }
- float num = 100f;
- SteamVR_TrackedController component = base.GetComponent<SteamVR_TrackedController>();
- Ray ray = new Ray(base.transform.position, base.transform.forward);
- RaycastHit raycastHit;
- bool flag = Physics.Raycast(ray, out raycastHit);
- if (this.previousContact && this.previousContact != raycastHit.transform)
- {
- PointerEventArgs e = default(PointerEventArgs);
- if (component != null)
- {
- e.controllerIndex = component.controllerIndex;
- }
- e.distance = 0f;
- e.flags = 0u;
- e.target = this.previousContact;
- this.OnPointerOut(e);
- this.previousContact = null;
- }
- if (flag && this.previousContact != raycastHit.transform)
- {
- PointerEventArgs e2 = default(PointerEventArgs);
- if (component != null)
- {
- e2.controllerIndex = component.controllerIndex;
- }
- e2.distance = raycastHit.distance;
- e2.flags = 0u;
- e2.target = raycastHit.transform;
- this.OnPointerIn(e2);
- this.previousContact = raycastHit.transform;
- }
- if (!flag)
- {
- this.previousContact = null;
- }
- if (flag && raycastHit.distance < 100f)
- {
- num = raycastHit.distance;
- }
- if (component != null && component.triggerPressed)
- {
- this.pointer.transform.localScale = new Vector3(this.thickness * 5f, this.thickness * 5f, num);
- }
- else
- {
- this.pointer.transform.localScale = new Vector3(this.thickness, this.thickness, num);
- }
- this.pointer.transform.localPosition = new Vector3(0f, 0f, num / 2f);
- }
- public bool active = true;
- public Color color;
- public float thickness = 0.002f;
- public GameObject holder;
- public GameObject pointer;
- private bool isActive;
- public bool addRigidBody;
- public Transform reference;
- private Transform previousContact;
- }
|