-
Notifications
You must be signed in to change notification settings - Fork 207
/
atlasing.js
276 lines (245 loc) · 8.21 KB
/
atlasing.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import * as THREE from 'three';
import {MaxRectsPacker} from 'maxrects-packer';
import {modUv} from './util.js';
import {startTextureAtlasSize, maxTextureAtlasSize} from './constants.js';
// const localVector = new THREE.Vector3();
// const localVector2 = new THREE.Vector3();
const localVector2D = new THREE.Vector2();
const localVector2D2 = new THREE.Vector2();
// const localVector4D = new THREE.Vector4();
// const localQuaternion = new THREE.Quaternion();
// const localMatrix = new THREE.Matrix4();
export const mapWarpedUvs = (src, srcOffset, dst, dstOffset, tx, ty, tw, th, canvasSize) => {
const count = src.count;
for (let i = 0; i < count; i++) {
const srcIndex = srcOffset + i * 2;
const localDstOffset = dstOffset + i * 2;
localVector2D.fromArray(src.array, srcIndex);
modUv(localVector2D);
localVector2D
.multiply(
localVector2D2.set(tw/canvasSize, th/canvasSize)
)
.add(
localVector2D2.set(tx/canvasSize, ty/canvasSize)
);
localVector2D.toArray(dst.array, localDstOffset);
}
};
const generateTextureAtlas = textureSpecs => {
const textureNames = Object.keys(textureSpecs);
const firstTextureArray = textureSpecs[textureNames[0]];
// compute texture sizes
const textureSizes = firstTextureArray.map((firstTexture, i) => {
/* const emissiveMap = emissiveMaps[i];
const normalMap = normalMaps[i];
const roughnessMap = roughnessMaps[i];
const metalnessMap = metalnessMaps[i]; */
const maxSize = new THREE.Vector2(0, 0);
for (const textureName of textureNames) {
const map = textureSpecs[textureName][i];
if (map) {
maxSize.x = Math.max(maxSize.x, map.image.width);
maxSize.y = Math.max(maxSize.y, map.image.height);
}
}
return maxSize;
});
const textureUuids = firstTextureArray.map((firstTexture, i) => {
/* const emissiveMap = emissiveMaps[i];
const normalMap = normalMaps[i];
const roughnessMap = roughnessMaps[i];
const metalnessMap = metalnessMaps[i]; */
const uuids = [];
for (const textureName of textureNames) {
const map = textureSpecs[textureName][i];
uuids.push(map ? map.uuid : null);
}
return uuids.join(':');
});
// generate atlas layouts
const _packAtlases = () => {
const _attemptPack = (textureSizes, atlasSize) => {
const maxRectsPacker = new MaxRectsPacker(atlasSize, atlasSize, 0);
const rectUuidCache = new Map();
const rectIndexCache = new Map();
textureSizes.forEach((textureSize, index) => {
const {x: width, y: height} = textureSize;
const hash = textureUuids[index];
let rect = rectUuidCache.get(hash);
if (!rect) {
rect = {
width,
height,
data: {
index,
},
};
rectUuidCache.set(hash, rect);
}
rectIndexCache.set(index, rect);
});
const rects = Array.from(rectUuidCache.values());
maxRectsPacker.addArray(rects);
let oversized = maxRectsPacker.bins.length > 1;
maxRectsPacker.bins.forEach(bin => {
bin.rects.forEach(rect => {
if (rect.oversized) {
oversized = true;
}
});
});
if (!oversized) {
maxRectsPacker.rectIndexCache = rectIndexCache;
return maxRectsPacker;
} else {
return null;
}
};
const hasTextures = textureSizes.some(textureSize => textureSize.x > 0 || textureSize.y > 0);
if (hasTextures) {
let atlas;
let atlasSize = startTextureAtlasSize;
while (!(atlas = _attemptPack(textureSizes, atlasSize))) {
atlasSize *= 2;
}
return atlas;
} else {
return null;
}
};
const atlas = _packAtlases();
// draw atlas images
const _drawAtlasImages = atlas => {
const _getTexturesKey = textures => textures.map(t => t ? t.uuid : '').join(',');
const _drawAtlasImage = (textureName, textures) => {
if (atlas && textures.some(t => t !== null)) {
const canvasSize = Math.min(atlas.width, maxTextureAtlasSize);
const canvasScale = canvasSize / atlas.width;
const canvas = document.createElement('canvas');
canvas.width = canvasSize;
canvas.height = canvasSize;
const initializer = textureInitializers[textureName];
if (initializer) {
initializer(canvas);
}
const ctx = canvas.getContext('2d');
atlas.bins.forEach(bin => {
bin.rects.forEach(rect => {
const {x, y, width: w, height: h, data: {index}} = rect;
const texture = textures[index];
if (texture) {
const image = texture.image;
// draw the image in the correct box on the canvas
const tx = x * canvasScale;
const ty = y * canvasScale;
const tw = w * canvasScale;
const th = h * canvasScale;
ctx.drawImage(image, 0, 0, image.width, image.height, tx, ty, tw, th);
}
});
});
return canvas;
} else {
return null;
}
};
const atlasImages = {};
const atlasImagesMap = new Map(); // cache to alias identical textures
for (const textureName of textureNames) {
const textures = textureSpecs[textureName];
const key = _getTexturesKey(textures);
// const textureName2 = textureName.replace(/s$/, '');
let atlasImage = atlasImagesMap.get(key);
if (atlasImage === undefined) { // cache miss
atlasImage = _drawAtlasImage(textureName, textures);
if (atlasImage !== null) {
atlasImage.key = key;
}
atlasImagesMap.set(key, atlasImage);
}
atlasImages[textureName] = atlasImage;
}
return atlasImages;
};
const atlasImages = _drawAtlasImages(atlas);
const atlasTextures = {};
for (const textureName of textureNames) {
const atlasImage = atlasImages[textureName];
if (atlasImage) {
const texture = new THREE.Texture(atlasImage);
texture.flipY = false;
texture.encoding = THREE.sRGBEncoding;
texture.needsUpdate = true;
atlasTextures[textureName] = texture;
}
}
return {
atlas,
atlasImages,
atlasTextures,
};
};
/* const textureTypes = [
'map',
'normalMap',
'roughnessMap',
'metalnessMap',
'emissiveMap',
]; */
const _colorCanvas = (canvas, fillStyle) => {
const ctx = canvas.getContext('2d');
ctx.fillStyle = fillStyle;
ctx.fillRect(0, 0, canvas.width, canvas.height);
};
const textureInitializers = {
normalMap(canvas) {
_colorCanvas(canvas, 'rgb(128, 128, 255)');
},
roughnessMap(canvas) {
_colorCanvas(canvas, 'rgb(255, 255, 255)');
},
/* metalness(canvas) {
_colorCanvas(canvas, 'rgb(0, 0, 0)');
}, */
};
export const createTextureAtlas = (meshes, {
textures = 'map',
attributes = ['position', 'normal', 'uv'],
} = {}) => {
const textureSpecs = {};
for (const textureName of textures) {
textureSpecs[textureName] = meshes.map(mesh => mesh.material[textureName]);
}
const {
atlas,
atlasImages,
atlasTextures,
} = generateTextureAtlas(textureSpecs);
const canvasSize = Math.min(atlas.width, maxTextureAtlasSize);
const canvasScale = canvasSize / atlas.width;
// geometry
const geometries = meshes.map((m, i) => {
const srcGeometry = m.geometry;
const geometry = new THREE.BufferGeometry();
for (const k of attributes) {
const attr = srcGeometry.attributes[k];
geometry.setAttribute(k, attr);
}
geometry.setIndex(srcGeometry.index);
const rect = atlas.rectIndexCache.get(i);
const {x, y, width: w, height: h} = rect;
const tx = x * canvasScale;
const ty = y * canvasScale;
const tw = w * canvasScale;
const th = h * canvasScale;
mapWarpedUvs(geometry.attributes.uv, 0, geometry.attributes.uv, 0, tx, ty, tw, th, canvasSize);
return geometry;
});
return {
atlas,
atlasImages,
atlasTextures,
geometries,
};
};