Fail fast on missing terrain assets and recover empty renders

This commit is contained in:
2026-10-04 22:32:11 +05:00
parent 79d0f53a79
commit 763cf8713c
15 changed files with 842 additions and 21 deletions

View File

@@ -0,0 +1,28 @@
import sharp from "sharp";
// Empty sky faces are valid when looking up. A surface map or the view straight
// down from a surface capture must contain terrain, even if other faces are sky.
export async function assertTerrainImage(bytes, label) {
const { data, info } = await sharp(bytes)
.resize(64, 64, { fit: "fill" })
.toColourspace("srgb")
.removeAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const sky = [0x95, 0xc4, 0xeb];
let terrainPixels = 0;
for (let i = 0; i < data.length; i += info.channels) {
if (
Math.abs(data[i] - sky[0]) > 12 ||
Math.abs(data[i + 1] - sky[1]) > 12 ||
Math.abs(data[i + 2] - sky[2]) > 12
)
terrainPixels++;
}
if (terrainPixels < 4) {
throw new Error(
`${label} contains only sky. Check Minecraft asset loading, capture coordinates and graphics errors; this render will not be submitted as a playable map.`,
);
}
return terrainPixels;
}