PRWMLoader.js 5.9 KB

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