katachi-runtime 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +164 -2
- package/assets.d.ts +81 -0
- package/character.d.ts +31 -0
- package/index.d.ts +5 -0
- package/index.js +1930 -0
- package/loader.d.ts +29 -0
- package/material.d.ts +19 -0
- package/model.d.ts +26 -0
- package/package.json +10 -3
- package/stage.d.ts +49 -0
- package/texture.d.ts +22 -0
- package/vehicle.d.ts +123 -0
package/index.js
ADDED
|
@@ -0,0 +1,1930 @@
|
|
|
1
|
+
// src/model.ts
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
function disposeMesh(object) {
|
|
4
|
+
const mesh = object;
|
|
5
|
+
mesh.geometry?.dispose();
|
|
6
|
+
mesh.skeleton?.dispose();
|
|
7
|
+
}
|
|
8
|
+
function prepareMesh(mesh) {
|
|
9
|
+
mesh.castShadow = !mesh.material.transparent;
|
|
10
|
+
mesh.receiveShadow = true;
|
|
11
|
+
mesh.frustumCulled = false;
|
|
12
|
+
}
|
|
13
|
+
var IDENTITY = new THREE.Matrix4;
|
|
14
|
+
var AT_ORIGIN = 0.0001;
|
|
15
|
+
function jointCentroids(mesh) {
|
|
16
|
+
const { position, skinIndex, skinWeight } = mesh.geometry.attributes;
|
|
17
|
+
const sums = new Map;
|
|
18
|
+
const vertex = new THREE.Vector3;
|
|
19
|
+
for (let index = 0;index < position.count; index++) {
|
|
20
|
+
let joint = 0;
|
|
21
|
+
let weight = -1;
|
|
22
|
+
for (let slot = 0;slot < 4; slot++) {
|
|
23
|
+
if (skinWeight.getComponent(index, slot) <= weight)
|
|
24
|
+
continue;
|
|
25
|
+
weight = skinWeight.getComponent(index, slot);
|
|
26
|
+
joint = skinIndex.getComponent(index, slot);
|
|
27
|
+
}
|
|
28
|
+
const sum = sums.get(joint) ?? { total: new THREE.Vector3, count: 0 };
|
|
29
|
+
sum.total.add(vertex.fromBufferAttribute(position, index));
|
|
30
|
+
sum.count++;
|
|
31
|
+
sums.set(joint, sum);
|
|
32
|
+
}
|
|
33
|
+
return new Map([...sums].map(([joint, { total, count }]) => [joint, total.divideScalar(count)]));
|
|
34
|
+
}
|
|
35
|
+
function placedBind(bone, anchor, rest, centroid) {
|
|
36
|
+
const boneRest = rest.get(bone)?.world;
|
|
37
|
+
const anchorRest = rest.get(anchor)?.world;
|
|
38
|
+
if (!centroid || !boneRest || !anchorRest)
|
|
39
|
+
return null;
|
|
40
|
+
const boneInAnchor = new THREE.Vector3().setFromMatrixPosition(anchorRest.clone().invert().multiply(boneRest));
|
|
41
|
+
return centroid.distanceTo(boneInAnchor) < centroid.length() ? boneRest.clone().invert().multiply(anchorRest) : null;
|
|
42
|
+
}
|
|
43
|
+
function attachModel(model, bones, root, anchor, rest) {
|
|
44
|
+
model.scene.updateMatrixWorld(true);
|
|
45
|
+
const toModel = model.scene.children[0].matrixWorld.clone().invert();
|
|
46
|
+
const attached = [];
|
|
47
|
+
model.scene.traverse((object) => {
|
|
48
|
+
const source = object;
|
|
49
|
+
if (!source.isMesh)
|
|
50
|
+
return;
|
|
51
|
+
if (source.isSkinnedMesh) {
|
|
52
|
+
const mesh = new THREE.SkinnedMesh(source.geometry, source.material);
|
|
53
|
+
const joints = [];
|
|
54
|
+
let centroids;
|
|
55
|
+
const inverses = source.skeleton.bones.map((bone, index) => {
|
|
56
|
+
const known = bones.get(bone.name);
|
|
57
|
+
joints.push(known ?? anchor);
|
|
58
|
+
const inverse = source.skeleton.boneInverses[index];
|
|
59
|
+
if (!known)
|
|
60
|
+
return new THREE.Matrix4().multiplyMatrices(toModel, bone.matrixWorld).multiply(inverse);
|
|
61
|
+
if (!rest || known === anchor || new THREE.Vector3().setFromMatrixPosition(inverse).lengthSq() > AT_ORIGIN)
|
|
62
|
+
return inverse;
|
|
63
|
+
centroids ??= jointCentroids(source);
|
|
64
|
+
return placedBind(known, anchor, rest, centroids.get(index)) ?? inverse;
|
|
65
|
+
});
|
|
66
|
+
mesh.bind(new THREE.Skeleton(joints, inverses), IDENTITY);
|
|
67
|
+
root.add(mesh);
|
|
68
|
+
attached.push(mesh);
|
|
69
|
+
} else {
|
|
70
|
+
const mesh = new THREE.Mesh(source.geometry, source.material);
|
|
71
|
+
anchor.add(mesh);
|
|
72
|
+
attached.push(mesh);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
attached.forEach(prepareMesh);
|
|
76
|
+
return attached;
|
|
77
|
+
}
|
|
78
|
+
function detach(objects) {
|
|
79
|
+
for (const object of objects) {
|
|
80
|
+
object.parent?.remove(object);
|
|
81
|
+
object.skeleton?.dispose();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
class Model {
|
|
86
|
+
name;
|
|
87
|
+
source;
|
|
88
|
+
root = new THREE.Group;
|
|
89
|
+
bones = new Map;
|
|
90
|
+
view = [0.6, 0.35, 0.75];
|
|
91
|
+
constructor(name, source) {
|
|
92
|
+
this.name = name;
|
|
93
|
+
this.source = source;
|
|
94
|
+
this.root.name = name;
|
|
95
|
+
this.root.add(source.scene);
|
|
96
|
+
source.scene.traverse((object) => {
|
|
97
|
+
if (object.isMesh)
|
|
98
|
+
prepareMesh(object);
|
|
99
|
+
if (object.name)
|
|
100
|
+
this.bones.set(object.name, object);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
placeAtOrigin() {
|
|
104
|
+
this.root.position.set(0, 0, 0);
|
|
105
|
+
this.root.updateMatrixWorld(true);
|
|
106
|
+
this.root.traverse((object) => object.skeleton?.update());
|
|
107
|
+
const box = new THREE.Box3().setFromObject(this.source.scene, true);
|
|
108
|
+
const center = box.getCenter(new THREE.Vector3);
|
|
109
|
+
const offset = new THREE.Vector3(-center.x, -box.min.y, -center.z);
|
|
110
|
+
this.root.position.copy(offset);
|
|
111
|
+
this.root.updateMatrixWorld(true);
|
|
112
|
+
return box.translate(offset);
|
|
113
|
+
}
|
|
114
|
+
dispose() {
|
|
115
|
+
this.root.traverse(disposeMesh);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/character.ts
|
|
120
|
+
var PROP_ANCHORS = {
|
|
121
|
+
head: "SKEL_Head",
|
|
122
|
+
eyes: "SKEL_Head",
|
|
123
|
+
ears: "SKEL_Head",
|
|
124
|
+
mouth: "SKEL_Head",
|
|
125
|
+
lhand: "SKEL_L_Hand",
|
|
126
|
+
rhand: "SKEL_R_Hand",
|
|
127
|
+
lwrist: "SKEL_L_Forearm",
|
|
128
|
+
rwrist: "SKEL_R_Forearm",
|
|
129
|
+
hip: "SKEL_Pelvis",
|
|
130
|
+
lfoot: "SKEL_L_Foot",
|
|
131
|
+
rfoot: "SKEL_R_Foot"
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
class Character extends Model {
|
|
135
|
+
entry;
|
|
136
|
+
loader;
|
|
137
|
+
view = [0.3, 0.08, 0.95];
|
|
138
|
+
worn = new Map;
|
|
139
|
+
selection = new Map;
|
|
140
|
+
revisions = new Map;
|
|
141
|
+
constructor(name, entry, skeleton, loader) {
|
|
142
|
+
super(name, skeleton);
|
|
143
|
+
this.entry = entry;
|
|
144
|
+
this.loader = loader;
|
|
145
|
+
}
|
|
146
|
+
static async load(name, entry, loader) {
|
|
147
|
+
if (!entry.skeleton)
|
|
148
|
+
throw new Error(`${name} has no skeleton: pass its .yft as an input or use --skeleton`);
|
|
149
|
+
const character = new Character(name, entry, await loader.load(entry.skeleton), loader);
|
|
150
|
+
await character.wearDefaults();
|
|
151
|
+
return character;
|
|
152
|
+
}
|
|
153
|
+
selected(slot) {
|
|
154
|
+
return this.selection.get(slot);
|
|
155
|
+
}
|
|
156
|
+
setComponent(component, piece, variant) {
|
|
157
|
+
return this.wear(component, piece, variant, this.source.scene);
|
|
158
|
+
}
|
|
159
|
+
setProp(component, piece, variant) {
|
|
160
|
+
return this.wear(`p_${component}`, piece, variant, this.bones.get(PROP_ANCHORS[component] ?? "SKEL_Head") ?? this.source.scene);
|
|
161
|
+
}
|
|
162
|
+
dispose() {
|
|
163
|
+
for (const objects of this.worn.values())
|
|
164
|
+
detach(objects);
|
|
165
|
+
this.worn.clear();
|
|
166
|
+
super.dispose();
|
|
167
|
+
}
|
|
168
|
+
async wearDefaults() {
|
|
169
|
+
const first = new Map;
|
|
170
|
+
for (const piece of this.entry.components) {
|
|
171
|
+
const current = first.get(piece.component);
|
|
172
|
+
if (!current || piece.index < current.index)
|
|
173
|
+
first.set(piece.component, piece);
|
|
174
|
+
}
|
|
175
|
+
await Promise.all([...first.values()].map((piece) => this.setComponent(piece.component, piece)));
|
|
176
|
+
}
|
|
177
|
+
async wear(slot, piece, variant, anchor) {
|
|
178
|
+
const revision = (this.revisions.get(slot) ?? 0) + 1;
|
|
179
|
+
this.revisions.set(slot, revision);
|
|
180
|
+
if (!piece) {
|
|
181
|
+
detach(this.worn.get(slot) ?? []);
|
|
182
|
+
this.worn.delete(slot);
|
|
183
|
+
this.selection.delete(slot);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const chosen = variant ?? Object.keys(piece.diffuse)[0];
|
|
187
|
+
const model = await this.loader.piece(piece, chosen);
|
|
188
|
+
if (revision !== this.revisions.get(slot))
|
|
189
|
+
return;
|
|
190
|
+
detach(this.worn.get(slot) ?? []);
|
|
191
|
+
this.worn.set(slot, attachModel(model, this.bones, this.source.scene, anchor));
|
|
192
|
+
this.selection.set(slot, { piece, variant: chosen });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// src/loader.ts
|
|
197
|
+
import * as THREE3 from "three";
|
|
198
|
+
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
|
|
199
|
+
|
|
200
|
+
// src/material.ts
|
|
201
|
+
import * as THREE2 from "three";
|
|
202
|
+
var VERTEX_HEADER = `
|
|
203
|
+
#ifdef KATACHI_COLOR
|
|
204
|
+
attribute vec4 color;
|
|
205
|
+
varying vec4 vKatachiColor;
|
|
206
|
+
#endif
|
|
207
|
+
#ifdef KATACHI_UV1
|
|
208
|
+
attribute vec2 uv1;
|
|
209
|
+
#endif
|
|
210
|
+
#ifdef KATACHI_UV2
|
|
211
|
+
attribute vec2 uv2;
|
|
212
|
+
#endif
|
|
213
|
+
varying vec2 vKatachiUv[3];
|
|
214
|
+
`;
|
|
215
|
+
var VERTEX_BODY = `
|
|
216
|
+
#ifdef KATACHI_COLOR
|
|
217
|
+
vKatachiColor = color;
|
|
218
|
+
#endif
|
|
219
|
+
vKatachiUv[0] = uv;
|
|
220
|
+
#ifdef KATACHI_UV1
|
|
221
|
+
vKatachiUv[1] = uv1;
|
|
222
|
+
#else
|
|
223
|
+
vKatachiUv[1] = uv;
|
|
224
|
+
#endif
|
|
225
|
+
#ifdef KATACHI_UV2
|
|
226
|
+
vKatachiUv[2] = uv2;
|
|
227
|
+
#else
|
|
228
|
+
vKatachiUv[2] = uv;
|
|
229
|
+
#endif
|
|
230
|
+
`;
|
|
231
|
+
var FRAGMENT_HEADER = `
|
|
232
|
+
uniform vec3 kPaint;
|
|
233
|
+
uniform vec2 kDiffuseTile;
|
|
234
|
+
uniform vec2 kSpecTile;
|
|
235
|
+
uniform float kSpecIntensity;
|
|
236
|
+
uniform float kSpecFalloff;
|
|
237
|
+
uniform float kSpecFresnel;
|
|
238
|
+
uniform vec3 kSpecMask;
|
|
239
|
+
uniform sampler2D kSecondMap;
|
|
240
|
+
uniform sampler2D kSpecMap;
|
|
241
|
+
#ifdef KATACHI_COLOR
|
|
242
|
+
varying vec4 vKatachiColor;
|
|
243
|
+
#endif
|
|
244
|
+
varying vec2 vKatachiUv[3];
|
|
245
|
+
float kSpecular = 0.0;
|
|
246
|
+
`;
|
|
247
|
+
var FRAGMENT_DIFFUSE = `
|
|
248
|
+
#ifdef USE_MAP
|
|
249
|
+
diffuseColor *= texture2D(map, vKatachiUv[KATACHI_DIFFUSE_SET] * kDiffuseTile);
|
|
250
|
+
#endif
|
|
251
|
+
diffuseColor.rgb *= kPaint;
|
|
252
|
+
#ifdef KATACHI_SECOND_MAP
|
|
253
|
+
vec4 kSecond = texture2D(kSecondMap, vKatachiUv[KATACHI_SECOND_SET]);
|
|
254
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, kSecond.rgb, kSecond.a);
|
|
255
|
+
#endif
|
|
256
|
+
#if defined(KATACHI_COLOR) && defined(KATACHI_DECAL)
|
|
257
|
+
diffuseColor.a *= vKatachiColor.a;
|
|
258
|
+
#endif
|
|
259
|
+
`;
|
|
260
|
+
var FRAGMENT_ROUGHNESS = `
|
|
261
|
+
#ifdef KATACHI_SPEC_MAP
|
|
262
|
+
vec4 kSpec = texture2D(kSpecMap, vKatachiUv[KATACHI_SPEC_SET] * kSpecTile);
|
|
263
|
+
#else
|
|
264
|
+
vec4 kSpec = vec4(0.1);
|
|
265
|
+
#endif
|
|
266
|
+
kSpec.rg *= kSpec.rg;
|
|
267
|
+
kSpecular = clamp(dot(kSpec.rgb, kSpecMask) * kSpecIntensity, 0.0, 1.0);
|
|
268
|
+
roughnessFactor = clamp(pow(2.0 / (max(kSpec.a * kSpecFalloff, 1.0) + 2.0), 0.25), 0.04, 1.0);
|
|
269
|
+
`;
|
|
270
|
+
var FRAGMENT_NORMAL = `
|
|
271
|
+
#ifdef USE_NORMALMAP_TANGENTSPACE
|
|
272
|
+
vec2 kBump = texture2D(normalMap, vNormalMapUv).rg * 2.0 - 1.0;
|
|
273
|
+
normal = normalize(tbn * vec3(kBump * normalScale, max(sqrt(abs(1.0 - dot(kBump, kBump))), 0.001)));
|
|
274
|
+
#endif
|
|
275
|
+
`;
|
|
276
|
+
var FRAGMENT_SPECULAR = `
|
|
277
|
+
material.specularColor = vec3(kSpecular);
|
|
278
|
+
#ifdef KATACHI_METAL_SPEC
|
|
279
|
+
float kPeak = max(diffuseColor.r, max(diffuseColor.g, diffuseColor.b));
|
|
280
|
+
material.specularColor *= mix(vec3(1.0), diffuseColor.rgb / max(kPeak, 0.001), smoothstep(0.5, 1.0, kSpecular) * step(0.04, kPeak));
|
|
281
|
+
#endif
|
|
282
|
+
material.specularColorBlended = mix(material.specularColor, diffuseColor.rgb, metalnessFactor);
|
|
283
|
+
material.specularF90 = mix(clamp(kSpecular + 1.0 - kSpecFresnel, 0.0, 1.0), 1.0, metalnessFactor);
|
|
284
|
+
`;
|
|
285
|
+
var FRAGMENT_AMBIENT_OCCLUSION = `
|
|
286
|
+
#ifdef KATACHI_COLOR
|
|
287
|
+
reflectedLight.indirectDiffuse *= vKatachiColor.r;
|
|
288
|
+
reflectedLight.indirectSpecular *= vKatachiColor.r;
|
|
289
|
+
#endif
|
|
290
|
+
#ifdef KATACHI_NO_REFLECTION
|
|
291
|
+
reflectedLight.indirectSpecular = vec3(0.0);
|
|
292
|
+
#endif
|
|
293
|
+
`;
|
|
294
|
+
var PAINT_SLOTS = [1, 2, 3, 4, 5, 6, 7];
|
|
295
|
+
var FIXED_PAINT = 5;
|
|
296
|
+
function createPaints() {
|
|
297
|
+
return new Map(PAINT_SLOTS.map((slot) => [slot, { value: slot === FIXED_PAINT ? new THREE2.Color().setRGB(0.5, 0.5, 0.5) : new THREE2.Color(16777215) }]));
|
|
298
|
+
}
|
|
299
|
+
function paintUniform(parameters, paints) {
|
|
300
|
+
const value = parameters.matDiffuseColor;
|
|
301
|
+
const shared = value?.[0] === 2 ? paints.get(Math.round(value[1])) : undefined;
|
|
302
|
+
if (shared)
|
|
303
|
+
return shared;
|
|
304
|
+
return { value: value ? new THREE2.Color().setRGB(value[0], value[1], value[2]) : new THREE2.Color(16777215) };
|
|
305
|
+
}
|
|
306
|
+
var PAINT_UV_SETS = {
|
|
307
|
+
"3": [0, 1, 1],
|
|
308
|
+
"4": [0, 1, 1],
|
|
309
|
+
"8": [1, 1, 1],
|
|
310
|
+
"9": [2, 1, 2]
|
|
311
|
+
};
|
|
312
|
+
function uvSets(name) {
|
|
313
|
+
return PAINT_UV_SETS[/^vehicle_paint(\d)/.exec(name)?.[1] ?? ""] ?? [0, 1, 0];
|
|
314
|
+
}
|
|
315
|
+
function vec2Parameter(parameters, name) {
|
|
316
|
+
const value = parameters[name];
|
|
317
|
+
return value ? new THREE2.Vector2(value[0], value[1]) : new THREE2.Vector2(1, 1);
|
|
318
|
+
}
|
|
319
|
+
function load(library, path, role) {
|
|
320
|
+
return path ? library.load(path, role) : Promise.resolve(null);
|
|
321
|
+
}
|
|
322
|
+
var PAINT_GLOSS = 16;
|
|
323
|
+
var REFLECTIVE = /^vehicle_|reflect|glass|mirror/;
|
|
324
|
+
function missingDiffuseColor(name) {
|
|
325
|
+
if (name.includes("smallspecmap"))
|
|
326
|
+
return 1;
|
|
327
|
+
return name.includes("tyrewall") || name.includes("black") ? 0.018 : 0.18;
|
|
328
|
+
}
|
|
329
|
+
async function buildMaterial(extras, geometry, paints, library) {
|
|
330
|
+
const { textures, parameters, shader: name, bucket, missingDiffuse } = extras;
|
|
331
|
+
const [map, second, spec, bump] = await Promise.all([
|
|
332
|
+
load(library, textures.DiffuseSampler, "color"),
|
|
333
|
+
load(library, textures.DiffuseSampler2, "color"),
|
|
334
|
+
load(library, textures.SpecSampler, "data"),
|
|
335
|
+
load(library, textures.BumpSampler, "data")
|
|
336
|
+
]);
|
|
337
|
+
const material = new THREE2.MeshPhysicalMaterial({ name, map, normalMap: bump });
|
|
338
|
+
if (bump)
|
|
339
|
+
material.normalScale.setScalar(parameters.bumpiness?.[0] ?? 1);
|
|
340
|
+
if (!map && missingDiffuse)
|
|
341
|
+
material.color.setScalar(missingDiffuseColor(missingDiffuse));
|
|
342
|
+
const isGlass = name.startsWith("vehicle_vehglass");
|
|
343
|
+
const isPaint = name.startsWith("vehicle_paint") || name === "vehicle_shuts";
|
|
344
|
+
const isDecal = bucket === 2 || name.startsWith("vehicle_badges") || name.startsWith("vehicle_decal");
|
|
345
|
+
if (isPaint) {
|
|
346
|
+
material.clearcoat = 1;
|
|
347
|
+
material.clearcoatRoughness = 0.02;
|
|
348
|
+
material.metalness = 0.4;
|
|
349
|
+
}
|
|
350
|
+
if (isGlass && !map) {
|
|
351
|
+
material.color.setRGB(0.01, 0.012, 0.014);
|
|
352
|
+
material.opacity = name === "vehicle_vehglass" ? 0.42 : 0.2;
|
|
353
|
+
}
|
|
354
|
+
if (isGlass) {
|
|
355
|
+
material.clearcoat = 1;
|
|
356
|
+
material.clearcoatRoughness = 0.02;
|
|
357
|
+
}
|
|
358
|
+
if (bucket === 1 || isDecal) {
|
|
359
|
+
material.transparent = true;
|
|
360
|
+
material.depthWrite = false;
|
|
361
|
+
}
|
|
362
|
+
if (isDecal) {
|
|
363
|
+
material.visible = map !== null;
|
|
364
|
+
material.polygonOffset = true;
|
|
365
|
+
material.polygonOffsetFactor = -1;
|
|
366
|
+
material.polygonOffsetUnits = -2;
|
|
367
|
+
}
|
|
368
|
+
if (bucket === 3)
|
|
369
|
+
material.alphaTest = 0.33;
|
|
370
|
+
if (name.startsWith("ped_hair"))
|
|
371
|
+
material.side = THREE2.DoubleSide;
|
|
372
|
+
const [diffuseSet, secondSet, specSet] = uvSets(name);
|
|
373
|
+
const defines = material.defines;
|
|
374
|
+
defines.KATACHI_DIFFUSE_SET = diffuseSet;
|
|
375
|
+
defines.KATACHI_SECOND_SET = secondSet;
|
|
376
|
+
defines.KATACHI_SPEC_SET = specSet;
|
|
377
|
+
if (geometry.hasAttribute("uv1"))
|
|
378
|
+
defines.KATACHI_UV1 = "";
|
|
379
|
+
if (geometry.hasAttribute("uv2"))
|
|
380
|
+
defines.KATACHI_UV2 = "";
|
|
381
|
+
if (second)
|
|
382
|
+
defines.KATACHI_SECOND_MAP = "";
|
|
383
|
+
if (spec)
|
|
384
|
+
defines.KATACHI_SPEC_MAP = "";
|
|
385
|
+
if (isDecal)
|
|
386
|
+
defines.KATACHI_DECAL = "";
|
|
387
|
+
if (geometry.hasAttribute("color"))
|
|
388
|
+
defines.KATACHI_COLOR = "";
|
|
389
|
+
if (!REFLECTIVE.test(name))
|
|
390
|
+
defines.KATACHI_NO_REFLECTION = "";
|
|
391
|
+
if (!name.startsWith("vehicle_"))
|
|
392
|
+
defines.KATACHI_METAL_SPEC = "";
|
|
393
|
+
const secondMap = { value: second };
|
|
394
|
+
const uniforms = {
|
|
395
|
+
kPaint: paintUniform(parameters, paints),
|
|
396
|
+
kDiffuseTile: { value: vec2Parameter(parameters, "DiffuseTexTileUV") },
|
|
397
|
+
kSpecTile: { value: vec2Parameter(parameters, "specTexTileUV") },
|
|
398
|
+
kSpecIntensity: { value: parameters.specularIntensityMult?.[0] ?? 0.25 },
|
|
399
|
+
kSpecFalloff: { value: (parameters.specularFalloffMult?.[0] ?? 100) * (isPaint ? PAINT_GLOSS : 1) },
|
|
400
|
+
kSpecFresnel: { value: parameters.specularFresnel?.[0] ?? 0.97 },
|
|
401
|
+
kSpecMask: { value: new THREE2.Vector3(...(parameters.specMapIntMask ?? [1, 0, 0]).slice(0, 3)) },
|
|
402
|
+
kSecondMap: secondMap,
|
|
403
|
+
kSpecMap: { value: spec }
|
|
404
|
+
};
|
|
405
|
+
material.onBeforeCompile = (program) => {
|
|
406
|
+
Object.assign(program.uniforms, uniforms);
|
|
407
|
+
program.vertexShader = program.vertexShader.replace("#include <common>", `#include <common>
|
|
408
|
+
${VERTEX_HEADER}`).replace("#include <begin_vertex>", `#include <begin_vertex>
|
|
409
|
+
${VERTEX_BODY}`);
|
|
410
|
+
program.fragmentShader = program.fragmentShader.replace("#include <common>", `#include <common>
|
|
411
|
+
${FRAGMENT_HEADER}`).replace("#include <map_fragment>", FRAGMENT_DIFFUSE).replace("#include <roughnessmap_fragment>", `#include <roughnessmap_fragment>
|
|
412
|
+
${FRAGMENT_ROUGHNESS}`).replace("#include <normal_fragment_maps>", `#include <normal_fragment_maps>
|
|
413
|
+
${FRAGMENT_NORMAL}`).replace("#include <lights_physical_fragment>", `#include <lights_physical_fragment>
|
|
414
|
+
${FRAGMENT_SPECULAR}`).replace("#include <aomap_fragment>", `#include <aomap_fragment>
|
|
415
|
+
${FRAGMENT_AMBIENT_OCCLUSION}`);
|
|
416
|
+
};
|
|
417
|
+
material.customProgramCacheKey = () => JSON.stringify(defines);
|
|
418
|
+
material.userData = { extras, secondMap };
|
|
419
|
+
return material;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// src/texture.ts
|
|
423
|
+
var IDENTIFIER = [171, 75, 84, 88, 32, 50, 48, 187, 13, 10, 26, 10];
|
|
424
|
+
function parseKtx2(buffer) {
|
|
425
|
+
if (buffer.byteLength < 80)
|
|
426
|
+
throw new Error("KTX2: file too small");
|
|
427
|
+
const bytes = new Uint8Array(buffer);
|
|
428
|
+
for (let i = 0;i < IDENTIFIER.length; i++) {
|
|
429
|
+
if (bytes[i] !== IDENTIFIER[i])
|
|
430
|
+
throw new Error("KTX2: bad identifier");
|
|
431
|
+
}
|
|
432
|
+
const view = new DataView(buffer);
|
|
433
|
+
const format = view.getUint32(12, true);
|
|
434
|
+
const width = view.getUint32(20, true);
|
|
435
|
+
const height = view.getUint32(24, true);
|
|
436
|
+
const levelCount = Math.max(view.getUint32(40, true), 1);
|
|
437
|
+
if (view.getUint32(44, true) !== 0)
|
|
438
|
+
throw new Error("KTX2: supercompression is not supported");
|
|
439
|
+
const levels = new Array(levelCount);
|
|
440
|
+
for (let i = 0;i < levelCount; i++) {
|
|
441
|
+
const entry = 80 + i * 24;
|
|
442
|
+
const offset = Number(view.getBigUint64(entry, true));
|
|
443
|
+
const length = Number(view.getBigUint64(entry + 8, true));
|
|
444
|
+
if (offset + length > buffer.byteLength)
|
|
445
|
+
throw new Error("KTX2: level out of bounds");
|
|
446
|
+
levels[i] = {
|
|
447
|
+
data: new Uint8Array(buffer, offset, length),
|
|
448
|
+
width: Math.max(width >> i, 1),
|
|
449
|
+
height: Math.max(height >> i, 1)
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
return { format, width, height, levels };
|
|
453
|
+
}
|
|
454
|
+
var palette = new Uint8Array(16);
|
|
455
|
+
var alphaPalette = new Uint8Array(8);
|
|
456
|
+
function expandColor(c, out, at) {
|
|
457
|
+
const r = c >> 11 & 31;
|
|
458
|
+
const g = c >> 5 & 63;
|
|
459
|
+
const b = c & 31;
|
|
460
|
+
out[at] = r << 3 | r >> 2;
|
|
461
|
+
out[at + 1] = g << 2 | g >> 4;
|
|
462
|
+
out[at + 2] = b << 3 | b >> 2;
|
|
463
|
+
out[at + 3] = 255;
|
|
464
|
+
}
|
|
465
|
+
function colorBlock(src, at, dst, dx, dy, w, h, forceFour) {
|
|
466
|
+
const c0 = src[at] | src[at + 1] << 8;
|
|
467
|
+
const c1 = src[at + 2] | src[at + 3] << 8;
|
|
468
|
+
expandColor(c0, palette, 0);
|
|
469
|
+
expandColor(c1, palette, 4);
|
|
470
|
+
if (c0 > c1 || forceFour) {
|
|
471
|
+
for (let i = 0;i < 3; i++) {
|
|
472
|
+
palette[8 + i] = (2 * palette[i] + palette[4 + i]) / 3;
|
|
473
|
+
palette[12 + i] = (palette[i] + 2 * palette[4 + i]) / 3;
|
|
474
|
+
}
|
|
475
|
+
palette[11] = 255;
|
|
476
|
+
palette[15] = 255;
|
|
477
|
+
} else {
|
|
478
|
+
for (let i = 0;i < 3; i++) {
|
|
479
|
+
palette[8 + i] = palette[i] + palette[4 + i] >> 1;
|
|
480
|
+
palette[12 + i] = 0;
|
|
481
|
+
}
|
|
482
|
+
palette[11] = 255;
|
|
483
|
+
palette[15] = 0;
|
|
484
|
+
}
|
|
485
|
+
const bits = src[at + 4] | src[at + 5] << 8 | src[at + 6] << 16 | src[at + 7] << 24;
|
|
486
|
+
for (let y = 0;y < 4; y++) {
|
|
487
|
+
if (dy + y >= h)
|
|
488
|
+
break;
|
|
489
|
+
for (let x = 0;x < 4; x++) {
|
|
490
|
+
if (dx + x >= w)
|
|
491
|
+
continue;
|
|
492
|
+
const index = bits >>> (y * 4 + x) * 2 & 3;
|
|
493
|
+
const out = ((dy + y) * w + dx + x) * 4;
|
|
494
|
+
dst[out] = palette[index * 4];
|
|
495
|
+
dst[out + 1] = palette[index * 4 + 1];
|
|
496
|
+
dst[out + 2] = palette[index * 4 + 2];
|
|
497
|
+
dst[out + 3] = palette[index * 4 + 3];
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
function channelBlock(src, at, dst, channel, dx, dy, w, h) {
|
|
502
|
+
const a0 = src[at];
|
|
503
|
+
const a1 = src[at + 1];
|
|
504
|
+
alphaPalette[0] = a0;
|
|
505
|
+
alphaPalette[1] = a1;
|
|
506
|
+
if (a0 > a1) {
|
|
507
|
+
for (let i = 1;i <= 6; i++)
|
|
508
|
+
alphaPalette[1 + i] = ((7 - i) * a0 + i * a1) / 7;
|
|
509
|
+
} else {
|
|
510
|
+
for (let i = 1;i <= 4; i++)
|
|
511
|
+
alphaPalette[1 + i] = ((5 - i) * a0 + i * a1) / 5;
|
|
512
|
+
alphaPalette[6] = 0;
|
|
513
|
+
alphaPalette[7] = 255;
|
|
514
|
+
}
|
|
515
|
+
const low = src[at + 2] | src[at + 3] << 8 | src[at + 4] << 16;
|
|
516
|
+
const high = src[at + 5] | src[at + 6] << 8 | src[at + 7] << 16;
|
|
517
|
+
for (let y = 0;y < 4; y++) {
|
|
518
|
+
if (dy + y >= h)
|
|
519
|
+
break;
|
|
520
|
+
const row = y < 2 ? low : high;
|
|
521
|
+
const rowShift = y % 2 * 12;
|
|
522
|
+
for (let x = 0;x < 4; x++) {
|
|
523
|
+
if (dx + x >= w)
|
|
524
|
+
continue;
|
|
525
|
+
const index = row >>> rowShift + x * 3 & 7;
|
|
526
|
+
dst[((dy + y) * w + dx + x) * 4 + channel] = alphaPalette[index];
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
function explicitAlphaBlock(src, at, dst, dx, dy, w, h) {
|
|
531
|
+
for (let y = 0;y < 4; y++) {
|
|
532
|
+
if (dy + y >= h)
|
|
533
|
+
break;
|
|
534
|
+
const row = src[at + y * 2] | src[at + y * 2 + 1] << 8;
|
|
535
|
+
for (let x = 0;x < 4; x++) {
|
|
536
|
+
if (dx + x >= w)
|
|
537
|
+
continue;
|
|
538
|
+
const value = row >> x * 4 & 15;
|
|
539
|
+
dst[((dy + y) * w + dx + x) * 4 + 3] = value << 4 | value;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
var BC7_PARTITION2 = new Uint16Array([
|
|
544
|
+
52428,
|
|
545
|
+
34952,
|
|
546
|
+
61166,
|
|
547
|
+
60616,
|
|
548
|
+
51328,
|
|
549
|
+
65260,
|
|
550
|
+
65224,
|
|
551
|
+
60544,
|
|
552
|
+
51200,
|
|
553
|
+
65516,
|
|
554
|
+
65152,
|
|
555
|
+
59392,
|
|
556
|
+
65512,
|
|
557
|
+
65280,
|
|
558
|
+
65520,
|
|
559
|
+
61440,
|
|
560
|
+
63248,
|
|
561
|
+
142,
|
|
562
|
+
28928,
|
|
563
|
+
2254,
|
|
564
|
+
140,
|
|
565
|
+
29456,
|
|
566
|
+
12544,
|
|
567
|
+
36046,
|
|
568
|
+
2188,
|
|
569
|
+
12560,
|
|
570
|
+
26214,
|
|
571
|
+
13932,
|
|
572
|
+
6120,
|
|
573
|
+
4080,
|
|
574
|
+
29070,
|
|
575
|
+
14748,
|
|
576
|
+
43690,
|
|
577
|
+
61680,
|
|
578
|
+
23130,
|
|
579
|
+
13260,
|
|
580
|
+
15420,
|
|
581
|
+
21930,
|
|
582
|
+
38550,
|
|
583
|
+
42330,
|
|
584
|
+
29646,
|
|
585
|
+
5064,
|
|
586
|
+
12876,
|
|
587
|
+
15324,
|
|
588
|
+
27030,
|
|
589
|
+
49980,
|
|
590
|
+
39270,
|
|
591
|
+
1632,
|
|
592
|
+
626,
|
|
593
|
+
1252,
|
|
594
|
+
20032,
|
|
595
|
+
10016,
|
|
596
|
+
51510,
|
|
597
|
+
37740,
|
|
598
|
+
14790,
|
|
599
|
+
25500,
|
|
600
|
+
37686,
|
|
601
|
+
40134,
|
|
602
|
+
33150,
|
|
603
|
+
59160,
|
|
604
|
+
52464,
|
|
605
|
+
4044,
|
|
606
|
+
30532,
|
|
607
|
+
60962
|
|
608
|
+
]);
|
|
609
|
+
var BC7_PARTITION3 = new Uint32Array([
|
|
610
|
+
2858963024,
|
|
611
|
+
1784303680,
|
|
612
|
+
1515864576,
|
|
613
|
+
1414570152,
|
|
614
|
+
2779054080,
|
|
615
|
+
2694860880,
|
|
616
|
+
1431675040,
|
|
617
|
+
1515868240,
|
|
618
|
+
2857697280,
|
|
619
|
+
2857719040,
|
|
620
|
+
2863289600,
|
|
621
|
+
2425393296,
|
|
622
|
+
2492765332,
|
|
623
|
+
2762253476,
|
|
624
|
+
2846200912,
|
|
625
|
+
705315408,
|
|
626
|
+
2777960512,
|
|
627
|
+
172118100,
|
|
628
|
+
2779096320,
|
|
629
|
+
1436590240,
|
|
630
|
+
2829603924,
|
|
631
|
+
1785348160,
|
|
632
|
+
2762231808,
|
|
633
|
+
437912832,
|
|
634
|
+
5285028,
|
|
635
|
+
2862977168,
|
|
636
|
+
342452500,
|
|
637
|
+
1768494080,
|
|
638
|
+
2693105056,
|
|
639
|
+
2860651540,
|
|
640
|
+
1352967248,
|
|
641
|
+
1784283648,
|
|
642
|
+
2846195712,
|
|
643
|
+
1351655592,
|
|
644
|
+
2829094992,
|
|
645
|
+
606348324,
|
|
646
|
+
11162880,
|
|
647
|
+
613566756,
|
|
648
|
+
608801316,
|
|
649
|
+
1352993360,
|
|
650
|
+
1342874960,
|
|
651
|
+
2863285316,
|
|
652
|
+
1717960704,
|
|
653
|
+
2778768800,
|
|
654
|
+
1352683680,
|
|
655
|
+
1764256040,
|
|
656
|
+
1152035396,
|
|
657
|
+
1717986816,
|
|
658
|
+
2856600644,
|
|
659
|
+
1420317864,
|
|
660
|
+
2508232064,
|
|
661
|
+
2526451200,
|
|
662
|
+
2824098984,
|
|
663
|
+
2157286784,
|
|
664
|
+
2853442580,
|
|
665
|
+
2526412800,
|
|
666
|
+
2863272980,
|
|
667
|
+
2689618080,
|
|
668
|
+
2695210400,
|
|
669
|
+
2516582400,
|
|
670
|
+
1082146944,
|
|
671
|
+
2846402984,
|
|
672
|
+
2863311428,
|
|
673
|
+
709513812
|
|
674
|
+
]);
|
|
675
|
+
var BC7_ANCHOR2 = new Uint8Array([
|
|
676
|
+
15,
|
|
677
|
+
15,
|
|
678
|
+
15,
|
|
679
|
+
15,
|
|
680
|
+
15,
|
|
681
|
+
15,
|
|
682
|
+
15,
|
|
683
|
+
15,
|
|
684
|
+
15,
|
|
685
|
+
15,
|
|
686
|
+
15,
|
|
687
|
+
15,
|
|
688
|
+
15,
|
|
689
|
+
15,
|
|
690
|
+
15,
|
|
691
|
+
15,
|
|
692
|
+
15,
|
|
693
|
+
2,
|
|
694
|
+
8,
|
|
695
|
+
2,
|
|
696
|
+
2,
|
|
697
|
+
8,
|
|
698
|
+
8,
|
|
699
|
+
15,
|
|
700
|
+
2,
|
|
701
|
+
8,
|
|
702
|
+
2,
|
|
703
|
+
2,
|
|
704
|
+
8,
|
|
705
|
+
8,
|
|
706
|
+
2,
|
|
707
|
+
2,
|
|
708
|
+
15,
|
|
709
|
+
15,
|
|
710
|
+
6,
|
|
711
|
+
8,
|
|
712
|
+
2,
|
|
713
|
+
8,
|
|
714
|
+
15,
|
|
715
|
+
15,
|
|
716
|
+
2,
|
|
717
|
+
8,
|
|
718
|
+
2,
|
|
719
|
+
2,
|
|
720
|
+
2,
|
|
721
|
+
15,
|
|
722
|
+
15,
|
|
723
|
+
6,
|
|
724
|
+
6,
|
|
725
|
+
2,
|
|
726
|
+
6,
|
|
727
|
+
8,
|
|
728
|
+
15,
|
|
729
|
+
15,
|
|
730
|
+
2,
|
|
731
|
+
2,
|
|
732
|
+
15,
|
|
733
|
+
15,
|
|
734
|
+
15,
|
|
735
|
+
15,
|
|
736
|
+
15,
|
|
737
|
+
2,
|
|
738
|
+
2,
|
|
739
|
+
15
|
|
740
|
+
]);
|
|
741
|
+
var BC7_ANCHOR3A = new Uint8Array([
|
|
742
|
+
3,
|
|
743
|
+
3,
|
|
744
|
+
15,
|
|
745
|
+
15,
|
|
746
|
+
8,
|
|
747
|
+
3,
|
|
748
|
+
15,
|
|
749
|
+
15,
|
|
750
|
+
8,
|
|
751
|
+
8,
|
|
752
|
+
6,
|
|
753
|
+
6,
|
|
754
|
+
6,
|
|
755
|
+
5,
|
|
756
|
+
3,
|
|
757
|
+
3,
|
|
758
|
+
3,
|
|
759
|
+
3,
|
|
760
|
+
8,
|
|
761
|
+
15,
|
|
762
|
+
3,
|
|
763
|
+
3,
|
|
764
|
+
6,
|
|
765
|
+
10,
|
|
766
|
+
5,
|
|
767
|
+
8,
|
|
768
|
+
8,
|
|
769
|
+
6,
|
|
770
|
+
8,
|
|
771
|
+
5,
|
|
772
|
+
15,
|
|
773
|
+
15,
|
|
774
|
+
8,
|
|
775
|
+
15,
|
|
776
|
+
3,
|
|
777
|
+
5,
|
|
778
|
+
6,
|
|
779
|
+
10,
|
|
780
|
+
8,
|
|
781
|
+
15,
|
|
782
|
+
15,
|
|
783
|
+
3,
|
|
784
|
+
15,
|
|
785
|
+
5,
|
|
786
|
+
15,
|
|
787
|
+
15,
|
|
788
|
+
15,
|
|
789
|
+
15,
|
|
790
|
+
3,
|
|
791
|
+
15,
|
|
792
|
+
5,
|
|
793
|
+
5,
|
|
794
|
+
5,
|
|
795
|
+
8,
|
|
796
|
+
5,
|
|
797
|
+
10,
|
|
798
|
+
5,
|
|
799
|
+
10,
|
|
800
|
+
8,
|
|
801
|
+
13,
|
|
802
|
+
15,
|
|
803
|
+
12,
|
|
804
|
+
3,
|
|
805
|
+
3
|
|
806
|
+
]);
|
|
807
|
+
var BC7_ANCHOR3B = new Uint8Array([
|
|
808
|
+
15,
|
|
809
|
+
8,
|
|
810
|
+
8,
|
|
811
|
+
3,
|
|
812
|
+
15,
|
|
813
|
+
15,
|
|
814
|
+
3,
|
|
815
|
+
8,
|
|
816
|
+
15,
|
|
817
|
+
15,
|
|
818
|
+
15,
|
|
819
|
+
15,
|
|
820
|
+
15,
|
|
821
|
+
15,
|
|
822
|
+
15,
|
|
823
|
+
8,
|
|
824
|
+
15,
|
|
825
|
+
8,
|
|
826
|
+
15,
|
|
827
|
+
3,
|
|
828
|
+
15,
|
|
829
|
+
8,
|
|
830
|
+
15,
|
|
831
|
+
8,
|
|
832
|
+
3,
|
|
833
|
+
15,
|
|
834
|
+
6,
|
|
835
|
+
10,
|
|
836
|
+
15,
|
|
837
|
+
15,
|
|
838
|
+
10,
|
|
839
|
+
8,
|
|
840
|
+
15,
|
|
841
|
+
3,
|
|
842
|
+
15,
|
|
843
|
+
10,
|
|
844
|
+
10,
|
|
845
|
+
8,
|
|
846
|
+
9,
|
|
847
|
+
10,
|
|
848
|
+
6,
|
|
849
|
+
15,
|
|
850
|
+
8,
|
|
851
|
+
15,
|
|
852
|
+
3,
|
|
853
|
+
6,
|
|
854
|
+
6,
|
|
855
|
+
8,
|
|
856
|
+
15,
|
|
857
|
+
3,
|
|
858
|
+
15,
|
|
859
|
+
15,
|
|
860
|
+
15,
|
|
861
|
+
15,
|
|
862
|
+
15,
|
|
863
|
+
15,
|
|
864
|
+
15,
|
|
865
|
+
15,
|
|
866
|
+
15,
|
|
867
|
+
15,
|
|
868
|
+
3,
|
|
869
|
+
15,
|
|
870
|
+
15,
|
|
871
|
+
8
|
|
872
|
+
]);
|
|
873
|
+
var BC7_WEIGHTS2 = [0, 21, 43, 64];
|
|
874
|
+
var BC7_WEIGHTS3 = [0, 9, 18, 27, 37, 46, 55, 64];
|
|
875
|
+
var BC7_WEIGHTS4 = [0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64];
|
|
876
|
+
function weightsFor(bits) {
|
|
877
|
+
return bits === 2 ? BC7_WEIGHTS2 : bits === 3 ? BC7_WEIGHTS3 : BC7_WEIGHTS4;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
class BitReader {
|
|
881
|
+
src;
|
|
882
|
+
at;
|
|
883
|
+
position = 0;
|
|
884
|
+
constructor(src, at) {
|
|
885
|
+
this.src = src;
|
|
886
|
+
this.at = at;
|
|
887
|
+
}
|
|
888
|
+
read(count) {
|
|
889
|
+
let value = 0;
|
|
890
|
+
for (let i = 0;i < count; i++) {
|
|
891
|
+
const bit = this.position + i;
|
|
892
|
+
value |= (this.src[this.at + (bit >> 3)] >> (bit & 7) & 1) << i;
|
|
893
|
+
}
|
|
894
|
+
this.position += count;
|
|
895
|
+
return value;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
var BC7_MODES = [
|
|
899
|
+
{ subsets: 3, partitionBits: 4, rotationBits: 0, indexModeBits: 0, colorBits: 4, alphaBits: 0, endpointP: 1, sharedP: 0, indexBits: 3, index2Bits: 0 },
|
|
900
|
+
{ subsets: 2, partitionBits: 6, rotationBits: 0, indexModeBits: 0, colorBits: 6, alphaBits: 0, endpointP: 0, sharedP: 1, indexBits: 3, index2Bits: 0 },
|
|
901
|
+
{ subsets: 3, partitionBits: 6, rotationBits: 0, indexModeBits: 0, colorBits: 5, alphaBits: 0, endpointP: 0, sharedP: 0, indexBits: 2, index2Bits: 0 },
|
|
902
|
+
{ subsets: 2, partitionBits: 6, rotationBits: 0, indexModeBits: 0, colorBits: 7, alphaBits: 0, endpointP: 1, sharedP: 0, indexBits: 2, index2Bits: 0 },
|
|
903
|
+
{ subsets: 1, partitionBits: 0, rotationBits: 2, indexModeBits: 1, colorBits: 5, alphaBits: 6, endpointP: 0, sharedP: 0, indexBits: 2, index2Bits: 3 },
|
|
904
|
+
{ subsets: 1, partitionBits: 0, rotationBits: 2, indexModeBits: 0, colorBits: 7, alphaBits: 8, endpointP: 0, sharedP: 0, indexBits: 2, index2Bits: 2 },
|
|
905
|
+
{ subsets: 1, partitionBits: 0, rotationBits: 0, indexModeBits: 0, colorBits: 7, alphaBits: 7, endpointP: 1, sharedP: 0, indexBits: 4, index2Bits: 0 },
|
|
906
|
+
{ subsets: 2, partitionBits: 6, rotationBits: 0, indexModeBits: 0, colorBits: 5, alphaBits: 5, endpointP: 1, sharedP: 1, indexBits: 2, index2Bits: 0 }
|
|
907
|
+
];
|
|
908
|
+
function subsetOf(subsets, partition, texel) {
|
|
909
|
+
if (subsets === 1)
|
|
910
|
+
return 0;
|
|
911
|
+
if (subsets === 2)
|
|
912
|
+
return BC7_PARTITION2[partition] >> texel & 1;
|
|
913
|
+
return BC7_PARTITION3[partition] >>> texel * 2 & 3;
|
|
914
|
+
}
|
|
915
|
+
function anchorOf(subsets, partition, subset) {
|
|
916
|
+
if (subset === 0)
|
|
917
|
+
return 0;
|
|
918
|
+
if (subsets === 2)
|
|
919
|
+
return BC7_ANCHOR2[partition];
|
|
920
|
+
return subset === 1 ? BC7_ANCHOR3A[partition] : BC7_ANCHOR3B[partition];
|
|
921
|
+
}
|
|
922
|
+
function bc7Block(src, at, dst, dx, dy, w, h) {
|
|
923
|
+
const reader = new BitReader(src, at);
|
|
924
|
+
let modeIndex = 0;
|
|
925
|
+
while (modeIndex < 8 && reader.read(1) === 0)
|
|
926
|
+
modeIndex++;
|
|
927
|
+
if (modeIndex === 8) {
|
|
928
|
+
for (let y = 0;y < 4 && dy + y < h; y++) {
|
|
929
|
+
for (let x = 0;x < 4 && dx + x < w; x++) {
|
|
930
|
+
const out = ((dy + y) * w + dx + x) * 4;
|
|
931
|
+
dst[out] = dst[out + 1] = dst[out + 2] = dst[out + 3] = 0;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
const mode = BC7_MODES[modeIndex];
|
|
937
|
+
const partition = reader.read(mode.partitionBits);
|
|
938
|
+
const rotation = reader.read(mode.rotationBits);
|
|
939
|
+
const indexMode = reader.read(mode.indexModeBits);
|
|
940
|
+
const endpointCount = mode.subsets * 2;
|
|
941
|
+
const endpoints = Array.from({ length: endpointCount }, () => [0, 0, 0, 255]);
|
|
942
|
+
for (let channel = 0;channel < 3; channel++) {
|
|
943
|
+
for (let e = 0;e < endpointCount; e++)
|
|
944
|
+
endpoints[e][channel] = reader.read(mode.colorBits);
|
|
945
|
+
}
|
|
946
|
+
if (mode.alphaBits > 0) {
|
|
947
|
+
for (let e = 0;e < endpointCount; e++)
|
|
948
|
+
endpoints[e][3] = reader.read(mode.alphaBits);
|
|
949
|
+
}
|
|
950
|
+
const colorPrecision = mode.colorBits + (mode.endpointP || mode.sharedP ? 1 : 0);
|
|
951
|
+
const alphaPrecision = mode.alphaBits > 0 ? mode.alphaBits + (mode.endpointP || mode.sharedP ? 1 : 0) : 0;
|
|
952
|
+
if (mode.endpointP) {
|
|
953
|
+
for (let e = 0;e < endpointCount; e++) {
|
|
954
|
+
const p = reader.read(1);
|
|
955
|
+
for (let c = 0;c < 3; c++)
|
|
956
|
+
endpoints[e][c] = endpoints[e][c] << 1 | p;
|
|
957
|
+
if (mode.alphaBits > 0)
|
|
958
|
+
endpoints[e][3] = endpoints[e][3] << 1 | p;
|
|
959
|
+
}
|
|
960
|
+
} else if (mode.sharedP) {
|
|
961
|
+
for (let s = 0;s < mode.subsets; s++) {
|
|
962
|
+
const p = reader.read(1);
|
|
963
|
+
for (let e = s * 2;e < s * 2 + 2; e++) {
|
|
964
|
+
for (let c = 0;c < 3; c++)
|
|
965
|
+
endpoints[e][c] = endpoints[e][c] << 1 | p;
|
|
966
|
+
if (mode.alphaBits > 0)
|
|
967
|
+
endpoints[e][3] = endpoints[e][3] << 1 | p;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
for (let e = 0;e < endpointCount; e++) {
|
|
972
|
+
for (let c = 0;c < 3; c++) {
|
|
973
|
+
const v = endpoints[e][c] << 8 - colorPrecision;
|
|
974
|
+
endpoints[e][c] = v | v >> colorPrecision;
|
|
975
|
+
}
|
|
976
|
+
if (mode.alphaBits > 0) {
|
|
977
|
+
const v = endpoints[e][3] << 8 - alphaPrecision;
|
|
978
|
+
endpoints[e][3] = v | v >> alphaPrecision;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
const primary = new Uint8Array(16);
|
|
982
|
+
const secondary = new Uint8Array(16);
|
|
983
|
+
for (let texel = 0;texel < 16; texel++) {
|
|
984
|
+
const subset = subsetOf(mode.subsets, partition, texel);
|
|
985
|
+
const bits = texel === anchorOf(mode.subsets, partition, subset) ? mode.indexBits - 1 : mode.indexBits;
|
|
986
|
+
primary[texel] = reader.read(bits);
|
|
987
|
+
}
|
|
988
|
+
if (mode.index2Bits > 0) {
|
|
989
|
+
for (let texel = 0;texel < 16; texel++) {
|
|
990
|
+
secondary[texel] = reader.read(texel === 0 ? mode.index2Bits - 1 : mode.index2Bits);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
const colorWeights = weightsFor(indexMode && mode.index2Bits ? mode.index2Bits : mode.indexBits);
|
|
994
|
+
const alphaWeights = mode.index2Bits ? weightsFor(indexMode ? mode.indexBits : mode.index2Bits) : colorWeights;
|
|
995
|
+
for (let y = 0;y < 4; y++) {
|
|
996
|
+
if (dy + y >= h)
|
|
997
|
+
break;
|
|
998
|
+
for (let x = 0;x < 4; x++) {
|
|
999
|
+
if (dx + x >= w)
|
|
1000
|
+
continue;
|
|
1001
|
+
const texel = y * 4 + x;
|
|
1002
|
+
const subset = subsetOf(mode.subsets, partition, texel);
|
|
1003
|
+
const a = endpoints[subset * 2];
|
|
1004
|
+
const b = endpoints[subset * 2 + 1];
|
|
1005
|
+
const colorIndex = mode.index2Bits && indexMode ? secondary[texel] : primary[texel];
|
|
1006
|
+
const alphaIndex = mode.index2Bits ? indexMode ? primary[texel] : secondary[texel] : primary[texel];
|
|
1007
|
+
const cw = colorWeights[colorIndex];
|
|
1008
|
+
const aw = alphaWeights[alphaIndex];
|
|
1009
|
+
let r = a[0] * (64 - cw) + b[0] * cw + 32 >> 6;
|
|
1010
|
+
let g = a[1] * (64 - cw) + b[1] * cw + 32 >> 6;
|
|
1011
|
+
let bl = a[2] * (64 - cw) + b[2] * cw + 32 >> 6;
|
|
1012
|
+
let al = a[3] * (64 - aw) + b[3] * aw + 32 >> 6;
|
|
1013
|
+
if (rotation === 1)
|
|
1014
|
+
[r, al] = [al, r];
|
|
1015
|
+
else if (rotation === 2)
|
|
1016
|
+
[g, al] = [al, g];
|
|
1017
|
+
else if (rotation === 3)
|
|
1018
|
+
[bl, al] = [al, bl];
|
|
1019
|
+
const out = ((dy + y) * w + dx + x) * 4;
|
|
1020
|
+
dst[out] = r;
|
|
1021
|
+
dst[out + 1] = g;
|
|
1022
|
+
dst[out + 2] = bl;
|
|
1023
|
+
dst[out + 3] = al;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
function blockBytes(format) {
|
|
1028
|
+
return format === 133 /* BC1 */ || format === 139 /* BC4 */ ? 8 : 16;
|
|
1029
|
+
}
|
|
1030
|
+
function decodeBlocks(format, src, width, height) {
|
|
1031
|
+
const dst = new Uint8Array(width * height * 4);
|
|
1032
|
+
const step = blockBytes(format);
|
|
1033
|
+
let at = 0;
|
|
1034
|
+
for (let dy = 0;dy < height; dy += 4) {
|
|
1035
|
+
for (let dx = 0;dx < width; dx += 4) {
|
|
1036
|
+
switch (format) {
|
|
1037
|
+
case 133 /* BC1 */:
|
|
1038
|
+
colorBlock(src, at, dst, dx, dy, width, height, false);
|
|
1039
|
+
break;
|
|
1040
|
+
case 135 /* BC2 */:
|
|
1041
|
+
colorBlock(src, at + 8, dst, dx, dy, width, height, true);
|
|
1042
|
+
explicitAlphaBlock(src, at, dst, dx, dy, width, height);
|
|
1043
|
+
break;
|
|
1044
|
+
case 137 /* BC3 */:
|
|
1045
|
+
colorBlock(src, at + 8, dst, dx, dy, width, height, true);
|
|
1046
|
+
channelBlock(src, at, dst, 3, dx, dy, width, height);
|
|
1047
|
+
break;
|
|
1048
|
+
case 139 /* BC4 */:
|
|
1049
|
+
channelBlock(src, at, dst, 0, dx, dy, width, height);
|
|
1050
|
+
break;
|
|
1051
|
+
case 141 /* BC5 */:
|
|
1052
|
+
channelBlock(src, at, dst, 0, dx, dy, width, height);
|
|
1053
|
+
channelBlock(src, at + 8, dst, 1, dx, dy, width, height);
|
|
1054
|
+
break;
|
|
1055
|
+
case 145 /* BC7 */:
|
|
1056
|
+
bc7Block(src, at, dst, dx, dy, width, height);
|
|
1057
|
+
break;
|
|
1058
|
+
}
|
|
1059
|
+
at += step;
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
if (format === 139 /* BC4 */ || format === 141 /* BC5 */) {
|
|
1063
|
+
for (let i = 0;i < dst.length; i += 4) {
|
|
1064
|
+
if (format === 139 /* BC4 */) {
|
|
1065
|
+
dst[i + 1] = dst[i];
|
|
1066
|
+
dst[i + 2] = dst[i];
|
|
1067
|
+
}
|
|
1068
|
+
dst[i + 3] = 255;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
return dst;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
// src/loader.ts
|
|
1075
|
+
var COMPRESSED = {
|
|
1076
|
+
[133 /* BC1 */]: THREE3.RGBA_S3TC_DXT1_Format,
|
|
1077
|
+
[135 /* BC2 */]: THREE3.RGBA_S3TC_DXT3_Format,
|
|
1078
|
+
[137 /* BC3 */]: THREE3.RGBA_S3TC_DXT5_Format,
|
|
1079
|
+
[139 /* BC4 */]: THREE3.RED_RGTC1_Format,
|
|
1080
|
+
[141 /* BC5 */]: THREE3.RED_GREEN_RGTC2_Format,
|
|
1081
|
+
[145 /* BC7 */]: THREE3.RGBA_BPTC_Format
|
|
1082
|
+
};
|
|
1083
|
+
var probe;
|
|
1084
|
+
function hasExtension(name) {
|
|
1085
|
+
probe ??= document.createElement("canvas").getContext("webgl2");
|
|
1086
|
+
return Boolean(probe?.getExtension(name));
|
|
1087
|
+
}
|
|
1088
|
+
function decodesNatively(image, role) {
|
|
1089
|
+
if (image.width % 4 !== 0 || image.height % 4 !== 0)
|
|
1090
|
+
return false;
|
|
1091
|
+
switch (image.format) {
|
|
1092
|
+
case 133 /* BC1 */:
|
|
1093
|
+
case 135 /* BC2 */:
|
|
1094
|
+
case 137 /* BC3 */:
|
|
1095
|
+
return hasExtension(role === "color" ? "WEBGL_compressed_texture_s3tc_srgb" : "WEBGL_compressed_texture_s3tc");
|
|
1096
|
+
case 139 /* BC4 */:
|
|
1097
|
+
case 141 /* BC5 */:
|
|
1098
|
+
return role === "data" && hasExtension("EXT_texture_compression_rgtc");
|
|
1099
|
+
case 145 /* BC7 */:
|
|
1100
|
+
return hasExtension("EXT_texture_compression_bptc");
|
|
1101
|
+
default:
|
|
1102
|
+
return false;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
function createTexture(image, role) {
|
|
1106
|
+
const compressed = COMPRESSED[image.format];
|
|
1107
|
+
if (compressed !== undefined && decodesNatively(image, role)) {
|
|
1108
|
+
return new THREE3.CompressedTexture(image.levels, image.width, image.height, compressed);
|
|
1109
|
+
}
|
|
1110
|
+
const levels = compressed === undefined ? image.levels : image.levels.map((level) => ({ ...level, data: decodeBlocks(image.format, level.data, level.width, level.height) }));
|
|
1111
|
+
const texture = new THREE3.DataTexture(levels[0].data, levels[0].width, levels[0].height, THREE3.RGBAFormat, THREE3.UnsignedByteType);
|
|
1112
|
+
texture.mipmaps = levels;
|
|
1113
|
+
return texture;
|
|
1114
|
+
}
|
|
1115
|
+
async function fetchBytes(url) {
|
|
1116
|
+
const response = await fetch(url);
|
|
1117
|
+
if (!response.ok)
|
|
1118
|
+
throw new Error(`Failed to load ${url}: ${response.status}`);
|
|
1119
|
+
const bytes = await response.arrayBuffer();
|
|
1120
|
+
const head = new Uint8Array(bytes);
|
|
1121
|
+
if (head[0] !== 31 || head[1] !== 139)
|
|
1122
|
+
return bytes;
|
|
1123
|
+
return new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"))).arrayBuffer();
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
class TextureLibrary {
|
|
1127
|
+
baseUrl;
|
|
1128
|
+
loaded = new Map;
|
|
1129
|
+
constructor(baseUrl) {
|
|
1130
|
+
this.baseUrl = baseUrl;
|
|
1131
|
+
}
|
|
1132
|
+
load(path, role) {
|
|
1133
|
+
const key = `${role}:${path}`;
|
|
1134
|
+
let texture = this.loaded.get(key);
|
|
1135
|
+
if (!texture) {
|
|
1136
|
+
texture = this.fetchTexture(path, role);
|
|
1137
|
+
this.loaded.set(key, texture);
|
|
1138
|
+
}
|
|
1139
|
+
return texture;
|
|
1140
|
+
}
|
|
1141
|
+
clear() {
|
|
1142
|
+
for (const texture of this.loaded.values())
|
|
1143
|
+
texture.then((loaded) => loaded.dispose(), () => {});
|
|
1144
|
+
this.loaded.clear();
|
|
1145
|
+
}
|
|
1146
|
+
async fetchTexture(path, role) {
|
|
1147
|
+
const image = parseKtx2(await fetchBytes(`${this.baseUrl}${path}`));
|
|
1148
|
+
const texture = createTexture(image, role);
|
|
1149
|
+
texture.wrapS = THREE3.RepeatWrapping;
|
|
1150
|
+
texture.wrapT = THREE3.RepeatWrapping;
|
|
1151
|
+
texture.magFilter = THREE3.LinearFilter;
|
|
1152
|
+
texture.minFilter = image.levels.length > 1 ? THREE3.LinearMipmapLinearFilter : THREE3.LinearFilter;
|
|
1153
|
+
texture.anisotropy = 16;
|
|
1154
|
+
texture.colorSpace = role === "color" ? THREE3.SRGBColorSpace : THREE3.NoColorSpace;
|
|
1155
|
+
texture.needsUpdate = true;
|
|
1156
|
+
return texture;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
class ModelLoader {
|
|
1161
|
+
baseUrl;
|
|
1162
|
+
gltf = new GLTFLoader;
|
|
1163
|
+
library;
|
|
1164
|
+
pieces = new Map;
|
|
1165
|
+
sharedPaints = createPaints();
|
|
1166
|
+
constructor(baseUrl) {
|
|
1167
|
+
this.baseUrl = baseUrl;
|
|
1168
|
+
this.library = new TextureLibrary(baseUrl);
|
|
1169
|
+
}
|
|
1170
|
+
texture(path) {
|
|
1171
|
+
return this.library.load(path, "color");
|
|
1172
|
+
}
|
|
1173
|
+
async load(path, paints = this.sharedPaints, diffuse) {
|
|
1174
|
+
const gltf = await this.gltf.parseAsync(await fetchBytes(`${this.baseUrl}${path}`), this.baseUrl);
|
|
1175
|
+
const scene = gltf.scene;
|
|
1176
|
+
const materials = [];
|
|
1177
|
+
const pending = [];
|
|
1178
|
+
scene.traverse((object) => {
|
|
1179
|
+
const mesh = object;
|
|
1180
|
+
if (!mesh.isMesh)
|
|
1181
|
+
return;
|
|
1182
|
+
const source = mesh.material;
|
|
1183
|
+
const raw = source.userData;
|
|
1184
|
+
if (!raw.shader)
|
|
1185
|
+
return;
|
|
1186
|
+
pending.push(buildMaterial(diffuse ? { ...raw, textures: { ...raw.textures, DiffuseSampler: diffuse } } : raw, mesh.geometry, paints, this.library).then((material) => {
|
|
1187
|
+
mesh.material = material;
|
|
1188
|
+
materials.push(material);
|
|
1189
|
+
}));
|
|
1190
|
+
});
|
|
1191
|
+
await Promise.all(pending);
|
|
1192
|
+
return { scene, materials };
|
|
1193
|
+
}
|
|
1194
|
+
clear() {
|
|
1195
|
+
this.library.clear();
|
|
1196
|
+
for (const piece of this.pieces.values())
|
|
1197
|
+
piece.then(({ scene }) => scene.traverse(disposeMesh), () => {});
|
|
1198
|
+
this.pieces.clear();
|
|
1199
|
+
}
|
|
1200
|
+
piece(piece, variant) {
|
|
1201
|
+
const key = `${piece.mesh}:${variant ?? ""}`;
|
|
1202
|
+
let pending = this.pieces.get(key);
|
|
1203
|
+
if (!pending) {
|
|
1204
|
+
pending = this.load(piece.mesh, this.sharedPaints, variant && piece.diffuse[variant]);
|
|
1205
|
+
this.pieces.set(key, pending);
|
|
1206
|
+
}
|
|
1207
|
+
return pending;
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// src/vehicle.ts
|
|
1212
|
+
import * as THREE4 from "three";
|
|
1213
|
+
var GTA_COLORS = "0d11161c1d2132383d454b4f999da0c2c4c6979a9763738063625c3c3f47444e541d212913181f26282a5155541519211e2429333a3c8c909539434d5062721e232f363a3fa0a199d3d3d3b7bfca778794c00e1ada1918b6111ba51e237b1a228e1b1f6f181849111db60f25d44a17c2944ff78616cf1f21732021f27d20ffc91f9c1016de0f188f1e17a94744b16c51371c25132428122e2b12383c31423f155c2d1b677066b81f22383e1d5a3f2d423f45594b65867f222e46233155304c7e47578f637ba7394762d6e7f176afbe345e720b9cf12f2d52282c4d2354a16ea3c61125521b203e2751906085922446a84271e13b39e01f2852253aa71c35514c5f8158688e74b5d8ffcf20fbe212916532e0e13d98d2239b8c78503218473f2b221b19653f23775c3eac99756c6b4b402e2ba4965f46231a752b19bfae7bdfd5b2f7edd53a2a1b785f33b5a079fffff6eaeaeab0ab944538312a282b726c576a747c3541589ba0a85870a1eae6dedfddd0f2ad2ef9a45883c566f1cc404cc3da4e6443bcac8ff8b658fcf9f1fffffb81844cfffffff21f99fdd6cddf5891f6ae20b0ee6e08e9fa0a0c170c0d180e0d149f9e8a6212760b142111141a6b1f7b1e1d22bc19172d362a6967487a6c55c3b4925a635281827fafd6e47a64407f6a48".match(/.{6}/g).map((hex) => `#${hex}`);
|
|
1214
|
+
var WIDTH = 512;
|
|
1215
|
+
var HEIGHT = 256;
|
|
1216
|
+
var FONT = '"Helvetica Neue", Arial, sans-serif';
|
|
1217
|
+
function createPlate() {
|
|
1218
|
+
const canvas = document.createElement("canvas");
|
|
1219
|
+
canvas.width = WIDTH;
|
|
1220
|
+
canvas.height = HEIGHT;
|
|
1221
|
+
const texture = new THREE4.CanvasTexture(canvas);
|
|
1222
|
+
texture.colorSpace = THREE4.SRGBColorSpace;
|
|
1223
|
+
texture.flipY = false;
|
|
1224
|
+
texture.anisotropy = 8;
|
|
1225
|
+
return texture;
|
|
1226
|
+
}
|
|
1227
|
+
function drawPlate(texture, text) {
|
|
1228
|
+
const context = texture.image.getContext("2d");
|
|
1229
|
+
context.fillStyle = "#f2f3f0";
|
|
1230
|
+
context.fillRect(0, 0, WIDTH, HEIGHT);
|
|
1231
|
+
context.fillStyle = "#1c2f7a";
|
|
1232
|
+
context.textAlign = "center";
|
|
1233
|
+
context.textBaseline = "middle";
|
|
1234
|
+
context.font = `600 34px ${FONT}`;
|
|
1235
|
+
context.fillText("SAN ANDREAS", WIDTH / 2, 44);
|
|
1236
|
+
const label = text.toUpperCase().slice(0, 8);
|
|
1237
|
+
context.font = `800 132px ${FONT}`;
|
|
1238
|
+
const fitted = Math.min(132, Math.floor(132 * (WIDTH - 40) / Math.max(context.measureText(label).width, 1)));
|
|
1239
|
+
context.font = `800 ${fitted}px ${FONT}`;
|
|
1240
|
+
context.fillText(label, WIDTH / 2, 156);
|
|
1241
|
+
texture.needsUpdate = true;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
class HiddenBones {
|
|
1245
|
+
bones;
|
|
1246
|
+
counts = new Map;
|
|
1247
|
+
skeletons = new Map;
|
|
1248
|
+
rigid = new Set;
|
|
1249
|
+
constructor(scene, bones) {
|
|
1250
|
+
this.bones = bones;
|
|
1251
|
+
scene.traverse((object) => {
|
|
1252
|
+
const mesh = object;
|
|
1253
|
+
if (!mesh.isMesh)
|
|
1254
|
+
return;
|
|
1255
|
+
if (mesh.isSkinnedMesh)
|
|
1256
|
+
this.skeletons.set(mesh.skeleton, mesh.skeleton.boneInverses.map((inverse) => inverse.clone()));
|
|
1257
|
+
else
|
|
1258
|
+
this.rigid.add(mesh);
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1261
|
+
hide(name) {
|
|
1262
|
+
this.counts.set(name, (this.counts.get(name) ?? 0) + 1);
|
|
1263
|
+
this.apply();
|
|
1264
|
+
}
|
|
1265
|
+
show(name) {
|
|
1266
|
+
const count = (this.counts.get(name) ?? 1) - 1;
|
|
1267
|
+
if (count > 0)
|
|
1268
|
+
this.counts.set(name, count);
|
|
1269
|
+
else
|
|
1270
|
+
this.counts.delete(name);
|
|
1271
|
+
this.apply();
|
|
1272
|
+
}
|
|
1273
|
+
apply() {
|
|
1274
|
+
for (const [skeleton, original] of this.skeletons) {
|
|
1275
|
+
skeleton.boneInverses.forEach((inverse, index) => inverse.copy(original[index]));
|
|
1276
|
+
}
|
|
1277
|
+
for (const mesh of this.rigid)
|
|
1278
|
+
mesh.visible = true;
|
|
1279
|
+
for (const name of this.counts.keys()) {
|
|
1280
|
+
this.bones.get(name)?.traverse((object) => {
|
|
1281
|
+
if (this.rigid.has(object))
|
|
1282
|
+
object.visible = false;
|
|
1283
|
+
for (const skeleton of this.skeletons.keys()) {
|
|
1284
|
+
const index = skeleton.bones.indexOf(object);
|
|
1285
|
+
if (index >= 0)
|
|
1286
|
+
skeleton.boneInverses[index].makeScale(0, 0, 0);
|
|
1287
|
+
}
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
var PAINT_SLOTS2 = { primary: 1, secondary: 2, pearlescent: 3, wheel: 4, interior: 6, dashboard: 7 };
|
|
1293
|
+
var OPEN_ANGLE = 1.239;
|
|
1294
|
+
var OPENINGS = {
|
|
1295
|
+
frontLeft: { bone: "door_dside_f", fallback: [0, 0, -OPEN_ANGLE] },
|
|
1296
|
+
frontRight: { bone: "door_pside_f", fallback: [0, 0, OPEN_ANGLE] },
|
|
1297
|
+
rearLeft: { bone: "door_dside_r", fallback: [0, 0, -OPEN_ANGLE] },
|
|
1298
|
+
rearRight: { bone: "door_pside_r", fallback: [0, 0, OPEN_ANGLE] },
|
|
1299
|
+
bonnet: { bone: "bonnet", fallback: [OPEN_ANGLE, 0, 0] },
|
|
1300
|
+
boot: { bone: "boot", fallback: [-OPEN_ANGLE, 0, 0] }
|
|
1301
|
+
};
|
|
1302
|
+
var WRAP_SLOT = 48;
|
|
1303
|
+
var WHEEL_SLOT = 23;
|
|
1304
|
+
var WHEEL_BONE = /^wheel_[lr][fmr]\d?$/;
|
|
1305
|
+
var LIGHT_SHADER = "vehicle_lightsemissive";
|
|
1306
|
+
var LIGHT_INTENSITY = 16;
|
|
1307
|
+
var WINDOW_TINTS = { 1: 0.97, 2: 0.8, 3: 0.55, 5: 0.9, 6: 0.65 };
|
|
1308
|
+
var TINT_COLOR = new THREE4.Color(0.01, 0.012, 0.014);
|
|
1309
|
+
function setColor(target, color) {
|
|
1310
|
+
if (typeof color === "string")
|
|
1311
|
+
target.set(color);
|
|
1312
|
+
else if (typeof color !== "number")
|
|
1313
|
+
target.setRGB(color[0] / 255, color[1] / 255, color[2] / 255, THREE4.SRGBColorSpace);
|
|
1314
|
+
else if (GTA_COLORS[color])
|
|
1315
|
+
target.set(GTA_COLORS[color]);
|
|
1316
|
+
}
|
|
1317
|
+
function glowTexture() {
|
|
1318
|
+
const canvas = document.createElement("canvas");
|
|
1319
|
+
canvas.width = canvas.height = 256;
|
|
1320
|
+
const context = canvas.getContext("2d");
|
|
1321
|
+
const glow = context.createRadialGradient(128, 128, 48, 128, 128, 128);
|
|
1322
|
+
glow.addColorStop(0, "rgba(255, 255, 255, 1)");
|
|
1323
|
+
glow.addColorStop(1, "rgba(255, 255, 255, 0)");
|
|
1324
|
+
context.fillStyle = glow;
|
|
1325
|
+
context.fillRect(0, 0, 256, 256);
|
|
1326
|
+
return new THREE4.CanvasTexture(canvas);
|
|
1327
|
+
}
|
|
1328
|
+
var EXTRA_BONE = /^extra_(\d+)$/;
|
|
1329
|
+
function farthestFromClosed(min, max) {
|
|
1330
|
+
if (!min || !max)
|
|
1331
|
+
return;
|
|
1332
|
+
const farthest = (axis) => Math.abs(max[axis]) >= Math.abs(min[axis]) ? max[axis] : min[axis];
|
|
1333
|
+
return [farthest(0), farthest(1), farthest(2)];
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
class Vehicle extends Model {
|
|
1337
|
+
entry;
|
|
1338
|
+
wheelTypes;
|
|
1339
|
+
paints;
|
|
1340
|
+
loader;
|
|
1341
|
+
view = [-0.6, 0.25, -0.75];
|
|
1342
|
+
rest = new Map;
|
|
1343
|
+
hidden;
|
|
1344
|
+
extraStates = new Map;
|
|
1345
|
+
worn = new Map;
|
|
1346
|
+
revisions = new Map;
|
|
1347
|
+
stockLivery = new Map;
|
|
1348
|
+
plate = null;
|
|
1349
|
+
neon = null;
|
|
1350
|
+
wheelType = 0;
|
|
1351
|
+
fittedWheels = [];
|
|
1352
|
+
constructor(name, entry, wheelTypes, source, paints, loader) {
|
|
1353
|
+
super(name, source);
|
|
1354
|
+
this.entry = entry;
|
|
1355
|
+
this.wheelTypes = wheelTypes;
|
|
1356
|
+
this.paints = paints;
|
|
1357
|
+
this.loader = loader;
|
|
1358
|
+
this.root.updateMatrixWorld(true);
|
|
1359
|
+
for (const bone of this.bones.values())
|
|
1360
|
+
this.rest.set(bone, { rotation: bone.quaternion.clone(), position: bone.position.clone(), world: bone.matrixWorld.clone() });
|
|
1361
|
+
this.hidden = new HiddenBones(source.scene, this.bones);
|
|
1362
|
+
const allExtras = entry.flags?.includes("FLAG_EXTRAS_ALL") ?? false;
|
|
1363
|
+
for (const bone of this.bones.keys()) {
|
|
1364
|
+
const id = EXTRA_BONE.exec(bone)?.[1];
|
|
1365
|
+
if (id)
|
|
1366
|
+
this.setExtra(Number(id), allExtras);
|
|
1367
|
+
}
|
|
1368
|
+
Object.keys(PAINT_SLOTS2).forEach((name, position) => this.setPaint(name, entry.colors?.[0]?.[position] ?? 255));
|
|
1369
|
+
}
|
|
1370
|
+
static async load(name, entry, wheels, loader) {
|
|
1371
|
+
const paints = createPaints();
|
|
1372
|
+
return new Vehicle(name, entry, wheels, await loader.load(entry.model, paints), paints, loader);
|
|
1373
|
+
}
|
|
1374
|
+
async apply(props) {
|
|
1375
|
+
for (const name of Object.keys(PAINT_SLOTS2)) {
|
|
1376
|
+
if (props[name] !== undefined)
|
|
1377
|
+
this.setPaint(name, props[name]);
|
|
1378
|
+
}
|
|
1379
|
+
if (props.plate !== undefined)
|
|
1380
|
+
this.setPlate(props.plate);
|
|
1381
|
+
if (props.windowTint !== undefined)
|
|
1382
|
+
this.setWindowTint(props.windowTint);
|
|
1383
|
+
if (props.neon !== undefined)
|
|
1384
|
+
this.setNeon(props.neon);
|
|
1385
|
+
if (props.lights !== undefined)
|
|
1386
|
+
this.setLights(props.lights);
|
|
1387
|
+
if (props.wheelType !== undefined)
|
|
1388
|
+
this.wheelType = props.wheelType;
|
|
1389
|
+
for (const [id, enabled] of Object.entries(props.extras ?? {}))
|
|
1390
|
+
this.setExtra(Number(id), enabled);
|
|
1391
|
+
await Promise.all([
|
|
1392
|
+
...Object.entries(props.mods ?? {}).map(([slot, option]) => this.setMod(Number(slot), option)),
|
|
1393
|
+
...props.wrap !== undefined ? [this.setWrap(props.wrap)] : []
|
|
1394
|
+
]);
|
|
1395
|
+
}
|
|
1396
|
+
setPaint(name, color) {
|
|
1397
|
+
setColor(this.paints.get(PAINT_SLOTS2[name]).value, color);
|
|
1398
|
+
}
|
|
1399
|
+
paint(name) {
|
|
1400
|
+
return `#${this.paints.get(PAINT_SLOTS2[name]).value.getHexString()}`;
|
|
1401
|
+
}
|
|
1402
|
+
setPlate(text) {
|
|
1403
|
+
const plates = this.source.materials.filter((material) => material.name === "vehicle_licenseplate");
|
|
1404
|
+
if (plates.length === 0)
|
|
1405
|
+
return;
|
|
1406
|
+
this.plate ??= createPlate();
|
|
1407
|
+
drawPlate(this.plate, text);
|
|
1408
|
+
for (const material of plates) {
|
|
1409
|
+
if (material.map === this.plate)
|
|
1410
|
+
continue;
|
|
1411
|
+
material.map = this.plate;
|
|
1412
|
+
material.needsUpdate = true;
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
openings() {
|
|
1416
|
+
return Object.keys(OPENINGS).filter((name) => this.bones.has(OPENINGS[name].bone));
|
|
1417
|
+
}
|
|
1418
|
+
setOpen(name, amount) {
|
|
1419
|
+
const { bone: boneName, fallback } = OPENINGS[name];
|
|
1420
|
+
const bone = this.bones.get(boneName);
|
|
1421
|
+
const rest = bone && this.rest.get(bone);
|
|
1422
|
+
if (!bone || !rest)
|
|
1423
|
+
return;
|
|
1424
|
+
const open = THREE4.MathUtils.clamp(amount, 0, 1);
|
|
1425
|
+
const limits = bone.userData;
|
|
1426
|
+
const turn = farthestFromClosed(limits.rotationMin, limits.rotationMax) ?? fallback;
|
|
1427
|
+
const slide = farthestFromClosed(limits.translationMin, limits.translationMax) ?? [0, 0, 0];
|
|
1428
|
+
bone.quaternion.copy(rest.rotation);
|
|
1429
|
+
bone.rotateX(turn[0] * open);
|
|
1430
|
+
bone.rotateY(turn[1] * open);
|
|
1431
|
+
bone.rotateZ(turn[2] * open);
|
|
1432
|
+
bone.position.set(rest.position.x + slide[0] * open, rest.position.y + slide[1] * open, rest.position.z + slide[2] * open);
|
|
1433
|
+
}
|
|
1434
|
+
extras() {
|
|
1435
|
+
return [...this.extraStates.keys()].sort((a, b) => a - b);
|
|
1436
|
+
}
|
|
1437
|
+
extraEnabled(id) {
|
|
1438
|
+
return this.extraStates.get(id) ?? false;
|
|
1439
|
+
}
|
|
1440
|
+
setExtra(id, enabled) {
|
|
1441
|
+
const bone = `extra_${id}`;
|
|
1442
|
+
const previous = this.extraStates.get(id);
|
|
1443
|
+
if (!this.bones.has(bone) || previous === enabled)
|
|
1444
|
+
return;
|
|
1445
|
+
this.extraStates.set(id, enabled);
|
|
1446
|
+
if (!enabled)
|
|
1447
|
+
this.hidden.hide(bone);
|
|
1448
|
+
else if (previous === false)
|
|
1449
|
+
this.hidden.show(bone);
|
|
1450
|
+
}
|
|
1451
|
+
wraps() {
|
|
1452
|
+
const { liveries = [] } = this.entry;
|
|
1453
|
+
if (liveries.length > 0)
|
|
1454
|
+
return liveries.map((_, index) => `Wrap ${index + 1}`);
|
|
1455
|
+
return this.options(WRAP_SLOT).map((mod) => mod.label || mod.model);
|
|
1456
|
+
}
|
|
1457
|
+
async setWrap(index) {
|
|
1458
|
+
const { liveries = [] } = this.entry;
|
|
1459
|
+
if (liveries.length === 0)
|
|
1460
|
+
return this.setMod(WRAP_SLOT, index);
|
|
1461
|
+
const revision = (this.revisions.get(WRAP_SLOT) ?? 0) + 1;
|
|
1462
|
+
this.revisions.set(WRAP_SLOT, revision);
|
|
1463
|
+
const texture = index === null ? null : await this.loader.texture(liveries[index]);
|
|
1464
|
+
if (revision === this.revisions.get(WRAP_SLOT))
|
|
1465
|
+
this.applyWrap(texture);
|
|
1466
|
+
}
|
|
1467
|
+
mods() {
|
|
1468
|
+
const slots = new Map;
|
|
1469
|
+
for (const mod of this.entry.mods) {
|
|
1470
|
+
if (mod.slot >= 0 && mod.slot !== WRAP_SLOT)
|
|
1471
|
+
slots.set(mod.slot, [...slots.get(mod.slot) ?? [], mod]);
|
|
1472
|
+
}
|
|
1473
|
+
return slots;
|
|
1474
|
+
}
|
|
1475
|
+
wheels() {
|
|
1476
|
+
return this.wheelTypes;
|
|
1477
|
+
}
|
|
1478
|
+
async setWheels(type, option) {
|
|
1479
|
+
this.wheelType = type;
|
|
1480
|
+
const wheel = option === null ? undefined : this.wheelTypes[type]?.[option];
|
|
1481
|
+
const revision = (this.revisions.get(WHEEL_SLOT) ?? 0) + 1;
|
|
1482
|
+
this.revisions.set(WHEEL_SLOT, revision);
|
|
1483
|
+
const part = wheel && await this.loader.load(wheel.path, this.paints);
|
|
1484
|
+
if (revision !== this.revisions.get(WHEEL_SLOT))
|
|
1485
|
+
return;
|
|
1486
|
+
detach(this.fittedWheels);
|
|
1487
|
+
this.fittedWheels.forEach(disposeMesh);
|
|
1488
|
+
this.fittedWheels = [];
|
|
1489
|
+
const meshes = [];
|
|
1490
|
+
part?.scene.traverse((object) => object.isMesh && meshes.push(object));
|
|
1491
|
+
const bounds = (geometries) => geometries.reduce((box, geometry) => box.union((geometry.computeBoundingBox(), geometry.boundingBox)), new THREE4.Box3).getSize(new THREE4.Vector3);
|
|
1492
|
+
const size = bounds(meshes.map((mesh) => mesh.geometry));
|
|
1493
|
+
for (const [name, bone] of this.bones) {
|
|
1494
|
+
if (!WHEEL_BONE.test(name))
|
|
1495
|
+
continue;
|
|
1496
|
+
const stock = bone.children.filter((child) => child.type === "Group" || child.isMesh);
|
|
1497
|
+
for (const object of stock)
|
|
1498
|
+
object.visible = !wheel;
|
|
1499
|
+
if (!wheel || stock.length === 0)
|
|
1500
|
+
continue;
|
|
1501
|
+
const stockGeometry = [];
|
|
1502
|
+
for (const object of stock)
|
|
1503
|
+
object.traverse((child) => child.isMesh && stockGeometry.push(child.geometry));
|
|
1504
|
+
const target = bounds(stockGeometry);
|
|
1505
|
+
const radial = target.y / size.y;
|
|
1506
|
+
for (const mesh of meshes) {
|
|
1507
|
+
const fitted = new THREE4.Mesh(mesh.geometry, mesh.material);
|
|
1508
|
+
fitted.position.copy(stock[0].position);
|
|
1509
|
+
fitted.quaternion.copy(stock[0].quaternion);
|
|
1510
|
+
fitted.scale.set(target.x / size.x, radial, radial);
|
|
1511
|
+
prepareMesh(fitted);
|
|
1512
|
+
bone.add(fitted);
|
|
1513
|
+
this.fittedWheels.push(fitted);
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
async setMod(slot, option) {
|
|
1518
|
+
if (slot === WHEEL_SLOT)
|
|
1519
|
+
return this.setWheels(this.wheelType, option);
|
|
1520
|
+
const mod = option === null ? undefined : this.options(slot)[option];
|
|
1521
|
+
const revision = (this.revisions.get(slot) ?? 0) + 1;
|
|
1522
|
+
this.revisions.set(slot, revision);
|
|
1523
|
+
const entries = mod ? [mod, ...mod.linkedModels.flatMap((name) => this.linkedMod(name) ?? [])] : [];
|
|
1524
|
+
const parts = await Promise.all(entries.map((entry) => this.loader.load(entry.path, this.paints)));
|
|
1525
|
+
if (revision !== this.revisions.get(slot))
|
|
1526
|
+
return;
|
|
1527
|
+
this.removeMod(slot);
|
|
1528
|
+
if (slot === WRAP_SLOT)
|
|
1529
|
+
return this.applyWrap(parts[0]?.materials.map((material) => material.userData.secondMap.value).find(Boolean) ?? null);
|
|
1530
|
+
if (!mod)
|
|
1531
|
+
return;
|
|
1532
|
+
const objects = parts.flatMap((part, index) => attachModel(part, this.bones, this.source.scene, this.bones.get(entries[index].bone) ?? this.bones.get("chassis") ?? this.source.scene, this.rest));
|
|
1533
|
+
for (const bone of mod.turnOffBones)
|
|
1534
|
+
this.hidden.hide(bone);
|
|
1535
|
+
this.worn.set(slot, { objects, hidden: mod.turnOffBones });
|
|
1536
|
+
}
|
|
1537
|
+
setWindowTint(id) {
|
|
1538
|
+
for (const material of this.source.materials) {
|
|
1539
|
+
if (!material.name.startsWith("vehicle_vehglass"))
|
|
1540
|
+
continue;
|
|
1541
|
+
material.userData.stock ??= { map: material.map, color: material.color.clone(), opacity: material.opacity, transparent: material.transparent, depthWrite: material.depthWrite };
|
|
1542
|
+
const stock = material.userData.stock;
|
|
1543
|
+
const tint = WINDOW_TINTS[id];
|
|
1544
|
+
Object.assign(material, tint === undefined ? { ...stock, color: stock.color.clone() } : { map: null, color: TINT_COLOR.clone(), opacity: tint, transparent: true, depthWrite: false });
|
|
1545
|
+
material.needsUpdate = true;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
setNeon(color) {
|
|
1549
|
+
if (color === null) {
|
|
1550
|
+
this.neon?.removeFromParent();
|
|
1551
|
+
this.neon?.material.map?.dispose();
|
|
1552
|
+
this.neon?.material.dispose();
|
|
1553
|
+
this.neon?.geometry.dispose();
|
|
1554
|
+
this.neon = null;
|
|
1555
|
+
return;
|
|
1556
|
+
}
|
|
1557
|
+
if (!this.neon) {
|
|
1558
|
+
const box = new THREE4.Box3().setFromObject(this.source.scene).translate(this.root.position.clone().negate());
|
|
1559
|
+
const size = box.getSize(new THREE4.Vector3);
|
|
1560
|
+
const center = box.getCenter(new THREE4.Vector3);
|
|
1561
|
+
this.neon = new THREE4.Mesh(new THREE4.PlaneGeometry(size.x * 1.7, size.z * 1.35), new THREE4.MeshBasicMaterial({ map: glowTexture(), transparent: true, depthWrite: false }));
|
|
1562
|
+
this.neon.rotation.x = -Math.PI / 2;
|
|
1563
|
+
this.neon.position.set(center.x, box.min.y + size.y * 0.01, center.z);
|
|
1564
|
+
this.root.add(this.neon);
|
|
1565
|
+
}
|
|
1566
|
+
setColor(this.neon.material.color, color);
|
|
1567
|
+
}
|
|
1568
|
+
setLights(on) {
|
|
1569
|
+
for (const material of this.source.materials) {
|
|
1570
|
+
if (material.name !== LIGHT_SHADER)
|
|
1571
|
+
continue;
|
|
1572
|
+
material.emissive.setScalar(on ? 1 : 0);
|
|
1573
|
+
material.emissiveMap = on ? material.map : null;
|
|
1574
|
+
material.emissiveIntensity = LIGHT_INTENSITY;
|
|
1575
|
+
material.needsUpdate = true;
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
dispose() {
|
|
1579
|
+
this.setNeon(null);
|
|
1580
|
+
super.dispose();
|
|
1581
|
+
this.plate?.dispose();
|
|
1582
|
+
}
|
|
1583
|
+
linkedMod(name) {
|
|
1584
|
+
const { mods } = this.entry;
|
|
1585
|
+
return mods.find((mod) => mod.model === name && !mod.type) ?? mods.find((mod) => mod.model === name);
|
|
1586
|
+
}
|
|
1587
|
+
options(slot) {
|
|
1588
|
+
return this.entry.mods.filter((mod) => mod.slot === slot);
|
|
1589
|
+
}
|
|
1590
|
+
removeMod(slot) {
|
|
1591
|
+
const current = this.worn.get(slot);
|
|
1592
|
+
if (!current)
|
|
1593
|
+
return;
|
|
1594
|
+
detach(current.objects);
|
|
1595
|
+
current.objects.forEach(disposeMesh);
|
|
1596
|
+
for (const bone of current.hidden)
|
|
1597
|
+
this.hidden.show(bone);
|
|
1598
|
+
this.worn.delete(slot);
|
|
1599
|
+
}
|
|
1600
|
+
applyWrap(texture) {
|
|
1601
|
+
const slots = this.source.materials.filter((material) => material.name.startsWith("vehicle_paint") && material.userData.extras.textures.DiffuseSampler2).map((material) => material.userData.secondMap);
|
|
1602
|
+
if (!texture) {
|
|
1603
|
+
for (const [slot, stock] of this.stockLivery)
|
|
1604
|
+
slot.value = stock;
|
|
1605
|
+
return this.stockLivery.clear();
|
|
1606
|
+
}
|
|
1607
|
+
for (const slot of slots) {
|
|
1608
|
+
if (!this.stockLivery.has(slot))
|
|
1609
|
+
this.stockLivery.set(slot, slot.value);
|
|
1610
|
+
slot.value = texture;
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
// src/assets.ts
|
|
1616
|
+
var SECTIONS = { vehicle: "vehicles", character: "peds", object: "models", map: "map" };
|
|
1617
|
+
|
|
1618
|
+
class Assets {
|
|
1619
|
+
manifest;
|
|
1620
|
+
loader;
|
|
1621
|
+
models;
|
|
1622
|
+
constructor(manifest, loader) {
|
|
1623
|
+
this.manifest = manifest;
|
|
1624
|
+
this.loader = loader;
|
|
1625
|
+
this.models = Object.keys(SECTIONS).flatMap((kind) => Object.keys(manifest[SECTIONS[kind]]).sort().map((name) => ({ kind, name })));
|
|
1626
|
+
}
|
|
1627
|
+
static async open(baseUrl) {
|
|
1628
|
+
const manifest = JSON.parse(new TextDecoder().decode(await fetchBytes(`${baseUrl}manifest.json.gz`)));
|
|
1629
|
+
if (manifest.version !== 2)
|
|
1630
|
+
throw new Error(`Unsupported manifest version ${manifest.version}`);
|
|
1631
|
+
return new Assets(manifest, new ModelLoader(baseUrl));
|
|
1632
|
+
}
|
|
1633
|
+
async load(name, props) {
|
|
1634
|
+
const { manifest, loader } = this;
|
|
1635
|
+
const kind = this.models.find((entry) => entry.name === name)?.kind;
|
|
1636
|
+
if (!kind)
|
|
1637
|
+
throw new Error(`${name} is not in these assets`);
|
|
1638
|
+
if (kind === "character")
|
|
1639
|
+
return Character.load(name, manifest.peds[name], loader);
|
|
1640
|
+
if (kind !== "vehicle")
|
|
1641
|
+
return new Model(name, await loader.load(manifest[SECTIONS[kind]][name]));
|
|
1642
|
+
const vehicle = await Vehicle.load(name, manifest.vehicles[name], manifest.wheels, loader);
|
|
1643
|
+
if (props)
|
|
1644
|
+
await vehicle.apply(props);
|
|
1645
|
+
return vehicle;
|
|
1646
|
+
}
|
|
1647
|
+
clear() {
|
|
1648
|
+
this.loader.clear();
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
// src/stage.ts
|
|
1652
|
+
import * as THREE5 from "three";
|
|
1653
|
+
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
|
|
1654
|
+
var KEY_DIRECTION = new THREE5.Vector3(-0.55, 1, 0.35).normalize();
|
|
1655
|
+
var AUTO_ROTATE_SPEED = 0.8;
|
|
1656
|
+
var STAGE_MARGIN = 1.12;
|
|
1657
|
+
var TURN_SAMPLES = 24;
|
|
1658
|
+
var SOFT_BOXES = [
|
|
1659
|
+
[0.1, [30, 0.1, 30], [0, -1.3, 0]],
|
|
1660
|
+
[5, [3.2, 0.1, 15], [-6.5, 6, 0]],
|
|
1661
|
+
[5, [3.2, 0.1, 15], [0, 6, 0]],
|
|
1662
|
+
[5, [3.2, 0.1, 15], [6.5, 6, 0]],
|
|
1663
|
+
[2.2, [0.1, 2.2, 18], [-12, 0.6, 0]],
|
|
1664
|
+
[2.2, [0.1, 2.2, 18], [12, 0.6, 0]]
|
|
1665
|
+
];
|
|
1666
|
+
function studio() {
|
|
1667
|
+
const scene = new THREE5.Scene;
|
|
1668
|
+
scene.background = new THREE5.Color(0.16, 0.16, 0.16);
|
|
1669
|
+
const geometry = new THREE5.BoxGeometry;
|
|
1670
|
+
for (const [brightness, size, position] of SOFT_BOXES) {
|
|
1671
|
+
const box = new THREE5.Mesh(geometry, new THREE5.MeshBasicMaterial({ color: new THREE5.Color(brightness, brightness, brightness) }));
|
|
1672
|
+
box.scale.fromArray(size);
|
|
1673
|
+
box.position.fromArray(position);
|
|
1674
|
+
scene.add(box);
|
|
1675
|
+
}
|
|
1676
|
+
return scene;
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
class Lighting {
|
|
1680
|
+
key = new THREE5.DirectionalLight(16774118, 1.9);
|
|
1681
|
+
environment;
|
|
1682
|
+
constructor(renderer, scene, camera) {
|
|
1683
|
+
renderer.toneMapping = THREE5.ACESFilmicToneMapping;
|
|
1684
|
+
renderer.toneMappingExposure = 0.7;
|
|
1685
|
+
renderer.shadowMap.enabled = true;
|
|
1686
|
+
const pmrem = new THREE5.PMREMGenerator(renderer);
|
|
1687
|
+
this.environment = pmrem.fromScene(studio(), 0.008, 0.1, 80, { size: 512 });
|
|
1688
|
+
pmrem.dispose();
|
|
1689
|
+
scene.environment = this.environment.texture;
|
|
1690
|
+
scene.environmentIntensity = 1.15;
|
|
1691
|
+
this.key.castShadow = true;
|
|
1692
|
+
this.key.shadow.mapSize.setScalar(2048);
|
|
1693
|
+
this.key.shadow.radius = 6;
|
|
1694
|
+
camera.add(new THREE5.DirectionalLight(14674431, 0.7));
|
|
1695
|
+
scene.add(this.key, camera);
|
|
1696
|
+
}
|
|
1697
|
+
fit(radius) {
|
|
1698
|
+
this.key.position.copy(KEY_DIRECTION).multiplyScalar(radius * 3);
|
|
1699
|
+
this.key.shadow.normalBias = radius * 0.005;
|
|
1700
|
+
const reach = radius * 1.2;
|
|
1701
|
+
Object.assign(this.key.shadow.camera, { left: -reach, right: reach, top: reach, bottom: -reach, near: radius, far: radius * 5 }).updateProjectionMatrix();
|
|
1702
|
+
}
|
|
1703
|
+
dispose() {
|
|
1704
|
+
this.environment.dispose();
|
|
1705
|
+
this.key.dispose();
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
class Stage {
|
|
1710
|
+
canvas;
|
|
1711
|
+
renderer;
|
|
1712
|
+
scene = new THREE5.Scene;
|
|
1713
|
+
camera = new THREE5.PerspectiveCamera(30, 1, 0.05, 300);
|
|
1714
|
+
controls;
|
|
1715
|
+
lighting;
|
|
1716
|
+
ground = new THREE5.Mesh(new THREE5.PlaneGeometry(4, 4), new THREE5.ShadowMaterial({ opacity: 0.25 }));
|
|
1717
|
+
resizer;
|
|
1718
|
+
watcher;
|
|
1719
|
+
subject = null;
|
|
1720
|
+
showing = 0;
|
|
1721
|
+
visible = true;
|
|
1722
|
+
constructor(canvas, options = {}) {
|
|
1723
|
+
this.canvas = canvas;
|
|
1724
|
+
this.renderer = new THREE5.WebGLRenderer({ canvas, antialias: true, alpha: true });
|
|
1725
|
+
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
|
1726
|
+
this.lighting = new Lighting(this.renderer, this.scene, this.camera);
|
|
1727
|
+
this.ground.rotation.x = -Math.PI / 2;
|
|
1728
|
+
this.ground.receiveShadow = true;
|
|
1729
|
+
this.scene.add(this.ground);
|
|
1730
|
+
this.controls = new OrbitControls(this.camera, canvas);
|
|
1731
|
+
this.controls.enableDamping = true;
|
|
1732
|
+
this.controls.maxPolarAngle = Math.PI / 2 - 0.02;
|
|
1733
|
+
this.controls.autoRotate = options.autoRotate ?? false;
|
|
1734
|
+
this.controls.enableZoom = options.zoom ?? true;
|
|
1735
|
+
if (!this.controls.enableZoom)
|
|
1736
|
+
canvas.style.touchAction = "pan-y";
|
|
1737
|
+
this.controls.autoRotateSpeed = AUTO_ROTATE_SPEED;
|
|
1738
|
+
this.controls.addEventListener("start", this.stopRotating);
|
|
1739
|
+
this.watcher = new IntersectionObserver(([entry]) => {
|
|
1740
|
+
this.visible = entry.isIntersecting;
|
|
1741
|
+
});
|
|
1742
|
+
this.watcher.observe(canvas);
|
|
1743
|
+
this.resizer = new ResizeObserver(() => this.resize());
|
|
1744
|
+
this.resizer.observe(canvas);
|
|
1745
|
+
this.resize();
|
|
1746
|
+
this.renderer.setAnimationLoop(() => {
|
|
1747
|
+
if (!this.visible || canvas.clientWidth === 0)
|
|
1748
|
+
return;
|
|
1749
|
+
this.controls.update();
|
|
1750
|
+
this.renderer.render(this.scene, this.camera);
|
|
1751
|
+
});
|
|
1752
|
+
}
|
|
1753
|
+
async show(assets, name, props) {
|
|
1754
|
+
const showing = ++this.showing;
|
|
1755
|
+
if (this.subject) {
|
|
1756
|
+
this.scene.remove(this.subject.root);
|
|
1757
|
+
this.subject.dispose();
|
|
1758
|
+
this.subject = null;
|
|
1759
|
+
assets.clear();
|
|
1760
|
+
}
|
|
1761
|
+
const subject = await assets.load(name, props);
|
|
1762
|
+
if (showing === this.showing) {
|
|
1763
|
+
await Promise.all([this.renderer.compileAsync(subject.root, this.camera, this.scene), this.renderer.compileAsync(this.scene, this.camera)]);
|
|
1764
|
+
}
|
|
1765
|
+
if (showing !== this.showing) {
|
|
1766
|
+
subject.dispose();
|
|
1767
|
+
return;
|
|
1768
|
+
}
|
|
1769
|
+
this.subject = subject;
|
|
1770
|
+
this.frame(subject);
|
|
1771
|
+
this.renderer.render(this.scene, this.camera);
|
|
1772
|
+
return subject;
|
|
1773
|
+
}
|
|
1774
|
+
dispose() {
|
|
1775
|
+
this.showing++;
|
|
1776
|
+
this.subject?.dispose();
|
|
1777
|
+
this.resizer.disconnect();
|
|
1778
|
+
this.watcher.disconnect();
|
|
1779
|
+
this.renderer.setAnimationLoop(null);
|
|
1780
|
+
this.controls.removeEventListener("start", this.stopRotating);
|
|
1781
|
+
this.controls.dispose();
|
|
1782
|
+
this.lighting.dispose();
|
|
1783
|
+
this.renderer.dispose();
|
|
1784
|
+
this.renderer.forceContextLoss();
|
|
1785
|
+
}
|
|
1786
|
+
stopRotating = () => {
|
|
1787
|
+
this.controls.autoRotate = false;
|
|
1788
|
+
};
|
|
1789
|
+
frame(subject) {
|
|
1790
|
+
const size = subject.placeAtOrigin().getSize(new THREE5.Vector3);
|
|
1791
|
+
this.scene.add(subject.root);
|
|
1792
|
+
const slopeY = Math.tan(THREE5.MathUtils.degToRad(this.camera.fov / 2));
|
|
1793
|
+
const slopeX = slopeY * this.camera.aspect;
|
|
1794
|
+
const radius = size.length() / 2;
|
|
1795
|
+
const reach = Math.hypot(size.x, size.z) / 2;
|
|
1796
|
+
const target = new THREE5.Vector3(0, size.y / 2, 0);
|
|
1797
|
+
const toCamera = new THREE5.Vector3(...subject.view).normalize();
|
|
1798
|
+
const right = new THREE5.Vector3().crossVectors(new THREE5.Vector3(0, 1, 0), toCamera).normalize();
|
|
1799
|
+
const up = new THREE5.Vector3().crossVectors(toCamera, right);
|
|
1800
|
+
const point = new THREE5.Vector3;
|
|
1801
|
+
let distance = 0;
|
|
1802
|
+
for (let turn = 0;turn < TURN_SAMPLES; turn++) {
|
|
1803
|
+
const angle = turn / TURN_SAMPLES * Math.PI * 2;
|
|
1804
|
+
for (const height of [-size.y / 2, size.y / 2]) {
|
|
1805
|
+
point.set(Math.cos(angle) * reach, height, Math.sin(angle) * reach);
|
|
1806
|
+
const depth = point.dot(toCamera);
|
|
1807
|
+
distance = Math.max(distance, depth + Math.abs(point.dot(right)) * STAGE_MARGIN / slopeX, depth + Math.abs(point.dot(up)) * STAGE_MARGIN / slopeY);
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
this.lighting.fit(radius);
|
|
1811
|
+
this.ground.scale.setScalar(radius);
|
|
1812
|
+
Object.assign(this.camera, { near: radius * 0.02, far: radius * 20 }).updateProjectionMatrix();
|
|
1813
|
+
this.controls.target.copy(target);
|
|
1814
|
+
this.controls.minDistance = radius * 0.2;
|
|
1815
|
+
this.controls.maxDistance = radius * 8;
|
|
1816
|
+
this.camera.position.copy(target).addScaledVector(toCamera, distance);
|
|
1817
|
+
this.controls.update();
|
|
1818
|
+
}
|
|
1819
|
+
resize() {
|
|
1820
|
+
const { clientWidth: width, clientHeight: height } = this.canvas;
|
|
1821
|
+
if (width === 0 || height === 0)
|
|
1822
|
+
return;
|
|
1823
|
+
this.renderer.setSize(width, height, false);
|
|
1824
|
+
this.camera.aspect = width / height;
|
|
1825
|
+
this.camera.updateProjectionMatrix();
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
var FIELD_OF_VIEW = 20;
|
|
1829
|
+
var MARGIN = 1.06;
|
|
1830
|
+
var VISIBLE_ALPHA = 16;
|
|
1831
|
+
var FRAMING_PASSES = 4;
|
|
1832
|
+
var WEBP_QUALITY = 0.9;
|
|
1833
|
+
|
|
1834
|
+
class ThumbnailRenderer {
|
|
1835
|
+
size;
|
|
1836
|
+
format;
|
|
1837
|
+
renderer;
|
|
1838
|
+
scene = new THREE5.Scene;
|
|
1839
|
+
camera = new THREE5.PerspectiveCamera(FIELD_OF_VIEW, 1);
|
|
1840
|
+
lighting;
|
|
1841
|
+
pixels;
|
|
1842
|
+
constructor(size = 512, format = "png") {
|
|
1843
|
+
this.size = size;
|
|
1844
|
+
this.format = format;
|
|
1845
|
+
this.renderer = new THREE5.WebGLRenderer({ antialias: true, alpha: true, preserveDrawingBuffer: true });
|
|
1846
|
+
this.renderer.setSize(size, size, false);
|
|
1847
|
+
this.renderer.setClearColor(0, 0);
|
|
1848
|
+
this.lighting = new Lighting(this.renderer, this.scene, this.camera);
|
|
1849
|
+
this.pixels = new Uint8Array(size * size * 4);
|
|
1850
|
+
}
|
|
1851
|
+
async render(model, view = model.view) {
|
|
1852
|
+
const box = model.placeAtOrigin();
|
|
1853
|
+
const center = box.getCenter(new THREE5.Vector3);
|
|
1854
|
+
const radius = box.getSize(new THREE5.Vector3).length() / 2;
|
|
1855
|
+
const toCamera = new THREE5.Vector3(...view).normalize();
|
|
1856
|
+
const right = new THREE5.Vector3().crossVectors(new THREE5.Vector3(0, 1, 0), toCamera).normalize();
|
|
1857
|
+
const up = new THREE5.Vector3().crossVectors(toCamera, right);
|
|
1858
|
+
const slope = Math.tan(THREE5.MathUtils.degToRad(FIELD_OF_VIEW / 2));
|
|
1859
|
+
let distance = 0;
|
|
1860
|
+
for (let corner = 0;corner < 8; corner++) {
|
|
1861
|
+
const point = new THREE5.Vector3(corner & 1 ? box.max.x : box.min.x, corner & 2 ? box.max.y : box.min.y, corner & 4 ? box.max.z : box.min.z).sub(center);
|
|
1862
|
+
const extent = Math.max(Math.abs(point.dot(right)), Math.abs(point.dot(up))) * MARGIN;
|
|
1863
|
+
distance = Math.max(distance, point.dot(toCamera) + extent / slope);
|
|
1864
|
+
}
|
|
1865
|
+
this.camera.position.copy(center).addScaledVector(toCamera, distance);
|
|
1866
|
+
Object.assign(this.camera, { near: distance / 100, far: distance + radius * 2 }).updateProjectionMatrix();
|
|
1867
|
+
this.camera.lookAt(center);
|
|
1868
|
+
this.lighting.fit(radius);
|
|
1869
|
+
this.scene.add(model.root);
|
|
1870
|
+
try {
|
|
1871
|
+
this.draw(model.name);
|
|
1872
|
+
} finally {
|
|
1873
|
+
this.scene.remove(model.root);
|
|
1874
|
+
this.camera.clearViewOffset();
|
|
1875
|
+
}
|
|
1876
|
+
return new Promise((resolve, reject) => this.renderer.domElement.toBlob((blob) => blob ? resolve(blob) : reject(new Error(`Could not encode ${model.name}`)), `image/${this.format}`, WEBP_QUALITY));
|
|
1877
|
+
}
|
|
1878
|
+
draw(name) {
|
|
1879
|
+
let frame = { x: 0, y: 0, side: this.size };
|
|
1880
|
+
for (let pass = 1;; pass++) {
|
|
1881
|
+
this.camera.setViewOffset(this.size, this.size, frame.x, frame.y, frame.side, frame.side);
|
|
1882
|
+
this.renderer.render(this.scene, this.camera);
|
|
1883
|
+
if (pass === FRAMING_PASSES)
|
|
1884
|
+
return;
|
|
1885
|
+
const fitted = this.fit(frame);
|
|
1886
|
+
if (!fitted)
|
|
1887
|
+
throw new Error(`${name} has nothing to show`);
|
|
1888
|
+
const pixel = frame.side / this.size;
|
|
1889
|
+
if (Math.max(Math.abs(fitted.x - frame.x), Math.abs(fitted.y - frame.y), Math.abs(fitted.side - frame.side)) < 2 * pixel)
|
|
1890
|
+
return;
|
|
1891
|
+
frame = fitted;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
fit(frame) {
|
|
1895
|
+
const { size, pixels } = this;
|
|
1896
|
+
const gl = this.renderer.getContext();
|
|
1897
|
+
gl.readPixels(0, 0, size, size, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
1898
|
+
let [left, right, bottom, top] = [size, -1, size, -1];
|
|
1899
|
+
for (let row = 0;row < size; row++) {
|
|
1900
|
+
for (let column = 0;column < size; column++) {
|
|
1901
|
+
if (pixels[(row * size + column) * 4 + 3] < VISIBLE_ALPHA)
|
|
1902
|
+
continue;
|
|
1903
|
+
left = Math.min(left, column);
|
|
1904
|
+
right = Math.max(right, column);
|
|
1905
|
+
bottom = Math.min(bottom, row);
|
|
1906
|
+
top = Math.max(top, row);
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
if (right < 0)
|
|
1910
|
+
return null;
|
|
1911
|
+
const pixel = frame.side / size;
|
|
1912
|
+
const clipped = left === 0 || bottom === 0 || right === size - 1 || top === size - 1;
|
|
1913
|
+
const side = clipped ? frame.side * 2 : Math.max(right - left + 1, top - bottom + 1) * pixel * MARGIN;
|
|
1914
|
+
const x = clipped ? size / 2 : (left + right + 1) / 2;
|
|
1915
|
+
const y = clipped ? size / 2 : size - (bottom + top + 1) / 2;
|
|
1916
|
+
return { x: frame.x + x * pixel - side / 2, y: frame.y + y * pixel - side / 2, side };
|
|
1917
|
+
}
|
|
1918
|
+
dispose() {
|
|
1919
|
+
this.lighting.dispose();
|
|
1920
|
+
this.renderer.dispose();
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
export {
|
|
1924
|
+
Assets,
|
|
1925
|
+
Character,
|
|
1926
|
+
Model,
|
|
1927
|
+
Stage,
|
|
1928
|
+
ThumbnailRenderer,
|
|
1929
|
+
Vehicle
|
|
1930
|
+
};
|