Merge branch 'perf/y-sort-event-driven'
@@ -477,8 +477,8 @@ export class WorldView
477
477
this.catalog = catalog;
478
478
this.preview = null;
479
479
this.zoom = 2;
480
/** World-Y origin for viewport-relative depth sort (see {@link #depthLayer}). */
481
this.ySortOrigin = 0;
480
/** Chunk-quantised world-Y origin for the depth sort (see {@link #depthLayer}). */
481
this.ySortOrigin = null;
482
482
this.chunks = new Map();
483
483
this.bakedChunks = new Map();
484
484
this.tiles = new Map();
@@ -1655,8 +1655,11 @@ export class WorldView
1655
1655
1656
1656
// The y-sort origin must settle before any layer is computed this frame,
1657
1657
// otherwise entities and blocks end up sorted against different origins.
1658
// It only moves when the view crosses a chunk row; the static world then
1659
// needs one re-layer pass, and nothing at all on every other frame.
1658
1660
this.#centerCamera();
1659
this.#syncYSortOrigin();
1661
if (this.#syncYSortOrigin())
1662
{ this.#refreshDepthLayers(); }
1660
1663
1661
1664
for (const entity of this.entities.values())
1662
1665
{ this.#placeEntity(entity, now); }
@@ -1665,7 +1668,6 @@ export class WorldView
1665
1668
this.#updateDamageNumbers(now);
1666
1669
this.#updateSkillLevelFloats(now);
1667
1670
this.#relayoutSpellTargetHighlight();
1668
this.#refreshDepthLayers();
1669
1671
this.#cullOffscreenDrawables();
1670
1672
this.#updateShade(deltaTime);
1671
1673
}
@@ -2345,10 +2347,9 @@ export class WorldView
2345
2347
highlight.object.transform.position.x = src.position.x;
2346
2348
highlight.object.transform.position.y = src.position.y;
2347
2349
highlight.object.transform.size = new vector(src.size.x + pad, src.size.y + pad);
2348
highlight.object.layer = LAYER_EDGE - 1;
2350
this.#setLayer(highlight.object, LAYER_EDGE - 1);
2349
2351
highlight.tileX = entity.tileX;
2350
2352
highlight.tileY = entity.tileY;
2351
this.#markSortDirty();
2352
2353
return;
2353
2354
}
2354
2355
const size = this.tileSize();
@@ -2357,8 +2358,7 @@ export class WorldView
2357
2358
highlight.object.transform.position.x = left + size / 2;
2358
2359
highlight.object.transform.position.y = top + size / 2;
2359
2360
highlight.object.transform.size = new vector(size, size);
2360
highlight.object.layer = LAYER_EDGE - 1;
2361
this.#markSortDirty();
2361
this.#setLayer(highlight.object, LAYER_EDGE - 1);
2362
2362
}
2363
2363
2364
2364
#relayoutSpawnRangeHighlight ()
@@ -2378,8 +2378,7 @@ export class WorldView
2378
2378
highlight.object.transform.position.x = left + side / 2;
2379
2379
highlight.object.transform.position.y = top + side / 2;
2380
2380
highlight.object.transform.size = new vector(side, side);
2381
highlight.object.layer = layer;
2382
this.#markSortDirty();
2381
this.#setLayer(highlight.object, layer);
2383
2382
}
2384
2383
2385
2384
setTilePreview (tileX, tileY, override)
@@ -2435,14 +2434,26 @@ export class WorldView
2435
2434
}
2436
2435
2437
2436
/**
2438
* World Y at the viewport centre. Depth sort uses worldY − this so layers stay
2439
* bounded (absolute tileY×zoom otherwise overflows past LAYER_EDGE).
2437
* World Y the depth sort is measured against, so layers stay bounded
2438
* (absolute tileY×zoom otherwise overflows past LAYER_EDGE).
2439
*
2440
* Quantised to the chunk row under the viewport centre: every sprite's layer
2441
* is relative to this, so an origin that moved with the camera would mean
2442
* re-layering the whole world every frame. Chunks only span the static 5×5
2443
* ring, so relative sortY stays far inside LAYER_EDGE.
2444
*
2445
* @returns {boolean} whether the origin moved, i.e. callers must re-layer.
2440
2446
*/
2441
2447
#syncYSortOrigin ()
2442
2448
{
2443
2449
const camY = this.scene.cameraPosition.y;
2444
2450
const screenH = this.scene.real_size.y;
2445
this.ySortOrigin = -camY + screenH / 2;
2451
const chunkPx = CHUNK_SIZE * this.tileSize();
2452
const origin = Math.floor((-camY + screenH / 2) / chunkPx) * chunkPx;
2453
if (origin === this.ySortOrigin)
2454
{ return false; }
2455
this.ySortOrigin = origin;
2456
return true;
2446
2457
}
2447
2458
2448
2459
/** Viewport-relative y-sort layer for world sprites. */
@@ -2454,8 +2465,11 @@ export class WorldView
2454
2465
2455
2466
/**
2456
2467
* Re-apply depth for every world sprite that is not repositioned each frame.
2457
* Their layer is relative to the y-sort origin, which moves with the camera,
2458
* so a layer assigned once at draw time drifts as the player walks.
2468
* Their layer is relative to the y-sort origin, so a layer assigned once at
2469
* draw time goes stale as soon as that origin moves.
2470
*
2471
* Only run when {@link #syncYSortOrigin} reports a move (or after a zoom /
2472
* layout change) — entities carry their own per-frame layer via #placeEntity.
2459
2473
*/
2460
2474
#refreshDepthLayers ()
2461
2475
{
@@ -2466,14 +2480,10 @@ export class WorldView
2466
2480
const chunkPx = CHUNK_SIZE * size;
2467
2481
for (let row = 0; row < CHUNK_SIZE; row++)
2468
2482
{
2469
const object = baked.blockObjects?.[row];
2470
if (!object)
2471
{ continue; }
2472
2483
const rowY = top + row * size + size / 2;
2473
object.layer = this.#depthLayer(rowY, SORT_BLOCK);
2484
this.#setLayer(baked.blockObjects?.[row], this.#depthLayer(rowY, SORT_BLOCK));
2474
2485
}
2475
if (baked.bloomObject)
2476
{ baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK); }
2486
this.#setLayer(baked.bloomObject, this.#depthLayer(top + chunkPx, SORT_BLOCK));
2477
2487
}
2478
2488
// Unbaked tile sprites: tall blocks, doors and animated grounds.
2479
2489
for (const objects of this.tiles.values())
@@ -2481,39 +2491,37 @@ export class WorldView
2481
2491
if (!objects.block && !objects.blockBloom && !objects.groundBloom)
2482
2492
{ continue; }
2483
2493
const pivotY = objects.tileY * size + size / 2;
2484
if (objects.block)
2485
{ objects.block.layer = this.#depthLayer(pivotY, objects.blockSort ?? SORT_ENTITY); }
2486
if (objects.blockBloom)
2487
{ objects.blockBloom.layer = this.#depthLayer(pivotY, SORT_ENTITY); }
2488
if (objects.groundBloom)
2489
{ objects.groundBloom.layer = this.#depthLayer(pivotY, LAYER_GROUND); }
2494
this.#setLayer(objects.block, this.#depthLayer(pivotY, objects.blockSort ?? SORT_ENTITY));
2495
this.#setLayer(objects.blockBloom, this.#depthLayer(pivotY, SORT_ENTITY));
2496
this.#setLayer(objects.groundBloom, this.#depthLayer(pivotY, LAYER_GROUND));
2490
2497
}
2491
2498
for (const item of this.groundItems.values())
2492
2499
{
2493
if (!item.object)
2494
{ continue; }
2495
item.object.layer = this.#depthLayer(item.tileY * size + size / 2 - 0.01, SORT_BLOCK);
2500
this.#setLayer(
2501
item.object,
2502
this.#depthLayer(item.tileY * size + size / 2 - 0.01, SORT_BLOCK)
2503
);
2496
2504
}
2497
2505
// Lerping FX are re-placed in #updateSwipes; static ones need the refresh.
2498
2506
for (const swipe of this.swipes)
2499
2507
{
2500
if (swipe.lerp || swipe.sortY == null || !swipe.object)
2508
if (swipe.lerp || swipe.sortY == null)
2501
2509
{ continue; }
2502
swipe.object.layer = this.#depthLayer(swipe.sortY, SORT_FX);
2510
this.#setLayer(swipe.object, this.#depthLayer(swipe.sortY, SORT_FX));
2503
2511
}
2504
2512
for (const entry of this.editorSpawners.values())
2505
2513
{
2506
2514
const pivotY = entry.tileY * size + size / 2;
2507
if (entry.object)
2508
{ entry.object.layer = this.#depthLayer(pivotY + 0.05, SORT_ENTITY); }
2515
this.#setLayer(entry.object, this.#depthLayer(pivotY + 0.05, SORT_ENTITY));
2509
2516
const overlays = entry.overlays ?? [];
2510
2517
for (let i = 0; i < overlays.length; i++)
2511
2518
{
2512
if (overlays[i])
2513
{ overlays[i].layer = this.#depthLayer(pivotY + 0.05 + 0.001 * (i + 1), SORT_ENTITY); }
2519
this.#setLayer(
2520
overlays[i],
2521
this.#depthLayer(pivotY + 0.05 + 0.001 * (i + 1), SORT_ENTITY)
2522
);
2514
2523
}
2515
2524
}
2516
this.#markSortDirty();
2517
2525
}
2518
2526
2519
2527
/**
@@ -3342,8 +3350,7 @@ export class WorldView
3342
3350
entity.hpBar.transform.position.x = pivotX;
3343
3351
entity.hpBar.transform.position.y = spriteTop - barH / 2;
3344
3352
entity.hpBar.transform.size = new vector(barW, barH);
3345
entity.hpBar.layer = this.#depthLayer(pivotY, SORT_HP_BAR);
3346
this.#markSortDirty();
3353
this.#setLayer(entity.hpBar, this.#depthLayer(pivotY, SORT_HP_BAR));
3347
3354
}
3348
3355
3349
3356
#syncSkullMarker (entity)
@@ -3397,9 +3404,8 @@ export class WorldView
3397
3404
entity.skullMarker.transform.position.x = pivotX;
3398
3405
entity.skullMarker.transform.position.y = bodyTop - skullH / 2;
3399
3406
entity.skullMarker.transform.size = new vector(skullW, skullH);
3400
entity.skullMarker.layer = this.#depthLayer(pivotY, SORT_HP_BAR);
3407
this.#setLayer(entity.skullMarker, this.#depthLayer(pivotY, SORT_HP_BAR));
3401
3408
this.#syncLitSprite(entity.skullMarker, darkness, 0, false, entity.skullTint);
3402
this.#markSortDirty();
3403
3409
}
3404
3410
3405
3411
#rebuildChunk (snapshot, { refreshSoftNeighbors = true } = {})
@@ -3923,7 +3929,7 @@ export class WorldView
3923
3929
baked.groundObject.transform.position.x = left + chunkPx / 2;
3924
3930
baked.groundObject.transform.position.y = top + chunkPx / 2;
3925
3931
baked.groundObject.transform.size = new vector(chunkPx, chunkPx);
3926
baked.groundObject.layer = LAYER_GROUND;
3932
this.#setLayer(baked.groundObject, LAYER_GROUND);
3927
3933
3928
3934
for (let row = 0; row < CHUNK_SIZE; row++)
3929
3935
{
@@ -3932,15 +3938,14 @@ export class WorldView
3932
3938
object.transform.position.x = left + chunkPx / 2;
3933
3939
object.transform.position.y = rowY;
3934
3940
object.transform.size = new vector(chunkPx, size);
3935
object.layer = this.#depthLayer(rowY, SORT_BLOCK);
3941
this.#setLayer(object, this.#depthLayer(rowY, SORT_BLOCK));
3936
3942
}
3937
3943
3938
3944
baked.bloomObject.transform.position.x = left + chunkPx / 2;
3939
3945
baked.bloomObject.transform.position.y = top + chunkPx / 2;
3940
3946
baked.bloomObject.transform.size = new vector(chunkPx, chunkPx);
3941
baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK);
3947
this.#setLayer(baked.bloomObject, this.#depthLayer(top + chunkPx, SORT_BLOCK));
3942
3948
baked.bloomObject.renderer.layer = RENDER_LAYER_BLOOM;
3943
this.#markSortDirty();
3944
3949
}
3945
3950
3946
3951
#destroyBakedChunk (key)
@@ -4014,10 +4019,9 @@ export class WorldView
4014
4019
objects.ground.transform.position.x = tileX * size + size / 2;
4015
4020
objects.ground.transform.position.y = tileY * size + size / 2;
4016
4021
objects.ground.transform.size = new vector(size, size);
4017
objects.ground.layer = LAYER_GROUND;
4022
this.#setLayer(objects.ground, LAYER_GROUND);
4018
4023
objects.groundTypeId = animatedGround.typeId;
4019
4024
this.#syncGroundWaterFx(objects, 1, performance.now() / 1000);
4020
this.#markSortDirty();
4021
4025
}
4022
4026
if (animatedGround.bloom)
4023
4027
{ objects.groundBloom = this.#placeBloomOverlay(objects.groundBloom, animatedGround.bloom, tilePivot(tileX, tileY, this.tileSize()), LAYER_GROUND); }
@@ -4127,8 +4131,7 @@ export class WorldView
4127
4131
object.transform.position.x = center.x;
4128
4132
object.transform.position.y = center.y;
4129
4133
object.transform.size = new vector(draw.w * factor, draw.h * factor);
4130
object.layer = this.#depthLayer(sortY, subOrder, flight);
4131
this.#markSortDirty();
4134
this.#setLayer(object, this.#depthLayer(sortY, subOrder, flight));
4132
4135
}
4133
4136
4134
4137
#ensureSprite (existing, name, layer)
@@ -4227,8 +4230,7 @@ export class WorldView
4227
4230
existing.transform.size = horizontal
4228
4231
? new vector(length, thickness)
4229
4232
: new vector(thickness, length);
4230
existing.layer = LAYER_EDGE;
4231
this.#markSortDirty();
4233
this.#setLayer(existing, LAYER_EDGE);
4232
4234
return existing;
4233
4235
}
4234
4236
@@ -4254,6 +4256,15 @@ export class WorldView
4254
4256
return fromAlpha + (toAlpha - fromAlpha) * t;
4255
4257
}
4256
4258
4259
/** A layer write costs the scene a full re-sort, so only write real changes. */
4260
#setLayer (object, layer)
4261
{
4262
if (!object || object.layer === layer)
4263
{ return; }
4264
object.layer = layer;
4265
this.#markSortDirty();
4266
}
4267
4257
4268
#markSortDirty ()
4258
4269
{
4259
4270
this.scene._sortDirty = true;
@@ -477,8 +477,8 @@ export class WorldView477
this.catalog = catalog;477
this.catalog = catalog;478
this.preview = null;478
this.preview = null;479
this.zoom = 2;479
this.zoom = 2;480
/** World-Y origin for viewport-relative depth sort (see {@link #depthLayer}). */480
/** Chunk-quantised world-Y origin for the depth sort (see {@link #depthLayer}). */481
this.ySortOrigin = 0;481
this.ySortOrigin = null;482
this.chunks = new Map();482
this.chunks = new Map();483
this.bakedChunks = new Map();483
this.bakedChunks = new Map();484
this.tiles = new Map();484
this.tiles = new Map();@@ -1655,8 +1655,11 @@ export class WorldView1655
1655
1656
// The y-sort origin must settle before any layer is computed this frame,1656
// The y-sort origin must settle before any layer is computed this frame,1657
// otherwise entities and blocks end up sorted against different origins.1657
// otherwise entities and blocks end up sorted against different origins.1658
// It only moves when the view crosses a chunk row; the static world then1659
// needs one re-layer pass, and nothing at all on every other frame.1658
this.#centerCamera();1660
this.#centerCamera();1659
this.#syncYSortOrigin();1661
if (this.#syncYSortOrigin())1662
{ this.#refreshDepthLayers(); }1660
1663
1661
for (const entity of this.entities.values())1664
for (const entity of this.entities.values())1662
{ this.#placeEntity(entity, now); }1665
{ this.#placeEntity(entity, now); }@@ -1665,7 +1668,6 @@ export class WorldView1665
this.#updateDamageNumbers(now);1668
this.#updateDamageNumbers(now);1666
this.#updateSkillLevelFloats(now);1669
this.#updateSkillLevelFloats(now);1667
this.#relayoutSpellTargetHighlight();1670
this.#relayoutSpellTargetHighlight();1668
this.#refreshDepthLayers();1669
this.#cullOffscreenDrawables();1671
this.#cullOffscreenDrawables();1670
this.#updateShade(deltaTime);1672
this.#updateShade(deltaTime);1671
}1673
}@@ -2345,10 +2347,9 @@ export class WorldView2345
highlight.object.transform.position.x = src.position.x;2347
highlight.object.transform.position.x = src.position.x;2346
highlight.object.transform.position.y = src.position.y;2348
highlight.object.transform.position.y = src.position.y;2347
highlight.object.transform.size = new vector(src.size.x + pad, src.size.y + pad);2349
highlight.object.transform.size = new vector(src.size.x + pad, src.size.y + pad);2348
highlight.object.layer = LAYER_EDGE - 1;2350
this.#setLayer(highlight.object, LAYER_EDGE - 1);2349
highlight.tileX = entity.tileX;2351
highlight.tileX = entity.tileX;2350
highlight.tileY = entity.tileY;2352
highlight.tileY = entity.tileY;2351
this.#markSortDirty();2352
return;2353
return;2353
}2354
}2354
const size = this.tileSize();2355
const size = this.tileSize();@@ -2357,8 +2358,7 @@ export class WorldView2357
highlight.object.transform.position.x = left + size / 2;2358
highlight.object.transform.position.x = left + size / 2;2358
highlight.object.transform.position.y = top + size / 2;2359
highlight.object.transform.position.y = top + size / 2;2359
highlight.object.transform.size = new vector(size, size);2360
highlight.object.transform.size = new vector(size, size);2360
highlight.object.layer = LAYER_EDGE - 1;2361
this.#setLayer(highlight.object, LAYER_EDGE - 1);2361
this.#markSortDirty();2362
}2362
}2363
2363
2364
#relayoutSpawnRangeHighlight ()2364
#relayoutSpawnRangeHighlight ()@@ -2378,8 +2378,7 @@ export class WorldView2378
highlight.object.transform.position.x = left + side / 2;2378
highlight.object.transform.position.x = left + side / 2;2379
highlight.object.transform.position.y = top + side / 2;2379
highlight.object.transform.position.y = top + side / 2;2380
highlight.object.transform.size = new vector(side, side);2380
highlight.object.transform.size = new vector(side, side);2381
highlight.object.layer = layer;2381
this.#setLayer(highlight.object, layer);2382
this.#markSortDirty();2383
}2382
}2384
2383
2385
setTilePreview (tileX, tileY, override)2384
setTilePreview (tileX, tileY, override)@@ -2435,14 +2434,26 @@ export class WorldView2435
}2434
}2436
2435
2437
/**2436
/**2438
* World Y at the viewport centre. Depth sort uses worldY − this so layers stay2437
* World Y the depth sort is measured against, so layers stay bounded2439
* bounded (absolute tileY×zoom otherwise overflows past LAYER_EDGE).2438
* (absolute tileY×zoom otherwise overflows past LAYER_EDGE).2439
*2440
* Quantised to the chunk row under the viewport centre: every sprite's layer2441
* is relative to this, so an origin that moved with the camera would mean2442
* re-layering the whole world every frame. Chunks only span the static 5×52443
* ring, so relative sortY stays far inside LAYER_EDGE.2444
*2445
* @returns {boolean} whether the origin moved, i.e. callers must re-layer.2440
*/2446
*/2441
#syncYSortOrigin ()2447
#syncYSortOrigin ()2442
{2448
{2443
const camY = this.scene.cameraPosition.y;2449
const camY = this.scene.cameraPosition.y;2444
const screenH = this.scene.real_size.y;2450
const screenH = this.scene.real_size.y;2445
this.ySortOrigin = -camY + screenH / 2;2451
const chunkPx = CHUNK_SIZE * this.tileSize();2452
const origin = Math.floor((-camY + screenH / 2) / chunkPx) * chunkPx;2453
if (origin === this.ySortOrigin)2454
{ return false; }2455
this.ySortOrigin = origin;2456
return true;2446
}2457
}2447
2458
2448
/** Viewport-relative y-sort layer for world sprites. */2459
/** Viewport-relative y-sort layer for world sprites. */@@ -2454,8 +2465,11 @@ export class WorldView2454
2465
2455
/**2466
/**2456
* Re-apply depth for every world sprite that is not repositioned each frame.2467
* Re-apply depth for every world sprite that is not repositioned each frame.2457
* Their layer is relative to the y-sort origin, which moves with the camera,2468
* Their layer is relative to the y-sort origin, so a layer assigned once at2458
* so a layer assigned once at draw time drifts as the player walks.2469
* draw time goes stale as soon as that origin moves.2470
*2471
* Only run when {@link #syncYSortOrigin} reports a move (or after a zoom /2472
* layout change) — entities carry their own per-frame layer via #placeEntity.2459
*/2473
*/2460
#refreshDepthLayers ()2474
#refreshDepthLayers ()2461
{2475
{@@ -2466,14 +2480,10 @@ export class WorldView2466
const chunkPx = CHUNK_SIZE * size;2480
const chunkPx = CHUNK_SIZE * size;2467
for (let row = 0; row < CHUNK_SIZE; row++)2481
for (let row = 0; row < CHUNK_SIZE; row++)2468
{2482
{2469
const object = baked.blockObjects?.[row];2470
if (!object)2471
{ continue; }2472
const rowY = top + row * size + size / 2;2483
const rowY = top + row * size + size / 2;2473
object.layer = this.#depthLayer(rowY, SORT_BLOCK);2484
this.#setLayer(baked.blockObjects?.[row], this.#depthLayer(rowY, SORT_BLOCK));2474
}2485
}2475
if (baked.bloomObject)2486
this.#setLayer(baked.bloomObject, this.#depthLayer(top + chunkPx, SORT_BLOCK));2476
{ baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK); }2477
}2487
}2478
// Unbaked tile sprites: tall blocks, doors and animated grounds.2488
// Unbaked tile sprites: tall blocks, doors and animated grounds.2479
for (const objects of this.tiles.values())2489
for (const objects of this.tiles.values())@@ -2481,39 +2491,37 @@ export class WorldView2481
if (!objects.block && !objects.blockBloom && !objects.groundBloom)2491
if (!objects.block && !objects.blockBloom && !objects.groundBloom)2482
{ continue; }2492
{ continue; }2483
const pivotY = objects.tileY * size + size / 2;2493
const pivotY = objects.tileY * size + size / 2;2484
if (objects.block)2494
this.#setLayer(objects.block, this.#depthLayer(pivotY, objects.blockSort ?? SORT_ENTITY));2485
{ objects.block.layer = this.#depthLayer(pivotY, objects.blockSort ?? SORT_ENTITY); }2495
this.#setLayer(objects.blockBloom, this.#depthLayer(pivotY, SORT_ENTITY));2486
if (objects.blockBloom)2496
this.#setLayer(objects.groundBloom, this.#depthLayer(pivotY, LAYER_GROUND));2487
{ objects.blockBloom.layer = this.#depthLayer(pivotY, SORT_ENTITY); }2488
if (objects.groundBloom)2489
{ objects.groundBloom.layer = this.#depthLayer(pivotY, LAYER_GROUND); }2490
}2497
}2491
for (const item of this.groundItems.values())2498
for (const item of this.groundItems.values())2492
{2499
{2493
if (!item.object)2500
this.#setLayer(2494
{ continue; }2501
item.object,2495
item.object.layer = this.#depthLayer(item.tileY * size + size / 2 - 0.01, SORT_BLOCK);2502
this.#depthLayer(item.tileY * size + size / 2 - 0.01, SORT_BLOCK)2503
);2496
}2504
}2497
// Lerping FX are re-placed in #updateSwipes; static ones need the refresh.2505
// Lerping FX are re-placed in #updateSwipes; static ones need the refresh.2498
for (const swipe of this.swipes)2506
for (const swipe of this.swipes)2499
{2507
{2500
if (swipe.lerp || swipe.sortY == null || !swipe.object)2508
if (swipe.lerp || swipe.sortY == null)2501
{ continue; }2509
{ continue; }2502
swipe.object.layer = this.#depthLayer(swipe.sortY, SORT_FX);2510
this.#setLayer(swipe.object, this.#depthLayer(swipe.sortY, SORT_FX));2503
}2511
}2504
for (const entry of this.editorSpawners.values())2512
for (const entry of this.editorSpawners.values())2505
{2513
{2506
const pivotY = entry.tileY * size + size / 2;2514
const pivotY = entry.tileY * size + size / 2;2507
if (entry.object)2515
this.#setLayer(entry.object, this.#depthLayer(pivotY + 0.05, SORT_ENTITY));2508
{ entry.object.layer = this.#depthLayer(pivotY + 0.05, SORT_ENTITY); }2509
const overlays = entry.overlays ?? [];2516
const overlays = entry.overlays ?? [];2510
for (let i = 0; i < overlays.length; i++)2517
for (let i = 0; i < overlays.length; i++)2511
{2518
{2512
if (overlays[i])2519
this.#setLayer(2513
{ overlays[i].layer = this.#depthLayer(pivotY + 0.05 + 0.001 * (i + 1), SORT_ENTITY); }2520
overlays[i],2521
this.#depthLayer(pivotY + 0.05 + 0.001 * (i + 1), SORT_ENTITY)2522
);2514
}2523
}2515
}2524
}2516
this.#markSortDirty();2517
}2525
}2518
2526
2519
/**2527
/**@@ -3342,8 +3350,7 @@ export class WorldView3342
entity.hpBar.transform.position.x = pivotX;3350
entity.hpBar.transform.position.x = pivotX;3343
entity.hpBar.transform.position.y = spriteTop - barH / 2;3351
entity.hpBar.transform.position.y = spriteTop - barH / 2;3344
entity.hpBar.transform.size = new vector(barW, barH);3352
entity.hpBar.transform.size = new vector(barW, barH);3345
entity.hpBar.layer = this.#depthLayer(pivotY, SORT_HP_BAR);3353
this.#setLayer(entity.hpBar, this.#depthLayer(pivotY, SORT_HP_BAR));3346
this.#markSortDirty();3347
}3354
}3348
3355
3349
#syncSkullMarker (entity)3356
#syncSkullMarker (entity)@@ -3397,9 +3404,8 @@ export class WorldView3397
entity.skullMarker.transform.position.x = pivotX;3404
entity.skullMarker.transform.position.x = pivotX;3398
entity.skullMarker.transform.position.y = bodyTop - skullH / 2;3405
entity.skullMarker.transform.position.y = bodyTop - skullH / 2;3399
entity.skullMarker.transform.size = new vector(skullW, skullH);3406
entity.skullMarker.transform.size = new vector(skullW, skullH);3400
entity.skullMarker.layer = this.#depthLayer(pivotY, SORT_HP_BAR);3407
this.#setLayer(entity.skullMarker, this.#depthLayer(pivotY, SORT_HP_BAR));3401
this.#syncLitSprite(entity.skullMarker, darkness, 0, false, entity.skullTint);3408
this.#syncLitSprite(entity.skullMarker, darkness, 0, false, entity.skullTint);3402
this.#markSortDirty();3403
}3409
}3404
3410
3405
#rebuildChunk (snapshot, { refreshSoftNeighbors = true } = {})3411
#rebuildChunk (snapshot, { refreshSoftNeighbors = true } = {})@@ -3923,7 +3929,7 @@ export class WorldView3923
baked.groundObject.transform.position.x = left + chunkPx / 2;3929
baked.groundObject.transform.position.x = left + chunkPx / 2;3924
baked.groundObject.transform.position.y = top + chunkPx / 2;3930
baked.groundObject.transform.position.y = top + chunkPx / 2;3925
baked.groundObject.transform.size = new vector(chunkPx, chunkPx);3931
baked.groundObject.transform.size = new vector(chunkPx, chunkPx);3926
baked.groundObject.layer = LAYER_GROUND;3932
this.#setLayer(baked.groundObject, LAYER_GROUND);3927
3933
3928
for (let row = 0; row < CHUNK_SIZE; row++)3934
for (let row = 0; row < CHUNK_SIZE; row++)3929
{3935
{@@ -3932,15 +3938,14 @@ export class WorldView3932
object.transform.position.x = left + chunkPx / 2;3938
object.transform.position.x = left + chunkPx / 2;3933
object.transform.position.y = rowY;3939
object.transform.position.y = rowY;3934
object.transform.size = new vector(chunkPx, size);3940
object.transform.size = new vector(chunkPx, size);3935
object.layer = this.#depthLayer(rowY, SORT_BLOCK);3941
this.#setLayer(object, this.#depthLayer(rowY, SORT_BLOCK));3936
}3942
}3937
3943
3938
baked.bloomObject.transform.position.x = left + chunkPx / 2;3944
baked.bloomObject.transform.position.x = left + chunkPx / 2;3939
baked.bloomObject.transform.position.y = top + chunkPx / 2;3945
baked.bloomObject.transform.position.y = top + chunkPx / 2;3940
baked.bloomObject.transform.size = new vector(chunkPx, chunkPx);3946
baked.bloomObject.transform.size = new vector(chunkPx, chunkPx);3941
baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK);3947
this.#setLayer(baked.bloomObject, this.#depthLayer(top + chunkPx, SORT_BLOCK));3942
baked.bloomObject.renderer.layer = RENDER_LAYER_BLOOM;3948
baked.bloomObject.renderer.layer = RENDER_LAYER_BLOOM;3943
this.#markSortDirty();3944
}3949
}3945
3950
3946
#destroyBakedChunk (key)3951
#destroyBakedChunk (key)@@ -4014,10 +4019,9 @@ export class WorldView4014
objects.ground.transform.position.x = tileX * size + size / 2;4019
objects.ground.transform.position.x = tileX * size + size / 2;4015
objects.ground.transform.position.y = tileY * size + size / 2;4020
objects.ground.transform.position.y = tileY * size + size / 2;4016
objects.ground.transform.size = new vector(size, size);4021
objects.ground.transform.size = new vector(size, size);4017
objects.ground.layer = LAYER_GROUND;4022
this.#setLayer(objects.ground, LAYER_GROUND);4018
objects.groundTypeId = animatedGround.typeId;4023
objects.groundTypeId = animatedGround.typeId;4019
this.#syncGroundWaterFx(objects, 1, performance.now() / 1000);4024
this.#syncGroundWaterFx(objects, 1, performance.now() / 1000);4020
this.#markSortDirty();4021
}4025
}4022
if (animatedGround.bloom)4026
if (animatedGround.bloom)4023
{ objects.groundBloom = this.#placeBloomOverlay(objects.groundBloom, animatedGround.bloom, tilePivot(tileX, tileY, this.tileSize()), LAYER_GROUND); }4027
{ objects.groundBloom = this.#placeBloomOverlay(objects.groundBloom, animatedGround.bloom, tilePivot(tileX, tileY, this.tileSize()), LAYER_GROUND); }@@ -4127,8 +4131,7 @@ export class WorldView4127
object.transform.position.x = center.x;4131
object.transform.position.x = center.x;4128
object.transform.position.y = center.y;4132
object.transform.position.y = center.y;4129
object.transform.size = new vector(draw.w * factor, draw.h * factor);4133
object.transform.size = new vector(draw.w * factor, draw.h * factor);4130
object.layer = this.#depthLayer(sortY, subOrder, flight);4134
this.#setLayer(object, this.#depthLayer(sortY, subOrder, flight));4131
this.#markSortDirty();4132
}4135
}4133
4136
4134
#ensureSprite (existing, name, layer)4137
#ensureSprite (existing, name, layer)@@ -4227,8 +4230,7 @@ export class WorldView4227
existing.transform.size = horizontal4230
existing.transform.size = horizontal4228
? new vector(length, thickness)4231
? new vector(length, thickness)4229
: new vector(thickness, length);4232
: new vector(thickness, length);4230
existing.layer = LAYER_EDGE;4233
this.#setLayer(existing, LAYER_EDGE);4231
this.#markSortDirty();4232
return existing;4234
return existing;4233
}4235
}4234
4236
@@ -4254,6 +4256,15 @@ export class WorldView4254
return fromAlpha + (toAlpha - fromAlpha) * t;4256
return fromAlpha + (toAlpha - fromAlpha) * t;4255
}4257
}4256
4258
4259
/** A layer write costs the scene a full re-sort, so only write real changes. */4260
#setLayer (object, layer)4261
{4262
if (!object || object.layer === layer)4263
{ return; }4264
object.layer = layer;4265
this.#markSortDirty();4266
}4267
4257
#markSortDirty ()4268
#markSortDirty ()4258
{4269
{4259
this.scene._sortDirty = true;4270
this.scene._sortDirty = true;@@ -1,6 +1,7 @@
1
1
import test from "node:test";
2
2
import assert from "node:assert/strict";
3
3
import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer, Y_SORT_BASE } from "../../shared/render/pivot.js";
4
import { CHUNK_SIZE, SPRITE_SIZE_PX, STATIC_CHUNK_RADIUS } from "../../shared/protocol/constants.js";
4
5
5
6
test("a 16×16 sprite pivot sits on the tile centre", () =>
6
7
{
@@ -48,6 +49,23 @@ test("viewport-relative Y-sort stays below UI edge band at high world Y", () =>
48
49
assert.ok(absoluteWouldOverflow > layerEdge);
49
50
});
50
51
52
test("chunk-quantised sort origin keeps the whole loaded ring inside the sort band", () =>
53
{
54
// The client pins the sort origin to the top of the chunk row under the
55
// viewport centre, so it only moves when the view crosses a chunk border.
56
// Loaded tiles then span centre − radius … centre + radius + 1 chunk.
57
const layerEdge = 900_000;
58
const maxZoom = 4;
59
const chunkPx = CHUNK_SIZE * SPRITE_SIZE_PX * maxZoom;
60
const northMost = -STATIC_CHUNK_RADIUS * chunkPx;
61
const southMost = (STATIC_CHUNK_RADIUS + 1) * chunkPx;
62
const northLayer = ySortLayer(northMost, 0);
63
const southLayer = ySortLayer(southMost, 15);
64
assert.ok(northLayer > 0, `expected ${northLayer} > 0`);
65
assert.ok(southLayer < layerEdge, `expected ${southLayer} < ${layerEdge}`);
66
assert.ok(flightSortLayer(northMost, 0) > southLayer);
67
});
68
51
69
test("S-closed door stays in front of entity equipment overlays on the same tile", () =>
52
70
{
53
71
const sortY = 128;
@@ -1,6 +1,7 @@1
import test from "node:test";1
import test from "node:test";2
import assert from "node:assert/strict";2
import assert from "node:assert/strict";3
import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer, Y_SORT_BASE } from "../../shared/render/pivot.js";3
import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer, Y_SORT_BASE } from "../../shared/render/pivot.js";4
import { CHUNK_SIZE, SPRITE_SIZE_PX, STATIC_CHUNK_RADIUS } from "../../shared/protocol/constants.js";4
5
5
test("a 16×16 sprite pivot sits on the tile centre", () =>6
test("a 16×16 sprite pivot sits on the tile centre", () =>6
{7
{@@ -48,6 +49,23 @@ test("viewport-relative Y-sort stays below UI edge band at high world Y", () =>48
assert.ok(absoluteWouldOverflow > layerEdge);49
assert.ok(absoluteWouldOverflow > layerEdge);49
});50
});50
51
52
test("chunk-quantised sort origin keeps the whole loaded ring inside the sort band", () =>53
{54
// The client pins the sort origin to the top of the chunk row under the55
// viewport centre, so it only moves when the view crosses a chunk border.56
// Loaded tiles then span centre − radius … centre + radius + 1 chunk.57
const layerEdge = 900_000;58
const maxZoom = 4;59
const chunkPx = CHUNK_SIZE * SPRITE_SIZE_PX * maxZoom;60
const northMost = -STATIC_CHUNK_RADIUS * chunkPx;61
const southMost = (STATIC_CHUNK_RADIUS + 1) * chunkPx;62
const northLayer = ySortLayer(northMost, 0);63
const southLayer = ySortLayer(southMost, 15);64
assert.ok(northLayer > 0, `expected ${northLayer} > 0`);65
assert.ok(southLayer < layerEdge, `expected ${southLayer} < ${layerEdge}`);66
assert.ok(flightSortLayer(northMost, 0) > southLayer);67
});68
51
test("S-closed door stays in front of entity equipment overlays on the same tile", () =>69
test("S-closed door stays in front of entity equipment overlays on the same tile", () =>52
{70
{53
const sortY = 128;71
const sortY = 128;