DRACOExporter.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {
  2. BufferGeometry
  3. } from "../../../build/three.module.js";
  4. /**
  5. * Export draco compressed files from threejs geometry objects.
  6. *
  7. * Draco files are compressed and usually are smaller than conventional 3D file formats.
  8. *
  9. * The exporter receives a options object containing
  10. * - decodeSpeed, indicates how to tune the encoder regarding decode speed (0 gives better speed but worst quality)
  11. * - encodeSpeed, indicates how to tune the encoder parameters (0 gives better speed but worst quality)
  12. * - encoderMethod
  13. * - quantization, indicates the presision of each type of data stored in the draco file in the order (POSITION, NORMAL, COLOR, TEX_COORD, GENERIC)
  14. * - exportUvs
  15. * - exportNormals
  16. */
  17. /* global DracoEncoderModule */
  18. var DRACOExporter = function () {};
  19. DRACOExporter.prototype = {
  20. constructor: DRACOExporter,
  21. parse: function ( object, options ) {
  22. if ( object.isBufferGeometry === true || object.isGeometry === true ) {
  23. throw new Error( 'DRACOExporter: The first parameter of parse() is now an instance of Mesh or Points.' );
  24. }
  25. if ( DracoEncoderModule === undefined ) {
  26. throw new Error( 'THREE.DRACOExporter: required the draco_decoder to work.' );
  27. }
  28. if ( options === undefined ) {
  29. options = {
  30. decodeSpeed: 5,
  31. encodeSpeed: 5,
  32. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  33. quantization: [ 16, 8, 8, 8, 8 ],
  34. exportUvs: true,
  35. exportNormals: true,
  36. exportColor: false,
  37. };
  38. }
  39. var geometry = object.geometry;
  40. var dracoEncoder = DracoEncoderModule();
  41. var encoder = new dracoEncoder.Encoder();
  42. var builder;
  43. var dracoObject;
  44. if ( geometry.isGeometry === true ) {
  45. var bufferGeometry = new BufferGeometry();
  46. bufferGeometry.setFromObject( object );
  47. geometry = bufferGeometry;
  48. }
  49. if ( geometry.isBufferGeometry !== true ) {
  50. throw new Error( 'THREE.DRACOExporter.parse(geometry, options): geometry is not a THREE.Geometry or BufferGeometry instance.' );
  51. }
  52. if ( object.isMesh === true ) {
  53. builder = new dracoEncoder.MeshBuilder();
  54. dracoObject = new dracoEncoder.Mesh();
  55. var vertices = geometry.getAttribute( 'position' );
  56. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  57. var faces = geometry.getIndex();
  58. if ( faces !== null ) {
  59. builder.AddFacesToMesh( dracoObject, faces.count / 3, faces.array );
  60. } else {
  61. var faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
  62. for ( var i = 0; i < faces.length; i ++ ) {
  63. faces[ i ] = i;
  64. }
  65. builder.AddFacesToMesh( dracoObject, vertices.count, faces );
  66. }
  67. if ( options.exportNormals === true ) {
  68. var normals = geometry.getAttribute( 'normal' );
  69. if ( normals !== undefined ) {
  70. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );
  71. }
  72. }
  73. if ( options.exportUvs === true ) {
  74. var uvs = geometry.getAttribute( 'uv' );
  75. if ( uvs !== undefined ) {
  76. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );
  77. }
  78. }
  79. if ( options.exportColor === true ) {
  80. var colors = geometry.getAttribute( 'color' );
  81. if ( colors !== undefined ) {
  82. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  83. }
  84. }
  85. } else if ( object.isPoints === true ) {
  86. builder = new dracoEncoder.PointCloudBuilder();
  87. dracoObject = new dracoEncoder.PointCloud();
  88. var vertices = geometry.getAttribute( 'position' );
  89. builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  90. if ( options.exportColor === true ) {
  91. var colors = geometry.getAttribute( 'color' );
  92. if ( colors !== undefined ) {
  93. builder.AddFloatAttribute( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array );
  94. }
  95. }
  96. } else {
  97. throw new Error( 'DRACOExporter: Unsupported object type.' );
  98. }
  99. //Compress using draco encoder
  100. var encodedData = new dracoEncoder.DracoInt8Array();
  101. //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).
  102. var encodeSpeed = ( options.encodeSpeed !== undefined ) ? options.encodeSpeed : 5;
  103. var decodeSpeed = ( options.decodeSpeed !== undefined ) ? options.decodeSpeed : 5;
  104. encoder.SetSpeedOptions( encodeSpeed, decodeSpeed );
  105. // Sets the desired encoding method for a given geometry.
  106. if ( options.encoderMethod !== undefined ) {
  107. encoder.SetEncodingMethod( options.encoderMethod );
  108. }
  109. // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  110. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  111. if ( options.quantization !== undefined ) {
  112. for ( var i = 0; i < 5; i ++ ) {
  113. if ( options.quantization[ i ] !== undefined ) {
  114. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  115. }
  116. }
  117. }
  118. var length;
  119. if ( object.isMesh === true ) {
  120. length = encoder.EncodeMeshToDracoBuffer( dracoObject, encodedData );
  121. } else {
  122. length = encoder.EncodePointCloudToDracoBuffer( dracoObject, true, encodedData );
  123. }
  124. dracoEncoder.destroy( dracoObject );
  125. if ( length === 0 ) {
  126. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  127. }
  128. //Copy encoded data to buffer.
  129. var outputData = new Int8Array( new ArrayBuffer( length ) );
  130. for ( var i = 0; i < length; i ++ ) {
  131. outputData[ i ] = encodedData.GetValue( i );
  132. }
  133. dracoEncoder.destroy( encodedData );
  134. dracoEncoder.destroy( encoder );
  135. dracoEncoder.destroy( builder );
  136. return outputData;
  137. }
  138. };
  139. // Encoder methods
  140. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  141. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0;
  142. // Geometry type
  143. DRACOExporter.POINT_CLOUD = 0;
  144. DRACOExporter.TRIANGULAR_MESH = 1;
  145. // Attribute type
  146. DRACOExporter.INVALID = - 1;
  147. DRACOExporter.POSITION = 0;
  148. DRACOExporter.NORMAL = 1;
  149. DRACOExporter.COLOR = 2;
  150. DRACOExporter.TEX_COORD = 3;
  151. DRACOExporter.GENERIC = 4;
  152. export { DRACOExporter };