DRACOExporter.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ( function () {
  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. var DRACOExporter = function () {};
  17. DRACOExporter.prototype = {
  18. constructor: DRACOExporter,
  19. parse: function ( object, options ) {
  20. if ( object.isBufferGeometry === true ) {
  21. throw new Error( 'DRACOExporter: The first parameter of parse() is now an instance of Mesh or Points.' );
  22. }
  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: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  31. quantization: [ 16, 8, 8, 8, 8 ],
  32. exportUvs: true,
  33. exportNormals: true,
  34. exportColor: false
  35. };
  36. }
  37. var geometry = object.geometry;
  38. var dracoEncoder = DracoEncoderModule();
  39. var encoder = new dracoEncoder.Encoder();
  40. var builder;
  41. var dracoObject;
  42. if ( geometry.isBufferGeometry !== true ) {
  43. throw new Error( 'THREE.DRACOExporter.parse(geometry, options): geometry is not a THREE.BufferGeometry instance.' );
  44. }
  45. if ( object.isMesh === true ) {
  46. builder = new dracoEncoder.MeshBuilder();
  47. dracoObject = new dracoEncoder.Mesh();
  48. var vertices = geometry.getAttribute( 'position' );
  49. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  50. var faces = geometry.getIndex();
  51. if ( faces !== null ) {
  52. builder.AddFacesToMesh( dracoObject, faces.count / 3, 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( dracoObject, vertices.count, faces );
  59. }
  60. if ( options.exportNormals === true ) {
  61. var normals = geometry.getAttribute( 'normal' );
  62. if ( normals !== undefined ) {
  63. builder.AddFloatAttributeToMesh( dracoObject, 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( dracoObject, 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( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  76. }
  77. }
  78. } else if ( object.isPoints === true ) {
  79. builder = new dracoEncoder.PointCloudBuilder();
  80. dracoObject = new dracoEncoder.PointCloud();
  81. var vertices = geometry.getAttribute( 'position' );
  82. builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  83. if ( options.exportColor === true ) {
  84. var colors = geometry.getAttribute( 'color' );
  85. if ( colors !== undefined ) {
  86. builder.AddFloatAttribute( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  87. }
  88. }
  89. } else {
  90. throw new Error( 'DRACOExporter: Unsupported object type.' );
  91. } //Compress using draco encoder
  92. var encodedData = new dracoEncoder.DracoInt8Array(); //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).
  93. var encodeSpeed = options.encodeSpeed !== undefined ? options.encodeSpeed : 5;
  94. var decodeSpeed = options.decodeSpeed !== undefined ? options.decodeSpeed : 5;
  95. encoder.SetSpeedOptions( encodeSpeed, decodeSpeed ); // Sets the desired encoding method for a given geometry.
  96. if ( options.encoderMethod !== undefined ) {
  97. encoder.SetEncodingMethod( options.encoderMethod );
  98. } // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  99. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  100. if ( options.quantization !== undefined ) {
  101. for ( var i = 0; i < 5; i ++ ) {
  102. if ( options.quantization[ i ] !== undefined ) {
  103. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  104. }
  105. }
  106. }
  107. var length;
  108. if ( object.isMesh === true ) {
  109. length = encoder.EncodeMeshToDracoBuffer( dracoObject, encodedData );
  110. } else {
  111. length = encoder.EncodePointCloudToDracoBuffer( dracoObject, true, encodedData );
  112. }
  113. dracoEncoder.destroy( dracoObject );
  114. if ( length === 0 ) {
  115. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  116. } //Copy encoded data to buffer.
  117. var outputData = new Int8Array( new ArrayBuffer( length ) );
  118. for ( var i = 0; i < length; i ++ ) {
  119. outputData[ i ] = encodedData.GetValue( i );
  120. }
  121. dracoEncoder.destroy( encodedData );
  122. dracoEncoder.destroy( encoder );
  123. dracoEncoder.destroy( builder );
  124. return outputData;
  125. }
  126. }; // Encoder methods
  127. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  128. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0; // Geometry type
  129. DRACOExporter.POINT_CLOUD = 0;
  130. DRACOExporter.TRIANGULAR_MESH = 1; // Attribute type
  131. DRACOExporter.INVALID = - 1;
  132. DRACOExporter.POSITION = 0;
  133. DRACOExporter.NORMAL = 1;
  134. DRACOExporter.COLOR = 2;
  135. DRACOExporter.TEX_COORD = 3;
  136. DRACOExporter.GENERIC = 4;
  137. THREE.DRACOExporter = DRACOExporter;
  138. } )();