BlueGit

timfa questward

Fix daylight shadow rims around solid tiles and water.

Occluding neighbours no longer pull lightmap corners toward zero (which carved daytime edges after Dark baked visible light into the map); water coast sync uses the same ambient=1 model.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
1e650102d4c0eb782a3f6833b207b23223e60166
Parent
71f29d06
3 files changed +92 −24
client/src/render/worldView.js +15 −24
@@ -2,6 +2,7 @@ import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT, DEFAULT_WORLD_SETTINGS, PIVOT_FROM_BO
2 2 import { dynamicLightAtTile, dynamicDarknessAtTile, lightIntensity } from "/shared/lighting/raycast.js";
3 3 import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";
4 4 import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";
5 import { smoothLitCorner } from "/shared/lighting/lightCorners.js";
5 6 import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";
6 7 import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
7 8 import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
@@ -2743,9 +2744,7 @@ export class WorldView
2743 2744 this.#syncLitSprite(objects.ground, objects.shadeAlpha ?? 0);
2744 2745 return;
2745 2746 }
2746 const fallbackLit = ambientDark > 0
2747 ? 1 - (objects.shadeAlpha ?? 0) / ambientDark
2748 : 1;
2747 const fallbackLit = 1 - (objects.shadeAlpha ?? 0);
2749 2748 const chunkX = Math.floor(objects.tileX / CHUNK_SIZE);
2750 2749 const chunkY = Math.floor(objects.tileY / CHUNK_SIZE);
2751 2750 const chunkBaked = baked ?? this.bakedChunks.get(`${chunkX},${chunkY}`);
@@ -3447,7 +3446,8 @@ export class WorldView
3447 3446 { continue; }
3448 3447 if (Math.abs(objects.chunkX - chunkX) > 1 || Math.abs(objects.chunkY - chunkY) > 1)
3449 3448 { continue; }
3450 this.#syncGroundWaterFx(objects, this.#ambientDarkness(), timeSec);
3449 // Light intensity grid is visible light (1 − shade); water shader uses ambient=1.
3450 this.#syncGroundWaterFx(objects, 1, timeSec);
3451 3451 }
3452 3452 }
3453 3453
@@ -3487,7 +3487,7 @@ export class WorldView
3487 3487 objects.ground.transform.size = new vector(size, size);
3488 3488 objects.ground.layer = LAYER_GROUND;
3489 3489 objects.groundTypeId = animatedGround.typeId;
3490 this.#syncGroundWaterFx(objects, this.#ambientDarkness(), performance.now() / 1000);
3490 this.#syncGroundWaterFx(objects, 1, performance.now() / 1000);
3491 3491 this.#markSortDirty();
3492 3492 }
3493 3493 if (animatedGround.bloom)
@@ -3977,25 +3977,16 @@ export class WorldView
3977 3977 {
3978 3978 const sample = (lx, ly) => grid[(ly + 1) * gridW + (lx + 1)];
3979 3979 const blocks = (lx, ly) => occlude[(ly + 1) * gridW + (lx + 1)] !== 0;
3980 const openSample = (lx, ly) => (blocks(lx, ly) ? 0 : sample(lx, ly));
3981 const smoothCorner = (homeLx, homeLy, aLx, aLy, bLx, bLy, dLx, dLy) =>
3982 {
3983 const home = sample(homeLx, homeLy);
3984 if (blocks(homeLx, homeLy))
3985 { return home; }
3986 let sum = home;
3987 let count = 1;
3988 sum += openSample(aLx, aLy);
3989 count += 1;
3990 sum += openSample(bLx, bLy);
3991 count += 1;
3992 if (!blocks(aLx, aLy) && !blocks(bLx, bLy))
3993 {
3994 sum += openSample(dLx, dLy);
3995 count += 1;
3996 }
3997 return sum / count;
3998 };
3980 const smoothCorner = (homeLx, homeLy, aLx, aLy, bLx, bLy, dLx, dLy) => smoothLitCorner({
3981 homeLit: sample(homeLx, homeLy),
3982 homeBlocks: blocks(homeLx, homeLy),
3983 aLit: sample(aLx, aLy),
3984 aBlocks: blocks(aLx, aLy),
3985 bLit: sample(bLx, bLy),
3986 bBlocks: blocks(bLx, bLy),
3987 dLit: sample(dLx, dLy),
3988 dBlocks: blocks(dLx, dLy)
3989 });
3999 3990 return { smoothCorner };
4000 3991 }
4001 3992
@@ -2,6 +2,7 @@ import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT, DEFAULT_WORLD_SETTINGS, PIVOT_FROM_BO
2import { dynamicLightAtTile, dynamicDarknessAtTile, lightIntensity } from "/shared/lighting/raycast.js";
2import { dynamicLightAtTile, dynamicDarknessAtTile, lightIntensity } from "/shared/lighting/raycast.js";
3import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";
3import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";
4import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";
4import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";
5import { smoothLitCorner } from "/shared/lighting/lightCorners.js";
5import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";
6import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";
6import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
7import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
7import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
8import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
@@ -2743,9 +2744,7 @@ export class WorldView
2743 this.#syncLitSprite(objects.ground, objects.shadeAlpha ?? 0);
2744 this.#syncLitSprite(objects.ground, objects.shadeAlpha ?? 0);
2744 return;
2745 return;
2745 }
2746 }
2746 const fallbackLit = ambientDark > 0
2747 const fallbackLit = 1 - (objects.shadeAlpha ?? 0);
2747 ? 1 - (objects.shadeAlpha ?? 0) / ambientDark
2748 : 1;
2749 const chunkX = Math.floor(objects.tileX / CHUNK_SIZE);
2748 const chunkX = Math.floor(objects.tileX / CHUNK_SIZE);
2750 const chunkY = Math.floor(objects.tileY / CHUNK_SIZE);
2749 const chunkY = Math.floor(objects.tileY / CHUNK_SIZE);
2751 const chunkBaked = baked ?? this.bakedChunks.get(`${chunkX},${chunkY}`);
2750 const chunkBaked = baked ?? this.bakedChunks.get(`${chunkX},${chunkY}`);
@@ -3447,7 +3446,8 @@ export class WorldView
3447 { continue; }
3446 { continue; }
3448 if (Math.abs(objects.chunkX - chunkX) > 1 || Math.abs(objects.chunkY - chunkY) > 1)
3447 if (Math.abs(objects.chunkX - chunkX) > 1 || Math.abs(objects.chunkY - chunkY) > 1)
3449 { continue; }
3448 { continue; }
3450 this.#syncGroundWaterFx(objects, this.#ambientDarkness(), timeSec);
3449 // Light intensity grid is visible light (1 − shade); water shader uses ambient=1.
3450 this.#syncGroundWaterFx(objects, 1, timeSec);
3451 }
3451 }
3452 }
3452 }
3453
3453
@@ -3487,7 +3487,7 @@ export class WorldView
3487 objects.ground.transform.size = new vector(size, size);
3487 objects.ground.transform.size = new vector(size, size);
3488 objects.ground.layer = LAYER_GROUND;
3488 objects.ground.layer = LAYER_GROUND;
3489 objects.groundTypeId = animatedGround.typeId;
3489 objects.groundTypeId = animatedGround.typeId;
3490 this.#syncGroundWaterFx(objects, this.#ambientDarkness(), performance.now() / 1000);
3490 this.#syncGroundWaterFx(objects, 1, performance.now() / 1000);
3491 this.#markSortDirty();
3491 this.#markSortDirty();
3492 }
3492 }
3493 if (animatedGround.bloom)
3493 if (animatedGround.bloom)
@@ -3977,25 +3977,16 @@ export class WorldView
3977 {
3977 {
3978 const sample = (lx, ly) => grid[(ly + 1) * gridW + (lx + 1)];
3978 const sample = (lx, ly) => grid[(ly + 1) * gridW + (lx + 1)];
3979 const blocks = (lx, ly) => occlude[(ly + 1) * gridW + (lx + 1)] !== 0;
3979 const blocks = (lx, ly) => occlude[(ly + 1) * gridW + (lx + 1)] !== 0;
3980 const openSample = (lx, ly) => (blocks(lx, ly) ? 0 : sample(lx, ly));
3980 const smoothCorner = (homeLx, homeLy, aLx, aLy, bLx, bLy, dLx, dLy) => smoothLitCorner({
3981 const smoothCorner = (homeLx, homeLy, aLx, aLy, bLx, bLy, dLx, dLy) =>
3981 homeLit: sample(homeLx, homeLy),
3982 {
3982 homeBlocks: blocks(homeLx, homeLy),
3983 const home = sample(homeLx, homeLy);
3983 aLit: sample(aLx, aLy),
3984 if (blocks(homeLx, homeLy))
3984 aBlocks: blocks(aLx, aLy),
3985 { return home; }
3985 bLit: sample(bLx, bLy),
3986 let sum = home;
3986 bBlocks: blocks(bLx, bLy),
3987 let count = 1;
3987 dLit: sample(dLx, dLy),
3988 sum += openSample(aLx, aLy);
3988 dBlocks: blocks(dLx, dLy)
3989 count += 1;
3989 });
3990 sum += openSample(bLx, bLy);
3991 count += 1;
3992 if (!blocks(aLx, aLy) && !blocks(bLx, bLy))
3993 {
3994 sum += openSample(dLx, dLy);
3995 count += 1;
3996 }
3997 return sum / count;
3998 };
3999 return { smoothCorner };
3990 return { smoothCorner };
4000 }
3991 }
4001
3992
server/tests/lighting.test.js +38 −0
@@ -180,3 +180,41 @@ test("encodeChunkBlob stores pre-baked occlusion aligned with chunkTileLightOccl
180 180 assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0));
181 181 assert.equal(decoded.occlusion[1], LightOcclusion.clear);
182 182 });
183
184 test("lit corner blend does not darken daylight beside occluding walls", async () =>
185 {
186 const { smoothLitCorner } = await import("../../shared/lighting/lightCorners.js");
187 const dayOpen = smoothLitCorner({
188 homeLit: 1,
189 homeBlocks: false,
190 aLit: 1,
191 aBlocks: false,
192 bLit: 0.2,
193 bBlocks: true,
194 dLit: 0,
195 dBlocks: true
196 });
197 assert.equal(dayOpen, 1);
198 const nightDarkBesideLitWall = smoothLitCorner({
199 homeLit: 0,
200 homeBlocks: false,
201 aLit: 0,
202 aBlocks: false,
203 bLit: 0.8,
204 bBlocks: true,
205 dLit: 0.5,
206 dBlocks: true
207 });
208 assert.equal(nightDarkBesideLitWall, 0);
209 const openBlend = smoothLitCorner({
210 homeLit: 1,
211 homeBlocks: false,
212 aLit: 0.5,
213 aBlocks: false,
214 bLit: 0.5,
215 bBlocks: false,
216 dLit: 0.25,
217 dBlocks: false
218 });
219 assert.equal(openBlend, (1 + 0.5 + 0.5 + 0.25) / 4);
220 });
@@ -180,3 +180,41 @@ test("encodeChunkBlob stores pre-baked occlusion aligned with chunkTileLightOccl
180 assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0));
180 assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0));
181 assert.equal(decoded.occlusion[1], LightOcclusion.clear);
181 assert.equal(decoded.occlusion[1], LightOcclusion.clear);
182});
182});
183
184test("lit corner blend does not darken daylight beside occluding walls", async () =>
185{
186 const { smoothLitCorner } = await import("../../shared/lighting/lightCorners.js");
187 const dayOpen = smoothLitCorner({
188 homeLit: 1,
189 homeBlocks: false,
190 aLit: 1,
191 aBlocks: false,
192 bLit: 0.2,
193 bBlocks: true,
194 dLit: 0,
195 dBlocks: true
196 });
197 assert.equal(dayOpen, 1);
198 const nightDarkBesideLitWall = smoothLitCorner({
199 homeLit: 0,
200 homeBlocks: false,
201 aLit: 0,
202 aBlocks: false,
203 bLit: 0.8,
204 bBlocks: true,
205 dLit: 0.5,
206 dBlocks: true
207 });
208 assert.equal(nightDarkBesideLitWall, 0);
209 const openBlend = smoothLitCorner({
210 homeLit: 1,
211 homeBlocks: false,
212 aLit: 0.5,
213 aBlocks: false,
214 bLit: 0.5,
215 bBlocks: false,
216 dLit: 0.25,
217 dBlocks: false
218 });
219 assert.equal(openBlend, (1 + 0.5 + 0.5 + 0.25) / 4);
220});
shared/lighting/lightCorners.js added +39 −0
@@ -0,0 +1,39 @@
1 /**
2 * Bilinear lightmap corner sample for one tile corner.
3 * Occluding neighbours reuse the home tile's lit value so they neither bleed
4 * torchlight onto the dark side of a wall nor carve daylight rims around solids.
5 *
6 * @param {{
7 * homeLit: number, homeBlocks: boolean,
8 * aLit: number, aBlocks: boolean,
9 * bLit: number, bBlocks: boolean,
10 * dLit: number, dBlocks: boolean
11 * }} samples Cardinal A/B and diagonal D around the home tile.
12 */
13 export function smoothLitCorner ({
14 homeLit,
15 homeBlocks,
16 aLit,
17 aBlocks,
18 bLit,
19 bBlocks,
20 dLit,
21 dBlocks
22 })
23 {
24 const home = Number(homeLit) || 0;
25 if (homeBlocks)
26 { return home; }
27 let sum = home;
28 let count = 1;
29 sum += aBlocks ? home : (Number(aLit) || 0);
30 count += 1;
31 sum += bBlocks ? home : (Number(bLit) || 0);
32 count += 1;
33 if (!aBlocks && !bBlocks)
34 {
35 sum += dBlocks ? home : (Number(dLit) || 0);
36 count += 1;
37 }
38 return sum / count;
39 }
@@ -0,0 +1,39 @@
1/**
2 * Bilinear lightmap corner sample for one tile corner.
3 * Occluding neighbours reuse the home tile's lit value so they neither bleed
4 * torchlight onto the dark side of a wall nor carve daylight rims around solids.
5 *
6 * @param {{
7 * homeLit: number, homeBlocks: boolean,
8 * aLit: number, aBlocks: boolean,
9 * bLit: number, bBlocks: boolean,
10 * dLit: number, dBlocks: boolean
11 * }} samples Cardinal A/B and diagonal D around the home tile.
12 */
13export function smoothLitCorner ({
14 homeLit,
15 homeBlocks,
16 aLit,
17 aBlocks,
18 bLit,
19 bBlocks,
20 dLit,
21 dBlocks
22})
23{
24 const home = Number(homeLit) || 0;
25 if (homeBlocks)
26 { return home; }
27 let sum = home;
28 let count = 1;
29 sum += aBlocks ? home : (Number(aLit) || 0);
30 count += 1;
31 sum += bBlocks ? home : (Number(bLit) || 0);
32 count += 1;
33 if (!aBlocks && !bBlocks)
34 {
35 sum += dBlocks ? home : (Number(dLit) || 0);
36 count += 1;
37 }
38 return sum / count;
39}