Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Viewport.cs
浏览该文件的文档.
1namespace Engine.Graphics {
2 public struct Viewport : IEquatable<Viewport> {
3 public int X;
4
5 public int Y;
6
7 public int Width;
8
9 public int Height;
10
11 public float MinDepth;
12
13 public float MaxDepth;
14
15 public Rectangle Rectangle => new(X, Y, Width, Height);
16
17 public float AspectRatio => Width / (float)Height;
18
19 public Viewport(int x, int y, int width, int height, float minDepth = 0f, float maxDepth = 1f) {
20 X = x;
21 Y = y;
22 Width = width;
23 Height = height;
24 MinDepth = minDepth;
25 MaxDepth = maxDepth;
26 }
27
28 public bool Equals(Viewport other) => X == other.X
29 && Y == other.Y
30 && Width == other.Width
31 && Height == other.Height
32 && MinDepth == other.MinDepth
33 && MaxDepth == other.MaxDepth;
34
35 public override bool Equals(object obj) => obj is Viewport && Equals(this);
36
37 public override int GetHashCode() => X.GetHashCode()
38 + Y.GetHashCode()
39 + Width.GetHashCode()
40 + Height.GetHashCode()
41 + MinDepth.GetHashCode()
42 + MaxDepth.GetHashCode();
43
44 public override string ToString() => $"{X}, {Y}, {Width}, {Height}, {MinDepth}, {MaxDepth}";
45
46 public Vector3 Project(Vector3 source, Matrix worldViewProjection) {
47 Vector3 result = Vector3.Transform(source, worldViewProjection);
48 result /= source.X * worldViewProjection.M14
49 + source.Y * worldViewProjection.M24
50 + source.Z * worldViewProjection.M34
51 + worldViewProjection.M44;
52 result.X = (result.X + 1f) * 0.5f * Width + X;
53 result.Y = (0f - result.Y + 1f) * 0.5f * Height + Y;
54 result.Z = result.Z * (MaxDepth - MinDepth) + MinDepth;
55 return result;
56 }
57
58 public Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world) => Project(source, world * view * projection);
59
60 public Vector3 Unproject(Vector3 source, Matrix worldViewProjection) {
61 Matrix m = Matrix.Invert(worldViewProjection);
62 source.X = (source.X - X) / Width * 2f - 1f;
63 source.Y = 0f - ((source.Y - Y) / Height * 2f - 1f);
64 source.Z = (source.Z - MinDepth) / (MaxDepth - MinDepth);
65 return Vector3.Transform(source, m) / (source.X * m.M14 + source.Y * m.M24 + source.Z * m.M34 + m.M44);
66 }
67
68 public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world) => Unproject(source, world * view * projection);
69
70 public static bool operator ==(Viewport v1, Viewport v2) => v1.Equals(v2);
71
72 public static bool operator !=(Viewport v1, Viewport v2) => !v1.Equals(v2);
73 }
74}
override string ToString()
Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
bool Equals(Viewport other)
static bool operator!=(Viewport v1, Viewport v2)
static bool operator==(Viewport v1, Viewport v2)
override bool Equals(object obj)
Viewport(int x, int y, int width, int height, float minDepth=0f, float maxDepth=1f)
Vector3 Unproject(Vector3 source, Matrix worldViewProjection)
Vector3 Project(Vector3 source, Matrix worldViewProjection)
static Matrix Invert(Matrix m)
static Vector3 Transform(Vector3 v, Matrix m)