-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVoxelNormal.cs
105 lines (92 loc) · 2.37 KB
/
VoxelNormal.cs
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
using System;
namespace Fydar.Vox.Meshing
{
public struct VoxelNormal : IEquatable<VoxelNormal>
{
private static readonly Vector3SByte[] vectorMapping = new Vector3SByte[]
{
new Vector3SByte(0, 1, 0), // Up
new Vector3SByte(0, -1, 0), // Down
new Vector3SByte(-1, 0, 0), // Left
new Vector3SByte(1, 0, 0), // Right
new Vector3SByte(0, 0, 1), // Forward
new Vector3SByte(0, 0, -1), // Back
};
private static readonly string[] nameMapping = new string[]
{
"Up",
"Down",
"Left",
"Right",
"Forward",
"Back",
};
public static VoxelNormal Up => new VoxelNormal(0);
public static VoxelNormal Down => new VoxelNormal(1);
public static VoxelNormal Left => new VoxelNormal(2);
public static VoxelNormal Right => new VoxelNormal(3);
public static VoxelNormal Forward => new VoxelNormal(4);
public static VoxelNormal Back => new VoxelNormal(5);
public byte data;
public VoxelNormal(byte data)
{
this.data = data;
}
public Vector3SByte ToVector()
{
return vectorMapping[data];
}
public override string ToString()
{
return nameMapping[data];
}
public static Vector3SByte RotateAroundOrigin(Vector3SByte position, VoxelNormal normal)
{
if (normal == Forward)
{
return new Vector3SByte(position.x, position.y, position.z);
}
else if (normal == Back)
{
return new Vector3SByte(position.x, position.y, -position.z);
}
else if (normal == Left)
{
return new Vector3SByte(position.z, position.y, -position.x);
}
else if (normal == Right)
{
return new Vector3SByte(position.z, position.y, position.x);
}
else if (normal == Up)
{
return new Vector3SByte(position.x, position.z, position.y);
}
else if (normal == Down)
{
return new Vector3SByte(position.x, position.z, -position.y);
}
throw new InvalidOperationException($"The normal {normal} is unsupported.");
}
public override bool Equals(object obj)
{
return obj is VoxelNormal normal && Equals(normal);
}
public bool Equals(VoxelNormal other)
{
return data == other.data;
}
public override int GetHashCode()
{
return 1768953197 + data.GetHashCode();
}
public static bool operator ==(VoxelNormal left, VoxelNormal right)
{
return left.Equals(right);
}
public static bool operator !=(VoxelNormal left, VoxelNormal right)
{
return !(left == right);
}
}
}