DRACOExporter.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. class DRACOExporter {
  22. /**
  23. * Parses the given mesh or point cloud and generates the Draco output.
  24. *
  25. * @param {(Mesh|Points)} object - The mesh or point cloud to export.
  26. * @param {DRACOExporter~Options} options - The export options.
  27. * @return {ArrayBuffer} The exported Draco.
  28. */
  29. parse( object, options = {} ) {
  30. options = Object.assign( {
  31. decodeSpeed: 5,
  32. encodeSpeed: 5,
  33. encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING,
  34. quantization: [ 16, 8, 8, 8, 8 ],
  35. exportUvs: true,
  36. exportNormals: true,
  37. exportColor: false,
  38. }, options );
  39. if ( DracoEncoderModule === undefined ) {
  40. throw new Error( 'THREE.DRACOExporter: required the draco_encoder to work.' );
  41. }
  42. const geometry = object.geometry;
  43. const dracoEncoder = DracoEncoderModule();
  44. const encoder = new dracoEncoder.Encoder();
  45. let builder;
  46. let dracoObject;
  47. if ( object.isMesh === true ) {
  48. builder = new dracoEncoder.MeshBuilder();
  49. dracoObject = new dracoEncoder.Mesh();
  50. const vertices = geometry.getAttribute( 'position' );
  51. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  52. const faces = geometry.getIndex();
  53. if ( faces !== null ) {
  54. builder.AddFacesToMesh( dracoObject, faces.count / 3, faces.array );
  55. } else {
  56. const faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
  57. for ( let i = 0; i < faces.length; i ++ ) {
  58. faces[ i ] = i;
  59. }
  60. builder.AddFacesToMesh( dracoObject, vertices.count, faces );
  61. }
  62. if ( options.exportNormals === true ) {
  63. const normals = geometry.getAttribute( 'normal' );
  64. if ( normals !== undefined ) {
  65. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array );
  66. }
  67. }
  68. if ( options.exportUvs === true ) {
  69. const uvs = geometry.getAttribute( 'uv' );
  70. if ( uvs !== undefined ) {
  71. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array );
  72. }
  73. }
  74. if ( options.exportColor === true ) {
  75. const colors = geometry.getAttribute( 'color' );
  76. if ( colors !== undefined ) {
  77. const array = createVertexColorSRGBArray( colors );
  78. builder.AddFloatAttributeToMesh( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, array );
  79. }
  80. }
  81. } else if ( object.isPoints === true ) {
  82. builder = new dracoEncoder.PointCloudBuilder();
  83. dracoObject = new dracoEncoder.PointCloud();
  84. const vertices = geometry.getAttribute( 'position' );
  85. builder.AddFloatAttribute( dracoObject, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array );
  86. if ( options.exportColor === true ) {
  87. const colors = geometry.getAttribute( 'color' );
  88. if ( colors !== undefined ) {
  89. const array = createVertexColorSRGBArray( colors );
  90. builder.AddFloatAttribute( dracoObject, dracoEncoder.COLOR, colors.count, colors.itemSize, array );
  91. }
  92. }
  93. } else {
  94. throw new Error( 'DRACOExporter: Unsupported object type.' );
  95. }
  96. //Compress using draco encoder
  97. const 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. const encodeSpeed = ( options.encodeSpeed !== undefined ) ? options.encodeSpeed : 5;
  100. const 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 ( let i = 0; i < 5; i ++ ) {
  110. if ( options.quantization[ i ] !== undefined ) {
  111. encoder.SetAttributeQuantization( i, options.quantization[ i ] );
  112. }
  113. }
  114. }
  115. let 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. const outputData = new Int8Array( new ArrayBuffer( length ) );
  127. for ( let 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. function createVertexColorSRGBArray( attribute ) {
  137. // While .drc files do not specify colorspace, the only 'official' tooling
  138. // is PLY and OBJ converters, which use sRGB. We'll assume sRGB is expected
  139. // for .drc files, but note that Draco buffers embedded in glTF files will
  140. // be Linear-sRGB instead.
  141. const _color = new Color();
  142. const count = attribute.count;
  143. const itemSize = attribute.itemSize;
  144. const array = new Float32Array( count * itemSize );
  145. for ( let i = 0, il = count; i < il; i ++ ) {
  146. _color.fromBufferAttribute( attribute, i );
  147. ColorManagement.fromWorkingColorSpace( _color, SRGBColorSpace );
  148. array[ i * itemSize ] = _color.r;
  149. array[ i * itemSize + 1 ] = _color.g;
  150. array[ i * itemSize + 2 ] = _color.b;
  151. if ( itemSize === 4 ) {
  152. array[ i * itemSize + 3 ] = attribute.getW( i );
  153. }
  154. }
  155. return array;
  156. }
  157. // Encoder methods
  158. /**
  159. * Edgebreaker encoding.
  160. *
  161. * @static
  162. * @constant
  163. * @type {number}
  164. * @default 1
  165. */
  166. DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1;
  167. /**
  168. * Sequential encoding.
  169. *
  170. * @static
  171. * @constant
  172. * @type {number}
  173. * @default 0
  174. */
  175. DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0;
  176. // Geometry type
  177. DRACOExporter.POINT_CLOUD = 0;
  178. DRACOExporter.TRIANGULAR_MESH = 1;
  179. // Attribute type
  180. DRACOExporter.INVALID = - 1;
  181. DRACOExporter.POSITION = 0;
  182. DRACOExporter.NORMAL = 1;
  183. DRACOExporter.COLOR = 2;
  184. DRACOExporter.TEX_COORD = 3;
  185. DRACOExporter.GENERIC = 4;
  186. /**
  187. * Export options of `DRACOExporter`.
  188. *
  189. * @typedef {Object} DRACOExporter~Options
  190. * @property {number} [decodeSpeed=5] - Indicates how to tune the encoder regarding decode speed (0 gives better speed but worst quality).
  191. * @property {number} [encodeSpeed=5] - Indicates how to tune the encoder parameters (0 gives better speed but worst quality).
  192. * @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.
  193. * @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).
  194. * @property {boolean} [exportUvs=true] - Whether to export UVs or not.
  195. * @property {boolean} [exportNormals=true] - Whether to export normals or not.
  196. * @property {boolean} [exportColor=false] - Whether to export colors or not.
  197. **/
  198. export { DRACOExporter };
粤ICP备19079148号