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