(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three")); } else { var mod = { exports: {} }; factory(mod.exports, global.three); global.PRWMLoader = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.PRWMLoader = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /** * See https://github.com/kchapelier/PRWM for more informations about this file format */ var bigEndianPlatform = null; /** * Check if the endianness of the platform is big-endian (most significant bit first) * @returns {boolean} True if big-endian, false if little-endian */ function _isBigEndianPlatform() { if (bigEndianPlatform === null) { var buffer = new ArrayBuffer(2), uint8Array = new Uint8Array(buffer), uint16Array = new Uint16Array(buffer); uint8Array[0] = 0xAA; // set first byte uint8Array[1] = 0xBB; // set second byte bigEndianPlatform = uint16Array[0] === 0xAABB; } return bigEndianPlatform; } // match the values defined in the spec to the TypedArray types var InvertedEncodingTypes = [null, Float32Array, null, Int8Array, Int16Array, null, Int32Array, Uint8Array, Uint16Array, null, Uint32Array]; // define the method to use on a DataView, corresponding the TypedArray type var getMethods = { Uint16Array: 'getUint16', Uint32Array: 'getUint32', Int16Array: 'getInt16', Int32Array: 'getInt32', Float32Array: 'getFloat32', Float64Array: 'getFloat64' }; function copyFromBuffer(sourceArrayBuffer, viewType, position, length, fromBigEndian) { var bytesPerElement = viewType.BYTES_PER_ELEMENT; var result; if (fromBigEndian === _isBigEndianPlatform() || bytesPerElement === 1) { result = new viewType(sourceArrayBuffer, position, length); } else { var readView = new DataView(sourceArrayBuffer, position, length * bytesPerElement), getMethod = getMethods[viewType.name], littleEndian = !fromBigEndian; result = new viewType(length); for (var i = 0; i < length; i++) { result[i] = readView[getMethod](i * bytesPerElement, littleEndian); } } return result; } function decodePrwm(buffer) { var array = new Uint8Array(buffer), version = array[0]; var flags = array[1]; var indexedGeometry = !!(flags >> 7 & 0x01), indicesType = flags >> 6 & 0x01, bigEndian = (flags >> 5 & 0x01) === 1, attributesNumber = flags & 0x1F; var valuesNumber = 0, indicesNumber = 0; if (bigEndian) { valuesNumber = (array[2] << 16) + (array[3] << 8) + array[4]; indicesNumber = (array[5] << 16) + (array[6] << 8) + array[7]; } else { valuesNumber = array[2] + (array[3] << 8) + (array[4] << 16); indicesNumber = array[5] + (array[6] << 8) + (array[7] << 16); } /** PRELIMINARY CHECKS **/ if (version === 0) { throw new Error('PRWM decoder: Invalid format version: 0'); } else if (version !== 1) { throw new Error('PRWM decoder: Unsupported format version: ' + version); } if (!indexedGeometry) { if (indicesType !== 0) { throw new Error('PRWM decoder: Indices type must be set to 0 for non-indexed geometries'); } else if (indicesNumber !== 0) { throw new Error('PRWM decoder: Number of indices must be set to 0 for non-indexed geometries'); } } /** PARSING **/ var pos = 8; var attributes = {}; for (var i = 0; i < attributesNumber; i++) { var attributeName = ''; while (pos < array.length) { var char = array[pos]; pos++; if (char === 0) { break; } else { attributeName += String.fromCharCode(char); } } flags = array[pos]; var attributeType = flags >> 7 & 0x01; var cardinality = (flags >> 4 & 0x03) + 1; var encodingType = flags & 0x0F; var arrayType = InvertedEncodingTypes[encodingType]; pos++; // padding to next multiple of 4 pos = Math.ceil(pos / 4) * 4; var values = copyFromBuffer(buffer, arrayType, pos, cardinality * valuesNumber, bigEndian); pos += arrayType.BYTES_PER_ELEMENT * cardinality * valuesNumber; attributes[attributeName] = { type: attributeType, cardinality: cardinality, values: values }; } pos = Math.ceil(pos / 4) * 4; var indices = null; if (indexedGeometry) { indices = copyFromBuffer(buffer, indicesType === 1 ? Uint32Array : Uint16Array, pos, indicesNumber, bigEndian); } return { version: version, attributes: attributes, indices: indices }; } // Define the public interface var PRWMLoader = /*#__PURE__*/function (_Loader) { _inherits(PRWMLoader, _Loader); var _super = _createSuper(PRWMLoader); function PRWMLoader(manager) { _classCallCheck(this, PRWMLoader); return _super.call(this, manager); } _createClass(PRWMLoader, [{ key: "load", value: function load(url, onLoad, onProgress, onError) { var scope = this; var loader = new _three.FileLoader(scope.manager); loader.setPath(scope.path); loader.setResponseType('arraybuffer'); loader.setRequestHeader(scope.requestHeader); loader.setWithCredentials(scope.withCredentials); url = url.replace(/\*/g, _isBigEndianPlatform() ? 'be' : 'le'); loader.load(url, function (arrayBuffer) { try { onLoad(scope.parse(arrayBuffer)); } catch (e) { if (onError) { onError(e); } else { console.error(e); } scope.manager.itemError(url); } }, onProgress, onError); } }, { key: "parse", value: function parse(arrayBuffer) { var data = decodePrwm(arrayBuffer), attributesKey = Object.keys(data.attributes), bufferGeometry = new _three.BufferGeometry(); for (var i = 0; i < attributesKey.length; i++) { var attribute = data.attributes[attributesKey[i]]; bufferGeometry.setAttribute(attributesKey[i], new _three.BufferAttribute(attribute.values, attribute.cardinality, attribute.normalized)); } if (data.indices !== null) { bufferGeometry.setIndex(new _three.BufferAttribute(data.indices, 1)); } return bufferGeometry; } }], [{ key: "isBigEndianPlatform", value: function isBigEndianPlatform() { return _isBigEndianPlatform(); } }]); return PRWMLoader; }(_three.Loader); _exports.PRWMLoader = PRWMLoader; });