DRACOExporter.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /* global DracoEncoderModule */
  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. if ( geometry.isBufferGeometry !== true ) {
  46. throw new Error( 'THREE.DRACOExporter.parse(geometry, options): geometry is not a THREE.Geometry or THREE.BufferGeometry instance.' );
  47. }
  48. var vertices = geometry.getAttribute( 'position' );
  49. builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  50. var faces = geometry.getIndex();
  51. if ( faces !== null ) {
  52. builder.AddFacesToMesh( mesh, faces.count, faces.array );
  53. } else {
  54. var faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
  55. for ( var i = 0; i < faces.length; i ++ ) {
  56. faces[ i ] = i;
  57. }
  58. builder.AddFacesToMesh( mesh, vertices.count, 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. if ( options.quantization !== undefined ) {
  89. for ( var i = 0; i < 5; i ++ ) {
  90. if ( options.quantization[ i ] !== undefined ) {
  91. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  92. }
  93. }
  94. }
  95. var length = encoder.EncodeMeshToDracoBuffer( mesh, encodedData );
  96. dracoEncoder.destroy( mesh );
  97. if ( length === 0 ) {
  98. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  99. }
  100. //Copy encoded data to buffer.
  101. var outputData = new Int8Array( new ArrayBuffer( length ) );
  102. for ( var i = 0; i < length; i ++ ) {
  103. outputData[ i ] = encodedData.GetValue( i );
  104. }
  105. dracoEncoder.destroy( encodedData );
  106. dracoEncoder.destroy( encoder );
  107. dracoEncoder.destroy( builder );
  108. return outputData;
  109. }
  110. };
  111. // Encoder methods
  112. THREE.DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  113. THREE.DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0;
  114. // Geometry type
  115. THREE.DRACOExporter.POINT_CLOUD = 0;
  116. THREE.DRACOExporter.TRIANGULAR_MESH = 1;
  117. // Attribute type
  118. THREE.DRACOExporter.INVALID = - 1;
  119. THREE.DRACOExporter.POSITION = 0;
  120. THREE.DRACOExporter.NORMAL = 1;
  121. THREE.DRACOExporter.COLOR = 2;
  122. THREE.DRACOExporter.TEX_COORD = 3;
  123. THREE.DRACOExporter.GENERIC = 4;