DRACOExporter.js 5.2 KB

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