@react-three/fiber 10.0.0-alpha.2 → 10.0.0-canary.0706a92
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 -21
- package/dist/index.cjs +1274 -1405
- package/dist/index.d.cts +1888 -1713
- package/dist/index.d.mts +1888 -1713
- package/dist/index.d.ts +1888 -1713
- package/dist/index.mjs +1235 -1384
- package/dist/legacy.cjs +1231 -1386
- package/dist/legacy.d.cts +1889 -1714
- package/dist/legacy.d.mts +1889 -1714
- package/dist/legacy.d.ts +1889 -1714
- package/dist/legacy.mjs +1192 -1365
- package/dist/webgpu/index.cjs +1832 -1516
- package/dist/webgpu/index.d.cts +2082 -1737
- package/dist/webgpu/index.d.mts +2082 -1737
- package/dist/webgpu/index.d.ts +2082 -1737
- package/dist/webgpu/index.mjs +1790 -1496
- package/package.json +4 -1
- package/readme.md +244 -318
package/dist/legacy.mjs
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
import * as three from 'three';
|
|
2
|
-
import { WebGLRenderTarget, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1,
|
|
3
|
-
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
2
|
+
import { WebGLRenderTarget, CubeReflectionMapping, EquirectangularReflectionMapping, CubeTextureLoader, Scene, WebGLCubeRenderTarget, HalfFloatType, Color, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, ShaderMaterial, Uniform, SRGBColorSpace, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1, Raycaster, OrthographicCamera, PerspectiveCamera, PCFShadowMap, VSMShadowMap, BasicShadowMap, ACESFilmicToneMapping, WebGLRenderer } from 'three';
|
|
3
|
+
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
4
4
|
import * as React from 'react';
|
|
5
|
-
import React__default, {
|
|
5
|
+
import React__default, { useLayoutEffect, useRef, useMemo, useEffect, useContext, useImperativeHandle, useCallback, useState } from 'react';
|
|
6
6
|
import useMeasure from 'react-use-measure';
|
|
7
7
|
import { useFiber, useContextBridge, traverseFiber, FiberProvider } from 'its-fine';
|
|
8
|
+
import { useThree as useThree$1, useLoader as useLoader$1, useFrame as useFrame$1, createPortal as createPortal$1, applyProps as applyProps$1, extend as extend$1 } from '@react-three/fiber';
|
|
9
|
+
import { GroundedSkybox } from 'three/examples/jsm/objects/GroundedSkybox.js';
|
|
10
|
+
import { HDRLoader } from 'three/examples/jsm/loaders/HDRLoader.js';
|
|
11
|
+
import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js';
|
|
12
|
+
import { UltraHDRLoader } from 'three/examples/jsm/loaders/UltraHDRLoader.js';
|
|
13
|
+
import { GainMapLoader } from '@monogrid/gainmap-js';
|
|
8
14
|
import Tb, { unstable_scheduleCallback, unstable_IdlePriority } from 'scheduler';
|
|
9
15
|
import { createWithEqualityFn } from 'zustand/traditional';
|
|
16
|
+
import { getScheduler } from '@pmndrs/scheduler';
|
|
17
|
+
export { Scheduler, getScheduler } from '@pmndrs/scheduler';
|
|
10
18
|
import { suspend, preload, clear } from 'suspend-react';
|
|
11
19
|
|
|
12
20
|
function _mergeNamespaces(n, m) {
|
|
@@ -45,9 +53,392 @@ const THREE = /*#__PURE__*/_mergeNamespaces({
|
|
|
45
53
|
WebGPURenderer: WebGPURenderer
|
|
46
54
|
}, [three]);
|
|
47
55
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
const primaryRegistry = /* @__PURE__ */ new Map();
|
|
57
|
+
const pendingSubscribers = /* @__PURE__ */ new Map();
|
|
58
|
+
function registerPrimary(id, renderer, store) {
|
|
59
|
+
if (primaryRegistry.has(id)) {
|
|
60
|
+
console.warn(`Canvas with id="${id}" already registered. Overwriting.`);
|
|
61
|
+
}
|
|
62
|
+
const entry = { renderer, store };
|
|
63
|
+
primaryRegistry.set(id, entry);
|
|
64
|
+
const subscribers = pendingSubscribers.get(id);
|
|
65
|
+
if (subscribers) {
|
|
66
|
+
subscribers.forEach((callback) => callback(entry));
|
|
67
|
+
pendingSubscribers.delete(id);
|
|
68
|
+
}
|
|
69
|
+
return () => {
|
|
70
|
+
const currentEntry = primaryRegistry.get(id);
|
|
71
|
+
if (currentEntry?.renderer === renderer) {
|
|
72
|
+
primaryRegistry.delete(id);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function getPrimary(id) {
|
|
77
|
+
return primaryRegistry.get(id);
|
|
78
|
+
}
|
|
79
|
+
function waitForPrimary(id, timeout = 5e3) {
|
|
80
|
+
const existing = primaryRegistry.get(id);
|
|
81
|
+
if (existing) {
|
|
82
|
+
return Promise.resolve(existing);
|
|
83
|
+
}
|
|
84
|
+
return new Promise((resolve, reject) => {
|
|
85
|
+
const timeoutId = setTimeout(() => {
|
|
86
|
+
const subscribers = pendingSubscribers.get(id);
|
|
87
|
+
if (subscribers) {
|
|
88
|
+
const index = subscribers.indexOf(callback);
|
|
89
|
+
if (index !== -1) subscribers.splice(index, 1);
|
|
90
|
+
if (subscribers.length === 0) pendingSubscribers.delete(id);
|
|
91
|
+
}
|
|
92
|
+
reject(new Error(`Timeout waiting for canvas with id="${id}". Make sure a <Canvas id="${id}"> is mounted.`));
|
|
93
|
+
}, timeout);
|
|
94
|
+
const callback = (entry) => {
|
|
95
|
+
clearTimeout(timeoutId);
|
|
96
|
+
resolve(entry);
|
|
97
|
+
};
|
|
98
|
+
if (!pendingSubscribers.has(id)) {
|
|
99
|
+
pendingSubscribers.set(id, []);
|
|
100
|
+
}
|
|
101
|
+
pendingSubscribers.get(id).push(callback);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function hasPrimary(id) {
|
|
105
|
+
return primaryRegistry.has(id);
|
|
106
|
+
}
|
|
107
|
+
function unregisterPrimary(id) {
|
|
108
|
+
primaryRegistry.delete(id);
|
|
109
|
+
}
|
|
110
|
+
function getPrimaryIds() {
|
|
111
|
+
return Array.from(primaryRegistry.keys());
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const presetsObj = {
|
|
115
|
+
apartment: "lebombo_1k.hdr",
|
|
116
|
+
city: "potsdamer_platz_1k.hdr",
|
|
117
|
+
dawn: "kiara_1_dawn_1k.hdr",
|
|
118
|
+
forest: "forest_slope_1k.hdr",
|
|
119
|
+
lobby: "st_fagans_interior_1k.hdr",
|
|
120
|
+
night: "dikhololo_night_1k.hdr",
|
|
121
|
+
park: "rooitou_park_1k.hdr",
|
|
122
|
+
studio: "studio_small_03_1k.hdr",
|
|
123
|
+
sunset: "venice_sunset_1k.hdr",
|
|
124
|
+
warehouse: "empty_warehouse_01_1k.hdr"
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const CUBEMAP_ROOT = "https://raw.githack.com/pmndrs/drei-assets/456060a26bbeb8fdf79326f224b6d99b8bcce736/hdri/";
|
|
128
|
+
const isArray = (arr) => Array.isArray(arr);
|
|
129
|
+
const defaultFiles = ["/px.png", "/nx.png", "/py.png", "/ny.png", "/pz.png", "/nz.png"];
|
|
130
|
+
function useEnvironment({
|
|
131
|
+
files = defaultFiles,
|
|
132
|
+
path = "",
|
|
133
|
+
preset = void 0,
|
|
134
|
+
colorSpace = void 0,
|
|
135
|
+
extensions
|
|
136
|
+
} = {}) {
|
|
137
|
+
if (preset) {
|
|
138
|
+
validatePreset(preset);
|
|
139
|
+
files = presetsObj[preset];
|
|
140
|
+
path = CUBEMAP_ROOT;
|
|
141
|
+
}
|
|
142
|
+
const multiFile = isArray(files);
|
|
143
|
+
const { extension, isCubemap } = getExtension(files);
|
|
144
|
+
const loader = getLoader$1(extension);
|
|
145
|
+
if (!loader) throw new Error("useEnvironment: Unrecognized file extension: " + files);
|
|
146
|
+
const renderer = useThree$1((state) => state.renderer);
|
|
147
|
+
useLayoutEffect(() => {
|
|
148
|
+
if (extension !== "webp" && extension !== "jpg" && extension !== "jpeg") return;
|
|
149
|
+
function clearGainmapTexture() {
|
|
150
|
+
useLoader$1.clear(loader, multiFile ? [files] : files);
|
|
151
|
+
}
|
|
152
|
+
renderer.domElement.addEventListener("webglcontextlost", clearGainmapTexture, { once: true });
|
|
153
|
+
}, [extension, files, loader, multiFile, renderer.domElement]);
|
|
154
|
+
const loaderResult = useLoader$1(
|
|
155
|
+
loader,
|
|
156
|
+
multiFile ? [files] : files,
|
|
157
|
+
(loader2) => {
|
|
158
|
+
if (extension === "webp" || extension === "jpg" || extension === "jpeg") {
|
|
159
|
+
loader2.setRenderer?.(renderer);
|
|
160
|
+
}
|
|
161
|
+
loader2.setPath?.(path);
|
|
162
|
+
if (extensions) extensions(loader2);
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
let texture = multiFile ? (
|
|
166
|
+
// @ts-ignore
|
|
167
|
+
loaderResult[0]
|
|
168
|
+
) : loaderResult;
|
|
169
|
+
if (extension === "jpg" || extension === "jpeg" || extension === "webp") {
|
|
170
|
+
texture = texture.renderTarget?.texture;
|
|
171
|
+
}
|
|
172
|
+
texture.mapping = isCubemap ? CubeReflectionMapping : EquirectangularReflectionMapping;
|
|
173
|
+
texture.colorSpace = colorSpace ?? (isCubemap ? "srgb" : "srgb-linear");
|
|
174
|
+
return texture;
|
|
175
|
+
}
|
|
176
|
+
const preloadDefaultOptions = {
|
|
177
|
+
files: defaultFiles,
|
|
178
|
+
path: "",
|
|
179
|
+
preset: void 0,
|
|
180
|
+
extensions: void 0
|
|
181
|
+
};
|
|
182
|
+
useEnvironment.preload = (preloadOptions) => {
|
|
183
|
+
const options = { ...preloadDefaultOptions, ...preloadOptions };
|
|
184
|
+
let { files, path = "" } = options;
|
|
185
|
+
const { preset, extensions } = options;
|
|
186
|
+
if (preset) {
|
|
187
|
+
validatePreset(preset);
|
|
188
|
+
files = presetsObj[preset];
|
|
189
|
+
path = CUBEMAP_ROOT;
|
|
190
|
+
}
|
|
191
|
+
const { extension } = getExtension(files);
|
|
192
|
+
if (extension === "webp" || extension === "jpg" || extension === "jpeg") {
|
|
193
|
+
throw new Error("useEnvironment: Preloading gainmaps is not supported");
|
|
194
|
+
}
|
|
195
|
+
const loader = getLoader$1(extension);
|
|
196
|
+
if (!loader) throw new Error("useEnvironment: Unrecognized file extension: " + files);
|
|
197
|
+
useLoader$1.preload(loader, isArray(files) ? [files] : files, (loader2) => {
|
|
198
|
+
loader2.setPath?.(path);
|
|
199
|
+
if (extensions) extensions(loader2);
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
const clearDefaultOptins = {
|
|
203
|
+
files: defaultFiles,
|
|
204
|
+
preset: void 0
|
|
205
|
+
};
|
|
206
|
+
useEnvironment.clear = (clearOptions) => {
|
|
207
|
+
const options = { ...clearDefaultOptins, ...clearOptions };
|
|
208
|
+
let { files } = options;
|
|
209
|
+
const { preset } = options;
|
|
210
|
+
if (preset) {
|
|
211
|
+
validatePreset(preset);
|
|
212
|
+
files = presetsObj[preset];
|
|
213
|
+
}
|
|
214
|
+
const { extension } = getExtension(files);
|
|
215
|
+
const loader = getLoader$1(extension);
|
|
216
|
+
if (!loader) throw new Error("useEnvironment: Unrecognized file extension: " + files);
|
|
217
|
+
useLoader$1.clear(loader, isArray(files) ? [files] : files);
|
|
218
|
+
};
|
|
219
|
+
function validatePreset(preset) {
|
|
220
|
+
if (!(preset in presetsObj)) throw new Error("Preset must be one of: " + Object.keys(presetsObj).join(", "));
|
|
221
|
+
}
|
|
222
|
+
function getExtension(files) {
|
|
223
|
+
const isCubemap = isArray(files) && files.length === 6;
|
|
224
|
+
const isGainmap = isArray(files) && files.length === 3 && files.some((file) => file.endsWith("json"));
|
|
225
|
+
const firstEntry = isArray(files) ? files[0] : files;
|
|
226
|
+
const extension = isCubemap ? "cube" : isGainmap ? "webp" : firstEntry.startsWith("data:application/exr") ? "exr" : firstEntry.startsWith("data:application/hdr") ? "hdr" : firstEntry.startsWith("data:image/jpeg") ? "jpg" : firstEntry.split(".").pop()?.split("?")?.shift()?.toLowerCase();
|
|
227
|
+
return { extension, isCubemap, isGainmap };
|
|
228
|
+
}
|
|
229
|
+
function getLoader$1(extension) {
|
|
230
|
+
const loader = extension === "cube" ? CubeTextureLoader : extension === "hdr" ? HDRLoader : extension === "exr" ? EXRLoader : extension === "jpg" || extension === "jpeg" ? UltraHDRLoader : extension === "webp" ? GainMapLoader : null;
|
|
231
|
+
return loader;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const isRef$1 = (obj) => obj.current && obj.current.isScene;
|
|
235
|
+
const resolveScene = (scene) => isRef$1(scene) ? scene.current : scene;
|
|
236
|
+
function setEnvProps(background, scene, defaultScene, texture, sceneProps = {}) {
|
|
237
|
+
sceneProps = {
|
|
238
|
+
backgroundBlurriness: 0,
|
|
239
|
+
backgroundIntensity: 1,
|
|
240
|
+
backgroundRotation: [0, 0, 0],
|
|
241
|
+
environmentIntensity: 1,
|
|
242
|
+
environmentRotation: [0, 0, 0],
|
|
243
|
+
...sceneProps
|
|
244
|
+
};
|
|
245
|
+
const target = resolveScene(scene || defaultScene);
|
|
246
|
+
const oldbg = target.background;
|
|
247
|
+
const oldenv = target.environment;
|
|
248
|
+
const oldSceneProps = {
|
|
249
|
+
// @ts-ignore
|
|
250
|
+
backgroundBlurriness: target.backgroundBlurriness,
|
|
251
|
+
// @ts-ignore
|
|
252
|
+
backgroundIntensity: target.backgroundIntensity,
|
|
253
|
+
// @ts-ignore
|
|
254
|
+
backgroundRotation: target.backgroundRotation?.clone?.() ?? [0, 0, 0],
|
|
255
|
+
// @ts-ignore
|
|
256
|
+
environmentIntensity: target.environmentIntensity,
|
|
257
|
+
// @ts-ignore
|
|
258
|
+
environmentRotation: target.environmentRotation?.clone?.() ?? [0, 0, 0]
|
|
259
|
+
};
|
|
260
|
+
if (background !== "only") target.environment = texture;
|
|
261
|
+
if (background) target.background = texture;
|
|
262
|
+
applyProps$1(target, sceneProps);
|
|
263
|
+
return () => {
|
|
264
|
+
if (background !== "only") target.environment = oldenv;
|
|
265
|
+
if (background) target.background = oldbg;
|
|
266
|
+
applyProps$1(target, oldSceneProps);
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
function EnvironmentMap({ scene, background = false, map, ...config }) {
|
|
270
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
271
|
+
React.useLayoutEffect(() => {
|
|
272
|
+
if (map) return setEnvProps(background, scene, defaultScene, map, config);
|
|
273
|
+
});
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
function EnvironmentCube({
|
|
277
|
+
background = false,
|
|
278
|
+
scene,
|
|
279
|
+
blur,
|
|
280
|
+
backgroundBlurriness,
|
|
281
|
+
backgroundIntensity,
|
|
282
|
+
backgroundRotation,
|
|
283
|
+
environmentIntensity,
|
|
284
|
+
environmentRotation,
|
|
285
|
+
...rest
|
|
286
|
+
}) {
|
|
287
|
+
const texture = useEnvironment(rest);
|
|
288
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
289
|
+
React.useLayoutEffect(() => {
|
|
290
|
+
return setEnvProps(background, scene, defaultScene, texture, {
|
|
291
|
+
backgroundBlurriness: blur ?? backgroundBlurriness,
|
|
292
|
+
backgroundIntensity,
|
|
293
|
+
backgroundRotation,
|
|
294
|
+
environmentIntensity,
|
|
295
|
+
environmentRotation
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
React.useEffect(() => {
|
|
299
|
+
return () => {
|
|
300
|
+
texture.dispose();
|
|
301
|
+
};
|
|
302
|
+
}, [texture]);
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
function EnvironmentPortal({
|
|
306
|
+
children,
|
|
307
|
+
near = 0.1,
|
|
308
|
+
far = 1e3,
|
|
309
|
+
resolution = 256,
|
|
310
|
+
frames = 1,
|
|
311
|
+
map,
|
|
312
|
+
background = false,
|
|
313
|
+
blur,
|
|
314
|
+
backgroundBlurriness,
|
|
315
|
+
backgroundIntensity,
|
|
316
|
+
backgroundRotation,
|
|
317
|
+
environmentIntensity,
|
|
318
|
+
environmentRotation,
|
|
319
|
+
scene,
|
|
320
|
+
files,
|
|
321
|
+
path,
|
|
322
|
+
preset = void 0,
|
|
323
|
+
extensions
|
|
324
|
+
}) {
|
|
325
|
+
const gl = useThree$1((state) => state.gl);
|
|
326
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
327
|
+
const camera = React.useRef(null);
|
|
328
|
+
const [virtualScene] = React.useState(() => new Scene());
|
|
329
|
+
const fbo = React.useMemo(() => {
|
|
330
|
+
const fbo2 = new WebGLCubeRenderTarget(resolution);
|
|
331
|
+
fbo2.texture.type = HalfFloatType;
|
|
332
|
+
return fbo2;
|
|
333
|
+
}, [resolution]);
|
|
334
|
+
React.useEffect(() => {
|
|
335
|
+
return () => {
|
|
336
|
+
fbo.dispose();
|
|
337
|
+
};
|
|
338
|
+
}, [fbo]);
|
|
339
|
+
React.useLayoutEffect(() => {
|
|
340
|
+
if (frames === 1) {
|
|
341
|
+
const autoClear = gl.autoClear;
|
|
342
|
+
gl.autoClear = true;
|
|
343
|
+
camera.current.update(gl, virtualScene);
|
|
344
|
+
gl.autoClear = autoClear;
|
|
345
|
+
}
|
|
346
|
+
return setEnvProps(background, scene, defaultScene, fbo.texture, {
|
|
347
|
+
backgroundBlurriness: blur ?? backgroundBlurriness,
|
|
348
|
+
backgroundIntensity,
|
|
349
|
+
backgroundRotation,
|
|
350
|
+
environmentIntensity,
|
|
351
|
+
environmentRotation
|
|
352
|
+
});
|
|
353
|
+
}, [
|
|
354
|
+
children,
|
|
355
|
+
virtualScene,
|
|
356
|
+
fbo.texture,
|
|
357
|
+
scene,
|
|
358
|
+
defaultScene,
|
|
359
|
+
background,
|
|
360
|
+
frames,
|
|
361
|
+
gl,
|
|
362
|
+
blur,
|
|
363
|
+
backgroundBlurriness,
|
|
364
|
+
backgroundIntensity,
|
|
365
|
+
backgroundRotation,
|
|
366
|
+
environmentIntensity,
|
|
367
|
+
environmentRotation
|
|
368
|
+
]);
|
|
369
|
+
let count = 1;
|
|
370
|
+
useFrame$1(() => {
|
|
371
|
+
if (frames === Infinity || count < frames) {
|
|
372
|
+
const autoClear = gl.autoClear;
|
|
373
|
+
gl.autoClear = true;
|
|
374
|
+
camera.current.update(gl, virtualScene);
|
|
375
|
+
gl.autoClear = autoClear;
|
|
376
|
+
count++;
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
return /* @__PURE__ */ jsx(Fragment, { children: createPortal$1(
|
|
380
|
+
/* @__PURE__ */ jsxs(Fragment, { children: [
|
|
381
|
+
children,
|
|
382
|
+
/* @__PURE__ */ jsx("cubeCamera", { ref: camera, args: [near, far, fbo] }),
|
|
383
|
+
files || preset ? /* @__PURE__ */ jsx(EnvironmentCube, { background: true, files, preset, path, extensions }) : map ? /* @__PURE__ */ jsx(EnvironmentMap, { background: true, map, extensions }) : null
|
|
384
|
+
] }),
|
|
385
|
+
virtualScene
|
|
386
|
+
) });
|
|
387
|
+
}
|
|
388
|
+
function EnvironmentGround(props) {
|
|
389
|
+
const textureDefault = useEnvironment(props);
|
|
390
|
+
const texture = props.map || textureDefault;
|
|
391
|
+
React.useMemo(() => extend$1({ GroundProjectedEnvImpl: GroundedSkybox }), []);
|
|
392
|
+
React.useEffect(() => {
|
|
393
|
+
return () => {
|
|
394
|
+
textureDefault.dispose();
|
|
395
|
+
};
|
|
396
|
+
}, [textureDefault]);
|
|
397
|
+
const height = props.ground?.height ?? 15;
|
|
398
|
+
const radius = props.ground?.radius ?? 60;
|
|
399
|
+
const scale = props.ground?.scale ?? 1e3;
|
|
400
|
+
const args = React.useMemo(
|
|
401
|
+
() => [texture, height, radius],
|
|
402
|
+
[texture, height, radius]
|
|
403
|
+
);
|
|
404
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
405
|
+
/* @__PURE__ */ jsx(EnvironmentMap, { ...props, map: texture }),
|
|
406
|
+
/* @__PURE__ */ jsx("groundProjectedEnvImpl", { args, scale })
|
|
407
|
+
] });
|
|
408
|
+
}
|
|
409
|
+
function EnvironmentColor({ color, scene }) {
|
|
410
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
411
|
+
React.useLayoutEffect(() => {
|
|
412
|
+
if (color === void 0) return;
|
|
413
|
+
const target = resolveScene(scene || defaultScene);
|
|
414
|
+
const oldBg = target.background;
|
|
415
|
+
target.background = new Color(color);
|
|
416
|
+
return () => {
|
|
417
|
+
target.background = oldBg;
|
|
418
|
+
};
|
|
419
|
+
});
|
|
420
|
+
return null;
|
|
421
|
+
}
|
|
422
|
+
function EnvironmentDualSource(props) {
|
|
423
|
+
const { backgroundFiles, ...envProps } = props;
|
|
424
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
425
|
+
/* @__PURE__ */ jsx(EnvironmentCube, { ...envProps, background: false }),
|
|
426
|
+
/* @__PURE__ */ jsx(EnvironmentCube, { ...props, files: backgroundFiles, background: "only" })
|
|
427
|
+
] });
|
|
428
|
+
}
|
|
429
|
+
function Environment(props) {
|
|
430
|
+
if (props.color && !props.files && !props.preset && !props.map) {
|
|
431
|
+
return /* @__PURE__ */ jsx(EnvironmentColor, { ...props });
|
|
432
|
+
}
|
|
433
|
+
if (props.backgroundFiles && props.backgroundFiles !== props.files) {
|
|
434
|
+
return /* @__PURE__ */ jsx(EnvironmentDualSource, { ...props });
|
|
435
|
+
}
|
|
436
|
+
return props.ground ? /* @__PURE__ */ jsx(EnvironmentGround, { ...props }) : props.map ? /* @__PURE__ */ jsx(EnvironmentMap, { ...props }) : props.children ? /* @__PURE__ */ jsx(EnvironmentPortal, { ...props }) : /* @__PURE__ */ jsx(EnvironmentCube, { ...props });
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
var __defProp = Object.defineProperty;
|
|
440
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
441
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
51
442
|
const act = React["act"];
|
|
52
443
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React.useLayoutEffect : React.useEffect;
|
|
53
444
|
function useMutableCallback(fn) {
|
|
@@ -79,7 +470,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
79
470
|
return _a = class extends React.Component {
|
|
80
471
|
constructor() {
|
|
81
472
|
super(...arguments);
|
|
82
|
-
__publicField
|
|
473
|
+
__publicField(this, "state", { error: false });
|
|
83
474
|
}
|
|
84
475
|
componentDidCatch(err) {
|
|
85
476
|
this.props.set(err);
|
|
@@ -87,7 +478,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
87
478
|
render() {
|
|
88
479
|
return this.state.error ? null : this.props.children;
|
|
89
480
|
}
|
|
90
|
-
}, __publicField
|
|
481
|
+
}, __publicField(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
91
482
|
})();
|
|
92
483
|
|
|
93
484
|
const is = {
|
|
@@ -273,7 +664,7 @@ function createOcclusionObserverNode(store, uniform) {
|
|
|
273
664
|
let occlusionSetupPromise = null;
|
|
274
665
|
function enableOcclusion(store) {
|
|
275
666
|
const state = store.getState();
|
|
276
|
-
const { internal, renderer
|
|
667
|
+
const { internal, renderer } = state;
|
|
277
668
|
if (internal.occlusionEnabled || occlusionSetupPromise) return;
|
|
278
669
|
const hasOcclusionSupport = typeof renderer?.isOccluded === "function";
|
|
279
670
|
if (!hasOcclusionSupport) {
|
|
@@ -381,6 +772,17 @@ function unregisterVisibility(store, object) {
|
|
|
381
772
|
internal.visibilityRegistry.delete(object.uuid);
|
|
382
773
|
internal.occlusionCache.delete(object);
|
|
383
774
|
}
|
|
775
|
+
function updateVisibilityHandlers(store, object, handlers) {
|
|
776
|
+
const { internal } = store.getState();
|
|
777
|
+
const entry = internal.visibilityRegistry.get(object.uuid);
|
|
778
|
+
if (!entry) return false;
|
|
779
|
+
entry.handlers = handlers;
|
|
780
|
+
if ((handlers.onOccluded || handlers.onVisible) && !internal.occlusionEnabled) {
|
|
781
|
+
object.occlusionTest = true;
|
|
782
|
+
enableOcclusion(store);
|
|
783
|
+
}
|
|
784
|
+
return true;
|
|
785
|
+
}
|
|
384
786
|
function checkVisibility(state) {
|
|
385
787
|
const { internal, camera } = state;
|
|
386
788
|
const registry = internal.visibilityRegistry;
|
|
@@ -436,6 +838,22 @@ function hasVisibilityHandlers(handlers) {
|
|
|
436
838
|
return !!(handlers.onFramed || handlers.onOccluded || handlers.onVisible);
|
|
437
839
|
}
|
|
438
840
|
|
|
841
|
+
const FROM_REF = Symbol.for("@react-three/fiber.fromRef");
|
|
842
|
+
function fromRef(ref) {
|
|
843
|
+
return { [FROM_REF]: ref };
|
|
844
|
+
}
|
|
845
|
+
function isFromRef(value) {
|
|
846
|
+
return value !== null && typeof value === "object" && FROM_REF in value;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
const ONCE = Symbol.for("@react-three/fiber.once");
|
|
850
|
+
function once(...args) {
|
|
851
|
+
return { [ONCE]: args.length ? args : true };
|
|
852
|
+
}
|
|
853
|
+
function isOnce(value) {
|
|
854
|
+
return value !== null && typeof value === "object" && ONCE in value;
|
|
855
|
+
}
|
|
856
|
+
|
|
439
857
|
const RESERVED_PROPS = [
|
|
440
858
|
"children",
|
|
441
859
|
"key",
|
|
@@ -506,7 +924,7 @@ function getMemoizedPrototype(root) {
|
|
|
506
924
|
ctor = new root.constructor();
|
|
507
925
|
MEMOIZED_PROTOTYPES.set(root.constructor, ctor);
|
|
508
926
|
}
|
|
509
|
-
} catch
|
|
927
|
+
} catch {
|
|
510
928
|
}
|
|
511
929
|
return ctor;
|
|
512
930
|
}
|
|
@@ -552,6 +970,22 @@ function applyProps(object, props) {
|
|
|
552
970
|
continue;
|
|
553
971
|
}
|
|
554
972
|
if (value === void 0) continue;
|
|
973
|
+
if (isFromRef(value)) continue;
|
|
974
|
+
if (isOnce(value)) {
|
|
975
|
+
if (instance?.appliedOnce?.has(prop)) continue;
|
|
976
|
+
if (instance) {
|
|
977
|
+
instance.appliedOnce ?? (instance.appliedOnce = /* @__PURE__ */ new Set());
|
|
978
|
+
instance.appliedOnce.add(prop);
|
|
979
|
+
}
|
|
980
|
+
const { root: targetRoot, key: targetKey } = resolve(object, prop);
|
|
981
|
+
const args = value[ONCE];
|
|
982
|
+
if (typeof targetRoot[targetKey] === "function") {
|
|
983
|
+
targetRoot[targetKey](...args === true ? [] : args);
|
|
984
|
+
} else if (args !== true && args.length > 0) {
|
|
985
|
+
targetRoot[targetKey] = args[0];
|
|
986
|
+
}
|
|
987
|
+
continue;
|
|
988
|
+
}
|
|
555
989
|
let { root, key, target } = resolve(object, prop);
|
|
556
990
|
if (target === void 0 && (typeof root !== "object" || root === null)) {
|
|
557
991
|
throw Error(`R3F: Cannot set "${prop}". Ensure it is an object before setting "${key}".`);
|
|
@@ -572,9 +1006,25 @@ function applyProps(object, props) {
|
|
|
572
1006
|
} else if (isVectorLike(target) && is.num(value)) {
|
|
573
1007
|
if ("setScalar" in target && typeof target.setScalar === "function") target.setScalar(value);
|
|
574
1008
|
else target.set(value);
|
|
1009
|
+
} else if (root instanceof ShaderMaterial && key === "uniforms" && is.obj(value)) {
|
|
1010
|
+
if (!is.obj(root.uniforms)) root.uniforms = {};
|
|
1011
|
+
const uniforms = root.uniforms;
|
|
1012
|
+
const nextUniforms = value;
|
|
1013
|
+
for (const name in nextUniforms) {
|
|
1014
|
+
const uniform = nextUniforms[name];
|
|
1015
|
+
const targetUniform = uniforms[name];
|
|
1016
|
+
if (targetUniform) Object.assign(targetUniform, uniform);
|
|
1017
|
+
else {
|
|
1018
|
+
const nextUniform = uniform instanceof Uniform ? uniform.clone() : new Uniform(uniform.value);
|
|
1019
|
+
uniforms[name] = nextUniform;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
575
1022
|
} else {
|
|
576
1023
|
root[key] = value;
|
|
577
|
-
if (
|
|
1024
|
+
if (key.endsWith("Node") && root.isMaterial) {
|
|
1025
|
+
root.needsUpdate = true;
|
|
1026
|
+
}
|
|
1027
|
+
if (rootState && rootState.renderer?.outputColorSpace === SRGBColorSpace && colorMaps.includes(key) && isTexture(value) && root[key]?.isTexture && // sRGB textures must be RGBA8 since r137 https://github.com/mrdoob/three.js/pull/23129
|
|
578
1028
|
root[key].format === RGBAFormat && root[key].type === UnsignedByteType) {
|
|
579
1029
|
root[key].colorSpace = rootState.textureColorSpace;
|
|
580
1030
|
}
|
|
@@ -587,6 +1037,9 @@ function applyProps(object, props) {
|
|
|
587
1037
|
if (instance.eventCount && object2.raycast !== null) {
|
|
588
1038
|
rootState.internal.interaction.push(object2);
|
|
589
1039
|
}
|
|
1040
|
+
}
|
|
1041
|
+
if (instance?.parent && rootState?.internal && instance.object?.isObject3D) {
|
|
1042
|
+
const object2 = instance.object;
|
|
590
1043
|
const root = findInitialRoot(instance);
|
|
591
1044
|
const visibilityHandlers = {
|
|
592
1045
|
onFramed: instance.handlers.onFramed,
|
|
@@ -594,7 +1047,9 @@ function applyProps(object, props) {
|
|
|
594
1047
|
onVisible: instance.handlers.onVisible
|
|
595
1048
|
};
|
|
596
1049
|
if (hasVisibilityHandlers(visibilityHandlers)) {
|
|
597
|
-
|
|
1050
|
+
if (!updateVisibilityHandlers(root, object2, visibilityHandlers)) {
|
|
1051
|
+
registerVisibility(root, object2, visibilityHandlers);
|
|
1052
|
+
}
|
|
598
1053
|
} else {
|
|
599
1054
|
unregisterVisibility(root, object2);
|
|
600
1055
|
}
|
|
@@ -607,38 +1062,60 @@ function applyProps(object, props) {
|
|
|
607
1062
|
return object;
|
|
608
1063
|
}
|
|
609
1064
|
|
|
1065
|
+
const DEFAULT_POINTER_ID = 0;
|
|
1066
|
+
const XR_POINTER_ID_START = 1e3;
|
|
1067
|
+
function getPointerState(internal, pointerId) {
|
|
1068
|
+
let state = internal.pointerMap.get(pointerId);
|
|
1069
|
+
if (!state) {
|
|
1070
|
+
state = {
|
|
1071
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1072
|
+
captured: /* @__PURE__ */ new Map(),
|
|
1073
|
+
initialClick: [0, 0],
|
|
1074
|
+
initialHits: []
|
|
1075
|
+
};
|
|
1076
|
+
internal.pointerMap.set(pointerId, state);
|
|
1077
|
+
}
|
|
1078
|
+
return state;
|
|
1079
|
+
}
|
|
1080
|
+
function getPointerId(event) {
|
|
1081
|
+
return "pointerId" in event ? event.pointerId : DEFAULT_POINTER_ID;
|
|
1082
|
+
}
|
|
610
1083
|
function makeId(event) {
|
|
611
1084
|
return (event.eventObject || event.object).uuid + "/" + event.index + event.instanceId;
|
|
612
1085
|
}
|
|
613
|
-
function releaseInternalPointerCapture(
|
|
614
|
-
const
|
|
1086
|
+
function releaseInternalPointerCapture(internal, obj, pointerId) {
|
|
1087
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1088
|
+
if (!pointerState) return;
|
|
1089
|
+
const captureData = pointerState.captured.get(obj);
|
|
615
1090
|
if (captureData) {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
capturedMap.delete(pointerId);
|
|
619
|
-
captureData.target.releasePointerCapture(pointerId);
|
|
620
|
-
}
|
|
1091
|
+
pointerState.captured.delete(obj);
|
|
1092
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
621
1093
|
}
|
|
622
1094
|
}
|
|
623
1095
|
function removeInteractivity(store, object) {
|
|
624
1096
|
const { internal } = store.getState();
|
|
625
1097
|
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
1098
|
+
for (const [pointerId, pointerState] of internal.pointerMap) {
|
|
1099
|
+
pointerState.initialHits = pointerState.initialHits.filter((o) => o !== object);
|
|
1100
|
+
pointerState.hovered.forEach((value, key) => {
|
|
1101
|
+
if (value.eventObject === object || value.object === object) {
|
|
1102
|
+
pointerState.hovered.delete(key);
|
|
1103
|
+
}
|
|
1104
|
+
});
|
|
1105
|
+
if (pointerState.captured.has(object)) {
|
|
1106
|
+
releaseInternalPointerCapture(internal, object, pointerId);
|
|
630
1107
|
}
|
|
631
|
-
}
|
|
632
|
-
internal.capturedMap.forEach((captures, pointerId) => {
|
|
633
|
-
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
634
|
-
});
|
|
1108
|
+
}
|
|
635
1109
|
unregisterVisibility(store, object);
|
|
636
1110
|
}
|
|
637
1111
|
function createEvents(store) {
|
|
638
|
-
function calculateDistance(event) {
|
|
1112
|
+
function calculateDistance(event, pointerId) {
|
|
639
1113
|
const { internal } = store.getState();
|
|
640
|
-
const
|
|
641
|
-
|
|
1114
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1115
|
+
if (!pointerState) return 0;
|
|
1116
|
+
const [initialX, initialY] = pointerState.initialClick;
|
|
1117
|
+
const dx = event.offsetX - initialX;
|
|
1118
|
+
const dy = event.offsetY - initialY;
|
|
642
1119
|
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
643
1120
|
}
|
|
644
1121
|
function filterPointerEvents(objects) {
|
|
@@ -674,6 +1151,15 @@ function createEvents(store) {
|
|
|
674
1151
|
return state2.raycaster.camera ? state2.raycaster.intersectObject(obj, true) : [];
|
|
675
1152
|
}
|
|
676
1153
|
let hits = eventsObjects.flatMap(handleRaycast).sort((a, b) => {
|
|
1154
|
+
const aInteractivePriority = a.object.userData?.interactivePriority;
|
|
1155
|
+
const bInteractivePriority = b.object.userData?.interactivePriority;
|
|
1156
|
+
if (aInteractivePriority !== void 0 || bInteractivePriority !== void 0) {
|
|
1157
|
+
if (aInteractivePriority !== void 0 && bInteractivePriority === void 0) return -1;
|
|
1158
|
+
if (bInteractivePriority !== void 0 && aInteractivePriority === void 0) return 1;
|
|
1159
|
+
if (aInteractivePriority !== bInteractivePriority) {
|
|
1160
|
+
return (bInteractivePriority ?? 0) - (aInteractivePriority ?? 0);
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
677
1163
|
const aState = getRootState(a.object);
|
|
678
1164
|
const bState = getRootState(b.object);
|
|
679
1165
|
const aPriority = aState?.events?.priority ?? 1;
|
|
@@ -695,9 +1181,13 @@ function createEvents(store) {
|
|
|
695
1181
|
eventObject = eventObject.parent;
|
|
696
1182
|
}
|
|
697
1183
|
}
|
|
698
|
-
if ("pointerId" in event
|
|
699
|
-
|
|
700
|
-
|
|
1184
|
+
if ("pointerId" in event) {
|
|
1185
|
+
const pointerId = event.pointerId;
|
|
1186
|
+
const pointerState = state.internal.pointerMap.get(pointerId);
|
|
1187
|
+
if (pointerState?.captured.size) {
|
|
1188
|
+
for (const captureData of pointerState.captured.values()) {
|
|
1189
|
+
if (!duplicates.has(makeId(captureData.intersection))) intersections.push(captureData.intersection);
|
|
1190
|
+
}
|
|
701
1191
|
}
|
|
702
1192
|
}
|
|
703
1193
|
return intersections;
|
|
@@ -710,27 +1200,25 @@ function createEvents(store) {
|
|
|
710
1200
|
if (state) {
|
|
711
1201
|
const { raycaster, pointer, camera, internal } = state;
|
|
712
1202
|
const unprojectedPoint = new Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
713
|
-
const hasPointerCapture = (id) =>
|
|
1203
|
+
const hasPointerCapture = (id) => {
|
|
1204
|
+
const pointerState = internal.pointerMap.get(id);
|
|
1205
|
+
return pointerState?.captured.has(hit.eventObject) ?? false;
|
|
1206
|
+
};
|
|
714
1207
|
const setPointerCapture = (id) => {
|
|
715
1208
|
const captureData = { intersection: hit, target: event.target };
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
} else {
|
|
719
|
-
internal.capturedMap.set(id, /* @__PURE__ */ new Map([[hit.eventObject, captureData]]));
|
|
720
|
-
}
|
|
1209
|
+
const pointerState = getPointerState(internal, id);
|
|
1210
|
+
pointerState.captured.set(hit.eventObject, captureData);
|
|
721
1211
|
event.target.setPointerCapture(id);
|
|
722
1212
|
};
|
|
723
1213
|
const releasePointerCapture = (id) => {
|
|
724
|
-
|
|
725
|
-
if (captures) {
|
|
726
|
-
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
727
|
-
}
|
|
1214
|
+
releaseInternalPointerCapture(internal, hit.eventObject, id);
|
|
728
1215
|
};
|
|
729
1216
|
const extractEventProps = {};
|
|
730
1217
|
for (const prop in event) {
|
|
731
1218
|
const property = event[prop];
|
|
732
1219
|
if (typeof property !== "function") extractEventProps[prop] = property;
|
|
733
1220
|
}
|
|
1221
|
+
const eventPointerId = "pointerId" in event ? event.pointerId : void 0;
|
|
734
1222
|
const raycastEvent = {
|
|
735
1223
|
...hit,
|
|
736
1224
|
...extractEventProps,
|
|
@@ -741,18 +1229,19 @@ function createEvents(store) {
|
|
|
741
1229
|
unprojectedPoint,
|
|
742
1230
|
ray: raycaster.ray,
|
|
743
1231
|
camera,
|
|
1232
|
+
pointerId: eventPointerId,
|
|
744
1233
|
// Hijack stopPropagation, which just sets a flag
|
|
745
1234
|
stopPropagation() {
|
|
746
|
-
const
|
|
1235
|
+
const pointerState = eventPointerId !== void 0 ? internal.pointerMap.get(eventPointerId) : void 0;
|
|
747
1236
|
if (
|
|
748
1237
|
// ...if this pointer hasn't been captured
|
|
749
|
-
!
|
|
750
|
-
|
|
1238
|
+
!pointerState?.captured.size || // ... or if the hit object is capturing the pointer
|
|
1239
|
+
pointerState.captured.has(hit.eventObject)
|
|
751
1240
|
) {
|
|
752
1241
|
raycastEvent.stopped = localState.stopped = true;
|
|
753
|
-
if (
|
|
1242
|
+
if (pointerState?.hovered.size && Array.from(pointerState.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
754
1243
|
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
755
|
-
cancelPointer([...higher, hit]);
|
|
1244
|
+
cancelPointer([...higher, hit], eventPointerId);
|
|
756
1245
|
}
|
|
757
1246
|
}
|
|
758
1247
|
},
|
|
@@ -768,15 +1257,18 @@ function createEvents(store) {
|
|
|
768
1257
|
}
|
|
769
1258
|
return intersections;
|
|
770
1259
|
}
|
|
771
|
-
function cancelPointer(intersections) {
|
|
1260
|
+
function cancelPointer(intersections, pointerId) {
|
|
772
1261
|
const { internal } = store.getState();
|
|
773
|
-
|
|
1262
|
+
const pid = pointerId ?? DEFAULT_POINTER_ID;
|
|
1263
|
+
const pointerState = internal.pointerMap.get(pid);
|
|
1264
|
+
if (!pointerState) return;
|
|
1265
|
+
for (const [hoveredId, hoveredObj] of pointerState.hovered) {
|
|
774
1266
|
if (!intersections.length || !intersections.find(
|
|
775
1267
|
(hit) => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId
|
|
776
1268
|
)) {
|
|
777
1269
|
const eventObject = hoveredObj.eventObject;
|
|
778
1270
|
const instance = eventObject.__r3f;
|
|
779
|
-
|
|
1271
|
+
pointerState.hovered.delete(hoveredId);
|
|
780
1272
|
if (instance?.eventCount) {
|
|
781
1273
|
const handlers = instance.handlers;
|
|
782
1274
|
const data = { ...hoveredObj, intersections };
|
|
@@ -805,41 +1297,118 @@ function createEvents(store) {
|
|
|
805
1297
|
instance?.handlers.onDropMissed?.(event);
|
|
806
1298
|
}
|
|
807
1299
|
}
|
|
1300
|
+
function cleanupPointer(pointerId) {
|
|
1301
|
+
const { internal } = store.getState();
|
|
1302
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1303
|
+
if (pointerState) {
|
|
1304
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1305
|
+
const eventObject = hoveredObj.eventObject;
|
|
1306
|
+
const instance = eventObject.__r3f;
|
|
1307
|
+
if (instance?.eventCount) {
|
|
1308
|
+
const handlers = instance.handlers;
|
|
1309
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1310
|
+
handlers.onPointerOut?.(data);
|
|
1311
|
+
handlers.onPointerLeave?.(data);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
internal.pointerMap.delete(pointerId);
|
|
1315
|
+
}
|
|
1316
|
+
internal.pointerDirty.delete(pointerId);
|
|
1317
|
+
}
|
|
1318
|
+
function processDeferredPointer(event, pointerId) {
|
|
1319
|
+
const state = store.getState();
|
|
1320
|
+
const { internal } = state;
|
|
1321
|
+
if (!state.events.enabled) return;
|
|
1322
|
+
const filter = filterPointerEvents;
|
|
1323
|
+
const hits = intersect(event, filter);
|
|
1324
|
+
cancelPointer(hits, pointerId);
|
|
1325
|
+
function onIntersect(data) {
|
|
1326
|
+
const eventObject = data.eventObject;
|
|
1327
|
+
const instance = eventObject.__r3f;
|
|
1328
|
+
if (!instance?.eventCount) return;
|
|
1329
|
+
const handlers = instance.handlers;
|
|
1330
|
+
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1331
|
+
const id = makeId(data);
|
|
1332
|
+
const pointerState = getPointerState(internal, pointerId);
|
|
1333
|
+
const hoveredItem = pointerState.hovered.get(id);
|
|
1334
|
+
if (!hoveredItem) {
|
|
1335
|
+
pointerState.hovered.set(id, data);
|
|
1336
|
+
handlers.onPointerOver?.(data);
|
|
1337
|
+
handlers.onPointerEnter?.(data);
|
|
1338
|
+
} else if (hoveredItem.stopped) {
|
|
1339
|
+
data.stopPropagation();
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
handlers.onPointerMove?.(data);
|
|
1343
|
+
}
|
|
1344
|
+
handleIntersects(hits, event, 0, onIntersect);
|
|
1345
|
+
}
|
|
808
1346
|
function handlePointer(name) {
|
|
809
1347
|
switch (name) {
|
|
810
1348
|
case "onPointerLeave":
|
|
811
|
-
case "onPointerCancel":
|
|
812
1349
|
case "onDragLeave":
|
|
813
1350
|
return () => cancelPointer([]);
|
|
1351
|
+
// Global cancel of these events
|
|
1352
|
+
case "onPointerCancel":
|
|
1353
|
+
return (event) => {
|
|
1354
|
+
const pointerId = getPointerId(event);
|
|
1355
|
+
cleanupPointer(pointerId);
|
|
1356
|
+
};
|
|
814
1357
|
case "onLostPointerCapture":
|
|
815
1358
|
return (event) => {
|
|
816
1359
|
const { internal } = store.getState();
|
|
817
|
-
|
|
1360
|
+
const pointerId = getPointerId(event);
|
|
1361
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1362
|
+
if (pointerState?.captured.size) {
|
|
818
1363
|
requestAnimationFrame(() => {
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
1364
|
+
const pointerState2 = internal.pointerMap.get(pointerId);
|
|
1365
|
+
if (pointerState2?.captured.size) {
|
|
1366
|
+
pointerState2.captured.clear();
|
|
822
1367
|
}
|
|
1368
|
+
cancelPointer([], pointerId);
|
|
823
1369
|
});
|
|
824
1370
|
}
|
|
825
1371
|
};
|
|
826
1372
|
}
|
|
827
1373
|
return function handleEvent(event) {
|
|
828
1374
|
const state = store.getState();
|
|
829
|
-
const { onPointerMissed, onDragOverMissed, onDropMissed, internal } = state;
|
|
1375
|
+
const { onPointerMissed, onDragOverMissed, onDropMissed, internal, events } = state;
|
|
1376
|
+
const pointerId = getPointerId(event);
|
|
830
1377
|
internal.lastEvent.current = event;
|
|
831
|
-
if (!
|
|
1378
|
+
if (!events.enabled) return;
|
|
832
1379
|
const isPointerMove = name === "onPointerMove";
|
|
833
1380
|
const isDragOver = name === "onDragOver";
|
|
834
1381
|
const isDrop = name === "onDrop";
|
|
835
1382
|
const isClickEvent = name === "onClick" || name === "onContextMenu" || name === "onDoubleClick";
|
|
1383
|
+
const isPointerDown = name === "onPointerDown";
|
|
1384
|
+
const isPointerUp = name === "onPointerUp";
|
|
1385
|
+
const isWheel = name === "onWheel";
|
|
1386
|
+
const canDeferRaycasts = events.frameTimedRaycasts && state.frameloop === "always";
|
|
1387
|
+
if (isPointerMove && canDeferRaycasts) {
|
|
1388
|
+
events.compute?.(event, state);
|
|
1389
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
if (isWheel && canDeferRaycasts && !events.alwaysFireOnScroll) {
|
|
1393
|
+
events.compute?.(event, state);
|
|
1394
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
if ((isClickEvent || isPointerDown || isPointerUp) && internal.pointerDirty.has(pointerId)) {
|
|
1398
|
+
const deferredEvent = internal.pointerDirty.get(pointerId);
|
|
1399
|
+
internal.pointerDirty.delete(pointerId);
|
|
1400
|
+
processDeferredPointer(deferredEvent, pointerId);
|
|
1401
|
+
}
|
|
836
1402
|
const filter = isPointerMove || isDragOver || isDrop ? filterPointerEvents : void 0;
|
|
837
1403
|
const hits = intersect(event, filter);
|
|
838
|
-
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
839
|
-
if (
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
1404
|
+
const delta = isClickEvent ? calculateDistance(event, pointerId) : 0;
|
|
1405
|
+
if (isPointerDown) {
|
|
1406
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1407
|
+
pointerState2.initialClick = [event.offsetX, event.offsetY];
|
|
1408
|
+
pointerState2.initialHits = hits.map((hit) => hit.eventObject);
|
|
1409
|
+
}
|
|
1410
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1411
|
+
const initialHits = pointerState?.initialHits ?? [];
|
|
843
1412
|
if (isClickEvent && !hits.length) {
|
|
844
1413
|
if (delta <= 2) {
|
|
845
1414
|
pointerMissed(event, internal.interaction);
|
|
@@ -854,7 +1423,9 @@ function createEvents(store) {
|
|
|
854
1423
|
dropMissed(event, internal.interaction);
|
|
855
1424
|
if (onDropMissed) onDropMissed(event);
|
|
856
1425
|
}
|
|
857
|
-
if (isPointerMove || isDragOver)
|
|
1426
|
+
if (isPointerMove || isDragOver) {
|
|
1427
|
+
cancelPointer(hits, pointerId);
|
|
1428
|
+
}
|
|
858
1429
|
function onIntersect(data) {
|
|
859
1430
|
const eventObject = data.eventObject;
|
|
860
1431
|
const instance = eventObject.__r3f;
|
|
@@ -863,9 +1434,10 @@ function createEvents(store) {
|
|
|
863
1434
|
if (isPointerMove) {
|
|
864
1435
|
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
865
1436
|
const id = makeId(data);
|
|
866
|
-
const
|
|
1437
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1438
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
867
1439
|
if (!hoveredItem) {
|
|
868
|
-
|
|
1440
|
+
pointerState2.hovered.set(id, data);
|
|
869
1441
|
handlers.onPointerOver?.(data);
|
|
870
1442
|
handlers.onPointerEnter?.(data);
|
|
871
1443
|
} else if (hoveredItem.stopped) {
|
|
@@ -875,9 +1447,10 @@ function createEvents(store) {
|
|
|
875
1447
|
handlers.onPointerMove?.(data);
|
|
876
1448
|
} else if (isDragOver) {
|
|
877
1449
|
const id = makeId(data);
|
|
878
|
-
const
|
|
1450
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1451
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
879
1452
|
if (!hoveredItem) {
|
|
880
|
-
|
|
1453
|
+
pointerState2.hovered.set(id, data);
|
|
881
1454
|
handlers.onDragOverEnter?.(data);
|
|
882
1455
|
} else if (hoveredItem.stopped) {
|
|
883
1456
|
data.stopPropagation();
|
|
@@ -888,18 +1461,18 @@ function createEvents(store) {
|
|
|
888
1461
|
} else {
|
|
889
1462
|
const handler = handlers[name];
|
|
890
1463
|
if (handler) {
|
|
891
|
-
if (!isClickEvent ||
|
|
1464
|
+
if (!isClickEvent || initialHits.includes(eventObject)) {
|
|
892
1465
|
pointerMissed(
|
|
893
1466
|
event,
|
|
894
|
-
internal.interaction.filter((object) => !
|
|
1467
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
895
1468
|
);
|
|
896
1469
|
handler(data);
|
|
897
1470
|
}
|
|
898
1471
|
} else {
|
|
899
|
-
if (isClickEvent &&
|
|
1472
|
+
if (isClickEvent && initialHits.includes(eventObject)) {
|
|
900
1473
|
pointerMissed(
|
|
901
1474
|
event,
|
|
902
|
-
internal.interaction.filter((object) => !
|
|
1475
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
903
1476
|
);
|
|
904
1477
|
}
|
|
905
1478
|
}
|
|
@@ -908,7 +1481,15 @@ function createEvents(store) {
|
|
|
908
1481
|
handleIntersects(hits, event, delta, onIntersect);
|
|
909
1482
|
};
|
|
910
1483
|
}
|
|
911
|
-
|
|
1484
|
+
function flushDeferredPointers() {
|
|
1485
|
+
const { internal, events } = store.getState();
|
|
1486
|
+
if (!events.frameTimedRaycasts) return;
|
|
1487
|
+
for (const [pointerId, event] of internal.pointerDirty) {
|
|
1488
|
+
processDeferredPointer(event, pointerId);
|
|
1489
|
+
}
|
|
1490
|
+
internal.pointerDirty.clear();
|
|
1491
|
+
}
|
|
1492
|
+
return { handlePointer, flushDeferredPointers, processDeferredPointer };
|
|
912
1493
|
}
|
|
913
1494
|
const DOM_EVENTS = {
|
|
914
1495
|
onClick: ["click", false],
|
|
@@ -927,11 +1508,16 @@ const DOM_EVENTS = {
|
|
|
927
1508
|
onLostPointerCapture: ["lostpointercapture", true]
|
|
928
1509
|
};
|
|
929
1510
|
function createPointerEvents(store) {
|
|
930
|
-
const { handlePointer } = createEvents(store);
|
|
1511
|
+
const { handlePointer, flushDeferredPointers, processDeferredPointer } = createEvents(store);
|
|
1512
|
+
let nextXRPointerId = XR_POINTER_ID_START;
|
|
1513
|
+
const xrPointers = /* @__PURE__ */ new Map();
|
|
931
1514
|
return {
|
|
932
1515
|
priority: 1,
|
|
933
1516
|
enabled: true,
|
|
934
|
-
|
|
1517
|
+
frameTimedRaycasts: true,
|
|
1518
|
+
alwaysFireOnScroll: true,
|
|
1519
|
+
updateOnFrame: false,
|
|
1520
|
+
compute(event, state) {
|
|
935
1521
|
state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
936
1522
|
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
937
1523
|
},
|
|
@@ -940,11 +1526,33 @@ function createPointerEvents(store) {
|
|
|
940
1526
|
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
941
1527
|
{}
|
|
942
1528
|
),
|
|
943
|
-
update: () => {
|
|
1529
|
+
update: (pointerId) => {
|
|
944
1530
|
const { events, internal } = store.getState();
|
|
945
|
-
if (
|
|
1531
|
+
if (!events.handlers) return;
|
|
1532
|
+
if (pointerId !== void 0) {
|
|
1533
|
+
const event = internal.pointerDirty.get(pointerId);
|
|
1534
|
+
if (event) {
|
|
1535
|
+
internal.pointerDirty.delete(pointerId);
|
|
1536
|
+
processDeferredPointer(event, pointerId);
|
|
1537
|
+
} else if (internal.lastEvent?.current) {
|
|
1538
|
+
processDeferredPointer(internal.lastEvent.current, pointerId);
|
|
1539
|
+
}
|
|
1540
|
+
} else {
|
|
1541
|
+
flushDeferredPointers();
|
|
1542
|
+
if (internal.lastEvent?.current) {
|
|
1543
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
},
|
|
1547
|
+
flush: () => {
|
|
1548
|
+
const { events, internal } = store.getState();
|
|
1549
|
+
flushDeferredPointers();
|
|
1550
|
+
if (events.updateOnFrame && internal.lastEvent?.current && events.handlers) {
|
|
1551
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1552
|
+
}
|
|
946
1553
|
},
|
|
947
1554
|
connect: (target) => {
|
|
1555
|
+
if (!target) return;
|
|
948
1556
|
const { set, events } = store.getState();
|
|
949
1557
|
events.disconnect?.();
|
|
950
1558
|
set((state) => ({ events: { ...state.events, connected: target } }));
|
|
@@ -968,6 +1576,33 @@ function createPointerEvents(store) {
|
|
|
968
1576
|
}
|
|
969
1577
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
970
1578
|
}
|
|
1579
|
+
},
|
|
1580
|
+
// PREVIEW (A8): the XR pointer registration API may change before stable.
|
|
1581
|
+
registerPointer: (config) => {
|
|
1582
|
+
const pointerId = nextXRPointerId++;
|
|
1583
|
+
xrPointers.set(pointerId, config);
|
|
1584
|
+
const { internal } = store.getState();
|
|
1585
|
+
getPointerState(internal, pointerId);
|
|
1586
|
+
return pointerId;
|
|
1587
|
+
},
|
|
1588
|
+
unregisterPointer: (pointerId) => {
|
|
1589
|
+
xrPointers.delete(pointerId);
|
|
1590
|
+
const { internal } = store.getState();
|
|
1591
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1592
|
+
if (pointerState) {
|
|
1593
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1594
|
+
const eventObject = hoveredObj.eventObject;
|
|
1595
|
+
const instance = eventObject.__r3f;
|
|
1596
|
+
if (instance?.eventCount) {
|
|
1597
|
+
const handlers = instance.handlers;
|
|
1598
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1599
|
+
handlers.onPointerOut?.(data);
|
|
1600
|
+
handlers.onPointerLeave?.(data);
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
internal.pointerMap.delete(pointerId);
|
|
1604
|
+
}
|
|
1605
|
+
internal.pointerDirty.delete(pointerId);
|
|
971
1606
|
}
|
|
972
1607
|
};
|
|
973
1608
|
}
|
|
@@ -1029,6 +1664,14 @@ function notifyAlpha({ message, link }) {
|
|
|
1029
1664
|
}
|
|
1030
1665
|
}
|
|
1031
1666
|
|
|
1667
|
+
const INTERNAL_LIB_MARKERS = ["zustand", "setState", "Object.assign"];
|
|
1668
|
+
const INTERNAL_FIBER_MARKERS = ["packages/fiber/src", "packages/fiber/dist", "@react-three/fiber", "@react-three_fiber"];
|
|
1669
|
+
function isInternalRendererAccess(stack, callerDepth = 2) {
|
|
1670
|
+
if (!stack) return false;
|
|
1671
|
+
const caller = stack.split("\n")[callerDepth] ?? "";
|
|
1672
|
+
return INTERNAL_LIB_MARKERS.some((m) => caller.includes(m)) || INTERNAL_FIBER_MARKERS.some((m) => caller.includes(m));
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1032
1675
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
1033
1676
|
const context = globalThis[R3F_CONTEXT] ?? (globalThis[R3F_CONTEXT] = React.createContext(null));
|
|
1034
1677
|
const createStore = (invalidate, advance) => {
|
|
@@ -1058,23 +1701,29 @@ const createStore = (invalidate, advance) => {
|
|
|
1058
1701
|
set,
|
|
1059
1702
|
get,
|
|
1060
1703
|
// Mock objects that have to be configured
|
|
1704
|
+
// primaryStore is set after store creation (self-reference for primary, primary's store for secondary)
|
|
1705
|
+
primaryStore: null,
|
|
1061
1706
|
gl: null,
|
|
1062
1707
|
renderer: null,
|
|
1063
1708
|
camera: null,
|
|
1064
1709
|
frustum: new Frustum(),
|
|
1065
1710
|
autoUpdateFrustum: true,
|
|
1066
1711
|
raycaster: null,
|
|
1067
|
-
events: {
|
|
1712
|
+
events: {
|
|
1713
|
+
priority: 1,
|
|
1714
|
+
enabled: true,
|
|
1715
|
+
connected: false,
|
|
1716
|
+
frameTimedRaycasts: true,
|
|
1717
|
+
alwaysFireOnScroll: true,
|
|
1718
|
+
updateOnFrame: false
|
|
1719
|
+
},
|
|
1068
1720
|
scene: null,
|
|
1069
1721
|
rootScene: null,
|
|
1070
1722
|
xr: null,
|
|
1071
1723
|
inspector: null,
|
|
1072
1724
|
invalidate: (frames = 1, stackFrames = false) => invalidate(get(), frames, stackFrames),
|
|
1073
1725
|
advance: (timestamp, runGlobalEffects) => advance(timestamp, runGlobalEffects, get()),
|
|
1074
|
-
|
|
1075
|
-
linear: false,
|
|
1076
|
-
flat: false,
|
|
1077
|
-
textureColorSpace: "srgb",
|
|
1726
|
+
textureColorSpace: SRGBColorSpace,
|
|
1078
1727
|
isLegacy: false,
|
|
1079
1728
|
webGPUSupported: false,
|
|
1080
1729
|
isNative: false,
|
|
@@ -1132,6 +1781,7 @@ const createStore = (invalidate, advance) => {
|
|
|
1132
1781
|
size: newSize,
|
|
1133
1782
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
1134
1783
|
}));
|
|
1784
|
+
getScheduler().invalidate();
|
|
1135
1785
|
}
|
|
1136
1786
|
}
|
|
1137
1787
|
return;
|
|
@@ -1146,6 +1796,7 @@ const createStore = (invalidate, advance) => {
|
|
|
1146
1796
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
1147
1797
|
_sizeImperative: true
|
|
1148
1798
|
}));
|
|
1799
|
+
getScheduler().invalidate();
|
|
1149
1800
|
},
|
|
1150
1801
|
setDpr: (dpr) => set((state2) => {
|
|
1151
1802
|
const resolved = calculateDpr(dpr);
|
|
@@ -1153,14 +1804,18 @@ const createStore = (invalidate, advance) => {
|
|
|
1153
1804
|
}),
|
|
1154
1805
|
setFrameloop: (frameloop = "always") => {
|
|
1155
1806
|
set(() => ({ frameloop }));
|
|
1807
|
+
getScheduler().frameloop = frameloop;
|
|
1156
1808
|
},
|
|
1157
1809
|
setError: (error) => set(() => ({ error })),
|
|
1158
1810
|
error: null,
|
|
1159
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useTextures,
|
|
1811
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
1160
1812
|
uniforms: {},
|
|
1161
1813
|
nodes: {},
|
|
1814
|
+
buffers: {},
|
|
1815
|
+
gpuStorage: {},
|
|
1162
1816
|
textures: /* @__PURE__ */ new Map(),
|
|
1163
|
-
|
|
1817
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1818
|
+
renderPipeline: null,
|
|
1164
1819
|
passes: {},
|
|
1165
1820
|
_hmrVersion: 0,
|
|
1166
1821
|
_sizeImperative: false,
|
|
@@ -1169,12 +1824,16 @@ const createStore = (invalidate, advance) => {
|
|
|
1169
1824
|
internal: {
|
|
1170
1825
|
// Events
|
|
1171
1826
|
interaction: [],
|
|
1172
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
1173
1827
|
subscribers: [],
|
|
1828
|
+
// Per-pointer state (new unified structure)
|
|
1829
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1830
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1831
|
+
lastEvent: React.createRef(),
|
|
1832
|
+
// Deprecated but kept for backwards compatibility
|
|
1833
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1174
1834
|
initialClick: [0, 0],
|
|
1175
1835
|
initialHits: [],
|
|
1176
1836
|
capturedMap: /* @__PURE__ */ new Map(),
|
|
1177
|
-
lastEvent: React.createRef(),
|
|
1178
1837
|
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
1179
1838
|
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
1180
1839
|
// Occlusion system (WebGPU only)
|
|
@@ -1213,8 +1872,7 @@ const createStore = (invalidate, advance) => {
|
|
|
1213
1872
|
const currentState = rootStore.getState();
|
|
1214
1873
|
if (!currentState.isLegacy && currentState.internal.actualRenderer) {
|
|
1215
1874
|
const stack = new Error().stack || "";
|
|
1216
|
-
|
|
1217
|
-
if (!isInternalAccess) {
|
|
1875
|
+
if (!isInternalRendererAccess(stack)) {
|
|
1218
1876
|
const cleanedStack = stack.split("\n").slice(2).join("\n") || "Stack trace unavailable";
|
|
1219
1877
|
notifyDepreciated({
|
|
1220
1878
|
heading: "Accessing state.gl in WebGPU mode",
|
|
@@ -1257,13 +1915,22 @@ const createStore = (invalidate, advance) => {
|
|
|
1257
1915
|
rootStore.subscribe(() => {
|
|
1258
1916
|
const { camera, size, viewport, set, internal } = rootStore.getState();
|
|
1259
1917
|
const actualRenderer = internal.actualRenderer;
|
|
1918
|
+
const canvasTarget = internal.canvasTarget;
|
|
1260
1919
|
if (size.width !== oldSize.width || size.height !== oldSize.height || viewport.dpr !== oldDpr) {
|
|
1261
1920
|
oldSize = size;
|
|
1262
1921
|
oldDpr = viewport.dpr;
|
|
1263
1922
|
updateCamera(camera, size);
|
|
1264
|
-
if (
|
|
1265
|
-
|
|
1266
|
-
|
|
1923
|
+
if (internal.isSecondary && canvasTarget) {
|
|
1924
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1925
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1926
|
+
} else {
|
|
1927
|
+
if (viewport.dpr > 0) actualRenderer.setPixelRatio(viewport.dpr);
|
|
1928
|
+
actualRenderer.setSize(size.width, size.height, false);
|
|
1929
|
+
if (canvasTarget) {
|
|
1930
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1931
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1267
1934
|
}
|
|
1268
1935
|
if (camera !== oldCamera) {
|
|
1269
1936
|
oldCamera = camera;
|
|
@@ -1318,1054 +1985,28 @@ function loadingFn(extensions, onProgress) {
|
|
|
1318
1985
|
);
|
|
1319
1986
|
};
|
|
1320
1987
|
}
|
|
1321
|
-
function useLoader(loader, input, extensions, onProgress) {
|
|
1322
|
-
const
|
|
1988
|
+
function useLoader(loader, input, extensions, onProgress, cacheKey) {
|
|
1989
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
1990
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
1323
1991
|
const fn = loadingFn(extensions, onProgress);
|
|
1324
|
-
const results =
|
|
1992
|
+
const results = cacheKeys.map(
|
|
1993
|
+
(key, index) => suspend(() => fn(loader, inputs[index]), [loader, key], { equal: is.equ })
|
|
1994
|
+
);
|
|
1325
1995
|
return Array.isArray(input) ? results : results[0];
|
|
1326
1996
|
}
|
|
1327
|
-
useLoader.preload = function(loader, input, extensions, onProgress) {
|
|
1328
|
-
const
|
|
1329
|
-
|
|
1997
|
+
useLoader.preload = function(loader, input, extensions, onProgress, cacheKey) {
|
|
1998
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
1999
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2000
|
+
cacheKeys.forEach(
|
|
2001
|
+
(key, index) => preload(() => loadingFn(extensions, onProgress)(loader, inputs[index]), [loader, key])
|
|
2002
|
+
);
|
|
1330
2003
|
};
|
|
1331
|
-
useLoader.clear = function(loader, input) {
|
|
1332
|
-
const
|
|
1333
|
-
|
|
2004
|
+
useLoader.clear = function(loader, input, cacheKey) {
|
|
2005
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2006
|
+
cacheKeys.forEach((key) => clear([loader, key]));
|
|
1334
2007
|
};
|
|
1335
2008
|
useLoader.loader = getLoader;
|
|
1336
2009
|
|
|
1337
|
-
var __defProp$1 = Object.defineProperty;
|
|
1338
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1339
|
-
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1340
|
-
const DEFAULT_PHASES = ["start", "input", "physics", "update", "render", "finish"];
|
|
1341
|
-
class PhaseGraph {
|
|
1342
|
-
constructor() {
|
|
1343
|
-
/** Ordered list of phase nodes */
|
|
1344
|
-
__publicField$1(this, "phases", []);
|
|
1345
|
-
/** Quick lookup by name */
|
|
1346
|
-
__publicField$1(this, "phaseMap", /* @__PURE__ */ new Map());
|
|
1347
|
-
/** Cached ordered names (invalidated on changes) */
|
|
1348
|
-
__publicField$1(this, "orderedNamesCache", null);
|
|
1349
|
-
this.initializeDefaultPhases();
|
|
1350
|
-
}
|
|
1351
|
-
//* Initialization --------------------------------
|
|
1352
|
-
initializeDefaultPhases() {
|
|
1353
|
-
for (const name of DEFAULT_PHASES) {
|
|
1354
|
-
const node = { name, isAutoGenerated: false };
|
|
1355
|
-
this.phases.push(node);
|
|
1356
|
-
this.phaseMap.set(name, node);
|
|
1357
|
-
}
|
|
1358
|
-
this.invalidateCache();
|
|
1359
|
-
}
|
|
1360
|
-
//* Public API --------------------------------
|
|
1361
|
-
/**
|
|
1362
|
-
* Add a named phase to the graph
|
|
1363
|
-
* @param name - Phase name (must be unique)
|
|
1364
|
-
* @param options - Position options (before or after another phase)
|
|
1365
|
-
*/
|
|
1366
|
-
addPhase(name, options = {}) {
|
|
1367
|
-
if (this.phaseMap.has(name)) {
|
|
1368
|
-
console.warn(`[useFrame] Phase "${name}" already exists`);
|
|
1369
|
-
return;
|
|
1370
|
-
}
|
|
1371
|
-
const { before, after } = options;
|
|
1372
|
-
const node = { name, isAutoGenerated: false };
|
|
1373
|
-
let insertIndex = this.phases.length;
|
|
1374
|
-
const targetIndex = this.getPhaseIndex(before ?? after);
|
|
1375
|
-
if (targetIndex !== -1) {
|
|
1376
|
-
insertIndex = before ? targetIndex : targetIndex + 1;
|
|
1377
|
-
} else {
|
|
1378
|
-
const constraintType = before ? "before" : "after";
|
|
1379
|
-
console.warn(`[useFrame] Phase "${before ?? after}" not found for '${constraintType}' constraint`);
|
|
1380
|
-
}
|
|
1381
|
-
this.phases.splice(insertIndex, 0, node);
|
|
1382
|
-
this.phaseMap.set(name, node);
|
|
1383
|
-
this.invalidateCache();
|
|
1384
|
-
}
|
|
1385
|
-
/**
|
|
1386
|
-
* Get ordered list of phase names
|
|
1387
|
-
*/
|
|
1388
|
-
getOrderedPhases() {
|
|
1389
|
-
if (this.orderedNamesCache === null) this.orderedNamesCache = this.phases.map((p) => p.name);
|
|
1390
|
-
return this.orderedNamesCache;
|
|
1391
|
-
}
|
|
1392
|
-
/**
|
|
1393
|
-
* Check if a phase exists
|
|
1394
|
-
*/
|
|
1395
|
-
hasPhase(name) {
|
|
1396
|
-
return this.phaseMap.has(name);
|
|
1397
|
-
}
|
|
1398
|
-
/**
|
|
1399
|
-
* Get the index of a phase (-1 if not found)
|
|
1400
|
-
*/
|
|
1401
|
-
getPhaseIndex(name) {
|
|
1402
|
-
if (!name) return -1;
|
|
1403
|
-
return this.phases.findIndex((p) => p.name === name);
|
|
1404
|
-
}
|
|
1405
|
-
/**
|
|
1406
|
-
* Ensure a phase exists, creating an auto-generated one if needed.
|
|
1407
|
-
* Used for resolving before/after constraints.
|
|
1408
|
-
*
|
|
1409
|
-
* @param name - The phase name to ensure exists
|
|
1410
|
-
* @returns The phase name (may be auto-generated like 'before:render')
|
|
1411
|
-
*/
|
|
1412
|
-
ensurePhase(name) {
|
|
1413
|
-
if (this.phaseMap.has(name)) return name;
|
|
1414
|
-
const node = { name, isAutoGenerated: true };
|
|
1415
|
-
this.phases.push(node);
|
|
1416
|
-
this.phaseMap.set(name, node);
|
|
1417
|
-
this.invalidateCache();
|
|
1418
|
-
return name;
|
|
1419
|
-
}
|
|
1420
|
-
/**
|
|
1421
|
-
* Resolve where a job with before/after constraints should go.
|
|
1422
|
-
* Creates auto-generated phases if needed.
|
|
1423
|
-
*
|
|
1424
|
-
* @param before - Phase(s) to run before
|
|
1425
|
-
* @param after - Phase(s) to run after
|
|
1426
|
-
* @returns The resolved phase name
|
|
1427
|
-
*/
|
|
1428
|
-
resolveConstraintPhase(before, after) {
|
|
1429
|
-
const beforeArr = before ? Array.isArray(before) ? before : [before] : [];
|
|
1430
|
-
const afterArr = after ? Array.isArray(after) ? after : [after] : [];
|
|
1431
|
-
if (beforeArr.length > 0) {
|
|
1432
|
-
return this.ensureAutoPhase(beforeArr[0], "before", 0);
|
|
1433
|
-
}
|
|
1434
|
-
if (afterArr.length > 0) {
|
|
1435
|
-
return this.ensureAutoPhase(afterArr[0], "after", 1);
|
|
1436
|
-
}
|
|
1437
|
-
return "update";
|
|
1438
|
-
}
|
|
1439
|
-
/**
|
|
1440
|
-
* Ensure an auto-generated phase exists relative to a target phase.
|
|
1441
|
-
* Creates the phase if it doesn't exist, inserting it at the correct position.
|
|
1442
|
-
*
|
|
1443
|
-
* @param target - The target phase name to position relative to
|
|
1444
|
-
* @param prefix - Prefix for auto-generated phase name ('before' or 'after')
|
|
1445
|
-
* @param offset - Insertion offset (0 for before, 1 for after)
|
|
1446
|
-
* @returns The auto-generated phase name
|
|
1447
|
-
*/
|
|
1448
|
-
ensureAutoPhase(target, prefix, offset) {
|
|
1449
|
-
const autoName = `${prefix}:${target}`;
|
|
1450
|
-
if (this.phaseMap.has(autoName)) return autoName;
|
|
1451
|
-
const node = { name: autoName, isAutoGenerated: true };
|
|
1452
|
-
const targetIndex = this.getPhaseIndex(target);
|
|
1453
|
-
if (targetIndex !== -1) this.phases.splice(targetIndex + offset, 0, node);
|
|
1454
|
-
else this.phases.push(node);
|
|
1455
|
-
this.phaseMap.set(autoName, node);
|
|
1456
|
-
this.invalidateCache();
|
|
1457
|
-
return autoName;
|
|
1458
|
-
}
|
|
1459
|
-
// Internal --------------------------------
|
|
1460
|
-
invalidateCache() {
|
|
1461
|
-
this.orderedNamesCache = null;
|
|
1462
|
-
}
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
|
-
function rebuildSortedJobs(jobs, phaseGraph) {
|
|
1466
|
-
const orderedPhases = phaseGraph.getOrderedPhases();
|
|
1467
|
-
const buckets = /* @__PURE__ */ new Map();
|
|
1468
|
-
for (const phase of orderedPhases) {
|
|
1469
|
-
buckets.set(phase, []);
|
|
1470
|
-
}
|
|
1471
|
-
for (const job of jobs.values()) {
|
|
1472
|
-
if (!job.enabled) continue;
|
|
1473
|
-
let bucket = buckets.get(job.phase);
|
|
1474
|
-
if (!bucket) {
|
|
1475
|
-
bucket = [];
|
|
1476
|
-
buckets.set(job.phase, bucket);
|
|
1477
|
-
}
|
|
1478
|
-
bucket.push(job);
|
|
1479
|
-
}
|
|
1480
|
-
const sortedBuckets = [];
|
|
1481
|
-
for (const phase of orderedPhases) {
|
|
1482
|
-
const bucket = buckets.get(phase);
|
|
1483
|
-
if (!bucket || bucket.length === 0) continue;
|
|
1484
|
-
bucket.sort((a, b) => {
|
|
1485
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1486
|
-
return a.index - b.index;
|
|
1487
|
-
});
|
|
1488
|
-
sortedBuckets.push(hasCrossJobConstraints(bucket) ? topologicalSort(bucket) : bucket);
|
|
1489
|
-
}
|
|
1490
|
-
for (const [phase, bucket] of buckets) {
|
|
1491
|
-
if (!orderedPhases.includes(phase) && bucket.length > 0) {
|
|
1492
|
-
bucket.sort((a, b) => {
|
|
1493
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1494
|
-
return a.index - b.index;
|
|
1495
|
-
});
|
|
1496
|
-
sortedBuckets.push(bucket);
|
|
1497
|
-
}
|
|
1498
|
-
}
|
|
1499
|
-
return sortedBuckets.flat();
|
|
1500
|
-
}
|
|
1501
|
-
function hasCrossJobConstraints(bucket) {
|
|
1502
|
-
const jobIds = new Set(bucket.map((j) => j.id));
|
|
1503
|
-
for (const job of bucket) {
|
|
1504
|
-
for (const ref of job.before) {
|
|
1505
|
-
if (jobIds.has(ref)) return true;
|
|
1506
|
-
}
|
|
1507
|
-
for (const ref of job.after) {
|
|
1508
|
-
if (jobIds.has(ref)) return true;
|
|
1509
|
-
}
|
|
1510
|
-
}
|
|
1511
|
-
return false;
|
|
1512
|
-
}
|
|
1513
|
-
function topologicalSort(jobs) {
|
|
1514
|
-
const n = jobs.length;
|
|
1515
|
-
if (n <= 1) return jobs;
|
|
1516
|
-
const jobMap = /* @__PURE__ */ new Map();
|
|
1517
|
-
const inDegree = /* @__PURE__ */ new Map();
|
|
1518
|
-
const adjacency = /* @__PURE__ */ new Map();
|
|
1519
|
-
for (const job of jobs) {
|
|
1520
|
-
jobMap.set(job.id, job);
|
|
1521
|
-
inDegree.set(job.id, 0);
|
|
1522
|
-
adjacency.set(job.id, []);
|
|
1523
|
-
}
|
|
1524
|
-
for (const job of jobs) {
|
|
1525
|
-
for (const ref of job.before) {
|
|
1526
|
-
if (jobMap.has(ref)) {
|
|
1527
|
-
adjacency.get(job.id).push(ref);
|
|
1528
|
-
inDegree.set(ref, inDegree.get(ref) + 1);
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
for (const ref of job.after) {
|
|
1532
|
-
if (jobMap.has(ref)) {
|
|
1533
|
-
adjacency.get(ref).push(job.id);
|
|
1534
|
-
inDegree.set(job.id, inDegree.get(job.id) + 1);
|
|
1535
|
-
}
|
|
1536
|
-
}
|
|
1537
|
-
}
|
|
1538
|
-
const queue = [];
|
|
1539
|
-
for (const job of jobs) {
|
|
1540
|
-
if (inDegree.get(job.id) === 0) {
|
|
1541
|
-
queue.push(job);
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
queue.sort((a, b) => {
|
|
1545
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1546
|
-
return a.index - b.index;
|
|
1547
|
-
});
|
|
1548
|
-
const result = [];
|
|
1549
|
-
while (queue.length > 0) {
|
|
1550
|
-
const job = queue.shift();
|
|
1551
|
-
result.push(job);
|
|
1552
|
-
const neighbors = adjacency.get(job.id) || [];
|
|
1553
|
-
for (const neighborId of neighbors) {
|
|
1554
|
-
const newDegree = inDegree.get(neighborId) - 1;
|
|
1555
|
-
inDegree.set(neighborId, newDegree);
|
|
1556
|
-
if (newDegree === 0) {
|
|
1557
|
-
const neighbor = jobMap.get(neighborId);
|
|
1558
|
-
insertSorted(queue, neighbor);
|
|
1559
|
-
}
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
if (result.length !== n) {
|
|
1563
|
-
console.warn("[useFrame] Circular dependency detected in job constraints");
|
|
1564
|
-
const resultIds = new Set(result.map((j) => j.id));
|
|
1565
|
-
for (const job of jobs) {
|
|
1566
|
-
if (!resultIds.has(job.id)) result.push(job);
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
return result;
|
|
1570
|
-
}
|
|
1571
|
-
function insertSorted(arr, job) {
|
|
1572
|
-
let i = 0;
|
|
1573
|
-
while (i < arr.length) {
|
|
1574
|
-
const cmp = arr[i];
|
|
1575
|
-
if (job.priority > cmp.priority || job.priority === cmp.priority && job.index < cmp.index) {
|
|
1576
|
-
break;
|
|
1577
|
-
}
|
|
1578
|
-
i++;
|
|
1579
|
-
}
|
|
1580
|
-
arr.splice(i, 0, job);
|
|
1581
|
-
}
|
|
1582
|
-
|
|
1583
|
-
function shouldRun(job, now) {
|
|
1584
|
-
if (!job.enabled) return false;
|
|
1585
|
-
if (!job.fps) return true;
|
|
1586
|
-
const minInterval = 1e3 / job.fps;
|
|
1587
|
-
const lastRun = job.lastRun ?? 0;
|
|
1588
|
-
const elapsed = now - lastRun;
|
|
1589
|
-
if (elapsed < minInterval) return false;
|
|
1590
|
-
if (job.drop) {
|
|
1591
|
-
job.lastRun = now;
|
|
1592
|
-
} else {
|
|
1593
|
-
const steps = Math.floor(elapsed / minInterval);
|
|
1594
|
-
job.lastRun = lastRun + steps * minInterval;
|
|
1595
|
-
if (job.lastRun < now - minInterval) {
|
|
1596
|
-
job.lastRun = now;
|
|
1597
|
-
}
|
|
1598
|
-
}
|
|
1599
|
-
return true;
|
|
1600
|
-
}
|
|
1601
|
-
function resetJobTiming(job) {
|
|
1602
|
-
job.lastRun = void 0;
|
|
1603
|
-
}
|
|
1604
|
-
|
|
1605
|
-
var __defProp = Object.defineProperty;
|
|
1606
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1607
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1608
|
-
const hmrData = (() => {
|
|
1609
|
-
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return void 0;
|
|
1610
|
-
if (typeof import_meta_hot !== "undefined") return import_meta_hot;
|
|
1611
|
-
try {
|
|
1612
|
-
return (0, eval)("import.meta.hot");
|
|
1613
|
-
} catch {
|
|
1614
|
-
return void 0;
|
|
1615
|
-
}
|
|
1616
|
-
})();
|
|
1617
|
-
const _Scheduler = class _Scheduler {
|
|
1618
|
-
//* Constructor ================================
|
|
1619
|
-
constructor() {
|
|
1620
|
-
//* Critical State ================================
|
|
1621
|
-
__publicField(this, "roots", /* @__PURE__ */ new Map());
|
|
1622
|
-
__publicField(this, "phaseGraph");
|
|
1623
|
-
__publicField(this, "loopState", {
|
|
1624
|
-
running: false,
|
|
1625
|
-
rafHandle: null,
|
|
1626
|
-
lastTime: null,
|
|
1627
|
-
// null = uninitialized, 0+ = valid timestamp
|
|
1628
|
-
frameCount: 0,
|
|
1629
|
-
elapsedTime: 0,
|
|
1630
|
-
createdAt: performance.now()
|
|
1631
|
-
});
|
|
1632
|
-
__publicField(this, "stoppedTime", 0);
|
|
1633
|
-
//* Private State ================================
|
|
1634
|
-
__publicField(this, "nextRootIndex", 0);
|
|
1635
|
-
__publicField(this, "globalBeforeJobs", /* @__PURE__ */ new Map());
|
|
1636
|
-
__publicField(this, "globalAfterJobs", /* @__PURE__ */ new Map());
|
|
1637
|
-
__publicField(this, "nextGlobalIndex", 0);
|
|
1638
|
-
__publicField(this, "idleCallbacks", /* @__PURE__ */ new Set());
|
|
1639
|
-
__publicField(this, "nextJobIndex", 0);
|
|
1640
|
-
__publicField(this, "jobStateListeners", /* @__PURE__ */ new Map());
|
|
1641
|
-
__publicField(this, "pendingFrames", 0);
|
|
1642
|
-
__publicField(this, "_frameloop", "always");
|
|
1643
|
-
//* Independent Mode & Error Handling State ================================
|
|
1644
|
-
__publicField(this, "_independent", false);
|
|
1645
|
-
__publicField(this, "errorHandler", null);
|
|
1646
|
-
__publicField(this, "rootReadyCallbacks", /* @__PURE__ */ new Set());
|
|
1647
|
-
//* Core Loop Execution Methods ================================
|
|
1648
|
-
/**
|
|
1649
|
-
* Main RAF loop callback.
|
|
1650
|
-
* Executes frame, handles demand mode, and schedules next frame.
|
|
1651
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
1652
|
-
* @returns {void}
|
|
1653
|
-
* @private
|
|
1654
|
-
*/
|
|
1655
|
-
__publicField(this, "loop", (timestamp) => {
|
|
1656
|
-
if (!this.loopState.running) return;
|
|
1657
|
-
this.executeFrame(timestamp);
|
|
1658
|
-
if (this._frameloop === "demand") {
|
|
1659
|
-
this.pendingFrames = Math.max(0, this.pendingFrames - 1);
|
|
1660
|
-
if (this.pendingFrames === 0) {
|
|
1661
|
-
this.notifyIdle(timestamp);
|
|
1662
|
-
return this.stop();
|
|
1663
|
-
}
|
|
1664
|
-
}
|
|
1665
|
-
this.loopState.rafHandle = requestAnimationFrame(this.loop);
|
|
1666
|
-
});
|
|
1667
|
-
this.phaseGraph = new PhaseGraph();
|
|
1668
|
-
}
|
|
1669
|
-
static get instance() {
|
|
1670
|
-
return globalThis[_Scheduler.INSTANCE_KEY] ?? null;
|
|
1671
|
-
}
|
|
1672
|
-
static set instance(value) {
|
|
1673
|
-
globalThis[_Scheduler.INSTANCE_KEY] = value;
|
|
1674
|
-
}
|
|
1675
|
-
/**
|
|
1676
|
-
* Get the global scheduler instance (creates if doesn't exist).
|
|
1677
|
-
* Uses HMR data to preserve instance across hot reloads.
|
|
1678
|
-
* @returns {Scheduler} The singleton scheduler instance
|
|
1679
|
-
*/
|
|
1680
|
-
static get() {
|
|
1681
|
-
if (!_Scheduler.instance && hmrData?.data?.scheduler) {
|
|
1682
|
-
_Scheduler.instance = hmrData.data.scheduler;
|
|
1683
|
-
}
|
|
1684
|
-
if (!_Scheduler.instance) {
|
|
1685
|
-
_Scheduler.instance = new _Scheduler();
|
|
1686
|
-
if (hmrData?.data) {
|
|
1687
|
-
hmrData.data.scheduler = _Scheduler.instance;
|
|
1688
|
-
}
|
|
1689
|
-
}
|
|
1690
|
-
return _Scheduler.instance;
|
|
1691
|
-
}
|
|
1692
|
-
/**
|
|
1693
|
-
* Reset the singleton instance. Stops the loop and clears all state.
|
|
1694
|
-
* Primarily used for testing to ensure clean state between tests.
|
|
1695
|
-
* @returns {void}
|
|
1696
|
-
*/
|
|
1697
|
-
static reset() {
|
|
1698
|
-
if (_Scheduler.instance) {
|
|
1699
|
-
_Scheduler.instance.stop();
|
|
1700
|
-
_Scheduler.instance = null;
|
|
1701
|
-
}
|
|
1702
|
-
if (hmrData?.data) {
|
|
1703
|
-
hmrData.data.scheduler = null;
|
|
1704
|
-
}
|
|
1705
|
-
}
|
|
1706
|
-
//* Getters & Setters ================================
|
|
1707
|
-
get phases() {
|
|
1708
|
-
return this.phaseGraph.getOrderedPhases();
|
|
1709
|
-
}
|
|
1710
|
-
get frameloop() {
|
|
1711
|
-
return this._frameloop;
|
|
1712
|
-
}
|
|
1713
|
-
set frameloop(mode) {
|
|
1714
|
-
if (this._frameloop === mode) return;
|
|
1715
|
-
const wasAlways = this._frameloop === "always";
|
|
1716
|
-
this._frameloop = mode;
|
|
1717
|
-
if (mode === "always" && !this.loopState.running && this.roots.size > 0) this.start();
|
|
1718
|
-
else if (mode !== "always" && wasAlways) this.stop();
|
|
1719
|
-
}
|
|
1720
|
-
get isRunning() {
|
|
1721
|
-
return this.loopState.running;
|
|
1722
|
-
}
|
|
1723
|
-
get isReady() {
|
|
1724
|
-
return this.roots.size > 0;
|
|
1725
|
-
}
|
|
1726
|
-
get independent() {
|
|
1727
|
-
return this._independent;
|
|
1728
|
-
}
|
|
1729
|
-
set independent(value) {
|
|
1730
|
-
this._independent = value;
|
|
1731
|
-
if (value) this.ensureDefaultRoot();
|
|
1732
|
-
}
|
|
1733
|
-
//* Root Management Methods ================================
|
|
1734
|
-
/**
|
|
1735
|
-
* Register a root (Canvas) with the scheduler.
|
|
1736
|
-
* The first root to register starts the RAF loop (if frameloop='always').
|
|
1737
|
-
* @param {string} id - Unique identifier for this root
|
|
1738
|
-
* @param {RootOptions} [options] - Optional configuration with getState and onError callbacks
|
|
1739
|
-
* @returns {() => void} Unsubscribe function to remove this root
|
|
1740
|
-
*/
|
|
1741
|
-
registerRoot(id, options = {}) {
|
|
1742
|
-
if (this.roots.has(id)) {
|
|
1743
|
-
console.warn(`[Scheduler] Root "${id}" already registered`);
|
|
1744
|
-
return () => this.unregisterRoot(id);
|
|
1745
|
-
}
|
|
1746
|
-
const entry = {
|
|
1747
|
-
id,
|
|
1748
|
-
getState: options.getState ?? (() => ({})),
|
|
1749
|
-
jobs: /* @__PURE__ */ new Map(),
|
|
1750
|
-
sortedJobs: [],
|
|
1751
|
-
needsRebuild: false
|
|
1752
|
-
};
|
|
1753
|
-
if (options.onError) {
|
|
1754
|
-
this.errorHandler = options.onError;
|
|
1755
|
-
}
|
|
1756
|
-
this.roots.set(id, entry);
|
|
1757
|
-
if (this.roots.size === 1) {
|
|
1758
|
-
this.notifyRootReady();
|
|
1759
|
-
if (this._frameloop === "always") this.start();
|
|
1760
|
-
}
|
|
1761
|
-
return () => this.unregisterRoot(id);
|
|
1762
|
-
}
|
|
1763
|
-
/**
|
|
1764
|
-
* Unregister a root from the scheduler.
|
|
1765
|
-
* Cleans up all job state listeners for this root's jobs.
|
|
1766
|
-
* The last root to unregister stops the RAF loop.
|
|
1767
|
-
* @param {string} id - The root ID to unregister
|
|
1768
|
-
* @returns {void}
|
|
1769
|
-
*/
|
|
1770
|
-
unregisterRoot(id) {
|
|
1771
|
-
const root = this.roots.get(id);
|
|
1772
|
-
if (!root) return;
|
|
1773
|
-
for (const jobId of root.jobs.keys()) {
|
|
1774
|
-
this.jobStateListeners.delete(jobId);
|
|
1775
|
-
}
|
|
1776
|
-
this.roots.delete(id);
|
|
1777
|
-
if (this.roots.size === 0) {
|
|
1778
|
-
this.stop();
|
|
1779
|
-
this.errorHandler = null;
|
|
1780
|
-
}
|
|
1781
|
-
}
|
|
1782
|
-
/**
|
|
1783
|
-
* Subscribe to be notified when a root becomes available.
|
|
1784
|
-
* Fires immediately if a root already exists.
|
|
1785
|
-
* @param {() => void} callback - Function called when first root registers
|
|
1786
|
-
* @returns {() => void} Unsubscribe function
|
|
1787
|
-
*/
|
|
1788
|
-
onRootReady(callback) {
|
|
1789
|
-
if (this.roots.size > 0) {
|
|
1790
|
-
callback();
|
|
1791
|
-
return () => {
|
|
1792
|
-
};
|
|
1793
|
-
}
|
|
1794
|
-
this.rootReadyCallbacks.add(callback);
|
|
1795
|
-
return () => this.rootReadyCallbacks.delete(callback);
|
|
1796
|
-
}
|
|
1797
|
-
/**
|
|
1798
|
-
* Notify all registered root-ready callbacks.
|
|
1799
|
-
* Called when the first root registers.
|
|
1800
|
-
* @returns {void}
|
|
1801
|
-
* @private
|
|
1802
|
-
*/
|
|
1803
|
-
notifyRootReady() {
|
|
1804
|
-
for (const cb of this.rootReadyCallbacks) {
|
|
1805
|
-
try {
|
|
1806
|
-
cb();
|
|
1807
|
-
} catch (error) {
|
|
1808
|
-
console.error("[Scheduler] Error in root-ready callback:", error);
|
|
1809
|
-
}
|
|
1810
|
-
}
|
|
1811
|
-
this.rootReadyCallbacks.clear();
|
|
1812
|
-
}
|
|
1813
|
-
/**
|
|
1814
|
-
* Ensure a default root exists for independent mode.
|
|
1815
|
-
* Creates a minimal root with no state provider.
|
|
1816
|
-
* @returns {void}
|
|
1817
|
-
* @private
|
|
1818
|
-
*/
|
|
1819
|
-
ensureDefaultRoot() {
|
|
1820
|
-
if (!this.roots.has("__default__")) {
|
|
1821
|
-
this.registerRoot("__default__");
|
|
1822
|
-
}
|
|
1823
|
-
}
|
|
1824
|
-
/**
|
|
1825
|
-
* Trigger error handling for job errors.
|
|
1826
|
-
* Uses the bound error handler if available, otherwise logs to console.
|
|
1827
|
-
* @param {Error} error - The error to handle
|
|
1828
|
-
* @returns {void}
|
|
1829
|
-
*/
|
|
1830
|
-
triggerError(error) {
|
|
1831
|
-
if (this.errorHandler) this.errorHandler(error);
|
|
1832
|
-
else console.error("[Scheduler]", error);
|
|
1833
|
-
}
|
|
1834
|
-
//* Phase Management Methods ================================
|
|
1835
|
-
/**
|
|
1836
|
-
* Add a named phase to the scheduler's execution order.
|
|
1837
|
-
* Marks all roots for rebuild to incorporate the new phase.
|
|
1838
|
-
* @param {string} name - The phase name (e.g., 'physics', 'postprocess')
|
|
1839
|
-
* @param {AddPhaseOptions} [options] - Positioning options (before/after other phases)
|
|
1840
|
-
* @returns {void}
|
|
1841
|
-
* @example
|
|
1842
|
-
* scheduler.addPhase('physics', { before: 'update' });
|
|
1843
|
-
* scheduler.addPhase('postprocess', { after: 'render' });
|
|
1844
|
-
*/
|
|
1845
|
-
addPhase(name, options) {
|
|
1846
|
-
this.phaseGraph.addPhase(name, options);
|
|
1847
|
-
for (const root of this.roots.values()) {
|
|
1848
|
-
root.needsRebuild = true;
|
|
1849
|
-
}
|
|
1850
|
-
}
|
|
1851
|
-
/**
|
|
1852
|
-
* Check if a phase exists in the scheduler.
|
|
1853
|
-
* @param {string} name - The phase name to check
|
|
1854
|
-
* @returns {boolean} True if the phase exists
|
|
1855
|
-
*/
|
|
1856
|
-
hasPhase(name) {
|
|
1857
|
-
return this.phaseGraph.hasPhase(name);
|
|
1858
|
-
}
|
|
1859
|
-
//* Global Job Registration Methods (Deprecated APIs) ================================
|
|
1860
|
-
/**
|
|
1861
|
-
* Register a global job that runs once per frame (not per-root).
|
|
1862
|
-
* Used internally by deprecated addEffect/addAfterEffect APIs.
|
|
1863
|
-
* @param {'before' | 'after'} phase - When to run: 'before' all roots or 'after' all roots
|
|
1864
|
-
* @param {string} id - Unique identifier for this global job
|
|
1865
|
-
* @param {(timestamp: number) => void} callback - Function called each frame with RAF timestamp
|
|
1866
|
-
* @returns {() => void} Unsubscribe function to remove this global job
|
|
1867
|
-
* @deprecated Use useFrame with phases instead
|
|
1868
|
-
*/
|
|
1869
|
-
registerGlobal(phase, id, callback) {
|
|
1870
|
-
const job = { id, callback };
|
|
1871
|
-
if (phase === "before") {
|
|
1872
|
-
this.globalBeforeJobs.set(id, job);
|
|
1873
|
-
} else {
|
|
1874
|
-
this.globalAfterJobs.set(id, job);
|
|
1875
|
-
}
|
|
1876
|
-
return () => {
|
|
1877
|
-
if (phase === "before") this.globalBeforeJobs.delete(id);
|
|
1878
|
-
else this.globalAfterJobs.delete(id);
|
|
1879
|
-
};
|
|
1880
|
-
}
|
|
1881
|
-
//* Idle Callback Methods (Deprecated API) ================================
|
|
1882
|
-
/**
|
|
1883
|
-
* Register an idle callback that fires when the loop stops.
|
|
1884
|
-
* Used internally by deprecated addTail API.
|
|
1885
|
-
* @param {(timestamp: number) => void} callback - Function called when loop becomes idle
|
|
1886
|
-
* @returns {() => void} Unsubscribe function to remove this idle callback
|
|
1887
|
-
* @deprecated Use demand mode with invalidate() instead
|
|
1888
|
-
*/
|
|
1889
|
-
onIdle(callback) {
|
|
1890
|
-
this.idleCallbacks.add(callback);
|
|
1891
|
-
return () => this.idleCallbacks.delete(callback);
|
|
1892
|
-
}
|
|
1893
|
-
/**
|
|
1894
|
-
* Notify all registered idle callbacks.
|
|
1895
|
-
* Called when the loop stops in demand mode.
|
|
1896
|
-
* @param {number} timestamp - The RAF timestamp when idle occurred
|
|
1897
|
-
* @returns {void}
|
|
1898
|
-
* @private
|
|
1899
|
-
*/
|
|
1900
|
-
notifyIdle(timestamp) {
|
|
1901
|
-
for (const cb of this.idleCallbacks) {
|
|
1902
|
-
try {
|
|
1903
|
-
cb(timestamp);
|
|
1904
|
-
} catch (error) {
|
|
1905
|
-
console.error("[Scheduler] Error in idle callback:", error);
|
|
1906
|
-
}
|
|
1907
|
-
}
|
|
1908
|
-
}
|
|
1909
|
-
//* Job Registration & Management Methods ================================
|
|
1910
|
-
/**
|
|
1911
|
-
* Register a job (frame callback) with a specific root.
|
|
1912
|
-
* This is the core registration method used by useFrame internally.
|
|
1913
|
-
* @param {FrameNextCallback} callback - The function to call each frame
|
|
1914
|
-
* @param {JobOptions & { rootId?: string; system?: boolean }} [options] - Job configuration
|
|
1915
|
-
* @param {string} [options.rootId] - Target root ID (defaults to first registered root)
|
|
1916
|
-
* @param {string} [options.id] - Unique job ID (auto-generated if not provided)
|
|
1917
|
-
* @param {string} [options.phase] - Execution phase (defaults to 'update')
|
|
1918
|
-
* @param {number} [options.priority] - Priority within phase (higher = earlier, default 0)
|
|
1919
|
-
* @param {number} [options.fps] - FPS throttle limit
|
|
1920
|
-
* @param {boolean} [options.drop] - Drop frames when behind (default true)
|
|
1921
|
-
* @param {boolean} [options.enabled] - Whether job is active (default true)
|
|
1922
|
-
* @param {boolean} [options.system] - Internal flag for system jobs (not user-facing)
|
|
1923
|
-
* @returns {() => void} Unsubscribe function to remove this job
|
|
1924
|
-
*/
|
|
1925
|
-
register(callback, options = {}) {
|
|
1926
|
-
const rootId = options.rootId;
|
|
1927
|
-
const root = rootId ? this.roots.get(rootId) : this.roots.values().next().value;
|
|
1928
|
-
if (!root) {
|
|
1929
|
-
console.warn("[Scheduler] No root registered. Is this inside a Canvas?");
|
|
1930
|
-
return () => {
|
|
1931
|
-
};
|
|
1932
|
-
}
|
|
1933
|
-
const id = options.id ?? this.generateJobId();
|
|
1934
|
-
let phase = options.phase ?? "update";
|
|
1935
|
-
if (!options.phase && (options.before || options.after)) {
|
|
1936
|
-
phase = this.phaseGraph.resolveConstraintPhase(options.before, options.after);
|
|
1937
|
-
}
|
|
1938
|
-
const before = this.normalizeConstraints(options.before);
|
|
1939
|
-
const after = this.normalizeConstraints(options.after);
|
|
1940
|
-
const job = {
|
|
1941
|
-
id,
|
|
1942
|
-
callback,
|
|
1943
|
-
phase,
|
|
1944
|
-
before,
|
|
1945
|
-
after,
|
|
1946
|
-
priority: options.priority ?? 0,
|
|
1947
|
-
index: this.nextJobIndex++,
|
|
1948
|
-
fps: options.fps,
|
|
1949
|
-
drop: options.drop ?? true,
|
|
1950
|
-
enabled: options.enabled ?? true,
|
|
1951
|
-
system: options.system ?? false
|
|
1952
|
-
};
|
|
1953
|
-
if (root.jobs.has(id)) {
|
|
1954
|
-
console.warn(`[useFrame] Job with id "${id}" already exists, replacing`);
|
|
1955
|
-
}
|
|
1956
|
-
root.jobs.set(id, job);
|
|
1957
|
-
root.needsRebuild = true;
|
|
1958
|
-
return () => this.unregister(id, root.id);
|
|
1959
|
-
}
|
|
1960
|
-
/**
|
|
1961
|
-
* Unregister a job by its ID.
|
|
1962
|
-
* Searches all roots if rootId is not provided.
|
|
1963
|
-
* @param {string} id - The job ID to unregister
|
|
1964
|
-
* @param {string} [rootId] - Optional root ID to search (searches all if not provided)
|
|
1965
|
-
* @returns {void}
|
|
1966
|
-
*/
|
|
1967
|
-
unregister(id, rootId) {
|
|
1968
|
-
const root = rootId ? this.roots.get(rootId) : Array.from(this.roots.values()).find((r) => r.jobs.has(id));
|
|
1969
|
-
if (root?.jobs.delete(id)) {
|
|
1970
|
-
root.needsRebuild = true;
|
|
1971
|
-
this.jobStateListeners.delete(id);
|
|
1972
|
-
}
|
|
1973
|
-
}
|
|
1974
|
-
/**
|
|
1975
|
-
* Update a job's options dynamically.
|
|
1976
|
-
* Searches all roots to find the job by ID.
|
|
1977
|
-
* Phase/constraint changes trigger a rebuild of the sorted job list.
|
|
1978
|
-
* @param {string} id - The job ID to update
|
|
1979
|
-
* @param {Partial<JobOptions>} options - The options to update
|
|
1980
|
-
* @returns {void}
|
|
1981
|
-
*/
|
|
1982
|
-
updateJob(id, options) {
|
|
1983
|
-
let job;
|
|
1984
|
-
let root;
|
|
1985
|
-
for (const r of this.roots.values()) {
|
|
1986
|
-
job = r.jobs.get(id);
|
|
1987
|
-
if (job) {
|
|
1988
|
-
root = r;
|
|
1989
|
-
break;
|
|
1990
|
-
}
|
|
1991
|
-
}
|
|
1992
|
-
if (!job || !root) return;
|
|
1993
|
-
if (options.priority !== void 0) job.priority = options.priority;
|
|
1994
|
-
if (options.fps !== void 0) job.fps = options.fps;
|
|
1995
|
-
if (options.drop !== void 0) job.drop = options.drop;
|
|
1996
|
-
if (options.enabled !== void 0) {
|
|
1997
|
-
const wasEnabled = job.enabled;
|
|
1998
|
-
job.enabled = options.enabled;
|
|
1999
|
-
if (!wasEnabled && job.enabled) resetJobTiming(job);
|
|
2000
|
-
if (wasEnabled !== job.enabled) root.needsRebuild = true;
|
|
2001
|
-
}
|
|
2002
|
-
if (options.phase !== void 0 || options.before !== void 0 || options.after !== void 0) {
|
|
2003
|
-
if (options.phase) job.phase = options.phase;
|
|
2004
|
-
if (options.before !== void 0) job.before = this.normalizeConstraints(options.before);
|
|
2005
|
-
if (options.after !== void 0) job.after = this.normalizeConstraints(options.after);
|
|
2006
|
-
root.needsRebuild = true;
|
|
2007
|
-
}
|
|
2008
|
-
}
|
|
2009
|
-
//* Job State Management Methods ================================
|
|
2010
|
-
/**
|
|
2011
|
-
* Check if a job is currently paused (disabled).
|
|
2012
|
-
* @param {string} id - The job ID to check
|
|
2013
|
-
* @returns {boolean} True if the job exists and is paused
|
|
2014
|
-
*/
|
|
2015
|
-
isJobPaused(id) {
|
|
2016
|
-
for (const root of this.roots.values()) {
|
|
2017
|
-
const job = root.jobs.get(id);
|
|
2018
|
-
if (job) return !job.enabled;
|
|
2019
|
-
}
|
|
2020
|
-
return false;
|
|
2021
|
-
}
|
|
2022
|
-
/**
|
|
2023
|
-
* Subscribe to state changes for a specific job.
|
|
2024
|
-
* Listener is called when job is paused or resumed.
|
|
2025
|
-
* @param {string} id - The job ID to subscribe to
|
|
2026
|
-
* @param {() => void} listener - Callback invoked on state changes
|
|
2027
|
-
* @returns {() => void} Unsubscribe function
|
|
2028
|
-
*/
|
|
2029
|
-
subscribeJobState(id, listener) {
|
|
2030
|
-
if (!this.jobStateListeners.has(id)) {
|
|
2031
|
-
this.jobStateListeners.set(id, /* @__PURE__ */ new Set());
|
|
2032
|
-
}
|
|
2033
|
-
this.jobStateListeners.get(id).add(listener);
|
|
2034
|
-
return () => {
|
|
2035
|
-
this.jobStateListeners.get(id)?.delete(listener);
|
|
2036
|
-
if (this.jobStateListeners.get(id)?.size === 0) {
|
|
2037
|
-
this.jobStateListeners.delete(id);
|
|
2038
|
-
}
|
|
2039
|
-
};
|
|
2040
|
-
}
|
|
2041
|
-
/**
|
|
2042
|
-
* Notify all listeners that a job's state has changed.
|
|
2043
|
-
* @param {string} id - The job ID that changed
|
|
2044
|
-
* @returns {void}
|
|
2045
|
-
* @private
|
|
2046
|
-
*/
|
|
2047
|
-
notifyJobStateChange(id) {
|
|
2048
|
-
this.jobStateListeners.get(id)?.forEach((listener) => listener());
|
|
2049
|
-
}
|
|
2050
|
-
/**
|
|
2051
|
-
* Pause a job by ID (sets enabled=false).
|
|
2052
|
-
* Notifies any subscribed state listeners.
|
|
2053
|
-
* @param {string} id - The job ID to pause
|
|
2054
|
-
* @returns {void}
|
|
2055
|
-
*/
|
|
2056
|
-
pauseJob(id) {
|
|
2057
|
-
this.updateJob(id, { enabled: false });
|
|
2058
|
-
this.notifyJobStateChange(id);
|
|
2059
|
-
}
|
|
2060
|
-
/**
|
|
2061
|
-
* Resume a paused job by ID (sets enabled=true).
|
|
2062
|
-
* Resets job timing to prevent frame accumulation.
|
|
2063
|
-
* Notifies any subscribed state listeners.
|
|
2064
|
-
* @param {string} id - The job ID to resume
|
|
2065
|
-
* @returns {void}
|
|
2066
|
-
*/
|
|
2067
|
-
resumeJob(id) {
|
|
2068
|
-
this.updateJob(id, { enabled: true });
|
|
2069
|
-
this.notifyJobStateChange(id);
|
|
2070
|
-
}
|
|
2071
|
-
//* Frame Loop Control Methods ================================
|
|
2072
|
-
/**
|
|
2073
|
-
* Start the requestAnimationFrame loop.
|
|
2074
|
-
* Resets timing state (elapsedTime, frameCount) on start.
|
|
2075
|
-
* No-op if already running.
|
|
2076
|
-
* @returns {void}
|
|
2077
|
-
*/
|
|
2078
|
-
start() {
|
|
2079
|
-
if (this.loopState.running) return;
|
|
2080
|
-
const { elapsedTime, createdAt } = this.loopState;
|
|
2081
|
-
let adjustedCreated = 0;
|
|
2082
|
-
if (this.stoppedTime > 0) {
|
|
2083
|
-
adjustedCreated = createdAt - (performance.now() - this.stoppedTime);
|
|
2084
|
-
this.stoppedTime = 0;
|
|
2085
|
-
}
|
|
2086
|
-
Object.assign(this.loopState, {
|
|
2087
|
-
running: true,
|
|
2088
|
-
elapsedTime: elapsedTime ?? 0,
|
|
2089
|
-
lastTime: performance.now(),
|
|
2090
|
-
createdAt: adjustedCreated > 0 ? adjustedCreated : performance.now(),
|
|
2091
|
-
frameCount: 0,
|
|
2092
|
-
rafHandle: requestAnimationFrame(this.loop)
|
|
2093
|
-
});
|
|
2094
|
-
}
|
|
2095
|
-
/**
|
|
2096
|
-
* Stop the requestAnimationFrame loop.
|
|
2097
|
-
* Cancels any pending RAF callback.
|
|
2098
|
-
* No-op if not running.
|
|
2099
|
-
* @returns {void}
|
|
2100
|
-
*/
|
|
2101
|
-
stop() {
|
|
2102
|
-
if (!this.loopState.running) return;
|
|
2103
|
-
this.loopState.running = false;
|
|
2104
|
-
if (this.loopState.rafHandle !== null) {
|
|
2105
|
-
cancelAnimationFrame(this.loopState.rafHandle);
|
|
2106
|
-
this.loopState.rafHandle = null;
|
|
2107
|
-
}
|
|
2108
|
-
this.stoppedTime = performance.now();
|
|
2109
|
-
}
|
|
2110
|
-
/**
|
|
2111
|
-
* Request frames to be rendered in demand mode.
|
|
2112
|
-
* Accumulates pending frames (capped at 60) and starts the loop if not running.
|
|
2113
|
-
* No-op if frameloop is not 'demand'.
|
|
2114
|
-
* @param {number} [frames=1] - Number of frames to request
|
|
2115
|
-
* @param {boolean} [stackFrames=false] - Whether to add frames to existing pending count
|
|
2116
|
-
* - `false` (default): Sets pending frames to the specified value (replaces existing count)
|
|
2117
|
-
* - `true`: Adds frames to existing pending count (useful for accumulating invalidations)
|
|
2118
|
-
* @returns {void}
|
|
2119
|
-
* @example
|
|
2120
|
-
* // Request a single frame render
|
|
2121
|
-
* scheduler.invalidate();
|
|
2122
|
-
*
|
|
2123
|
-
* @example
|
|
2124
|
-
* // Request 5 frames (e.g., for animations)
|
|
2125
|
-
* scheduler.invalidate(5);
|
|
2126
|
-
*
|
|
2127
|
-
* @example
|
|
2128
|
-
* // Set pending frames to exactly 3 (don't stack with existing)
|
|
2129
|
-
* scheduler.invalidate(3, false);
|
|
2130
|
-
*
|
|
2131
|
-
* @example
|
|
2132
|
-
* // Add 2 more frames to existing pending count
|
|
2133
|
-
* scheduler.invalidate(2, true);
|
|
2134
|
-
*/
|
|
2135
|
-
invalidate(frames = 1, stackFrames = false) {
|
|
2136
|
-
if (this._frameloop !== "demand") return;
|
|
2137
|
-
const baseFrames = stackFrames ? this.pendingFrames : 0;
|
|
2138
|
-
this.pendingFrames = Math.min(60, baseFrames + frames);
|
|
2139
|
-
if (!this.loopState.running && this.pendingFrames > 0) this.start();
|
|
2140
|
-
}
|
|
2141
|
-
/**
|
|
2142
|
-
* Reset timing state for deterministic testing.
|
|
2143
|
-
* Preserves jobs and roots but resets lastTime, frameCount, elapsedTime, etc.
|
|
2144
|
-
* @returns {void}
|
|
2145
|
-
*/
|
|
2146
|
-
resetTiming() {
|
|
2147
|
-
this.loopState.lastTime = null;
|
|
2148
|
-
this.loopState.frameCount = 0;
|
|
2149
|
-
this.loopState.elapsedTime = 0;
|
|
2150
|
-
this.loopState.createdAt = performance.now();
|
|
2151
|
-
}
|
|
2152
|
-
//* Manual Stepping Methods ================================
|
|
2153
|
-
/**
|
|
2154
|
-
* Manually execute a single frame for all roots.
|
|
2155
|
-
* Useful for frameloop='never' mode or testing scenarios.
|
|
2156
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2157
|
-
* @returns {void}
|
|
2158
|
-
* @example
|
|
2159
|
-
* // Manual control mode
|
|
2160
|
-
* scheduler.frameloop = 'never';
|
|
2161
|
-
* scheduler.step(); // Execute one frame
|
|
2162
|
-
*/
|
|
2163
|
-
step(timestamp) {
|
|
2164
|
-
const now = timestamp ?? performance.now();
|
|
2165
|
-
this.executeFrame(now);
|
|
2166
|
-
}
|
|
2167
|
-
/**
|
|
2168
|
-
* Manually execute a single job by its ID.
|
|
2169
|
-
* Useful for testing individual job callbacks in isolation.
|
|
2170
|
-
* @param {string} id - The job ID to step
|
|
2171
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2172
|
-
* @returns {void}
|
|
2173
|
-
*/
|
|
2174
|
-
stepJob(id, timestamp) {
|
|
2175
|
-
let job;
|
|
2176
|
-
let root;
|
|
2177
|
-
for (const r of this.roots.values()) {
|
|
2178
|
-
job = r.jobs.get(id);
|
|
2179
|
-
if (job) {
|
|
2180
|
-
root = r;
|
|
2181
|
-
break;
|
|
2182
|
-
}
|
|
2183
|
-
}
|
|
2184
|
-
if (!job || !root) {
|
|
2185
|
-
console.warn(`[Scheduler] Job "${id}" not found`);
|
|
2186
|
-
return;
|
|
2187
|
-
}
|
|
2188
|
-
const now = timestamp ?? performance.now();
|
|
2189
|
-
const deltaMs = this.loopState.lastTime !== null ? now - this.loopState.lastTime : 0;
|
|
2190
|
-
const delta = deltaMs / 1e3;
|
|
2191
|
-
const elapsed = now - this.loopState.createdAt;
|
|
2192
|
-
const providedState = root.getState?.() ?? {};
|
|
2193
|
-
const frameState = {
|
|
2194
|
-
...providedState,
|
|
2195
|
-
time: now,
|
|
2196
|
-
delta,
|
|
2197
|
-
elapsed,
|
|
2198
|
-
frame: this.loopState.frameCount
|
|
2199
|
-
};
|
|
2200
|
-
try {
|
|
2201
|
-
job.callback(frameState, delta);
|
|
2202
|
-
} catch (error) {
|
|
2203
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2204
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2205
|
-
}
|
|
2206
|
-
}
|
|
2207
|
-
/**
|
|
2208
|
-
* Execute a single frame across all roots.
|
|
2209
|
-
* Order: globalBefore → each root's jobs → globalAfter
|
|
2210
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2211
|
-
* @returns {void}
|
|
2212
|
-
* @private
|
|
2213
|
-
*/
|
|
2214
|
-
executeFrame(timestamp) {
|
|
2215
|
-
const deltaMs = this.loopState.lastTime !== null ? timestamp - this.loopState.lastTime : 0;
|
|
2216
|
-
const delta = deltaMs / 1e3;
|
|
2217
|
-
this.loopState.lastTime = timestamp;
|
|
2218
|
-
this.loopState.frameCount++;
|
|
2219
|
-
this.loopState.elapsedTime += deltaMs;
|
|
2220
|
-
this.runGlobalJobs(this.globalBeforeJobs, timestamp);
|
|
2221
|
-
for (const root of this.roots.values()) {
|
|
2222
|
-
this.tickRoot(root, timestamp, delta);
|
|
2223
|
-
}
|
|
2224
|
-
this.runGlobalJobs(this.globalAfterJobs, timestamp);
|
|
2225
|
-
}
|
|
2226
|
-
/**
|
|
2227
|
-
* Run all global jobs from a job map.
|
|
2228
|
-
* Catches and logs errors without stopping execution.
|
|
2229
|
-
* @param {Map<string, GlobalJob>} jobs - The global jobs map to execute
|
|
2230
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2231
|
-
* @returns {void}
|
|
2232
|
-
* @private
|
|
2233
|
-
*/
|
|
2234
|
-
runGlobalJobs(jobs, timestamp) {
|
|
2235
|
-
for (const job of jobs.values()) {
|
|
2236
|
-
try {
|
|
2237
|
-
job.callback(timestamp);
|
|
2238
|
-
} catch (error) {
|
|
2239
|
-
console.error(`[Scheduler] Error in global job "${job.id}":`, error);
|
|
2240
|
-
}
|
|
2241
|
-
}
|
|
2242
|
-
}
|
|
2243
|
-
/**
|
|
2244
|
-
* Execute all jobs for a single root in sorted order.
|
|
2245
|
-
* Rebuilds sorted job list if needed, then dispatches each job.
|
|
2246
|
-
* Errors are caught and propagated via triggerError.
|
|
2247
|
-
* @param {RootEntry} root - The root entry to tick
|
|
2248
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2249
|
-
* @param {number} delta - Time since last frame in seconds
|
|
2250
|
-
* @returns {void}
|
|
2251
|
-
* @private
|
|
2252
|
-
*/
|
|
2253
|
-
tickRoot(root, timestamp, delta) {
|
|
2254
|
-
if (root.needsRebuild) {
|
|
2255
|
-
root.sortedJobs = rebuildSortedJobs(root.jobs, this.phaseGraph);
|
|
2256
|
-
root.needsRebuild = false;
|
|
2257
|
-
}
|
|
2258
|
-
const providedState = root.getState?.() ?? {};
|
|
2259
|
-
const frameState = {
|
|
2260
|
-
...providedState,
|
|
2261
|
-
time: timestamp,
|
|
2262
|
-
delta,
|
|
2263
|
-
elapsed: this.loopState.elapsedTime / 1e3,
|
|
2264
|
-
// Convert ms to seconds
|
|
2265
|
-
frame: this.loopState.frameCount
|
|
2266
|
-
};
|
|
2267
|
-
for (const job of root.sortedJobs) {
|
|
2268
|
-
if (!shouldRun(job, timestamp)) continue;
|
|
2269
|
-
try {
|
|
2270
|
-
job.callback(frameState, delta);
|
|
2271
|
-
} catch (error) {
|
|
2272
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2273
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2274
|
-
}
|
|
2275
|
-
}
|
|
2276
|
-
}
|
|
2277
|
-
//* Debug & Inspection Methods ================================
|
|
2278
|
-
/**
|
|
2279
|
-
* Get the total number of registered jobs across all roots.
|
|
2280
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2281
|
-
* @returns {number} Total job count
|
|
2282
|
-
*/
|
|
2283
|
-
getJobCount() {
|
|
2284
|
-
let count = 0;
|
|
2285
|
-
for (const root of this.roots.values()) {
|
|
2286
|
-
count += root.jobs.size;
|
|
2287
|
-
}
|
|
2288
|
-
return count + this.globalBeforeJobs.size + this.globalAfterJobs.size;
|
|
2289
|
-
}
|
|
2290
|
-
/**
|
|
2291
|
-
* Get all registered job IDs across all roots.
|
|
2292
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2293
|
-
* @returns {string[]} Array of all job IDs
|
|
2294
|
-
*/
|
|
2295
|
-
getJobIds() {
|
|
2296
|
-
const ids = [];
|
|
2297
|
-
for (const root of this.roots.values()) {
|
|
2298
|
-
ids.push(...root.jobs.keys());
|
|
2299
|
-
}
|
|
2300
|
-
ids.push(...this.globalBeforeJobs.keys());
|
|
2301
|
-
ids.push(...this.globalAfterJobs.keys());
|
|
2302
|
-
return ids;
|
|
2303
|
-
}
|
|
2304
|
-
/**
|
|
2305
|
-
* Get the number of registered roots (Canvas instances).
|
|
2306
|
-
* @returns {number} Number of registered roots
|
|
2307
|
-
*/
|
|
2308
|
-
getRootCount() {
|
|
2309
|
-
return this.roots.size;
|
|
2310
|
-
}
|
|
2311
|
-
/**
|
|
2312
|
-
* Check if any user (non-system) jobs are registered in a specific phase.
|
|
2313
|
-
* Used by the default render job to know if a user has taken over rendering.
|
|
2314
|
-
*
|
|
2315
|
-
* @param phase The phase to check
|
|
2316
|
-
* @param rootId Optional root ID to check (checks all roots if not provided)
|
|
2317
|
-
* @returns true if any user jobs exist in the phase
|
|
2318
|
-
*/
|
|
2319
|
-
hasUserJobsInPhase(phase, rootId) {
|
|
2320
|
-
const rootsToCheck = rootId ? [this.roots.get(rootId)].filter(Boolean) : Array.from(this.roots.values());
|
|
2321
|
-
return rootsToCheck.some((root) => {
|
|
2322
|
-
if (!root) return false;
|
|
2323
|
-
for (const job of root.jobs.values()) {
|
|
2324
|
-
if (job.phase === phase && !job.system && job.enabled) return true;
|
|
2325
|
-
}
|
|
2326
|
-
return false;
|
|
2327
|
-
});
|
|
2328
|
-
}
|
|
2329
|
-
//* Utility Methods ================================
|
|
2330
|
-
/**
|
|
2331
|
-
* Generate a unique root ID for automatic root registration.
|
|
2332
|
-
* @returns {string} A unique root ID in the format 'root_N'
|
|
2333
|
-
*/
|
|
2334
|
-
generateRootId() {
|
|
2335
|
-
return `root_${this.nextRootIndex++}`;
|
|
2336
|
-
}
|
|
2337
|
-
/**
|
|
2338
|
-
* Generate a unique job ID.
|
|
2339
|
-
* @returns {string} A unique job ID in the format 'job_N'
|
|
2340
|
-
* @private
|
|
2341
|
-
*/
|
|
2342
|
-
generateJobId() {
|
|
2343
|
-
return `job_${this.nextJobIndex}`;
|
|
2344
|
-
}
|
|
2345
|
-
/**
|
|
2346
|
-
* Normalize before/after constraints to a Set.
|
|
2347
|
-
* Handles undefined, single string, or array inputs.
|
|
2348
|
-
* @param {string | string[] | undefined} value - The constraint value(s)
|
|
2349
|
-
* @returns {Set<string>} Normalized Set of constraint strings
|
|
2350
|
-
* @private
|
|
2351
|
-
*/
|
|
2352
|
-
normalizeConstraints(value) {
|
|
2353
|
-
if (!value) return /* @__PURE__ */ new Set();
|
|
2354
|
-
if (Array.isArray(value)) return new Set(value);
|
|
2355
|
-
return /* @__PURE__ */ new Set([value]);
|
|
2356
|
-
}
|
|
2357
|
-
};
|
|
2358
|
-
//* Static State & Methods (Singleton Usage) ================================
|
|
2359
|
-
//* Cross-Bundle Singleton Key ==============================
|
|
2360
|
-
// Use Symbol.for() to ensure scheduler is shared across bundle boundaries
|
|
2361
|
-
// This prevents issues when mixing imports from @react-three/fiber and @react-three/fiber/webgpu
|
|
2362
|
-
__publicField(_Scheduler, "INSTANCE_KEY", Symbol.for("@react-three/fiber.scheduler"));
|
|
2363
|
-
let Scheduler = _Scheduler;
|
|
2364
|
-
const getScheduler = () => Scheduler.get();
|
|
2365
|
-
if (hmrData) {
|
|
2366
|
-
hmrData.accept?.();
|
|
2367
|
-
}
|
|
2368
|
-
|
|
2369
2010
|
function useFrame(callback, priorityOrOptions) {
|
|
2370
2011
|
const store = React.useContext(context);
|
|
2371
2012
|
const isInsideCanvas = store !== null;
|
|
@@ -2438,20 +2079,14 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2438
2079
|
}
|
|
2439
2080
|
};
|
|
2440
2081
|
} else {
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
unregisterJob = registerOutside();
|
|
2450
|
-
});
|
|
2451
|
-
return () => {
|
|
2452
|
-
unsubReady();
|
|
2453
|
-
unregisterJob?.();
|
|
2454
|
-
};
|
|
2082
|
+
return scheduler.register(
|
|
2083
|
+
(state, delta) => {
|
|
2084
|
+
const frameState = state;
|
|
2085
|
+
if (!frameState.renderer) return;
|
|
2086
|
+
callbackRef.current?.(frameState, delta);
|
|
2087
|
+
},
|
|
2088
|
+
{ id, ...options }
|
|
2089
|
+
);
|
|
2455
2090
|
}
|
|
2456
2091
|
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2457
2092
|
const isPaused = React.useSyncExternalStore(
|
|
@@ -2542,22 +2177,29 @@ function buildFromCache(input, textureCache) {
|
|
|
2542
2177
|
function useTexture(input, optionsOrOnLoad) {
|
|
2543
2178
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
2544
2179
|
const store = useStore();
|
|
2545
|
-
const textureCache = useThree((state) => state.textures);
|
|
2546
2180
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
2547
|
-
const { onLoad, cache =
|
|
2181
|
+
const { onLoad, cache = true } = options;
|
|
2182
|
+
const onLoadRef = useRef(onLoad);
|
|
2183
|
+
onLoadRef.current = onLoad;
|
|
2184
|
+
const onLoadCalledForRef = useRef(null);
|
|
2548
2185
|
const urls = useMemo(() => getUrls(input), [input]);
|
|
2549
2186
|
const cachedResult = useMemo(() => {
|
|
2550
2187
|
if (!cache) return null;
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2188
|
+
const textures = store.getState().textures;
|
|
2189
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2190
|
+
return buildFromCache(input, textures);
|
|
2191
|
+
}, [cache, urls, input, store]);
|
|
2554
2192
|
const loadedTextures = useLoader(
|
|
2555
2193
|
TextureLoader,
|
|
2556
2194
|
IsObject(input) ? Object.values(input) : input
|
|
2557
2195
|
);
|
|
2196
|
+
const inputKey = urls.join("\0");
|
|
2558
2197
|
useLayoutEffect(() => {
|
|
2559
|
-
if (
|
|
2560
|
-
|
|
2198
|
+
if (cachedResult) return;
|
|
2199
|
+
if (onLoadCalledForRef.current === inputKey) return;
|
|
2200
|
+
onLoadCalledForRef.current = inputKey;
|
|
2201
|
+
onLoadRef.current?.(loadedTextures);
|
|
2202
|
+
}, [cachedResult, loadedTextures, inputKey]);
|
|
2561
2203
|
useEffect(() => {
|
|
2562
2204
|
if (cachedResult) return;
|
|
2563
2205
|
if ("initTexture" in renderer) {
|
|
@@ -2590,8 +2232,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
2590
2232
|
}, [input, loadedTextures, cachedResult]);
|
|
2591
2233
|
useEffect(() => {
|
|
2592
2234
|
if (!cache) return;
|
|
2593
|
-
if (cachedResult) return;
|
|
2594
|
-
const set = store.setState;
|
|
2595
2235
|
const urlTextureMap = [];
|
|
2596
2236
|
if (typeof input === "string") {
|
|
2597
2237
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -2605,18 +2245,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
2605
2245
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
2606
2246
|
}
|
|
2607
2247
|
}
|
|
2608
|
-
|
|
2609
|
-
const
|
|
2610
|
-
let
|
|
2248
|
+
store.setState((state) => {
|
|
2249
|
+
const refs = new Map(state._textureRefs);
|
|
2250
|
+
let textures = state.textures;
|
|
2251
|
+
let added = false;
|
|
2611
2252
|
for (const [url, texture] of urlTextureMap) {
|
|
2612
|
-
if (!
|
|
2613
|
-
|
|
2614
|
-
|
|
2253
|
+
if (!textures.has(url)) {
|
|
2254
|
+
if (!added) {
|
|
2255
|
+
textures = new Map(textures);
|
|
2256
|
+
added = true;
|
|
2257
|
+
}
|
|
2258
|
+
textures.set(url, texture);
|
|
2615
2259
|
}
|
|
2260
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
2616
2261
|
}
|
|
2617
|
-
return
|
|
2262
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
2263
|
+
});
|
|
2264
|
+
return () => store.setState((state) => {
|
|
2265
|
+
const refs = new Map(state._textureRefs);
|
|
2266
|
+
for (const [url] of urlTextureMap) {
|
|
2267
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2268
|
+
if (next <= 0) refs.delete(url);
|
|
2269
|
+
else refs.set(url, next);
|
|
2270
|
+
}
|
|
2271
|
+
return { _textureRefs: refs };
|
|
2618
2272
|
});
|
|
2619
|
-
}, [cache, input, mappedTextures, store
|
|
2273
|
+
}, [cache, input, mappedTextures, store]);
|
|
2620
2274
|
return mappedTextures;
|
|
2621
2275
|
}
|
|
2622
2276
|
useTexture.preload = (url) => useLoader.preload(TextureLoader, url);
|
|
@@ -2632,106 +2286,90 @@ const Texture = ({
|
|
|
2632
2286
|
return /* @__PURE__ */ jsx(Fragment, { children: children?.(ret) });
|
|
2633
2287
|
};
|
|
2634
2288
|
|
|
2635
|
-
function
|
|
2636
|
-
if (entry instanceof Texture$1) return entry;
|
|
2637
|
-
if (entry && typeof entry === "object" && "value" in entry && entry.value instanceof Texture$1) {
|
|
2638
|
-
return entry.value;
|
|
2639
|
-
}
|
|
2640
|
-
return null;
|
|
2641
|
-
}
|
|
2642
|
-
function useTextures() {
|
|
2289
|
+
function useTextures(selector) {
|
|
2643
2290
|
const store = useStore();
|
|
2644
|
-
|
|
2645
|
-
const set = store.setState;
|
|
2291
|
+
const registry = useMemo(() => {
|
|
2646
2292
|
const getState = store.getState;
|
|
2647
|
-
const
|
|
2648
|
-
|
|
2649
|
-
const newMap = new Map(state.textures);
|
|
2650
|
-
newMap.set(key, value);
|
|
2651
|
-
return { textures: newMap };
|
|
2652
|
-
});
|
|
2653
|
-
};
|
|
2654
|
-
const addMultiple = (items) => {
|
|
2655
|
-
set((state) => {
|
|
2656
|
-
const newMap = new Map(state.textures);
|
|
2657
|
-
const entries = items instanceof Map ? items.entries() : Object.entries(items);
|
|
2658
|
-
for (const [key, value] of entries) {
|
|
2659
|
-
newMap.set(key, value);
|
|
2660
|
-
}
|
|
2661
|
-
return { textures: newMap };
|
|
2662
|
-
});
|
|
2663
|
-
};
|
|
2664
|
-
const remove = (key) => {
|
|
2665
|
-
set((state) => {
|
|
2666
|
-
const newMap = new Map(state.textures);
|
|
2667
|
-
newMap.delete(key);
|
|
2668
|
-
return { textures: newMap };
|
|
2669
|
-
});
|
|
2670
|
-
};
|
|
2671
|
-
const removeMultiple = (keys) => {
|
|
2672
|
-
set((state) => {
|
|
2673
|
-
const newMap = new Map(state.textures);
|
|
2674
|
-
for (const key of keys) newMap.delete(key);
|
|
2675
|
-
return { textures: newMap };
|
|
2676
|
-
});
|
|
2677
|
-
};
|
|
2678
|
-
const dispose = (key) => {
|
|
2679
|
-
const entry = getState().textures.get(key);
|
|
2680
|
-
if (entry) {
|
|
2681
|
-
const tex = getTextureValue(entry);
|
|
2682
|
-
tex?.dispose();
|
|
2683
|
-
}
|
|
2684
|
-
remove(key);
|
|
2685
|
-
};
|
|
2686
|
-
const disposeMultiple = (keys) => {
|
|
2687
|
-
const textures = getState().textures;
|
|
2688
|
-
for (const key of keys) {
|
|
2689
|
-
const entry = textures.get(key);
|
|
2690
|
-
if (entry) {
|
|
2691
|
-
const tex = getTextureValue(entry);
|
|
2692
|
-
tex?.dispose();
|
|
2693
|
-
}
|
|
2694
|
-
}
|
|
2695
|
-
removeMultiple(keys);
|
|
2696
|
-
};
|
|
2697
|
-
const disposeAll = () => {
|
|
2698
|
-
const textures = getState().textures;
|
|
2699
|
-
for (const entry of textures.values()) {
|
|
2700
|
-
const tex = getTextureValue(entry);
|
|
2701
|
-
tex?.dispose();
|
|
2702
|
-
}
|
|
2703
|
-
set({ textures: /* @__PURE__ */ new Map() });
|
|
2704
|
-
};
|
|
2293
|
+
const setState = store.setState;
|
|
2294
|
+
const getOne = (key) => getState().textures.get(key);
|
|
2705
2295
|
return {
|
|
2706
|
-
|
|
2707
|
-
get textures() {
|
|
2296
|
+
get all() {
|
|
2708
2297
|
return getState().textures;
|
|
2709
2298
|
},
|
|
2710
|
-
|
|
2711
|
-
|
|
2299
|
+
get(input) {
|
|
2300
|
+
if (typeof input === "string") return getOne(input);
|
|
2301
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2302
|
+
const out = {};
|
|
2303
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2304
|
+
return out;
|
|
2305
|
+
},
|
|
2712
2306
|
has: (key) => getState().textures.has(key),
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2307
|
+
add(keyOrRecord, texture) {
|
|
2308
|
+
setState((state) => {
|
|
2309
|
+
const textures = new Map(state.textures);
|
|
2310
|
+
if (typeof keyOrRecord === "string") {
|
|
2311
|
+
textures.set(keyOrRecord, texture);
|
|
2312
|
+
} else {
|
|
2313
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2314
|
+
}
|
|
2315
|
+
return { textures };
|
|
2316
|
+
});
|
|
2317
|
+
},
|
|
2318
|
+
dispose(key, options) {
|
|
2319
|
+
const state = getState();
|
|
2320
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2321
|
+
if (refs > 0 && !options?.force) {
|
|
2322
|
+
console.warn(
|
|
2323
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2324
|
+
);
|
|
2325
|
+
return false;
|
|
2326
|
+
}
|
|
2327
|
+
state.textures.get(key)?.dispose();
|
|
2328
|
+
setState((s) => {
|
|
2329
|
+
const textures = new Map(s.textures);
|
|
2330
|
+
textures.delete(key);
|
|
2331
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2332
|
+
nextRefs.delete(key);
|
|
2333
|
+
return { textures, _textureRefs: nextRefs };
|
|
2334
|
+
});
|
|
2335
|
+
return true;
|
|
2336
|
+
},
|
|
2337
|
+
disposeAll() {
|
|
2338
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2339
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2340
|
+
}
|
|
2723
2341
|
};
|
|
2724
2342
|
}, [store]);
|
|
2343
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2344
|
+
const selected = useThree(subscribe);
|
|
2345
|
+
return selector ? selected : registry;
|
|
2725
2346
|
}
|
|
2726
2347
|
|
|
2727
|
-
function useRenderTarget(
|
|
2348
|
+
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
2728
2349
|
const isLegacy = useThree((s) => s.isLegacy);
|
|
2729
2350
|
const size = useThree((s) => s.size);
|
|
2351
|
+
let width;
|
|
2352
|
+
let height;
|
|
2353
|
+
let opts;
|
|
2354
|
+
if (typeof widthOrOptions === "object") {
|
|
2355
|
+
opts = widthOrOptions;
|
|
2356
|
+
} else if (typeof widthOrOptions === "number") {
|
|
2357
|
+
width = widthOrOptions;
|
|
2358
|
+
if (typeof heightOrOptions === "object") {
|
|
2359
|
+
height = widthOrOptions;
|
|
2360
|
+
opts = heightOrOptions;
|
|
2361
|
+
} else if (typeof heightOrOptions === "number") {
|
|
2362
|
+
height = heightOrOptions;
|
|
2363
|
+
opts = options;
|
|
2364
|
+
} else {
|
|
2365
|
+
height = widthOrOptions;
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2730
2368
|
return useMemo(() => {
|
|
2731
2369
|
const w = width ?? size.width;
|
|
2732
2370
|
const h = height ?? size.height;
|
|
2733
|
-
return new WebGLRenderTarget(w, h,
|
|
2734
|
-
}, [width, height, size.width, size.height,
|
|
2371
|
+
return new WebGLRenderTarget(w, h, opts);
|
|
2372
|
+
}, [width, height, size.width, size.height, opts, isLegacy]);
|
|
2735
2373
|
}
|
|
2736
2374
|
|
|
2737
2375
|
function useStore() {
|
|
@@ -2781,11 +2419,11 @@ function addTail(callback) {
|
|
|
2781
2419
|
function invalidate(state, frames = 1, stackFrames = false) {
|
|
2782
2420
|
getScheduler().invalidate(frames, stackFrames);
|
|
2783
2421
|
}
|
|
2784
|
-
function advance(timestamp
|
|
2422
|
+
function advance(timestamp) {
|
|
2785
2423
|
getScheduler().step(timestamp);
|
|
2786
2424
|
}
|
|
2787
2425
|
|
|
2788
|
-
const version = "10.0.0-alpha.
|
|
2426
|
+
const version = "10.0.0-alpha.3";
|
|
2789
2427
|
const packageData = {
|
|
2790
2428
|
version: version};
|
|
2791
2429
|
|
|
@@ -14224,6 +13862,7 @@ function swapInstances() {
|
|
|
14224
13862
|
}
|
|
14225
13863
|
}
|
|
14226
13864
|
if (instance.isHidden) unhideInstance(instance);
|
|
13865
|
+
if (isObject3D(instance.object)) removeInteractivity(findInitialRoot(instance), instance.object);
|
|
14227
13866
|
if (instance.object.__r3f) delete instance.object.__r3f;
|
|
14228
13867
|
if (instance.type !== "primitive") disposeOnIdle(instance.object);
|
|
14229
13868
|
}
|
|
@@ -14235,6 +13874,9 @@ function swapInstances() {
|
|
|
14235
13874
|
instance.object = instance.props.object ?? new target(...instance.props.args ?? []);
|
|
14236
13875
|
instance.object.__r3f = instance;
|
|
14237
13876
|
setFiberRef(fiber, instance.object);
|
|
13877
|
+
delete instance.appliedOnce;
|
|
13878
|
+
instance.eventCount = 0;
|
|
13879
|
+
instance.handlers = {};
|
|
14238
13880
|
applyProps(instance.object, instance.props);
|
|
14239
13881
|
if (instance.props.attach) {
|
|
14240
13882
|
attach(parent, instance);
|
|
@@ -14308,8 +13950,22 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14308
13950
|
const isTailSibling = fiber.sibling === null || (fiber.flags & Update) === NoFlags;
|
|
14309
13951
|
if (isTailSibling) swapInstances();
|
|
14310
13952
|
},
|
|
14311
|
-
finalizeInitialChildren: () =>
|
|
14312
|
-
|
|
13953
|
+
finalizeInitialChildren: (instance) => {
|
|
13954
|
+
for (const prop in instance.props) {
|
|
13955
|
+
if (isFromRef(instance.props[prop])) return true;
|
|
13956
|
+
}
|
|
13957
|
+
return false;
|
|
13958
|
+
},
|
|
13959
|
+
commitMount(instance) {
|
|
13960
|
+
const resolved = {};
|
|
13961
|
+
for (const prop in instance.props) {
|
|
13962
|
+
const value = instance.props[prop];
|
|
13963
|
+
if (isFromRef(value)) {
|
|
13964
|
+
const ref = value[FROM_REF];
|
|
13965
|
+
if (ref.current != null) resolved[prop] = ref.current;
|
|
13966
|
+
}
|
|
13967
|
+
}
|
|
13968
|
+
if (Object.keys(resolved).length) applyProps(instance.object, resolved);
|
|
14313
13969
|
},
|
|
14314
13970
|
getPublicInstance: (instance) => instance?.object,
|
|
14315
13971
|
prepareForCommit: () => null,
|
|
@@ -14436,11 +14092,15 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14436
14092
|
cloneMutableTextInstance(textInstance) {
|
|
14437
14093
|
return textInstance;
|
|
14438
14094
|
},
|
|
14095
|
+
// React 19.2 added these for root-level view transitions. They only run for hosts that opt into
|
|
14096
|
+
// view transitions by returning a container from cloneRootViewTransitionContainer; R3F renders
|
|
14097
|
+
// into a WebGL/WebGPU canvas, where cloning the root has no meaning, so we never opt in and
|
|
14098
|
+
// React never calls them. They stay as no-ops rather than throws: the host config is shared by
|
|
14099
|
+
// every root, and a throw here would turn a React-internal probe into a hard crash for users.
|
|
14100
|
+
// Guarded by a test asserting a full render + unmount never reaches them.
|
|
14439
14101
|
cloneRootViewTransitionContainer(_rootContainer) {
|
|
14440
|
-
throw new Error("Not implemented.");
|
|
14441
14102
|
},
|
|
14442
14103
|
removeRootViewTransitionClone(_rootContainer, _clone) {
|
|
14443
|
-
throw new Error("Not implemented.");
|
|
14444
14104
|
},
|
|
14445
14105
|
// https://github.com/facebook/react/pull/32465
|
|
14446
14106
|
createFragmentInstance: (_fiber) => null,
|
|
@@ -14525,11 +14185,15 @@ function createRoot(canvas) {
|
|
|
14525
14185
|
const lastConfiguredProps = {};
|
|
14526
14186
|
let configured = false;
|
|
14527
14187
|
let pending = null;
|
|
14188
|
+
let rendererSetup = null;
|
|
14528
14189
|
return {
|
|
14529
14190
|
async configure(props = {}) {
|
|
14530
14191
|
let resolve;
|
|
14531
14192
|
pending = new Promise((_resolve) => resolve = _resolve);
|
|
14532
14193
|
const {
|
|
14194
|
+
id: canvasId,
|
|
14195
|
+
primaryCanvas,
|
|
14196
|
+
scheduler: schedulerConfig,
|
|
14533
14197
|
gl: glConfig,
|
|
14534
14198
|
renderer: rendererConfig,
|
|
14535
14199
|
size: propsSize,
|
|
@@ -14537,10 +14201,6 @@ function createRoot(canvas) {
|
|
|
14537
14201
|
events,
|
|
14538
14202
|
onCreated: onCreatedCallback,
|
|
14539
14203
|
shadows = false,
|
|
14540
|
-
linear = false,
|
|
14541
|
-
flat = false,
|
|
14542
|
-
textureColorSpace = SRGBColorSpace,
|
|
14543
|
-
legacy = false,
|
|
14544
14204
|
orthographic = false,
|
|
14545
14205
|
frameloop = "always",
|
|
14546
14206
|
dpr = [1, 2],
|
|
@@ -14552,9 +14212,11 @@ function createRoot(canvas) {
|
|
|
14552
14212
|
onDropMissed,
|
|
14553
14213
|
autoUpdateFrustum = true,
|
|
14554
14214
|
occlusion = false,
|
|
14555
|
-
_sizeProps
|
|
14215
|
+
_sizeProps,
|
|
14216
|
+
forceEven
|
|
14556
14217
|
} = props;
|
|
14557
|
-
const
|
|
14218
|
+
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || SRGBColorSpace;
|
|
14219
|
+
let state = store.getState();
|
|
14558
14220
|
const defaultGLProps = {
|
|
14559
14221
|
canvas,
|
|
14560
14222
|
powerPreference: "high-performance",
|
|
@@ -14566,15 +14228,37 @@ function createRoot(canvas) {
|
|
|
14566
14228
|
"WebGPURenderer (renderer prop) is not available in this build. Use @react-three/fiber or @react-three/fiber/webgpu instead."
|
|
14567
14229
|
);
|
|
14568
14230
|
}
|
|
14569
|
-
(state.isLegacy || glConfig || !R3F_BUILD_WEBGPU);
|
|
14231
|
+
const wantsGL = (state.isLegacy || glConfig || !R3F_BUILD_WEBGPU);
|
|
14570
14232
|
if (glConfig && rendererConfig) {
|
|
14571
14233
|
throw new Error("Cannot use both gl and renderer props at the same time");
|
|
14572
14234
|
}
|
|
14573
14235
|
let renderer = state.internal.actualRenderer;
|
|
14236
|
+
if (primaryCanvas && !R3F_BUILD_WEBGPU) {
|
|
14237
|
+
throw new Error(
|
|
14238
|
+
"The `primaryCanvas` prop for multi-canvas rendering is only available with WebGPU. Use @react-three/fiber/webgpu instead."
|
|
14239
|
+
);
|
|
14240
|
+
}
|
|
14241
|
+
if (primaryCanvas && wantsGL) {
|
|
14242
|
+
throw new Error(
|
|
14243
|
+
"The `primaryCanvas` prop for multi-canvas rendering cannot be used with WebGL. Remove the `gl` prop or use WebGPU."
|
|
14244
|
+
);
|
|
14245
|
+
}
|
|
14574
14246
|
if (!state.internal.actualRenderer) {
|
|
14575
|
-
|
|
14576
|
-
|
|
14577
|
-
|
|
14247
|
+
if (!rendererSetup) {
|
|
14248
|
+
rendererSetup = (async () => {
|
|
14249
|
+
{
|
|
14250
|
+
renderer = await resolveRenderer(glConfig, defaultGLProps, WebGLRenderer);
|
|
14251
|
+
state.internal.actualRenderer = renderer;
|
|
14252
|
+
state.set({ isLegacy: true, gl: renderer, renderer, primaryStore: store });
|
|
14253
|
+
}
|
|
14254
|
+
})().catch((err) => {
|
|
14255
|
+
rendererSetup = null;
|
|
14256
|
+
throw err;
|
|
14257
|
+
});
|
|
14258
|
+
}
|
|
14259
|
+
await rendererSetup;
|
|
14260
|
+
state = store.getState();
|
|
14261
|
+
renderer = state.internal.actualRenderer;
|
|
14578
14262
|
}
|
|
14579
14263
|
let raycaster = state.raycaster;
|
|
14580
14264
|
if (!raycaster) state.set({ raycaster: raycaster = new Raycaster() });
|
|
@@ -14583,6 +14267,7 @@ function createRoot(canvas) {
|
|
|
14583
14267
|
if (!is.equ(params, raycaster.params, shallowLoose)) {
|
|
14584
14268
|
applyProps(raycaster, { params: { ...raycaster.params, ...params } });
|
|
14585
14269
|
}
|
|
14270
|
+
let tempCamera = state.camera;
|
|
14586
14271
|
if (!state.camera || state.camera === lastCamera && !is.equ(lastCamera, cameraOptions, shallowLoose)) {
|
|
14587
14272
|
lastCamera = cameraOptions;
|
|
14588
14273
|
const isCamera = cameraOptions?.isCamera;
|
|
@@ -14602,6 +14287,7 @@ function createRoot(canvas) {
|
|
|
14602
14287
|
if (!state.camera && !cameraOptions?.rotation) camera.lookAt(0, 0, 0);
|
|
14603
14288
|
}
|
|
14604
14289
|
state.set({ camera });
|
|
14290
|
+
tempCamera = camera;
|
|
14605
14291
|
raycaster.camera = camera;
|
|
14606
14292
|
}
|
|
14607
14293
|
if (!state.scene) {
|
|
@@ -14619,7 +14305,7 @@ function createRoot(canvas) {
|
|
|
14619
14305
|
rootScene: scene,
|
|
14620
14306
|
internal: { ...prev.internal, container: scene }
|
|
14621
14307
|
}));
|
|
14622
|
-
const camera =
|
|
14308
|
+
const camera = tempCamera;
|
|
14623
14309
|
if (camera && !camera.parent) scene.add(camera);
|
|
14624
14310
|
}
|
|
14625
14311
|
if (events && !state.events.handlers) {
|
|
@@ -14636,6 +14322,9 @@ function createRoot(canvas) {
|
|
|
14636
14322
|
if (_sizeProps !== void 0) {
|
|
14637
14323
|
state.set({ _sizeProps });
|
|
14638
14324
|
}
|
|
14325
|
+
if (forceEven !== void 0 && state.internal.forceEven !== forceEven) {
|
|
14326
|
+
state.set((prev) => ({ internal: { ...prev.internal, forceEven } }));
|
|
14327
|
+
}
|
|
14639
14328
|
const size = computeInitialSize(canvas, propsSize);
|
|
14640
14329
|
if (!state._sizeImperative && !is.equ(size, state.size, shallowLoose)) {
|
|
14641
14330
|
const wasImperative = state._sizeImperative;
|
|
@@ -14662,10 +14351,10 @@ function createRoot(canvas) {
|
|
|
14662
14351
|
lastConfiguredProps.performance = performance;
|
|
14663
14352
|
}
|
|
14664
14353
|
if (!state.xr) {
|
|
14665
|
-
const handleXRFrame = (timestamp,
|
|
14354
|
+
const handleXRFrame = (timestamp, _frame) => {
|
|
14666
14355
|
const state2 = store.getState();
|
|
14667
14356
|
if (state2.frameloop === "never") return;
|
|
14668
|
-
advance(timestamp
|
|
14357
|
+
advance(timestamp);
|
|
14669
14358
|
};
|
|
14670
14359
|
const actualRenderer = state.internal.actualRenderer;
|
|
14671
14360
|
const handleSessionChange = () => {
|
|
@@ -14677,16 +14366,16 @@ function createRoot(canvas) {
|
|
|
14677
14366
|
};
|
|
14678
14367
|
const xr = {
|
|
14679
14368
|
connect() {
|
|
14680
|
-
const { gl, renderer: renderer2
|
|
14681
|
-
const
|
|
14682
|
-
|
|
14683
|
-
|
|
14369
|
+
const { gl, renderer: renderer2 } = store.getState();
|
|
14370
|
+
const xrManager = (renderer2 || gl).xr;
|
|
14371
|
+
xrManager.addEventListener("sessionstart", handleSessionChange);
|
|
14372
|
+
xrManager.addEventListener("sessionend", handleSessionChange);
|
|
14684
14373
|
},
|
|
14685
14374
|
disconnect() {
|
|
14686
|
-
const { gl, renderer: renderer2
|
|
14687
|
-
const
|
|
14688
|
-
|
|
14689
|
-
|
|
14375
|
+
const { gl, renderer: renderer2 } = store.getState();
|
|
14376
|
+
const xrManager = (renderer2 || gl).xr;
|
|
14377
|
+
xrManager.removeEventListener("sessionstart", handleSessionChange);
|
|
14378
|
+
xrManager.removeEventListener("sessionend", handleSessionChange);
|
|
14690
14379
|
}
|
|
14691
14380
|
};
|
|
14692
14381
|
if (typeof renderer.xr?.addEventListener === "function") xr.connect();
|
|
@@ -14698,15 +14387,22 @@ function createRoot(canvas) {
|
|
|
14698
14387
|
const oldType = renderer.shadowMap.type;
|
|
14699
14388
|
renderer.shadowMap.enabled = !!shadows;
|
|
14700
14389
|
if (is.boo(shadows)) {
|
|
14701
|
-
renderer.shadowMap.type =
|
|
14390
|
+
renderer.shadowMap.type = PCFShadowMap;
|
|
14702
14391
|
} else if (is.str(shadows)) {
|
|
14392
|
+
if (shadows === "soft") {
|
|
14393
|
+
notifyDepreciated({
|
|
14394
|
+
heading: 'shadows="soft" is deprecated',
|
|
14395
|
+
body: "Three has depreciated soft and improved basic PCFShadows, we converted for you.",
|
|
14396
|
+
link: "https://github.com/mrdoob/three.js/wiki/Migration-Guide?utm_source=chatgpt.com#181--182"
|
|
14397
|
+
});
|
|
14398
|
+
}
|
|
14703
14399
|
const types = {
|
|
14704
14400
|
basic: BasicShadowMap,
|
|
14705
14401
|
percentage: PCFShadowMap,
|
|
14706
|
-
soft:
|
|
14402
|
+
soft: PCFShadowMap,
|
|
14707
14403
|
variance: VSMShadowMap
|
|
14708
14404
|
};
|
|
14709
|
-
renderer.shadowMap.type = types[shadows] ??
|
|
14405
|
+
renderer.shadowMap.type = types[shadows] ?? PCFShadowMap;
|
|
14710
14406
|
} else if (is.obj(shadows)) {
|
|
14711
14407
|
Object.assign(renderer.shadowMap, shadows);
|
|
14712
14408
|
}
|
|
@@ -14714,47 +14410,69 @@ function createRoot(canvas) {
|
|
|
14714
14410
|
renderer.shadowMap.needsUpdate = true;
|
|
14715
14411
|
}
|
|
14716
14412
|
}
|
|
14717
|
-
{
|
|
14718
|
-
|
|
14719
|
-
|
|
14720
|
-
const flatChanged = flat !== lastConfiguredProps.flat;
|
|
14721
|
-
if (legacyChanged) {
|
|
14722
|
-
ColorManagement.enabled = !legacy;
|
|
14723
|
-
lastConfiguredProps.legacy = legacy;
|
|
14724
|
-
}
|
|
14725
|
-
if (!configured || linearChanged) {
|
|
14726
|
-
renderer.outputColorSpace = linear ? LinearSRGBColorSpace : SRGBColorSpace;
|
|
14727
|
-
lastConfiguredProps.linear = linear;
|
|
14728
|
-
}
|
|
14729
|
-
if (!configured || flatChanged) {
|
|
14730
|
-
renderer.toneMapping = flat ? NoToneMapping : ACESFilmicToneMapping;
|
|
14731
|
-
lastConfiguredProps.flat = flat;
|
|
14732
|
-
}
|
|
14733
|
-
if (legacyChanged && state.legacy !== legacy) state.set(() => ({ legacy }));
|
|
14734
|
-
if (linearChanged && state.linear !== linear) state.set(() => ({ linear }));
|
|
14735
|
-
if (flatChanged && state.flat !== flat) state.set(() => ({ flat }));
|
|
14413
|
+
if (!configured) {
|
|
14414
|
+
renderer.outputColorSpace = SRGBColorSpace;
|
|
14415
|
+
renderer.toneMapping = ACESFilmicToneMapping;
|
|
14736
14416
|
}
|
|
14737
14417
|
if (textureColorSpace !== lastConfiguredProps.textureColorSpace) {
|
|
14738
14418
|
if (state.textureColorSpace !== textureColorSpace) state.set(() => ({ textureColorSpace }));
|
|
14739
14419
|
lastConfiguredProps.textureColorSpace = textureColorSpace;
|
|
14740
14420
|
}
|
|
14421
|
+
const r3fProps = ["textureColorSpace"];
|
|
14422
|
+
const constructorOnlyProps = ["samples", "antialias", "alpha", "canvas", "powerPreference"];
|
|
14423
|
+
const nonApplyProps = [...r3fProps, ...constructorOnlyProps];
|
|
14741
14424
|
if (glConfig && !is.fun(glConfig) && !isRenderer(glConfig) && !is.equ(glConfig, renderer, shallowLoose)) {
|
|
14742
|
-
|
|
14425
|
+
const glProps = {};
|
|
14426
|
+
for (const key in glConfig) {
|
|
14427
|
+
if (!nonApplyProps.includes(key)) glProps[key] = glConfig[key];
|
|
14428
|
+
}
|
|
14429
|
+
applyProps(renderer, glProps);
|
|
14743
14430
|
}
|
|
14744
14431
|
if (rendererConfig && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && state.renderer) {
|
|
14745
14432
|
const currentRenderer = state.renderer;
|
|
14746
14433
|
if (!is.equ(rendererConfig, currentRenderer, shallowLoose)) {
|
|
14747
|
-
|
|
14434
|
+
const rendererProps = {};
|
|
14435
|
+
for (const key in rendererConfig) {
|
|
14436
|
+
if (!nonApplyProps.includes(key)) rendererProps[key] = rendererConfig[key];
|
|
14437
|
+
}
|
|
14438
|
+
applyProps(currentRenderer, rendererProps);
|
|
14748
14439
|
}
|
|
14749
14440
|
}
|
|
14750
14441
|
const scheduler = getScheduler();
|
|
14751
14442
|
const rootId = state.internal.rootId;
|
|
14752
14443
|
if (!rootId) {
|
|
14753
|
-
const newRootId = scheduler.generateRootId();
|
|
14444
|
+
const newRootId = canvasId || scheduler.generateRootId();
|
|
14754
14445
|
const unregisterRoot = scheduler.registerRoot(newRootId, {
|
|
14755
14446
|
getState: () => store.getState(),
|
|
14756
14447
|
onError: (err) => store.getState().setError(err)
|
|
14757
14448
|
});
|
|
14449
|
+
const unregisterCanvasTarget = scheduler.register(
|
|
14450
|
+
() => {
|
|
14451
|
+
const state2 = store.getState();
|
|
14452
|
+
if (state2.internal.isMultiCanvas && state2.internal.canvasTarget) {
|
|
14453
|
+
const renderer2 = state2.internal.actualRenderer;
|
|
14454
|
+
renderer2.setCanvasTarget(state2.internal.canvasTarget);
|
|
14455
|
+
}
|
|
14456
|
+
},
|
|
14457
|
+
{
|
|
14458
|
+
id: `${newRootId}_canvasTarget`,
|
|
14459
|
+
rootId: newRootId,
|
|
14460
|
+
phase: "start",
|
|
14461
|
+
system: true
|
|
14462
|
+
}
|
|
14463
|
+
);
|
|
14464
|
+
const unregisterEventsFlush = scheduler.register(
|
|
14465
|
+
() => {
|
|
14466
|
+
const state2 = store.getState();
|
|
14467
|
+
state2.events.flush?.();
|
|
14468
|
+
},
|
|
14469
|
+
{
|
|
14470
|
+
id: `${newRootId}_events`,
|
|
14471
|
+
rootId: newRootId,
|
|
14472
|
+
phase: "input",
|
|
14473
|
+
system: true
|
|
14474
|
+
}
|
|
14475
|
+
);
|
|
14758
14476
|
const unregisterFrustum = scheduler.register(
|
|
14759
14477
|
() => {
|
|
14760
14478
|
const state2 = store.getState();
|
|
@@ -14765,7 +14483,7 @@ function createRoot(canvas) {
|
|
|
14765
14483
|
{
|
|
14766
14484
|
id: `${newRootId}_frustum`,
|
|
14767
14485
|
rootId: newRootId,
|
|
14768
|
-
|
|
14486
|
+
before: "render",
|
|
14769
14487
|
system: true
|
|
14770
14488
|
}
|
|
14771
14489
|
);
|
|
@@ -14777,7 +14495,7 @@ function createRoot(canvas) {
|
|
|
14777
14495
|
{
|
|
14778
14496
|
id: `${newRootId}_visibility`,
|
|
14779
14497
|
rootId: newRootId,
|
|
14780
|
-
|
|
14498
|
+
before: "render",
|
|
14781
14499
|
system: true,
|
|
14782
14500
|
after: `${newRootId}_frustum`
|
|
14783
14501
|
}
|
|
@@ -14789,18 +14507,22 @@ function createRoot(canvas) {
|
|
|
14789
14507
|
const userHandlesRender = scheduler.hasUserJobsInPhase("render", newRootId);
|
|
14790
14508
|
if (userHandlesRender || state2.internal.priority) return;
|
|
14791
14509
|
try {
|
|
14792
|
-
if (state2.
|
|
14510
|
+
if (state2.renderPipeline?.render) state2.renderPipeline.render();
|
|
14793
14511
|
else if (renderer2?.render) renderer2.render(state2.scene, state2.camera);
|
|
14794
14512
|
} catch (error) {
|
|
14795
14513
|
state2.setError(error instanceof Error ? error : new Error(String(error)));
|
|
14796
14514
|
}
|
|
14797
14515
|
},
|
|
14798
14516
|
{
|
|
14799
|
-
|
|
14517
|
+
// Use canvas ID directly as job ID if available, otherwise use generated rootId
|
|
14518
|
+
id: canvasId || `${newRootId}_render`,
|
|
14800
14519
|
rootId: newRootId,
|
|
14801
14520
|
phase: "render",
|
|
14802
|
-
system: true
|
|
14521
|
+
system: true,
|
|
14803
14522
|
// Internal flag: this is a system job, not user-controlled
|
|
14523
|
+
// Apply scheduler config for render ordering and rate limiting
|
|
14524
|
+
...schedulerConfig?.after && { after: schedulerConfig.after },
|
|
14525
|
+
...schedulerConfig?.fps && { fps: schedulerConfig.fps }
|
|
14804
14526
|
}
|
|
14805
14527
|
);
|
|
14806
14528
|
state.set((state2) => ({
|
|
@@ -14809,6 +14531,8 @@ function createRoot(canvas) {
|
|
|
14809
14531
|
rootId: newRootId,
|
|
14810
14532
|
unregisterRoot: () => {
|
|
14811
14533
|
unregisterRoot();
|
|
14534
|
+
unregisterCanvasTarget();
|
|
14535
|
+
unregisterEventsFlush();
|
|
14812
14536
|
unregisterFrustum();
|
|
14813
14537
|
unregisterVisibility();
|
|
14814
14538
|
unregisterRender();
|
|
@@ -14867,15 +14591,24 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
14867
14591
|
const renderer = state.internal.actualRenderer;
|
|
14868
14592
|
const unregisterRoot = state.internal.unregisterRoot;
|
|
14869
14593
|
if (unregisterRoot) unregisterRoot();
|
|
14594
|
+
const unregisterPrimary = state.internal.unregisterPrimary;
|
|
14595
|
+
if (unregisterPrimary) unregisterPrimary();
|
|
14596
|
+
const canvasTarget = state.internal.canvasTarget;
|
|
14597
|
+
if (canvasTarget?.dispose) canvasTarget.dispose();
|
|
14870
14598
|
state.events.disconnect?.();
|
|
14871
14599
|
cleanupHelperGroup(root.store);
|
|
14872
|
-
renderer
|
|
14873
|
-
|
|
14874
|
-
|
|
14600
|
+
if (state.isLegacy && renderer) {
|
|
14601
|
+
;
|
|
14602
|
+
renderer.renderLists?.dispose?.();
|
|
14603
|
+
renderer.forceContextLoss?.();
|
|
14604
|
+
}
|
|
14605
|
+
if (!state.internal.isSecondary) {
|
|
14606
|
+
if (renderer?.xr) state.xr.disconnect();
|
|
14607
|
+
}
|
|
14875
14608
|
dispose(state.scene);
|
|
14876
14609
|
_roots.delete(canvas);
|
|
14877
14610
|
if (callback) callback(canvas);
|
|
14878
|
-
} catch
|
|
14611
|
+
} catch {
|
|
14879
14612
|
}
|
|
14880
14613
|
}, 500);
|
|
14881
14614
|
}
|
|
@@ -14883,36 +14616,34 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
14883
14616
|
}
|
|
14884
14617
|
}
|
|
14885
14618
|
function createPortal(children, container, state) {
|
|
14886
|
-
return /* @__PURE__ */ jsx(
|
|
14619
|
+
return /* @__PURE__ */ jsx(Portal, { children, container, state });
|
|
14887
14620
|
}
|
|
14888
|
-
function
|
|
14621
|
+
function Portal({ children, container, state }) {
|
|
14889
14622
|
const isRef = useCallback((obj) => obj && "current" in obj, []);
|
|
14890
|
-
const [resolvedContainer,
|
|
14623
|
+
const [resolvedContainer, _setResolvedContainer] = useState(() => {
|
|
14891
14624
|
if (isRef(container)) return container.current ?? null;
|
|
14892
14625
|
return container;
|
|
14893
14626
|
});
|
|
14627
|
+
const setResolvedContainer = useCallback(
|
|
14628
|
+
(newContainer) => {
|
|
14629
|
+
if (!newContainer || newContainer === resolvedContainer) return;
|
|
14630
|
+
_setResolvedContainer(isRef(newContainer) ? newContainer.current : newContainer);
|
|
14631
|
+
},
|
|
14632
|
+
[resolvedContainer, _setResolvedContainer, isRef]
|
|
14633
|
+
);
|
|
14894
14634
|
useMemo(() => {
|
|
14895
|
-
if (isRef(container)) {
|
|
14896
|
-
|
|
14897
|
-
|
|
14898
|
-
|
|
14899
|
-
const updated = container.current;
|
|
14900
|
-
if (updated && updated !== resolvedContainer) {
|
|
14901
|
-
setResolvedContainer(updated);
|
|
14902
|
-
}
|
|
14903
|
-
});
|
|
14904
|
-
} else if (current !== resolvedContainer) {
|
|
14905
|
-
setResolvedContainer(current);
|
|
14906
|
-
}
|
|
14907
|
-
} else if (container !== resolvedContainer) {
|
|
14908
|
-
setResolvedContainer(container);
|
|
14635
|
+
if (isRef(container) && !container.current) {
|
|
14636
|
+
return queueMicrotask(() => {
|
|
14637
|
+
setResolvedContainer(container.current);
|
|
14638
|
+
});
|
|
14909
14639
|
}
|
|
14910
|
-
|
|
14640
|
+
setResolvedContainer(container);
|
|
14641
|
+
}, [container, isRef, setResolvedContainer]);
|
|
14911
14642
|
if (!resolvedContainer) return /* @__PURE__ */ jsx(Fragment, {});
|
|
14912
14643
|
const portalKey = resolvedContainer.uuid ?? `portal-${resolvedContainer.id ?? "unknown"}`;
|
|
14913
|
-
return /* @__PURE__ */ jsx(
|
|
14644
|
+
return /* @__PURE__ */ jsx(PortalInner, { children, container: resolvedContainer, state }, portalKey);
|
|
14914
14645
|
}
|
|
14915
|
-
function
|
|
14646
|
+
function PortalInner({ state = {}, children, container }) {
|
|
14916
14647
|
const { events, size, injectScene = true, ...rest } = state;
|
|
14917
14648
|
const previousRoot = useStore();
|
|
14918
14649
|
const [raycaster] = useState(() => new Raycaster());
|
|
@@ -14933,12 +14664,14 @@ function Portal({ state = {}, children, container }) {
|
|
|
14933
14664
|
};
|
|
14934
14665
|
}, [portalScene, container, injectScene]);
|
|
14935
14666
|
const inject = useMutableCallback((rootState, injectState) => {
|
|
14667
|
+
const resolvedSize = { ...rootState.size, ...injectState.size, ...size };
|
|
14936
14668
|
let viewport = void 0;
|
|
14937
|
-
if (injectState.camera && size) {
|
|
14669
|
+
if (injectState.camera && (size || injectState.size)) {
|
|
14938
14670
|
const camera = injectState.camera;
|
|
14939
|
-
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(),
|
|
14940
|
-
if (camera !== rootState.camera) updateCamera(camera,
|
|
14671
|
+
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(), resolvedSize);
|
|
14672
|
+
if (camera !== rootState.camera) updateCamera(camera, resolvedSize);
|
|
14941
14673
|
}
|
|
14674
|
+
const set = injectState.set;
|
|
14942
14675
|
return {
|
|
14943
14676
|
// The intersect consists of the previous root state
|
|
14944
14677
|
...rootState,
|
|
@@ -14954,10 +14687,10 @@ function Portal({ state = {}, children, container }) {
|
|
|
14954
14687
|
previousRoot,
|
|
14955
14688
|
// Events, size and viewport can be overridden by the inject layer
|
|
14956
14689
|
events: { ...rootState.events, ...injectState.events, ...events },
|
|
14957
|
-
size:
|
|
14690
|
+
size: resolvedSize,
|
|
14958
14691
|
viewport: { ...rootState.viewport, ...viewport },
|
|
14959
14692
|
// Layers are allowed to override events
|
|
14960
|
-
setEvents: (events2) =>
|
|
14693
|
+
setEvents: (events2) => set((state2) => ({ ...state2, events: { ...state2.events, ...events2 } })),
|
|
14961
14694
|
// Container for child attachment - the portalScene (injected or container itself)
|
|
14962
14695
|
internal: { ...rootState.internal, ...injectState.internal, container: portalScene }
|
|
14963
14696
|
};
|
|
@@ -14966,9 +14699,13 @@ function Portal({ state = {}, children, container }) {
|
|
|
14966
14699
|
const store = createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
14967
14700
|
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
14968
14701
|
onMutate(previousRoot.getState());
|
|
14969
|
-
previousRoot.subscribe(onMutate);
|
|
14970
14702
|
return store;
|
|
14971
14703
|
}, [previousRoot, container]);
|
|
14704
|
+
useIsomorphicLayoutEffect(() => {
|
|
14705
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14706
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14707
|
+
return unsubscribe;
|
|
14708
|
+
}, [previousRoot, usePortalStore]);
|
|
14972
14709
|
return (
|
|
14973
14710
|
// @ts-ignore, reconciler types are not maintained
|
|
14974
14711
|
/* @__PURE__ */ jsx(Fragment, { children: reconciler.createPortal(
|
|
@@ -14982,21 +14719,72 @@ function flushSync(fn) {
|
|
|
14982
14719
|
return reconciler.flushSyncFromReconciler(fn);
|
|
14983
14720
|
}
|
|
14984
14721
|
|
|
14722
|
+
function parseBackground(background) {
|
|
14723
|
+
if (!background) return null;
|
|
14724
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14725
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14726
|
+
return {
|
|
14727
|
+
...rest,
|
|
14728
|
+
preset,
|
|
14729
|
+
files: envMap || files,
|
|
14730
|
+
backgroundFiles: backgroundMap,
|
|
14731
|
+
background: true
|
|
14732
|
+
};
|
|
14733
|
+
}
|
|
14734
|
+
if (typeof background === "number") {
|
|
14735
|
+
return { color: background, background: true };
|
|
14736
|
+
}
|
|
14737
|
+
if (typeof background === "string") {
|
|
14738
|
+
if (background in presetsObj) {
|
|
14739
|
+
return { preset: background, background: true };
|
|
14740
|
+
}
|
|
14741
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14742
|
+
return { files: background, background: true };
|
|
14743
|
+
}
|
|
14744
|
+
return { color: background, background: true };
|
|
14745
|
+
}
|
|
14746
|
+
if (background.isColor) {
|
|
14747
|
+
return { color: background, background: true };
|
|
14748
|
+
}
|
|
14749
|
+
return null;
|
|
14750
|
+
}
|
|
14751
|
+
|
|
14752
|
+
function parseRendererConfig(rendererProp) {
|
|
14753
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14754
|
+
if (isRendererConfig) {
|
|
14755
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14756
|
+
return {
|
|
14757
|
+
primaryCanvas: pc,
|
|
14758
|
+
scheduler: sc,
|
|
14759
|
+
renderer: Object.keys(rest).length > 0 ? rest : rendererProp
|
|
14760
|
+
};
|
|
14761
|
+
}
|
|
14762
|
+
return { primaryCanvas: void 0, scheduler: void 0, renderer: rendererProp };
|
|
14763
|
+
}
|
|
14764
|
+
|
|
14765
|
+
function clearHmrCaches(store) {
|
|
14766
|
+
store.setState((state) => ({
|
|
14767
|
+
nodes: {},
|
|
14768
|
+
uniforms: {},
|
|
14769
|
+
buffers: {},
|
|
14770
|
+
gpuStorage: {},
|
|
14771
|
+
_hmrVersion: state._hmrVersion + 1
|
|
14772
|
+
}));
|
|
14773
|
+
}
|
|
14774
|
+
|
|
14985
14775
|
function CanvasImpl({
|
|
14986
14776
|
ref,
|
|
14987
14777
|
children,
|
|
14988
14778
|
fallback,
|
|
14989
14779
|
resize,
|
|
14990
14780
|
style,
|
|
14781
|
+
id,
|
|
14991
14782
|
gl,
|
|
14992
|
-
renderer,
|
|
14783
|
+
renderer: rendererProp,
|
|
14993
14784
|
events = createPointerEvents,
|
|
14994
14785
|
eventSource,
|
|
14995
14786
|
eventPrefix,
|
|
14996
14787
|
shadows,
|
|
14997
|
-
linear,
|
|
14998
|
-
flat,
|
|
14999
|
-
legacy,
|
|
15000
14788
|
orthographic,
|
|
15001
14789
|
frameloop,
|
|
15002
14790
|
dpr,
|
|
@@ -15004,6 +14792,8 @@ function CanvasImpl({
|
|
|
15004
14792
|
raycaster,
|
|
15005
14793
|
camera,
|
|
15006
14794
|
scene,
|
|
14795
|
+
autoUpdateFrustum,
|
|
14796
|
+
occlusion,
|
|
15007
14797
|
onPointerMissed,
|
|
15008
14798
|
onDragOverMissed,
|
|
15009
14799
|
onDropMissed,
|
|
@@ -15011,10 +14801,14 @@ function CanvasImpl({
|
|
|
15011
14801
|
hmr,
|
|
15012
14802
|
width,
|
|
15013
14803
|
height,
|
|
14804
|
+
background,
|
|
14805
|
+
forceEven,
|
|
15014
14806
|
...props
|
|
15015
14807
|
}) {
|
|
14808
|
+
const { primaryCanvas, scheduler, renderer } = parseRendererConfig(rendererProp);
|
|
15016
14809
|
React.useMemo(() => extend(THREE), []);
|
|
15017
14810
|
const Bridge = useBridge();
|
|
14811
|
+
const backgroundProps = React.useMemo(() => parseBackground(background), [background]);
|
|
15018
14812
|
const hasInitialSizeRef = React.useRef(false);
|
|
15019
14813
|
const measureConfig = React.useMemo(() => {
|
|
15020
14814
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15031,15 +14825,20 @@ function CanvasImpl({
|
|
|
15031
14825
|
};
|
|
15032
14826
|
}, [resize, hasInitialSizeRef.current]);
|
|
15033
14827
|
const [containerRef, containerRect] = useMeasure(measureConfig);
|
|
15034
|
-
const effectiveSize = React.useMemo(
|
|
15035
|
-
|
|
15036
|
-
|
|
15037
|
-
|
|
14828
|
+
const effectiveSize = React.useMemo(() => {
|
|
14829
|
+
let w = width ?? containerRect.width;
|
|
14830
|
+
let h = height ?? containerRect.height;
|
|
14831
|
+
if (forceEven) {
|
|
14832
|
+
w = Math.ceil(w / 2) * 2;
|
|
14833
|
+
h = Math.ceil(h / 2) * 2;
|
|
14834
|
+
}
|
|
14835
|
+
return {
|
|
14836
|
+
width: w,
|
|
14837
|
+
height: h,
|
|
15038
14838
|
top: containerRect.top,
|
|
15039
14839
|
left: containerRect.left
|
|
15040
|
-
}
|
|
15041
|
-
|
|
15042
|
-
);
|
|
14840
|
+
};
|
|
14841
|
+
}, [width, height, containerRect, forceEven]);
|
|
15043
14842
|
if (!hasInitialSizeRef.current && effectiveSize.width > 0 && effectiveSize.height > 0) {
|
|
15044
14843
|
hasInitialSizeRef.current = true;
|
|
15045
14844
|
}
|
|
@@ -15051,6 +14850,7 @@ function CanvasImpl({
|
|
|
15051
14850
|
const handleDropMissed = useMutableCallback(onDropMissed);
|
|
15052
14851
|
const [block, setBlock] = React.useState(false);
|
|
15053
14852
|
const [error, setError] = React.useState(false);
|
|
14853
|
+
const [fallbackVisible, setFallbackVisible] = React.useState(false);
|
|
15054
14854
|
if (block) throw block;
|
|
15055
14855
|
if (error) throw error;
|
|
15056
14856
|
const root = React.useRef(null);
|
|
@@ -15058,6 +14858,7 @@ function CanvasImpl({
|
|
|
15058
14858
|
const unsubscribeErrorRef = React.useRef(null);
|
|
15059
14859
|
useIsomorphicLayoutEffect(() => {
|
|
15060
14860
|
effectActiveRef.current = true;
|
|
14861
|
+
if (fallbackVisible) return;
|
|
15061
14862
|
const canvas = canvasRef.current;
|
|
15062
14863
|
if (effectiveSize.width > 0 && effectiveSize.height > 0 && canvas) {
|
|
15063
14864
|
if (!root.current) {
|
|
@@ -15078,24 +14879,27 @@ function CanvasImpl({
|
|
|
15078
14879
|
}
|
|
15079
14880
|
async function run() {
|
|
15080
14881
|
if (!effectActiveRef.current || !root.current) return;
|
|
15081
|
-
await root.current.configure({
|
|
14882
|
+
const configured = await root.current.configure({
|
|
14883
|
+
id,
|
|
14884
|
+
primaryCanvas,
|
|
14885
|
+
scheduler,
|
|
15082
14886
|
gl,
|
|
15083
14887
|
renderer,
|
|
15084
14888
|
scene,
|
|
15085
14889
|
events,
|
|
15086
14890
|
shadows,
|
|
15087
|
-
linear,
|
|
15088
|
-
flat,
|
|
15089
|
-
legacy,
|
|
15090
14891
|
orthographic,
|
|
15091
14892
|
frameloop,
|
|
15092
14893
|
dpr,
|
|
15093
14894
|
performance,
|
|
15094
14895
|
raycaster,
|
|
15095
14896
|
camera,
|
|
14897
|
+
autoUpdateFrustum,
|
|
14898
|
+
occlusion,
|
|
15096
14899
|
size: effectiveSize,
|
|
15097
14900
|
// Store size props for reset functionality
|
|
15098
14901
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
14902
|
+
forceEven,
|
|
15099
14903
|
// Pass mutable reference to onPointerMissed so it's free to update
|
|
15100
14904
|
onPointerMissed: (...args) => handlePointerMissed.current?.(...args),
|
|
15101
14905
|
onDragOverMissed: (...args) => handleDragOverMissed.current?.(...args),
|
|
@@ -15116,10 +14920,25 @@ function CanvasImpl({
|
|
|
15116
14920
|
}
|
|
15117
14921
|
onCreated?.(state);
|
|
15118
14922
|
}
|
|
15119
|
-
})
|
|
15120
|
-
|
|
14923
|
+
}).then(
|
|
14924
|
+
() => true,
|
|
14925
|
+
// Renderer setup failed (e.g. no WebGL/WebGPU support). The `fallback` prop lives
|
|
14926
|
+
// inside <canvas>, which browsers don't display, so surface it as visible DOM
|
|
14927
|
+
// instead; with no fallback, rethrow to an external error boundary. (#3757)
|
|
14928
|
+
(setupError) => {
|
|
14929
|
+
if (effectActiveRef.current) {
|
|
14930
|
+
if (fallback != null) setFallbackVisible(true);
|
|
14931
|
+
else setError(setupError);
|
|
14932
|
+
}
|
|
14933
|
+
return false;
|
|
14934
|
+
}
|
|
14935
|
+
);
|
|
14936
|
+
if (!configured || !effectActiveRef.current || !root.current) return;
|
|
15121
14937
|
root.current.render(
|
|
15122
|
-
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */
|
|
14938
|
+
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */ jsxs(React.Suspense, { fallback: /* @__PURE__ */ jsx(Block, { set: setBlock }), children: [
|
|
14939
|
+
backgroundProps && /* @__PURE__ */ jsx(Environment, { ...backgroundProps }),
|
|
14940
|
+
children ?? null
|
|
14941
|
+
] }) }) })
|
|
15123
14942
|
);
|
|
15124
14943
|
}
|
|
15125
14944
|
run();
|
|
@@ -15146,20 +14965,15 @@ function CanvasImpl({
|
|
|
15146
14965
|
const canvas = canvasRef.current;
|
|
15147
14966
|
if (!canvas) return;
|
|
15148
14967
|
const handleHMR = () => {
|
|
15149
|
-
|
|
15150
|
-
|
|
15151
|
-
rootEntry
|
|
15152
|
-
|
|
15153
|
-
uniforms: {},
|
|
15154
|
-
_hmrVersion: state._hmrVersion + 1
|
|
15155
|
-
}));
|
|
15156
|
-
}
|
|
14968
|
+
queueMicrotask(() => {
|
|
14969
|
+
const rootEntry = _roots.get(canvas);
|
|
14970
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
14971
|
+
});
|
|
15157
14972
|
};
|
|
15158
14973
|
if (typeof import.meta !== "undefined" && import.meta.hot) {
|
|
15159
14974
|
const hot = import.meta.hot;
|
|
15160
14975
|
hot.on("vite:afterUpdate", handleHMR);
|
|
15161
|
-
return () => hot.
|
|
15162
|
-
});
|
|
14976
|
+
return () => hot.off?.("vite:afterUpdate", handleHMR);
|
|
15163
14977
|
}
|
|
15164
14978
|
if (typeof module !== "undefined" && module.hot) {
|
|
15165
14979
|
const hot = module.hot;
|
|
@@ -15182,7 +14996,20 @@ function CanvasImpl({
|
|
|
15182
14996
|
...style
|
|
15183
14997
|
},
|
|
15184
14998
|
...props,
|
|
15185
|
-
children:
|
|
14999
|
+
children: fallbackVisible ? (
|
|
15000
|
+
// Renderer setup failed: render the fallback as visible DOM. Inside <canvas> (below)
|
|
15001
|
+
// it exists in the tree but browsers never paint it, which is the whole bug (#3757).
|
|
15002
|
+
fallback
|
|
15003
|
+
) : /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
15004
|
+
"canvas",
|
|
15005
|
+
{
|
|
15006
|
+
ref: canvasRef,
|
|
15007
|
+
id,
|
|
15008
|
+
className: "r3f-canvas",
|
|
15009
|
+
style: { display: "block", width: "100%", height: "100%" },
|
|
15010
|
+
children: fallback
|
|
15011
|
+
}
|
|
15012
|
+
) })
|
|
15186
15013
|
}
|
|
15187
15014
|
);
|
|
15188
15015
|
}
|
|
@@ -15192,4 +15019,4 @@ function Canvas(props) {
|
|
|
15192
15019
|
|
|
15193
15020
|
extend(THREE);
|
|
15194
15021
|
|
|
15195
|
-
export { Block, Canvas, ErrorBoundary, IsObject, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS,
|
|
15022
|
+
export { Block, Canvas, Environment, EnvironmentCube, EnvironmentMap, EnvironmentPortal, ErrorBoundary, FROM_REF, IsObject, ONCE, Portal, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS, Texture, _roots, act, addAfterEffect, addEffect, addTail, advance, applyProps, attach, buildGraph, calculateDpr, context, createEvents, createPointerEvents, createPortal, createRoot, createStore, detach, diffProps, dispose, createPointerEvents as events, extend, findInitialRoot, flushSync, fromRef, getInstanceProps, getPrimary, getPrimaryIds, getRootState, getUuidPrefix, hasConstructor, hasPrimary, invalidate, invalidateInstance, is, isColorRepresentation, isCopyable, isFromRef, isObject3D, isOnce, isOrthographicCamera, isRef, isRenderer, isTexture, isVectorLike, once, prepare, presetsObj, reconciler, registerPrimary, removeInteractivity, resolve, unmountComponentAtNode, unregisterPrimary, updateCamera, updateFrustum, useBridge, useEnvironment, useFrame, useGraph, useInstanceHandle, useIsomorphicLayoutEffect, useLoader, useMutableCallback, useRenderTarget, useStore, useTexture, useTextures, useThree, waitForPrimary };
|