PRWMLoader.js 5.9 KB

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