DRACOExporter.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import { Color, ColorManagement, SRGBColorSpace } from 'three';
  2. /* global DracoEncoderModule */
  3. /**
  4. * An exporter to compress geometry with the Draco library.
  5. *
  6. * [Draco]{@link https://google.github.io/draco/} is an open source library for compressing and
  7. * decompressing 3D meshes and point clouds. Compressed geometry can be significantly smaller,
  8. * at the cost of additional decoding time on the client device.
  9. *
  10. * Standalone Draco files have a `.drc` extension, and contain vertex positions,
  11. * normals, colors, and other attributes. Draco files *do not* contain materials,
  12. * textures, animation, or node hierarchies – to use these features, embed Draco geometry
  13. * inside of a glTF file. A normal glTF file can be converted to a Draco-compressed glTF file
  14. * using [glTF-Pipeline]{@link https://github.com/AnalyticalGraphicsInc/gltf-pipeline}.
  15. *
  16. * ```js
  17. * const exporter = new DRACOExporter();
  18. * const data = exporter.parse( mesh, options );
  19. * ```
  20. *
  21. * @three_import import { DRACOExporter } from 'three/addons/exporters/DRACOExporter.js';
  22. */
  23. class DRACOExporter {
  24. /**
  25. * Parses the given mesh or point cloud and generates the Draco output.
  26. *
  27. * @param {(Mesh|Points)} object - The mesh or point cloud to export.
  28. * @param {DRACOExporter~Options} options - The export options.
  29. * @return {Int8Array} The exported Draco.
  30. */
  31. parse( object, options = {} ) {
  32. options = Object.assign( {
  33. decodeSpeed: 5,
  34. encodeSpeed: 5,
  35. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  36. quantization: [ 16, 8, 8, 8, 8 ],
  37. exportUvs: true,
  38. exportNormals: true,
  39. exportColor: false,
  40. }, options );
  41. if ( DracoEncoderModule === undefined ) {
  42. throw new Error( 'THREE.DRACOExporter: required the draco_encoder to work.' );
  43. }
  44. const geometry = object.geometry;
  45. const dracoEncoder = DracoEncoderModule();
  46. const encoder = new dracoEncoder.Encoder();
  47. let builder;
  48. let dracoObject;
  49. if ( object.isMesh === true ) {
  50. builder = new dracoEncoder.MeshBuilder();
  51. dracoObject = new dracoEncoder.Mesh();
  52. const vertices = geometry.getAttribute( 'position' );
  53. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  54. const faces = geometry.getIndex();
  55. if ( faces !== null ) {
  56. builder.AddFacesToMesh( dracoObject, faces.count / 3, faces.array );
  57. } else {
  58. const faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
  59. for ( let 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. const 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. const 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. const colors = geometry.getAttribute( 'color' );
  78. if ( colors !== undefined ) {
  79. const array = createVertexColorSRGBArray( colors );
  80. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, array );
  81. }
  82. }
  83. } else if ( object.isPoints === true ) {
  84. builder = new dracoEncoder.PointCloudBuilder();
  85. dracoObject = new dracoEncoder.PointCloud();
  86. const vertices = geometry.getAttribute( 'position' );
  87. builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  88. if ( options.exportColor === true ) {
  89. const colors = geometry.getAttribute( 'color' );
  90. if ( colors !== undefined ) {
  91. const array = createVertexColorSRGBArray( colors );
  92. builder.AddFloatAttribute( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, array );
  93. }
  94. }
  95. } else {
  96. throw new Error( 'DRACOExporter: Unsupported object type.' );
  97. }
  98. //Compress using draco encoder
  99. const encodedData = new dracoEncoder.DracoInt8Array();
  100. //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).
  101. const encodeSpeed = ( options.encodeSpeed !== undefined ) ? options.encodeSpeed : 5;
  102. const decodeSpeed = ( options.decodeSpeed !== undefined ) ? options.decodeSpeed : 5;
  103. encoder.SetSpeedOptions( encodeSpeed, decodeSpeed );
  104. // Sets the desired encoding method for a given geometry.
  105. if ( options.encoderMethod !== undefined ) {
  106. encoder.SetEncodingMethod( options.encoderMethod );
  107. }
  108. // Sets the quantization (number of bits used to represent) compression options for a named attribute.
  109. // The attribute values will be quantized in a box defined by the maximum extent of the attribute values.
  110. if ( options.quantization !== undefined ) {
  111. for ( let i = 0; i < 5; i ++ ) {
  112. if ( options.quantization[ i ] !== undefined ) {
  113. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  114. }
  115. }
  116. }
  117. let length;
  118. if ( object.isMesh === true ) {
  119. length = encoder.EncodeMeshToDracoBuffer( dracoObject, encodedData );
  120. } else {
  121. length = encoder.EncodePointCloudToDracoBuffer( dracoObject, true, encodedData );
  122. }
  123. dracoEncoder.destroy( dracoObject );
  124. if ( length === 0 ) {
  125. throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' );
  126. }
  127. //Copy encoded data to buffer.
  128. const outputData = new Int8Array( new ArrayBuffer( length ) );
  129. for ( let i = 0; i < length; i ++ ) {
  130. outputData[ i ] = encodedData.GetValue( i );
  131. }
  132. dracoEncoder.destroy( encodedData );
  133. dracoEncoder.destroy( encoder );
  134. dracoEncoder.destroy( builder );
  135. return outputData;
  136. }
  137. }
  138. function createVertexColorSRGBArray( attribute ) {
  139. // While .drc files do not specify colorspace, the only 'official' tooling
  140. // is PLY and OBJ converters, which use sRGB. We'll assume sRGB is expected
  141. // for .drc files, but note that Draco buffers embedded in glTF files will
  142. // be Linear-sRGB instead.
  143. const _color = new Color();
  144. const count = attribute.count;
  145. const itemSize = attribute.itemSize;
  146. const array = new Float32Array( count * itemSize );
  147. for ( let i = 0, il = count; i < il; i ++ ) {
  148. _color.fromBufferAttribute( attribute, i );
  149. ColorManagement.fromWorkingColorSpace( _color, SRGBColorSpace );
  150. array[ i * itemSize ] = _color.r;
  151. array[ i * itemSize + 1 ] = _color.g;
  152. array[ i * itemSize + 2 ] = _color.b;
  153. if ( itemSize === 4 ) {
  154. array[ i * itemSize + 3 ] = attribute.getW( i );
  155. }
  156. }
  157. return array;
  158. }
  159. // Encoder methods
  160. /**
  161. * Edgebreaker encoding.
  162. *
  163. * @static
  164. * @constant
  165. * @type {number}
  166. * @default 1
  167. */
  168. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  169. /**
  170. * Sequential encoding.
  171. *
  172. * @static
  173. * @constant
  174. * @type {number}
  175. * @default 0
  176. */
  177. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0;
  178. // Geometry type
  179. DRACOExporter.POINT_CLOUD = 0;
  180. DRACOExporter.TRIANGULAR_MESH = 1;
  181. // Attribute type
  182. DRACOExporter.INVALID = - 1;
  183. DRACOExporter.POSITION = 0;
  184. DRACOExporter.NORMAL = 1;
  185. DRACOExporter.COLOR = 2;
  186. DRACOExporter.TEX_COORD = 3;
  187. DRACOExporter.GENERIC = 4;
  188. /**
  189. * Export options of `DRACOExporter`.
  190. *
  191. * @typedef {Object} DRACOExporter~Options
  192. * @property {number} [decodeSpeed=5] - Indicates how to tune the encoder regarding decode speed (0 gives better speed but worst quality).
  193. * @property {number} [encodeSpeed=5] - Indicates how to tune the encoder parameters (0 gives better speed but worst quality).
  194. * @property {number} [encoderMethod=1] - Either sequential (very little compression) or Edgebreaker. Edgebreaker traverses the triangles of the mesh in a deterministic, spiral-like way which provides most of the benefits of this data format.
  195. * @property {Array<number>} [quantization=[ 16, 8, 8, 8, 8 ]] - Indicates the precision of each type of data stored in the draco file in the order (POSITION, NORMAL, COLOR, TEX_COORD, GENERIC).
  196. * @property {boolean} [exportUvs=true] - Whether to export UVs or not.
  197. * @property {boolean} [exportNormals=true] - Whether to export normals or not.
  198. * @property {boolean} [exportColor=false] - Whether to export colors or not.
  199. **/
  200. export { DRACOExporter };