Skip to content

Commit 107f11b

Browse files
authored
Merge pull request #16040 from tentone/DracoExporter
Google draco exporter (.drc)
2 parents dd924ab + 45ef683 commit 107f11b

File tree

3 files changed

+434
-0
lines changed

3 files changed

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

examples/js/libs/draco/draco_encoder.js

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)