Skip to content

Commit 7ff21a3

Browse files
authored
Merge pull request #16751 from Mugen87/dev31
JSM: Added module and TS file for DRACOExporter.
2 parents 0820136 + 615bb94 commit 7ff21a3

File tree

5 files changed

+237
-3
lines changed

5 files changed

+237
-3
lines changed

docs/manual/en/introduction/Import-via-modules.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ <h2>Importable Examples</h2>
124124
<li>exporters
125125
<ul>
126126
<li>ColladaExporter</li>
127+
<li>DRACOExporter</li>
127128
<li>GLTFExporter</li>
128129
<li>MMDExporter</li>
129130
<li>OBJExporter</li>

examples/js/exporters/DracoExporter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
/**
42
* Export draco compressed files from threejs geometry objects.
53
*
@@ -17,6 +15,8 @@
1715
* @author tentone
1816
*/
1917

18+
/* global DracoEncoderModule */
19+
2020
THREE.DRACOExporter = function () {};
2121

2222
THREE.DRACOExporter.prototype = {
@@ -78,7 +78,7 @@ THREE.DRACOExporter.prototype = {
7878

7979
} else {
8080

81-
var faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array ) ( vertices.count );
81+
var faces = new ( vertices.count > 65535 ? Uint32Array : Uint16Array )( vertices.count );
8282

8383
for ( var i = 0; i < faces.length; i ++ ) {
8484

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
BufferGeometry,
3+
Geometry
4+
} from '../../../src/Three';
5+
6+
export interface DRACOExporterOptions {
7+
decodeSpeed?: number;
8+
encodeSpeed?: number;
9+
encoderMethod?: number;
10+
quantization?: number[];
11+
exportUvs?: boolean;
12+
exportNormals?: boolean;
13+
exportColor?: boolean;
14+
}
15+
16+
export class DRACOExporter {
17+
constructor();
18+
19+
parse(geometry: BufferGeometry, options: DRACOExporterOptions): Int8Array;
20+
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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 };

utils/modularize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var files = [
4141
{ path: 'effects/StereoEffect.js', dependencies: [], ignoreList: [] },
4242

4343
{ path: 'exporters/ColladaExporter.js', dependencies: [], ignoreList: [] },
44+
{ path: 'exporters/DRACOExporter.js', dependencies: [], ignoreList: [ 'Geometry' ] },
4445
{ path: 'exporters/GLTFExporter.js', dependencies: [], ignoreList: [ 'AnimationClip', 'Camera', 'Geometry', 'Material', 'Mesh', 'Object3D', 'RGBFormat', 'Scenes', 'ShaderMaterial', 'VertexColors' ] },
4546
{ path: 'exporters/MMDExporter.js', dependencies: [], ignoreList: [] },
4647
{ path: 'exporters/OBJExporter.js', dependencies: [], ignoreList: [] },

0 commit comments

Comments
 (0)