BlueGit

timfa questward

Sort world sprites relative to the viewport Y origin.

Absolute tileY×zoom depth keys overflow past LAYER_EDGE on high chunks when zoomed in, so blocks/mobs sorted into the wrong band and vanished. Keep layers viewport-relative and refresh baked rows when the camera moves.

timfa Tim Falken · pushed by timfa

Authored 2026-09-02 · 2 months ago

Hash
7e332c40d7cea9db2d0a888eb8d6b8adf335444b
Parent
62496a89
3 files changed +79 −11
client/src/render/worldView.js +51 −7
@@ -473,6 +473,8 @@ export class WorldView
473 473 this.catalog = catalog;
474 474 this.preview = null;
475 475 this.zoom = 2;
476 /** World-Y origin for viewport-relative depth sort (see {@link #depthLayer}). */
477 this.ySortOrigin = 0;
476 478 this.chunks = new Map();
477 479 this.bakedChunks = new Map();
478 480 this.tiles = new Map();
@@ -1375,6 +1377,8 @@ export class WorldView
1375 1377 refreshViewport ()
1376 1378 {
1377 1379 this.#centerCamera();
1380 this.#syncYSortOrigin();
1381 this.#refreshBakedDepthLayers();
1378 1382 this.#cullOffscreenDrawables();
1379 1383 }
1380 1384
@@ -1478,6 +1482,7 @@ export class WorldView
1478 1482 this.#updateSkillLevelFloats(now);
1479 1483 this.#relayoutSpellTargetHighlight();
1480 1484 this.#centerCamera();
1485 this.#refreshBakedDepthLayers();
1481 1486 this.#cullOffscreenDrawables();
1482 1487 this.#updateShade(deltaTime);
1483 1488 }
@@ -2232,6 +2237,47 @@ export class WorldView
2232 2237 const y = local.visualY + (local.stackOffsetY ?? 0);
2233 2238 this.scene.cameraPosition.x = this.scene.real_size.x / 2 - x;
2234 2239 this.scene.cameraPosition.y = this.scene.real_size.y / 2 - y;
2240 this.#syncYSortOrigin();
2241 }
2242
2243 /**
2244 * World Y at the viewport centre. Depth sort uses worldY − this so layers stay
2245 * bounded (absolute tileY×zoom otherwise overflows past LAYER_EDGE).
2246 */
2247 #syncYSortOrigin ()
2248 {
2249 const camY = this.scene.cameraPosition.y;
2250 const screenH = this.scene.real_size.y;
2251 this.ySortOrigin = -camY + screenH / 2;
2252 }
2253
2254 /** Viewport-relative y-sort layer for world sprites. */
2255 #depthLayer (sortY, subOrder, flight = false)
2256 {
2257 const relative = sortY - (this.ySortOrigin ?? 0);
2258 return flight ? flightSortLayer(relative, subOrder) : ySortLayer(relative, subOrder);
2259 }
2260
2261 /** Re-apply baked chunk row depth after the y-sort origin moves with the camera. */
2262 #refreshBakedDepthLayers ()
2263 {
2264 const size = this.tileSize();
2265 for (const baked of this.bakedChunks.values())
2266 {
2267 const top = baked.chunkY * CHUNK_SIZE * size;
2268 const chunkPx = CHUNK_SIZE * size;
2269 for (let row = 0; row < CHUNK_SIZE; row++)
2270 {
2271 const object = baked.blockObjects?.[row];
2272 if (!object)
2273 { continue; }
2274 const rowY = top + row * size + size / 2;
2275 object.layer = this.#depthLayer(rowY, SORT_BLOCK);
2276 }
2277 if (baked.bloomObject)
2278 { baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK); }
2279 }
2280 this.#markSortDirty();
2235 2281 }
2236 2282
2237 2283 /**
@@ -3026,7 +3072,7 @@ export class WorldView
3026 3072 entity.hpBar.transform.position.x = pivotX;
3027 3073 entity.hpBar.transform.position.y = spriteTop - barH / 2;
3028 3074 entity.hpBar.transform.size = new vector(barW, barH);
3029 entity.hpBar.layer = ySortLayer(pivotY, SORT_HP_BAR);
3075 entity.hpBar.layer = this.#depthLayer(pivotY, SORT_HP_BAR);
3030 3076 this.#markSortDirty();
3031 3077 }
3032 3078
@@ -3081,7 +3127,7 @@ export class WorldView
3081 3127 entity.skullMarker.transform.position.x = pivotX;
3082 3128 entity.skullMarker.transform.position.y = bodyTop - skullH / 2;
3083 3129 entity.skullMarker.transform.size = new vector(skullW, skullH);
3084 entity.skullMarker.layer = ySortLayer(pivotY, SORT_HP_BAR);
3130 entity.skullMarker.layer = this.#depthLayer(pivotY, SORT_HP_BAR);
3085 3131 this.#syncLitSprite(entity.skullMarker, darkness, 0, false, entity.skullTint);
3086 3132 this.#markSortDirty();
3087 3133 }
@@ -3616,13 +3662,13 @@ export class WorldView
3616 3662 object.transform.position.x = left + chunkPx / 2;
3617 3663 object.transform.position.y = rowY;
3618 3664 object.transform.size = new vector(chunkPx, size);
3619 object.layer = ySortLayer(rowY, SORT_BLOCK);
3665 object.layer = this.#depthLayer(rowY, SORT_BLOCK);
3620 3666 }
3621 3667
3622 3668 baked.bloomObject.transform.position.x = left + chunkPx / 2;
3623 3669 baked.bloomObject.transform.position.y = top + chunkPx / 2;
3624 3670 baked.bloomObject.transform.size = new vector(chunkPx, chunkPx);
3625 baked.bloomObject.layer = ySortLayer(top + chunkPx, SORT_BLOCK);
3671 baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK);
3626 3672 baked.bloomObject.renderer.layer = RENDER_LAYER_BLOOM;
3627 3673 this.#markSortDirty();
3628 3674 }
@@ -3809,9 +3855,7 @@ export class WorldView
3809 3855 object.transform.position.x = center.x;
3810 3856 object.transform.position.y = center.y;
3811 3857 object.transform.size = new vector(draw.w * factor, draw.h * factor);
3812 object.layer = flight
3813 ? flightSortLayer(sortY, subOrder)
3814 : ySortLayer(sortY, subOrder);
3858 object.layer = this.#depthLayer(sortY, subOrder, flight);
3815 3859 this.#markSortDirty();
3816 3860 }
3817 3861
@@ -473,6 +473,8 @@ export class WorldView
473 this.catalog = catalog;
473 this.catalog = catalog;
474 this.preview = null;
474 this.preview = null;
475 this.zoom = 2;
475 this.zoom = 2;
476 /** World-Y origin for viewport-relative depth sort (see {@link #depthLayer}). */
477 this.ySortOrigin = 0;
476 this.chunks = new Map();
478 this.chunks = new Map();
477 this.bakedChunks = new Map();
479 this.bakedChunks = new Map();
478 this.tiles = new Map();
480 this.tiles = new Map();
@@ -1375,6 +1377,8 @@ export class WorldView
1375 refreshViewport ()
1377 refreshViewport ()
1376 {
1378 {
1377 this.#centerCamera();
1379 this.#centerCamera();
1380 this.#syncYSortOrigin();
1381 this.#refreshBakedDepthLayers();
1378 this.#cullOffscreenDrawables();
1382 this.#cullOffscreenDrawables();
1379 }
1383 }
1380
1384
@@ -1478,6 +1482,7 @@ export class WorldView
1478 this.#updateSkillLevelFloats(now);
1482 this.#updateSkillLevelFloats(now);
1479 this.#relayoutSpellTargetHighlight();
1483 this.#relayoutSpellTargetHighlight();
1480 this.#centerCamera();
1484 this.#centerCamera();
1485 this.#refreshBakedDepthLayers();
1481 this.#cullOffscreenDrawables();
1486 this.#cullOffscreenDrawables();
1482 this.#updateShade(deltaTime);
1487 this.#updateShade(deltaTime);
1483 }
1488 }
@@ -2232,6 +2237,47 @@ export class WorldView
2232 const y = local.visualY + (local.stackOffsetY ?? 0);
2237 const y = local.visualY + (local.stackOffsetY ?? 0);
2233 this.scene.cameraPosition.x = this.scene.real_size.x / 2 - x;
2238 this.scene.cameraPosition.x = this.scene.real_size.x / 2 - x;
2234 this.scene.cameraPosition.y = this.scene.real_size.y / 2 - y;
2239 this.scene.cameraPosition.y = this.scene.real_size.y / 2 - y;
2240 this.#syncYSortOrigin();
2241 }
2242
2243 /**
2244 * World Y at the viewport centre. Depth sort uses worldY − this so layers stay
2245 * bounded (absolute tileY×zoom otherwise overflows past LAYER_EDGE).
2246 */
2247 #syncYSortOrigin ()
2248 {
2249 const camY = this.scene.cameraPosition.y;
2250 const screenH = this.scene.real_size.y;
2251 this.ySortOrigin = -camY + screenH / 2;
2252 }
2253
2254 /** Viewport-relative y-sort layer for world sprites. */
2255 #depthLayer (sortY, subOrder, flight = false)
2256 {
2257 const relative = sortY - (this.ySortOrigin ?? 0);
2258 return flight ? flightSortLayer(relative, subOrder) : ySortLayer(relative, subOrder);
2259 }
2260
2261 /** Re-apply baked chunk row depth after the y-sort origin moves with the camera. */
2262 #refreshBakedDepthLayers ()
2263 {
2264 const size = this.tileSize();
2265 for (const baked of this.bakedChunks.values())
2266 {
2267 const top = baked.chunkY * CHUNK_SIZE * size;
2268 const chunkPx = CHUNK_SIZE * size;
2269 for (let row = 0; row < CHUNK_SIZE; row++)
2270 {
2271 const object = baked.blockObjects?.[row];
2272 if (!object)
2273 { continue; }
2274 const rowY = top + row * size + size / 2;
2275 object.layer = this.#depthLayer(rowY, SORT_BLOCK);
2276 }
2277 if (baked.bloomObject)
2278 { baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK); }
2279 }
2280 this.#markSortDirty();
2235 }
2281 }
2236
2282
2237 /**
2283 /**
@@ -3026,7 +3072,7 @@ export class WorldView
3026 entity.hpBar.transform.position.x = pivotX;
3072 entity.hpBar.transform.position.x = pivotX;
3027 entity.hpBar.transform.position.y = spriteTop - barH / 2;
3073 entity.hpBar.transform.position.y = spriteTop - barH / 2;
3028 entity.hpBar.transform.size = new vector(barW, barH);
3074 entity.hpBar.transform.size = new vector(barW, barH);
3029 entity.hpBar.layer = ySortLayer(pivotY, SORT_HP_BAR);
3075 entity.hpBar.layer = this.#depthLayer(pivotY, SORT_HP_BAR);
3030 this.#markSortDirty();
3076 this.#markSortDirty();
3031 }
3077 }
3032
3078
@@ -3081,7 +3127,7 @@ export class WorldView
3081 entity.skullMarker.transform.position.x = pivotX;
3127 entity.skullMarker.transform.position.x = pivotX;
3082 entity.skullMarker.transform.position.y = bodyTop - skullH / 2;
3128 entity.skullMarker.transform.position.y = bodyTop - skullH / 2;
3083 entity.skullMarker.transform.size = new vector(skullW, skullH);
3129 entity.skullMarker.transform.size = new vector(skullW, skullH);
3084 entity.skullMarker.layer = ySortLayer(pivotY, SORT_HP_BAR);
3130 entity.skullMarker.layer = this.#depthLayer(pivotY, SORT_HP_BAR);
3085 this.#syncLitSprite(entity.skullMarker, darkness, 0, false, entity.skullTint);
3131 this.#syncLitSprite(entity.skullMarker, darkness, 0, false, entity.skullTint);
3086 this.#markSortDirty();
3132 this.#markSortDirty();
3087 }
3133 }
@@ -3616,13 +3662,13 @@ export class WorldView
3616 object.transform.position.x = left + chunkPx / 2;
3662 object.transform.position.x = left + chunkPx / 2;
3617 object.transform.position.y = rowY;
3663 object.transform.position.y = rowY;
3618 object.transform.size = new vector(chunkPx, size);
3664 object.transform.size = new vector(chunkPx, size);
3619 object.layer = ySortLayer(rowY, SORT_BLOCK);
3665 object.layer = this.#depthLayer(rowY, SORT_BLOCK);
3620 }
3666 }
3621
3667
3622 baked.bloomObject.transform.position.x = left + chunkPx / 2;
3668 baked.bloomObject.transform.position.x = left + chunkPx / 2;
3623 baked.bloomObject.transform.position.y = top + chunkPx / 2;
3669 baked.bloomObject.transform.position.y = top + chunkPx / 2;
3624 baked.bloomObject.transform.size = new vector(chunkPx, chunkPx);
3670 baked.bloomObject.transform.size = new vector(chunkPx, chunkPx);
3625 baked.bloomObject.layer = ySortLayer(top + chunkPx, SORT_BLOCK);
3671 baked.bloomObject.layer = this.#depthLayer(top + chunkPx, SORT_BLOCK);
3626 baked.bloomObject.renderer.layer = RENDER_LAYER_BLOOM;
3672 baked.bloomObject.renderer.layer = RENDER_LAYER_BLOOM;
3627 this.#markSortDirty();
3673 this.#markSortDirty();
3628 }
3674 }
@@ -3809,9 +3855,7 @@ export class WorldView
3809 object.transform.position.x = center.x;
3855 object.transform.position.x = center.x;
3810 object.transform.position.y = center.y;
3856 object.transform.position.y = center.y;
3811 object.transform.size = new vector(draw.w * factor, draw.h * factor);
3857 object.transform.size = new vector(draw.w * factor, draw.h * factor);
3812 object.layer = flight
3858 object.layer = this.#depthLayer(sortY, subOrder, flight);
3813 ? flightSortLayer(sortY, subOrder)
3814 : ySortLayer(sortY, subOrder);
3815 this.#markSortDirty();
3859 this.#markSortDirty();
3816 }
3860 }
3817
3861
server/tests/pivot.test.js +20 −3
@@ -1,6 +1,6 @@
1 1 import test from "node:test";
2 2 import assert from "node:assert/strict";
3 import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer } from "../../shared/render/pivot.js";
3 import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer, Y_SORT_BASE } from "../../shared/render/pivot.js";
4 4
5 5 test("a 16×16 sprite pivot sits on the tile centre", () =>
6 6 {
@@ -33,6 +33,21 @@ test("Y-sort puts a southern tile in front of a northern tile", () =>
33 33 assert.ok(ySortLayer(16, 1) > ySortLayer(0, 1));
34 34 });
35 35
36 test("viewport-relative Y-sort stays below UI edge band at high world Y", () =>
37 {
38 const layerEdge = 900_000;
39 const focusY = 50_000;
40 const zoomedTile = 64;
41 // Player around tileY ≈ 780 at zoom 4 — absolute sort used to exceed LAYER_EDGE.
42 const worldY = focusY + zoomedTile * 2;
43 const relative = worldY - focusY;
44 const layer = ySortLayer(relative, 3);
45 assert.ok(layer > Y_SORT_BASE);
46 assert.ok(layer < layerEdge, `expected ${layer} < ${layerEdge}`);
47 const absoluteWouldOverflow = Y_SORT_BASE + Math.round(worldY) * 16 + 3;
48 assert.ok(absoluteWouldOverflow > layerEdge);
49 });
50
36 51 test("S-closed door stays in front of entity equipment overlays on the same tile", () =>
37 52 {
38 53 const sortY = 128;
@@ -48,7 +63,9 @@ test("S-closed door stays in front of entity equipment overlays on the same tile
48 63
49 64 test("flight sort stays above any ground y-sort in view", () =>
50 65 {
51 const farSouthBlock = ySortLayer(50_000, 3);
52 assert.ok(flightSortLayer(0, 0) > farSouthBlock);
66 // In-view relative sortY stays within roughly ±screen height in world px.
67 const farInView = ySortLayer(2_000, 3);
68 assert.ok(flightSortLayer(0, 0) > farInView);
53 69 assert.ok(flightSortLayer(16, 1) > flightSortLayer(0, 1));
70 assert.ok(farInView < 900_000);
54 71 });
@@ -1,6 +1,6 @@
1import test from "node:test";
1import test from "node:test";
2import assert from "node:assert/strict";
2import assert from "node:assert/strict";
3import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer } from "../../shared/render/pivot.js";
3import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer, Y_SORT_BASE } from "../../shared/render/pivot.js";
4
4
5test("a 16×16 sprite pivot sits on the tile centre", () =>
5test("a 16×16 sprite pivot sits on the tile centre", () =>
6{
6{
@@ -33,6 +33,21 @@ test("Y-sort puts a southern tile in front of a northern tile", () =>
33 assert.ok(ySortLayer(16, 1) > ySortLayer(0, 1));
33 assert.ok(ySortLayer(16, 1) > ySortLayer(0, 1));
34});
34});
35
35
36test("viewport-relative Y-sort stays below UI edge band at high world Y", () =>
37{
38 const layerEdge = 900_000;
39 const focusY = 50_000;
40 const zoomedTile = 64;
41 // Player around tileY ≈ 780 at zoom 4 — absolute sort used to exceed LAYER_EDGE.
42 const worldY = focusY + zoomedTile * 2;
43 const relative = worldY - focusY;
44 const layer = ySortLayer(relative, 3);
45 assert.ok(layer > Y_SORT_BASE);
46 assert.ok(layer < layerEdge, `expected ${layer} < ${layerEdge}`);
47 const absoluteWouldOverflow = Y_SORT_BASE + Math.round(worldY) * 16 + 3;
48 assert.ok(absoluteWouldOverflow > layerEdge);
49});
50
36test("S-closed door stays in front of entity equipment overlays on the same tile", () =>
51test("S-closed door stays in front of entity equipment overlays on the same tile", () =>
37{
52{
38 const sortY = 128;
53 const sortY = 128;
@@ -48,7 +63,9 @@ test("S-closed door stays in front of entity equipment overlays on the same tile
48
63
49test("flight sort stays above any ground y-sort in view", () =>
64test("flight sort stays above any ground y-sort in view", () =>
50{
65{
51 const farSouthBlock = ySortLayer(50_000, 3);
66 // In-view relative sortY stays within roughly ±screen height in world px.
52 assert.ok(flightSortLayer(0, 0) > farSouthBlock);
67 const farInView = ySortLayer(2_000, 3);
68 assert.ok(flightSortLayer(0, 0) > farInView);
53 assert.ok(flightSortLayer(16, 1) > flightSortLayer(0, 1));
69 assert.ok(flightSortLayer(16, 1) > flightSortLayer(0, 1));
70 assert.ok(farInView < 900_000);
54});
71});
shared/render/pivot.js +8 −1
@@ -28,13 +28,20 @@ export function spriteDrawSize (spriteWidth, spriteHeight, zoom)
28 28 /**
29 29 * Within one rounded sortY, layers occupy [0, Y_SORT_SUBORDER_STRIDE).
30 30 * Keep door front / HP bar above entity body + equipment overlays.
31 *
32 * Callers should pass viewport-relative sortY (worldY − viewportOriginY) so
33 * values stay bounded; absolute world pixels grow with tileY×zoom and collide
34 * with UI bands like LAYER_EDGE.
31 35 */
32 36 export const Y_SORT_SUBORDER_STRIDE = 16;
33 37
38 /** Bias so northern (negative relative) sortY stays above LAYER_GROUND (0). */
39 export const Y_SORT_BASE = 100_000;
40
34 41 export function ySortLayer (sortY, subOrder)
35 42 {
36 43 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
37 return 10000 + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
44 return Y_SORT_BASE + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
38 45 }
39 46
40 47 /**
@@ -28,13 +28,20 @@ export function spriteDrawSize (spriteWidth, spriteHeight, zoom)
28/**
28/**
29 * Within one rounded sortY, layers occupy [0, Y_SORT_SUBORDER_STRIDE).
29 * Within one rounded sortY, layers occupy [0, Y_SORT_SUBORDER_STRIDE).
30 * Keep door front / HP bar above entity body + equipment overlays.
30 * Keep door front / HP bar above entity body + equipment overlays.
31 *
32 * Callers should pass viewport-relative sortY (worldY − viewportOriginY) so
33 * values stay bounded; absolute world pixels grow with tileY×zoom and collide
34 * with UI bands like LAYER_EDGE.
31 */
35 */
32export const Y_SORT_SUBORDER_STRIDE = 16;
36export const Y_SORT_SUBORDER_STRIDE = 16;
33
37
38/** Bias so northern (negative relative) sortY stays above LAYER_GROUND (0). */
39export const Y_SORT_BASE = 100_000;
40
34export function ySortLayer (sortY, subOrder)
41export function ySortLayer (sortY, subOrder)
35{
42{
36 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
43 const slot = Math.max(0, Math.min(Y_SORT_SUBORDER_STRIDE - 1, subOrder | 0));
37 return 10000 + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
44 return Y_SORT_BASE + Math.round(sortY) * Y_SORT_SUBORDER_STRIDE + slot;
38}
45}
39
46
40/**
47/**