DRACOExporter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ( function () {
  2. /**
  3. * Export draco compressed files from threejs geometry objects.
  4. *
  5. * Draco files are compressed and usually are smaller than conventional 3D file formats.
  6. *
  7. * The exporter receives a options object containing
  8. * - decodeSpeed, indicates how to tune the encoder regarding decode speed (0 gives better speed but worst quality)
  9. * - encodeSpeed, indicates how to tune the encoder parameters (0 gives better speed but worst quality)
  10. * - encoderMethod
  11. * - quantization, indicates the presision of each type of data stored in the draco file in the order (POSITION, NORMAL, COLOR, TEX_COORD, GENERIC)
  12. * - exportUvs
  13. * - exportNormals
  14. */
  15. /* global DracoEncoderModule */
  16. class DRACOExporter {
  17. parse( object, options = {
  18. decodeSpeed: 5,
  19. encodeSpeed: 5,
  20. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  21. quantization: [ 16, 8, 8, 8, 8 ],
  22. exportUvs: true,
  23. exportNormals: true,
  24. exportColor: false
  25. } ) {
  26. if ( object.isBufferGeometry === true ) {
  27. throw new Error( 'DRACOExporter: The first parameter of parse() is now an instance of Mesh or Points.' );
  28. }
  29. if ( DracoEncoderModule === undefined ) {
  30. throw new Error( 'THREE.DRACOExporter: required the draco_encoder to work.' );
  31. }
  32. const geometry = object.geometry;
  33. const dracoEncoder = DracoEncoderModule();
  34. const encoder = new dracoEncoder.Encoder();
  35. let builder;
  36. let dracoObject;
  37. if ( geometry.isBufferGeometry !== true ) {
  38. throw new Error( 'THREE.DRACOExporter.parse(geometry, options): geometry is not a THREE.BufferGeometry instance.' );
  39. }
  40. if ( object.isMesh === true ) {
  41. builder = new dracoEncoder.MeshBuilder();
  42. dracoObject = new dracoEncoder.Mesh();
  43. const vertices = geometry.getAttribute( 'position' );
  44. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  45. const faces = geometry.getIndex();
  46. if ( faces !== null ) {
  47. builder.AddFacesToMesh( dracoObject, faces.count / 3, faces.array );
  48. } else {
  49. const faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
  50. for ( let i = 0; i < faces.length; i ++ ) {
  51. faces[ i ] = i;
  52. }
  53. builder.AddFacesToMesh( dracoObject, vertices.count, faces );
  54. }
  55. if ( options.exportNormals === true ) {
  56. const normals = geometry.getAttribute( 'normal' );
  57. if ( normals !== undefined ) {
  58. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );
  59. }
  60. }
  61. if ( options.exportUvs === true ) {
  62. const uvs = geometry.getAttribute( 'uv' );
  63. if ( uvs !== undefined ) {
  64. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );
  65. }
  66. }
  67. if ( options.exportColor === true ) {
  68. const colors = geometry.getAttribute( 'color' );
  69. if ( colors !== undefined ) {
  70. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  71. }
  72. }
  73. } else if ( object.isPoints === true ) {
  74. builder = new dracoEncoder.PointCloudBuilder();
  75. dracoObject = new dracoEncoder.PointCloud();
  76. const vertices = geometry.getAttribute( 'position' );
  77. builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  78. if ( options.exportColor === true ) {
  79. const colors = geometry.getAttribute( 'color' );
  80. if ( colors !== undefined ) {
  81. builder.AddFloatAttribute( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  82. }
  83. }
  84. } else {
  85. throw new Error( 'DRACOExporter: Unsupported object type.' );
  86. } //Compress using draco encoder
  87. const encodedData = new dracoEncoder.DracoInt8Array(); //Sets the desired encoding and decoding speed for the given options from 0 (slowest speed, but the best compression) to 10 (fastest, but the worst compression).
  88. const encodeSpeed = options.encodeSpeed !== undefined ? options.encodeSpeed : 5;
  89. const decodeSpeed = options.decodeSpeed !== undefined ? options.decodeSpeed : 5;
  90. encoder.SetSpeedOptions( encodeSpeed, decodeSpeed ); // Sets the desired encoding method for a given geometry.
  91. if ( options.encoderMethod !== undefined ) {
  92. encoder.SetEncodingMethod( options.encoderMethod );
  93. } // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  94. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  95. if ( options.quantization !== undefined ) {
  96. for ( let i = 0; i < 5; i ++ ) {
  97. if ( options.quantization[ i ] !== undefined ) {
  98. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  99. }
  100. }
  101. }
  102. let length;
  103. if ( object.isMesh === true ) {
  104. length = encoder.EncodeMeshToDracoBuffer( dracoObject, encodedData );
  105. } else {
  106. length = encoder.EncodePointCloudToDracoBuffer( dracoObject, true, encodedData );
  107. }
  108. dracoEncoder.destroy( dracoObject );
  109. if ( length === 0 ) {
  110. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  111. } //Copy encoded data to buffer.
  112. const outputData = new Int8Array( new ArrayBuffer( length ) );
  113. for ( let i = 0; i < length; i ++ ) {
  114. outputData[ i ] = encodedData.GetValue( i );
  115. }
  116. dracoEncoder.destroy( encodedData );
  117. dracoEncoder.destroy( encoder );
  118. dracoEncoder.destroy( builder );
  119. return outputData;
  120. }
  121. } // Encoder methods
  122. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  123. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0; // Geometry type
  124. DRACOExporter.POINT_CLOUD = 0;
  125. DRACOExporter.TRIANGULAR_MESH = 1; // Attribute type
  126. DRACOExporter.INVALID = - 1;
  127. DRACOExporter.POSITION = 0;
  128. DRACOExporter.NORMAL = 1;
  129. DRACOExporter.COLOR = 2;
  130. DRACOExporter.TEX_COORD = 3;
  131. DRACOExporter.GENERIC = 4;
  132. THREE.DRACOExporter = DRACOExporter;
  133. } )();