PRWMLoader.js 5.7 KB

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