PRWMLoader.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ( function () {
  2. /**
  3. * See https://github.com/kchapelier/PRWM for more informations about this file format
  4. */
  5. let bigEndianPlatform = null;
  6. /**
  7. * Check if the endianness of the platform is big-endian (most significant bit first)
  8. * @returns {boolean} True if big-endian, false if little-endian
  9. */
  10. function isBigEndianPlatform() {
  11. if ( bigEndianPlatform === null ) {
  12. const buffer = new ArrayBuffer( 2 ),
  13. uint8Array = new Uint8Array( buffer ),
  14. uint16Array = new Uint16Array( buffer );
  15. uint8Array[ 0 ] = 0xAA; // set first byte
  16. uint8Array[ 1 ] = 0xBB; // set second byte
  17. bigEndianPlatform = uint16Array[ 0 ] === 0xAABB;
  18. }
  19. return bigEndianPlatform;
  20. }
  21. // match the values defined in the spec to the TypedArray types
  22. const InvertedEncodingTypes = [ null, Float32Array, null, Int8Array, Int16Array, null, Int32Array, Uint8Array, Uint16Array, null, Uint32Array ];
  23. // define the method to use on a DataView, corresponding the TypedArray type
  24. const getMethods = {
  25. Uint16Array: 'getUint16',
  26. Uint32Array: 'getUint32',
  27. Int16Array: 'getInt16',
  28. Int32Array: 'getInt32',
  29. Float32Array: 'getFloat32',
  30. Float64Array: 'getFloat64'
  31. };
  32. function copyFromBuffer( sourceArrayBuffer, viewType, position, length, fromBigEndian ) {
  33. const bytesPerElement = viewType.BYTES_PER_ELEMENT;
  34. let result;
  35. if ( fromBigEndian === isBigEndianPlatform() || bytesPerElement === 1 ) {
  36. result = new viewType( sourceArrayBuffer, position, length );
  37. } else {
  38. const readView = new DataView( sourceArrayBuffer, position, length * bytesPerElement ),
  39. getMethod = getMethods[ viewType.name ],
  40. littleEndian = ! fromBigEndian;
  41. result = new viewType( length );
  42. for ( let i = 0; i < length; i ++ ) {
  43. result[ i ] = readView[ getMethod ]( i * bytesPerElement, littleEndian );
  44. }
  45. }
  46. return result;
  47. }
  48. function decodePrwm( buffer ) {
  49. const array = new Uint8Array( buffer ),
  50. version = array[ 0 ];
  51. let flags = array[ 1 ];
  52. const indexedGeometry = !! ( flags >> 7 & 0x01 ),
  53. indicesType = flags >> 6 & 0x01,
  54. bigEndian = ( flags >> 5 & 0x01 ) === 1,
  55. attributesNumber = flags & 0x1F;
  56. let valuesNumber = 0,
  57. indicesNumber = 0;
  58. if ( bigEndian ) {
  59. valuesNumber = ( array[ 2 ] << 16 ) + ( array[ 3 ] << 8 ) + array[ 4 ];
  60. indicesNumber = ( array[ 5 ] << 16 ) + ( array[ 6 ] << 8 ) + array[ 7 ];
  61. } else {
  62. valuesNumber = array[ 2 ] + ( array[ 3 ] << 8 ) + ( array[ 4 ] << 16 );
  63. indicesNumber = array[ 5 ] + ( array[ 6 ] << 8 ) + ( array[ 7 ] << 16 );
  64. }
  65. /** PRELIMINARY CHECKS **/
  66. if ( version === 0 ) {
  67. throw new Error( 'PRWM decoder: Invalid format version: 0' );
  68. } else if ( version !== 1 ) {
  69. throw new Error( 'PRWM decoder: Unsupported format version: ' + version );
  70. }
  71. if ( ! indexedGeometry ) {
  72. if ( indicesType !== 0 ) {
  73. throw new Error( 'PRWM decoder: Indices type must be set to 0 for non-indexed geometries' );
  74. } else if ( indicesNumber !== 0 ) {
  75. throw new Error( 'PRWM decoder: Number of indices must be set to 0 for non-indexed geometries' );
  76. }
  77. }
  78. /** PARSING **/
  79. let pos = 8;
  80. const attributes = {};
  81. for ( let i = 0; i < attributesNumber; i ++ ) {
  82. let attributeName = '';
  83. while ( pos < array.length ) {
  84. const char = array[ pos ];
  85. pos ++;
  86. if ( char === 0 ) {
  87. break;
  88. } else {
  89. attributeName += String.fromCharCode( char );
  90. }
  91. }
  92. flags = array[ pos ];
  93. const attributeType = flags >> 7 & 0x01;
  94. const cardinality = ( flags >> 4 & 0x03 ) + 1;
  95. const encodingType = flags & 0x0F;
  96. const arrayType = InvertedEncodingTypes[ encodingType ];
  97. pos ++;
  98. // padding to next multiple of 4
  99. pos = Math.ceil( pos / 4 ) * 4;
  100. const values = copyFromBuffer( buffer, arrayType, pos, cardinality * valuesNumber, bigEndian );
  101. pos += arrayType.BYTES_PER_ELEMENT * cardinality * valuesNumber;
  102. attributes[ attributeName ] = {
  103. type: attributeType,
  104. cardinality: cardinality,
  105. values: values
  106. };
  107. }
  108. pos = Math.ceil( pos / 4 ) * 4;
  109. let indices = null;
  110. if ( indexedGeometry ) {
  111. indices = copyFromBuffer( buffer, indicesType === 1 ? Uint32Array : Uint16Array, pos, indicesNumber, bigEndian );
  112. }
  113. return {
  114. version: version,
  115. attributes: attributes,
  116. indices: indices
  117. };
  118. }
  119. // Define the public interface
  120. class PRWMLoader extends THREE.Loader {
  121. constructor( manager ) {
  122. super( manager );
  123. }
  124. load( url, onLoad, onProgress, onError ) {
  125. const scope = this;
  126. const loader = new THREE.FileLoader( scope.manager );
  127. loader.setPath( scope.path );
  128. loader.setResponseType( 'arraybuffer' );
  129. loader.setRequestHeader( scope.requestHeader );
  130. loader.setWithCredentials( scope.withCredentials );
  131. url = url.replace( /\*/g, isBigEndianPlatform() ? 'be' : 'le' );
  132. loader.load( url, function ( arrayBuffer ) {
  133. try {
  134. onLoad( scope.parse( arrayBuffer ) );
  135. } catch ( e ) {
  136. if ( onError ) {
  137. onError( e );
  138. } else {
  139. console.error( e );
  140. }
  141. scope.manager.itemError( url );
  142. }
  143. }, onProgress, onError );
  144. }
  145. parse( arrayBuffer ) {
  146. const data = decodePrwm( arrayBuffer ),
  147. attributesKey = Object.keys( data.attributes ),
  148. bufferGeometry = new THREE.BufferGeometry();
  149. for ( let i = 0; i < attributesKey.length; i ++ ) {
  150. const attribute = data.attributes[ attributesKey[ i ] ];
  151. bufferGeometry.setAttribute( attributesKey[ i ], new THREE.BufferAttribute( attribute.values, attribute.cardinality, attribute.normalized ) );
  152. }
  153. if ( data.indices !== null ) {
  154. bufferGeometry.setIndex( new THREE.BufferAttribute( data.indices, 1 ) );
  155. }
  156. return bufferGeometry;
  157. }
  158. static isBigEndianPlatform() {
  159. return isBigEndianPlatform();
  160. }
  161. }
  162. THREE.PRWMLoader = PRWMLoader;
  163. } )();