VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_MovementRay.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 using AVR.Core;
6 
7 /// <summary>
8 /// Namespace of the arc-vr-motion package
9 /// </summary>
10 namespace AVR.Motion {
11  /// <summary>
12  /// AVR_SolidRay but, when an object is hit, the hit is either valid or invalid.
13  /// The validity of the hit is determined by AVR_MovementRay.filter.
14  /// A different color-gradient and/or reticule is displayed depending on the validity of the ray.
15  /// If no object is hit, the ray always defaults to invalid.
16  /// </summary>
17  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_motion_1_1_a_v_r___movement_ray.html")]
18  [RequireComponent(typeof(LineRenderer))]
20  {
21  // Filter that determine whether a hit is valid. By default always return true.
22  public System.Func<RaycastHit, bool> filter = (RaycastHit h) => true;
23 
24  // Public
25  [Header("HitReticules")]
26  /// <summary>
27  /// Reticule object displayed when a valid surface is hit
28  /// </summary>
29  public GameObject reticule;
30  /// <summary>
31  /// Reticule object displayed when an invalid surface is hit
32  /// </summary>
33  public GameObject invalid_reticule;
34 
35  // COLOR GRADIENTS
36  [Header("Gradients")]
37  [SerializeField]
38  Gradient m_ValidColorGradient = new Gradient()
39  {
40  colorKeys = new GradientColorKey[] { new GradientColorKey(Color.white, 0.0f), new GradientColorKey(Color.white, 1.0f) },
41  alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(1f, 0.0f), new GradientAlphaKey(1f, 1.0f) }
42  };
43  /// <summary>Gets or sets the color of the line as a gradient from start to end to indicate a valid state.</summary>
44  public Gradient validColorGradient { get { return m_ValidColorGradient; } set { m_ValidColorGradient = value; } }
45 
46  [SerializeField]
47  Gradient m_InvalidColorGradient = new Gradient()
48  {
49  colorKeys = new GradientColorKey[] { new GradientColorKey(Color.red, 0.0f), new GradientColorKey(Color.red, 1.0f) },
50  alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(1f, 0.0f), new GradientAlphaKey(1f, 1.0f) }
51  };
52  /// <summary>Gets or sets the color of the line as a gradient from start to end to indicate an invalid state.</summary>
53  public Gradient invalidColorGradient { get { return m_InvalidColorGradient; } set { m_InvalidColorGradient = value; } }
54 
55 
56  /// <summary>
57  /// Is this ray valid.
58  /// The validity of a Raycasthit is determined on whether if AVR_MovementRay.filter satisfies the current hit.
59  /// </summary>
60  public bool isValid {
61  get { return _valid; }
62  }
63  private bool _valid;
64 
65  public override void hide() {
66  base.hide();
67  if(reticule) reticule.SetActive(false);
68  if(invalid_reticule) invalid_reticule.SetActive(false);
69  }
70 
71  protected override void UpdateRay() {
72  if(isHidden) return;
73 
74  base.UpdateRay();
75 
76  // Check if object is valid
77  _valid = objectHit && this.filter(hitPosition);
78 
79  // Set reticule
80  if(reticule) {
81  reticule.transform.position = hitPosition.point;
82  reticule.transform.forward = new Vector3(Camera.main.transform.forward.x, 0.0f, Camera.main.transform.forward.z);
83  reticule.SetActive(objectHit && isValid);
84  }
85  else {
86  reticule.SetActive(false);
87  }
88 
89  // Set invalid reticule
90  if(invalid_reticule) {
91  invalid_reticule.transform.position = hitPosition.point;
92  invalid_reticule.transform.forward = new Vector3(Camera.main.transform.forward.x, 0.0f, Camera.main.transform.forward.z);
93  invalid_reticule.SetActive(objectHit && !isValid);
94  }
95  else {
96  invalid_reticule.SetActive(false);
97  }
98 
99  // Set gradient
100  if(isValid) {
101  lr.colorGradient = m_ValidColorGradient;
102  }
103  else {
104  lr.colorGradient = m_InvalidColorGradient;
105  }
106  }
107  }
108 }
override void hide()
Hides the ray. A ray is not updated while hidden.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
GameObject reticule
Reticule object displayed when a valid surface is hit
GameObject invalid_reticule
Reticule object displayed when an invalid surface is hit
A more advanced type of AVR_Ray. The ray can be used to get the first object hit by it...
Definition: AVR_SolidRay.cs:12
override void UpdateRay()
Updates the ray. Called from Monobehaviour.Update()
AVR_SolidRay but, when an object is hit, the hit is either valid or invalid. The validity of the hit ...