DRACOExporter.js 5.5 KB

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