Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Vector4.cs
浏览该文件的文档.
1namespace Engine {
2 public struct Vector4 : IEquatable<Vector4> {
3 public float X;
4
5 public float Y;
6
7 public float Z;
8
9 public float W;
10
11 public static readonly Vector4 Zero = new(0f);
12
13 public static readonly Vector4 One = new(1f);
14
15 public static readonly Vector4 UnitX = new(1f, 0f, 0f, 0f);
16
17 public static readonly Vector4 UnitY = new(0f, 1f, 0f, 0f);
18
19 public static readonly Vector4 UnitZ = new(0f, 0f, 1f, 0f);
20
21 public static readonly Vector4 UnitW = new(0f, 0f, 0f, 1f);
22
23 public Vector4(float v) {
24 X = v;
25 Y = v;
26 Z = v;
27 W = v;
28 }
29
30 public Vector4(float x, float y, float z, float w) {
31 X = x;
32 Y = y;
33 Z = z;
34 W = w;
35 }
36
37 public Vector4(Vector3 xyz, float w) {
38 X = xyz.X;
39 Y = xyz.Y;
40 Z = xyz.Z;
41 W = w;
42 }
43
44 public Vector4(Color c) {
45 X = c.R / 255f;
46 Y = c.G / 255f;
47 Z = c.B / 255f;
48 W = c.A / 255f;
49 }
50
51 public static implicit operator Vector4((float X, float Y, float Z, float W) v) => new(v.X, v.Y, v.Z, v.W);
52
53 public override bool Equals(object obj) => obj is Vector4 && Equals((Vector4)obj);
54
55 public override int GetHashCode() => X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode() + W.GetHashCode();
56
57 public override string ToString() => $"{X},{Y},{Z},{W}";
58
59 public bool Equals(Vector4 other) => X == other.X && Y == other.Y && Z == other.Z && W == other.W;
60
61 public static float Distance(Vector4 v1, Vector4 v2) => MathF.Sqrt(DistanceSquared(v1, v2));
62
63 public static float DistanceSquared(Vector4 v1, Vector4 v2) =>
64 MathUtils.Sqr(v1.X - v2.X) + MathUtils.Sqr(v1.Y - v2.Y) + MathUtils.Sqr(v1.Z - v2.Z) + MathUtils.Sqr(v1.W - v2.W);
65
66 public static float Dot(Vector4 v1, Vector4 v2) => v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z + v1.W * v2.W;
67
68 public float Length() => MathF.Sqrt(LengthSquared());
69
70 public float LengthSquared() => X * X + Y * Y + Z * Z;
71
72 public static Vector4 Floor(Vector4 v) => new(MathF.Floor(v.X), MathF.Floor(v.Y), MathF.Floor(v.Z), MathF.Floor(v.W));
73
74 public static Vector4 Ceiling(Vector4 v) => new(MathF.Ceiling(v.X), MathF.Ceiling(v.Y), MathF.Ceiling(v.Z), MathF.Ceiling(v.W));
75
76 public static Vector4 Round(Vector4 v) => new(MathF.Round(v.X), MathF.Round(v.Y), MathF.Round(v.Z), MathF.Round(v.W));
77
78 public static Vector4 Min(Vector4 v, float f) => new(MathF.Min(v.X, f), MathF.Min(v.Y, f), MathF.Min(v.Z, f), MathF.Min(v.W, f));
79
80 public static Vector4 Min(Vector4 v1, Vector4 v2) => new(
81 MathF.Min(v1.X, v2.X),
82 MathF.Min(v1.Y, v2.Y),
83 MathF.Min(v1.Z, v2.Z),
84 MathF.Min(v1.W, v2.W)
85 );
86
87 public static Vector4 Max(Vector4 v, float f) => new(MathF.Max(v.X, f), MathF.Max(v.Y, f), MathF.Max(v.Z, f), MathF.Max(v.W, f));
88
89 public static Vector4 Max(Vector4 v1, Vector4 v2) => new(
90 MathF.Max(v1.X, v2.X),
91 MathF.Max(v1.Y, v2.Y),
92 MathF.Max(v1.Z, v2.Z),
93 MathF.Max(v1.W, v2.W)
94 );
95
96 public static Vector4 Clamp(Vector4 v, float min, float max) => new(
97 Math.Clamp(v.X, min, max),
98 Math.Clamp(v.Y, min, max),
99 Math.Clamp(v.Z, min, max),
100 Math.Clamp(v.W, min, max)
101 );
102
103 public static Vector4 Saturate(Vector4 v) => new(
108 );
109
110 public static Vector4 Lerp(Vector4 v1, Vector4 v2, float f) => new(
111 MathUtils.Lerp(v1.X, v2.X, f),
112 MathUtils.Lerp(v1.Y, v2.Y, f),
113 MathUtils.Lerp(v1.Z, v2.Z, f),
114 MathUtils.Lerp(v1.W, v2.W, f)
115 );
116
117 public static Vector4 CatmullRom(Vector4 v1, Vector4 v2, Vector4 v3, Vector4 v4, float f) => new(
118 MathUtils.CatmullRom(v1.X, v2.X, v3.X, v4.X, f),
119 MathUtils.CatmullRom(v1.Y, v2.Y, v3.Y, v4.Y, f),
120 MathUtils.CatmullRom(v1.Z, v2.Z, v3.Z, v4.Z, f),
121 MathUtils.CatmullRom(v1.W, v2.W, v3.W, v4.W, f)
122 );
123
124 public static Vector4 Normalize(Vector4 v) {
125 float num = v.Length();
126 return !(num > 0f) ? UnitX : v / num;
127 }
128
129 public static Vector4 LimitLength(Vector4 v, float maxLength) {
130 float num = v.LengthSquared();
131 return num > maxLength * maxLength ? v * (maxLength / MathF.Sqrt(num)) : v;
132 }
133
134 public static Vector4 Transform(Vector4 v, Matrix m) => new(
135 v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31 + m.M41,
136 v.X * m.M12 + v.Y * m.M22 + v.Z * m.M32 + m.M42,
137 v.X * m.M13 + v.Y * m.M23 + v.Z * m.M33 + m.M43,
138 v.X * m.M14 + v.Y * m.M24 + v.Z * m.M34 + m.M44
139 );
140
141 public static void Transform(ref Vector4 v, ref Matrix m, out Vector4 result) {
142 result = new Vector4(
143 v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31 + m.M41,
144 v.X * m.M12 + v.Y * m.M22 + v.Z * m.M32 + m.M42,
145 v.X * m.M13 + v.Y * m.M23 + v.Z * m.M33 + m.M43,
146 v.X * m.M14 + v.Y * m.M24 + v.Z * m.M34 + m.M44
147 );
148 }
149
150 public static void Transform(Vector4[] sourceArray,
151 int sourceIndex,
152 ref Matrix m,
153 Vector4[] destinationArray,
154 int destinationIndex,
155 int count) {
156 for (int i = 0; i < count; i++) {
157 Vector4 vector = sourceArray[sourceIndex + i];
158 destinationArray[destinationIndex + i] = new Vector4(
159 vector.X * m.M11 + vector.Y * m.M21 + vector.Z * m.M31 + m.M41,
160 vector.X * m.M12 + vector.Y * m.M22 + vector.Z * m.M32 + m.M42,
161 vector.X * m.M13 + vector.Y * m.M23 + vector.Z * m.M33 + m.M43,
162 vector.X * m.M14 + vector.Y * m.M24 + vector.Z * m.M34 + m.M44
163 );
164 }
165 }
166
167 public static bool operator ==(Vector4 v1, Vector4 v2) => v1.Equals(v2);
168
169 public static bool operator !=(Vector4 v1, Vector4 v2) => !v1.Equals(v2);
170
171 public static Vector4 operator +(Vector4 v) => v;
172
173 public static Vector4 operator -(Vector4 v) => new(0f - v.X, 0f - v.Y, 0f - v.Z, 0f - v.W);
174
175 public static Vector4 operator +(Vector4 v1, Vector4 v2) => new(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z, v1.W + v2.W);
176
177 public static Vector4 operator -(Vector4 v1, Vector4 v2) => new(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z, v1.W - v2.W);
178
179 public static Vector4 operator *(Vector4 v1, Vector4 v2) => new(v1.X * v2.X, v1.Y * v2.Y, v1.Z * v2.Z, v1.W * v2.W);
180
181 public static Vector4 operator *(Vector4 v, float s) => new(v.X * s, v.Y * s, v.Z * s, v.W * s);
182
183 public static Vector4 operator *(float s, Vector4 v) => new(v.X * s, v.Y * s, v.Z * s, v.W * s);
184
185 public static Vector4 operator /(Vector4 v1, Vector4 v2) => new(v1.X / v2.X, v1.Y / v2.Y, v1.Z / v2.Z, v1.W / v2.W);
186
187 public static Vector4 operator /(Vector4 v, float d) {
188 float num = 1f / d;
189 return new Vector4(v.X * num, v.Y * num, v.Z * num, v.W * num);
190 }
191
192 public static Vector4 FixNaN(Vector4 v) {
193 if (float.IsNaN(v.X)) {
194 v.X = 0;
195 }
196 if (float.IsNaN(v.Y)) {
197 v.Y = 0;
198 }
199 if (float.IsNaN(v.Z)) {
200 v.Z = 0;
201 }
202 if (float.IsNaN(v.W)) {
203 v.W = 0;
204 }
205 return v;
206 }
207
208 public Vector4 FixNaN() {
209 if (float.IsNaN(X)) {
210 X = 0;
211 }
212 if (float.IsNaN(Y)) {
213 Y = 0;
214 }
215 if (float.IsNaN(Z)) {
216 Z = 0;
217 }
218 if (float.IsNaN(W)) {
219 W = 0;
220 }
221 return this;
222 }
223
224 public unsafe Span<float> AsSpan() {
225 fixed (float* ptr = &X) {
226 return new Span<float>(ptr, 4);
227 }
228 }
229
230 public unsafe float* AsPointer() {
231 fixed (float* ptr = &X) {
232 return ptr;
233 }
234 }
235
236 public Vector2 XY => new(X, Y);
237 public Vector2 XZ => new(X, Z);
238 public Vector2 XW => new(X, W);
239 public Vector2 YZ => new(Y, Z);
240 public Vector2 YW => new(Y, W);
241 public Vector2 ZW => new(Z, W);
242 public Vector3 XYZ => new(X, Y, Z);
243 public Vector3 XYW => new(X, Y, W);
244 public Vector3 XZW => new(X, Z, W);
245 public Vector3 YZW => new(Y, Z, W);
246 }
247}
unsafe
定义 Main.cs:15
static float Saturate(float x)
static int Sqr(int x)
static float CatmullRom(float v1, float v2, float v3, float v4, float f)
static float Lerp(float x1, float x2, float f)
static Vector4 operator+(Vector4 v)
static Vector4 Max(Vector4 v1, Vector4 v2)
static Vector4 Round(Vector4 v)
static readonly Vector4 UnitW
static bool operator==(Vector4 v1, Vector4 v2)
static bool operator!=(Vector4 v1, Vector4 v2)
static Vector4 CatmullRom(Vector4 v1, Vector4 v2, Vector4 v3, Vector4 v4, float f)
static void Transform(ref Vector4 v, ref Matrix m, out Vector4 result)
static Vector4 FixNaN(Vector4 v)
static readonly Vector4 One
static Vector4 Lerp(Vector4 v1, Vector4 v2, float f)
unsafe Span< float > AsSpan()
static Vector4 LimitLength(Vector4 v, float maxLength)
static void Transform(Vector4[] sourceArray, int sourceIndex, ref Matrix m, Vector4[] destinationArray, int destinationIndex, int count)
static Vector4 Max(Vector4 v, float f)
static readonly Vector4 UnitZ
float LengthSquared()
static Vector4 operator/(Vector4 v1, Vector4 v2)
static float Dot(Vector4 v1, Vector4 v2)
Vector4(float x, float y, float z, float w)
static Vector4 Floor(Vector4 v)
override string ToString()
bool Equals(Vector4 other)
static Vector4 Saturate(Vector4 v)
static float DistanceSquared(Vector4 v1, Vector4 v2)
static Vector4 operator*(Vector4 v1, Vector4 v2)
static Vector4 operator-(Vector4 v)
static Vector4 Transform(Vector4 v, Matrix m)
static Vector4 Ceiling(Vector4 v)
override bool Equals(object obj)
static readonly Vector4 UnitX
static readonly Vector4 Zero
static Vector4 Min(Vector4 v, float f)
override int GetHashCode()
Vector4(Vector3 xyz, float w)
static readonly Vector4 UnitY
static Vector4 Normalize(Vector4 v)
unsafe float * AsPointer()
static float Distance(Vector4 v1, Vector4 v2)
static Vector4 Clamp(Vector4 v, float min, float max)
static Vector4 Min(Vector4 v1, Vector4 v2)