PRWMLoader.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. console.warn( "THREE.PRWMLoader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * See https://github.com/kchapelier/PRWM for more informations about this file format
  4. */
  5. THREE.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. }
  22. // match the values defined in the spec to the TypedArray types
  23. var InvertedEncodingTypes = [
  24. null,
  25. Float32Array,
  26. null,
  27. Int8Array,
  28. Int16Array,
  29. null,
  30. Int32Array,
  31. Uint8Array,
  32. Uint16Array,
  33. null,
  34. Uint32Array
  35. ];
  36. // define the method to use on a DataView, corresponding the TypedArray type
  37. var getMethods = {
  38. Uint16Array: 'getUint16',
  39. Uint32Array: 'getUint32',
  40. Int16Array: 'getInt16',
  41. Int32Array: 'getInt32',
  42. Float32Array: 'getFloat32',
  43. Float64Array: 'getFloat64'
  44. };
  45. function copyFromBuffer( sourceArrayBuffer, viewType, position, length, fromBigEndian ) {
  46. var bytesPerElement = viewType.BYTES_PER_ELEMENT,
  47. result;
  48. if ( fromBigEndian === isBigEndianPlatform() || bytesPerElement === 1 ) {
  49. result = new viewType( sourceArrayBuffer, position, length );
  50. } else {
  51. var readView = new DataView( sourceArrayBuffer, position, length * bytesPerElement ),
  52. getMethod = getMethods[ viewType.name ],
  53. littleEndian = ! fromBigEndian,
  54. i = 0;
  55. result = new viewType( length );
  56. for ( ; i < length; i ++ ) {
  57. result[ i ] = readView[ getMethod ]( i * bytesPerElement, littleEndian );
  58. }
  59. }
  60. return result;
  61. }
  62. function decodePrwm( buffer ) {
  63. var array = new Uint8Array( buffer ),
  64. version = array[ 0 ],
  65. flags = array[ 1 ],
  66. indexedGeometry = !! ( flags >> 7 & 0x01 ),
  67. indicesType = flags >> 6 & 0x01,
  68. bigEndian = ( flags >> 5 & 0x01 ) === 1,
  69. attributesNumber = flags & 0x1F,
  70. valuesNumber = 0,
  71. indicesNumber = 0;
  72. if ( bigEndian ) {
  73. valuesNumber = ( array[ 2 ] << 16 ) + ( array[ 3 ] << 8 ) + array[ 4 ];
  74. indicesNumber = ( array[ 5 ] << 16 ) + ( array[ 6 ] << 8 ) + array[ 7 ];
  75. } else {
  76. valuesNumber = array[ 2 ] + ( array[ 3 ] << 8 ) + ( array[ 4 ] << 16 );
  77. indicesNumber = array[ 5 ] + ( array[ 6 ] << 8 ) + ( array[ 7 ] << 16 );
  78. }
  79. /** PRELIMINARY CHECKS **/
  80. if ( version === 0 ) {
  81. throw new Error( 'PRWM decoder: Invalid format version: 0' );
  82. } else if ( version !== 1 ) {
  83. throw new Error( 'PRWM decoder: Unsupported format version: ' + version );
  84. }
  85. if ( ! indexedGeometry ) {
  86. if ( indicesType !== 0 ) {
  87. throw new Error( 'PRWM decoder: Indices type must be set to 0 for non-indexed geometries' );
  88. } else if ( indicesNumber !== 0 ) {
  89. throw new Error( 'PRWM decoder: Number of indices must be set to 0 for non-indexed geometries' );
  90. }
  91. }
  92. /** PARSING **/
  93. var pos = 8;
  94. var attributes = {},
  95. attributeName,
  96. char,
  97. attributeType,
  98. cardinality,
  99. encodingType,
  100. arrayType,
  101. values,
  102. indices,
  103. i;
  104. for ( i = 0; i < attributesNumber; i ++ ) {
  105. attributeName = '';
  106. while ( pos < array.length ) {
  107. char = array[ pos ];
  108. pos ++;
  109. if ( char === 0 ) {
  110. break;
  111. } else {
  112. attributeName += String.fromCharCode( char );
  113. }
  114. }
  115. flags = array[ pos ];
  116. attributeType = flags >> 7 & 0x01;
  117. cardinality = ( flags >> 4 & 0x03 ) + 1;
  118. encodingType = flags & 0x0F;
  119. arrayType = InvertedEncodingTypes[ encodingType ];
  120. pos ++;
  121. // padding to next multiple of 4
  122. pos = Math.ceil( pos / 4 ) * 4;
  123. values = copyFromBuffer( buffer, arrayType, pos, cardinality * valuesNumber, bigEndian );
  124. pos += arrayType.BYTES_PER_ELEMENT * cardinality * valuesNumber;
  125. attributes[ attributeName ] = {
  126. type: attributeType,
  127. cardinality: cardinality,
  128. values: values
  129. };
  130. }
  131. pos = Math.ceil( pos / 4 ) * 4;
  132. indices = null;
  133. if ( indexedGeometry ) {
  134. indices = copyFromBuffer(
  135. buffer,
  136. indicesType === 1 ? Uint32Array : Uint16Array,
  137. pos,
  138. indicesNumber,
  139. bigEndian
  140. );
  141. }
  142. return {
  143. version: version,
  144. attributes: attributes,
  145. indices: indices
  146. };
  147. }
  148. // Define the public interface
  149. function PRWMLoader( manager ) {
  150. THREE.Loader.call( this, manager );
  151. }
  152. PRWMLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  153. constructor: PRWMLoader,
  154. load: function ( url, onLoad, onProgress, onError ) {
  155. var scope = this;
  156. var loader = new THREE.FileLoader( scope.manager );
  157. loader.setPath( scope.path );
  158. loader.setResponseType( 'arraybuffer' );
  159. loader.setRequestHeader( scope.requestHeader );
  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. } )();