@@ -52,8 +52,7 @@ public class World : IDisposable {
52
52
53
53
public Random random ;
54
54
55
- // max. 5 msec in each frame for chunkload
56
- private const long MAX_CHUNKLOAD_FRAMETIME = 10 ;
55
+ private const long MAX_CHUNKLOAD_FRAMETIME = 20 ;
57
56
private const long MAX_LIGHT_FRAMETIME = 10 ;
58
57
private const int SPAWNCHUNKS_SIZE = 1 ;
59
58
private const int MAX_TICKING_DISTANCE = 128 ;
@@ -125,7 +124,7 @@ public void loadAroundPlayer() {
125
124
// create terrain
126
125
//genTerrainNoise();
127
126
// separate loop so all data is there
128
- player . loadChunksAroundThePlayerLoading ( Settings . instance . renderDistance ) ;
127
+ player . loadChunksAroundThePlayer ( Settings . instance . renderDistance ) ;
129
128
}
130
129
131
130
@@ -331,68 +330,6 @@ public void addToChunkLoadQueue(ChunkCoord chunkCoord, ChunkStatus level) {
331
330
/// TODO unload chunks which are renderDistance + 2 away (this is bigger to prevent chunk flicker)
332
331
/// </summary>
333
332
public void loadChunksAroundChunk ( ChunkCoord chunkCoord , int renderDistance ) {
334
- // load +2 chunks around renderdistance
335
- for ( int x = chunkCoord . x - renderDistance - 2 ; x <= chunkCoord . x + renderDistance + 2 ; x ++ ) {
336
- for ( int z = chunkCoord . z - renderDistance - 2 ; z <= chunkCoord . z + renderDistance + 2 ; z ++ ) {
337
- var coord = new ChunkCoord ( x , z ) ;
338
- if ( coord . distanceSq ( chunkCoord ) <= ( renderDistance + 2 ) * ( renderDistance + 2 ) ) {
339
- addToChunkLoadQueue ( coord , ChunkStatus . GENERATED ) ;
340
- }
341
- }
342
- }
343
- // populate around renderDistance + 1
344
- // light around renderDistance + 1
345
- for ( int x = chunkCoord . x - renderDistance - 1 ; x <= chunkCoord . x + renderDistance + 1 ; x ++ ) {
346
- for ( int z = chunkCoord . z - renderDistance - 1 ; z <= chunkCoord . z + renderDistance + 1 ; z ++ ) {
347
- var coord = new ChunkCoord ( x , z ) ;
348
- if ( coord . distanceSq ( chunkCoord ) <= ( renderDistance + 1 ) * ( renderDistance + 1 ) ) {
349
- addToChunkLoadQueue ( coord , ChunkStatus . POPULATED ) ;
350
- addToChunkLoadQueue ( coord , ChunkStatus . LIGHTED ) ;
351
- }
352
- }
353
- }
354
- // finally, mesh around renderDistance
355
- for ( int x = chunkCoord . x - renderDistance ; x <= chunkCoord . x + renderDistance ; x ++ ) {
356
- for ( int z = chunkCoord . z - renderDistance ; z <= chunkCoord . z + renderDistance ; z ++ ) {
357
- var coord = new ChunkCoord ( x , z ) ;
358
- if ( coord . distanceSq ( chunkCoord ) <= renderDistance * renderDistance ) {
359
- addToChunkLoadQueue ( coord , ChunkStatus . MESHED ) ;
360
- }
361
- }
362
- }
363
-
364
- // unload chunks which are far away
365
- foreach ( var chunk in chunks . Values ) {
366
- var playerChunk = player . getChunk ( ) ;
367
- var coord = chunk . coord ;
368
- // if distance is greater than renderDistance + 3, unload
369
- if ( playerChunk . distanceSq ( coord ) >= ( renderDistance + 3 ) * ( renderDistance + 3 ) ) {
370
- unloadChunk ( coord ) ;
371
- }
372
- }
373
- }
374
-
375
- public void loadChunksAroundChunkLoading ( ChunkCoord chunkCoord , int renderDistance ) {
376
- // load +2 chunks around renderdistance
377
- for ( int x = chunkCoord . x - renderDistance - 2 ; x <= chunkCoord . x + renderDistance + 2 ; x ++ ) {
378
- for ( int z = chunkCoord . z - renderDistance - 2 ; z <= chunkCoord . z + renderDistance + 2 ; z ++ ) {
379
- var coord = new ChunkCoord ( x , z ) ;
380
- if ( coord . distanceSq ( chunkCoord ) <= ( renderDistance + 2 ) * ( renderDistance + 2 ) ) {
381
- addToChunkLoadQueue ( coord , ChunkStatus . GENERATED ) ;
382
- }
383
- }
384
- }
385
- // populate around renderDistance + 1
386
- // light around renderDistance + 1
387
- for ( int x = chunkCoord . x - renderDistance - 1 ; x <= chunkCoord . x + renderDistance + 1 ; x ++ ) {
388
- for ( int z = chunkCoord . z - renderDistance - 1 ; z <= chunkCoord . z + renderDistance + 1 ; z ++ ) {
389
- var coord = new ChunkCoord ( x , z ) ;
390
- if ( coord . distanceSq ( chunkCoord ) <= ( renderDistance + 1 ) * ( renderDistance + 1 ) ) {
391
- addToChunkLoadQueue ( coord , ChunkStatus . POPULATED ) ;
392
- addToChunkLoadQueue ( coord , ChunkStatus . LIGHTED ) ;
393
- }
394
- }
395
- }
396
333
// finally, mesh around renderDistance
397
334
for ( int x = chunkCoord . x - renderDistance ; x <= chunkCoord . x + renderDistance ; x ++ ) {
398
335
for ( int z = chunkCoord . z - renderDistance ; z <= chunkCoord . z + renderDistance ; z ++ ) {
@@ -415,26 +352,6 @@ public void loadChunksAroundChunkLoading(ChunkCoord chunkCoord, int renderDistan
415
352
}
416
353
417
354
public void loadChunksAroundChunkImmediately ( ChunkCoord chunkCoord , int renderDistance ) {
418
- // load +2 chunks around renderdistance
419
- for ( int x = chunkCoord . x - renderDistance - 2 ; x <= chunkCoord . x + renderDistance + 2 ; x ++ ) {
420
- for ( int z = chunkCoord . z - renderDistance - 2 ; z <= chunkCoord . z + renderDistance + 2 ; z ++ ) {
421
- var coord = new ChunkCoord ( x , z ) ;
422
- if ( coord . distanceSq ( chunkCoord ) <= ( renderDistance + 2 ) * ( renderDistance + 2 ) ) {
423
- loadChunk ( coord , ChunkStatus . GENERATED ) ;
424
- }
425
- }
426
- }
427
- // populate around renderDistance + 1
428
- // light around renderDistance + 1
429
- for ( int x = chunkCoord . x - renderDistance - 1 ; x <= chunkCoord . x + renderDistance + 1 ; x ++ ) {
430
- for ( int z = chunkCoord . z - renderDistance - 1 ; z <= chunkCoord . z + renderDistance + 1 ; z ++ ) {
431
- var coord = new ChunkCoord ( x , z ) ;
432
- if ( coord . distanceSq ( chunkCoord ) <= ( renderDistance + 1 ) * ( renderDistance + 1 ) ) {
433
- loadChunk ( coord , ChunkStatus . POPULATED ) ;
434
- loadChunk ( coord , ChunkStatus . LIGHTED ) ;
435
- }
436
- }
437
- }
438
355
// finally, mesh around renderDistance
439
356
for ( int x = chunkCoord . x - renderDistance ; x <= chunkCoord . x + renderDistance ; x ++ ) {
440
357
for ( int z = chunkCoord . z - renderDistance ; z <= chunkCoord . z + renderDistance ; z ++ ) {
@@ -519,13 +436,23 @@ public Chunk loadChunk(ChunkCoord chunkCoord, ChunkStatus status) {
519
436
generator . generate ( chunkCoord ) ;
520
437
}
521
438
if ( status >= ChunkStatus . POPULATED && ( ! hasChunk || ( hasChunk && chunks [ chunkCoord ] . status < ChunkStatus . POPULATED ) ) ) {
522
- //Console.Out.WriteLine($"chunkload {chunk.GetHashCode()}");
439
+ // load adjacent first
440
+ loadChunk ( new ChunkCoord ( chunkCoord . x - 1 , chunkCoord . z ) , ChunkStatus . GENERATED ) ;
441
+ loadChunk ( new ChunkCoord ( chunkCoord . x + 1 , chunkCoord . z ) , ChunkStatus . GENERATED ) ;
442
+ loadChunk ( new ChunkCoord ( chunkCoord . x , chunkCoord . z - 1 ) , ChunkStatus . GENERATED ) ;
443
+ loadChunk ( new ChunkCoord ( chunkCoord . x , chunkCoord . z + 1 ) , ChunkStatus . GENERATED ) ;
444
+
523
445
generator . populate ( chunkCoord ) ;
524
446
}
525
447
if ( status >= ChunkStatus . LIGHTED && ( ! hasChunk || ( hasChunk && chunks [ chunkCoord ] . status < ChunkStatus . LIGHTED ) ) ) {
526
448
chunks [ chunkCoord ] . lightChunk ( ) ;
527
449
}
528
450
if ( status >= ChunkStatus . MESHED && ( ! hasChunk || ( hasChunk && chunks [ chunkCoord ] . status < ChunkStatus . MESHED ) ) ) {
451
+ // load adjacent first
452
+ loadChunk ( new ChunkCoord ( chunkCoord . x - 1 , chunkCoord . z ) , ChunkStatus . LIGHTED ) ;
453
+ loadChunk ( new ChunkCoord ( chunkCoord . x + 1 , chunkCoord . z ) , ChunkStatus . LIGHTED ) ;
454
+ loadChunk ( new ChunkCoord ( chunkCoord . x , chunkCoord . z - 1 ) , ChunkStatus . LIGHTED ) ;
455
+ loadChunk ( new ChunkCoord ( chunkCoord . x , chunkCoord . z + 1 ) , ChunkStatus . LIGHTED ) ;
529
456
chunks [ chunkCoord ] . meshChunk ( ) ;
530
457
}
531
458
return chunks [ chunkCoord ] ;
0 commit comments