Plane.cs
3.8 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EzySlice {
/**
* Quick Internal structure which checks where the point lays on the
* Plane. UP = Upwards from the Normal, DOWN = Downwards from the Normal
* ON = Point lays straight on the plane
*/
public enum SideOfPlane {
UP,
DOWN,
ON
}
/**
* Represents a simple 3D Plane structure with a position
* and direction which extends infinitely in its axis. This provides
* an optimal structure for collision tests for the slicing framework.
*/
public struct Plane {
private Vector3 m_normal;
private float m_dist;
// this is for editor debugging only! do NOT try to access this
// variable at runtime, we will be stripping it out for final
// builds
#if UNITY_EDITOR
private Transform trans_ref;
#endif
public Plane(Vector3 pos, Vector3 norm) {
this.m_normal = norm;
this.m_dist = Vector3.Dot(norm, pos);
// this is for editor debugging only!
#if UNITY_EDITOR
trans_ref = null;
#endif
}
public Plane(Vector3 norm, float dot) {
this.m_normal = norm;
this.m_dist = dot;
// this is for editor debugging only!
#if UNITY_EDITOR
trans_ref = null;
#endif
}
public Plane(Vector3 a, Vector3 b, Vector3 c) {
m_normal = Vector3.Normalize(Vector3.Cross(b - a, c - a));
m_dist = -Vector3.Dot(m_normal, a);
// this is for editor debugging only!
#if UNITY_EDITOR
trans_ref = null;
#endif
}
public void Compute(Vector3 pos, Vector3 norm) {
this.m_normal = norm;
this.m_dist = Vector3.Dot(norm, pos);
}
public void Compute(Transform trans) {
Compute(trans.position, trans.up);
// this is for editor debugging only!
#if UNITY_EDITOR
trans_ref = trans;
#endif
}
public void Compute(GameObject obj) {
Compute(obj.transform);
}
public Vector3 normal {
get { return this.m_normal; }
}
public float dist {
get { return this.m_dist; }
}
/**
* Checks which side of the plane the point lays on.
*/
public SideOfPlane SideOf(Vector3 pt) {
float result = Vector3.Dot(m_normal, pt) - m_dist;
if (result > Intersector.Epsilon) {
return SideOfPlane.UP;
}
if (result < -Intersector.Epsilon) {
return SideOfPlane.DOWN;
}
return SideOfPlane.ON;
}
/**
* Editor only DEBUG functionality. This should not be compiled in the final
* Version.
*/
public void OnDebugDraw() {
OnDebugDraw(Color.white);
}
public void OnDebugDraw(Color drawColor) {
// NOTE -> Gizmos are only supported in the editor. We will keep these function
// signatures for consistancy however at final build, these will do nothing
// TO/DO -> Should we throw a runtime exception if this function tried to get executed
// at runtime?
#if UNITY_EDITOR
if (trans_ref == null) {
return;
}
Color prevColor = Gizmos.color;
Matrix4x4 prevMatrix = Gizmos.matrix;
// TO-DO
Gizmos.matrix = Matrix4x4.TRS(trans_ref.position, trans_ref.rotation, trans_ref.localScale);
Gizmos.color = drawColor;
Gizmos.DrawWireCube(Vector3.zero, new Vector3(1.0f, 0.0f, 1.0f));
Gizmos.color = prevColor;
Gizmos.matrix = prevMatrix;
#endif
}
}
}