forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesianGridOnCubeUtility.cs
More file actions
247 lines (195 loc) · 6.81 KB
/
Copy pathCartesianGridOnCubeUtility.cs
File metadata and controls
247 lines (195 loc) · 6.81 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using Unity.Mathematics;
public static unsafe class CartesianGridOnCubeUtility
{
// Next face to move to when moving off edge of a face
public static readonly byte[] NextFaceIndex =
{
// 0 1 2 3 4 5
// X+ X- Y+ Y- Z+ Z- <- From which face
4, 4, 4, 4, 2, 2, // Off north edge
5, 5, 5, 5, 3, 3, // Off south edge
2, 3, 1, 0, 0, 1, // Off west edge
3, 2, 0, 1, 1, 0, // Off east edge
};
public static readonly byte[] NextFaceDirection =
{
// 0 1 2 3 4 5
// X+ X- Y+ Y- Z+ Z- <- From which face
3, 2, 1, 0, 1, 0, // Off north edge
2, 3, 1, 0, 1, 0, // Off south edge
2, 2, 2, 2, 1, 0, // Off west edge
3, 3, 3, 3, 1, 0, // Off east edge
};
static readonly float2[] m_GridToGrid =
{
//
// From North edge
//
// 0
new float2(0.0f, -1.0f),
new float2(-1.0f, 0.0f),
// 1
new float2(0.0f, 1.0f),
new float2(1.0f, 0.0f),
// 2
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 3
new float2(1.0f, 0.0f),
new float2(0.0f, -1.0f),
// 4
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 5
new float2(1.0f, 0.0f),
new float2(0.0f, -1.0f),
//
// From South edge
//
// 0
new float2(0.0f, -1.0f),
new float2(-1.0f, 0.0f),
// 1
new float2(0.0f, 1.0f),
new float2(1.0f, 0.0f),
// 2
new float2(1.0f, 0.0f),
new float2(0.0f, -1.0f),
// 3
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 4
new float2(1.0f, 0.0f),
new float2(0.0f, -1.0f),
// 5
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
//
// From West edge
//
// 0
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 1
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 2
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 3
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 4
new float2(0.0f, -1.0f),
new float2(-1.0f, 0.0f),
// 5
new float2(0.0f, 1.0f),
new float2(1.0f, 0.0f),
//
// From East edge
//
// 0
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 1
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 2
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 3
new float2(-1.0f, 0.0f),
new float2(0.0f, 1.0f),
// 4
new float2(0.0f, 1.0f),
new float2(1.0f, 0.0f),
// 5
new float2(0.0f, -1.0f),
new float2(-1.0f, 0.0f),
};
public static int CellFaceIndex(int cellIndex, int rowCount)
{
var cellCount = rowCount * rowCount;
var faceIndex = cellIndex / cellCount;
return faceIndex;
}
public static CartesianGridCoordinates CellFaceCoordinates(int cellIndex, int rowCount)
{
var cellCount = rowCount * rowCount;
var faceCellIndex = cellIndex % cellCount;
var y = faceCellIndex / rowCount;
var x = faceCellIndex - (y * rowCount);
return new CartesianGridCoordinates { x = (short)x, y = (short)y };
}
public static int CellIndexFromExitEdge(int edge, int cellIndex, int rowCount)
{
var faceIndex = CellFaceIndex(cellIndex, rowCount);
var facePosition = CellFaceCoordinates(cellIndex, rowCount);
return CellIndexFromExitEdge(edge, faceIndex, facePosition, rowCount);
}
public static int CellIndexFromExitEdge(int edge, int faceIndex, CartesianGridCoordinates facePosition, int rowCount)
{
var cellCount = rowCount * rowCount;
var nextFaceIndex = NextFaceIndex[(edge * 6) + faceIndex];
var cx = (rowCount - 1) * 0.5f;
var px = math.clamp(facePosition.x, 0, rowCount - 1);
var py = math.clamp(facePosition.y, 0, rowCount - 1);
var x0 = px - cx;
var y0 = py - cx;
var ax = m_GridToGrid[(edge * 6 * 2) + (faceIndex * 2) + 0];
var ay = m_GridToGrid[(edge * 6 * 2) + (faceIndex * 2) + 1];
var x1 = (ax.x * x0) + (ax.y * y0);
var y1 = (ay.x * x0) + (ay.y * y0);
var nx = (short)(x1 + cx + 0.5f);
var ny = (short)(y1 + cx + 0.5f);
var nextFaceCellIndex = (ny * rowCount) + nx;
return (nextFaceIndex * cellCount) + nextFaceCellIndex;
}
public static int CellIndex(CartesianGridCoordinates cellPosition, CartesianGridOnCubeFace cubeFace, int rowCount)
{
var rowStride = rowCount;
var faceStride = rowCount * rowStride;
var cellIndex = (cubeFace.Value * faceStride) + (cellPosition.y * rowStride) + cellPosition.x;
return cellIndex;
}
public static int CellIndexNorth(int cellIndex, int rowCount, float4x4* faceLocalToLocal)
{
var faceIndex = CellFaceIndex(cellIndex, rowCount);
var facePosition = CellFaceCoordinates(cellIndex, rowCount);
facePosition.y += 1;
var edge = CartesianGridMovement.CubeExitEdge(facePosition, rowCount);
if (edge == -1)
return cellIndex + rowCount;
return CellIndexFromExitEdge(edge, faceIndex, facePosition, rowCount);
}
public static int CellIndexSouth(int cellIndex, int rowCount, float4x4* faceLocalToLocal)
{
var faceIndex = CellFaceIndex(cellIndex, rowCount);
var facePosition = CellFaceCoordinates(cellIndex, rowCount);
facePosition.y -= 1;
var edge = CartesianGridMovement.CubeExitEdge(facePosition, rowCount);
if (edge == -1)
return cellIndex - rowCount;
return CellIndexFromExitEdge(edge, faceIndex, facePosition, rowCount);
}
public static int CellIndexWest(int cellIndex, int rowCount, float4x4* faceLocalToLocal)
{
var faceIndex = CellFaceIndex(cellIndex, rowCount);
var facePosition = CellFaceCoordinates(cellIndex, rowCount);
facePosition.x -= 1;
var edge = CartesianGridMovement.CubeExitEdge(facePosition, rowCount);
if (edge == -1)
return cellIndex - 1;
return CellIndexFromExitEdge(edge, faceIndex, facePosition, rowCount);
}
public static int CellIndexEast(int cellIndex, int rowCount, float4x4* faceLocalToLocal)
{
var faceIndex = CellFaceIndex(cellIndex, rowCount);
var facePosition = CellFaceCoordinates(cellIndex, rowCount);
facePosition.x += 1;
var edge = CartesianGridMovement.CubeExitEdge(facePosition, rowCount);
if (edge == -1)
return cellIndex + 1;
return CellIndexFromExitEdge(edge, faceIndex, facePosition, rowCount);
}
}