DRACOExporter.js 4.9 KB

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