|
| 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 | + * @class DRACOExporter |
| 15 | + * @author tentone |
| 16 | + */ |
| 17 | + |
| 18 | +import { |
| 19 | + BufferGeometry |
| 20 | +} from "../../../build/three.module.js"; |
| 21 | + |
| 22 | +/* global DracoEncoderModule */ |
| 23 | + |
| 24 | +var DRACOExporter = function () {}; |
| 25 | + |
| 26 | +DRACOExporter.prototype = { |
| 27 | + |
| 28 | + constructor: DRACOExporter, |
| 29 | + |
| 30 | + parse: function ( geometry, options ) { |
| 31 | + |
| 32 | + |
| 33 | + if ( DracoEncoderModule === undefined ) { |
| 34 | + |
| 35 | + throw new Error( 'THREE.DRACOExporter: required the draco_decoder to work.' ); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + if ( options === undefined ) { |
| 40 | + |
| 41 | + options = { |
| 42 | + |
| 43 | + decodeSpeed: 5, |
| 44 | + encodeSpeed: 5, |
| 45 | + encoderMethod: DRACOExporter.MESH_EDGEBREAKER_ENCODING, |
| 46 | + quantization: [ 16, 8, 8, 8, 8 ], |
| 47 | + exportUvs: true, |
| 48 | + exportNormals: true, |
| 49 | + exportColor: false, |
| 50 | + |
| 51 | + }; |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + var dracoEncoder = DracoEncoderModule(); |
| 56 | + var encoder = new dracoEncoder.Encoder(); |
| 57 | + var builder = new dracoEncoder.MeshBuilder(); |
| 58 | + var mesh = new dracoEncoder.Mesh(); |
| 59 | + |
| 60 | + if ( geometry.isGeometry === true ) { |
| 61 | + |
| 62 | + var bufferGeometry = new BufferGeometry(); |
| 63 | + bufferGeometry.fromGeometry( geometry ); |
| 64 | + geometry = bufferGeometry; |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + if ( geometry.isBufferGeometry !== true ) { |
| 69 | + |
| 70 | + throw new Error( 'THREE.DRACOExporter.parse(geometry, options): geometry is not a THREE.Geometry or BufferGeometry instance.' ); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + var vertices = geometry.getAttribute( 'position' ); |
| 75 | + builder.AddFloatAttributeToMesh( mesh, dracoEncoder.POSITION, vertices.count, vertices.itemSize, vertices.array ); |
| 76 | + |
| 77 | + var faces = geometry.getIndex(); |
| 78 | + |
| 79 | + if ( faces !== null ) { |
| 80 | + |
| 81 | + builder.AddFacesToMesh( mesh, faces.count, faces.array ); |
| 82 | + |
| 83 | + } else { |
| 84 | + |
| 85 | + var faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count ); |
| 86 | + |
| 87 | + for ( var i = 0; i < faces.length; i ++ ) { |
| 88 | + |
| 89 | + faces[ i ] = i; |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + builder.AddFacesToMesh( mesh, vertices.count, faces ); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + if ( options.exportNormals === true ) { |
| 98 | + |
| 99 | + var normals = geometry.getAttribute( 'normal' ); |
| 100 | + |
| 101 | + if ( normals !== undefined ) { |
| 102 | + |
| 103 | + builder.AddFloatAttributeToMesh( mesh, dracoEncoder.NORMAL, normals.count, normals.itemSize, normals.array ); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + if ( options.exportUvs === true ) { |
| 110 | + |
| 111 | + var uvs = geometry.getAttribute( 'uv' ); |
| 112 | + |
| 113 | + if ( uvs !== undefined ) { |
| 114 | + |
| 115 | + builder.AddFloatAttributeToMesh( mesh, dracoEncoder.TEX_COORD, uvs.count, uvs.itemSize, uvs.array ); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + if ( options.exportColor === true ) { |
| 122 | + |
| 123 | + var colors = geometry.getAttribute( 'color' ); |
| 124 | + |
| 125 | + if ( colors !== undefined ) { |
| 126 | + |
| 127 | + builder.AddFloatAttributeToMesh( mesh, dracoEncoder.COLOR, colors.count, colors.itemSize, colors.array ); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + //Compress using draco encoder |
| 134 | + |
| 135 | + var encodedData = new dracoEncoder.DracoInt8Array(); |
| 136 | + |
| 137 | + //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). |
| 138 | + |
| 139 | + encoder.SetSpeedOptions( options.encodeSpeed || 5, options.decodeSpeed || 5 ); |
| 140 | + |
| 141 | + // Sets the desired encoding method for a given geometry. |
| 142 | + |
| 143 | + if ( options.encoderMethod !== undefined ) { |
| 144 | + |
| 145 | + encoder.SetEncodingMethod( options.encoderMethod ); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + // Sets the quantization (number of bits used to represent) compression options for a named attribute. |
| 150 | + // The attribute values will be quantized in a box defined by the maximum extent of the attribute values. |
| 151 | + if ( options.quantization !== undefined ) { |
| 152 | + |
| 153 | + for ( var i = 0; i < 5; i ++ ) { |
| 154 | + |
| 155 | + if ( options.quantization[ i ] !== undefined ) { |
| 156 | + |
| 157 | + encoder.SetAttributeQuantization( i, options.quantization[ i ] ); |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + var length = encoder.EncodeMeshToDracoBuffer( mesh, encodedData ); |
| 166 | + dracoEncoder.destroy( mesh ); |
| 167 | + |
| 168 | + if ( length === 0 ) { |
| 169 | + |
| 170 | + throw new Error( 'THREE.DRACOExporter: Draco encoding failed.' ); |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + //Copy encoded data to buffer. |
| 175 | + var outputData = new Int8Array( new ArrayBuffer( length ) ); |
| 176 | + |
| 177 | + for ( var i = 0; i < length; i ++ ) { |
| 178 | + |
| 179 | + outputData[ i ] = encodedData.GetValue( i ); |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + dracoEncoder.destroy( encodedData ); |
| 184 | + dracoEncoder.destroy( encoder ); |
| 185 | + dracoEncoder.destroy( builder ); |
| 186 | + |
| 187 | + return outputData; |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | +}; |
| 192 | + |
| 193 | +// Encoder methods |
| 194 | + |
| 195 | +DRACOExporter.MESH_EDGEBREAKER_ENCODING = 1; |
| 196 | +DRACOExporter.MESH_SEQUENTIAL_ENCODING = 0; |
| 197 | + |
| 198 | +// Geometry type |
| 199 | + |
| 200 | +DRACOExporter.POINT_CLOUD = 0; |
| 201 | +DRACOExporter.TRIANGULAR_MESH = 1; |
| 202 | + |
| 203 | +// Attribute type |
| 204 | + |
| 205 | +DRACOExporter.INVALID = - 1; |
| 206 | +DRACOExporter.POSITION = 0; |
| 207 | +DRACOExporter.NORMAL = 1; |
| 208 | +DRACOExporter.COLOR = 2; |
| 209 | +DRACOExporter.TEX_COORD = 3; |
| 210 | +DRACOExporter.GENERIC = 4; |
| 211 | + |
| 212 | +export { DRACOExporter }; |
0 commit comments