Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
BoundingFrustum.cs
浏览该文件的文档.
1namespace Engine {
2 public class BoundingFrustum : IEquatable<BoundingFrustum> {
4
5 Plane[] m_planes = new Plane[6];
6
8
10
11 public Plane Near => m_planes[0];
12
13 public Plane Far => m_planes[1];
14
15 public Plane Left => m_planes[2];
16
17 public Plane Right => m_planes[3];
18
19 public Plane Top => m_planes[4];
20
21 public Plane Bottom => m_planes[5];
22
23 public Matrix Matrix {
24 get => m_viewProjection;
25 set {
26 m_viewProjection = value;
27 m_planes[0].Normal.X = 0f - value.M13;
28 m_planes[0].Normal.Y = 0f - value.M23;
29 m_planes[0].Normal.Z = 0f - value.M33;
30 m_planes[0].D = 0f - value.M43;
31 m_planes[1].Normal.X = 0f - value.M14 + value.M13;
32 m_planes[1].Normal.Y = 0f - value.M24 + value.M23;
33 m_planes[1].Normal.Z = 0f - value.M34 + value.M33;
34 m_planes[1].D = 0f - value.M44 + value.M43;
35 m_planes[2].Normal.X = 0f - value.M14 - value.M11;
36 m_planes[2].Normal.Y = 0f - value.M24 - value.M21;
37 m_planes[2].Normal.Z = 0f - value.M34 - value.M31;
38 m_planes[2].D = 0f - value.M44 - value.M41;
39 m_planes[3].Normal.X = 0f - value.M14 + value.M11;
40 m_planes[3].Normal.Y = 0f - value.M24 + value.M21;
41 m_planes[3].Normal.Z = 0f - value.M34 + value.M31;
42 m_planes[3].D = 0f - value.M44 + value.M41;
43 m_planes[4].Normal.X = 0f - value.M14 + value.M12;
44 m_planes[4].Normal.Y = 0f - value.M24 + value.M22;
45 m_planes[4].Normal.Z = 0f - value.M34 + value.M32;
46 m_planes[4].D = 0f - value.M44 + value.M42;
47 m_planes[5].Normal.X = 0f - value.M14 - value.M12;
48 m_planes[5].Normal.Y = 0f - value.M24 - value.M22;
49 m_planes[5].Normal.Z = 0f - value.M34 - value.M32;
50 m_planes[5].D = 0f - value.M44 - value.M42;
51 for (int i = 0; i < 6; i++) {
52 float num = m_planes[i].Normal.Length();
53 m_planes[i].Normal /= num;
54 m_planes[i].D /= num;
55 }
56 m_cornersValid = false;
57 }
58 }
59
60 public ReadOnlyList<Vector3> Corners {
61 get {
62 if (!m_cornersValid) {
63 m_cornersValid = true;
76 }
77 return new ReadOnlyList<Vector3>(m_corners);
78 }
79 }
80
81 public BoundingFrustum(Matrix viewProjection) => Matrix = viewProjection;
82
83 public override bool Equals(object obj) {
84 BoundingFrustum boundingFrustum = obj as BoundingFrustum;
85 return boundingFrustum != null && m_viewProjection == boundingFrustum.m_viewProjection;
86 }
87
88 public override int GetHashCode() =>
89 // ReSharper disable NonReadonlyMemberInGetHashCode
90 m_viewProjection.GetHashCode();
91 // ReSharper restore NonReadonlyMemberInGetHashCode
92
93 public bool Equals(BoundingFrustum other) => other != null && m_viewProjection == other.m_viewProjection;
94
95 public override string ToString() => m_viewProjection.ToString();
96
97 public bool Intersection(Vector3 point) {
98 for (int i = 0; i < m_planes.Length; i++) {
99 float x = m_planes[i].Normal.X;
100 float y = m_planes[i].Normal.Y;
101 float z = m_planes[i].Normal.Z;
102 float d = m_planes[i].D;
103 if (x * point.X + y * point.Y + z * point.Z + d > 0f) {
104 return false;
105 }
106 }
107 return true;
108 }
109
110 public bool Intersection(BoundingSphere sphere) {
111 for (int i = 0; i < m_planes.Length; i++) {
112 float x = m_planes[i].Normal.X;
113 float y = m_planes[i].Normal.Y;
114 float z = m_planes[i].Normal.Z;
115 float d = m_planes[i].D;
116 if (x * sphere.Center.X + y * sphere.Center.Y + z * sphere.Center.Z + d > sphere.Radius) {
117 return false;
118 }
119 }
120 return true;
121 }
122
123 public bool Intersection(BoundingBox box) {
124 for (int i = 0; i < m_planes.Length; i++) {
125 float x = m_planes[i].Normal.X;
126 float y = m_planes[i].Normal.Y;
127 float z = m_planes[i].Normal.Z;
128 float d = m_planes[i].D;
129 float num = x > 0f ? box.Min.X : box.Max.X;
130 float num2 = y > 0f ? box.Min.Y : box.Max.Y;
131 float num3 = z > 0f ? box.Min.Z : box.Max.Z;
132 if (x * num + y * num2 + z * num3 + d > 0f) {
133 return false;
134 }
135 }
136 return true;
137 }
138
139 public static bool operator ==(BoundingFrustum f1, BoundingFrustum f2) => Equals(f1, f2);
140
141 public static bool operator !=(BoundingFrustum f1, BoundingFrustum f2) => !Equals(f1, f2);
142
143 public static Vector3 ComputeIntersection(Plane plane, Ray3 ray) {
144 float s = (0f - plane.D - Vector3.Dot(plane.Normal, ray.Position)) / Vector3.Dot(plane.Normal, ray.Direction);
145 return ray.Position + ray.Direction * s;
146 }
147
148 public static Ray3 ComputeIntersectionLine(Plane p1, Plane p2) {
149 Ray3 result = default;
150 result.Direction = Vector3.Cross(p1.Normal, p2.Normal);
151 float d = result.Direction.LengthSquared();
152 result.Position = Vector3.Cross((0f - p1.D) * p2.Normal + p2.D * p1.Normal, result.Direction) / d;
153 return result;
154 }
155 }
156}
static bool operator==(BoundingFrustum f1, BoundingFrustum f2)
ReadOnlyList< Vector3 > Corners
bool Intersection(Vector3 point)
bool Equals(BoundingFrustum other)
bool Intersection(BoundingSphere sphere)
bool Intersection(BoundingBox box)
override bool Equals(object obj)
static Vector3 ComputeIntersection(Plane plane, Ray3 ray)
BoundingFrustum(Matrix viewProjection)
static Ray3 ComputeIntersectionLine(Plane p1, Plane p2)
static bool operator!=(BoundingFrustum f1, BoundingFrustum f2)
Vector3 Normal
定义 Plane.cs:3
Vector3 Position
定义 Ray3.cs:3
Vector3 Direction
定义 Ray3.cs:5
static Vector3 Cross(Vector3 v1, Vector3 v2)
float LengthSquared()
static float Dot(Vector3 v1, Vector3 v2)