AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
RaycastResultHelper.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // Licensed under the MIT License. See LICENSE in the project root for license information.
4 //
5 using System;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity
9 {
10  public struct RaycastResultHelper
11  {
12  public RaycastResultHelper(RaycastHit hit, LayerMask surface) : this(hit.collider, hit.point, hit.normal, hit.distance, hit.textureCoord, hit.textureCoord2, surface)
13  {
14  }
15 
16  public RaycastResultHelper(Collider collider, Vector3 point, Vector3 normal, float distance, Vector2 textureCoord, Vector2 textureCoord2, LayerMask surface)
17  {
18  this.layer = collider != null ? collider.gameObject.layer : LayerExtensions.Surface;
19 
20  if (this.layer == surface.value)
21  {
22  // Spoof the mirage raycast result of 'no collider' if we hit a unity SR layer.
23  this.collider = null;
24  }
25  else
26  {
27  this.collider = collider;
28  }
29 
30  this.point = point;
31  this.normal = normal;
32  this.distance = distance;
33  this.transform = null;
34  this.textureCoord = textureCoord;
35  this.textureCoord2 = textureCoord2;
36  }
37 
38  private Collider collider;
39  public Collider Collider
40  {
41  get { return this.collider; }
42  private set { this.collider = value; }
43  }
44 
45  private int layer;
46  public int Layer
47  {
48  get { return this.layer; }
49  private set { this.layer = value; }
50  }
51 
52  private Vector3 normal;
53  public Vector3 Normal
54  {
55  get { return this.normal; }
56  private set { this.normal = value; }
57  }
58 
59  private float distance;
60  public float Distance
61  {
62  get { return this.distance; }
63  private set { this.distance = value; }
64  }
65 
66  private Vector3 point;
67  public Vector3 Point
68  {
69  get { return this.point; }
70  private set { this.point = value; }
71  }
72 
73  private Transform transform;
74  public Transform Transform
75  {
76  get
77  {
78  if (this.transform == null &&
79  this.Collider != null)
80  {
81  this.transform = this.Collider.transform;
82  }
83 
84  return this.transform;
85  }
86  }
87 
88  private Vector2 textureCoord;
89  public Vector2 TextureCoord
90  {
91  get { return this.textureCoord; }
92  private set { this.textureCoord = value; }
93  }
94 
95  private Vector2 textureCoord2;
96  public Vector2 TextureCoord2
97  {
98  get { return this.textureCoord2; }
99  private set { this.textureCoord2 = value; }
100  }
101 
102  public Vector3 GetNormalFromTextureCoord()
103  {
104  // Assumes packed at unit length
105  return new Vector3(textureCoord.x, textureCoord.y, 1f - Mathf.Sqrt(textureCoord.x * textureCoord.x + textureCoord.y * textureCoord.y));
106  }
108  {
109  normal = GetNormalFromTextureCoord();
110  }
111 
112  public void OverridePoint(Vector3 pos)
113  {
114  point = pos;
115  }
116 
117  public override string ToString()
118  {
119  return string.Format(
120  "Collider: {1}{0}Game object: {2}{0}Distance: {3}{0}Normal: {4}{0}Point: {5}{0}Texture coord: {6}{0}Texture coord 2: {7}",
121  Environment.NewLine,
122  this.Collider != null ? this.Collider.ToString() : "None",
123  this.Collider != null ? this.Collider.gameObject.ToString() : "None",
124  this.Distance,
125  this.Normal,
126  this.Point,
127  this.TextureCoord,
128  this.TextureCoord2);
129  }
130 
131  public static readonly RaycastResultHelper None = default(RaycastResultHelper);
132  }
133 }
RaycastResultHelper(RaycastHit hit, LayerMask surface)
RaycastResultHelper(Collider collider, Vector3 point, Vector3 normal, float distance, Vector2 textureCoord, Vector2 textureCoord2, LayerMask surface)