Files
osmWorkflow/.trellis/tasks/09-17-cesium-export-performance/research/compare-glb.cjs
T

75 lines
3.7 KiB
JavaScript

// Diagnostic: run from repository root with two export directory arguments.
// Strict 1e-6 comparison; known control noise is reported, never suppressed.
const fs = require('fs');
const path = require('path');
const assert = require('assert/strict');
const crypto = require('crypto');
const {digest} = require(path.join(process.cwd(), 'scripts/glb-digest'));
function glb(file) {
const data = fs.readFileSync(file);
let json, bin;
for (let i=12; i<data.length;) {
const len=data.readUInt32LE(i), type=data.readUInt32LE(i+4);
if(type===0x4e4f534a) json=JSON.parse(data.subarray(i+8,i+8+len));
if(type===0x004e4942) bin=data.subarray(i+8,i+8+len);
i+=8+len;
}
function accessor(id) {
const a=json.accessors[id];
assert(!a.sparse, 'sparse accessor unsupported');
const v=json.bufferViews[a.bufferView];
const n={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT4:16}[a.type];
const [bytes,fn]={5120:[1,'readInt8'],5121:[1,'readUInt8'],5122:[2,'readInt16LE'],5123:[2,'readUInt16LE'],5125:[4,'readUInt32LE'],5126:[4,'readFloatLE']}[a.componentType];
const out=[];
for(let i=0;i<a.count;i++) for(let j=0;j<n;j++) {
out.push(bin[fn]((v.byteOffset||0)+(a.byteOffset||0)+i*(v.byteStride||n*bytes)+j*bytes));
}
return out;
}
function imageHash(image) {
const v=json.bufferViews[image.bufferView];
return crypto.createHash('sha256').update(bin.subarray(v.byteOffset||0,(v.byteOffset||0)+v.byteLength)).digest('hex');
}
return {json, accessor, imageHash};
}
const [before,after]=process.argv.slice(2);
let failures=0;
for(const file of fs.readdirSync(before).filter(f=>f.endsWith('.glb')).sort()) {
const issues=[];
try {
const da=digest(path.join(before,file)), db=digest(path.join(after,file));
for(const d of [da,db]) {delete d.fileBytes;delete d.buffers;delete d.counts.accessors;}
try {assert.deepEqual(da,db);} catch {issues.push('structural digest differs');}
const a=glb(path.join(before,file)),b=glb(path.join(after,file));
for(const key of ['nodes','scenes','scene','materials','textures','samplers','animations','skins','extensions','extensionsUsed','extensionsRequired']) {
assert.deepEqual(a.json[key],b.json[key],key);
}
assert.equal(a.json.meshes.length,b.json.meshes.length);
let maxError=0;
for(let m=0;m<a.json.meshes.length;m++) {
const am=a.json.meshes[m],bm=b.json.meshes[m];
assert.equal(am.name,bm.name);
assert.equal(am.primitives.length,bm.primitives.length);
for(let p=0;p<am.primitives.length;p++) {
const ap=am.primitives[p],bp=bm.primitives[p];
assert.equal(ap.material,bp.material);
assert.deepEqual(Object.keys(ap.attributes),Object.keys(bp.attributes));
for(const key of ['indices',...Object.keys(ap.attributes)]) {
const av=a.accessor(key==='indices'?ap.indices:ap.attributes[key]);
const bv=b.accessor(key==='indices'?bp.indices:bp.attributes[key]);
if(av.length!==bv.length) {issues.push(`${am.name}/${p}/${key}: count ${av.length} -> ${bv.length}`);continue;}
let error=0;
for(let i=0;i<av.length;i++) error=Math.max(error,Math.abs(av[i]-bv[i]));
maxError=Math.max(maxError,error);
if(error>(key==='indices'?0:1e-6)) issues.push(`${am.name}/${p}/${key}: max delta ${error}`);
}
}
}
assert.equal(a.json.images?.length||0,b.json.images?.length||0);
for(let i=0;i<(a.json.images||[]).length;i++) assert.equal(a.imageHash(a.json.images[i]),b.imageHash(b.json.images[i]));
console.log(file, issues.length?'FAIL':'PASS', 'max attribute delta',maxError,issues.slice(0,12));
} catch(e) {issues.push(e.message);console.log(file,'ERROR',e.message.slice(0,500));}
if(issues.length) failures++;
}
process.exitCode=failures?1:0;