@adatechnology/game-render-three 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +34 -2
- package/dist/index.d.ts +440 -0
- package/dist/index.js +1333 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1333 @@
|
|
|
1
|
+
// src/lights/slots.ts
|
|
2
|
+
var PARKED_Y = -50;
|
|
3
|
+
var DEFAULT_DECAY = 2;
|
|
4
|
+
var DEFAULT_ANGLE = Math.PI / 3;
|
|
5
|
+
var POSITION_EPSILON = 1e-6;
|
|
6
|
+
function createValues(base) {
|
|
7
|
+
return {
|
|
8
|
+
decay: DEFAULT_DECAY,
|
|
9
|
+
target: void 0,
|
|
10
|
+
angle: DEFAULT_ANGLE,
|
|
11
|
+
penumbra: 0,
|
|
12
|
+
...base,
|
|
13
|
+
position: [...base.position]
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function mergeValues(values, patch) {
|
|
17
|
+
if (patch.color !== void 0) values.color = patch.color;
|
|
18
|
+
if (patch.intensity !== void 0) values.intensity = patch.intensity;
|
|
19
|
+
if (patch.distance !== void 0) values.distance = patch.distance;
|
|
20
|
+
if (patch.decay !== void 0) values.decay = patch.decay;
|
|
21
|
+
if (patch.position !== void 0) values.position = [...patch.position];
|
|
22
|
+
if (patch.target !== void 0) values.target = [...patch.target];
|
|
23
|
+
if (patch.angle !== void 0) values.angle = patch.angle;
|
|
24
|
+
if (patch.penumbra !== void 0) values.penumbra = patch.penumbra;
|
|
25
|
+
}
|
|
26
|
+
function createSlots(THREE, group, kind, count, shadows, mapSize, settings = {}) {
|
|
27
|
+
const slots = [];
|
|
28
|
+
for (let index = 0; index < count; index++) {
|
|
29
|
+
const light = kind === "point" ? new THREE.PointLight(16777215, 0, 0, DEFAULT_DECAY) : new THREE.SpotLight(16777215, 0, 0, DEFAULT_ANGLE, 0, DEFAULT_DECAY);
|
|
30
|
+
light.position.set(0, PARKED_Y, 0);
|
|
31
|
+
light.layers.enableAll();
|
|
32
|
+
const shadow = index < shadows;
|
|
33
|
+
if (shadow) {
|
|
34
|
+
light.castShadow = true;
|
|
35
|
+
light.shadow.autoUpdate = false;
|
|
36
|
+
light.shadow.mapSize.set(mapSize, mapSize);
|
|
37
|
+
if (settings.bias !== void 0) light.shadow.bias = settings.bias;
|
|
38
|
+
if (settings.normalBias !== void 0) light.shadow.normalBias = settings.normalBias;
|
|
39
|
+
if (settings.near !== void 0) light.shadow.camera.near = settings.near;
|
|
40
|
+
}
|
|
41
|
+
group.add(light);
|
|
42
|
+
if ("target" in light) group.add(light.target);
|
|
43
|
+
slots.push({ index, kind, shadow, light, holder: null, pending: null, leased: false, fade: 0, fadeTarget: 0, fadeOutMs: 0, baseIntensity: 0, lastPosition: [0, PARKED_Y, 0] });
|
|
44
|
+
}
|
|
45
|
+
return slots;
|
|
46
|
+
}
|
|
47
|
+
function isFree(slot) {
|
|
48
|
+
return !slot.leased && slot.holder === null && slot.pending === null;
|
|
49
|
+
}
|
|
50
|
+
function applyIntensity(slot) {
|
|
51
|
+
slot.light.intensity = slot.baseIntensity * slot.fade;
|
|
52
|
+
}
|
|
53
|
+
function markShadow(slot) {
|
|
54
|
+
if (slot.shadow) slot.light.shadow.needsUpdate = true;
|
|
55
|
+
}
|
|
56
|
+
function writeLight(THREE, slot, values) {
|
|
57
|
+
const { light } = slot;
|
|
58
|
+
if (typeof values.color === "number") light.color.set(values.color);
|
|
59
|
+
else light.color.setRGB(values.color[0], values.color[1], values.color[2], THREE.LinearSRGBColorSpace);
|
|
60
|
+
light.distance = values.distance;
|
|
61
|
+
light.decay = values.decay;
|
|
62
|
+
const [x, y, z] = values.position;
|
|
63
|
+
light.position.set(x, y, z);
|
|
64
|
+
if ("angle" in light) {
|
|
65
|
+
light.angle = values.angle;
|
|
66
|
+
light.penumbra = values.penumbra;
|
|
67
|
+
const [tx, ty, tz] = values.target ?? [x, y - 1, z];
|
|
68
|
+
light.target.position.set(tx, ty, tz);
|
|
69
|
+
}
|
|
70
|
+
const last = slot.lastPosition;
|
|
71
|
+
if (slot.shadow && (Math.abs(last[0] - x) > POSITION_EPSILON || Math.abs(last[1] - y) > POSITION_EPSILON || Math.abs(last[2] - z) > POSITION_EPSILON)) markShadow(slot);
|
|
72
|
+
slot.lastPosition = [x, y, z];
|
|
73
|
+
slot.baseIntensity = values.intensity;
|
|
74
|
+
applyIntensity(slot);
|
|
75
|
+
}
|
|
76
|
+
function install(THREE, slot, holder, startAtFull) {
|
|
77
|
+
slot.holder = holder;
|
|
78
|
+
slot.pending = null;
|
|
79
|
+
slot.fade = startAtFull ? 1 : 0;
|
|
80
|
+
slot.fadeTarget = 1;
|
|
81
|
+
holder.slot = slot;
|
|
82
|
+
markShadow(slot);
|
|
83
|
+
writeLight(THREE, slot, holder.values);
|
|
84
|
+
}
|
|
85
|
+
function vacate(slot, fadeOutMs) {
|
|
86
|
+
slot.holder = null;
|
|
87
|
+
slot.fadeTarget = 0;
|
|
88
|
+
slot.fadeOutMs = fadeOutMs;
|
|
89
|
+
if (fadeOutMs <= 0) {
|
|
90
|
+
slot.fade = 0;
|
|
91
|
+
applyIntensity(slot);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function stepFade(slot, dtSeconds, fadeInMs) {
|
|
95
|
+
if (slot.fade < slot.fadeTarget) {
|
|
96
|
+
slot.fade = fadeInMs <= 0 ? slot.fadeTarget : Math.min(slot.fadeTarget, slot.fade + dtSeconds * 1e3 / fadeInMs);
|
|
97
|
+
} else if (slot.fade > slot.fadeTarget) {
|
|
98
|
+
slot.fade = slot.fadeOutMs <= 0 ? slot.fadeTarget : Math.max(slot.fadeTarget, slot.fade - dtSeconds * 1e3 / slot.fadeOutMs);
|
|
99
|
+
}
|
|
100
|
+
applyIntensity(slot);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/lights/arbiter.ts
|
|
104
|
+
function poolOf(core, kind, shadow) {
|
|
105
|
+
return core.slots[kind].filter((slot) => !slot.leased && slot.shadow === shadow);
|
|
106
|
+
}
|
|
107
|
+
function contribution(holder) {
|
|
108
|
+
return holder.values.intensity / Math.max(1, holder.distance) ** 2;
|
|
109
|
+
}
|
|
110
|
+
function isWeaker(a, b) {
|
|
111
|
+
if (a.priority !== b.priority) return a.priority < b.priority;
|
|
112
|
+
return contribution(a) < contribution(b);
|
|
113
|
+
}
|
|
114
|
+
function pickFree(slots) {
|
|
115
|
+
let best;
|
|
116
|
+
for (const slot of slots) if (isFree(slot) && (best === void 0 || slot.fade < best.fade)) best = slot;
|
|
117
|
+
return best;
|
|
118
|
+
}
|
|
119
|
+
function place(core, slot, holder) {
|
|
120
|
+
holder.slot = slot;
|
|
121
|
+
if (slot.fade > 0 && !holder.urgent) {
|
|
122
|
+
slot.pending = holder;
|
|
123
|
+
slot.fadeTarget = 0;
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
install(core.THREE, slot, holder, holder.kind === "request");
|
|
127
|
+
}
|
|
128
|
+
function evict(core, slot, challenger, cut) {
|
|
129
|
+
const victim = slot.holder;
|
|
130
|
+
if (victim === null) return;
|
|
131
|
+
victim.slot = null;
|
|
132
|
+
if (victim.kind === "request") {
|
|
133
|
+
victim.alive = false;
|
|
134
|
+
victim.onLost?.();
|
|
135
|
+
}
|
|
136
|
+
core.preempted++;
|
|
137
|
+
vacate(slot, cut ? 0 : core.fadeMs);
|
|
138
|
+
if (cut) {
|
|
139
|
+
install(core.THREE, slot, challenger, true);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
slot.pending = challenger;
|
|
143
|
+
challenger.slot = slot;
|
|
144
|
+
}
|
|
145
|
+
function weakestHolder(slots) {
|
|
146
|
+
let weakest;
|
|
147
|
+
for (const slot of slots) {
|
|
148
|
+
if (slot.holder === null || slot.pending !== null) continue;
|
|
149
|
+
if (weakest === void 0 || isWeaker(slot.holder, weakest.holder)) weakest = slot;
|
|
150
|
+
}
|
|
151
|
+
return weakest;
|
|
152
|
+
}
|
|
153
|
+
function freeBeyondReserve(core, pool, reserve) {
|
|
154
|
+
const free = pool.filter(isFree);
|
|
155
|
+
return free.length > reserve ? pickFree(pool) : void 0;
|
|
156
|
+
}
|
|
157
|
+
function admitRequest(core, holder) {
|
|
158
|
+
const pool = poolOf(core, holder.lightKind, false);
|
|
159
|
+
const reserve = holder.lightKind === "point" ? core.urgentReserve : 0;
|
|
160
|
+
const free = holder.urgent ? pickFree(pool) : freeBeyondReserve(core, pool, reserve);
|
|
161
|
+
if (free !== void 0) {
|
|
162
|
+
place(core, free, holder);
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
const victim = weakestHolder(pool);
|
|
166
|
+
if (victim === void 0 || victim.holder.priority >= holder.priority) {
|
|
167
|
+
core.refused++;
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
evict(core, victim, holder, holder.urgent);
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
function beats(challenger, incumbent, hysteresis) {
|
|
174
|
+
if (challenger.priority !== incumbent.priority) return challenger.priority > incumbent.priority;
|
|
175
|
+
return challenger.distance < incumbent.distance * (1 - hysteresis);
|
|
176
|
+
}
|
|
177
|
+
function bestFirst(a, b) {
|
|
178
|
+
return b.priority - a.priority || a.distance - b.distance;
|
|
179
|
+
}
|
|
180
|
+
function arbitrateSources(core, pool, sources, reserve) {
|
|
181
|
+
for (const slot of pool) {
|
|
182
|
+
const holder = slot.holder;
|
|
183
|
+
if (slot.pending?.kind === "source" && !slot.pending.wanted) {
|
|
184
|
+
slot.pending.slot = null;
|
|
185
|
+
slot.pending = null;
|
|
186
|
+
}
|
|
187
|
+
if (holder?.kind === "source" && !holder.wanted) {
|
|
188
|
+
holder.slot = null;
|
|
189
|
+
vacate(slot, core.fadeMs);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const challengers = sources.filter((source) => source.wanted && source.slot === null).sort(bestFirst);
|
|
193
|
+
for (const challenger of challengers) {
|
|
194
|
+
const free = freeBeyondReserve(core, pool, reserve);
|
|
195
|
+
if (free !== void 0) {
|
|
196
|
+
place(core, free, challenger);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const incumbentSlots = pool.filter((slot) => slot.holder?.kind === "source");
|
|
200
|
+
let worst;
|
|
201
|
+
for (const slot of incumbentSlots) {
|
|
202
|
+
if (slot.pending !== null) continue;
|
|
203
|
+
if (worst === void 0 || bestFirst(slot.holder, worst.holder) > 0) worst = slot;
|
|
204
|
+
}
|
|
205
|
+
if (worst === void 0 || !beats(challenger, worst.holder, core.hysteresis)) break;
|
|
206
|
+
evict(core, worst, challenger, false);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function stepSlots(core, dtSeconds) {
|
|
210
|
+
for (const kind of ["point", "spot"]) {
|
|
211
|
+
for (const slot of core.slots[kind]) {
|
|
212
|
+
stepFade(slot, dtSeconds, core.fadeMs);
|
|
213
|
+
if (slot.pending !== null && slot.fade <= 0) install(core.THREE, slot, slot.pending, false);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/fog.ts
|
|
219
|
+
var NEUTRAL_FOG_NEAR = 6e4;
|
|
220
|
+
var NEUTRAL_FOG_FAR = 64e3;
|
|
221
|
+
function isLinearFog(fog) {
|
|
222
|
+
return fog?.isFog === true;
|
|
223
|
+
}
|
|
224
|
+
function isExp2Fog(fog) {
|
|
225
|
+
return fog?.isFogExp2 === true;
|
|
226
|
+
}
|
|
227
|
+
function isNeutralFog(fog) {
|
|
228
|
+
const candidate = fog;
|
|
229
|
+
return candidate?.isFog === true && candidate.near === NEUTRAL_FOG_NEAR && candidate.far === NEUTRAL_FOG_FAR;
|
|
230
|
+
}
|
|
231
|
+
function createFog(THREE, scene, options = {}) {
|
|
232
|
+
const original = scene.fog;
|
|
233
|
+
const hostLinear = isLinearFog(original) ? { color: original.color.getHex(), near: original.near, far: original.far } : void 0;
|
|
234
|
+
const resting = hostLinear ?? { color: options.color ?? 0, near: NEUTRAL_FOG_NEAR, far: NEUTRAL_FOG_FAR };
|
|
235
|
+
return {
|
|
236
|
+
useLinear(color, near, far) {
|
|
237
|
+
const current = scene.fog;
|
|
238
|
+
if (isLinearFog(current)) {
|
|
239
|
+
current.color.set(color);
|
|
240
|
+
current.near = near;
|
|
241
|
+
current.far = far;
|
|
242
|
+
return current;
|
|
243
|
+
}
|
|
244
|
+
const fog = new THREE.Fog(color, near, far);
|
|
245
|
+
scene.fog = fog;
|
|
246
|
+
return fog;
|
|
247
|
+
},
|
|
248
|
+
useExp2(color, density) {
|
|
249
|
+
const current = scene.fog;
|
|
250
|
+
if (isExp2Fog(current)) {
|
|
251
|
+
current.color.set(color);
|
|
252
|
+
current.density = density;
|
|
253
|
+
return current;
|
|
254
|
+
}
|
|
255
|
+
const fog = new THREE.FogExp2(color, density);
|
|
256
|
+
scene.fog = fog;
|
|
257
|
+
return fog;
|
|
258
|
+
},
|
|
259
|
+
clear() {
|
|
260
|
+
if (options.stable !== true) {
|
|
261
|
+
scene.fog = original;
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const current = scene.fog;
|
|
265
|
+
if (isLinearFog(current)) {
|
|
266
|
+
current.color.set(resting.color);
|
|
267
|
+
current.near = resting.near;
|
|
268
|
+
current.far = resting.far;
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
scene.fog = new THREE.Fog(resting.color, resting.near, resting.far);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// src/lights/scene-state.ts
|
|
277
|
+
var LIGHT_FLAGS = [
|
|
278
|
+
["point", "isPointLight"],
|
|
279
|
+
["spot", "isSpotLight"],
|
|
280
|
+
["hemi", "isHemisphereLight"],
|
|
281
|
+
["dir", "isDirectionalLight"],
|
|
282
|
+
["ambient", "isAmbientLight"],
|
|
283
|
+
["rect", "isRectAreaLight"]
|
|
284
|
+
];
|
|
285
|
+
function lightSignature(scene) {
|
|
286
|
+
const counters = {};
|
|
287
|
+
for (const [name] of LIGHT_FLAGS) counters[name] = { count: 0, shadows: 0 };
|
|
288
|
+
scene.traverseVisible((object) => {
|
|
289
|
+
const flags = object;
|
|
290
|
+
for (const [name, flag] of LIGHT_FLAGS) {
|
|
291
|
+
if (flags[flag] !== true) continue;
|
|
292
|
+
const counter = counters[name];
|
|
293
|
+
counter.count++;
|
|
294
|
+
if (flags.castShadow === true) counter.shadows++;
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
return LIGHT_FLAGS.map(([name]) => `${name}:${counters[name].count}/${counters[name].shadows}`).join(" ");
|
|
298
|
+
}
|
|
299
|
+
function fogStateOf(scene) {
|
|
300
|
+
const fog = scene.fog;
|
|
301
|
+
if (fog === null) return "none";
|
|
302
|
+
return fog.isFogExp2 === true ? "exp2" : "linear";
|
|
303
|
+
}
|
|
304
|
+
function findStrays(scene, group) {
|
|
305
|
+
const strays = [];
|
|
306
|
+
const visit = (object, path) => {
|
|
307
|
+
if (object === group) return;
|
|
308
|
+
const flags = object;
|
|
309
|
+
if (flags.isPointLight === true || flags.isSpotLight === true) strays.push(`${path}${object.name === "" ? object.type : object.name}`);
|
|
310
|
+
object.children.forEach((child, index) => visit(child, `${path}${index}/`));
|
|
311
|
+
};
|
|
312
|
+
visit(scene, "");
|
|
313
|
+
return strays;
|
|
314
|
+
}
|
|
315
|
+
function environmentOf(scene) {
|
|
316
|
+
return scene.environment === null ? "none" : `texture:${scene.environment.mapping}`;
|
|
317
|
+
}
|
|
318
|
+
function targetColorSpaceOf(target) {
|
|
319
|
+
return target ? target.texture.colorSpace : "canvas";
|
|
320
|
+
}
|
|
321
|
+
function captureSealed(renderer, scene, options) {
|
|
322
|
+
return {
|
|
323
|
+
lights: lightSignature(scene),
|
|
324
|
+
environment: environmentOf(scene),
|
|
325
|
+
toneMapping: renderer.toneMapping,
|
|
326
|
+
outputColorSpace: renderer.outputColorSpace,
|
|
327
|
+
targetColorSpace: targetColorSpaceOf(options.target),
|
|
328
|
+
shadowMapType: renderer.shadowMap.type,
|
|
329
|
+
fogStates: options.fogStates ?? [fogStateOf(scene)],
|
|
330
|
+
cameras: options.cameras,
|
|
331
|
+
target: options.target
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function describe(state) {
|
|
335
|
+
const { lights, fog, environment, toneMapping, outputColorSpace, targetColorSpace, shadowMapType } = state;
|
|
336
|
+
return `${lights} | fog:${fog} env:${environment} tone:${toneMapping} out:${outputColorSpace}/${targetColorSpace} shadow:${shadowMapType}`;
|
|
337
|
+
}
|
|
338
|
+
function farBeyondNeutralFog(scene, sealed) {
|
|
339
|
+
const fog = scene.fog;
|
|
340
|
+
if (fog?.isFog !== true || fog.near !== NEUTRAL_FOG_NEAR) return false;
|
|
341
|
+
return (sealed.cameras ?? []).some((camera) => (camera.far ?? 0) >= NEUTRAL_FOG_NEAR);
|
|
342
|
+
}
|
|
343
|
+
function checkSealed(sealed, renderer, scene, group) {
|
|
344
|
+
const strays = findStrays(scene, group);
|
|
345
|
+
const lights = lightSignature(scene);
|
|
346
|
+
if (sealed === null) return { ok: strays.length === 0, expected: lights, actual: lights, strays };
|
|
347
|
+
const fog = fogStateOf(scene);
|
|
348
|
+
const fogAllowed = sealed.fogStates.includes(fog);
|
|
349
|
+
const expected = describe({ ...sealed, fog: fogAllowed ? fog : sealed.fogStates.join("|") });
|
|
350
|
+
const actual = describe({
|
|
351
|
+
lights,
|
|
352
|
+
fog,
|
|
353
|
+
environment: environmentOf(scene),
|
|
354
|
+
toneMapping: renderer.toneMapping,
|
|
355
|
+
outputColorSpace: renderer.outputColorSpace,
|
|
356
|
+
targetColorSpace: targetColorSpaceOf(sealed.target),
|
|
357
|
+
shadowMapType: renderer.shadowMap.type
|
|
358
|
+
});
|
|
359
|
+
const ok = strays.length === 0 && expected === actual && !farBeyondNeutralFog(scene, sealed);
|
|
360
|
+
return { ok, expected, actual, strays };
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// src/lights/budget.ts
|
|
364
|
+
var DEFAULTS = { point: 16, spot: 4, pointShadows: 2, spotShadows: 0, shadowMapSize: 512, urgentReserve: 2, fadeMs: 120, hysteresis: 0.15, hemisphere: 0 };
|
|
365
|
+
var ZERO = [0, 0, 0];
|
|
366
|
+
var ALL_LAYERS_MASK = -1;
|
|
367
|
+
function newHolder(kind, lightKind, request, urgent) {
|
|
368
|
+
return {
|
|
369
|
+
kind,
|
|
370
|
+
owner: request.owner,
|
|
371
|
+
lightKind,
|
|
372
|
+
priority: request.priority,
|
|
373
|
+
urgent,
|
|
374
|
+
values: createValues({ color: request.color, intensity: request.intensity, distance: request.distance, position: [...request.position], ...optional(request) }),
|
|
375
|
+
slot: null,
|
|
376
|
+
alive: true,
|
|
377
|
+
onLost: void 0,
|
|
378
|
+
follow: void 0,
|
|
379
|
+
shadow: false,
|
|
380
|
+
wanted: true,
|
|
381
|
+
distance: 0,
|
|
382
|
+
resolved: createValues({ color: request.color, intensity: 0, distance: 0, position: ZERO })
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function optional(request) {
|
|
386
|
+
const values = {};
|
|
387
|
+
if (request.decay !== void 0) values.decay = request.decay;
|
|
388
|
+
if (request.target !== void 0) values.target = [...request.target];
|
|
389
|
+
if (request.angle !== void 0) values.angle = request.angle;
|
|
390
|
+
if (request.penumbra !== void 0) values.penumbra = request.penumbra;
|
|
391
|
+
return values;
|
|
392
|
+
}
|
|
393
|
+
function isAttached(object, root) {
|
|
394
|
+
for (let node = object; node !== null; node = node.parent) if (node === root) return true;
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
function createLightBudget(THREE, scene, options = {}) {
|
|
398
|
+
const opts = { ...DEFAULTS, ...options };
|
|
399
|
+
const pointShadows = Math.min(opts.pointShadows, opts.point);
|
|
400
|
+
const spotShadows = Math.min(opts.spotShadows, opts.spot);
|
|
401
|
+
const group = new THREE.Group();
|
|
402
|
+
group.name = "light-budget";
|
|
403
|
+
const core = {
|
|
404
|
+
THREE,
|
|
405
|
+
slots: {
|
|
406
|
+
point: createSlots(THREE, group, "point", opts.point, pointShadows, opts.shadowMapSize, options.shadowSettings),
|
|
407
|
+
spot: createSlots(THREE, group, "spot", opts.spot, spotShadows, opts.shadowMapSize, options.shadowSettings)
|
|
408
|
+
},
|
|
409
|
+
urgentReserve: opts.urgentReserve,
|
|
410
|
+
fadeMs: opts.fadeMs,
|
|
411
|
+
hysteresis: opts.hysteresis,
|
|
412
|
+
preempted: 0,
|
|
413
|
+
refused: 0
|
|
414
|
+
};
|
|
415
|
+
const hemispheres = [];
|
|
416
|
+
for (let index = 0; index < opts.hemisphere; index++) {
|
|
417
|
+
const light = new THREE.HemisphereLight(16777215, 16777215, 0);
|
|
418
|
+
light.layers.enableAll();
|
|
419
|
+
group.add(light);
|
|
420
|
+
hemispheres.push(light);
|
|
421
|
+
}
|
|
422
|
+
scene.add(group);
|
|
423
|
+
scene.children.splice(scene.children.indexOf(group), 1);
|
|
424
|
+
scene.children.unshift(group);
|
|
425
|
+
const sources = /* @__PURE__ */ new Set();
|
|
426
|
+
const world = new THREE.Vector3();
|
|
427
|
+
const direction = new THREE.Vector3();
|
|
428
|
+
let sealed = null;
|
|
429
|
+
let leasedHemispheres = 0;
|
|
430
|
+
const allSlots = () => [...core.slots.point, ...core.slots.spot];
|
|
431
|
+
function writeHolder(holder) {
|
|
432
|
+
if (holder.slot !== null && holder.slot.holder === holder) writeLight(THREE, holder.slot, holder.values);
|
|
433
|
+
}
|
|
434
|
+
function makeHandle(holder, release) {
|
|
435
|
+
return {
|
|
436
|
+
get slot() {
|
|
437
|
+
return holder.slot?.index ?? -1;
|
|
438
|
+
},
|
|
439
|
+
kind: holder.lightKind,
|
|
440
|
+
get alive() {
|
|
441
|
+
return holder.alive;
|
|
442
|
+
},
|
|
443
|
+
update(patch) {
|
|
444
|
+
if (!holder.alive) return;
|
|
445
|
+
mergeValues(holder.values, patch);
|
|
446
|
+
writeHolder(holder);
|
|
447
|
+
},
|
|
448
|
+
release(releaseOptions) {
|
|
449
|
+
if (!holder.alive || release === null) return;
|
|
450
|
+
holder.alive = false;
|
|
451
|
+
release(releaseOptions?.fadeMs);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
function dropHolder(holder, fadeMs) {
|
|
456
|
+
const slot = holder.slot;
|
|
457
|
+
holder.slot = null;
|
|
458
|
+
if (slot === null) return;
|
|
459
|
+
if (slot.pending === holder) slot.pending = null;
|
|
460
|
+
else if (slot.holder === holder) vacate(slot, fadeMs);
|
|
461
|
+
}
|
|
462
|
+
function resolveSource(holder, focus) {
|
|
463
|
+
const { follow, values, resolved } = holder;
|
|
464
|
+
holder.wanted = values.intensity > 0;
|
|
465
|
+
if (follow === void 0) {
|
|
466
|
+
holder.distance = Math.hypot(values.position[0] - focus[0], values.position[1] - focus[1], values.position[2] - focus[2]);
|
|
467
|
+
Object.assign(resolved, values);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
holder.wanted = holder.wanted && isAttached(follow, scene);
|
|
471
|
+
if (!holder.wanted) return;
|
|
472
|
+
follow.getWorldPosition(world);
|
|
473
|
+
const [ox, oy, oz] = values.position;
|
|
474
|
+
if (ox !== 0 || oy !== 0 || oz !== 0) world.set(ox, oy, oz).applyMatrix4(follow.matrixWorld);
|
|
475
|
+
const axis = follow.matrixWorld.elements;
|
|
476
|
+
direction.set(-axis[8], -axis[9], -axis[10]);
|
|
477
|
+
holder.distance = Math.hypot(world.x - focus[0], world.y - focus[1], world.z - focus[2]);
|
|
478
|
+
Object.assign(resolved, values);
|
|
479
|
+
resolved.position = [world.x, world.y, world.z];
|
|
480
|
+
resolved.target = values.target ?? [world.x + direction.x, world.y + direction.y, world.z + direction.z];
|
|
481
|
+
}
|
|
482
|
+
return {
|
|
483
|
+
group,
|
|
484
|
+
sizes: { point: opts.point, spot: opts.spot, pointShadows, spotShadows },
|
|
485
|
+
acquire(request) {
|
|
486
|
+
const holder = newHolder("request", request.kind ?? "point", request, request.urgent === true);
|
|
487
|
+
holder.onLost = request.onLost;
|
|
488
|
+
if (!admitRequest(core, holder)) return null;
|
|
489
|
+
return makeHandle(holder, (fadeMs) => dropHolder(holder, fadeMs ?? opts.fadeMs));
|
|
490
|
+
},
|
|
491
|
+
addSource(source) {
|
|
492
|
+
const holder = newHolder("source", source.kind ?? "point", source, false);
|
|
493
|
+
holder.follow = source.follow;
|
|
494
|
+
holder.shadow = source.shadow === true;
|
|
495
|
+
sources.add(holder);
|
|
496
|
+
return {
|
|
497
|
+
get slot() {
|
|
498
|
+
return holder.slot !== null && holder.slot.holder === holder ? holder.slot.index : -1;
|
|
499
|
+
},
|
|
500
|
+
update(patch) {
|
|
501
|
+
mergeValues(holder.values, patch);
|
|
502
|
+
if (patch.priority !== void 0) holder.priority = patch.priority;
|
|
503
|
+
if ("follow" in patch) holder.follow = patch.follow;
|
|
504
|
+
if (holder.follow === void 0) writeHolder(holder);
|
|
505
|
+
},
|
|
506
|
+
remove() {
|
|
507
|
+
holder.alive = false;
|
|
508
|
+
holder.wanted = false;
|
|
509
|
+
sources.delete(holder);
|
|
510
|
+
dropHolder(holder, opts.fadeMs);
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
},
|
|
514
|
+
lease({ owner, point, shadows = 0 }) {
|
|
515
|
+
const shadowSlots = core.slots.point.filter((slot) => slot.shadow && isFree(slot));
|
|
516
|
+
const plainSlots = core.slots.point.filter((slot) => !slot.shadow && isFree(slot));
|
|
517
|
+
if (shadowSlots.length < shadows || plainSlots.length < point - shadows) return null;
|
|
518
|
+
const taken = [...shadowSlots.slice(0, shadows), ...plainSlots.slice(0, point - shadows)];
|
|
519
|
+
const holders = taken.map((slot) => {
|
|
520
|
+
const holder = newHolder("lease", "point", { owner, priority: Number.POSITIVE_INFINITY, color: 16777215, intensity: 0, distance: 0, position: ZERO }, false);
|
|
521
|
+
slot.leased = true;
|
|
522
|
+
slot.holder = holder;
|
|
523
|
+
slot.fade = 1;
|
|
524
|
+
slot.fadeTarget = 1;
|
|
525
|
+
holder.slot = slot;
|
|
526
|
+
markShadow(slot);
|
|
527
|
+
return holder;
|
|
528
|
+
});
|
|
529
|
+
const handles = holders.map((holder) => makeHandle(holder, null));
|
|
530
|
+
return {
|
|
531
|
+
handles,
|
|
532
|
+
invalidateShadow(index) {
|
|
533
|
+
for (const [at, holder] of holders.entries()) if ((index === void 0 || index === at) && holder.slot !== null) markShadow(holder.slot);
|
|
534
|
+
},
|
|
535
|
+
release() {
|
|
536
|
+
for (const holder of holders) {
|
|
537
|
+
const slot = holder.slot;
|
|
538
|
+
holder.alive = false;
|
|
539
|
+
holder.slot = null;
|
|
540
|
+
if (slot === null) continue;
|
|
541
|
+
slot.leased = false;
|
|
542
|
+
slot.holder = null;
|
|
543
|
+
vacate(slot, 0);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
},
|
|
548
|
+
leaseHemisphere() {
|
|
549
|
+
const light = hemispheres[leasedHemispheres];
|
|
550
|
+
if (light === void 0) return null;
|
|
551
|
+
leasedHemispheres++;
|
|
552
|
+
return light;
|
|
553
|
+
},
|
|
554
|
+
present(focus, dtSeconds) {
|
|
555
|
+
for (const holder of sources) resolveSource(holder, focus);
|
|
556
|
+
for (const kind of ["point", "spot"]) {
|
|
557
|
+
const reserve = kind === "point" ? core.urgentReserve : 0;
|
|
558
|
+
const kindSources = [...sources].filter((holder) => holder.lightKind === kind);
|
|
559
|
+
arbitrateSources(core, poolOf(core, kind, true), kindSources.filter((holder) => holder.shadow), 0);
|
|
560
|
+
arbitrateSources(core, poolOf(core, kind, false), kindSources.filter((holder) => !holder.shadow), reserve);
|
|
561
|
+
}
|
|
562
|
+
stepSlots(core, dtSeconds);
|
|
563
|
+
for (const holder of sources) if (holder.wanted && holder.slot?.holder === holder) writeLight(THREE, holder.slot, holder.resolved);
|
|
564
|
+
},
|
|
565
|
+
seal(renderer, scene2, sealOptions = {}) {
|
|
566
|
+
sealed = captureSealed(renderer, scene2, sealOptions);
|
|
567
|
+
},
|
|
568
|
+
check(renderer, checkedScene) {
|
|
569
|
+
const result = checkSealed(sealed, renderer, checkedScene, group);
|
|
570
|
+
const layersIntact = allSlots().every((slot) => slot.light.visible && slot.light.layers.mask === ALL_LAYERS_MASK);
|
|
571
|
+
const isFirst = checkedScene.children[0] === group;
|
|
572
|
+
if (!layersIntact) return { ...result, ok: false, actual: `${result.actual} | budget lights hidden or off a layer` };
|
|
573
|
+
return isFirst ? result : { ...result, ok: false, actual: `${result.actual} | budget group is not the first child of the scene` };
|
|
574
|
+
},
|
|
575
|
+
stats() {
|
|
576
|
+
const slots = allSlots();
|
|
577
|
+
return {
|
|
578
|
+
lit: slots.filter((slot) => slot.light.intensity > 0).length,
|
|
579
|
+
free: slots.filter(isFree).length,
|
|
580
|
+
leased: slots.filter((slot) => slot.leased).length,
|
|
581
|
+
sources: sources.size,
|
|
582
|
+
sourcesWithoutSlot: [...sources].filter((holder) => holder.wanted && holder.slot === null).length,
|
|
583
|
+
preempted: core.preempted,
|
|
584
|
+
refused: core.refused
|
|
585
|
+
};
|
|
586
|
+
},
|
|
587
|
+
dispose() {
|
|
588
|
+
group.removeFromParent();
|
|
589
|
+
for (const slot of allSlots()) slot.light.dispose();
|
|
590
|
+
sources.clear();
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// src/lights/types.ts
|
|
596
|
+
var LIGHT_PRIORITY = {
|
|
597
|
+
blast: 900,
|
|
598
|
+
rocket: 800,
|
|
599
|
+
muzzle: 750,
|
|
600
|
+
fire: 600,
|
|
601
|
+
flare: 500,
|
|
602
|
+
headlight: 400,
|
|
603
|
+
weaponLight: 400,
|
|
604
|
+
lamp: 200,
|
|
605
|
+
street: 150
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
// src/lights/guard.ts
|
|
609
|
+
function isLightBudget(value) {
|
|
610
|
+
const candidate = value;
|
|
611
|
+
return typeof candidate === "object" && candidate !== null && typeof candidate.acquire === "function" && typeof candidate.addSource === "function" && typeof candidate.lease === "function" && typeof candidate.present === "function";
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// src/program-key.ts
|
|
615
|
+
function programKeysOf(renderer, material) {
|
|
616
|
+
const properties = renderer.properties;
|
|
617
|
+
if (typeof properties?.get !== "function") return void 0;
|
|
618
|
+
const programs = properties.get(material)?.programs;
|
|
619
|
+
if (programs === void 0) return [];
|
|
620
|
+
if (!(programs instanceof Map)) return void 0;
|
|
621
|
+
return [...programs.keys()].map(String);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// src/pools/anchors.ts
|
|
625
|
+
function createMaterialAnchors(renderer) {
|
|
626
|
+
const anchors = /* @__PURE__ */ new Set();
|
|
627
|
+
let reader = renderer;
|
|
628
|
+
function coveredKeys() {
|
|
629
|
+
const covered = /* @__PURE__ */ new Set();
|
|
630
|
+
if (reader === void 0) return covered;
|
|
631
|
+
for (const anchor of anchors) for (const key of programKeysOf(reader, anchor) ?? []) covered.add(key);
|
|
632
|
+
return covered;
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
get size() {
|
|
636
|
+
return anchors.size;
|
|
637
|
+
},
|
|
638
|
+
bind(next) {
|
|
639
|
+
reader = next;
|
|
640
|
+
},
|
|
641
|
+
offer(material) {
|
|
642
|
+
if (anchors.has(material)) return true;
|
|
643
|
+
const keys = reader === void 0 ? void 0 : programKeysOf(reader, material);
|
|
644
|
+
if (keys === void 0) {
|
|
645
|
+
anchors.add(material);
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
if (keys.length === 0) return false;
|
|
649
|
+
const covered = coveredKeys();
|
|
650
|
+
if (keys.every((key) => covered.has(key))) return false;
|
|
651
|
+
anchors.add(material);
|
|
652
|
+
return true;
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// src/probes.ts
|
|
658
|
+
function spriteProbes(THREE) {
|
|
659
|
+
return {
|
|
660
|
+
id: "sprite",
|
|
661
|
+
build() {
|
|
662
|
+
const group = new THREE.Group();
|
|
663
|
+
const texture = new THREE.DataTexture(new Uint8Array([255, 255, 255, 255]), 1, 1);
|
|
664
|
+
texture.needsUpdate = true;
|
|
665
|
+
const materials = [true, false].map((fog) => new THREE.SpriteMaterial({ map: texture, fog, transparent: true }));
|
|
666
|
+
for (const material of materials) group.add(new THREE.Sprite(material));
|
|
667
|
+
return { group, materials, dispose: () => texture.dispose() };
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
function hasMissingDistanceMaterial(root) {
|
|
672
|
+
let found;
|
|
673
|
+
root.traverse((object) => {
|
|
674
|
+
const mesh = object;
|
|
675
|
+
if (found === void 0 && mesh.isInstancedMesh === true && mesh.castShadow === true && mesh.customDepthMaterial && !mesh.customDistanceMaterial) found = object.name || object.type;
|
|
676
|
+
});
|
|
677
|
+
return found;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// src/warmup.ts
|
|
681
|
+
var EFFECT_STEP_SECONDS = 1 / 50;
|
|
682
|
+
var DEFAULT_EFFECT_FRAMES = 3;
|
|
683
|
+
var WARM_FOG_DENSITY = 0.01;
|
|
684
|
+
var WARM_FOG_FAR = 100;
|
|
685
|
+
function register(items, item) {
|
|
686
|
+
items.add(item);
|
|
687
|
+
return () => void items.delete(item);
|
|
688
|
+
}
|
|
689
|
+
function countLinks(context, counters) {
|
|
690
|
+
const existing = counters.get(context);
|
|
691
|
+
if (existing !== void 0) {
|
|
692
|
+
existing.count = 0;
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
const counter = { count: 0 };
|
|
696
|
+
counters.set(context, counter);
|
|
697
|
+
const original = context.linkProgram.bind(context);
|
|
698
|
+
context.linkProgram = (program) => {
|
|
699
|
+
counter.count++;
|
|
700
|
+
original(program);
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
function failedProgram(renderer) {
|
|
704
|
+
for (const program of renderer.info.programs ?? []) {
|
|
705
|
+
const diagnostics = program.diagnostics;
|
|
706
|
+
if (diagnostics?.runnable === false) return diagnostics.programLog ?? "program failed to link";
|
|
707
|
+
}
|
|
708
|
+
return void 0;
|
|
709
|
+
}
|
|
710
|
+
function finishPrograms(renderer) {
|
|
711
|
+
let finished = 0;
|
|
712
|
+
for (const program of renderer.info.programs ?? []) {
|
|
713
|
+
const linked = program;
|
|
714
|
+
if (typeof linked.getUniforms !== "function") continue;
|
|
715
|
+
linked.getUniforms();
|
|
716
|
+
linked.getAttributes?.();
|
|
717
|
+
finished++;
|
|
718
|
+
}
|
|
719
|
+
return finished;
|
|
720
|
+
}
|
|
721
|
+
function createProgramWarmup(THREE) {
|
|
722
|
+
const probes = /* @__PURE__ */ new Set();
|
|
723
|
+
const effects = /* @__PURE__ */ new Set();
|
|
724
|
+
const textures = /* @__PURE__ */ new Set();
|
|
725
|
+
const anchors = createMaterialAnchors();
|
|
726
|
+
const linkCounters = /* @__PURE__ */ new WeakMap();
|
|
727
|
+
function setFog(scene, state) {
|
|
728
|
+
const color = scene.fog?.color.getHex() ?? 0;
|
|
729
|
+
if (state === "none") scene.fog = null;
|
|
730
|
+
else if (state === "linear" && scene.fog?.isFog !== true) scene.fog = new THREE.Fog(color, 1, WARM_FOG_FAR);
|
|
731
|
+
else if (state === "exp2" && scene.fog?.isFogExp2 !== true) scene.fog = new THREE.FogExp2(color, WARM_FOG_DENSITY);
|
|
732
|
+
}
|
|
733
|
+
function draw(renderer, scene, camera, target) {
|
|
734
|
+
renderer.setRenderTarget(target ?? null);
|
|
735
|
+
renderer.compile(scene, camera);
|
|
736
|
+
renderer.render(scene, camera);
|
|
737
|
+
}
|
|
738
|
+
function drawOnly(scene, root) {
|
|
739
|
+
const hidden = [];
|
|
740
|
+
const visit = (object) => {
|
|
741
|
+
if (object === root || !object.visible) return;
|
|
742
|
+
const drawable = object;
|
|
743
|
+
if (drawable.isMesh === true || drawable.isPoints === true || drawable.isLine === true || drawable.isSprite === true) {
|
|
744
|
+
if (!holdsLight(object) && !holdsObject(object, root)) {
|
|
745
|
+
object.visible = false;
|
|
746
|
+
hidden.push(object);
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
for (const child of object.children) visit(child);
|
|
751
|
+
};
|
|
752
|
+
for (const child of scene.children) visit(child);
|
|
753
|
+
return () => hidden.forEach((object) => object.visible = true);
|
|
754
|
+
}
|
|
755
|
+
function holdsLight(object) {
|
|
756
|
+
let found = false;
|
|
757
|
+
object.traverse((each) => {
|
|
758
|
+
if (each.isLight === true) found = true;
|
|
759
|
+
});
|
|
760
|
+
return found;
|
|
761
|
+
}
|
|
762
|
+
function holdsObject(object, target) {
|
|
763
|
+
for (let at = target; at !== null; at = at.parent) if (at === object) return true;
|
|
764
|
+
return false;
|
|
765
|
+
}
|
|
766
|
+
function playEffect(renderer, scene, camera, effect, target) {
|
|
767
|
+
const root = effect.root?.();
|
|
768
|
+
const restore = root === null || root === void 0 ? () => void 0 : drawOnly(scene, root);
|
|
769
|
+
try {
|
|
770
|
+
playFrames(renderer, scene, camera, effect, target);
|
|
771
|
+
} finally {
|
|
772
|
+
restore();
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
function playFrames(renderer, scene, camera, effect, target) {
|
|
776
|
+
effect.play();
|
|
777
|
+
try {
|
|
778
|
+
for (let frame = 0; frame < (effect.frames ?? DEFAULT_EFFECT_FRAMES); frame++) {
|
|
779
|
+
effect.step(EFFECT_STEP_SECONDS);
|
|
780
|
+
renderer.setRenderTarget(target ?? null);
|
|
781
|
+
renderer.render(scene, camera);
|
|
782
|
+
}
|
|
783
|
+
} finally {
|
|
784
|
+
effect.stop();
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
function materialsProbe(materials) {
|
|
788
|
+
const geometry = new THREE.BufferGeometry();
|
|
789
|
+
geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array([0, 0, 0, 1e-3, 0, 0, 0, 1e-3, 0]), 3));
|
|
790
|
+
const group = new THREE.Group();
|
|
791
|
+
for (const material of materials) group.add(drawableOf(material, geometry));
|
|
792
|
+
return { probe: { id: "materials", build: () => ({ group, materials: [], dispose: () => geometry.dispose() }) }, built: { group, materials: [], dispose: () => geometry.dispose() } };
|
|
793
|
+
}
|
|
794
|
+
function drawableOf(material, geometry) {
|
|
795
|
+
const flags = material;
|
|
796
|
+
const object = flags.isSpriteMaterial === true ? new THREE.Sprite(material) : flags.isPointsMaterial === true ? new THREE.Points(geometry, material) : flags.isLineBasicMaterial === true ? new THREE.LineSegments(geometry, material) : new THREE.Mesh(geometry, material);
|
|
797
|
+
object.frustumCulled = false;
|
|
798
|
+
return object;
|
|
799
|
+
}
|
|
800
|
+
function buildProbes(materials) {
|
|
801
|
+
const built = [...probes].map((probe) => ({ probe, built: probe.build() }));
|
|
802
|
+
if (materials !== void 0 && materials.length > 0) built.push(materialsProbe(materials));
|
|
803
|
+
return built;
|
|
804
|
+
}
|
|
805
|
+
function addProbes(scene, built) {
|
|
806
|
+
for (const { probe, built: item } of built) {
|
|
807
|
+
const missing = hasMissingDistanceMaterial(item.group);
|
|
808
|
+
if (missing !== void 0) throw new Error(`warmup probe "${probe.id}": instanced mesh "${missing}" casts shadows with a customDepthMaterial but no customDistanceMaterial`);
|
|
809
|
+
scene.add(item.group);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
function removeProbes(scene, built) {
|
|
813
|
+
for (const { built: item } of built) {
|
|
814
|
+
scene.remove(item.group);
|
|
815
|
+
item.dispose();
|
|
816
|
+
for (const material of item.materials) anchors.offer(material);
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
function drawStates(renderer, scene, camera, states, target) {
|
|
820
|
+
for (const state of states) {
|
|
821
|
+
setFog(scene, state);
|
|
822
|
+
draw(renderer, scene, camera, target);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
function forceDraws(scene) {
|
|
826
|
+
const undo = [];
|
|
827
|
+
scene.traverse((object) => {
|
|
828
|
+
const drawable = object;
|
|
829
|
+
const { geometry } = drawable;
|
|
830
|
+
if (geometry?.isInstancedBufferGeometry === true && geometry.instanceCount === 0) {
|
|
831
|
+
geometry.instanceCount = 1;
|
|
832
|
+
undo.push(() => geometry.instanceCount = 0);
|
|
833
|
+
}
|
|
834
|
+
if (drawable.isInstancedMesh === true && drawable.count === 0) {
|
|
835
|
+
drawable.count = 1;
|
|
836
|
+
undo.push(() => drawable.count = 0);
|
|
837
|
+
}
|
|
838
|
+
});
|
|
839
|
+
return () => undo.forEach((restore) => restore());
|
|
840
|
+
}
|
|
841
|
+
return {
|
|
842
|
+
anchors,
|
|
843
|
+
register: (probe) => register(probes, probe),
|
|
844
|
+
registerEffect: (effect) => register(effects, effect),
|
|
845
|
+
registerTexture: (texture) => register(textures, texture),
|
|
846
|
+
warmUp(renderer, scene, camera, options = {}) {
|
|
847
|
+
const started = performance.now();
|
|
848
|
+
const { lights, target } = options;
|
|
849
|
+
anchors.bind(renderer);
|
|
850
|
+
const originalFog = scene.fog;
|
|
851
|
+
const states = options.fogStates ?? [fogStateOf(scene)];
|
|
852
|
+
lights?.seal(renderer, scene, { fogStates: options.fogStates, cameras: [camera], target });
|
|
853
|
+
scene.traverse((object) => {
|
|
854
|
+
const light = object;
|
|
855
|
+
if (light.castShadow === true && light.shadow !== void 0) light.shadow.needsUpdate = true;
|
|
856
|
+
});
|
|
857
|
+
const built = buildProbes(options.materials);
|
|
858
|
+
let restoreDraws = () => void 0;
|
|
859
|
+
try {
|
|
860
|
+
addProbes(scene, built);
|
|
861
|
+
restoreDraws = forceDraws(scene);
|
|
862
|
+
drawStates(renderer, scene, camera, states, target);
|
|
863
|
+
scene.fog = originalFog;
|
|
864
|
+
for (const effect of effects) playEffect(renderer, scene, camera, effect, target);
|
|
865
|
+
for (const texture of textures) renderer.initTexture(texture);
|
|
866
|
+
} finally {
|
|
867
|
+
restoreDraws();
|
|
868
|
+
scene.fog = originalFog;
|
|
869
|
+
removeProbes(scene, built);
|
|
870
|
+
}
|
|
871
|
+
finishPrograms(renderer);
|
|
872
|
+
const context = renderer.getContext();
|
|
873
|
+
const uniformVectors = Number(context.getParameter(context.MAX_FRAGMENT_UNIFORM_VECTORS));
|
|
874
|
+
const failure = failedProgram(renderer);
|
|
875
|
+
if (failure !== void 0) {
|
|
876
|
+
throw new Error(`a shader program failed to link (MAX_FRAGMENT_UNIFORM_VECTORS ${uniformVectors}, lights: ${lightSignature(scene)}): ${failure}`);
|
|
877
|
+
}
|
|
878
|
+
countLinks(context, linkCounters);
|
|
879
|
+
if (context.RGBA !== void 0 && context.UNSIGNED_BYTE !== void 0) context.readPixels?.(0, 0, 1, 1, context.RGBA, context.UNSIGNED_BYTE, new Uint8Array(4));
|
|
880
|
+
return {
|
|
881
|
+
programs: renderer.info.programs?.length ?? 0,
|
|
882
|
+
geometries: renderer.info.memory.geometries,
|
|
883
|
+
textures: renderer.info.memory.textures,
|
|
884
|
+
milliseconds: performance.now() - started,
|
|
885
|
+
lightSignature: lightSignature(scene),
|
|
886
|
+
uniformVectors,
|
|
887
|
+
anchors: anchors.size
|
|
888
|
+
};
|
|
889
|
+
},
|
|
890
|
+
linkedSince(renderer) {
|
|
891
|
+
return linkCounters.get(renderer.getContext())?.count ?? 0;
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// src/pools/pooled.ts
|
|
897
|
+
var POOLED = "__pooled";
|
|
898
|
+
function isDevelopment() {
|
|
899
|
+
return typeof process === "undefined" || process.env.NODE_ENV !== "production";
|
|
900
|
+
}
|
|
901
|
+
function markPooled(resource, key, owner) {
|
|
902
|
+
resource.userData[POOLED] = key;
|
|
903
|
+
const original = resource.dispose.bind(resource);
|
|
904
|
+
let fromPool = false;
|
|
905
|
+
if (isDevelopment()) {
|
|
906
|
+
resource.dispose = () => {
|
|
907
|
+
if (!fromPool) console.warn(`[game-render-three] ${owner} "${key}" was disposed directly; release it to its pool instead`);
|
|
908
|
+
original();
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
return {
|
|
912
|
+
disposeForPool() {
|
|
913
|
+
fromPool = true;
|
|
914
|
+
original();
|
|
915
|
+
fromPool = false;
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
function pooledKey(resource) {
|
|
920
|
+
const key = resource.userData[POOLED];
|
|
921
|
+
return typeof key === "string" ? key : void 0;
|
|
922
|
+
}
|
|
923
|
+
function roundedNumber(value, step) {
|
|
924
|
+
return String(Number((Math.round(value / step) * step).toFixed(6)));
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// src/pools/geometry.ts
|
|
928
|
+
var DEFAULT_KEY_STEP = 1e-3;
|
|
929
|
+
function geometryKey(kind, params, step = DEFAULT_KEY_STEP) {
|
|
930
|
+
return [kind, ...params.map((value) => roundedNumber(value, step))].join("|");
|
|
931
|
+
}
|
|
932
|
+
function createGeometryPool() {
|
|
933
|
+
const entries = /* @__PURE__ */ new Map();
|
|
934
|
+
let clock = 0;
|
|
935
|
+
function disposeEntry(key, entry) {
|
|
936
|
+
entries.delete(key);
|
|
937
|
+
entry.disposeForPool();
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
acquire(key, build) {
|
|
941
|
+
let entry = entries.get(key);
|
|
942
|
+
if (entry === void 0) {
|
|
943
|
+
const geometry = build();
|
|
944
|
+
entry = { geometry, uses: 0, retained: false, idleSince: 0, ...markPooled(geometry, key, "geometry") };
|
|
945
|
+
entries.set(key, entry);
|
|
946
|
+
}
|
|
947
|
+
entry.uses++;
|
|
948
|
+
return entry.geometry;
|
|
949
|
+
},
|
|
950
|
+
release(geometry) {
|
|
951
|
+
const key = pooledKey(geometry);
|
|
952
|
+
const entry = key === void 0 ? void 0 : entries.get(key);
|
|
953
|
+
if (entry === void 0 || entry.uses === 0) return;
|
|
954
|
+
entry.uses--;
|
|
955
|
+
if (entry.uses === 0) entry.idleSince = ++clock;
|
|
956
|
+
},
|
|
957
|
+
retain(key) {
|
|
958
|
+
const entry = entries.get(key);
|
|
959
|
+
if (entry !== void 0) entry.retained = true;
|
|
960
|
+
},
|
|
961
|
+
trim(keep = 0) {
|
|
962
|
+
const idle = [...entries].filter(([, entry]) => entry.uses === 0 && !entry.retained).sort((a, b) => a[1].idleSince - b[1].idleSince);
|
|
963
|
+
const excess = idle.slice(0, Math.max(0, idle.length - keep));
|
|
964
|
+
for (const [key, entry] of excess) disposeEntry(key, entry);
|
|
965
|
+
return excess.length;
|
|
966
|
+
},
|
|
967
|
+
stats() {
|
|
968
|
+
const all = [...entries.values()];
|
|
969
|
+
return { entries: all.length, retained: all.filter((entry) => entry.retained).length, inUse: all.filter((entry) => entry.uses > 0).length };
|
|
970
|
+
},
|
|
971
|
+
dispose() {
|
|
972
|
+
for (const [key, entry] of [...entries]) disposeEntry(key, entry);
|
|
973
|
+
}
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// src/pools/material.ts
|
|
978
|
+
var DEFAULT_KEY_STEP2 = 1e-3;
|
|
979
|
+
function materialKey(type, params) {
|
|
980
|
+
const parts = Object.keys(params).sort().map((name) => {
|
|
981
|
+
const value = params[name];
|
|
982
|
+
return `${name}=${typeof value === "number" ? roundedNumber(value, DEFAULT_KEY_STEP2) : JSON.stringify(value)}`;
|
|
983
|
+
});
|
|
984
|
+
return [type, ...parts].join("|");
|
|
985
|
+
}
|
|
986
|
+
function createMaterialPool(options = {}) {
|
|
987
|
+
const anchors = options.anchors ?? createMaterialAnchors();
|
|
988
|
+
if (options.renderer !== void 0) anchors.bind(options.renderer);
|
|
989
|
+
const entries = /* @__PURE__ */ new Map();
|
|
990
|
+
const owned = /* @__PURE__ */ new Map();
|
|
991
|
+
let clock = 0;
|
|
992
|
+
function createEntry(key, material, registered) {
|
|
993
|
+
const entry = { material, uses: 0, registered, idleSince: 0, ...markPooled(material, key, "material") };
|
|
994
|
+
entries.set(key, entry);
|
|
995
|
+
return entry;
|
|
996
|
+
}
|
|
997
|
+
function retire(material, disposeForPool) {
|
|
998
|
+
if (anchors.offer(material)) return false;
|
|
999
|
+
disposeForPool();
|
|
1000
|
+
return true;
|
|
1001
|
+
}
|
|
1002
|
+
return {
|
|
1003
|
+
acquire(key, build) {
|
|
1004
|
+
const entry = entries.get(key) ?? createEntry(key, build(), false);
|
|
1005
|
+
entry.uses++;
|
|
1006
|
+
return entry.material;
|
|
1007
|
+
},
|
|
1008
|
+
register(key, build) {
|
|
1009
|
+
const entry = entries.get(key) ?? createEntry(key, build(), true);
|
|
1010
|
+
entry.registered = true;
|
|
1011
|
+
return entry.material;
|
|
1012
|
+
},
|
|
1013
|
+
own(build) {
|
|
1014
|
+
const material = build();
|
|
1015
|
+
owned.set(material, markPooled(material, "own", "material").disposeForPool);
|
|
1016
|
+
return material;
|
|
1017
|
+
},
|
|
1018
|
+
release(material) {
|
|
1019
|
+
const disposeOwned = owned.get(material);
|
|
1020
|
+
if (disposeOwned !== void 0) {
|
|
1021
|
+
owned.delete(material);
|
|
1022
|
+
retire(material, disposeOwned);
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
const key = pooledKey(material);
|
|
1026
|
+
const entry = key === void 0 ? void 0 : entries.get(key);
|
|
1027
|
+
if (entry === void 0 || entry.uses === 0) return;
|
|
1028
|
+
entry.uses--;
|
|
1029
|
+
if (entry.uses === 0) entry.idleSince = ++clock;
|
|
1030
|
+
},
|
|
1031
|
+
trim(keep = 0) {
|
|
1032
|
+
const idle = [...entries].filter(([, entry]) => entry.uses === 0 && !entry.registered).sort((a, b) => a[1].idleSince - b[1].idleSince);
|
|
1033
|
+
let disposed = 0;
|
|
1034
|
+
for (const [key, entry] of idle.slice(0, Math.max(0, idle.length - keep))) {
|
|
1035
|
+
entries.delete(key);
|
|
1036
|
+
if (retire(entry.material, entry.disposeForPool)) disposed++;
|
|
1037
|
+
}
|
|
1038
|
+
return disposed;
|
|
1039
|
+
},
|
|
1040
|
+
registeredMaterials() {
|
|
1041
|
+
return [...entries.values()].filter((entry) => entry.registered).map((entry) => entry.material);
|
|
1042
|
+
},
|
|
1043
|
+
stats() {
|
|
1044
|
+
const all = [...entries.values()];
|
|
1045
|
+
return {
|
|
1046
|
+
entries: all.length,
|
|
1047
|
+
registered: all.filter((entry) => entry.registered).length,
|
|
1048
|
+
inUse: all.filter((entry) => entry.uses > 0).length,
|
|
1049
|
+
families: anchors.size
|
|
1050
|
+
};
|
|
1051
|
+
},
|
|
1052
|
+
dispose() {
|
|
1053
|
+
for (const entry of entries.values()) entry.disposeForPool();
|
|
1054
|
+
for (const disposeOwned of owned.values()) disposeOwned();
|
|
1055
|
+
entries.clear();
|
|
1056
|
+
owned.clear();
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
// src/pools/render-pools.ts
|
|
1062
|
+
function isRenderPools(value) {
|
|
1063
|
+
const candidate = value;
|
|
1064
|
+
return typeof candidate === "object" && candidate !== null && typeof candidate.geometries?.acquire === "function" && typeof candidate.materials?.acquire === "function";
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
// src/frame-pacer.ts
|
|
1068
|
+
var DEFAULT_MAX_SKIPS = 3;
|
|
1069
|
+
var DEFAULT_FRAMES_IN_FLIGHT = 2;
|
|
1070
|
+
function createFramePacer(context, options = {}) {
|
|
1071
|
+
const maxSkips = options.maxSkips ?? DEFAULT_MAX_SKIPS;
|
|
1072
|
+
const framesInFlight = options.framesInFlight ?? DEFAULT_FRAMES_IN_FLIGHT;
|
|
1073
|
+
const fences = [];
|
|
1074
|
+
let skippedInRow = 0;
|
|
1075
|
+
let skipped = 0;
|
|
1076
|
+
const dropOldest = () => {
|
|
1077
|
+
const oldest = fences.shift();
|
|
1078
|
+
if (oldest && context) context.deleteSync(oldest);
|
|
1079
|
+
};
|
|
1080
|
+
return {
|
|
1081
|
+
ready() {
|
|
1082
|
+
if (!context) return true;
|
|
1083
|
+
while (fences.length > 0 && context.clientWaitSync(fences[0], 0, 0) !== context.TIMEOUT_EXPIRED) dropOldest();
|
|
1084
|
+
if (fences.length >= framesInFlight && skippedInRow < maxSkips) {
|
|
1085
|
+
skippedInRow++;
|
|
1086
|
+
skipped++;
|
|
1087
|
+
return false;
|
|
1088
|
+
}
|
|
1089
|
+
skippedInRow = 0;
|
|
1090
|
+
if (fences.length >= framesInFlight) dropOldest();
|
|
1091
|
+
return true;
|
|
1092
|
+
},
|
|
1093
|
+
submitted() {
|
|
1094
|
+
if (!context || typeof context.fenceSync !== "function") return;
|
|
1095
|
+
const fence = context.fenceSync(context.SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
1096
|
+
if (fence) fences.push(fence);
|
|
1097
|
+
context.flush();
|
|
1098
|
+
},
|
|
1099
|
+
get skipped() {
|
|
1100
|
+
return skipped;
|
|
1101
|
+
},
|
|
1102
|
+
dispose() {
|
|
1103
|
+
while (fences.length > 0) dropOldest();
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// src/static-batcher.ts
|
|
1109
|
+
var BATCHED_LAYER = 30;
|
|
1110
|
+
var MATRIX_EPSILON = 1e-6;
|
|
1111
|
+
var ATTRIBUTES = ["position", "normal", "uv", "color"];
|
|
1112
|
+
var isShown = (object, root) => {
|
|
1113
|
+
for (let node = object; node; node = node.parent) {
|
|
1114
|
+
if (node === root) return true;
|
|
1115
|
+
if (!node.visible) return false;
|
|
1116
|
+
}
|
|
1117
|
+
return false;
|
|
1118
|
+
};
|
|
1119
|
+
var isBatchable = (mesh) => {
|
|
1120
|
+
const flags = mesh;
|
|
1121
|
+
if (flags.isInstancedMesh === true || flags.isSkinnedMesh === true || Array.isArray(mesh.material)) return false;
|
|
1122
|
+
const geometry = mesh.geometry;
|
|
1123
|
+
return geometry.attributes.position !== void 0 && Object.keys(geometry.morphAttributes).length === 0;
|
|
1124
|
+
};
|
|
1125
|
+
var signatureOf = (mesh) => {
|
|
1126
|
+
const { attributes } = mesh.geometry;
|
|
1127
|
+
const present = ATTRIBUTES.filter((name) => attributes[name] !== void 0).map((name) => `${name}${attributes[name].itemSize}`);
|
|
1128
|
+
return `${mesh.material.uuid}|${present.join(",")}|${mesh.castShadow ? 1 : 0}${mesh.receiveShadow ? 1 : 0}`;
|
|
1129
|
+
};
|
|
1130
|
+
var sameMatrix = (a, b) => a.elements.every((value, index) => Math.abs(value - b.elements[index]) < MATRIX_EPSILON);
|
|
1131
|
+
function createStaticBatcher(THREE, root, options = {}) {
|
|
1132
|
+
const include = options.include ?? (() => true);
|
|
1133
|
+
const follow = options.followMeshes === true;
|
|
1134
|
+
root.updateMatrixWorld(true);
|
|
1135
|
+
const inverse = new THREE.Matrix4().copy(root.matrixWorld).invert();
|
|
1136
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1137
|
+
root.traverse((object) => {
|
|
1138
|
+
const mesh = object;
|
|
1139
|
+
if (!mesh.isMesh || !isShown(mesh, root) || !isBatchable(mesh) || !include(mesh)) return;
|
|
1140
|
+
const key = signatureOf(mesh);
|
|
1141
|
+
groups.set(key, [...groups.get(key) ?? [], mesh]);
|
|
1142
|
+
});
|
|
1143
|
+
const group = new THREE.Group();
|
|
1144
|
+
group.name = "static-batches";
|
|
1145
|
+
const sources = [];
|
|
1146
|
+
const pending = [...groups.values()].filter((meshes) => meshes.length >= 2);
|
|
1147
|
+
const mergeSome = (deadline) => {
|
|
1148
|
+
while (pending.length > 0) {
|
|
1149
|
+
const meshes = pending.shift();
|
|
1150
|
+
if (meshes.every((mesh) => isShown(mesh, root))) sources.push(...mergeGroup(THREE, { meshes, inverse: new THREE.Matrix4().copy(root.matrixWorld).invert(), group, follow }));
|
|
1151
|
+
if (performance.now() >= deadline) return;
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
if (options.budgetMs === void 0) mergeSome(Number.POSITIVE_INFINITY);
|
|
1155
|
+
root.add(group);
|
|
1156
|
+
const scratch = new THREE.Matrix4();
|
|
1157
|
+
let released = 0;
|
|
1158
|
+
const release = (source) => {
|
|
1159
|
+
source.released = true;
|
|
1160
|
+
source.mesh.layers.mask = source.mask;
|
|
1161
|
+
const { index } = source.batch;
|
|
1162
|
+
const first = index.array[source.start];
|
|
1163
|
+
for (let at = source.start; at < source.start + source.count; at++) index.array[at] = first;
|
|
1164
|
+
index.needsUpdate = true;
|
|
1165
|
+
if (--source.batch.live === 0) source.batch.mesh.visible = false;
|
|
1166
|
+
released++;
|
|
1167
|
+
};
|
|
1168
|
+
const hasChanged = (source) => {
|
|
1169
|
+
const { mesh } = source;
|
|
1170
|
+
if (mesh.geometry !== source.geometry || mesh.material !== source.material || !isShown(mesh, root)) return true;
|
|
1171
|
+
if (mesh.geometry.attributes.position.version !== source.version) return true;
|
|
1172
|
+
if (follow || sameMatrix(mesh.matrixWorld, source.world)) return false;
|
|
1173
|
+
if (!sameMatrix(scratch.multiplyMatrices(inverse, mesh.matrixWorld), source.local)) return true;
|
|
1174
|
+
source.world.copy(mesh.matrixWorld);
|
|
1175
|
+
return false;
|
|
1176
|
+
};
|
|
1177
|
+
return {
|
|
1178
|
+
group,
|
|
1179
|
+
update() {
|
|
1180
|
+
if (pending.length > 0) mergeSome(performance.now() + (options.budgetMs ?? Number.POSITIVE_INFINITY));
|
|
1181
|
+
const before = released;
|
|
1182
|
+
inverse.copy(root.matrixWorld).invert();
|
|
1183
|
+
for (const source of sources) if (!source.released && hasChanged(source)) release(source);
|
|
1184
|
+
return released - before;
|
|
1185
|
+
},
|
|
1186
|
+
stats: () => ({ batches: group.children.length, batched: sources.filter((source) => !source.released).length, released }),
|
|
1187
|
+
dispose() {
|
|
1188
|
+
for (const source of sources) if (!source.released) source.mesh.layers.mask = source.mask;
|
|
1189
|
+
group.removeFromParent();
|
|
1190
|
+
for (const child of group.children) {
|
|
1191
|
+
;
|
|
1192
|
+
child.geometry.dispose();
|
|
1193
|
+
child.skeleton?.dispose();
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
function copyAttribute(name, attribute, target, offset, matrix, normalMatrix) {
|
|
1199
|
+
const size = attribute.itemSize;
|
|
1200
|
+
const source = attribute.array;
|
|
1201
|
+
const flat = !attribute.isInterleavedBufferAttribute && source.length === attribute.count * size;
|
|
1202
|
+
if (name !== "position" && name !== "normal") {
|
|
1203
|
+
if (flat) target.set(source, offset * size);
|
|
1204
|
+
else for (let at = 0; at < attribute.count; at++) for (let k = 0; k < size; k++) target[(offset + at) * size + k] = attribute.getComponent(at, k);
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
const e = (name === "position" ? matrix : normalMatrix).elements;
|
|
1208
|
+
const isNormal = name === "normal";
|
|
1209
|
+
for (let at = 0; at < attribute.count; at++) {
|
|
1210
|
+
const x = attribute.getX(at);
|
|
1211
|
+
const y = attribute.getY(at);
|
|
1212
|
+
const z = attribute.getZ(at);
|
|
1213
|
+
const out = (offset + at) * 3;
|
|
1214
|
+
if (isNormal) {
|
|
1215
|
+
const nx = e[0] * x + e[3] * y + e[6] * z;
|
|
1216
|
+
const ny = e[1] * x + e[4] * y + e[7] * z;
|
|
1217
|
+
const nz = e[2] * x + e[5] * y + e[8] * z;
|
|
1218
|
+
const length = Math.hypot(nx, ny, nz) || 1;
|
|
1219
|
+
target[out] = nx / length;
|
|
1220
|
+
target[out + 1] = ny / length;
|
|
1221
|
+
target[out + 2] = nz / length;
|
|
1222
|
+
} else {
|
|
1223
|
+
target[out] = e[0] * x + e[4] * y + e[8] * z + e[12];
|
|
1224
|
+
target[out + 1] = e[1] * x + e[5] * y + e[9] * z + e[13];
|
|
1225
|
+
target[out + 2] = e[2] * x + e[6] * y + e[10] * z + e[14];
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
function skinnedBatch(THREE, merged, material, meshes) {
|
|
1230
|
+
const vertices = merged.attributes.position.count;
|
|
1231
|
+
const skinIndex = new Uint16Array(vertices * 4);
|
|
1232
|
+
const skinWeight = new Float32Array(vertices * 4);
|
|
1233
|
+
let at = 0;
|
|
1234
|
+
meshes.forEach((mesh2, bone) => {
|
|
1235
|
+
for (let k = 0; k < mesh2.geometry.attributes.position.count; k++, at++) {
|
|
1236
|
+
skinIndex[at * 4] = bone;
|
|
1237
|
+
skinWeight[at * 4] = 1;
|
|
1238
|
+
}
|
|
1239
|
+
});
|
|
1240
|
+
merged.setAttribute("skinIndex", new THREE.BufferAttribute(skinIndex, 4));
|
|
1241
|
+
merged.setAttribute("skinWeight", new THREE.BufferAttribute(skinWeight, 4));
|
|
1242
|
+
const mesh = new THREE.SkinnedMesh(merged, material);
|
|
1243
|
+
const identities = meshes.map(() => new THREE.Matrix4());
|
|
1244
|
+
mesh.bind(new THREE.Skeleton(meshes, identities), new THREE.Matrix4());
|
|
1245
|
+
mesh.frustumCulled = false;
|
|
1246
|
+
return mesh;
|
|
1247
|
+
}
|
|
1248
|
+
function mergeGroup(THREE, { meshes, inverse: rootInverse, group, follow }) {
|
|
1249
|
+
const identity = new THREE.Matrix4();
|
|
1250
|
+
const inverse = follow ? identity : rootInverse;
|
|
1251
|
+
const first = meshes[0];
|
|
1252
|
+
const names = ATTRIBUTES.filter((name) => first.geometry.attributes[name] !== void 0);
|
|
1253
|
+
const pieces = meshes.map((mesh2) => {
|
|
1254
|
+
const local = new THREE.Matrix4().multiplyMatrices(inverse, mesh2.matrixWorld);
|
|
1255
|
+
const vertices = mesh2.geometry.attributes.position.count;
|
|
1256
|
+
const indexCount = mesh2.geometry.index ? mesh2.geometry.index.count : vertices;
|
|
1257
|
+
return { mesh: mesh2, local, vertices, indexCount };
|
|
1258
|
+
});
|
|
1259
|
+
const totalVertices = pieces.reduce((sum, piece) => sum + piece.vertices, 0);
|
|
1260
|
+
const totalIndices = pieces.reduce((sum, piece) => sum + piece.indexCount, 0);
|
|
1261
|
+
const merged = new THREE.BufferGeometry();
|
|
1262
|
+
const normalMatrix = new THREE.Matrix3();
|
|
1263
|
+
for (const name of names) {
|
|
1264
|
+
const size = first.geometry.attributes[name].itemSize;
|
|
1265
|
+
const array = new Float32Array(totalVertices * size);
|
|
1266
|
+
let offset = 0;
|
|
1267
|
+
for (const piece of pieces) {
|
|
1268
|
+
const matrix = follow ? identity : piece.local;
|
|
1269
|
+
copyAttribute(name, piece.mesh.geometry.attributes[name], array, offset, matrix, normalMatrix.getNormalMatrix(matrix));
|
|
1270
|
+
offset += piece.vertices;
|
|
1271
|
+
}
|
|
1272
|
+
merged.setAttribute(name, new THREE.BufferAttribute(array, size));
|
|
1273
|
+
}
|
|
1274
|
+
const indexArray = totalVertices > 65535 ? new Uint32Array(totalIndices) : new Uint16Array(totalIndices);
|
|
1275
|
+
const indexAttribute = new THREE.BufferAttribute(indexArray, 1);
|
|
1276
|
+
merged.setIndex(indexAttribute);
|
|
1277
|
+
const mesh = follow ? skinnedBatch(THREE, merged, first.material, meshes) : new THREE.Mesh(merged, first.material);
|
|
1278
|
+
mesh.name = `batch:${first.material.name || first.material.type}`;
|
|
1279
|
+
mesh.castShadow = first.castShadow;
|
|
1280
|
+
mesh.receiveShadow = first.receiveShadow;
|
|
1281
|
+
mesh.raycast = () => void 0;
|
|
1282
|
+
merged.computeBoundingBox();
|
|
1283
|
+
merged.computeBoundingSphere();
|
|
1284
|
+
group.add(mesh);
|
|
1285
|
+
const batch = { mesh, live: pieces.length, index: indexAttribute };
|
|
1286
|
+
const sources = [];
|
|
1287
|
+
let vertexOffset = 0;
|
|
1288
|
+
let indexOffset = 0;
|
|
1289
|
+
for (const piece of pieces) {
|
|
1290
|
+
const index = piece.mesh.geometry.index;
|
|
1291
|
+
if (index) for (let at = 0; at < piece.indexCount; at++) indexArray[indexOffset + at] = index.getX(at) + vertexOffset;
|
|
1292
|
+
else for (let at = 0; at < piece.indexCount; at++) indexArray[indexOffset + at] = at + vertexOffset;
|
|
1293
|
+
sources.push({
|
|
1294
|
+
mesh: piece.mesh,
|
|
1295
|
+
geometry: piece.mesh.geometry,
|
|
1296
|
+
material: piece.mesh.material,
|
|
1297
|
+
version: piece.mesh.geometry.attributes.position.version,
|
|
1298
|
+
local: piece.local,
|
|
1299
|
+
world: piece.mesh.matrixWorld.clone(),
|
|
1300
|
+
mask: piece.mesh.layers.mask,
|
|
1301
|
+
batch,
|
|
1302
|
+
start: indexOffset,
|
|
1303
|
+
count: piece.indexCount,
|
|
1304
|
+
released: false
|
|
1305
|
+
});
|
|
1306
|
+
piece.mesh.layers.set(BATCHED_LAYER);
|
|
1307
|
+
vertexOffset += piece.vertices;
|
|
1308
|
+
indexOffset += piece.indexCount;
|
|
1309
|
+
}
|
|
1310
|
+
return sources;
|
|
1311
|
+
}
|
|
1312
|
+
export {
|
|
1313
|
+
BATCHED_LAYER,
|
|
1314
|
+
LIGHT_PRIORITY,
|
|
1315
|
+
NEUTRAL_FOG_FAR,
|
|
1316
|
+
NEUTRAL_FOG_NEAR,
|
|
1317
|
+
createFog,
|
|
1318
|
+
createFramePacer,
|
|
1319
|
+
createGeometryPool,
|
|
1320
|
+
createLightBudget,
|
|
1321
|
+
createMaterialAnchors,
|
|
1322
|
+
createMaterialPool,
|
|
1323
|
+
createProgramWarmup,
|
|
1324
|
+
createStaticBatcher,
|
|
1325
|
+
geometryKey,
|
|
1326
|
+
isLightBudget,
|
|
1327
|
+
isNeutralFog,
|
|
1328
|
+
isRenderPools,
|
|
1329
|
+
materialKey,
|
|
1330
|
+
programKeysOf,
|
|
1331
|
+
spriteProbes
|
|
1332
|
+
};
|
|
1333
|
+
//# sourceMappingURL=index.js.map
|