PRWMLoader.js 6.0 KB

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