PRWMLoader.js 6.1 KB

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