DRACOExporter.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { Color } from 'three';
  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. * - exportColor
  15. */
  16. /* global DracoEncoderModule */
  17. class DRACOExporter {
  18. parse( object, options = {} ) {
  19. options = Object.assign( {
  20. decodeSpeed: 5,
  21. encodeSpeed: 5,
  22. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  23. quantization: [ 16, 8, 8, 8, 8 ],
  24. exportUvs: true,
  25. exportNormals: true,
  26. exportColor: false,
  27. }, options );
  28. if ( DracoEncoderModule === undefined ) {
  29. throw new Error( 'THREE.DRACOExporter: required the draco_encoder to work.' );
  30. }
  31. const geometry = object.geometry;
  32. const dracoEncoder = DracoEncoderModule();
  33. const encoder = new dracoEncoder.Encoder();
  34. let builder;
  35. let dracoObject;
  36. if ( object.isMesh === true ) {
  37. builder = new dracoEncoder.MeshBuilder();
  38. dracoObject = new dracoEncoder.Mesh();
  39. const vertices = geometry.getAttribute( 'position' );
  40. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  41. const faces = geometry.getIndex();
  42. if ( faces !== null ) {
  43. builder.AddFacesToMesh( dracoObject, faces.count / 3, faces.array );
  44. } else {
  45. const faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
  46. for ( let i = 0; i < faces.length; i ++ ) {
  47. faces[ i ] = i;
  48. }
  49. builder.AddFacesToMesh( dracoObject, vertices.count, faces );
  50. }
  51. if ( options.exportNormals === true ) {
  52. const normals = geometry.getAttribute( 'normal' );
  53. if ( normals !== undefined ) {
  54. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );
  55. }
  56. }
  57. if ( options.exportUvs === true ) {
  58. const uvs = geometry.getAttribute( 'uv' );
  59. if ( uvs !== undefined ) {
  60. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );
  61. }
  62. }
  63. if ( options.exportColor === true ) {
  64. const colors = geometry.getAttribute( 'color' );
  65. if ( colors !== undefined ) {
  66. const array = createVertexColorSRGBArray( colors );
  67. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, array );
  68. }
  69. }
  70. } else if ( object.isPoints === true ) {
  71. builder = new dracoEncoder.PointCloudBuilder();
  72. dracoObject = new dracoEncoder.PointCloud();
  73. const vertices = geometry.getAttribute( 'position' );
  74. builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  75. if ( options.exportColor === true ) {
  76. const colors = geometry.getAttribute( 'color' );
  77. if ( colors !== undefined ) {
  78. const array = createVertexColorSRGBArray( colors );
  79. builder.AddFloatAttribute( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, array );
  80. }
  81. }
  82. } else {
  83. throw new Error( 'DRACOExporter: Unsupported object type.' );
  84. }
  85. //Compress using draco encoder
  86. const encodedData = new dracoEncoder.DracoInt8Array();
  87. //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).
  88. const encodeSpeed = ( options.encodeSpeed !== undefined ) ? options.encodeSpeed : 5;
  89. const decodeSpeed = ( options.decodeSpeed !== undefined ) ? options.decodeSpeed : 5;
  90. encoder.SetSpeedOptions( encodeSpeed, decodeSpeed );
  91. // Sets the desired encoding method for a given geometry.
  92. if ( options.encoderMethod !== undefined ) {
  93. encoder.SetEncodingMethod( options.encoderMethod );
  94. }
  95. // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  96. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  97. if ( options.quantization !== undefined ) {
  98. for ( let i = 0; i < 5; i ++ ) {
  99. if ( options.quantization[ i ] !== undefined ) {
  100. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  101. }
  102. }
  103. }
  104. let length;
  105. if ( object.isMesh === true ) {
  106. length = encoder.EncodeMeshToDracoBuffer( dracoObject, encodedData );
  107. } else {
  108. length = encoder.EncodePointCloudToDracoBuffer( dracoObject, true, encodedData );
  109. }
  110. dracoEncoder.destroy( dracoObject );
  111. if ( length === 0 ) {
  112. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  113. }
  114. //Copy encoded data to buffer.
  115. const outputData = new Int8Array( new ArrayBuffer( length ) );
  116. for ( let i = 0; i < length; i ++ ) {
  117. outputData[ i ] = encodedData.GetValue( i );
  118. }
  119. dracoEncoder.destroy( encodedData );
  120. dracoEncoder.destroy( encoder );
  121. dracoEncoder.destroy( builder );
  122. return outputData;
  123. }
  124. }
  125. function createVertexColorSRGBArray( attribute ) {
  126. // While .drc files do not specify colorspace, the only 'official' tooling
  127. // is PLY and OBJ converters, which use sRGB. We'll assume sRGB is expected
  128. // for .drc files, but note that Draco buffers embedded in glTF files will
  129. // be Linear-sRGB instead.
  130. const _color = new Color();
  131. const count = attribute.count;
  132. const itemSize = attribute.itemSize;
  133. const array = new Float32Array( count * itemSize );
  134. for ( let i = 0, il = count; i < il; i ++ ) {
  135. _color.fromBufferAttribute( attribute, i ).convertLinearToSRGB();
  136. array[ i * itemSize ] = _color.r;
  137. array[ i * itemSize + 1 ] = _color.g;
  138. array[ i * itemSize + 2 ] = _color.b;
  139. if ( itemSize === 4 ) {
  140. array[ i * itemSize + 3 ] = attribute.getW( i );
  141. }
  142. }
  143. return array;
  144. }
  145. // Encoder methods
  146. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  147. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0;
  148. // Geometry type
  149. DRACOExporter.POINT_CLOUD = 0;
  150. DRACOExporter.TRIANGULAR_MESH = 1;
  151. // Attribute type
  152. DRACOExporter.INVALID = - 1;
  153. DRACOExporter.POSITION = 0;
  154. DRACOExporter.NORMAL = 1;
  155. DRACOExporter.COLOR = 2;
  156. DRACOExporter.TEX_COORD = 3;
  157. DRACOExporter.GENERIC = 4;
  158. export { DRACOExporter };