forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaycastHelper.cs
More file actions
186 lines (153 loc) · 6.75 KB
/
RaycastHelper.cs
File metadata and controls
186 lines (153 loc) · 6.75 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
//
using System.Collections.Generic;
using UnityEngine;
namespace HoloToolkit.Unity
{
public static class RaycastHelper
{
public static bool DebugEnabled = false;
public delegate bool RaycastFunc(Vector3 origin, Vector3 direction, float distance, LayerMask layerMask, out RaycastResultHelper result);
public static bool First(Vector3 origin, Vector3 direction, float distance, LayerMask layerMask, out RaycastResultHelper result)
{
result = default(RaycastResultHelper);
RaycastHit hit;
bool hitSomething = false;
// Check to see if the ray cast hits any of the requested Unity layers
if (layerMask != 0 &&
UnityEngine.Physics.Raycast(origin, direction, out hit, distance, layerMask))
{
result = new RaycastResultHelper(hit, layerMask);
hitSomething = true;
}
return hitSomething;
}
public static bool SphereFirst(Vector3 origin, Vector3 direction, float radius, float distance, LayerMask layerMask, out RaycastResultHelper result)
{
result = default(RaycastResultHelper);
RaycastHit hit;
bool hitSomething = false;
// Check to see if the ray cast hits any of the requested Unity layers
if (layerMask != 0 &&
UnityEngine.Physics.SphereCast(origin, radius, direction, out hit, distance, layerMask))
{
result = new RaycastResultHelper(hit, layerMask);
hitSomething = true;
}
return hitSomething;
}
public static List<RaycastResultHelper> All(Vector3 origin, Vector3 direction, float distance, LayerMask layerMask)
{
return All(origin, direction, distance, layerMask, null);
}
public static List<RaycastResultHelper> All(Vector3 origin, Vector3 direction, float distance, LayerMask layerMask, List<Collider> movedColliders)
{
// Check to see if the ray cast hits any of the requested Unity layers
List<RaycastResultHelper> results = null;
if (layerMask != 0)
{
var raycastHits = UnityEngine.Physics.RaycastAll(origin, direction, distance, layerMask);
if (raycastHits != null)
{
results = new List<RaycastResultHelper>(raycastHits.Length);
foreach (var raycastHit in raycastHits)
{
results.Add(new RaycastResultHelper(raycastHit, layerMask));
}
}
}
// If we have colliders that have moved, then remove them from the results list and redo the raycast.
if (movedColliders != null)
{
RaycastHit hitInfo;
Ray ray = new Ray(origin, direction);
foreach (var collider in movedColliders)
{
if ((collider.gameObject.layer & layerMask) != 0)
{
int colliderIndex = results.FindIndex(x => x.Collider == collider);
if (collider.Raycast(ray, out hitInfo, distance))
{
if (colliderIndex >= 0)
{
results[colliderIndex] = new RaycastResultHelper(hitInfo, layerMask);
}
else
{
results.Add(new RaycastResultHelper(hitInfo, layerMask));
}
}
else if (colliderIndex >= 0)
{
results.RemoveAt(colliderIndex);
}
}
}
}
// Unity doesn't return hit results in any particular order, so we need to sort them to closest first.
if (results != null)
{
results.Sort((x, y) => x.Distance < y.Distance ? -1 : 1);
}
return results;
}
public static Vector3 GetBoxColliderExtents(BoxCollider boxCollider)
{
return boxCollider.size;
}
// raysPerEdge should be odd
public static bool CastBoxExtents(Vector3 extents, Vector3 targetPosition, Matrix4x4 trs, Ray ray, float maxDistance, LayerMask surface, RaycastFunc raycastFunc, int raysPerEdge, bool ortho, out Vector3[] points, out Vector3[] normals, out bool[] hits)
{
bool debugEnabled = DebugEnabled;
if (debugEnabled)
{
Debug.DrawLine(ray.origin, ray.origin + ray.direction * 10.0f, Color.green);
}
extents /= (raysPerEdge - 1);
int halfRaysPerEdge = (raysPerEdge - 1) / 2;
int numRays = raysPerEdge * raysPerEdge;
bool hitSomething = false;
points = new Vector3[numRays];
normals = new Vector3[numRays];
hits = new bool[numRays];
int index = 0;
for (int x = -halfRaysPerEdge; x <= halfRaysPerEdge; x += 1)
{
for (int y = -halfRaysPerEdge; y <= halfRaysPerEdge; y += 1)
{
Vector3 offset = trs.MultiplyVector(new Vector3(x * extents.x, y * extents.y, 0));
Vector3 origin = ray.origin;
Vector3 direction = (targetPosition + offset) - ray.origin;
if (ortho)
{
origin += offset;
direction = ray.direction;
}
RaycastResultHelper rayHit;
hits[index] = raycastFunc(origin, direction, maxDistance, surface, out rayHit);
if (hits[index])
{
hitSomething = true;
points[index] = rayHit.Point;
normals[index] = rayHit.Normal;
if (debugEnabled)
{
Debug.DrawLine(origin, points[index], Color.yellow);
}
}
else
{
if (debugEnabled)
{
Debug.DrawLine(origin, origin + direction * 3.0f, Color.gray);
}
}
index++;
}
}
return hitSomething;
}
}
}