-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicBlockSpawner.cs
421 lines (348 loc) · 10 KB
/
DynamicBlockSpawner.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using UnityEngine;
using System.Collections.Generic;
namespace Uzu
{
/// <summary>
/// Config for the creation of a dynamic block spawner.
/// </summary>
public struct DynamicBlockSpawnerConfig
{
/// <summary>
/// The material to be used by the dynamic blocks.
/// </summary>
public Material Material { get; set; }
/// <summary>
/// The maximum number of dynamic blocks that will be managed.
/// </summary>
public int MaxBlockCount { get; set; }
}
/// <summary>
/// Manages 'dynamic' blocks.
/// Blocks are grouped together in a single mesh for performance.
/// </summary>
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class DynamicBlockSpawner : BaseBehaviour
{
/// <summary>
/// Perform initialization.
/// </summary>
public void Initialize (DynamicBlockSpawnerConfig config)
{
// Assign material.
MeshRenderer meshRenderer = GetComponent<MeshRenderer> ();
Destroy (meshRenderer.material);
meshRenderer.material = config.Material;
meshRenderer.castShadows = false;
meshRenderer.receiveShadows = false;
// Mesh buffer creation.
{
ChunkMeshCreationConfig meshConfig = new ChunkMeshCreationConfig ();
meshConfig.MaxVisibileFaceCount = config.MaxBlockCount * FACE_COUNT_PER_BLOCK;
_meshDesc = new ChunkMeshDesc (meshConfig);
_meshIndicesDesc = new ChunkSubMeshDesc (meshConfig);
}
// Block desc allocation.
{
_activeBlockDescs = new FixedList<DynamicBlockDesc> (config.MaxBlockCount);
_availableBlockDescs = new FixedList<DynamicBlockDesc> (config.MaxBlockCount);
for (int i = 0; i < _availableBlockDescs.Capacity; i++) {
_availableBlockDescs.Add (new DynamicBlockDesc ());
}
}
}
/// <summary>
/// Spawns a new dynamic block.
/// </summary>
public void SpawnBlock (DynamicBlockCreationConfig config)
{
if (_availableBlockDescs.Count == 0) {
Debug.LogWarning ("Cannot spawn any more dynamic blocks. Capacity [" + _activeBlockDescs.Capacity + "] reached.");
return;
}
// Get an available block from the pool.
DynamicBlockDesc blockDesc;
{
int lastIndex = _availableBlockDescs.Count - 1;
blockDesc = _availableBlockDescs [lastIndex];
_availableBlockDescs.RemoveAt (lastIndex);
}
Dbg.Assert (blockDesc != null);
// Set up the desc.
{
blockDesc.Config = config;
blockDesc.ElapsedTime = 0.0f;
}
// Activate.
_activeBlockDescs.Add (blockDesc);
}
/// <summary>
/// Clears all dynamic blocks.
/// </summary>
public void ClearBlocks ()
{
_mesh.Clear ();
_meshDesc.Clear ();
_meshIndicesDesc.Clear ();
_meshIndicesDesc.FillToCapacity ();
// Reset block descs - return to available pool.
{
for (int i = 0; i < _activeBlockDescs.Count; i++) {
_availableBlockDescs.Add (_activeBlockDescs [i]);
}
_activeBlockDescs.Clear ();
}
}
#region Implementation.
private const int FACE_COUNT_PER_BLOCK = 6;
private Mesh _mesh;
private ChunkMeshDesc _meshDesc;
private ChunkSubMeshDesc _meshIndicesDesc;
private FixedList<DynamicBlockDesc> _activeBlockDescs;
private FixedList<DynamicBlockDesc> _availableBlockDescs;
private class DynamicBlockDesc
{
public DynamicBlockCreationConfig Config { get; set; }
public float ElapsedTime { get; set; }
}
private void Update ()
{
// Skip all processing if there are no active blocks.
if (_activeBlockDescs.Count == 0) {
return;
}
// Clear the mesh for rebuilding.
{
_mesh.Clear ();
_meshDesc.Clear ();
_meshIndicesDesc.Clear ();
}
// Rebuild the mesh for all the active blocks.
{
for (int i = 0; i < _activeBlockDescs.Count; /*i++*/) {
DynamicBlockDesc blockDesc = _activeBlockDescs [i];
// Expired block?
if (blockDesc.ElapsedTime >= blockDesc.Config.Duration) {
// Deactivate.
{
// Remove from active list - swap with last item.
{
int lastIndex = _activeBlockDescs.Count - 1;
_activeBlockDescs [i] = _activeBlockDescs [lastIndex];
_activeBlockDescs.RemoveAt (lastIndex);
}
// Move back to available pool.
_availableBlockDescs.Add (blockDesc);
}
// Trigger callback.
if (blockDesc.Config.OnDynamicBlockDie != null) {
blockDesc.Config.OnDynamicBlockDie (blockDesc.Config.DynamicBlockDieContext);
}
continue;
}
// Add the block to the mesh.
CreateBlock (_meshDesc, _meshIndicesDesc, blockDesc);
// Progress time.
blockDesc.ElapsedTime += Time.deltaTime;
i++;
}
}
// Mesh creation.
{
// Fill arrays out with default data so that no garbage remains.
{
_meshIndicesDesc.FillToCapacity ();
}
_mesh.vertices = _meshDesc.VertexList.ToArray ();
_mesh.normals = _meshDesc.NormalList.ToArray ();
_mesh.colors32 = _meshDesc.ColorList.ToArray ();
_mesh.uv = _meshDesc.UVList.ToArray ();
_mesh.triangles = _meshIndicesDesc.IndexList.ToArray ();
_mesh.Optimize ();
}
}
private static void CreateBlock (ChunkMeshDesc meshDesc, ChunkSubMeshDesc meshIndicesDesc, DynamicBlockDesc blockDesc)
{
DynamicBlockCreationConfig config = blockDesc.Config;
float t = blockDesc.ElapsedTime / config.Duration;
Vector3 size = Vector3.Lerp (config.StartScale, config.EndScale, t);
Vector3 centerPos = Vector3.Lerp (config.StartPosition, config.EndPosition, t);
Vector3 pos = centerPos - (size * 0.5f);
FixedList<Vector3> vList = meshDesc.VertexList;
FixedList<Vector3> nList = meshDesc.NormalList;
FixedList<Color32> cList = meshDesc.ColorList;
FixedList<Vector2> uvList = meshDesc.UVList;
FixedList<int> iList = meshIndicesDesc.IndexList;
// Vertices.
Vector3 v0 = new Vector3 (pos.x, pos.y, pos.z);
Vector3 v1 = new Vector3 (pos.x + size.x, pos.y, pos.z);
Vector3 v2 = new Vector3 (pos.x + size.x, pos.y + size.y, pos.z);
Vector3 v3 = new Vector3 (pos.x, pos.y + size.y, pos.z);
Vector3 v4 = new Vector3 (pos.x + size.x, pos.y, pos.z + size.z);
Vector3 v5 = new Vector3 (pos.x, pos.y, pos.z + size.z);
Vector3 v6 = new Vector3 (pos.x, pos.y + size.y, pos.z + size.z);
Vector3 v7 = new Vector3 (pos.x + size.x, pos.y + size.y, pos.z + size.z);
// Normals.
Vector3 up = Vector3.up;
Vector3 down = Vector3.down;
Vector3 front = Vector3.back;
Vector3 back = Vector3.forward;
Vector3 left = Vector3.left;
Vector3 right = Vector3.right;
// UVs.
Vector2 uv00 = Vector2.zero;
Vector2 uv10 = new Vector2 (1.0f, 0.0f);
Vector2 uv11 = Vector2.one;
Vector2 uv01 = new Vector2 (0.0f, 1.0f);
// Color.
Color32 blockColor = Color32.Lerp(config.StartColor, config.EndColor, t);
// Starting index offset (offset by # of vertices).
int baseIdx = vList.Count;
#region Polygon construction.
// Front.
{
vList.Add (v0);
vList.Add (v1);
vList.Add (v2);
vList.Add (v3);
nList.Add (front);
nList.Add (front);
nList.Add (front);
nList.Add (front);
iList.Add (baseIdx + 3);
iList.Add (baseIdx + 1);
iList.Add (baseIdx + 0);
iList.Add (baseIdx + 3);
iList.Add (baseIdx + 2);
iList.Add (baseIdx + 1);
uvList.Add (uv00);
uvList.Add (uv10);
uvList.Add (uv11);
uvList.Add (uv01);
}
// Right.
{
vList.Add (v1);
vList.Add (v4);
vList.Add (v7);
vList.Add (v2);
nList.Add (right);
nList.Add (right);
nList.Add (right);
nList.Add (right);
iList.Add (baseIdx + 7);
iList.Add (baseIdx + 5);
iList.Add (baseIdx + 4);
iList.Add (baseIdx + 7);
iList.Add (baseIdx + 6);
iList.Add (baseIdx + 5);
uvList.Add (uv00);
uvList.Add (uv10);
uvList.Add (uv11);
uvList.Add (uv01);
}
// Back.
{
vList.Add (v4);
vList.Add (v5);
vList.Add (v6);
vList.Add (v7);
nList.Add (back);
nList.Add (back);
nList.Add (back);
nList.Add (back);
iList.Add (baseIdx + 11);
iList.Add (baseIdx + 9);
iList.Add (baseIdx + 8);
iList.Add (baseIdx + 11);
iList.Add (baseIdx + 10);
iList.Add (baseIdx + 9);
uvList.Add (uv00);
uvList.Add (uv10);
uvList.Add (uv11);
uvList.Add (uv01);
}
// Left.
{
vList.Add (v5);
vList.Add (v0);
vList.Add (v3);
vList.Add (v6);
nList.Add (left);
nList.Add (left);
nList.Add (left);
nList.Add (left);
iList.Add (baseIdx + 15);
iList.Add (baseIdx + 13);
iList.Add (baseIdx + 12);
iList.Add (baseIdx + 15);
iList.Add (baseIdx + 14);
iList.Add (baseIdx + 13);
uvList.Add (uv00);
uvList.Add (uv10);
uvList.Add (uv11);
uvList.Add (uv01);
}
// Top.
{
vList.Add (v3);
vList.Add (v2);
vList.Add (v7);
vList.Add (v6);
nList.Add (up);
nList.Add (up);
nList.Add (up);
nList.Add (up);
iList.Add (baseIdx + 19);
iList.Add (baseIdx + 17);
iList.Add (baseIdx + 16);
iList.Add (baseIdx + 19);
iList.Add (baseIdx + 18);
iList.Add (baseIdx + 17);
uvList.Add (uv00);
uvList.Add (uv10);
uvList.Add (uv11);
uvList.Add (uv01);
}
// Bottom.
{
vList.Add (v5);
vList.Add (v4);
vList.Add (v1);
vList.Add (v0);
nList.Add (down);
nList.Add (down);
nList.Add (down);
nList.Add (down);
iList.Add (baseIdx + 23);
iList.Add (baseIdx + 21);
iList.Add (baseIdx + 20);
iList.Add (baseIdx + 23);
iList.Add (baseIdx + 22);
iList.Add (baseIdx + 21);
uvList.Add (uv00);
uvList.Add (uv10);
uvList.Add (uv11);
uvList.Add (uv01);
}
// Set colors per vertex.
const int vertCount = 4 * FACE_COUNT_PER_BLOCK;
for (int i = 0; i < vertCount; i++) {
cList.Add (blockColor);
}
#endregion
}
protected override void Awake ()
{
base.Awake ();
// Get the mesh for rendering.
{
_mesh = new Mesh ();
MeshFilter meshFilter = GetComponent<MeshFilter> ();
meshFilter.mesh = _mesh;
_mesh.MarkDynamic ();
}
}
#endregion
}
}