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. }
  81. //Compress using draco encoder
  82. const encodedData = new dracoEncoder.DracoInt8Array();
  83. //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).
  84. const encodeSpeed = options.encodeSpeed !== undefined ? options.encodeSpeed : 5;
  85. const decodeSpeed = options.decodeSpeed !== undefined ? options.decodeSpeed : 5;
  86. encoder.SetSpeedOptions( encodeSpeed, decodeSpeed );
  87. // Sets the desired encoding method for a given geometry.
  88. if ( options.encoderMethod !== undefined ) {
  89. encoder.SetEncodingMethod( options.encoderMethod );
  90. }
  91. // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  92. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  93. if ( options.quantization !== undefined ) {
  94. for ( let i = 0; i < 5; i ++ ) {
  95. if ( options.quantization[ i ] !== undefined ) {
  96. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  97. }
  98. }
  99. }
  100. let length;
  101. if ( object.isMesh === true ) {
  102. length = encoder.EncodeMeshToDracoBuffer( dracoObject, encodedData );
  103. } else {
  104. length = encoder.EncodePointCloudToDracoBuffer( dracoObject, true, encodedData );
  105. }
  106. dracoEncoder.destroy( dracoObject );
  107. if ( length === 0 ) {
  108. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  109. }
  110. //Copy encoded data to buffer.
  111. const outputData = new Int8Array( new ArrayBuffer( length ) );
  112. for ( let i = 0; i < length; i ++ ) {
  113. outputData[ i ] = encodedData.GetValue( i );
  114. }
  115. dracoEncoder.destroy( encodedData );
  116. dracoEncoder.destroy( encoder );
  117. dracoEncoder.destroy( builder );
  118. return outputData;
  119. }
  120. }
  121. // Encoder methods
  122. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  123. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0;
  124. // Geometry type
  125. DRACOExporter.POINT_CLOUD = 0;
  126. DRACOExporter.TRIANGULAR_MESH = 1;
  127. // Attribute type
  128. DRACOExporter.INVALID = - 1;
  129. DRACOExporter.POSITION = 0;
  130. DRACOExporter.NORMAL = 1;
  131. DRACOExporter.COLOR = 2;
  132. DRACOExporter.TEX_COORD = 3;
  133. DRACOExporter.GENERIC = 4;
  134. THREE.DRACOExporter = DRACOExporter;
  135. } )();