forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartesianGridGeneratorUtility.cs
More file actions
230 lines (203 loc) · 9.86 KB
/
Copy pathCartesianGridGeneratorUtility.cs
File metadata and controls
230 lines (203 loc) · 9.86 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
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
public static unsafe class CartesianGridGeneratorUtility
{
public enum WallFlags : byte
{
SouthWall = 1,
WestWall = 2,
}
static readonly float4x4[] m_FaceLocalToWorldRotation =
{
new float4x4(
new float4(0.00f, -1.00f, 0.00f, 0.00f),
new float4(1.00f, 0.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, 1.00f, 0.00f),
new float4(0.00f, 0.00f, 0.00f, 1.00f)),
new float4x4(
new float4(0.00f, 1.00f, 0.00f, 0.00f),
new float4(-1.00f, 0.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, 1.00f, 0.00f),
new float4(0.00f, 0.00f, 0.00f, 1.00f)),
new float4x4(
new float4(1.00f, 0.00f, 0.00f, 0.00f),
new float4(0.00f, 1.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, 1.00f, 0.00f),
new float4(0.00f, 0.00f, 0.00f, 1.00f)),
new float4x4(
new float4(-1.00f, 0.00f, 0.00f, 0.00f),
new float4(0.00f, -1.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, 1.00f, 0.00f),
new float4(0.00f, 0.00f, 0.00f, 1.00f)),
new float4x4(
new float4(-1.00f, 0.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, 1.00f, 0.00f),
new float4(0.00f, 1.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, 0.00f, 1.00f)),
new float4x4(
new float4(1.00f, 0.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, -1.00f, 0.00f),
new float4(0.00f, 1.00f, 0.00f, 0.00f),
new float4(0.00f, 0.00f, 0.00f, 1.00f)),
};
public static unsafe void CreateGridPath(int rowCount, int columnCount, byte* gridWalls, float wallSProbability, float wallWProbability, bool outerWalls)
{
var buildGridPathJob = new BuildGridPath
{
GridWalls = gridWalls,
RowCount = rowCount,
ColumnCount = columnCount,
WallSProbability = wallSProbability,
WallWProbability = wallWProbability,
OuterWalls = outerWalls
};
buildGridPathJob.Run();
}
[BurstCompile]
unsafe struct BuildGridPath : IJob
{
[NativeDisableUnsafePtrRestriction]
public byte* GridWalls;
public int RowCount;
public int ColumnCount;
public float WallSProbability;
public float WallWProbability;
public bool OuterWalls;
public void Execute()
{
var cx = (ColumnCount * 0.5f);
var cz = (RowCount * 0.5f);
// Add additional row/col to create SE walls along outer edge
var GridWallsRowCount = RowCount + 1;
var GridWallsColumnCount = ColumnCount + 1;
// 2 bit (0=None, 1=South Wall, 2=West Wall)
// Temp allocations in jobs auto-disposed
var GridWallsSW = new NativeArray<byte>(GridWallsRowCount * GridWallsColumnCount, Allocator.Temp);
// Populate the grid
for (int y = 0; y < GridWallsRowCount; y++)
for (int x = 0; x < GridWallsColumnCount; x++)
{
// By default place outer walls along the edge of the grid.
if (((y & 1) == 1) && (x & 1) == 1)
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.SouthWall;
if ((y == 0) && (x < (GridWallsColumnCount - 1)))
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.SouthWall;
if ((x == 0) && (y < (GridWallsRowCount - 1)))
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.WestWall;
if ((y == (GridWallsRowCount - 1)) && (x < (GridWallsColumnCount - 1)))
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.SouthWall;
if ((x == (GridWallsColumnCount - 1)) && (y < (GridWallsRowCount - 1)))
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.WestWall;
if ((x < (GridWallsColumnCount - 1)) && (y < (GridWallsRowCount - 1)))
{
var tx = ((float)x) - cx;
var tz = ((float)y) - cz;
var noiseShift = (float)((((long)GridWalls) >> 8) & 0xfff);
var n0 = noise.snoise(new float2(tx * 1.2345f + noiseShift, tz * 1.2345f + noiseShift));
var n1 = noise.snoise(new float2(tz * 1.6789f + noiseShift, tx * 1.6789f + noiseShift));
if (((n0 * 0.5f) + 0.5f) < WallSProbability)
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.SouthWall;
if (((n1 * 0.5f) + 0.5f) < WallWProbability)
GridWallsSW[(y * GridWallsColumnCount) + x] |= (byte)WallFlags.WestWall;
}
// Make sure there are no outer walls along the edge of the grid
if (!OuterWalls)
{
if (x == 0)
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.WestWall;
if (x == (GridWallsColumnCount - 1))
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.WestWall;
if (y == (GridWallsRowCount - 1))
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.SouthWall;
if (y == 0)
GridWallsSW[(y * GridWallsColumnCount) + x] &= (byte)~WallFlags.SouthWall;
}
}
int gridWallSize = RowCount * ((ColumnCount + 1) / 2);
UnsafeUtility.MemClear(GridWalls, gridWallSize);
for (int y = 0; y < RowCount; y++)
for (int x = 0; x < ColumnCount; x++)
{
var wallN = ((GridWallsSW[((y + 1) * GridWallsColumnCount) + x] & (byte)WallFlags.SouthWall) != 0);
var wallS = ((GridWallsSW[(y * GridWallsColumnCount) + x] & (byte)WallFlags.SouthWall) != 0);
var wallW = ((GridWallsSW[(y * GridWallsColumnCount) + x] & (byte)WallFlags.WestWall) != 0);
var wallE = ((GridWallsSW[(y * GridWallsColumnCount) + (x + 1)] & (byte)WallFlags.WestWall) != 0);
var walls = ((wallN) ? 0x01 : 0x00)
| ((wallS) ? 0x02 : 0x00)
| ((wallW) ? 0x04 : 0x00)
| ((wallE) ? 0x08 : 0x00);
var gridWallIndex = (y * ((ColumnCount + 1) / 2)) + (x / 2);
walls <<= (x & 1) * 4; // odd columns packed into upper 4 bits
walls |= GridWalls[gridWallIndex];
GridWalls[gridWallIndex] = (byte)walls;
}
}
}
public static void CreateFloorPanel(EntityManager dstManager, Entity prefab, float4x4 parentLocalToWorld, float tx, float tz)
{
var pos = new float3(tx + 0.5f, 0.0f, tz + 0.5f);
var childLocalToParent = math.mul(float4x4.Translate(pos), float4x4.Scale(0.98f));
var localToWorld = new LocalToWorld
{
Value = math.mul(parentLocalToWorld, childLocalToParent)
};
CreatePanel(dstManager, prefab, localToWorld);
}
public static void CreateWallS(EntityManager dstManager, Entity prefab, float4x4 parentLocalToWorld, float tx, float tz)
{
var pos = new float3(tx + 0.5f, 1.0f, tz);
var childLocalToParent = math.mul(float4x4.Translate(pos), float4x4.Scale(1.1f, 1.1f, 0.1f));
var localToWorld = new LocalToWorld
{
Value = math.mul(parentLocalToWorld, childLocalToParent)
};
CreatePanel(dstManager, prefab, localToWorld);
}
public static void CreateWallW(EntityManager dstManager, Entity prefab, float4x4 parentLocalToWorld, float tx, float tz)
{
var pos = new float3(tx, 1.0f, tz + 0.5f);
var childLocalToParent = math.mul(float4x4.Translate(pos), float4x4.Scale(0.1f, 1.1f, 1.1f));
var localToWorld = new LocalToWorld
{
Value = math.mul(parentLocalToWorld, childLocalToParent)
};
CreatePanel(dstManager, prefab, localToWorld);
}
public static void CreatePanel(EntityManager dstManager, Entity prefab, LocalToWorld localToWorld)
{
var panelEntity = dstManager.Instantiate(prefab);
#if !ENABLE_TRANSFORM_V1
dstManager.RemoveComponent<LocalToWorldTransform>(panelEntity);
#else
dstManager.RemoveComponent<Translation>(panelEntity);
dstManager.RemoveComponent<Rotation>(panelEntity);
#endif
dstManager.SetComponentData(panelEntity, localToWorld);
}
public static void FillCubeFaceTransforms(int rowCount, float4x4* faceLocalToWorld, float4x4* faceWorldToLocal, float4x4* faceLocalToLocal)
{
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
ref var localToWorld = ref m_FaceLocalToWorldRotation[faceIndex];
// Translate along normal of face by width
localToWorld.c3.xyz = localToWorld.c1.xyz * rowCount * 0.5f;
faceLocalToWorld[faceIndex] = localToWorld;
faceWorldToLocal[faceIndex] = math.fastinverse(faceLocalToWorld[faceIndex]);
}
// Face x Face matrix for access simplicity, but:
// - Diagonal is identity and unused
// - Only the edge transitions are actually used (4 edges for each face)
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
faceLocalToLocal[(i * 6) + j] = math.mul(faceWorldToLocal[j], faceLocalToWorld[i]);
}
}
}
}