BlueGit

timfa questward

Fix soft night shading on animated water tiles.

Water sprites now bilinearly interpolate the same per-tile corner lit values as baked ground atlases, instead of sampling the chunk lightmap texture which did not bind reliably on individual sprites.

timfa Tim Falken · pushed by timfa

Authored 2026-08-25 · 2 months ago

Hash
59c6bf2e0ba895f1da0bf0fa14e64972985281f8
Parent
e42fc282
2 files changed +101 −67
client/src/render/worldView.js +100 −66
@@ -60,12 +60,11 @@ const WATER_GROUND_FRAG = [
60 60 "precision mediump float;",
61 61 "varying vec2 v_texCoord;",
62 62 "uniform sampler2D u_texture;",
63 "uniform sampler2D u_secondary;",
64 63 "uniform float u_ambientDark;",
65 "uniform float u_lightTileX;",
66 "uniform float u_lightTileY;",
67 "uniform float u_useLightmap;",
68 "uniform float u_fallbackLit;",
64 "uniform float u_litNW;",
65 "uniform float u_litNE;",
66 "uniform float u_litSW;",
67 "uniform float u_litSE;",
69 68 "uniform float u_time;",
70 69 "uniform float u_edgeN;",
71 70 "uniform float u_edgeE;",
@@ -90,11 +89,9 @@ const WATER_GROUND_FRAG = [
90 89 " foam = max(foam, ((v_texCoord.x - 0.78) / 0.22) * pulse);",
91 90 " }",
92 91 " rgb = mix(rgb, vec3(0.78, 0.92, 1.0), clamp(foam, 0.0, 0.85));",
93 " float lit = u_fallbackLit;",
94 " if (u_useLightmap > 0.5) {",
95 " vec2 lightUV = vec2(u_lightTileX + v_texCoord.x / 16.0, u_lightTileY + v_texCoord.y / 16.0);",
96 " lit = texture2D(u_secondary, lightUV).r;",
97 " }",
92 " float litTop = mix(u_litNW, u_litNE, v_texCoord.x);",
93 " float litBottom = mix(u_litSW, u_litSE, v_texCoord.x);",
94 " float lit = mix(litTop, litBottom, v_texCoord.y);",
98 95 " float darkness = u_ambientDark * (1.0 - lit);",
99 96 " rgb = mix(rgb, vec3(0.0), darkness);",
100 97 " gl_FragColor = vec4(rgb, color.a);",
@@ -1200,25 +1197,26 @@ export class WorldView
1200 1197 };
1201 1198 }
1202 1199
1203 #syncWaterGroundSprite (object, tileX, tileY, ambientDark, edges, timeSec, fallbackLit = 1)
1200 #syncWaterGroundSprite (object, corners, ambientDark, edges, timeSec, fallbackLit = 1)
1204 1201 {
1205 1202 if (!object?.renderer)
1206 1203 { return; }
1207 const chunkX = Math.floor(tileX / CHUNK_SIZE);
1208 const chunkY = Math.floor(tileY / CHUNK_SIZE);
1209 const baked = this.bakedChunks.get(`${chunkX},${chunkY}`);
1210 const localX = tileX - chunkX * CHUNK_SIZE;
1211 const localY = tileY - chunkY * CHUNK_SIZE;
1204 const lit = corners ?? {
1205 nw: fallbackLit,
1206 ne: fallbackLit,
1207 sw: fallbackLit,
1208 se: fallbackLit
1209 };
1212 1210 const dark = Math.min(1, Math.max(0, ambientDark ?? 0));
1213 1211 object._lit = true;
1214 1212 object.renderer.shader = getWaterGroundShader();
1215 object.renderer.secondaryMap = baked?.lightmapImage ?? null;
1213 object.renderer.secondaryMap = null;
1216 1214 object.renderer.shaderUniforms = {
1217 1215 u_ambientDark: dark,
1218 u_lightTileX: localX / CHUNK_SIZE,
1219 u_lightTileY: localY / CHUNK_SIZE,
1220 u_useLightmap: baked?.lightmapImage ? 1 : 0,
1221 u_fallbackLit: Math.min(1, Math.max(0, fallbackLit ?? 1)),
1216 u_litNW: lit.nw,
1217 u_litNE: lit.ne,
1218 u_litSW: lit.sw,
1219 u_litSE: lit.se,
1222 1220 u_time: timeSec,
1223 1221 u_edgeN: edges?.n ?? 0,
1224 1222 u_edgeE: edges?.e ?? 0,
@@ -1228,7 +1226,7 @@ export class WorldView
1228 1226 }
1229 1227
1230 1228 /** Liquid water gets coast foam; frozen shallow ice uses the normal lit sprite. */
1231 #syncGroundWaterFx (objects, ambientDark, timeSec)
1229 #syncGroundWaterFx (objects, ambientDark, timeSec, baked = null)
1232 1230 {
1233 1231 if (!objects?.ground)
1234 1232 { return; }
@@ -1241,11 +1239,18 @@ export class WorldView
1241 1239 const fallbackLit = ambientDark > 0
1242 1240 ? 1 - (objects.shadeAlpha ?? 0) / ambientDark
1243 1241 : 1;
1242 const chunkX = Math.floor(objects.tileX / CHUNK_SIZE);
1243 const chunkY = Math.floor(objects.tileY / CHUNK_SIZE);
1244 const chunkBaked = baked ?? this.bakedChunks.get(`${chunkX},${chunkY}`);
1245 const localX = objects.tileX - chunkX * CHUNK_SIZE;
1246 const localY = objects.tileY - chunkY * CHUNK_SIZE;
1247 const corners = chunkBaked
1248 ? this.#tileLitCorners(chunkBaked, localX, localY)
1249 : null;
1244 1250 objects.waterEdges = this.#waterCoastEdges(objects.tileX, objects.tileY);
1245 1251 this.#syncWaterGroundSprite(
1246 1252 objects.ground,
1247 objects.tileX,
1248 objects.tileY,
1253 corners,
1249 1254 ambientDark,
1250 1255 objects.waterEdges,
1251 1256 timeSec,
@@ -1780,7 +1785,10 @@ export class WorldView
1780 1785 if (objects.block)
1781 1786 { this.#syncLitSprite(objects.block, objects.shadeAlpha); }
1782 1787 if (objects.ground && isWaterGround(objects.groundTypeId))
1783 { this.#syncGroundWaterFx(objects, ambientDark, timeSec); }
1788 {
1789 const baked = this.bakedChunks.get(`${objects.chunkX},${objects.chunkY}`);
1790 this.#syncGroundWaterFx(objects, ambientDark, timeSec, baked);
1791 }
1784 1792 else if (objects.ground)
1785 1793 { this.#syncLitSprite(objects.ground, objects.shadeAlpha); }
1786 1794 }
@@ -1842,22 +1850,8 @@ export class WorldView
1842 1850 return isBlockOccludingLight(snapshot.blocks?.[index]?.typeId ?? null);
1843 1851 }
1844 1852
1845 /**
1846 * Per-chunk lightmap for atlas ground/blocks. Open tiles blend into open neighbours;
1847 * occluding tiles stay flat and contribute 0 into others so light does not bleed
1848 * through a wall to the far side. Diagonals only when both cardinals are clear.
1849 */
1850 #writeChunkLightmap (baked, activeLights, lightOcclusion)
1853 #fillChunkLightGrids (baked, activeLights, lightOcclusion)
1851 1854 {
1852 const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
1853 if (!snapshot)
1854 { return; }
1855 const atlasPx = CHUNK_SIZE * SPRITE_SIZE_PX;
1856 const pixelCount = atlasPx * atlasPx * 4;
1857 if (!baked.lightmapPixels || baked.lightmapPixels.length !== pixelCount)
1858 { baked.lightmapPixels = new Uint8ClampedArray(pixelCount); }
1859 const pixels = baked.lightmapPixels;
1860
1861 1855 const gridW = CHUNK_SIZE + 2;
1862 1856 const gridSize = gridW * gridW;
1863 1857 if (!baked.lightIntensityGrid || baked.lightIntensityGrid.length !== gridSize)
@@ -1877,12 +1871,14 @@ export class WorldView
1877 1871 occlude[gi] = this.#tileOccludesLight(tileX, tileY) ? 1 : 0;
1878 1872 }
1879 1873 }
1874 return { grid, occlude, gridW };
1875 }
1880 1876
1877 #makeLightCornerSampler (grid, occlude, gridW)
1878 {
1881 1879 const sample = (lx, ly) => grid[(ly + 1) * gridW + (lx + 1)];
1882 1880 const blocks = (lx, ly) => occlude[(ly + 1) * gridW + (lx + 1)] !== 0;
1883 /** Walls add 0 to open neighbours — not their facing-side lit value (that caused bleed). */
1884 1881 const openSample = (lx, ly) => (blocks(lx, ly) ? 0 : sample(lx, ly));
1885
1886 1882 const smoothCorner = (homeLx, homeLy, aLx, aLy, bLx, bLy, dLx, dLy) =>
1887 1883 {
1888 1884 const home = sample(homeLx, homeLy);
@@ -1901,35 +1897,73 @@ export class WorldView
1901 1897 }
1902 1898 return sum / count;
1903 1899 };
1900 return { smoothCorner };
1901 }
1902
1903 /** Corner lit intensities (0–1) for one tile — matches baked atlas lightmap corners. */
1904 #tileLitCorners (baked, localX, localY)
1905 {
1906 const grid = baked?.lightIntensityGrid;
1907 const occlude = baked?.lightOccludeGrid;
1908 if (!grid?.length || !occlude?.length)
1909 { return null; }
1910 const gridW = CHUNK_SIZE + 2;
1911 const { smoothCorner } = this.#makeLightCornerSampler(grid, occlude, gridW);
1912 return {
1913 nw: smoothCorner(
1914 localX, localY,
1915 localX - 1, localY,
1916 localX, localY - 1,
1917 localX - 1, localY - 1
1918 ),
1919 ne: smoothCorner(
1920 localX, localY,
1921 localX + 1, localY,
1922 localX, localY - 1,
1923 localX + 1, localY - 1
1924 ),
1925 sw: smoothCorner(
1926 localX, localY,
1927 localX - 1, localY,
1928 localX, localY + 1,
1929 localX - 1, localY + 1
1930 ),
1931 se: smoothCorner(
1932 localX, localY,
1933 localX + 1, localY,
1934 localX, localY + 1,
1935 localX + 1, localY + 1
1936 )
1937 };
1938 }
1939
1940 /**
1941 * Per-chunk lightmap for atlas ground/blocks. Open tiles blend into open neighbours;
1942 * occluding tiles stay flat and contribute 0 into others so light does not bleed
1943 * through a wall to the far side. Diagonals only when both cardinals are clear.
1944 */
1945 #writeChunkLightmap (baked, activeLights, lightOcclusion)
1946 {
1947 const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
1948 if (!snapshot)
1949 { return; }
1950 const atlasPx = CHUNK_SIZE * SPRITE_SIZE_PX;
1951 const pixelCount = atlasPx * atlasPx * 4;
1952 if (!baked.lightmapPixels || baked.lightmapPixels.length !== pixelCount)
1953 { baked.lightmapPixels = new Uint8ClampedArray(pixelCount); }
1954 const pixels = baked.lightmapPixels;
1955
1956 this.#fillChunkLightGrids(baked, activeLights, lightOcclusion);
1904 1957
1905 1958 for (let localY = 0; localY < CHUNK_SIZE; localY++)
1906 1959 {
1907 1960 for (let localX = 0; localX < CHUNK_SIZE; localX++)
1908 1961 {
1909 const nw = smoothCorner(
1910 localX, localY,
1911 localX - 1, localY,
1912 localX, localY - 1,
1913 localX - 1, localY - 1
1914 );
1915 const ne = smoothCorner(
1916 localX, localY,
1917 localX + 1, localY,
1918 localX, localY - 1,
1919 localX + 1, localY - 1
1920 );
1921 const sw = smoothCorner(
1922 localX, localY,
1923 localX - 1, localY,
1924 localX, localY + 1,
1925 localX - 1, localY + 1
1926 );
1927 const se = smoothCorner(
1928 localX, localY,
1929 localX + 1, localY,
1930 localX, localY + 1,
1931 localX + 1, localY + 1
1932 );
1962 const corners = this.#tileLitCorners(baked, localX, localY);
1963 const nw = corners.nw;
1964 const ne = corners.ne;
1965 const sw = corners.sw;
1966 const se = corners.se;
1933 1967 const baseX = localX * SPRITE_SIZE_PX;
1934 1968 const baseY = localY * SPRITE_SIZE_PX;
1935 1969 for (let py = 0; py < SPRITE_SIZE_PX; py++)
@@ -60,12 +60,11 @@ const WATER_GROUND_FRAG = [
60 "precision mediump float;",
60 "precision mediump float;",
61 "varying vec2 v_texCoord;",
61 "varying vec2 v_texCoord;",
62 "uniform sampler2D u_texture;",
62 "uniform sampler2D u_texture;",
63 "uniform sampler2D u_secondary;",
64 "uniform float u_ambientDark;",
63 "uniform float u_ambientDark;",
65 "uniform float u_lightTileX;",
64 "uniform float u_litNW;",
66 "uniform float u_lightTileY;",
65 "uniform float u_litNE;",
67 "uniform float u_useLightmap;",
66 "uniform float u_litSW;",
68 "uniform float u_fallbackLit;",
67 "uniform float u_litSE;",
69 "uniform float u_time;",
68 "uniform float u_time;",
70 "uniform float u_edgeN;",
69 "uniform float u_edgeN;",
71 "uniform float u_edgeE;",
70 "uniform float u_edgeE;",
@@ -90,11 +89,9 @@ const WATER_GROUND_FRAG = [
90 " foam = max(foam, ((v_texCoord.x - 0.78) / 0.22) * pulse);",
89 " foam = max(foam, ((v_texCoord.x - 0.78) / 0.22) * pulse);",
91 " }",
90 " }",
92 " rgb = mix(rgb, vec3(0.78, 0.92, 1.0), clamp(foam, 0.0, 0.85));",
91 " rgb = mix(rgb, vec3(0.78, 0.92, 1.0), clamp(foam, 0.0, 0.85));",
93 " float lit = u_fallbackLit;",
92 " float litTop = mix(u_litNW, u_litNE, v_texCoord.x);",
94 " if (u_useLightmap > 0.5) {",
93 " float litBottom = mix(u_litSW, u_litSE, v_texCoord.x);",
95 " vec2 lightUV = vec2(u_lightTileX + v_texCoord.x / 16.0, u_lightTileY + v_texCoord.y / 16.0);",
94 " float lit = mix(litTop, litBottom, v_texCoord.y);",
96 " lit = texture2D(u_secondary, lightUV).r;",
97 " }",
98 " float darkness = u_ambientDark * (1.0 - lit);",
95 " float darkness = u_ambientDark * (1.0 - lit);",
99 " rgb = mix(rgb, vec3(0.0), darkness);",
96 " rgb = mix(rgb, vec3(0.0), darkness);",
100 " gl_FragColor = vec4(rgb, color.a);",
97 " gl_FragColor = vec4(rgb, color.a);",
@@ -1200,25 +1197,26 @@ export class WorldView
1200 };
1197 };
1201 }
1198 }
1202
1199
1203 #syncWaterGroundSprite (object, tileX, tileY, ambientDark, edges, timeSec, fallbackLit = 1)
1200 #syncWaterGroundSprite (object, corners, ambientDark, edges, timeSec, fallbackLit = 1)
1204 {
1201 {
1205 if (!object?.renderer)
1202 if (!object?.renderer)
1206 { return; }
1203 { return; }
1207 const chunkX = Math.floor(tileX / CHUNK_SIZE);
1204 const lit = corners ?? {
1208 const chunkY = Math.floor(tileY / CHUNK_SIZE);
1205 nw: fallbackLit,
1209 const baked = this.bakedChunks.get(`${chunkX},${chunkY}`);
1206 ne: fallbackLit,
1210 const localX = tileX - chunkX * CHUNK_SIZE;
1207 sw: fallbackLit,
1211 const localY = tileY - chunkY * CHUNK_SIZE;
1208 se: fallbackLit
1209 };
1212 const dark = Math.min(1, Math.max(0, ambientDark ?? 0));
1210 const dark = Math.min(1, Math.max(0, ambientDark ?? 0));
1213 object._lit = true;
1211 object._lit = true;
1214 object.renderer.shader = getWaterGroundShader();
1212 object.renderer.shader = getWaterGroundShader();
1215 object.renderer.secondaryMap = baked?.lightmapImage ?? null;
1213 object.renderer.secondaryMap = null;
1216 object.renderer.shaderUniforms = {
1214 object.renderer.shaderUniforms = {
1217 u_ambientDark: dark,
1215 u_ambientDark: dark,
1218 u_lightTileX: localX / CHUNK_SIZE,
1216 u_litNW: lit.nw,
1219 u_lightTileY: localY / CHUNK_SIZE,
1217 u_litNE: lit.ne,
1220 u_useLightmap: baked?.lightmapImage ? 1 : 0,
1218 u_litSW: lit.sw,
1221 u_fallbackLit: Math.min(1, Math.max(0, fallbackLit ?? 1)),
1219 u_litSE: lit.se,
1222 u_time: timeSec,
1220 u_time: timeSec,
1223 u_edgeN: edges?.n ?? 0,
1221 u_edgeN: edges?.n ?? 0,
1224 u_edgeE: edges?.e ?? 0,
1222 u_edgeE: edges?.e ?? 0,
@@ -1228,7 +1226,7 @@ export class WorldView
1228 }
1226 }
1229
1227
1230 /** Liquid water gets coast foam; frozen shallow ice uses the normal lit sprite. */
1228 /** Liquid water gets coast foam; frozen shallow ice uses the normal lit sprite. */
1231 #syncGroundWaterFx (objects, ambientDark, timeSec)
1229 #syncGroundWaterFx (objects, ambientDark, timeSec, baked = null)
1232 {
1230 {
1233 if (!objects?.ground)
1231 if (!objects?.ground)
1234 { return; }
1232 { return; }
@@ -1241,11 +1239,18 @@ export class WorldView
1241 const fallbackLit = ambientDark > 0
1239 const fallbackLit = ambientDark > 0
1242 ? 1 - (objects.shadeAlpha ?? 0) / ambientDark
1240 ? 1 - (objects.shadeAlpha ?? 0) / ambientDark
1243 : 1;
1241 : 1;
1242 const chunkX = Math.floor(objects.tileX / CHUNK_SIZE);
1243 const chunkY = Math.floor(objects.tileY / CHUNK_SIZE);
1244 const chunkBaked = baked ?? this.bakedChunks.get(`${chunkX},${chunkY}`);
1245 const localX = objects.tileX - chunkX * CHUNK_SIZE;
1246 const localY = objects.tileY - chunkY * CHUNK_SIZE;
1247 const corners = chunkBaked
1248 ? this.#tileLitCorners(chunkBaked, localX, localY)
1249 : null;
1244 objects.waterEdges = this.#waterCoastEdges(objects.tileX, objects.tileY);
1250 objects.waterEdges = this.#waterCoastEdges(objects.tileX, objects.tileY);
1245 this.#syncWaterGroundSprite(
1251 this.#syncWaterGroundSprite(
1246 objects.ground,
1252 objects.ground,
1247 objects.tileX,
1253 corners,
1248 objects.tileY,
1249 ambientDark,
1254 ambientDark,
1250 objects.waterEdges,
1255 objects.waterEdges,
1251 timeSec,
1256 timeSec,
@@ -1780,7 +1785,10 @@ export class WorldView
1780 if (objects.block)
1785 if (objects.block)
1781 { this.#syncLitSprite(objects.block, objects.shadeAlpha); }
1786 { this.#syncLitSprite(objects.block, objects.shadeAlpha); }
1782 if (objects.ground && isWaterGround(objects.groundTypeId))
1787 if (objects.ground && isWaterGround(objects.groundTypeId))
1783 { this.#syncGroundWaterFx(objects, ambientDark, timeSec); }
1788 {
1789 const baked = this.bakedChunks.get(`${objects.chunkX},${objects.chunkY}`);
1790 this.#syncGroundWaterFx(objects, ambientDark, timeSec, baked);
1791 }
1784 else if (objects.ground)
1792 else if (objects.ground)
1785 { this.#syncLitSprite(objects.ground, objects.shadeAlpha); }
1793 { this.#syncLitSprite(objects.ground, objects.shadeAlpha); }
1786 }
1794 }
@@ -1842,22 +1850,8 @@ export class WorldView
1842 return isBlockOccludingLight(snapshot.blocks?.[index]?.typeId ?? null);
1850 return isBlockOccludingLight(snapshot.blocks?.[index]?.typeId ?? null);
1843 }
1851 }
1844
1852
1845 /**
1853 #fillChunkLightGrids (baked, activeLights, lightOcclusion)
1846 * Per-chunk lightmap for atlas ground/blocks. Open tiles blend into open neighbours;
1847 * occluding tiles stay flat and contribute 0 into others so light does not bleed
1848 * through a wall to the far side. Diagonals only when both cardinals are clear.
1849 */
1850 #writeChunkLightmap (baked, activeLights, lightOcclusion)
1851 {
1854 {
1852 const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
1853 if (!snapshot)
1854 { return; }
1855 const atlasPx = CHUNK_SIZE * SPRITE_SIZE_PX;
1856 const pixelCount = atlasPx * atlasPx * 4;
1857 if (!baked.lightmapPixels || baked.lightmapPixels.length !== pixelCount)
1858 { baked.lightmapPixels = new Uint8ClampedArray(pixelCount); }
1859 const pixels = baked.lightmapPixels;
1860
1861 const gridW = CHUNK_SIZE + 2;
1855 const gridW = CHUNK_SIZE + 2;
1862 const gridSize = gridW * gridW;
1856 const gridSize = gridW * gridW;
1863 if (!baked.lightIntensityGrid || baked.lightIntensityGrid.length !== gridSize)
1857 if (!baked.lightIntensityGrid || baked.lightIntensityGrid.length !== gridSize)
@@ -1877,12 +1871,14 @@ export class WorldView
1877 occlude[gi] = this.#tileOccludesLight(tileX, tileY) ? 1 : 0;
1871 occlude[gi] = this.#tileOccludesLight(tileX, tileY) ? 1 : 0;
1878 }
1872 }
1879 }
1873 }
1874 return { grid, occlude, gridW };
1875 }
1880
1876
1877 #makeLightCornerSampler (grid, occlude, gridW)
1878 {
1881 const sample = (lx, ly) => grid[(ly + 1) * gridW + (lx + 1)];
1879 const sample = (lx, ly) => grid[(ly + 1) * gridW + (lx + 1)];
1882 const blocks = (lx, ly) => occlude[(ly + 1) * gridW + (lx + 1)] !== 0;
1880 const blocks = (lx, ly) => occlude[(ly + 1) * gridW + (lx + 1)] !== 0;
1883 /** Walls add 0 to open neighbours — not their facing-side lit value (that caused bleed). */
1884 const openSample = (lx, ly) => (blocks(lx, ly) ? 0 : sample(lx, ly));
1881 const openSample = (lx, ly) => (blocks(lx, ly) ? 0 : sample(lx, ly));
1885
1886 const smoothCorner = (homeLx, homeLy, aLx, aLy, bLx, bLy, dLx, dLy) =>
1882 const smoothCorner = (homeLx, homeLy, aLx, aLy, bLx, bLy, dLx, dLy) =>
1887 {
1883 {
1888 const home = sample(homeLx, homeLy);
1884 const home = sample(homeLx, homeLy);
@@ -1901,35 +1897,73 @@ export class WorldView
1901 }
1897 }
1902 return sum / count;
1898 return sum / count;
1903 };
1899 };
1900 return { smoothCorner };
1901 }
1902
1903 /** Corner lit intensities (0–1) for one tile — matches baked atlas lightmap corners. */
1904 #tileLitCorners (baked, localX, localY)
1905 {
1906 const grid = baked?.lightIntensityGrid;
1907 const occlude = baked?.lightOccludeGrid;
1908 if (!grid?.length || !occlude?.length)
1909 { return null; }
1910 const gridW = CHUNK_SIZE + 2;
1911 const { smoothCorner } = this.#makeLightCornerSampler(grid, occlude, gridW);
1912 return {
1913 nw: smoothCorner(
1914 localX, localY,
1915 localX - 1, localY,
1916 localX, localY - 1,
1917 localX - 1, localY - 1
1918 ),
1919 ne: smoothCorner(
1920 localX, localY,
1921 localX + 1, localY,
1922 localX, localY - 1,
1923 localX + 1, localY - 1
1924 ),
1925 sw: smoothCorner(
1926 localX, localY,
1927 localX - 1, localY,
1928 localX, localY + 1,
1929 localX - 1, localY + 1
1930 ),
1931 se: smoothCorner(
1932 localX, localY,
1933 localX + 1, localY,
1934 localX, localY + 1,
1935 localX + 1, localY + 1
1936 )
1937 };
1938 }
1939
1940 /**
1941 * Per-chunk lightmap for atlas ground/blocks. Open tiles blend into open neighbours;
1942 * occluding tiles stay flat and contribute 0 into others so light does not bleed
1943 * through a wall to the far side. Diagonals only when both cardinals are clear.
1944 */
1945 #writeChunkLightmap (baked, activeLights, lightOcclusion)
1946 {
1947 const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
1948 if (!snapshot)
1949 { return; }
1950 const atlasPx = CHUNK_SIZE * SPRITE_SIZE_PX;
1951 const pixelCount = atlasPx * atlasPx * 4;
1952 if (!baked.lightmapPixels || baked.lightmapPixels.length !== pixelCount)
1953 { baked.lightmapPixels = new Uint8ClampedArray(pixelCount); }
1954 const pixels = baked.lightmapPixels;
1955
1956 this.#fillChunkLightGrids(baked, activeLights, lightOcclusion);
1904
1957
1905 for (let localY = 0; localY < CHUNK_SIZE; localY++)
1958 for (let localY = 0; localY < CHUNK_SIZE; localY++)
1906 {
1959 {
1907 for (let localX = 0; localX < CHUNK_SIZE; localX++)
1960 for (let localX = 0; localX < CHUNK_SIZE; localX++)
1908 {
1961 {
1909 const nw = smoothCorner(
1962 const corners = this.#tileLitCorners(baked, localX, localY);
1910 localX, localY,
1963 const nw = corners.nw;
1911 localX - 1, localY,
1964 const ne = corners.ne;
1912 localX, localY - 1,
1965 const sw = corners.sw;
1913 localX - 1, localY - 1
1966 const se = corners.se;
1914 );
1915 const ne = smoothCorner(
1916 localX, localY,
1917 localX + 1, localY,
1918 localX, localY - 1,
1919 localX + 1, localY - 1
1920 );
1921 const sw = smoothCorner(
1922 localX, localY,
1923 localX - 1, localY,
1924 localX, localY + 1,
1925 localX - 1, localY + 1
1926 );
1927 const se = smoothCorner(
1928 localX, localY,
1929 localX + 1, localY,
1930 localX, localY + 1,
1931 localX + 1, localY + 1
1932 );
1933 const baseX = localX * SPRITE_SIZE_PX;
1967 const baseX = localX * SPRITE_SIZE_PX;
1934 const baseY = localY * SPRITE_SIZE_PX;
1968 const baseY = localY * SPRITE_SIZE_PX;
1935 for (let py = 0; py < SPRITE_SIZE_PX; py++)
1969 for (let py = 0; py < SPRITE_SIZE_PX; py++)
docs/FEATURES.md +1 −1
@@ -136,7 +136,7 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
136 136 ## Night shading
137 137
138 138 **Status:** full
139 **Spec:** World time is server time minus 1200 years. At night, unlit tiles are darkened by `nightDarkness` (a live world setting) and reachable tiles use the baked 0–255 intensity so a torch fades tile-by-tile instead of a hard circle. Baked ground/block atlases and **animated liquid water** sample a per-chunk lightmap with neighbour blending; **light-occluding tiles** (`blocksLight` / impassable ground) stay flat and contribute **0** into open neighbours (and diagonals need both cardinals clear), so a torch beside a wall does not soft-light the far side. Oversized/animated blocks and mobiles still use a uniform darkness for their standing tile (entities lerp shade while moving). `worldTime` also carries the real **server-local calendar season** (`summer` / `fall` / `winter`) for seasonal tile visuals.
139 **Spec:** World time is server time minus 1200 years. At night, unlit tiles are darkened by `nightDarkness` (a live world setting) and reachable tiles use the baked 0–255 intensity so a torch fades tile-by-tile instead of a hard circle. Baked ground/block atlases sample a per-chunk lightmap with neighbour blending; **animated liquid water** uses the same corner-interpolated lit values via shader uniforms (not a flat per-tile shade). **Light-occluding tiles** (`blocksLight` / impassable ground) stay flat and contribute **0** into open neighbours (and diagonals need both cardinals clear), so a torch beside a wall does not soft-light the far side. Oversized/animated blocks and mobiles still use a uniform darkness for their standing tile (entities lerp shade while moving). `worldTime` also carries the real **server-local calendar season** (`summer` / `fall` / `winter`) for seasonal tile visuals.
140 140 **Code:** `client/src/render/worldView.js` (`nightFactor`, `#updateShade`, `#writeChunkLightmap`, `#placeSpriteShade`, `setWorldSeason`), `server/src/simulation/simulation.js` (`worldTime`), `shared/world/season.js`
141 141
142 142 ## FrameworkV3 client bootstrap
@@ -136,7 +136,7 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
136## Night shading
136## Night shading
137
137
138**Status:** full
138**Status:** full
139**Spec:** World time is server time minus 1200 years. At night, unlit tiles are darkened by `nightDarkness` (a live world setting) and reachable tiles use the baked 0–255 intensity so a torch fades tile-by-tile instead of a hard circle. Baked ground/block atlases and **animated liquid water** sample a per-chunk lightmap with neighbour blending; **light-occluding tiles** (`blocksLight` / impassable ground) stay flat and contribute **0** into open neighbours (and diagonals need both cardinals clear), so a torch beside a wall does not soft-light the far side. Oversized/animated blocks and mobiles still use a uniform darkness for their standing tile (entities lerp shade while moving). `worldTime` also carries the real **server-local calendar season** (`summer` / `fall` / `winter`) for seasonal tile visuals.
139**Spec:** World time is server time minus 1200 years. At night, unlit tiles are darkened by `nightDarkness` (a live world setting) and reachable tiles use the baked 0–255 intensity so a torch fades tile-by-tile instead of a hard circle. Baked ground/block atlases sample a per-chunk lightmap with neighbour blending; **animated liquid water** uses the same corner-interpolated lit values via shader uniforms (not a flat per-tile shade). **Light-occluding tiles** (`blocksLight` / impassable ground) stay flat and contribute **0** into open neighbours (and diagonals need both cardinals clear), so a torch beside a wall does not soft-light the far side. Oversized/animated blocks and mobiles still use a uniform darkness for their standing tile (entities lerp shade while moving). `worldTime` also carries the real **server-local calendar season** (`summer` / `fall` / `winter`) for seasonal tile visuals.
140**Code:** `client/src/render/worldView.js` (`nightFactor`, `#updateShade`, `#writeChunkLightmap`, `#placeSpriteShade`, `setWorldSeason`), `server/src/simulation/simulation.js` (`worldTime`), `shared/world/season.js`
140**Code:** `client/src/render/worldView.js` (`nightFactor`, `#updateShade`, `#writeChunkLightmap`, `#placeSpriteShade`, `setWorldSeason`), `server/src/simulation/simulation.js` (`worldTime`), `shared/world/season.js`
141
141
142## FrameworkV3 client bootstrap
142## FrameworkV3 client bootstrap