PRWMLoader.js 5.6 KB

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