@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
98 lines (95 loc) • 3.79 kB
JavaScript
import { j as U8, ak as DV } from './index-By0tcgYN.esm.js';
const BYTE = 5120;
const UNSIGNED_BYTE = 5121;
const SHORT = 5122;
const UNSIGNED_SHORT = 5123;
const FLOAT = 5126;
const TYPE_COMPONENTS = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 };
const COMPONENT_BYTES = { 5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4 };
function align4(n) {
return n + 3 & -4;
}
function readComponent(view, offset, componentType, normalized) {
switch (componentType) {
case BYTE: {
const c = view.getInt8(offset);
return normalized ? Math.max(c / 127, -1) : c;
}
case UNSIGNED_BYTE: {
const c = view.getUint8(offset);
return normalized ? c / 255 : c;
}
case SHORT: {
const c = view.getInt16(offset, true);
return normalized ? Math.max(c / 32767, -1) : c;
}
case UNSIGNED_SHORT: {
const c = view.getUint16(offset, true);
return normalized ? c / 65535 : c;
}
case FLOAT:
return view.getFloat32(offset, true);
default:
throw new Error(`KHR_mesh_quantization: unsupported componentType ${componentType}`);
}
}
const feature = {
id: "KHR_mesh_quantization",
async preParse(json, binChunk) {
const accessors = json.accessors ?? [];
const bufferViews = json.bufferViews ?? [];
const convert = [];
let appended = 0;
for (let i = 0; i < accessors.length; i++) {
const a = accessors[i];
if (a.bufferView === void 0) {
continue;
}
const componentCount = TYPE_COMPONENTS[a.type] ?? 1;
const stride = bufferViews[a.bufferView]?.byteStride;
const compBytes = COMPONENT_BYTES[a.componentType];
const signed = a.componentType === BYTE || a.componentType === SHORT;
const stridedFloat = a.componentType === FLOAT && stride !== void 0 && stride !== componentCount * 4;
const unsignedInt = a.componentType === UNSIGNED_BYTE || a.componentType === UNSIGNED_SHORT;
const stridedUnsignedInt = unsignedInt && a.normalized !== true && a.type !== "VEC4" && stride !== void 0 && compBytes !== void 0 && stride !== componentCount * compBytes;
if (signed || a.normalized === true || stridedFloat || stridedUnsignedInt) {
convert.push(i);
appended = align4(appended + a.count * componentCount * 4);
}
}
if (convert.length === 0) {
return;
}
const baseLen = align4(binChunk.byteLength);
const out = new ArrayBuffer(baseLen + appended);
new U8(out).set(new U8(binChunk.buffer, binChunk.byteOffset, binChunk.byteLength));
const outView = new DV(out);
let cursor = baseLen;
for (const i of convert) {
const a = accessors[i];
const bv = bufferViews[a.bufferView];
const componentCount = TYPE_COMPONENTS[a.type] ?? 1;
const compBytes = COMPONENT_BYTES[a.componentType];
const stride = bv.byteStride ?? componentCount * compBytes;
const srcBase = (bv.byteOffset ?? 0) + (a.byteOffset ?? 0);
const dstOffset = cursor;
for (let v = 0; v < a.count; v++) {
for (let c = 0; c < componentCount; c++) {
const value = readComponent(binChunk, srcBase + v * stride + c * compBytes, a.componentType, !!a.normalized);
outView.setFloat32(dstOffset + (v * componentCount + c) * 4, value, true);
}
}
const byteLength = a.count * componentCount * 4;
const newBvIndex = bufferViews.length;
bufferViews.push({ buffer: 0, byteOffset: dstOffset, byteLength });
a.bufferView = newBvIndex;
a.byteOffset = 0;
a.componentType = FLOAT;
a.normalized = false;
cursor = align4(cursor + byteLength);
}
return outView;
}
};
export { feature as default };
//# sourceMappingURL=gltf-ext-quantization-D_TWzdbd.esm.js.map