DRACOExporter.js 4.9 KB

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