forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaycastResultHelper.cs
More file actions
133 lines (116 loc) · 3.97 KB
/
RaycastResultHelper.cs
File metadata and controls
133 lines (116 loc) · 3.97 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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
//
using System;
using UnityEngine;
namespace HoloToolkit.Unity
{
public struct RaycastResultHelper
{
public RaycastResultHelper(RaycastHit hit, LayerMask surface) : this(hit.collider, hit.point, hit.normal, hit.distance, hit.textureCoord, hit.textureCoord2, surface)
{
}
public RaycastResultHelper(Collider collider, Vector3 point, Vector3 normal, float distance, Vector2 textureCoord, Vector2 textureCoord2, LayerMask surface)
{
this.layer = collider != null ? collider.gameObject.layer : LayerExtensions.Surface;
if (this.layer == surface.value)
{
// Spoof the mirage raycast result of 'no collider' if we hit a unity SR layer.
this.collider = null;
}
else
{
this.collider = collider;
}
this.point = point;
this.normal = normal;
this.distance = distance;
this.transform = null;
this.textureCoord = textureCoord;
this.textureCoord2 = textureCoord2;
}
private Collider collider;
public Collider Collider
{
get { return this.collider; }
private set { this.collider = value; }
}
private int layer;
public int Layer
{
get { return this.layer; }
private set { this.layer = value; }
}
private Vector3 normal;
public Vector3 Normal
{
get { return this.normal; }
private set { this.normal = value; }
}
private float distance;
public float Distance
{
get { return this.distance; }
private set { this.distance = value; }
}
private Vector3 point;
public Vector3 Point
{
get { return this.point; }
private set { this.point = value; }
}
private Transform transform;
public Transform Transform
{
get
{
if (this.transform == null &&
this.Collider != null)
{
this.transform = this.Collider.transform;
}
return this.transform;
}
}
private Vector2 textureCoord;
public Vector2 TextureCoord
{
get { return this.textureCoord; }
private set { this.textureCoord = value; }
}
private Vector2 textureCoord2;
public Vector2 TextureCoord2
{
get { return this.textureCoord2; }
private set { this.textureCoord2 = value; }
}
public Vector3 GetNormalFromTextureCoord()
{
// Assumes packed at unit length
return new Vector3(textureCoord.x, textureCoord.y, 1f - Mathf.Sqrt(textureCoord.x * textureCoord.x + textureCoord.y * textureCoord.y));
}
public void OverrideNormalFromTextureCoord()
{
normal = GetNormalFromTextureCoord();
}
public void OverridePoint(Vector3 pos)
{
point = pos;
}
public override string ToString()
{
return string.Format(
"Collider: {1}{0}Game object: {2}{0}Distance: {3}{0}Normal: {4}{0}Point: {5}{0}Texture coord: {6}{0}Texture coord 2: {7}",
Environment.NewLine,
this.Collider != null ? this.Collider.ToString() : "None",
this.Collider != null ? this.Collider.gameObject.ToString() : "None",
this.Distance,
this.Normal,
this.Point,
this.TextureCoord,
this.TextureCoord2);
}
public static readonly RaycastResultHelper None = default(RaycastResultHelper);
}
}