DRACOExporter.js 5.2 KB

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