DracoExporter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. 'use strict';
  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. THREE.DRACOExporter = function () {};
  19. THREE.DRACOExporter.prototype = {
  20. constructor: THREE.DRACOExporter,
  21. parse: function ( geometry, options ) {
  22. if ( DracoEncoderModule === undefined ) {
  23. throw new Error( 'THREE.DRACOExporter: required the draco_decoder to work.' );
  24. }
  25. if ( options === undefined ) {
  26. options = {
  27. decodeSpeed: 5,
  28. encodeSpeed: 5,
  29. encoderMethod: THREE.DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  30. quantization: [ 16, 8, 8, 8, 8 ],
  31. exportUvs: true,
  32. exportNormals: true,
  33. exportColor: false,
  34. };
  35. }
  36. var dracoEncoder = DracoEncoderModule();
  37. var encoder = new dracoEncoder.Encoder();
  38. var builder = new dracoEncoder.MeshBuilder();
  39. var mesh = new dracoEncoder.Mesh();
  40. if ( geometry.isGeometry === true ) {
  41. var bufferGeometry = new THREE.BufferGeometry();
  42. bufferGeometry.fromGeometry( geometry );
  43. geometry = bufferGeometry;
  44. }
  45. var vertices = geometry.getAttribute( 'position' );
  46. builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  47. var faces = geometry.getIndex();
  48. if ( faces !== null ) {
  49. builder.AddFacesToMesh( mesh, faces.count, faces.array );
  50. } else {
  51. var faces = new Uint32Array( vertices.array.length * 3 );
  52. for ( var i = 0, f = 0; i < vertices.array.length; i += 3 ) {
  53. faces[ i ] = f;
  54. faces[ i + 1 ] = f + 1;
  55. faces[ i + 2 ] = f + 2;
  56. f ++;
  57. }
  58. builder.AddFacesToMesh( mesh, vertices.array.length, faces );
  59. }
  60. if ( options.exportNormals === true ) {
  61. var normals = geometry.getAttribute( 'normal' );
  62. if ( normals !== undefined ) {
  63. builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );
  64. }
  65. }
  66. if ( options.exportUvs === true ) {
  67. var uvs = geometry.getAttribute( 'uv' );
  68. if ( uvs !== undefined ) {
  69. builder.AddFloatAttributeToMesh( mesh, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );
  70. }
  71. }
  72. if ( options.exportColor === true ) {
  73. var colors = geometry.getAttribute( 'color' );
  74. if ( colors !== undefined ) {
  75. builder.AddFloatAttributeToMesh( mesh, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  76. }
  77. }
  78. //Compress using draco encoder
  79. var encodedData = new dracoEncoder.DracoInt8Array();
  80. //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).
  81. encoder.SetSpeedOptions( options.encodeSpeed || 5, options.decodeSpeed || 5 );
  82. // Sets the desired encoding method for a given geometry.
  83. if ( options.encoderMethod !== undefined ) {
  84. encoder.SetEncodingMethod( options.encoderMethod );
  85. }
  86. // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  87. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  88. for ( var i = 0; i < 5; i ++ ) {
  89. if ( options.quantization[ i ] !== undefined ) {
  90. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  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;