PRWMLoader.js 6.0 KB

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