import assert from 'node:assert/strict' import { readFile } from 'node:fs/promises' import { enuToGltf, instanceGltf } from '../../../src/modules/cockpit/services/pipeInstanceGltf.js' import { Matrix4, Matrix3, Cartesian3 } from 'cesium' // Independent literal: installed Cesium default correction maps glTF [x,y,z] -> ENU [z,x,y]. const correction = Matrix4.fromRotationTranslation(Matrix3.fromArray([0,1,0,0,0,1,1,0,0])) const converted = new Cartesian3(...enuToGltf([11,23,37])) assert.deepEqual(Matrix4.multiplyByPoint(correction, converted, new Cartesian3()), new Cartesian3(11,23,37)) const template = JSON.parse(await readFile(new URL('../../../public/cockpit/models/pipes/spline.gltf', import.meta.url))) const doc = instanceGltf(template, [ { translation: [0,0,0], rotation: [0,0,0,1], scale: [.4,.4,10] }, { translation: [1,2,3], rotation: [0,0,0,1], scale: [.4,.4,20] }, ]) assert.equal(doc.meshes.length, 1) assert.equal(doc.meshes[0].primitives.length, 1) assert.equal(doc.buffers[0].uri, template.buffers[0].uri, 'shared template geometry not copied per instance') const attrs = doc.nodes[0].extensions.EXT_mesh_gpu_instancing.attributes assert.equal(doc.accessors[attrs.TRANSLATION].count, 2) assert.equal(doc.accessors[attrs.SCALE].count, 2) assert.ok(doc.extensionsRequired.includes('EXT_mesh_gpu_instancing')) assert.equal(template.nodes[0].extensions, undefined, 'shared template remains immutable') const data = Buffer.from(doc.buffers[1].uri.split(',')[1], 'base64') assert.equal(data.readFloatLE(12), 1) assert.equal(data.readFloatLE(20), 3) assert.equal(data.readFloatLE(80-4), 20) console.log('PASS two GPU instances reuse one immutable mesh and preserve literal TRS')