1 line
9.7 KiB
Plaintext
1 line
9.7 KiB
Plaintext
|
|
{"version":3,"file":"PRWMLoader.cjs","sources":["../../src/loaders/PRWMLoader.js"],"sourcesContent":["import { BufferAttribute, BufferGeometry, FileLoader, Loader } from 'three'\n\n/**\n * See https://github.com/kchapelier/PRWM for more informations about this file format\n */\n\nlet bigEndianPlatform = null\n\n/**\n * Check if the endianness of the platform is big-endian (most significant bit first)\n * @returns {boolean} True if big-endian, false if little-endian\n */\nfunction isBigEndianPlatform() {\n if (bigEndianPlatform === null) {\n const buffer = new ArrayBuffer(2),\n uint8Array = new Uint8Array(buffer),\n uint16Array = new Uint16Array(buffer)\n\n uint8Array[0] = 0xaa // set first byte\n uint8Array[1] = 0xbb // set second byte\n bigEndianPlatform = uint16Array[0] === 0xaabb\n }\n\n return bigEndianPlatform\n}\n\n// match the values defined in the spec to the TypedArray types\nconst InvertedEncodingTypes = [\n null,\n Float32Array,\n null,\n Int8Array,\n Int16Array,\n null,\n Int32Array,\n Uint8Array,\n Uint16Array,\n null,\n Uint32Array,\n]\n\n// define the method to use on a DataView, corresponding the TypedArray type\nconst getMethods = {\n Uint16Array: 'getUint16',\n Uint32Array: 'getUint32',\n Int16Array: 'getInt16',\n Int32Array: 'getInt32',\n Float32Array: 'getFloat32',\n Float64Array: 'getFloat64',\n}\n\nfunction copyFromBuffer(sourceArrayBuffer, viewType, position, length, fromBigEndian) {\n const bytesPerElement = viewType.BYTES_PER_ELEMENT\n let result\n\n if (fromBigEndian === isBigEndianPlatform() || bytesPerElement === 1) {\n result = new viewType(sourceArrayBuffer, position, length)\n } else {\n const readView = new DataView(sourceArrayBuffer, position, length * bytesPerElement),\n getMethod = getMethods[viewType.name],\n littleEndian = !fromBigEndian\n\n result = new viewType(length)\n\n for (let i = 0; i < length; i++) {\n result[i] = readView[getMethod](i * bytesPerElement, littleEndian)\n }\n }\n\n return result\n}\n\nfunction decodePrwm(buffer) {\n const array = new Uint8Array(buffer),\n version = array[0]\n\n let flags = array[1]\n\n const indexedGeometry = !!((flags >> 7) & 0x01),\n indicesType = (flags >> 6) & 0x01,\n bigEndian = ((flags >> 5) & 0x01) === 1,\n attributesNumber = flags & 0x1f\n\n let valuesNumber = 0,\n indicesNumber = 0\n\n if (bigEndian) {\n valuesNumber = (array[2] << 16) + (array[3] << 8) + array[4]\n indicesNumber = (array[5] << 16) + (array[6] << 8) + array[7]\n } else {\n valuesNumber = array[2] + (array[3] << 8) + (array[4] << 16)\n indicesNumber = array[5] + (array[6] << 8) + (array[7] << 16)\n }\n\n /** PRELIMINARY CHECKS **/\n\n if (version === 0) {\n throw new Error('PRWM decoder: Invalid format version: 0')\n } else if (version !== 1) {\n throw new Error('PRWM decoder: Unsupported format version: ' + version)\n }\n\n if (!indexedGeometry) {\n if (indicesType !== 0) {\n throw new Error('PRWM decoder: Indices type must be set to 0 for non-indexed geometries')\n } else if (indicesNumber !== 0) {\n throw new Error('PRWM decoder: Number of indices must be set to 0 for non-indexed geometries')\n }\n }\n\n /** PARSING **/\n\n let pos = 8\n\n const attributes = {}\n\n for (let i = 0; i < attributesNumber; i++) {\n let attributeName = ''\n\n while (pos < array.length) {\n const char = array[pos]\n pos++\n\n if (char === 0) {\n break\n } else {\n attributeName += String.fromCharCode(char)\n }\n }\n\n flags = array[pos]\n\n const attributeType = (flags >> 7) & 0x01\n const cardinality = ((flags >> 4) & 0x03) + 1\n const encodingType = flags & 0x0f\n const arrayType = InvertedEncodingTypes[encodingType]\n\n pos++\n\n // padding to next multiple of 4\n pos = Math.ceil(pos / 4) * 4\n\n const values = copyFromBuffer(buffer, arrayType, pos, cardinality * valuesNumber, bigEndian)\n\n pos += arrayType.BYTES_PER_ELEMENT * cardinality * valuesNumber\n\n att
|