DRACOExporter.js 5.8 KB

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