-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockWorld.cs
710 lines (620 loc) · 21.7 KB
/
BlockWorld.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
using UnityEngine;
using System.Collections.Generic;
namespace Uzu
{
/// <summary>
/// Configuration used in creation of a block world.
/// </summary>
public class BlockWorldConfig
{
public Vector3 BlockSize { get; set; }
/// <summary>
/// Number of blocks per chunk.
/// The smaller the chunk size, the better the chunk Rebuild performance.
/// However, too many small chunks will incur update and memory and draw call performance hits.
/// </summary>
public VectorI3 ChunkSizeInBlocks { get; set; }
/// <summary>
/// Maximum number of chunks to rebuild per frame
/// Set to <= 0 to not limit the number.
/// </summary>
public int MaxChunkRebuildCountPerFrame { get; set; }
/// <summary>
/// Maximum number of visible blocks that will exist in a chunk.
/// If this parameter is not set from outside, the default value will be used.
/// This only affects the number of visible block faces - the block states
/// and collision data is not affected.
/// </summary>
public int MaxVisibleBlockFaceCountPerChunk { get; set; }
/// <summary>
/// Maximum # of block types used in the application.
/// </summary>
public int MaxBlockTypeCount { get; set; }
public BlockDesc[] BlockDescs {
get { return _blockDescs; }
set {
#if UNITY_EDITOR
{
if (MaxBlockTypeCount == 0) {
Debug.LogError ("Please initialize MaxBlockTypeCount before setting block descs.");
return;
}
// Verify size is adequate.
if (value.Length != MaxBlockTypeCount) {
Debug.LogError ("Invalid block desc list. Must contain [" + MaxBlockTypeCount + "] entries.");
return;
}
// Verify contents.
for (int i = 0; i < value.Length; i++) {
if (value[i] == null) {
Debug.LogError ("Invalid (null) desc contained in block desc list.");
return;
}
}
}
#endif
_blockDescs = value;
}
}
public delegate void OnChunkLoadDelegate (BlockWorldChunkLoadContext context);
public delegate void OnChunkUnloadDelegate (BlockWorldChunkUnloadContext context);
/// <summary>
/// Called when a chunk is stream in and loaded.
/// </summary>
public OnChunkLoadDelegate OnChunkLoad { get; set; }
/// <summary>
/// Called when a chunk is stream out and unloaded.
/// </summary>
public OnChunkUnloadDelegate OnChunkUnload { get; set; }
private BlockDesc[] _blockDescs;
}
/// <summary>
/// Context passed to callback when chunk is loaded in.
/// </summary>
public struct BlockWorldChunkLoadContext
{
public BlockWorld BlockWorld { get; set; }
public VectorI3 ChunkIndex { get; set; }
public BlockContainer Blocks { get; set; }
}
/// <summary>
/// Context passed to callback when chunk is unloaded.
/// </summary>
public struct BlockWorldChunkUnloadContext
{
public BlockWorld BlockWorld { get; set; }
public VectorI3 ChunkIndex { get; set; }
}
/// <summary>
/// Iterator class for chunks.
/// </summary>
public struct ChunkIterator
{
private Dictionary<VectorI3, Chunk>.Enumerator _it;
public ChunkIterator (Dictionary<VectorI3, Chunk>.Enumerator beginIt)
{
_it = beginIt;
}
/// <summary>
/// Progress to the next value, returning false if finished.
/// </summary>
public bool MoveNext ()
{
return _it.MoveNext ();
}
public Chunk CurrentChunk {
get { return _it.Current.Value; }
}
public VectorI3 CurrentChunkIndex {
get { return _it.Current.Key; }
}
}
/// <summary>
/// Represents an entire block world.
/// Block worlds contain chunks, which in turn contain the actual blocks.
/// All block related functionality (block world setup, collision detection, etc.)
/// are exposed through the block world interface.
/// </summary>
public class BlockWorld : BaseBehaviour
{
private BlockWorldConfig _config;
private List<Chunk> _idleChunks = new List<Chunk> ();
private Dictionary<VectorI3, Chunk> _chunksToLoad = new Dictionary<VectorI3, Chunk> ();
private Dictionary<VectorI3, Chunk> _activeChunks = new Dictionary<VectorI3, Chunk> ();
public BlockWorldConfig Config {
get { return _config; }
}
/// <summary>
/// Returns an iterator to the currently active chunks.
/// </summary>
public ChunkIterator GetActiveChunksIterator ()
{
return new ChunkIterator (_activeChunks.GetEnumerator ());
}
/// <summary>
/// Perform initialization.
/// </summary>
public void Initialize (BlockWorldConfig config)
{
_config = config;
// Calculate max block count per chunk.
{
int maxFaceCount = _config.MaxVisibleBlockFaceCountPerChunk;
if (maxFaceCount == 0) {
// Assume every other block is set, which means that every block face is exposed.
// This should be the worst possible case.
const int FACE_COUNT_PER_BLOCK = 6;
maxFaceCount = (VectorI3.ElementProduct (_config.ChunkSizeInBlocks) * FACE_COUNT_PER_BLOCK) / 2;
}
_config.MaxVisibleBlockFaceCountPerChunk = maxFaceCount;
}
InternalInitialize ();
}
#region Loading.
/// <summary>
/// Loads a chunk at the given index.
/// </summary>
public void LoadChunk (VectorI3 chunkIndex)
{
// Find an available chunk to use, or create a new one if we have to.
Chunk chunk;
{
if (_idleChunks.Count > 0) {
int lastIndex = _idleChunks.Count - 1;
chunk = _idleChunks [lastIndex];
_idleChunks.RemoveAt (lastIndex);
} else {
GameObject chunkGO = new GameObject (string.Empty, typeof(Chunk));
chunk = chunkGO.GetComponent<Chunk> ();
}
PrepareChunk (chunk, chunkIndex);
}
#if UNITY_EDITOR
// Duplicate load.
{
if (_chunksToLoad.ContainsKey (chunkIndex)) {
Debug.LogError ("Chunk [" + chunkIndex + "] is already requesting load.");
return;
}
if (_activeChunks.ContainsKey (chunkIndex)) {
Debug.LogError ("Chunk [" + chunkIndex + "] is already active.");
return;
}
}
#endif // UNITY_EDITOR
// Queue up to load.
_chunksToLoad.Add (chunkIndex, chunk);
}
/// <summary>
/// Unloads a chunk at the given index.
/// </summary>
public void UnloadChunk (VectorI3 chunkIndex)
{
Chunk chunk;
// Currently in load queue, so just remove it.
if (_chunksToLoad.TryGetValue (chunkIndex, out chunk)) {
_chunksToLoad.Remove (chunkIndex);
_idleChunks.Add (chunk);
return;
}
if (!_activeChunks.TryGetValue (chunkIndex, out chunk)) {
Debug.LogError ("Chunk at index [" + chunkIndex + "] is not loaded, so unable to unload.");
return;
}
if (_idleChunks.Contains (chunk)) {
Debug.LogError ("Attempting to return chunk [" + chunkIndex + "] to available pool, but it already exists.");
return;
}
// Trigger callback.
{
BlockWorldChunkUnloadContext unloadContext = new BlockWorldChunkUnloadContext ();
unloadContext.BlockWorld = this;
unloadContext.ChunkIndex = chunkIndex;
if (_config.OnChunkUnload != null) {
_config.OnChunkUnload (unloadContext);
}
}
// Cleanup.
chunk.TearDown ();
_activeChunks.Remove (chunkIndex);
// Return to pool for re-use.
_idleChunks.Add (chunk);
#if UNITY_EDITOR
// Set name to make it easy to read.
chunk.name = "Chunk - Inactive";
#endif // UNITY_EDITOR
}
#region Implementation.
private void PrepareChunk (Chunk chunk, VectorI3 chunkIndex)
{
#if UNITY_EDITOR
// Set name to make it easy to read.
chunk.name = "Chunk_" + chunkIndex.x + "_" + chunkIndex.y + "_" + chunkIndex.z;
#endif // UNITY_EDITOR
Vector3 chunkOffsetPos = chunkIndex * _config.ChunkSizeInBlocks * _config.BlockSize;
// Make chunk a child of world.
GameObject chunkGO = chunk.gameObject;
Transform chunkXform = chunkGO.transform;
chunkXform.parent = CachedXform;
chunkXform.localPosition = chunkOffsetPos;
chunkXform.localScale = Vector3.one;
// Perform initialization.
chunk.Initialize (_config);
}
#endregion
#endregion
#region Index and position utilities.
private bool GetChunk (VectorI3 blockWorldIndex, out Chunk chunk, out VectorI3 blockChunkIndex)
{
if (IsValidWorldBlockIndex (blockWorldIndex)) {
VectorI3 chunkIndex = blockWorldIndex / _config.ChunkSizeInBlocks;
if (_activeChunks.TryGetValue (chunkIndex, out chunk) ||
_chunksToLoad.TryGetValue (chunkIndex, out chunk)) {
blockChunkIndex = blockWorldIndex - (chunkIndex * _config.ChunkSizeInBlocks);
return true;
}
}
chunk = null;
blockChunkIndex = VectorI3.zero;
return false;
}
public VectorI3 GetBlockWorldIndex (Vector3 worldPos)
{
Vector3 blockSize = _config.BlockSize;
Vector3 relPos = worldPos - CachedXform.position;
return new VectorI3 (relPos.x / blockSize.x, relPos.y / blockSize.y, relPos.z / blockSize.z);
}
/// <summary>
/// Gets the base position of the specified blockWorldIndex.
/// </summary>
public Vector3 GetBlockBasePosition (VectorI3 blockWorldIndex)
{
Vector3 blockSize = _config.BlockSize;
Vector3 relPos = blockWorldIndex * blockSize;
return relPos + CachedXform.position;
}
/// <summary>
/// Gets the center position of the specified blockWorldIndex.
/// </summary>
public Vector3 GetBlockCenterPosition (VectorI3 blockWorldIndex)
{
Vector3 blockCenterOffset = _config.BlockSize / 2.0f;
return GetBlockBasePosition (blockWorldIndex) + blockCenterOffset;
}
/// <summary>
/// Gets the bounding volume of the specified blockWorldIndex.
/// </summary>
public void GetBlockBounds (VectorI3 blockWorldIndex, out Bounds bounds)
{
bounds = new Bounds (GetBlockCenterPosition (blockWorldIndex), _config.BlockSize);
}
/// <summary>
/// Gets the type of block at specified blockWorldIndex.
/// </summary>
public BlockType GetBlockType (VectorI3 blockWorldIndex)
{
Chunk chunk;
VectorI3 blockChunkIndex;
if (GetChunk (blockWorldIndex, out chunk, out blockChunkIndex)) {
return chunk.GetBlockType (blockChunkIndex);
}
return BlockType.EMPTY;
}
/// <summary>
/// Gets the color of block at specified blockWorldIndex.
/// </summary>
public Color32 GetBlockColor (VectorI3 blockWorldIndex)
{
Chunk chunk;
VectorI3 blockChunkIndex;
if (GetChunk (blockWorldIndex, out chunk, out blockChunkIndex)) {
return chunk.GetBlockColor (blockChunkIndex);
}
return new Color32 ();
}
public void SetBlockType (VectorI3 blockWorldIndex, BlockType blockType)
{
Chunk chunk;
VectorI3 blockChunkIndex;
if (GetChunk (blockWorldIndex, out chunk, out blockChunkIndex)) {
chunk.SetBlockType (blockChunkIndex, blockType);
}
}
public void SetBlockColor (VectorI3 blockWorldIndex, Color32 blockColor)
{
Chunk chunk;
VectorI3 blockChunkIndex;
if (GetChunk (blockWorldIndex, out chunk, out blockChunkIndex)) {
chunk.SetBlockColor (blockChunkIndex, blockColor);
}
}
#endregion
#region Collision detection.
/// <summary>
/// The results of a detected collision.
/// </summary>
public struct CollisionResult
{
public Vector3 Position;
public Vector3 Normal;
public VectorI3 BlockWorldIndex;
}
/// <summary>
/// Performs line collision check on block world.
/// </summary>
public bool CheckCollision (Vector3 fromPos, Vector3 toPos)
{
CollisionResult result;
return CheckCollision (fromPos, toPos, out result);
}
/// <summary>
/// Performs line collision check on block world.
/// </summary>
public bool CheckCollision (Vector3 fromPos, Vector3 toPos, out CollisionResult result)
{
result = new CollisionResult ();
Vector3 dir = Vector3.Normalize (toPos - fromPos);
// Get start index and verify within world bounds.
VectorI3 fromBlockWorldIdx = GetBlockWorldIndex (fromPos);
if (!IsValidWorldBlockIndex (fromBlockWorldIdx)) {
return false;
}
// If start index originates in solid box, treat as collision.
if (GetBlockType (fromBlockWorldIdx) != BlockType.EMPTY) {
result.BlockWorldIndex = fromBlockWorldIdx;
result.Position = fromPos;
result.Normal = Vector3.forward; // When originating in a solid box, hitNormal has no meaning.
return true;
}
// Get end index and clamp to world bounds.
VectorI3 toBlockWorldIdx = GetBlockWorldIndex (toPos);
toBlockWorldIdx = VectorI3.MaxPerElement (toBlockWorldIdx, VectorI3.zero);
VectorI3 currentBlockWorldIdx = fromBlockWorldIdx;
Vector3 currentPos = fromPos;
while (true) {
// We made it to the destination without any collisions.
if (currentBlockWorldIdx == toBlockWorldIdx) {
break;
}
// Get the next block index along direction vector.
CollisionResult nextResult;
if (!GetNeighborBlockWorldIndex (currentBlockWorldIdx, currentPos, dir, out nextResult)) {
#if true
Debug.Log (fromBlockWorldIdx.ToString ());
Debug.Log (toBlockWorldIdx.ToString ());
Debug.LogError ("Infinite loop detected.");
#endif
break;
}
// We've reached the edge of the world and haven't found a collision.
if (!IsValidWorldBlockIndex (nextResult.BlockWorldIndex)) {
break;
}
// Is there a collision with the next block?
if (GetBlockType (nextResult.BlockWorldIndex) != BlockType.EMPTY) {
result = nextResult;
return true;
}
// Progress to next index.
currentBlockWorldIdx = nextResult.BlockWorldIndex;
currentPos = nextResult.Position;
}
// No collision.
return false;
}
#endregion
/// <summary>
/// Is the given blockWorldIndex within the world bounds?
/// </summary>
public bool IsValidWorldBlockIndex (VectorI3 blockWorldIndex)
{
return !(VectorI3.AnyLower (blockWorldIndex, VectorI3.zero));
}
#region Implementation.
#region Normalized block faces. Used for collision detection.
private Vector3[] _xNegFaceVertices;
private Vector3[] _xPosFaceVertices;
private Vector3[] _zNegFaceVertices;
private Vector3[] _zPosFaceVertices;
private Vector3[] _yNegFaceVertices;
private Vector3[] _yPosFaceVertices;
#endregion
/// <summary>
/// Perform any setup work.
/// </summary>
private void InternalInitialize ()
{
#region Normalized block faces.
// Used for collision detection.
// Gives the normalized coordinates of a block looking from the center point of the block.
// These coordinates are then offset by a block base position in order to give
// the actual face coordinates of any block in the world.
{
Vector3 blockSize = _config.BlockSize;
// X
{
_xNegFaceVertices = new Vector3[] {
Vector3.Scale(new Vector3 (0.0f, 0.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 1.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 1.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 0.0f, 1.0f), blockSize),
};
_xPosFaceVertices = new Vector3[] {
Vector3.Scale(new Vector3 (1.0f, 0.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 0.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 1.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 1.0f, 0.0f), blockSize),
};
}
// Y
{
_yNegFaceVertices = new Vector3[] {
Vector3.Scale(new Vector3 (1.0f, 0.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 0.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 0.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 0.0f, 1.0f), blockSize),
};
_yPosFaceVertices = new Vector3[] {
Vector3.Scale(new Vector3 (1.0f, 1.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 1.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 1.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 1.0f, 0.0f), blockSize),
};
}
// Z
{
_zNegFaceVertices = new Vector3[] {
Vector3.Scale(new Vector3 (1.0f, 0.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 1.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 1.0f, 0.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 0.0f, 0.0f), blockSize),
};
_zPosFaceVertices = new Vector3[] {
Vector3.Scale(new Vector3 (0.0f, 0.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (0.0f, 1.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 1.0f, 1.0f), blockSize),
Vector3.Scale(new Vector3 (1.0f, 0.0f, 1.0f), blockSize),
};
}
}
#endregion
}
private void Update ()
{
// We relegate the updating of chunks to the world (as opposed to each chunk),
// to allow the world to have control of chunk execution.
// For example, the world could easily cap the number of chunks
// that get rebuilt in a single frame, which allows finer control
// of performance.
// Load chunks.
{
ChunkIterator it = new ChunkIterator (_chunksToLoad.GetEnumerator ());
while (it.MoveNext()) {
VectorI3 chunkIndex = it.CurrentChunkIndex;
Chunk chunk = it.CurrentChunk;
// Trigger callback.
BlockWorldChunkLoadContext loadContext = new BlockWorldChunkLoadContext ();
loadContext.BlockWorld = this;
loadContext.ChunkIndex = chunkIndex;
loadContext.Blocks = chunk.Blocks;
if (_config.OnChunkLoad != null) {
_config.OnChunkLoad (loadContext);
chunk.RequestRebuild ();
}
// Add to active chunk list.
_activeChunks.Add (chunkIndex, chunk);
}
_chunksToLoad.Clear ();
}
// Rebuild dirty chunks.
{
int chunkRebuildCount = 0;
ChunkIterator it = new ChunkIterator (_activeChunks.GetEnumerator ());
while (it.MoveNext()) {
Chunk chunk = it.CurrentChunk;
if (chunk.DoesNeedRebuild) {
chunk.Rebuild ();
// Cap the number of chunks we rebuild this frame.
chunkRebuildCount++;
if (chunkRebuildCount == _config.MaxChunkRebuildCountPerFrame) {
break;
}
}
}
}
}
private bool GetNeighborBlockWorldIndex (VectorI3 blockWorldIndex, Vector3 startPos, Vector3 dir, out CollisionResult result)
{
// If we use "hitPos" as-is, collision checks start to go weird.
// Could it be cascading an out parameter in C# is causing problems???
// I have no idea, so I'm changing this variable name and just assigning the result at the end.
Vector3 hitPos2;
result = new CollisionResult ();
// Clamp startPos to be within the current block index.
Bounds blockBounds;
GetBlockBounds (blockWorldIndex, out blockBounds);
startPos = Math.Clamp (startPos, blockBounds.min, blockBounds.max);
// For the end position, we want a point that is definitely outside of the current block.
Vector3 endPos = startPos + dir * Math.VectorMaxElement (_config.BlockSize) * 2.0f;
Vector3 blockBasePos = GetBlockBasePosition (blockWorldIndex);
while (true) {
if (dir.x > 0.0f) {
Vector3 a = blockBasePos + _xPosFaceVertices [0];
Vector3 b = blockBasePos + _xPosFaceVertices [1];
Vector3 c = blockBasePos + _xPosFaceVertices [2];
Vector3 d = blockBasePos + _xPosFaceVertices [3];
if (Math.IntersectLineQuad (startPos, endPos, a, b, c, d, out hitPos2)) {
result.BlockWorldIndex = blockWorldIndex + VectorI3.right;
result.Normal = Vector3.left;
break;
}
} else if (dir.x < 0.0f) {
Vector3 a = blockBasePos + _xNegFaceVertices [0];
Vector3 b = blockBasePos + _xNegFaceVertices [1];
Vector3 c = blockBasePos + _xNegFaceVertices [2];
Vector3 d = blockBasePos + _xNegFaceVertices [3];
if (Math.IntersectLineQuad (startPos, endPos, a, b, c, d, out hitPos2)) {
result.BlockWorldIndex = blockWorldIndex + VectorI3.left;
result.Normal = Vector3.right;
break;
}
}
if (dir.z > 0.0f) {
Vector3 a = blockBasePos + _zPosFaceVertices [0];
Vector3 b = blockBasePos + _zPosFaceVertices [1];
Vector3 c = blockBasePos + _zPosFaceVertices [2];
Vector3 d = blockBasePos + _zPosFaceVertices [3];
if (Math.IntersectLineQuad (startPos, endPos, a, b, c, d, out hitPos2)) {
result.BlockWorldIndex = blockWorldIndex + VectorI3.forward;
result.Normal = Vector3.back;
break;
}
} else if (dir.z < 0.0f) {
Vector3 a = blockBasePos + _zNegFaceVertices [0];
Vector3 b = blockBasePos + _zNegFaceVertices [1];
Vector3 c = blockBasePos + _zNegFaceVertices [2];
Vector3 d = blockBasePos + _zNegFaceVertices [3];
if (Math.IntersectLineQuad (startPos, endPos, a, b, c, d, out hitPos2)) {
result.BlockWorldIndex = blockWorldIndex + VectorI3.back;
result.Normal = Vector3.forward;
break;
}
}
if (dir.y > 0.0f) {
Vector3 a = blockBasePos + _yPosFaceVertices [0];
Vector3 b = blockBasePos + _yPosFaceVertices [1];
Vector3 c = blockBasePos + _yPosFaceVertices [2];
Vector3 d = blockBasePos + _yPosFaceVertices [3];
if (Math.IntersectLineQuad (startPos, endPos, a, b, c, d, out hitPos2)) {
result.BlockWorldIndex = blockWorldIndex + VectorI3.up;
result.Normal = Vector3.down;
break;
}
} else if (dir.y < 0.0f) {
Vector3 a = blockBasePos + _yNegFaceVertices [0];
Vector3 b = blockBasePos + _yNegFaceVertices [1];
Vector3 c = blockBasePos + _yNegFaceVertices [2];
Vector3 d = blockBasePos + _yNegFaceVertices [3];
if (Math.IntersectLineQuad (startPos, endPos, a, b, c, d, out hitPos2)) {
result.BlockWorldIndex = blockWorldIndex + VectorI3.down;
result.Normal = Vector3.up;
break;
}
}
// TODO: this case is sometimes happening at edge of world. Need to fix.
// If we get here, none of the intersection checks passed.
#if false
Debug.LogError ("No intersection detected.");
Debug.Log ("startPos: " + startPos.ToString ());
Debug.Log ("endPos: " + endPos.ToString ());
Debug.Log ("blockIdx: " + blockWorldIndex.ToString ());
Debug.Log ("dir: " + dir.ToString ());
#endif
return false;
}
result.Position = hitPos2;
return true;
}
#endregion
}
}