DRACOExporter.js 6.2 KB

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