-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.ts
245 lines (208 loc) · 7.52 KB
/
optimizer.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { Gltf } from "./types";
interface Ref<T> {
get(): T;
set(value: T): T;
}
const memberRef = <O, K extends keyof O>(o: O, k: K): Ref<O[K]> => ({
get: () => o[k],
set: (v) => (o[k] = v),
});
const maybeMemberRef = <T, K extends keyof any, O extends Partial<Record<K, T>>>(
o: O | undefined,
k: K
): Ref<O[K] | undefined> => ({
get: () => o?.[k],
set: (v) => (o ? (o[k] = v!) : v),
});
const collectUnused = <T>(
arrayRef: Ref<T[] | undefined>,
indexRefs: () => Iterable<Ref<number | undefined>>
): void => {
const array = arrayRef.get();
if (!array) return;
const used = new Set<number>();
for (const ref of indexRefs()) {
const index = ref.get();
if (index !== undefined) used.add(index);
}
if (used.size === 0) {
arrayRef.set(undefined);
return;
}
const indexMap = new Map<number, number>();
let i = 0;
for (const u of used) indexMap.set(u, i++);
for (const ref of indexRefs()) {
const index = ref.get();
if (index !== undefined) ref.set(indexMap.get(index));
}
const newArray = array.slice(0, indexMap.size);
for (const [oldIndex, newIndex] of indexMap) {
newArray[newIndex] = array[oldIndex];
}
arrayRef.set(newArray);
};
const dedup = <T, H>(
arrayRef: Ref<T[] | undefined>,
hashFn: (t: T) => H,
indexRefs: () => Iterable<Ref<number | undefined>>
): void => {
const array = arrayRef.get();
if (!array) return;
const indexMap = new Map<number, number>();
const hashMap = new Map<H, number>();
for (const [index, value] of array.entries()) {
const hash = hashFn(value);
const existingIndex = hashMap.get(hash);
if (existingIndex !== undefined) {
indexMap.set(index, existingIndex);
} else {
hashMap.set(hash, index);
}
}
for (const ref of indexRefs()) {
const index = ref.get();
if (index !== undefined) ref.set(indexMap.get(index) ?? index);
}
collectUnused(arrayRef, indexRefs);
};
export class GltfOptimizer {
constructor(readonly file: Gltf.File) {}
private *_textureIndexRefs(): Iterable<Ref<Gltf.Index<Gltf.Texture>>> {
for (const m of this.file.materials ?? []) {
if (m.emissiveTexture) yield memberRef(m.emissiveTexture, "index");
if (m.normalTexture) yield memberRef(m.normalTexture, "index");
if (m.occlusionTexture) yield memberRef(m.occlusionTexture, "index");
const pbr = m.pbrMetallicRoughness;
if (pbr?.baseColorTexture) yield memberRef(pbr.baseColorTexture, "index");
if (pbr?.metallicTexture) yield memberRef(pbr.metallicTexture, "index");
if (pbr?.roughnessTexture) yield memberRef(pbr.roughnessTexture, "index");
}
}
private *_samplerIndexRefs(): Iterable<Ref<Gltf.Index<Gltf.Sampler> | undefined>> {
for (const t of this.file.textures ?? []) {
yield memberRef(t, "sampler");
}
}
private *_imageIndexRefs(): Iterable<Ref<Gltf.Index<Gltf.Image> | undefined>> {
for (const t of this.file.textures ?? []) {
yield memberRef(t, "source");
}
}
private *_accessorIndexRefs(): Iterable<Ref<Gltf.Index<Gltf.Accessor> | undefined>> {
for (const m of this.file.meshes ?? []) {
for (const p of m.primitives) {
yield memberRef(p, "indices");
for (const attr in p.attributes) yield memberRef(p.attributes, attr as any);
}
}
for (const s of this.file.skins ?? []) {
yield memberRef(s, "inverseBindMatrices");
}
for (const a of this.file.animations ?? []) {
for (const s of a.samplers ?? []) {
yield memberRef(s, "input");
yield memberRef(s, "output");
}
}
}
private *_bufferViewIndexRefs(): Iterable<Ref<Gltf.Index<Gltf.BufferView> | undefined>> {
for (const a of this.file.accessors ?? []) {
yield memberRef(a, "bufferView");
}
for (const im of this.file.images ?? []) {
yield memberRef(im, "bufferView");
}
}
private *_bufferIndexRefs(): Iterable<Ref<Gltf.Index<Gltf.Buffer> | undefined>> {
for (const bv of this.file.bufferViews ?? []) {
yield memberRef(bv, "buffer");
}
}
private _collectUnusedTexCoords(): void {
for (const mesh of this.file.meshes ?? []) {
for (const prim of mesh.primitives) {
const mat = prim.material ? this.file.materials?.[prim.material] : undefined;
const refs = [
maybeMemberRef(mat?.emissiveTexture, "texCoord"),
maybeMemberRef(mat?.normalTexture, "texCoord"),
maybeMemberRef(mat?.occlusionTexture, "texCoord"),
maybeMemberRef(mat?.pbrMetallicRoughness?.baseColorTexture, "texCoord"),
maybeMemberRef(mat?.pbrMetallicRoughness?.metallicTexture, "texCoord"),
maybeMemberRef(mat?.pbrMetallicRoughness?.roughnessTexture, "texCoord"),
];
const used = new Set<number>();
for (const ref of refs) {
const index = ref.get();
if (index !== undefined) used.add(index);
}
const indexMap = new Map<number, number>();
let i = 0;
for (const u of used) indexMap.set(u, i++);
for (const ref of refs) {
const index = ref.get();
if (index !== undefined) ref.set(indexMap.get(index));
}
for (const [oldIndex, newIndex] of indexMap) {
prim.attributes[`TEXCOORD_${newIndex}`] = prim.attributes[`TEXCOORD_${oldIndex}`];
}
for (let i = used.size; `TEXCOORD_${i}` in prim.attributes; i++) {
delete prim.attributes[`TEXCOORD_${i}`];
}
}
}
}
collectUnused(targets: {
textures: boolean;
samplers: boolean;
images: boolean;
texCoords: boolean;
accessors: boolean;
bufferViews: boolean;
buffers: boolean;
}): void {
// the order is important: top-to-bottom
/* prettier-ignore */ {
if (targets.textures)
collectUnused(memberRef(this.file, "textures"), () => this._textureIndexRefs());
if (targets.samplers)
collectUnused(memberRef(this.file, "samplers"), () => this._samplerIndexRefs());
if (targets.images)
collectUnused(memberRef(this.file, "images"), () => this._imageIndexRefs());
if (targets.texCoords)
this._collectUnusedTexCoords();
if (targets.accessors)
collectUnused(memberRef(this.file, "accessors"), () => this._accessorIndexRefs());
if (targets.bufferViews)
collectUnused(memberRef(this.file, "bufferViews"), () => this._bufferViewIndexRefs());
if (targets.buffers)
collectUnused(memberRef(this.file, "buffers"), () => this._bufferIndexRefs());
}
}
deduplicate(targets: {
buffers: boolean;
bufferViews: boolean;
accessors: boolean;
images: boolean;
samplers: boolean;
textures: boolean;
}): void {
const hashFn = <T extends { name?: string }>({ name, ...rest }: T) => JSON.stringify(rest);
// the order is important: bottom-to-top
// TODO: more optimal hash functions?
/* prettier-ignore */ {
if (targets.buffers)
dedup(memberRef(this.file, "buffers"), hashFn, () => this._bufferIndexRefs());
if (targets.bufferViews)
dedup(memberRef(this.file, "bufferViews"), hashFn, () => this._bufferViewIndexRefs());
if (targets.accessors)
dedup(memberRef(this.file, "accessors"), hashFn, () => this._accessorIndexRefs());
if (targets.images)
dedup(memberRef(this.file, "images"), hashFn, () => this._imageIndexRefs());
if (targets.samplers)
dedup(memberRef(this.file, "samplers"), hashFn, () => this._samplerIndexRefs());
if (targets.textures)
dedup(memberRef(this.file, "textures"), hashFn, () => this._textureIndexRefs());
}
}
}