Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
CellFace.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
4 public struct CellFace : IEquatable<CellFace> {
5 public int X;
6
7 public int Y;
8
9 public int Z;
10
11 public int Face;
12
13 public static readonly int[] m_oppositeFaces = [2, 3, 0, 1, 5, 4];
14
15 public static readonly Point3[] m_faceToPoint3 = [new(0, 0, 1), new(1, 0, 0), new(0, 0, -1), new(-1, 0, 0), new(0, 1, 0), new(0, -1, 0)];
16
17 public static readonly Vector3[] m_faceToVector3 = [
18 new(0f, 0f, 1f), new(1f, 0f, 0f), new(0f, 0f, -1f), new(-1f, 0f, 0f), new(0f, 1f, 0f), new(0f, -1f, 0f)
19 ];
20
21 public static readonly int[][] m_faceToTangents = [[1, 4, 3, 5], [4, 0, 5, 2], [4, 1, 5, 3], [0, 4, 2, 5], [0, 1, 2, 3], [1, 0, 3, 2]];
22
23 public Point3 Point {
24 get => new(X, Y, Z);
25 set {
26 X = value.X;
27 Y = value.Y;
28 Z = value.Z;
29 }
30 }
31
32 public CellFace(int x, int y, int z, int face) {
33 X = x;
34 Y = y;
35 Z = z;
36 Face = face;
37 }
38
39 public CellFace(Point3 point, int face) {
40 X = point.X;
41 Y = point.Y;
42 Z = point.Z;
43 Face = face;
44 }
45
46 public static int OppositeFace(int face) => m_oppositeFaces[face];
47
48 public static Point3 FaceToPoint3(int face) => m_faceToPoint3[face];
49
50 public static Vector3 FaceToVector3(int face) => m_faceToVector3[face];
51
52 public static int[] FaceToTangents(int face) => m_faceToTangents[face];
53
54 public static int Point3ToFace(Point3 p, int maxFace = 5) {
55 for (int i = 0; i < maxFace; i++) {
56 if (m_faceToPoint3[i] == p) {
57 return i;
58 }
59 }
60 throw new InvalidOperationException("Invalid Point3.");
61 }
62
63 public static int Vector3ToFace(Vector3 v, int maxFace = 5) {
64 float num = -1f / 0f;
65 int result = 0;
66 for (int i = 0; i <= maxFace; i++) {
67 float num2 = Vector3.Dot(m_faceToVector3[i], v);
68 if (num2 > num) {
69 result = i;
70 num = num2;
71 }
72 }
73 return result;
74 }
75
76 public static CellFace FromAxisAndDirection(int x, int y, int z, int axis, float direction) {
77 CellFace result = default;
78 result.X = x;
79 result.Y = y;
80 result.Z = z;
81 switch (axis) {
82 case 0: result.Face = direction > 0f ? 1 : 3; break;
83 case 1: result.Face = direction > 0f ? 4 : 5; break;
84 case 2: result.Face = !(direction > 0f) ? 2 : 0; break;
85 }
86 return result;
87 }
88
90 switch (Face) {
91 case 0: return new Plane(new Vector3(0f, 0f, 1f), -(Z + 1));
92 case 1: return new Plane(new Vector3(-1f, 0f, 0f), X + 1);
93 case 2: return new Plane(new Vector3(0f, 0f, -1f), Z);
94 case 3: return new Plane(new Vector3(1f, 0f, 0f), -X);
95 case 4: return new Plane(new Vector3(0f, 1f, 0f), -(Y + 1));
96 default: return new Plane(new Vector3(0f, -1f, 0f), Y);
97 }
98 }
99
100 public Vector3 GetFaceCenter(float offset = 0f) {
101 return new Vector3(X + 0.5f, Y + 0.5f, Z + 0.5f) + FaceToVector3(Face) * (0.5f + offset);
102 }
103
104 public Vector3[] GetFourVertices(float size = 1f, float offset = 0f) {
105 float halfSize = size * 0.5f;
106 Vector3 center = GetFaceCenter(offset);
107 int[] tangents = FaceToTangents(Face);
108 Vector3 tangent1 = FaceToVector3(tangents[0]) * halfSize;
109 Vector3 tangent2 = FaceToVector3(tangents[1]) * halfSize;
110 Vector3[] result = new Vector3[4];
111 result[0] = center - tangent1 - tangent2;
112 result[1] = center + tangent1 - tangent2;
113 result[2] = center + tangent1 + tangent2;
114 result[3] = center - tangent1 + tangent2;
115 return result;
116 }
117
118 public Vector3[] GetSixVertices(float size = 1f, float offset = 0f) {
119 float halfSize = size * 0.5f;
120 Vector3 center = GetFaceCenter(offset);
121 int[] tangents = FaceToTangents(Face);
122 Vector3 tangent1 = FaceToVector3(tangents[0]) * halfSize;
123 Vector3 tangent2 = FaceToVector3(tangents[1]) * halfSize;
124 Vector3 v0 = center - tangent1 - tangent2;
125 Vector3 v1 = center + tangent1 - tangent2;
126 Vector3 v2 = center + tangent1 + tangent2;
127 Vector3 v3 = center - tangent1 + tangent2;
128 return [
129 // Triangle 1
130 v0,
131 v1,
132 v2,
133 // Triangle 2
134 v2,
135 v3,
136 v0
137 ];
138 }
139
140 public override int GetHashCode() => (X << 11) + (Y << 7) + (Z << 3) + Face;
141
142 public override bool Equals(object obj) {
143 if (!(obj is CellFace)) {
144 return false;
145 }
146 return Equals((CellFace)obj);
147 }
148
149 public bool Equals(CellFace other) {
150 if (other.X == X
151 && other.Y == Y
152 && other.Z == Z) {
153 return other.Face == Face;
154 }
155 return false;
156 }
157
158 public override string ToString() => $"{X}, {Y}, {Z}, face {Face}";
159
160 public static bool operator ==(CellFace c1, CellFace c2) => c1.Equals(c2);
161
162 public static bool operator !=(CellFace c1, CellFace c2) => !c1.Equals(c2);
163 }
164}
Engine.Vector3 Vector3
static float Dot(Vector3 v1, Vector3 v2)
static readonly Vector3[] m_faceToVector3
override bool Equals(object obj)
bool Equals(CellFace other)
Vector3[] GetSixVertices(float size=1f, float offset=0f)
CellFace(int x, int y, int z, int face)
override int GetHashCode()
static int[] FaceToTangents(int face)
Plane CalculatePlane()
override string ToString()
static readonly Point3[] m_faceToPoint3
static readonly int[][] m_faceToTangents
static int Vector3ToFace(Vector3 v, int maxFace=5)
static int OppositeFace(int face)
static bool operator!=(CellFace c1, CellFace c2)
Vector3 GetFaceCenter(float offset=0f)
static Vector3 FaceToVector3(int face)
static readonly int[] m_oppositeFaces
Vector3[] GetFourVertices(float size=1f, float offset=0f)
static bool operator==(CellFace c1, CellFace c2)
static Point3 FaceToPoint3(int face)
static int Point3ToFace(Point3 p, int maxFace=5)
CellFace(Point3 point, int face)
static CellFace FromAxisAndDirection(int x, int y, int z, int axis, float direction)