Files
DCCONSTRUCTIONS d0e5f90952 Preserve installed operator workspace before consolidation
Local preservation snapshot, not a new deployment or simulation acceptance.
2026-10-08 09:59:51 +03:00

72 lines
5.0 KiB
JavaScript

// Lossless Draco unpacking through the same vendored Google decoder as PlayCanvas.
// No reconstruction, simplification or frame change is performed here.
const fs = require('node:fs');
const path = require('node:path');
const vendor = path.resolve(__dirname, '../../apps/control-station/public/vendor/playcanvas/draco/1.5.7');
const vm = require('node:vm');
const decoderModule = {exports:{}};
vm.runInNewContext(fs.readFileSync(path.join(vendor,'draco_wasm_wrapper_gltf.js'),'utf8'),
{module:decoderModule,exports:decoderModule.exports,require,process,Buffer,console,
__dirname:vendor,__filename:path.join(vendor,'draco_wasm_wrapper_gltf.js')});
const factory = decoderModule.exports;
async function main() {
const input=fs.readFileSync(process.argv[2]);
if(input.readUInt32LE(0)!==0x46546c67 || input.readUInt32LE(4)!==2 || input.readUInt32LE(8)!==input.length) throw Error('Invalid GLB');
let document, binary;
for(let o=12;o<input.length;) {
const n=input.readUInt32LE(o),kind=input.readUInt32LE(o+4),bytes=input.subarray(o+8,o+8+n);
if(kind===0x4e4f534a)document=JSON.parse(bytes);if(kind===0x004e4942)binary=bytes;o+=8+n;
}
if(!document||!binary)throw Error('Missing GLB chunks');
if(document.nodes.some(n=>n.matrix||n.translation||n.rotation||n.scale))throw Error('Unbaked collider transform');
const module=await factory({wasmBinary:fs.readFileSync(path.join(vendor,'draco_decoder_gltf.wasm'))});
const decoder=new module.Decoder(), chunks=[],views=[],accessors=[],meshes=[];let offset=0;
function append(values,type,componentType) {
const bytes=Buffer.from(values.buffer,values.byteOffset,values.byteLength);
const index=accessors.length;
accessors.push({bufferView:views.length,componentType,count:values.length/(type==='VEC3'?3:1),type});
views.push({buffer:0,byteOffset:offset,byteLength:bytes.length});chunks.push(bytes);offset+=bytes.length;
return index;
}
function raw(index) {
const a=document.accessors[index],v=document.bufferViews[a.bufferView];
const C=a.componentType===5126?Float32Array:a.componentType===5125?Uint32Array:Uint16Array;
const width=a.type==='VEC3'?3:1,out=new C(a.count*width), stride=v.byteStride||C.BYTES_PER_ELEMENT*width;
for(let i=0;i<a.count;i++) for(let j=0;j<width;j++) {
const o=(v.byteOffset||0)+(a.byteOffset||0)+i*stride+j*C.BYTES_PER_ELEMENT;
out[i*width+j]=a.componentType===5126?binary.readFloatLE(o):a.componentType===5125?binary.readUInt32LE(o):binary.readUInt16LE(o);
}
return out;
}
try { for(const mesh of document.meshes) {
const primitives=[];
for(const p of mesh.primitives) {
if(p.mode!==undefined&&p.mode!==4)throw Error('Collider requires triangles');
const ext=p.extensions?.KHR_draco_mesh_compression;let positions,indices;
if(ext) {
const v=document.bufferViews[ext.bufferView],bytes=binary.subarray(v.byteOffset||0,(v.byteOffset||0)+v.byteLength);
const buffer=new module.DecoderBuffer(), decoded=new module.Mesh(), positionsArray=new module.DracoFloat32Array(), face=new module.DracoInt32Array();
try {
buffer.Init(new Int8Array(bytes),bytes.length);
const status=decoder.DecodeBufferToMesh(buffer,decoded);
if(!status.ok())throw Error(status.error_msg());
const attribute=decoder.GetAttributeByUniqueId(decoded,ext.attributes.POSITION);
if(!decoder.GetAttributeFloatForAllPoints(decoded,attribute,positionsArray))throw Error('Cannot decode position');
positions=new Float32Array(decoded.num_points()*3);
for(let i=0;i<positions.length;i++)positions[i]=positionsArray.GetValue(i);
indices=new Uint32Array(decoded.num_faces()*3);
for(let i=0;i<decoded.num_faces();i++){decoder.GetFaceFromMesh(decoded,i,face);for(let j=0;j<3;j++)indices[i*3+j]=face.GetValue(j);}
} finally {for(const o of [face,positionsArray,decoded,buffer])module.destroy(o);}
} else {positions=raw(p.attributes.POSITION);indices=new Uint32Array(raw(p.indices));}
primitives.push({attributes:{POSITION:append(positions,'VEC3',5126)},indices:append(indices,'SCALAR',5125),mode:4});
}
meshes.push({primitives});
}} finally {module.destroy(decoder);}
const output={asset:{version:'2.0',generator:'Mission Core / Google Draco 1.5.7'},scene:0,scenes:[{nodes:meshes.map((_,i)=>i)}],nodes:meshes.map((_,i)=>({mesh:i})),meshes,accessors,bufferViews:views,buffers:[{byteLength:offset}]};
const json=Buffer.from(JSON.stringify(output));const padding=Buffer.alloc((4-json.length%4)%4,32),body=Buffer.concat(chunks);
const header=Buffer.alloc(20);header.writeUInt32LE(0x46546c67);header.writeUInt32LE(2,4);header.writeUInt32LE(28+json.length+padding.length+body.length,8);header.writeUInt32LE(json.length+padding.length,12);header.writeUInt32LE(0x4e4f534a,16);
const binaryHeader=Buffer.alloc(8);binaryHeader.writeUInt32LE(body.length);binaryHeader.writeUInt32LE(0x004e4942,4);
fs.writeFileSync(process.argv[3],Buffer.concat([header,json,padding,binaryHeader,body]));
}
main().catch(e=>{console.error(e.message);process.exitCode=1;});