Fail fast on missing terrain assets and recover empty renders
This commit is contained in:
28
renderer/image-validation.mjs
Normal file
28
renderer/image-validation.mjs
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user