UNPKG

xatlas-wasi

Version:
439 lines 16.5 kB
/** * Class for managing WebAssembly context and memory operations. */ export class WasmContext { /** * Creates an instance of WasmContext. * @param exports - The WebAssembly exports object. */ constructor(exports) { this.exports = exports; this.reload(); } /** * Reloads the memory views. * Call this method manually in case of memory growth. */ reload() { this.memU8 = new Uint8Array(this.exports.memory.buffer); this.memU16 = new Uint16Array(this.exports.memory.buffer); this.memU32 = new Uint32Array(this.exports.memory.buffer); this.memF32 = new Float32Array(this.exports.memory.buffer); } /** * Reads a portion of the WebAssembly memory as a Uint8Array. * @param ptr - Pointer to the start of the memory. * @param size - Number of bytes to read. * @returns The portion of memory as a Uint8Array. */ readU8(ptr, size) { return this.memU8.subarray(ptr, ptr + size); } /** * Writes an array of Uint8 values to the WebAssembly memory. * @param ptr - Pointer to the start of the memory. * @param arr - Array of Uint8 values to write. */ writeU8(ptr, arr) { this.memU8.set(arr, ptr); } /** * Reads a portion of the WebAssembly memory as a Uint16Array. * @param ptr - Pointer to the start of the memory. * @param size - Number of elements to read. * @returns The portion of memory as a Uint16Array. */ readU16(ptr, size) { return this.memU16.subarray(ptr >> 1, (ptr >> 1) + size); } /** * Writes an array of Uint16 values to the WebAssembly memory. * @param ptr - Pointer to the start of the memory. * @param arr - Array of Uint16 values to write. */ writeU16(ptr, arr) { this.memU16.set(arr, ptr >> 1); } /** * Reads a portion of the WebAssembly memory as a Uint32Array. * @param ptr - Pointer to the start of the memory. * @param size - Number of elements to read. * @returns The portion of memory as a Uint32Array. */ readU32(ptr, size) { return this.memU32.subarray(ptr >> 2, (ptr >> 2) + size); } /** * Writes an array of Uint32 values to the WebAssembly memory. * @param ptr - Pointer to the start of the memory. * @param arr - Array of Uint32 values to write. */ writeU32(ptr, arr) { this.memU32.set(arr, ptr >> 2); } /** * Reads a portion of the WebAssembly memory as a Float32Array. * @param ptr - Pointer to the start of the memory. * @param size - Number of elements to read. * @returns The portion of memory as a Float32Array. */ readF32(ptr, size) { return this.memF32.subarray(ptr >> 2, (ptr >> 2) + size); } /** * Writes an array of Float32 values to the WebAssembly memory. * @param ptr - Pointer to the start of the memory. * @param arr - Array of Float32 values to write. */ writeF32(ptr, arr) { this.memF32.set(arr, ptr >> 2); } } export class xatlasChart { /** * Reads the `xatlasChart` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasChart` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const mem = ctx.memU32; let offset = ptr / 4; this.faceArray = mem[offset]; this.atlasIndex = mem[offset + 1]; this.faceCount = mem[offset + 2]; this.type = mem[offset + 3]; this.material = mem[offset + 4]; } /** * Writes the `xatlasChart` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasChart` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const mem = ctx.memU32; let offset = ptr / 4; mem[offset] = this.faceArray; mem[offset + 1] = this.atlasIndex; mem[offset + 2] = this.faceCount; mem[offset + 3] = this.type; mem[offset + 4] = this.material; } } /** * Size of the `xatlasChart` structure in bytes. */ xatlasChart.SIZE = 20; export class xatlasVertex { /** * Reads the `xatlasVertex` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasVertex` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const memI32 = ctx.memU32; const memF32 = ctx.memF32; let offset = ptr / 4; this.atlasIndex = memI32[offset]; this.chartIndex = memI32[offset + 1]; this.uv = [memF32[offset + 2], memF32[offset + 3]]; this.xref = memI32[offset + 4]; } /** * Writes the `xatlasVertex` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasVertex` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const memI32 = ctx.memU32; const memF32 = ctx.memF32; let offset = ptr / 4; memI32[offset] = this.atlasIndex; memI32[offset + 1] = this.chartIndex; memF32[offset + 2] = this.uv[0]; memF32[offset + 3] = this.uv[1]; memI32[offset + 4] = this.xref; } } /** * Size of the `xatlasVertex` structure in bytes. */ xatlasVertex.SIZE = 20; export class xatlasMesh { /** * Reads the `xatlasMesh` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasMesh` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const mem = ctx.memU32; let offset = ptr / 4; this.chartArray = mem[offset]; this.indexArray = mem[offset + 1]; this.vertexArray = mem[offset + 2]; this.chartCount = mem[offset + 3]; this.indexCount = mem[offset + 4]; this.vertexCount = mem[offset + 5]; } /** * Writes the `xatlasMesh` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasMesh` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const mem = ctx.memU32; let offset = ptr / 4; mem[offset] = this.chartArray; mem[offset + 1] = this.indexArray; mem[offset + 2] = this.vertexArray; mem[offset + 3] = this.chartCount; mem[offset + 4] = this.indexCount; mem[offset + 5] = this.vertexCount; } } /** * Size of the `xatlasMesh` structure in bytes. */ xatlasMesh.SIZE = 24; export class xatlasAtlas { /** * Reads the `xatlasAtlas` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasAtlas` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; let offset = ptr / 4; this.image = mem[offset]; this.meshes = mem[offset + 1]; this.utilization = mem[offset + 2]; this.width = mem[offset + 3]; this.height = mem[offset + 4]; this.atlasCount = mem[offset + 5]; this.chartCount = mem[offset + 6]; this.meshCount = mem[offset + 7]; this.texelsPerUnit = memF32[offset + 8]; } /** * Writes the `xatlasAtlas` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasAtlas` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; let offset = ptr / 4; mem[offset] = this.image; mem[offset + 1] = this.meshes; mem[offset + 2] = this.utilization; mem[offset + 3] = this.width; mem[offset + 4] = this.height; mem[offset + 5] = this.atlasCount; mem[offset + 6] = this.chartCount; mem[offset + 7] = this.meshCount; memF32[offset + 8] = this.texelsPerUnit; } } /** * Warning: do not allocate this directly, real size larger than this. */ xatlasAtlas.SIZE = 36; export class xatlasMeshDecl { /** * Reads the `xatlasMeshDecl` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasMeshDecl` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; let offset = ptr / 4; this.vertexPositionData = mem[offset]; this.vertexNormalData = mem[offset + 1]; this.vertexUvData = mem[offset + 2]; this.indexData = mem[offset + 3]; this.faceIgnoreData = mem[offset + 4]; this.faceMaterialData = mem[offset + 5]; this.faceVertexCount = mem[offset + 6]; this.vertexCount = mem[offset + 7]; this.vertexPositionStride = mem[offset + 8]; this.vertexNormalStride = mem[offset + 9]; this.vertexUvStride = mem[offset + 10]; this.indexCount = mem[offset + 11]; this.indexOffset = mem[offset + 12]; this.faceCount = mem[offset + 13]; this.indexFormat = mem[offset + 14]; this.epsilon = memF32[offset + 15]; } /** * Writes the `xatlasMeshDecl` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasMeshDecl` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; let offset = ptr / 4; mem[offset] = this.vertexPositionData; mem[offset + 1] = this.vertexNormalData; mem[offset + 2] = this.vertexUvData; mem[offset + 3] = this.indexData; mem[offset + 4] = this.faceIgnoreData; mem[offset + 5] = this.faceMaterialData; mem[offset + 6] = this.faceVertexCount; mem[offset + 7] = this.vertexCount; mem[offset + 8] = this.vertexPositionStride; mem[offset + 9] = this.vertexNormalStride; mem[offset + 10] = this.vertexUvStride; mem[offset + 11] = this.indexCount; mem[offset + 12] = this.indexOffset; mem[offset + 13] = this.faceCount; mem[offset + 14] = this.indexFormat; memF32[offset + 15] = this.epsilon; } } /** * Size of the `xatlasMeshDecl` structure in bytes. */ xatlasMeshDecl.SIZE = 64; export class xatlasUvMeshDecl { /** * Reads the `xatlasUvMeshDecl` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasUvMeshDecl` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const mem = ctx.memU32; let offset = ptr / 4; this.vertexUvData = mem[offset]; this.indexData = mem[offset + 1]; this.faceMaterialData = mem[offset + 2]; this.vertexCount = mem[offset + 3]; this.vertexStride = mem[offset + 4]; this.indexCount = mem[offset + 5]; this.indexOffset = mem[offset + 6]; this.indexFormat = mem[offset + 7]; } /** * Writes the `xatlasUvMeshDecl` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasUvMeshDecl` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const mem = ctx.memU32; let offset = ptr / 4; mem[offset] = this.vertexUvData; mem[offset + 1] = this.indexData; mem[offset + 2] = this.faceMaterialData; mem[offset + 3] = this.vertexCount; mem[offset + 4] = this.vertexStride; mem[offset + 5] = this.indexCount; mem[offset + 6] = this.indexOffset; mem[offset + 7] = this.indexFormat; } } /** * Size of the `xatlasUvMeshDecl` structure in bytes. */ xatlasUvMeshDecl.SIZE = 32; export class xatlasChartOptions { /** * Reads the `xatlasChartOptions` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasChartOptions` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; const memU8 = ctx.memU8; let offset = ptr / 4; this.paramFunc = mem[offset]; this.maxChartArea = memF32[offset + 1]; this.maxBoundaryLength = memF32[offset + 2]; this.normalDeviationWeight = memF32[offset + 3]; this.roundnessWeight = memF32[offset + 4]; this.straightnessWeight = memF32[offset + 5]; this.normalSeamWeight = memF32[offset + 6]; this.textureSeamWeight = memF32[offset + 7]; this.maxCost = memF32[offset + 8]; this.maxIterations = mem[offset + 9]; this.useInputMeshUvs = !!memU8[ptr + 40]; this.fixWinding = !!memU8[ptr + 41]; } /** * Writes the `xatlasChartOptions` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasChartOptions` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; const memU8 = ctx.memU8; let offset = ptr / 4; mem[offset] = this.paramFunc; memF32[offset + 1] = this.maxChartArea; memF32[offset + 2] = this.maxBoundaryLength; memF32[offset + 3] = this.normalDeviationWeight; memF32[offset + 4] = this.roundnessWeight; memF32[offset + 5] = this.straightnessWeight; memF32[offset + 6] = this.normalSeamWeight; memF32[offset + 7] = this.textureSeamWeight; memF32[offset + 8] = this.maxCost; mem[offset + 9] = this.maxIterations; memU8[ptr + 40] = +this.useInputMeshUvs; memU8[ptr + 41] = +this.fixWinding; } } /** * Size of the `xatlasChartOptions` structure in bytes. */ xatlasChartOptions.SIZE = 44; export class xatlasPackOptions { /** * Reads the `xatlasPackOptions` data from the WebAssembly memory. * @param ptr - Pointer to the `xatlasPackOptions` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ read(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; const memU8 = ctx.memU8; let offset = ptr / 4; this.maxChartSize = mem[offset]; this.padding = mem[offset + 1]; this.texelsPerUnit = memF32[offset + 2]; this.resolution = mem[offset + 3]; this.bilinear = !!memU8[ptr + 16]; this.blockAlign = !!memU8[ptr + 17]; this.bruteForce = !!memU8[ptr + 18]; this.createImage = !!memU8[ptr + 19]; this.rotateChartsToAxis = !!memU8[ptr + 20]; this.rotateCharts = !!memU8[ptr + 21]; } /** * Writes the `xatlasPackOptions` data to the WebAssembly memory. * @param ptr - Pointer to the `xatlasPackOptions` structure in the WebAssembly memory. * @param ctx - The `WasmContext` instance providing access to the WebAssembly memory. */ write(ptr, ctx) { const mem = ctx.memU32; const memF32 = ctx.memF32; const memU8 = ctx.memU8; let offset = ptr / 4; mem[offset] = this.maxChartSize; mem[offset + 1] = this.padding; memF32[offset + 2] = this.texelsPerUnit; mem[offset + 3] = this.resolution; memU8[ptr + 16] = +this.bilinear; memU8[ptr + 17] = +this.blockAlign; memU8[ptr + 18] = +this.bruteForce; memU8[ptr + 19] = +this.createImage; memU8[ptr + 20] = +this.rotateChartsToAxis; memU8[ptr + 21] = +this.rotateCharts; } } /** * Size of the `xatlasPackOptions` structure in bytes. */ xatlasPackOptions.SIZE = 24; //# sourceMappingURL=index.js.map