DRACOExporter.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 ( DracoEncoderModule === undefined ) {
  27. throw new Error( 'THREE.DRACOExporter: required the draco_encoder to work.' );
  28. }
  29. const geometry = object.geometry;
  30. const dracoEncoder = DracoEncoderModule();
  31. const encoder = new dracoEncoder.Encoder();
  32. let builder;
  33. let dracoObject;
  34. if ( object.isMesh === true ) {
  35. builder = new dracoEncoder.MeshBuilder();
  36. dracoObject = new dracoEncoder.Mesh();
  37. const vertices = geometry.getAttribute( 'position' );
  38. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  39. const faces = geometry.getIndex();
  40. if ( faces !== null ) {
  41. builder.AddFacesToMesh( dracoObject, faces.count / 3, faces.array );
  42. } else {
  43. const faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
  44. for ( let i = 0; i < faces.length; i ++ ) {
  45. faces[ i ] = i;
  46. }
  47. builder.AddFacesToMesh( dracoObject, vertices.count, faces );
  48. }
  49. if ( options.exportNormals === true ) {
  50. const normals = geometry.getAttribute( 'normal' );
  51. if ( normals !== undefined ) {
  52. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );
  53. }
  54. }
  55. if ( options.exportUvs === true ) {
  56. const uvs = geometry.getAttribute( 'uv' );
  57. if ( uvs !== undefined ) {
  58. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );
  59. }
  60. }
  61. if ( options.exportColor === true ) {
  62. const colors = geometry.getAttribute( 'color' );
  63. if ( colors !== undefined ) {
  64. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  65. }
  66. }
  67. } else if ( object.isPoints === true ) {
  68. builder = new dracoEncoder.PointCloudBuilder();
  69. dracoObject = new dracoEncoder.PointCloud();
  70. const vertices = geometry.getAttribute( 'position' );
  71. builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  72. if ( options.exportColor === true ) {
  73. const colors = geometry.getAttribute( 'color' );
  74. if ( colors !== undefined ) {
  75. builder.AddFloatAttribute( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  76. }
  77. }
  78. } else {
  79. throw new Error( 'DRACOExporter: Unsupported object type.' );
  80. } //Compress using draco encoder
  81. 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).
  82. const encodeSpeed = options.encodeSpeed !== undefined ? options.encodeSpeed : 5;
  83. const decodeSpeed = options.decodeSpeed !== undefined ? options.decodeSpeed : 5;
  84. encoder.SetSpeedOptions( encodeSpeed, decodeSpeed ); // Sets the desired encoding method for a given geometry.
  85. if ( options.encoderMethod !== undefined ) {
  86. encoder.SetEncodingMethod( options.encoderMethod );
  87. } // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  88. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  89. if ( options.quantization !== undefined ) {
  90. for ( let i = 0; i < 5; i ++ ) {
  91. if ( options.quantization[ i ] !== undefined ) {
  92. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  93. }
  94. }
  95. }
  96. let length;
  97. if ( object.isMesh === true ) {
  98. length = encoder.EncodeMeshToDracoBuffer( dracoObject, encodedData );
  99. } else {
  100. length = encoder.EncodePointCloudToDracoBuffer( dracoObject, true, encodedData );
  101. }
  102. dracoEncoder.destroy( dracoObject );
  103. if ( length === 0 ) {
  104. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  105. } //Copy encoded data to buffer.
  106. const outputData = new Int8Array( new ArrayBuffer( length ) );
  107. for ( let i = 0; i < length; i ++ ) {
  108. outputData[ i ] = encodedData.GetValue( i );
  109. }
  110. dracoEncoder.destroy( encodedData );
  111. dracoEncoder.destroy( encoder );
  112. dracoEncoder.destroy( builder );
  113. return outputData;
  114. }
  115. } // Encoder methods
  116. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  117. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0; // Geometry type
  118. DRACOExporter.POINT_CLOUD = 0;
  119. DRACOExporter.TRIANGULAR_MESH = 1; // Attribute type
  120. DRACOExporter.INVALID = - 1;
  121. DRACOExporter.POSITION = 0;
  122. DRACOExporter.NORMAL = 1;
  123. DRACOExporter.COLOR = 2;
  124. DRACOExporter.TEX_COORD = 3;
  125. DRACOExporter.GENERIC = 4;
  126. THREE.DRACOExporter = DRACOExporter;
  127. } )();