DRACOExporter.js 5.9 KB

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