@react-three/fiber 10.0.0-alpha.1 → 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 +1364 -1446
- package/dist/index.d.cts +1888 -1702
- package/dist/index.d.mts +1888 -1702
- package/dist/index.d.ts +1888 -1702
- package/dist/index.mjs +1324 -1425
- package/dist/legacy.cjs +1321 -1428
- package/dist/legacy.d.cts +1889 -1703
- package/dist/legacy.d.mts +1889 -1703
- package/dist/legacy.d.ts +1889 -1703
- package/dist/legacy.mjs +1281 -1407
- package/dist/webgpu/index.cjs +2046 -1497
- package/dist/webgpu/index.d.cts +2163 -1725
- package/dist/webgpu/index.d.mts +2163 -1725
- package/dist/webgpu/index.d.ts +2163 -1725
- package/dist/webgpu/index.mjs +1999 -1476
- package/package.json +8 -5
- package/react-reconciler/constants.js +1 -9
- package/react-reconciler/index.js +4 -20
- package/readme.md +244 -318
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import * as webgpu from 'three/webgpu';
|
|
2
|
-
import { RenderTarget, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1,
|
|
2
|
+
import { RenderTarget, 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, CanvasTarget, Raycaster, OrthographicCamera, PerspectiveCamera, PCFShadowMap, VSMShadowMap, BasicShadowMap, ACESFilmicToneMapping, WebGPURenderer } from 'three/webgpu';
|
|
3
3
|
import { WebGLRenderTarget, WebGLRenderer } from 'three';
|
|
4
4
|
import { Inspector } from 'three/addons/inspector/Inspector.js';
|
|
5
|
-
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
5
|
+
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
6
6
|
import * as React from 'react';
|
|
7
|
-
import React__default, {
|
|
7
|
+
import React__default, { useLayoutEffect, useRef, useMemo, useEffect, useContext, useImperativeHandle, useCallback, useState } from 'react';
|
|
8
8
|
import useMeasure from 'react-use-measure';
|
|
9
9
|
import { useFiber, useContextBridge, traverseFiber, FiberProvider } from 'its-fine';
|
|
10
|
+
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';
|
|
11
|
+
import { GroundedSkybox } from 'three/examples/jsm/objects/GroundedSkybox.js';
|
|
12
|
+
import { HDRLoader } from 'three/examples/jsm/loaders/HDRLoader.js';
|
|
13
|
+
import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js';
|
|
14
|
+
import { UltraHDRLoader } from 'three/examples/jsm/loaders/UltraHDRLoader.js';
|
|
15
|
+
import { GainMapLoader } from '@monogrid/gainmap-js';
|
|
10
16
|
import Tb, { unstable_scheduleCallback, unstable_IdlePriority } from 'scheduler';
|
|
11
17
|
import { createWithEqualityFn } from 'zustand/traditional';
|
|
18
|
+
import { getScheduler } from '@pmndrs/scheduler';
|
|
19
|
+
export { Scheduler, getScheduler } from '@pmndrs/scheduler';
|
|
12
20
|
import { suspend, preload, clear } from 'suspend-react';
|
|
13
21
|
|
|
14
22
|
function _mergeNamespaces(n, m) {
|
|
@@ -36,9 +44,392 @@ const THREE = /*#__PURE__*/_mergeNamespaces({
|
|
|
36
44
|
WebGLRenderer: WebGLRenderer
|
|
37
45
|
}, [webgpu]);
|
|
38
46
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
const primaryRegistry = /* @__PURE__ */ new Map();
|
|
48
|
+
const pendingSubscribers = /* @__PURE__ */ new Map();
|
|
49
|
+
function registerPrimary(id, renderer, store) {
|
|
50
|
+
if (primaryRegistry.has(id)) {
|
|
51
|
+
console.warn(`Canvas with id="${id}" already registered. Overwriting.`);
|
|
52
|
+
}
|
|
53
|
+
const entry = { renderer, store };
|
|
54
|
+
primaryRegistry.set(id, entry);
|
|
55
|
+
const subscribers = pendingSubscribers.get(id);
|
|
56
|
+
if (subscribers) {
|
|
57
|
+
subscribers.forEach((callback) => callback(entry));
|
|
58
|
+
pendingSubscribers.delete(id);
|
|
59
|
+
}
|
|
60
|
+
return () => {
|
|
61
|
+
const currentEntry = primaryRegistry.get(id);
|
|
62
|
+
if (currentEntry?.renderer === renderer) {
|
|
63
|
+
primaryRegistry.delete(id);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function getPrimary(id) {
|
|
68
|
+
return primaryRegistry.get(id);
|
|
69
|
+
}
|
|
70
|
+
function waitForPrimary(id, timeout = 5e3) {
|
|
71
|
+
const existing = primaryRegistry.get(id);
|
|
72
|
+
if (existing) {
|
|
73
|
+
return Promise.resolve(existing);
|
|
74
|
+
}
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
const timeoutId = setTimeout(() => {
|
|
77
|
+
const subscribers = pendingSubscribers.get(id);
|
|
78
|
+
if (subscribers) {
|
|
79
|
+
const index = subscribers.indexOf(callback);
|
|
80
|
+
if (index !== -1) subscribers.splice(index, 1);
|
|
81
|
+
if (subscribers.length === 0) pendingSubscribers.delete(id);
|
|
82
|
+
}
|
|
83
|
+
reject(new Error(`Timeout waiting for canvas with id="${id}". Make sure a <Canvas id="${id}"> is mounted.`));
|
|
84
|
+
}, timeout);
|
|
85
|
+
const callback = (entry) => {
|
|
86
|
+
clearTimeout(timeoutId);
|
|
87
|
+
resolve(entry);
|
|
88
|
+
};
|
|
89
|
+
if (!pendingSubscribers.has(id)) {
|
|
90
|
+
pendingSubscribers.set(id, []);
|
|
91
|
+
}
|
|
92
|
+
pendingSubscribers.get(id).push(callback);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function hasPrimary(id) {
|
|
96
|
+
return primaryRegistry.has(id);
|
|
97
|
+
}
|
|
98
|
+
function unregisterPrimary(id) {
|
|
99
|
+
primaryRegistry.delete(id);
|
|
100
|
+
}
|
|
101
|
+
function getPrimaryIds() {
|
|
102
|
+
return Array.from(primaryRegistry.keys());
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const presetsObj = {
|
|
106
|
+
apartment: "lebombo_1k.hdr",
|
|
107
|
+
city: "potsdamer_platz_1k.hdr",
|
|
108
|
+
dawn: "kiara_1_dawn_1k.hdr",
|
|
109
|
+
forest: "forest_slope_1k.hdr",
|
|
110
|
+
lobby: "st_fagans_interior_1k.hdr",
|
|
111
|
+
night: "dikhololo_night_1k.hdr",
|
|
112
|
+
park: "rooitou_park_1k.hdr",
|
|
113
|
+
studio: "studio_small_03_1k.hdr",
|
|
114
|
+
sunset: "venice_sunset_1k.hdr",
|
|
115
|
+
warehouse: "empty_warehouse_01_1k.hdr"
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const CUBEMAP_ROOT = "https://raw.githack.com/pmndrs/drei-assets/456060a26bbeb8fdf79326f224b6d99b8bcce736/hdri/";
|
|
119
|
+
const isArray = (arr) => Array.isArray(arr);
|
|
120
|
+
const defaultFiles = ["/px.png", "/nx.png", "/py.png", "/ny.png", "/pz.png", "/nz.png"];
|
|
121
|
+
function useEnvironment({
|
|
122
|
+
files = defaultFiles,
|
|
123
|
+
path = "",
|
|
124
|
+
preset = void 0,
|
|
125
|
+
colorSpace = void 0,
|
|
126
|
+
extensions
|
|
127
|
+
} = {}) {
|
|
128
|
+
if (preset) {
|
|
129
|
+
validatePreset(preset);
|
|
130
|
+
files = presetsObj[preset];
|
|
131
|
+
path = CUBEMAP_ROOT;
|
|
132
|
+
}
|
|
133
|
+
const multiFile = isArray(files);
|
|
134
|
+
const { extension, isCubemap } = getExtension(files);
|
|
135
|
+
const loader = getLoader$1(extension);
|
|
136
|
+
if (!loader) throw new Error("useEnvironment: Unrecognized file extension: " + files);
|
|
137
|
+
const renderer = useThree$1((state) => state.renderer);
|
|
138
|
+
useLayoutEffect(() => {
|
|
139
|
+
if (extension !== "webp" && extension !== "jpg" && extension !== "jpeg") return;
|
|
140
|
+
function clearGainmapTexture() {
|
|
141
|
+
useLoader$1.clear(loader, multiFile ? [files] : files);
|
|
142
|
+
}
|
|
143
|
+
renderer.domElement.addEventListener("webglcontextlost", clearGainmapTexture, { once: true });
|
|
144
|
+
}, [extension, files, loader, multiFile, renderer.domElement]);
|
|
145
|
+
const loaderResult = useLoader$1(
|
|
146
|
+
loader,
|
|
147
|
+
multiFile ? [files] : files,
|
|
148
|
+
(loader2) => {
|
|
149
|
+
if (extension === "webp" || extension === "jpg" || extension === "jpeg") {
|
|
150
|
+
loader2.setRenderer?.(renderer);
|
|
151
|
+
}
|
|
152
|
+
loader2.setPath?.(path);
|
|
153
|
+
if (extensions) extensions(loader2);
|
|
154
|
+
}
|
|
155
|
+
);
|
|
156
|
+
let texture = multiFile ? (
|
|
157
|
+
// @ts-ignore
|
|
158
|
+
loaderResult[0]
|
|
159
|
+
) : loaderResult;
|
|
160
|
+
if (extension === "jpg" || extension === "jpeg" || extension === "webp") {
|
|
161
|
+
texture = texture.renderTarget?.texture;
|
|
162
|
+
}
|
|
163
|
+
texture.mapping = isCubemap ? CubeReflectionMapping : EquirectangularReflectionMapping;
|
|
164
|
+
texture.colorSpace = colorSpace ?? (isCubemap ? "srgb" : "srgb-linear");
|
|
165
|
+
return texture;
|
|
166
|
+
}
|
|
167
|
+
const preloadDefaultOptions = {
|
|
168
|
+
files: defaultFiles,
|
|
169
|
+
path: "",
|
|
170
|
+
preset: void 0,
|
|
171
|
+
extensions: void 0
|
|
172
|
+
};
|
|
173
|
+
useEnvironment.preload = (preloadOptions) => {
|
|
174
|
+
const options = { ...preloadDefaultOptions, ...preloadOptions };
|
|
175
|
+
let { files, path = "" } = options;
|
|
176
|
+
const { preset, extensions } = options;
|
|
177
|
+
if (preset) {
|
|
178
|
+
validatePreset(preset);
|
|
179
|
+
files = presetsObj[preset];
|
|
180
|
+
path = CUBEMAP_ROOT;
|
|
181
|
+
}
|
|
182
|
+
const { extension } = getExtension(files);
|
|
183
|
+
if (extension === "webp" || extension === "jpg" || extension === "jpeg") {
|
|
184
|
+
throw new Error("useEnvironment: Preloading gainmaps is not supported");
|
|
185
|
+
}
|
|
186
|
+
const loader = getLoader$1(extension);
|
|
187
|
+
if (!loader) throw new Error("useEnvironment: Unrecognized file extension: " + files);
|
|
188
|
+
useLoader$1.preload(loader, isArray(files) ? [files] : files, (loader2) => {
|
|
189
|
+
loader2.setPath?.(path);
|
|
190
|
+
if (extensions) extensions(loader2);
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
const clearDefaultOptins = {
|
|
194
|
+
files: defaultFiles,
|
|
195
|
+
preset: void 0
|
|
196
|
+
};
|
|
197
|
+
useEnvironment.clear = (clearOptions) => {
|
|
198
|
+
const options = { ...clearDefaultOptins, ...clearOptions };
|
|
199
|
+
let { files } = options;
|
|
200
|
+
const { preset } = options;
|
|
201
|
+
if (preset) {
|
|
202
|
+
validatePreset(preset);
|
|
203
|
+
files = presetsObj[preset];
|
|
204
|
+
}
|
|
205
|
+
const { extension } = getExtension(files);
|
|
206
|
+
const loader = getLoader$1(extension);
|
|
207
|
+
if (!loader) throw new Error("useEnvironment: Unrecognized file extension: " + files);
|
|
208
|
+
useLoader$1.clear(loader, isArray(files) ? [files] : files);
|
|
209
|
+
};
|
|
210
|
+
function validatePreset(preset) {
|
|
211
|
+
if (!(preset in presetsObj)) throw new Error("Preset must be one of: " + Object.keys(presetsObj).join(", "));
|
|
212
|
+
}
|
|
213
|
+
function getExtension(files) {
|
|
214
|
+
const isCubemap = isArray(files) && files.length === 6;
|
|
215
|
+
const isGainmap = isArray(files) && files.length === 3 && files.some((file) => file.endsWith("json"));
|
|
216
|
+
const firstEntry = isArray(files) ? files[0] : files;
|
|
217
|
+
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();
|
|
218
|
+
return { extension, isCubemap, isGainmap };
|
|
219
|
+
}
|
|
220
|
+
function getLoader$1(extension) {
|
|
221
|
+
const loader = extension === "cube" ? CubeTextureLoader : extension === "hdr" ? HDRLoader : extension === "exr" ? EXRLoader : extension === "jpg" || extension === "jpeg" ? UltraHDRLoader : extension === "webp" ? GainMapLoader : null;
|
|
222
|
+
return loader;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const isRef$1 = (obj) => obj.current && obj.current.isScene;
|
|
226
|
+
const resolveScene = (scene) => isRef$1(scene) ? scene.current : scene;
|
|
227
|
+
function setEnvProps(background, scene, defaultScene, texture, sceneProps = {}) {
|
|
228
|
+
sceneProps = {
|
|
229
|
+
backgroundBlurriness: 0,
|
|
230
|
+
backgroundIntensity: 1,
|
|
231
|
+
backgroundRotation: [0, 0, 0],
|
|
232
|
+
environmentIntensity: 1,
|
|
233
|
+
environmentRotation: [0, 0, 0],
|
|
234
|
+
...sceneProps
|
|
235
|
+
};
|
|
236
|
+
const target = resolveScene(scene || defaultScene);
|
|
237
|
+
const oldbg = target.background;
|
|
238
|
+
const oldenv = target.environment;
|
|
239
|
+
const oldSceneProps = {
|
|
240
|
+
// @ts-ignore
|
|
241
|
+
backgroundBlurriness: target.backgroundBlurriness,
|
|
242
|
+
// @ts-ignore
|
|
243
|
+
backgroundIntensity: target.backgroundIntensity,
|
|
244
|
+
// @ts-ignore
|
|
245
|
+
backgroundRotation: target.backgroundRotation?.clone?.() ?? [0, 0, 0],
|
|
246
|
+
// @ts-ignore
|
|
247
|
+
environmentIntensity: target.environmentIntensity,
|
|
248
|
+
// @ts-ignore
|
|
249
|
+
environmentRotation: target.environmentRotation?.clone?.() ?? [0, 0, 0]
|
|
250
|
+
};
|
|
251
|
+
if (background !== "only") target.environment = texture;
|
|
252
|
+
if (background) target.background = texture;
|
|
253
|
+
applyProps$1(target, sceneProps);
|
|
254
|
+
return () => {
|
|
255
|
+
if (background !== "only") target.environment = oldenv;
|
|
256
|
+
if (background) target.background = oldbg;
|
|
257
|
+
applyProps$1(target, oldSceneProps);
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function EnvironmentMap({ scene, background = false, map, ...config }) {
|
|
261
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
262
|
+
React.useLayoutEffect(() => {
|
|
263
|
+
if (map) return setEnvProps(background, scene, defaultScene, map, config);
|
|
264
|
+
});
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
function EnvironmentCube({
|
|
268
|
+
background = false,
|
|
269
|
+
scene,
|
|
270
|
+
blur,
|
|
271
|
+
backgroundBlurriness,
|
|
272
|
+
backgroundIntensity,
|
|
273
|
+
backgroundRotation,
|
|
274
|
+
environmentIntensity,
|
|
275
|
+
environmentRotation,
|
|
276
|
+
...rest
|
|
277
|
+
}) {
|
|
278
|
+
const texture = useEnvironment(rest);
|
|
279
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
280
|
+
React.useLayoutEffect(() => {
|
|
281
|
+
return setEnvProps(background, scene, defaultScene, texture, {
|
|
282
|
+
backgroundBlurriness: blur ?? backgroundBlurriness,
|
|
283
|
+
backgroundIntensity,
|
|
284
|
+
backgroundRotation,
|
|
285
|
+
environmentIntensity,
|
|
286
|
+
environmentRotation
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
React.useEffect(() => {
|
|
290
|
+
return () => {
|
|
291
|
+
texture.dispose();
|
|
292
|
+
};
|
|
293
|
+
}, [texture]);
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
function EnvironmentPortal({
|
|
297
|
+
children,
|
|
298
|
+
near = 0.1,
|
|
299
|
+
far = 1e3,
|
|
300
|
+
resolution = 256,
|
|
301
|
+
frames = 1,
|
|
302
|
+
map,
|
|
303
|
+
background = false,
|
|
304
|
+
blur,
|
|
305
|
+
backgroundBlurriness,
|
|
306
|
+
backgroundIntensity,
|
|
307
|
+
backgroundRotation,
|
|
308
|
+
environmentIntensity,
|
|
309
|
+
environmentRotation,
|
|
310
|
+
scene,
|
|
311
|
+
files,
|
|
312
|
+
path,
|
|
313
|
+
preset = void 0,
|
|
314
|
+
extensions
|
|
315
|
+
}) {
|
|
316
|
+
const gl = useThree$1((state) => state.gl);
|
|
317
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
318
|
+
const camera = React.useRef(null);
|
|
319
|
+
const [virtualScene] = React.useState(() => new Scene());
|
|
320
|
+
const fbo = React.useMemo(() => {
|
|
321
|
+
const fbo2 = new WebGLCubeRenderTarget(resolution);
|
|
322
|
+
fbo2.texture.type = HalfFloatType;
|
|
323
|
+
return fbo2;
|
|
324
|
+
}, [resolution]);
|
|
325
|
+
React.useEffect(() => {
|
|
326
|
+
return () => {
|
|
327
|
+
fbo.dispose();
|
|
328
|
+
};
|
|
329
|
+
}, [fbo]);
|
|
330
|
+
React.useLayoutEffect(() => {
|
|
331
|
+
if (frames === 1) {
|
|
332
|
+
const autoClear = gl.autoClear;
|
|
333
|
+
gl.autoClear = true;
|
|
334
|
+
camera.current.update(gl, virtualScene);
|
|
335
|
+
gl.autoClear = autoClear;
|
|
336
|
+
}
|
|
337
|
+
return setEnvProps(background, scene, defaultScene, fbo.texture, {
|
|
338
|
+
backgroundBlurriness: blur ?? backgroundBlurriness,
|
|
339
|
+
backgroundIntensity,
|
|
340
|
+
backgroundRotation,
|
|
341
|
+
environmentIntensity,
|
|
342
|
+
environmentRotation
|
|
343
|
+
});
|
|
344
|
+
}, [
|
|
345
|
+
children,
|
|
346
|
+
virtualScene,
|
|
347
|
+
fbo.texture,
|
|
348
|
+
scene,
|
|
349
|
+
defaultScene,
|
|
350
|
+
background,
|
|
351
|
+
frames,
|
|
352
|
+
gl,
|
|
353
|
+
blur,
|
|
354
|
+
backgroundBlurriness,
|
|
355
|
+
backgroundIntensity,
|
|
356
|
+
backgroundRotation,
|
|
357
|
+
environmentIntensity,
|
|
358
|
+
environmentRotation
|
|
359
|
+
]);
|
|
360
|
+
let count = 1;
|
|
361
|
+
useFrame$1(() => {
|
|
362
|
+
if (frames === Infinity || count < frames) {
|
|
363
|
+
const autoClear = gl.autoClear;
|
|
364
|
+
gl.autoClear = true;
|
|
365
|
+
camera.current.update(gl, virtualScene);
|
|
366
|
+
gl.autoClear = autoClear;
|
|
367
|
+
count++;
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
return /* @__PURE__ */ jsx(Fragment, { children: createPortal$1(
|
|
371
|
+
/* @__PURE__ */ jsxs(Fragment, { children: [
|
|
372
|
+
children,
|
|
373
|
+
/* @__PURE__ */ jsx("cubeCamera", { ref: camera, args: [near, far, fbo] }),
|
|
374
|
+
files || preset ? /* @__PURE__ */ jsx(EnvironmentCube, { background: true, files, preset, path, extensions }) : map ? /* @__PURE__ */ jsx(EnvironmentMap, { background: true, map, extensions }) : null
|
|
375
|
+
] }),
|
|
376
|
+
virtualScene
|
|
377
|
+
) });
|
|
378
|
+
}
|
|
379
|
+
function EnvironmentGround(props) {
|
|
380
|
+
const textureDefault = useEnvironment(props);
|
|
381
|
+
const texture = props.map || textureDefault;
|
|
382
|
+
React.useMemo(() => extend$1({ GroundProjectedEnvImpl: GroundedSkybox }), []);
|
|
383
|
+
React.useEffect(() => {
|
|
384
|
+
return () => {
|
|
385
|
+
textureDefault.dispose();
|
|
386
|
+
};
|
|
387
|
+
}, [textureDefault]);
|
|
388
|
+
const height = props.ground?.height ?? 15;
|
|
389
|
+
const radius = props.ground?.radius ?? 60;
|
|
390
|
+
const scale = props.ground?.scale ?? 1e3;
|
|
391
|
+
const args = React.useMemo(
|
|
392
|
+
() => [texture, height, radius],
|
|
393
|
+
[texture, height, radius]
|
|
394
|
+
);
|
|
395
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
396
|
+
/* @__PURE__ */ jsx(EnvironmentMap, { ...props, map: texture }),
|
|
397
|
+
/* @__PURE__ */ jsx("groundProjectedEnvImpl", { args, scale })
|
|
398
|
+
] });
|
|
399
|
+
}
|
|
400
|
+
function EnvironmentColor({ color, scene }) {
|
|
401
|
+
const defaultScene = useThree$1((state) => state.scene);
|
|
402
|
+
React.useLayoutEffect(() => {
|
|
403
|
+
if (color === void 0) return;
|
|
404
|
+
const target = resolveScene(scene || defaultScene);
|
|
405
|
+
const oldBg = target.background;
|
|
406
|
+
target.background = new Color(color);
|
|
407
|
+
return () => {
|
|
408
|
+
target.background = oldBg;
|
|
409
|
+
};
|
|
410
|
+
});
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
function EnvironmentDualSource(props) {
|
|
414
|
+
const { backgroundFiles, ...envProps } = props;
|
|
415
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
416
|
+
/* @__PURE__ */ jsx(EnvironmentCube, { ...envProps, background: false }),
|
|
417
|
+
/* @__PURE__ */ jsx(EnvironmentCube, { ...props, files: backgroundFiles, background: "only" })
|
|
418
|
+
] });
|
|
419
|
+
}
|
|
420
|
+
function Environment(props) {
|
|
421
|
+
if (props.color && !props.files && !props.preset && !props.map) {
|
|
422
|
+
return /* @__PURE__ */ jsx(EnvironmentColor, { ...props });
|
|
423
|
+
}
|
|
424
|
+
if (props.backgroundFiles && props.backgroundFiles !== props.files) {
|
|
425
|
+
return /* @__PURE__ */ jsx(EnvironmentDualSource, { ...props });
|
|
426
|
+
}
|
|
427
|
+
return props.ground ? /* @__PURE__ */ jsx(EnvironmentGround, { ...props }) : props.map ? /* @__PURE__ */ jsx(EnvironmentMap, { ...props }) : props.children ? /* @__PURE__ */ jsx(EnvironmentPortal, { ...props }) : /* @__PURE__ */ jsx(EnvironmentCube, { ...props });
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
var __defProp = Object.defineProperty;
|
|
431
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
432
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
42
433
|
const act = React["act"];
|
|
43
434
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React.useLayoutEffect : React.useEffect;
|
|
44
435
|
function useMutableCallback(fn) {
|
|
@@ -70,7 +461,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
70
461
|
return _a = class extends React.Component {
|
|
71
462
|
constructor() {
|
|
72
463
|
super(...arguments);
|
|
73
|
-
__publicField
|
|
464
|
+
__publicField(this, "state", { error: false });
|
|
74
465
|
}
|
|
75
466
|
componentDidCatch(err) {
|
|
76
467
|
this.props.set(err);
|
|
@@ -78,7 +469,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
78
469
|
render() {
|
|
79
470
|
return this.state.error ? null : this.props.children;
|
|
80
471
|
}
|
|
81
|
-
}, __publicField
|
|
472
|
+
}, __publicField(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
82
473
|
})();
|
|
83
474
|
|
|
84
475
|
const is = {
|
|
@@ -264,7 +655,7 @@ function createOcclusionObserverNode(store, uniform) {
|
|
|
264
655
|
let occlusionSetupPromise = null;
|
|
265
656
|
function enableOcclusion(store) {
|
|
266
657
|
const state = store.getState();
|
|
267
|
-
const { internal, renderer
|
|
658
|
+
const { internal, renderer } = state;
|
|
268
659
|
if (internal.occlusionEnabled || occlusionSetupPromise) return;
|
|
269
660
|
const hasOcclusionSupport = typeof renderer?.isOccluded === "function";
|
|
270
661
|
if (!hasOcclusionSupport) {
|
|
@@ -372,6 +763,17 @@ function unregisterVisibility(store, object) {
|
|
|
372
763
|
internal.visibilityRegistry.delete(object.uuid);
|
|
373
764
|
internal.occlusionCache.delete(object);
|
|
374
765
|
}
|
|
766
|
+
function updateVisibilityHandlers(store, object, handlers) {
|
|
767
|
+
const { internal } = store.getState();
|
|
768
|
+
const entry = internal.visibilityRegistry.get(object.uuid);
|
|
769
|
+
if (!entry) return false;
|
|
770
|
+
entry.handlers = handlers;
|
|
771
|
+
if ((handlers.onOccluded || handlers.onVisible) && !internal.occlusionEnabled) {
|
|
772
|
+
object.occlusionTest = true;
|
|
773
|
+
enableOcclusion(store);
|
|
774
|
+
}
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
375
777
|
function checkVisibility(state) {
|
|
376
778
|
const { internal, camera } = state;
|
|
377
779
|
const registry = internal.visibilityRegistry;
|
|
@@ -427,6 +829,22 @@ function hasVisibilityHandlers(handlers) {
|
|
|
427
829
|
return !!(handlers.onFramed || handlers.onOccluded || handlers.onVisible);
|
|
428
830
|
}
|
|
429
831
|
|
|
832
|
+
const FROM_REF = Symbol.for("@react-three/fiber.fromRef");
|
|
833
|
+
function fromRef(ref) {
|
|
834
|
+
return { [FROM_REF]: ref };
|
|
835
|
+
}
|
|
836
|
+
function isFromRef(value) {
|
|
837
|
+
return value !== null && typeof value === "object" && FROM_REF in value;
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
const ONCE = Symbol.for("@react-three/fiber.once");
|
|
841
|
+
function once(...args) {
|
|
842
|
+
return { [ONCE]: args.length ? args : true };
|
|
843
|
+
}
|
|
844
|
+
function isOnce(value) {
|
|
845
|
+
return value !== null && typeof value === "object" && ONCE in value;
|
|
846
|
+
}
|
|
847
|
+
|
|
430
848
|
const RESERVED_PROPS = [
|
|
431
849
|
"children",
|
|
432
850
|
"key",
|
|
@@ -497,7 +915,7 @@ function getMemoizedPrototype(root) {
|
|
|
497
915
|
ctor = new root.constructor();
|
|
498
916
|
MEMOIZED_PROTOTYPES.set(root.constructor, ctor);
|
|
499
917
|
}
|
|
500
|
-
} catch
|
|
918
|
+
} catch {
|
|
501
919
|
}
|
|
502
920
|
return ctor;
|
|
503
921
|
}
|
|
@@ -528,7 +946,7 @@ function applyProps(object, props) {
|
|
|
528
946
|
const rootState = instance && findInitialRoot(instance).getState();
|
|
529
947
|
const prevHandlers = instance?.eventCount;
|
|
530
948
|
for (const prop in props) {
|
|
531
|
-
|
|
949
|
+
const value = props[prop];
|
|
532
950
|
if (RESERVED_PROPS.includes(prop)) continue;
|
|
533
951
|
if (instance && EVENT_REGEX.test(prop)) {
|
|
534
952
|
if (typeof value === "function") instance.handlers[prop] = value;
|
|
@@ -543,6 +961,22 @@ function applyProps(object, props) {
|
|
|
543
961
|
continue;
|
|
544
962
|
}
|
|
545
963
|
if (value === void 0) continue;
|
|
964
|
+
if (isFromRef(value)) continue;
|
|
965
|
+
if (isOnce(value)) {
|
|
966
|
+
if (instance?.appliedOnce?.has(prop)) continue;
|
|
967
|
+
if (instance) {
|
|
968
|
+
instance.appliedOnce ?? (instance.appliedOnce = /* @__PURE__ */ new Set());
|
|
969
|
+
instance.appliedOnce.add(prop);
|
|
970
|
+
}
|
|
971
|
+
const { root: targetRoot, key: targetKey } = resolve(object, prop);
|
|
972
|
+
const args = value[ONCE];
|
|
973
|
+
if (typeof targetRoot[targetKey] === "function") {
|
|
974
|
+
targetRoot[targetKey](...args === true ? [] : args);
|
|
975
|
+
} else if (args !== true && args.length > 0) {
|
|
976
|
+
targetRoot[targetKey] = args[0];
|
|
977
|
+
}
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
546
980
|
let { root, key, target } = resolve(object, prop);
|
|
547
981
|
if (target === void 0 && (typeof root !== "object" || root === null)) {
|
|
548
982
|
throw Error(`R3F: Cannot set "${prop}". Ensure it is an object before setting "${key}".`);
|
|
@@ -563,9 +997,25 @@ function applyProps(object, props) {
|
|
|
563
997
|
} else if (isVectorLike(target) && is.num(value)) {
|
|
564
998
|
if ("setScalar" in target && typeof target.setScalar === "function") target.setScalar(value);
|
|
565
999
|
else target.set(value);
|
|
1000
|
+
} else if (root instanceof ShaderMaterial && key === "uniforms" && is.obj(value)) {
|
|
1001
|
+
if (!is.obj(root.uniforms)) root.uniforms = {};
|
|
1002
|
+
const uniforms = root.uniforms;
|
|
1003
|
+
const nextUniforms = value;
|
|
1004
|
+
for (const name in nextUniforms) {
|
|
1005
|
+
const uniform = nextUniforms[name];
|
|
1006
|
+
const targetUniform = uniforms[name];
|
|
1007
|
+
if (targetUniform) Object.assign(targetUniform, uniform);
|
|
1008
|
+
else {
|
|
1009
|
+
const nextUniform = uniform instanceof Uniform ? uniform.clone() : new Uniform(uniform.value);
|
|
1010
|
+
uniforms[name] = nextUniform;
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
566
1013
|
} else {
|
|
567
1014
|
root[key] = value;
|
|
568
|
-
if (
|
|
1015
|
+
if (key.endsWith("Node") && root.isMaterial) {
|
|
1016
|
+
root.needsUpdate = true;
|
|
1017
|
+
}
|
|
1018
|
+
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
|
|
569
1019
|
root[key].format === RGBAFormat && root[key].type === UnsignedByteType) {
|
|
570
1020
|
root[key].colorSpace = rootState.textureColorSpace;
|
|
571
1021
|
}
|
|
@@ -578,6 +1028,9 @@ function applyProps(object, props) {
|
|
|
578
1028
|
if (instance.eventCount && object2.raycast !== null) {
|
|
579
1029
|
rootState.internal.interaction.push(object2);
|
|
580
1030
|
}
|
|
1031
|
+
}
|
|
1032
|
+
if (instance?.parent && rootState?.internal && instance.object?.isObject3D) {
|
|
1033
|
+
const object2 = instance.object;
|
|
581
1034
|
const root = findInitialRoot(instance);
|
|
582
1035
|
const visibilityHandlers = {
|
|
583
1036
|
onFramed: instance.handlers.onFramed,
|
|
@@ -585,7 +1038,9 @@ function applyProps(object, props) {
|
|
|
585
1038
|
onVisible: instance.handlers.onVisible
|
|
586
1039
|
};
|
|
587
1040
|
if (hasVisibilityHandlers(visibilityHandlers)) {
|
|
588
|
-
|
|
1041
|
+
if (!updateVisibilityHandlers(root, object2, visibilityHandlers)) {
|
|
1042
|
+
registerVisibility(root, object2, visibilityHandlers);
|
|
1043
|
+
}
|
|
589
1044
|
} else {
|
|
590
1045
|
unregisterVisibility(root, object2);
|
|
591
1046
|
}
|
|
@@ -598,38 +1053,60 @@ function applyProps(object, props) {
|
|
|
598
1053
|
return object;
|
|
599
1054
|
}
|
|
600
1055
|
|
|
1056
|
+
const DEFAULT_POINTER_ID = 0;
|
|
1057
|
+
const XR_POINTER_ID_START = 1e3;
|
|
1058
|
+
function getPointerState(internal, pointerId) {
|
|
1059
|
+
let state = internal.pointerMap.get(pointerId);
|
|
1060
|
+
if (!state) {
|
|
1061
|
+
state = {
|
|
1062
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1063
|
+
captured: /* @__PURE__ */ new Map(),
|
|
1064
|
+
initialClick: [0, 0],
|
|
1065
|
+
initialHits: []
|
|
1066
|
+
};
|
|
1067
|
+
internal.pointerMap.set(pointerId, state);
|
|
1068
|
+
}
|
|
1069
|
+
return state;
|
|
1070
|
+
}
|
|
1071
|
+
function getPointerId(event) {
|
|
1072
|
+
return "pointerId" in event ? event.pointerId : DEFAULT_POINTER_ID;
|
|
1073
|
+
}
|
|
601
1074
|
function makeId(event) {
|
|
602
1075
|
return (event.eventObject || event.object).uuid + "/" + event.index + event.instanceId;
|
|
603
1076
|
}
|
|
604
|
-
function releaseInternalPointerCapture(
|
|
605
|
-
const
|
|
1077
|
+
function releaseInternalPointerCapture(internal, obj, pointerId) {
|
|
1078
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1079
|
+
if (!pointerState) return;
|
|
1080
|
+
const captureData = pointerState.captured.get(obj);
|
|
606
1081
|
if (captureData) {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
capturedMap.delete(pointerId);
|
|
610
|
-
captureData.target.releasePointerCapture(pointerId);
|
|
611
|
-
}
|
|
1082
|
+
pointerState.captured.delete(obj);
|
|
1083
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
612
1084
|
}
|
|
613
1085
|
}
|
|
614
1086
|
function removeInteractivity(store, object) {
|
|
615
1087
|
const { internal } = store.getState();
|
|
616
1088
|
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
1089
|
+
for (const [pointerId, pointerState] of internal.pointerMap) {
|
|
1090
|
+
pointerState.initialHits = pointerState.initialHits.filter((o) => o !== object);
|
|
1091
|
+
pointerState.hovered.forEach((value, key) => {
|
|
1092
|
+
if (value.eventObject === object || value.object === object) {
|
|
1093
|
+
pointerState.hovered.delete(key);
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
if (pointerState.captured.has(object)) {
|
|
1097
|
+
releaseInternalPointerCapture(internal, object, pointerId);
|
|
621
1098
|
}
|
|
622
|
-
}
|
|
623
|
-
internal.capturedMap.forEach((captures, pointerId) => {
|
|
624
|
-
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
625
|
-
});
|
|
1099
|
+
}
|
|
626
1100
|
unregisterVisibility(store, object);
|
|
627
1101
|
}
|
|
628
1102
|
function createEvents(store) {
|
|
629
|
-
function calculateDistance(event) {
|
|
1103
|
+
function calculateDistance(event, pointerId) {
|
|
630
1104
|
const { internal } = store.getState();
|
|
631
|
-
const
|
|
632
|
-
|
|
1105
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1106
|
+
if (!pointerState) return 0;
|
|
1107
|
+
const [initialX, initialY] = pointerState.initialClick;
|
|
1108
|
+
const dx = event.offsetX - initialX;
|
|
1109
|
+
const dy = event.offsetY - initialY;
|
|
633
1110
|
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
634
1111
|
}
|
|
635
1112
|
function filterPointerEvents(objects) {
|
|
@@ -665,6 +1142,15 @@ function createEvents(store) {
|
|
|
665
1142
|
return state2.raycaster.camera ? state2.raycaster.intersectObject(obj, true) : [];
|
|
666
1143
|
}
|
|
667
1144
|
let hits = eventsObjects.flatMap(handleRaycast).sort((a, b) => {
|
|
1145
|
+
const aInteractivePriority = a.object.userData?.interactivePriority;
|
|
1146
|
+
const bInteractivePriority = b.object.userData?.interactivePriority;
|
|
1147
|
+
if (aInteractivePriority !== void 0 || bInteractivePriority !== void 0) {
|
|
1148
|
+
if (aInteractivePriority !== void 0 && bInteractivePriority === void 0) return -1;
|
|
1149
|
+
if (bInteractivePriority !== void 0 && aInteractivePriority === void 0) return 1;
|
|
1150
|
+
if (aInteractivePriority !== bInteractivePriority) {
|
|
1151
|
+
return (bInteractivePriority ?? 0) - (aInteractivePriority ?? 0);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
668
1154
|
const aState = getRootState(a.object);
|
|
669
1155
|
const bState = getRootState(b.object);
|
|
670
1156
|
const aPriority = aState?.events?.priority ?? 1;
|
|
@@ -680,14 +1166,19 @@ function createEvents(store) {
|
|
|
680
1166
|
for (const hit of hits) {
|
|
681
1167
|
let eventObject = hit.object;
|
|
682
1168
|
while (eventObject) {
|
|
683
|
-
if (eventObject.__r3f?.eventCount)
|
|
1169
|
+
if (eventObject.__r3f?.eventCount) {
|
|
684
1170
|
intersections.push({ ...hit, eventObject });
|
|
1171
|
+
}
|
|
685
1172
|
eventObject = eventObject.parent;
|
|
686
1173
|
}
|
|
687
1174
|
}
|
|
688
|
-
if ("pointerId" in event
|
|
689
|
-
|
|
690
|
-
|
|
1175
|
+
if ("pointerId" in event) {
|
|
1176
|
+
const pointerId = event.pointerId;
|
|
1177
|
+
const pointerState = state.internal.pointerMap.get(pointerId);
|
|
1178
|
+
if (pointerState?.captured.size) {
|
|
1179
|
+
for (const captureData of pointerState.captured.values()) {
|
|
1180
|
+
if (!duplicates.has(makeId(captureData.intersection))) intersections.push(captureData.intersection);
|
|
1181
|
+
}
|
|
691
1182
|
}
|
|
692
1183
|
}
|
|
693
1184
|
return intersections;
|
|
@@ -700,28 +1191,26 @@ function createEvents(store) {
|
|
|
700
1191
|
if (state) {
|
|
701
1192
|
const { raycaster, pointer, camera, internal } = state;
|
|
702
1193
|
const unprojectedPoint = new Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
703
|
-
const hasPointerCapture = (id) =>
|
|
1194
|
+
const hasPointerCapture = (id) => {
|
|
1195
|
+
const pointerState = internal.pointerMap.get(id);
|
|
1196
|
+
return pointerState?.captured.has(hit.eventObject) ?? false;
|
|
1197
|
+
};
|
|
704
1198
|
const setPointerCapture = (id) => {
|
|
705
1199
|
const captureData = { intersection: hit, target: event.target };
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
} else {
|
|
709
|
-
internal.capturedMap.set(id, /* @__PURE__ */ new Map([[hit.eventObject, captureData]]));
|
|
710
|
-
}
|
|
1200
|
+
const pointerState = getPointerState(internal, id);
|
|
1201
|
+
pointerState.captured.set(hit.eventObject, captureData);
|
|
711
1202
|
event.target.setPointerCapture(id);
|
|
712
1203
|
};
|
|
713
1204
|
const releasePointerCapture = (id) => {
|
|
714
|
-
|
|
715
|
-
if (captures) {
|
|
716
|
-
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
717
|
-
}
|
|
1205
|
+
releaseInternalPointerCapture(internal, hit.eventObject, id);
|
|
718
1206
|
};
|
|
719
|
-
|
|
720
|
-
for (
|
|
721
|
-
|
|
1207
|
+
const extractEventProps = {};
|
|
1208
|
+
for (const prop in event) {
|
|
1209
|
+
const property = event[prop];
|
|
722
1210
|
if (typeof property !== "function") extractEventProps[prop] = property;
|
|
723
1211
|
}
|
|
724
|
-
|
|
1212
|
+
const eventPointerId = "pointerId" in event ? event.pointerId : void 0;
|
|
1213
|
+
const raycastEvent = {
|
|
725
1214
|
...hit,
|
|
726
1215
|
...extractEventProps,
|
|
727
1216
|
pointer,
|
|
@@ -731,18 +1220,19 @@ function createEvents(store) {
|
|
|
731
1220
|
unprojectedPoint,
|
|
732
1221
|
ray: raycaster.ray,
|
|
733
1222
|
camera,
|
|
1223
|
+
pointerId: eventPointerId,
|
|
734
1224
|
// Hijack stopPropagation, which just sets a flag
|
|
735
1225
|
stopPropagation() {
|
|
736
|
-
const
|
|
1226
|
+
const pointerState = eventPointerId !== void 0 ? internal.pointerMap.get(eventPointerId) : void 0;
|
|
737
1227
|
if (
|
|
738
1228
|
// ...if this pointer hasn't been captured
|
|
739
|
-
!
|
|
740
|
-
|
|
1229
|
+
!pointerState?.captured.size || // ... or if the hit object is capturing the pointer
|
|
1230
|
+
pointerState.captured.has(hit.eventObject)
|
|
741
1231
|
) {
|
|
742
1232
|
raycastEvent.stopped = localState.stopped = true;
|
|
743
|
-
if (
|
|
1233
|
+
if (pointerState?.hovered.size && Array.from(pointerState.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
744
1234
|
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
745
|
-
cancelPointer([...higher, hit]);
|
|
1235
|
+
cancelPointer([...higher, hit], eventPointerId);
|
|
746
1236
|
}
|
|
747
1237
|
}
|
|
748
1238
|
},
|
|
@@ -758,15 +1248,18 @@ function createEvents(store) {
|
|
|
758
1248
|
}
|
|
759
1249
|
return intersections;
|
|
760
1250
|
}
|
|
761
|
-
function cancelPointer(intersections) {
|
|
1251
|
+
function cancelPointer(intersections, pointerId) {
|
|
762
1252
|
const { internal } = store.getState();
|
|
763
|
-
|
|
1253
|
+
const pid = pointerId ?? DEFAULT_POINTER_ID;
|
|
1254
|
+
const pointerState = internal.pointerMap.get(pid);
|
|
1255
|
+
if (!pointerState) return;
|
|
1256
|
+
for (const [hoveredId, hoveredObj] of pointerState.hovered) {
|
|
764
1257
|
if (!intersections.length || !intersections.find(
|
|
765
1258
|
(hit) => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId
|
|
766
1259
|
)) {
|
|
767
1260
|
const eventObject = hoveredObj.eventObject;
|
|
768
1261
|
const instance = eventObject.__r3f;
|
|
769
|
-
|
|
1262
|
+
pointerState.hovered.delete(hoveredId);
|
|
770
1263
|
if (instance?.eventCount) {
|
|
771
1264
|
const handlers = instance.handlers;
|
|
772
1265
|
const data = { ...hoveredObj, intersections };
|
|
@@ -795,41 +1288,118 @@ function createEvents(store) {
|
|
|
795
1288
|
instance?.handlers.onDropMissed?.(event);
|
|
796
1289
|
}
|
|
797
1290
|
}
|
|
1291
|
+
function cleanupPointer(pointerId) {
|
|
1292
|
+
const { internal } = store.getState();
|
|
1293
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1294
|
+
if (pointerState) {
|
|
1295
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1296
|
+
const eventObject = hoveredObj.eventObject;
|
|
1297
|
+
const instance = eventObject.__r3f;
|
|
1298
|
+
if (instance?.eventCount) {
|
|
1299
|
+
const handlers = instance.handlers;
|
|
1300
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1301
|
+
handlers.onPointerOut?.(data);
|
|
1302
|
+
handlers.onPointerLeave?.(data);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
internal.pointerMap.delete(pointerId);
|
|
1306
|
+
}
|
|
1307
|
+
internal.pointerDirty.delete(pointerId);
|
|
1308
|
+
}
|
|
1309
|
+
function processDeferredPointer(event, pointerId) {
|
|
1310
|
+
const state = store.getState();
|
|
1311
|
+
const { internal } = state;
|
|
1312
|
+
if (!state.events.enabled) return;
|
|
1313
|
+
const filter = filterPointerEvents;
|
|
1314
|
+
const hits = intersect(event, filter);
|
|
1315
|
+
cancelPointer(hits, pointerId);
|
|
1316
|
+
function onIntersect(data) {
|
|
1317
|
+
const eventObject = data.eventObject;
|
|
1318
|
+
const instance = eventObject.__r3f;
|
|
1319
|
+
if (!instance?.eventCount) return;
|
|
1320
|
+
const handlers = instance.handlers;
|
|
1321
|
+
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1322
|
+
const id = makeId(data);
|
|
1323
|
+
const pointerState = getPointerState(internal, pointerId);
|
|
1324
|
+
const hoveredItem = pointerState.hovered.get(id);
|
|
1325
|
+
if (!hoveredItem) {
|
|
1326
|
+
pointerState.hovered.set(id, data);
|
|
1327
|
+
handlers.onPointerOver?.(data);
|
|
1328
|
+
handlers.onPointerEnter?.(data);
|
|
1329
|
+
} else if (hoveredItem.stopped) {
|
|
1330
|
+
data.stopPropagation();
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
handlers.onPointerMove?.(data);
|
|
1334
|
+
}
|
|
1335
|
+
handleIntersects(hits, event, 0, onIntersect);
|
|
1336
|
+
}
|
|
798
1337
|
function handlePointer(name) {
|
|
799
1338
|
switch (name) {
|
|
800
1339
|
case "onPointerLeave":
|
|
801
|
-
case "onPointerCancel":
|
|
802
1340
|
case "onDragLeave":
|
|
803
1341
|
return () => cancelPointer([]);
|
|
1342
|
+
// Global cancel of these events
|
|
1343
|
+
case "onPointerCancel":
|
|
1344
|
+
return (event) => {
|
|
1345
|
+
const pointerId = getPointerId(event);
|
|
1346
|
+
cleanupPointer(pointerId);
|
|
1347
|
+
};
|
|
804
1348
|
case "onLostPointerCapture":
|
|
805
1349
|
return (event) => {
|
|
806
1350
|
const { internal } = store.getState();
|
|
807
|
-
|
|
1351
|
+
const pointerId = getPointerId(event);
|
|
1352
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1353
|
+
if (pointerState?.captured.size) {
|
|
808
1354
|
requestAnimationFrame(() => {
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
1355
|
+
const pointerState2 = internal.pointerMap.get(pointerId);
|
|
1356
|
+
if (pointerState2?.captured.size) {
|
|
1357
|
+
pointerState2.captured.clear();
|
|
812
1358
|
}
|
|
1359
|
+
cancelPointer([], pointerId);
|
|
813
1360
|
});
|
|
814
1361
|
}
|
|
815
1362
|
};
|
|
816
1363
|
}
|
|
817
1364
|
return function handleEvent(event) {
|
|
818
1365
|
const state = store.getState();
|
|
819
|
-
const { onPointerMissed, onDragOverMissed, onDropMissed, internal } = state;
|
|
1366
|
+
const { onPointerMissed, onDragOverMissed, onDropMissed, internal, events } = state;
|
|
1367
|
+
const pointerId = getPointerId(event);
|
|
820
1368
|
internal.lastEvent.current = event;
|
|
821
|
-
if (!
|
|
1369
|
+
if (!events.enabled) return;
|
|
822
1370
|
const isPointerMove = name === "onPointerMove";
|
|
823
1371
|
const isDragOver = name === "onDragOver";
|
|
824
1372
|
const isDrop = name === "onDrop";
|
|
825
1373
|
const isClickEvent = name === "onClick" || name === "onContextMenu" || name === "onDoubleClick";
|
|
1374
|
+
const isPointerDown = name === "onPointerDown";
|
|
1375
|
+
const isPointerUp = name === "onPointerUp";
|
|
1376
|
+
const isWheel = name === "onWheel";
|
|
1377
|
+
const canDeferRaycasts = events.frameTimedRaycasts && state.frameloop === "always";
|
|
1378
|
+
if (isPointerMove && canDeferRaycasts) {
|
|
1379
|
+
events.compute?.(event, state);
|
|
1380
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
if (isWheel && canDeferRaycasts && !events.alwaysFireOnScroll) {
|
|
1384
|
+
events.compute?.(event, state);
|
|
1385
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1386
|
+
return;
|
|
1387
|
+
}
|
|
1388
|
+
if ((isClickEvent || isPointerDown || isPointerUp) && internal.pointerDirty.has(pointerId)) {
|
|
1389
|
+
const deferredEvent = internal.pointerDirty.get(pointerId);
|
|
1390
|
+
internal.pointerDirty.delete(pointerId);
|
|
1391
|
+
processDeferredPointer(deferredEvent, pointerId);
|
|
1392
|
+
}
|
|
826
1393
|
const filter = isPointerMove || isDragOver || isDrop ? filterPointerEvents : void 0;
|
|
827
1394
|
const hits = intersect(event, filter);
|
|
828
|
-
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
829
|
-
if (
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
1395
|
+
const delta = isClickEvent ? calculateDistance(event, pointerId) : 0;
|
|
1396
|
+
if (isPointerDown) {
|
|
1397
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1398
|
+
pointerState2.initialClick = [event.offsetX, event.offsetY];
|
|
1399
|
+
pointerState2.initialHits = hits.map((hit) => hit.eventObject);
|
|
1400
|
+
}
|
|
1401
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1402
|
+
const initialHits = pointerState?.initialHits ?? [];
|
|
833
1403
|
if (isClickEvent && !hits.length) {
|
|
834
1404
|
if (delta <= 2) {
|
|
835
1405
|
pointerMissed(event, internal.interaction);
|
|
@@ -844,7 +1414,9 @@ function createEvents(store) {
|
|
|
844
1414
|
dropMissed(event, internal.interaction);
|
|
845
1415
|
if (onDropMissed) onDropMissed(event);
|
|
846
1416
|
}
|
|
847
|
-
if (isPointerMove || isDragOver)
|
|
1417
|
+
if (isPointerMove || isDragOver) {
|
|
1418
|
+
cancelPointer(hits, pointerId);
|
|
1419
|
+
}
|
|
848
1420
|
function onIntersect(data) {
|
|
849
1421
|
const eventObject = data.eventObject;
|
|
850
1422
|
const instance = eventObject.__r3f;
|
|
@@ -853,9 +1425,10 @@ function createEvents(store) {
|
|
|
853
1425
|
if (isPointerMove) {
|
|
854
1426
|
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
855
1427
|
const id = makeId(data);
|
|
856
|
-
const
|
|
1428
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1429
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
857
1430
|
if (!hoveredItem) {
|
|
858
|
-
|
|
1431
|
+
pointerState2.hovered.set(id, data);
|
|
859
1432
|
handlers.onPointerOver?.(data);
|
|
860
1433
|
handlers.onPointerEnter?.(data);
|
|
861
1434
|
} else if (hoveredItem.stopped) {
|
|
@@ -865,9 +1438,10 @@ function createEvents(store) {
|
|
|
865
1438
|
handlers.onPointerMove?.(data);
|
|
866
1439
|
} else if (isDragOver) {
|
|
867
1440
|
const id = makeId(data);
|
|
868
|
-
const
|
|
1441
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1442
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
869
1443
|
if (!hoveredItem) {
|
|
870
|
-
|
|
1444
|
+
pointerState2.hovered.set(id, data);
|
|
871
1445
|
handlers.onDragOverEnter?.(data);
|
|
872
1446
|
} else if (hoveredItem.stopped) {
|
|
873
1447
|
data.stopPropagation();
|
|
@@ -878,18 +1452,18 @@ function createEvents(store) {
|
|
|
878
1452
|
} else {
|
|
879
1453
|
const handler = handlers[name];
|
|
880
1454
|
if (handler) {
|
|
881
|
-
if (!isClickEvent ||
|
|
1455
|
+
if (!isClickEvent || initialHits.includes(eventObject)) {
|
|
882
1456
|
pointerMissed(
|
|
883
1457
|
event,
|
|
884
|
-
internal.interaction.filter((object) => !
|
|
1458
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
885
1459
|
);
|
|
886
1460
|
handler(data);
|
|
887
1461
|
}
|
|
888
1462
|
} else {
|
|
889
|
-
if (isClickEvent &&
|
|
1463
|
+
if (isClickEvent && initialHits.includes(eventObject)) {
|
|
890
1464
|
pointerMissed(
|
|
891
1465
|
event,
|
|
892
|
-
internal.interaction.filter((object) => !
|
|
1466
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
893
1467
|
);
|
|
894
1468
|
}
|
|
895
1469
|
}
|
|
@@ -898,7 +1472,15 @@ function createEvents(store) {
|
|
|
898
1472
|
handleIntersects(hits, event, delta, onIntersect);
|
|
899
1473
|
};
|
|
900
1474
|
}
|
|
901
|
-
|
|
1475
|
+
function flushDeferredPointers() {
|
|
1476
|
+
const { internal, events } = store.getState();
|
|
1477
|
+
if (!events.frameTimedRaycasts) return;
|
|
1478
|
+
for (const [pointerId, event] of internal.pointerDirty) {
|
|
1479
|
+
processDeferredPointer(event, pointerId);
|
|
1480
|
+
}
|
|
1481
|
+
internal.pointerDirty.clear();
|
|
1482
|
+
}
|
|
1483
|
+
return { handlePointer, flushDeferredPointers, processDeferredPointer };
|
|
902
1484
|
}
|
|
903
1485
|
const DOM_EVENTS = {
|
|
904
1486
|
onClick: ["click", false],
|
|
@@ -917,11 +1499,16 @@ const DOM_EVENTS = {
|
|
|
917
1499
|
onLostPointerCapture: ["lostpointercapture", true]
|
|
918
1500
|
};
|
|
919
1501
|
function createPointerEvents(store) {
|
|
920
|
-
const { handlePointer } = createEvents(store);
|
|
1502
|
+
const { handlePointer, flushDeferredPointers, processDeferredPointer } = createEvents(store);
|
|
1503
|
+
let nextXRPointerId = XR_POINTER_ID_START;
|
|
1504
|
+
const xrPointers = /* @__PURE__ */ new Map();
|
|
921
1505
|
return {
|
|
922
1506
|
priority: 1,
|
|
923
1507
|
enabled: true,
|
|
924
|
-
|
|
1508
|
+
frameTimedRaycasts: true,
|
|
1509
|
+
alwaysFireOnScroll: true,
|
|
1510
|
+
updateOnFrame: false,
|
|
1511
|
+
compute(event, state) {
|
|
925
1512
|
state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
926
1513
|
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
927
1514
|
},
|
|
@@ -930,11 +1517,33 @@ function createPointerEvents(store) {
|
|
|
930
1517
|
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
931
1518
|
{}
|
|
932
1519
|
),
|
|
933
|
-
update: () => {
|
|
1520
|
+
update: (pointerId) => {
|
|
934
1521
|
const { events, internal } = store.getState();
|
|
935
|
-
if (
|
|
1522
|
+
if (!events.handlers) return;
|
|
1523
|
+
if (pointerId !== void 0) {
|
|
1524
|
+
const event = internal.pointerDirty.get(pointerId);
|
|
1525
|
+
if (event) {
|
|
1526
|
+
internal.pointerDirty.delete(pointerId);
|
|
1527
|
+
processDeferredPointer(event, pointerId);
|
|
1528
|
+
} else if (internal.lastEvent?.current) {
|
|
1529
|
+
processDeferredPointer(internal.lastEvent.current, pointerId);
|
|
1530
|
+
}
|
|
1531
|
+
} else {
|
|
1532
|
+
flushDeferredPointers();
|
|
1533
|
+
if (internal.lastEvent?.current) {
|
|
1534
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
},
|
|
1538
|
+
flush: () => {
|
|
1539
|
+
const { events, internal } = store.getState();
|
|
1540
|
+
flushDeferredPointers();
|
|
1541
|
+
if (events.updateOnFrame && internal.lastEvent?.current && events.handlers) {
|
|
1542
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1543
|
+
}
|
|
936
1544
|
},
|
|
937
1545
|
connect: (target) => {
|
|
1546
|
+
if (!target) return;
|
|
938
1547
|
const { set, events } = store.getState();
|
|
939
1548
|
events.disconnect?.();
|
|
940
1549
|
set((state) => ({ events: { ...state.events, connected: target } }));
|
|
@@ -958,6 +1567,33 @@ function createPointerEvents(store) {
|
|
|
958
1567
|
}
|
|
959
1568
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
960
1569
|
}
|
|
1570
|
+
},
|
|
1571
|
+
// PREVIEW (A8): the XR pointer registration API may change before stable.
|
|
1572
|
+
registerPointer: (config) => {
|
|
1573
|
+
const pointerId = nextXRPointerId++;
|
|
1574
|
+
xrPointers.set(pointerId, config);
|
|
1575
|
+
const { internal } = store.getState();
|
|
1576
|
+
getPointerState(internal, pointerId);
|
|
1577
|
+
return pointerId;
|
|
1578
|
+
},
|
|
1579
|
+
unregisterPointer: (pointerId) => {
|
|
1580
|
+
xrPointers.delete(pointerId);
|
|
1581
|
+
const { internal } = store.getState();
|
|
1582
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1583
|
+
if (pointerState) {
|
|
1584
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1585
|
+
const eventObject = hoveredObj.eventObject;
|
|
1586
|
+
const instance = eventObject.__r3f;
|
|
1587
|
+
if (instance?.eventCount) {
|
|
1588
|
+
const handlers = instance.handlers;
|
|
1589
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1590
|
+
handlers.onPointerOut?.(data);
|
|
1591
|
+
handlers.onPointerLeave?.(data);
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
internal.pointerMap.delete(pointerId);
|
|
1595
|
+
}
|
|
1596
|
+
internal.pointerDirty.delete(pointerId);
|
|
961
1597
|
}
|
|
962
1598
|
};
|
|
963
1599
|
}
|
|
@@ -1019,6 +1655,14 @@ function notifyAlpha({ message, link }) {
|
|
|
1019
1655
|
}
|
|
1020
1656
|
}
|
|
1021
1657
|
|
|
1658
|
+
const INTERNAL_LIB_MARKERS = ["zustand", "setState", "Object.assign"];
|
|
1659
|
+
const INTERNAL_FIBER_MARKERS = ["packages/fiber/src", "packages/fiber/dist", "@react-three/fiber", "@react-three_fiber"];
|
|
1660
|
+
function isInternalRendererAccess(stack, callerDepth = 2) {
|
|
1661
|
+
if (!stack) return false;
|
|
1662
|
+
const caller = stack.split("\n")[callerDepth] ?? "";
|
|
1663
|
+
return INTERNAL_LIB_MARKERS.some((m) => caller.includes(m)) || INTERNAL_FIBER_MARKERS.some((m) => caller.includes(m));
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1022
1666
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
1023
1667
|
const context = globalThis[R3F_CONTEXT] ?? (globalThis[R3F_CONTEXT] = React.createContext(null));
|
|
1024
1668
|
const createStore = (invalidate, advance) => {
|
|
@@ -1048,23 +1692,29 @@ const createStore = (invalidate, advance) => {
|
|
|
1048
1692
|
set,
|
|
1049
1693
|
get,
|
|
1050
1694
|
// Mock objects that have to be configured
|
|
1695
|
+
// primaryStore is set after store creation (self-reference for primary, primary's store for secondary)
|
|
1696
|
+
primaryStore: null,
|
|
1051
1697
|
gl: null,
|
|
1052
1698
|
renderer: null,
|
|
1053
1699
|
camera: null,
|
|
1054
1700
|
frustum: new Frustum(),
|
|
1055
1701
|
autoUpdateFrustum: true,
|
|
1056
1702
|
raycaster: null,
|
|
1057
|
-
events: {
|
|
1703
|
+
events: {
|
|
1704
|
+
priority: 1,
|
|
1705
|
+
enabled: true,
|
|
1706
|
+
connected: false,
|
|
1707
|
+
frameTimedRaycasts: true,
|
|
1708
|
+
alwaysFireOnScroll: true,
|
|
1709
|
+
updateOnFrame: false
|
|
1710
|
+
},
|
|
1058
1711
|
scene: null,
|
|
1059
1712
|
rootScene: null,
|
|
1060
1713
|
xr: null,
|
|
1061
1714
|
inspector: null,
|
|
1062
1715
|
invalidate: (frames = 1, stackFrames = false) => invalidate(get(), frames, stackFrames),
|
|
1063
1716
|
advance: (timestamp, runGlobalEffects) => advance(timestamp, runGlobalEffects, get()),
|
|
1064
|
-
|
|
1065
|
-
linear: false,
|
|
1066
|
-
flat: false,
|
|
1067
|
-
textureColorSpace: "srgb",
|
|
1717
|
+
textureColorSpace: SRGBColorSpace,
|
|
1068
1718
|
isLegacy: false,
|
|
1069
1719
|
webGPUSupported: false,
|
|
1070
1720
|
isNative: false,
|
|
@@ -1104,10 +1754,40 @@ const createStore = (invalidate, advance) => {
|
|
|
1104
1754
|
getCurrentViewport
|
|
1105
1755
|
},
|
|
1106
1756
|
setEvents: (events) => set((state2) => ({ ...state2, events: { ...state2.events, ...events } })),
|
|
1107
|
-
setSize: (width, height, top
|
|
1108
|
-
const
|
|
1109
|
-
|
|
1110
|
-
|
|
1757
|
+
setSize: (width, height, top, left) => {
|
|
1758
|
+
const state2 = get();
|
|
1759
|
+
if (width === void 0) {
|
|
1760
|
+
set({ _sizeImperative: false });
|
|
1761
|
+
if (state2._sizeProps) {
|
|
1762
|
+
const { width: propW, height: propH } = state2._sizeProps;
|
|
1763
|
+
if (propW !== void 0 || propH !== void 0) {
|
|
1764
|
+
const currentSize = state2.size;
|
|
1765
|
+
const newSize = {
|
|
1766
|
+
width: propW ?? currentSize.width,
|
|
1767
|
+
height: propH ?? currentSize.height,
|
|
1768
|
+
top: currentSize.top,
|
|
1769
|
+
left: currentSize.left
|
|
1770
|
+
};
|
|
1771
|
+
set((s) => ({
|
|
1772
|
+
size: newSize,
|
|
1773
|
+
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
1774
|
+
}));
|
|
1775
|
+
getScheduler().invalidate();
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
return;
|
|
1779
|
+
}
|
|
1780
|
+
const w = width;
|
|
1781
|
+
const h = height ?? width;
|
|
1782
|
+
const t = top ?? state2.size.top;
|
|
1783
|
+
const l = left ?? state2.size.left;
|
|
1784
|
+
const size = { width: w, height: h, top: t, left: l };
|
|
1785
|
+
set((s) => ({
|
|
1786
|
+
size,
|
|
1787
|
+
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
1788
|
+
_sizeImperative: true
|
|
1789
|
+
}));
|
|
1790
|
+
getScheduler().invalidate();
|
|
1111
1791
|
},
|
|
1112
1792
|
setDpr: (dpr) => set((state2) => {
|
|
1113
1793
|
const resolved = calculateDpr(dpr);
|
|
@@ -1115,25 +1795,36 @@ const createStore = (invalidate, advance) => {
|
|
|
1115
1795
|
}),
|
|
1116
1796
|
setFrameloop: (frameloop = "always") => {
|
|
1117
1797
|
set(() => ({ frameloop }));
|
|
1798
|
+
getScheduler().frameloop = frameloop;
|
|
1118
1799
|
},
|
|
1119
1800
|
setError: (error) => set(() => ({ error })),
|
|
1120
1801
|
error: null,
|
|
1121
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useTextures,
|
|
1802
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
1122
1803
|
uniforms: {},
|
|
1123
1804
|
nodes: {},
|
|
1805
|
+
buffers: {},
|
|
1806
|
+
gpuStorage: {},
|
|
1124
1807
|
textures: /* @__PURE__ */ new Map(),
|
|
1125
|
-
|
|
1808
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1809
|
+
renderPipeline: null,
|
|
1126
1810
|
passes: {},
|
|
1811
|
+
_hmrVersion: 0,
|
|
1812
|
+
_sizeImperative: false,
|
|
1813
|
+
_sizeProps: null,
|
|
1127
1814
|
previousRoot: void 0,
|
|
1128
1815
|
internal: {
|
|
1129
1816
|
// Events
|
|
1130
1817
|
interaction: [],
|
|
1131
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
1132
1818
|
subscribers: [],
|
|
1819
|
+
// Per-pointer state (new unified structure)
|
|
1820
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1821
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1822
|
+
lastEvent: React.createRef(),
|
|
1823
|
+
// Deprecated but kept for backwards compatibility
|
|
1824
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1133
1825
|
initialClick: [0, 0],
|
|
1134
1826
|
initialHits: [],
|
|
1135
1827
|
capturedMap: /* @__PURE__ */ new Map(),
|
|
1136
|
-
lastEvent: React.createRef(),
|
|
1137
1828
|
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
1138
1829
|
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
1139
1830
|
// Occlusion system (WebGPU only)
|
|
@@ -1172,8 +1863,7 @@ const createStore = (invalidate, advance) => {
|
|
|
1172
1863
|
const currentState = rootStore.getState();
|
|
1173
1864
|
if (!currentState.isLegacy && currentState.internal.actualRenderer) {
|
|
1174
1865
|
const stack = new Error().stack || "";
|
|
1175
|
-
|
|
1176
|
-
if (!isInternalAccess) {
|
|
1866
|
+
if (!isInternalRendererAccess(stack)) {
|
|
1177
1867
|
const cleanedStack = stack.split("\n").slice(2).join("\n") || "Stack trace unavailable";
|
|
1178
1868
|
notifyDepreciated({
|
|
1179
1869
|
heading: "Accessing state.gl in WebGPU mode",
|
|
@@ -1216,13 +1906,22 @@ const createStore = (invalidate, advance) => {
|
|
|
1216
1906
|
rootStore.subscribe(() => {
|
|
1217
1907
|
const { camera, size, viewport, set, internal } = rootStore.getState();
|
|
1218
1908
|
const actualRenderer = internal.actualRenderer;
|
|
1909
|
+
const canvasTarget = internal.canvasTarget;
|
|
1219
1910
|
if (size.width !== oldSize.width || size.height !== oldSize.height || viewport.dpr !== oldDpr) {
|
|
1220
1911
|
oldSize = size;
|
|
1221
1912
|
oldDpr = viewport.dpr;
|
|
1222
1913
|
updateCamera(camera, size);
|
|
1223
|
-
if (
|
|
1224
|
-
|
|
1225
|
-
|
|
1914
|
+
if (internal.isSecondary && canvasTarget) {
|
|
1915
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1916
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1917
|
+
} else {
|
|
1918
|
+
if (viewport.dpr > 0) actualRenderer.setPixelRatio(viewport.dpr);
|
|
1919
|
+
actualRenderer.setSize(size.width, size.height, false);
|
|
1920
|
+
if (canvasTarget) {
|
|
1921
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1922
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1226
1925
|
}
|
|
1227
1926
|
if (camera !== oldCamera) {
|
|
1228
1927
|
oldCamera = camera;
|
|
@@ -1277,1053 +1976,28 @@ function loadingFn(extensions, onProgress) {
|
|
|
1277
1976
|
);
|
|
1278
1977
|
};
|
|
1279
1978
|
}
|
|
1280
|
-
function useLoader(loader, input, extensions, onProgress) {
|
|
1281
|
-
const
|
|
1979
|
+
function useLoader(loader, input, extensions, onProgress, cacheKey) {
|
|
1980
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
1981
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
1282
1982
|
const fn = loadingFn(extensions, onProgress);
|
|
1283
|
-
const results =
|
|
1983
|
+
const results = cacheKeys.map(
|
|
1984
|
+
(key, index) => suspend(() => fn(loader, inputs[index]), [loader, key], { equal: is.equ })
|
|
1985
|
+
);
|
|
1284
1986
|
return Array.isArray(input) ? results : results[0];
|
|
1285
1987
|
}
|
|
1286
|
-
useLoader.preload = function(loader, input, extensions, onProgress) {
|
|
1287
|
-
const
|
|
1288
|
-
|
|
1988
|
+
useLoader.preload = function(loader, input, extensions, onProgress, cacheKey) {
|
|
1989
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
1990
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
1991
|
+
cacheKeys.forEach(
|
|
1992
|
+
(key, index) => preload(() => loadingFn(extensions, onProgress)(loader, inputs[index]), [loader, key])
|
|
1993
|
+
);
|
|
1289
1994
|
};
|
|
1290
|
-
useLoader.clear = function(loader, input) {
|
|
1291
|
-
const
|
|
1292
|
-
|
|
1995
|
+
useLoader.clear = function(loader, input, cacheKey) {
|
|
1996
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
1997
|
+
cacheKeys.forEach((key) => clear([loader, key]));
|
|
1293
1998
|
};
|
|
1294
1999
|
useLoader.loader = getLoader;
|
|
1295
2000
|
|
|
1296
|
-
var __defProp$1 = Object.defineProperty;
|
|
1297
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1298
|
-
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1299
|
-
const DEFAULT_PHASES = ["start", "input", "physics", "update", "render", "finish"];
|
|
1300
|
-
class PhaseGraph {
|
|
1301
|
-
constructor() {
|
|
1302
|
-
/** Ordered list of phase nodes */
|
|
1303
|
-
__publicField$1(this, "phases", []);
|
|
1304
|
-
/** Quick lookup by name */
|
|
1305
|
-
__publicField$1(this, "phaseMap", /* @__PURE__ */ new Map());
|
|
1306
|
-
/** Cached ordered names (invalidated on changes) */
|
|
1307
|
-
__publicField$1(this, "orderedNamesCache", null);
|
|
1308
|
-
this.initializeDefaultPhases();
|
|
1309
|
-
}
|
|
1310
|
-
//* Initialization --------------------------------
|
|
1311
|
-
initializeDefaultPhases() {
|
|
1312
|
-
for (const name of DEFAULT_PHASES) {
|
|
1313
|
-
const node = { name, isAutoGenerated: false };
|
|
1314
|
-
this.phases.push(node);
|
|
1315
|
-
this.phaseMap.set(name, node);
|
|
1316
|
-
}
|
|
1317
|
-
this.invalidateCache();
|
|
1318
|
-
}
|
|
1319
|
-
//* Public API --------------------------------
|
|
1320
|
-
/**
|
|
1321
|
-
* Add a named phase to the graph
|
|
1322
|
-
* @param name - Phase name (must be unique)
|
|
1323
|
-
* @param options - Position options (before or after another phase)
|
|
1324
|
-
*/
|
|
1325
|
-
addPhase(name, options = {}) {
|
|
1326
|
-
if (this.phaseMap.has(name)) {
|
|
1327
|
-
console.warn(`[useFrame] Phase "${name}" already exists`);
|
|
1328
|
-
return;
|
|
1329
|
-
}
|
|
1330
|
-
const { before, after } = options;
|
|
1331
|
-
const node = { name, isAutoGenerated: false };
|
|
1332
|
-
let insertIndex = this.phases.length;
|
|
1333
|
-
const targetIndex = this.getPhaseIndex(before ?? after);
|
|
1334
|
-
if (targetIndex !== -1) insertIndex = before ? targetIndex : targetIndex + 1;
|
|
1335
|
-
else {
|
|
1336
|
-
const constraintType = before ? "before" : "after";
|
|
1337
|
-
console.warn(`[useFrame] Phase "${before ?? after}" not found for '${constraintType}' constraint`);
|
|
1338
|
-
}
|
|
1339
|
-
this.phases.splice(insertIndex, 0, node);
|
|
1340
|
-
this.phaseMap.set(name, node);
|
|
1341
|
-
this.invalidateCache();
|
|
1342
|
-
}
|
|
1343
|
-
/**
|
|
1344
|
-
* Get ordered list of phase names
|
|
1345
|
-
*/
|
|
1346
|
-
getOrderedPhases() {
|
|
1347
|
-
if (this.orderedNamesCache === null) this.orderedNamesCache = this.phases.map((p) => p.name);
|
|
1348
|
-
return this.orderedNamesCache;
|
|
1349
|
-
}
|
|
1350
|
-
/**
|
|
1351
|
-
* Check if a phase exists
|
|
1352
|
-
*/
|
|
1353
|
-
hasPhase(name) {
|
|
1354
|
-
return this.phaseMap.has(name);
|
|
1355
|
-
}
|
|
1356
|
-
/**
|
|
1357
|
-
* Get the index of a phase (-1 if not found)
|
|
1358
|
-
*/
|
|
1359
|
-
getPhaseIndex(name) {
|
|
1360
|
-
if (!name) return -1;
|
|
1361
|
-
return this.phases.findIndex((p) => p.name === name);
|
|
1362
|
-
}
|
|
1363
|
-
/**
|
|
1364
|
-
* Ensure a phase exists, creating an auto-generated one if needed.
|
|
1365
|
-
* Used for resolving before/after constraints.
|
|
1366
|
-
*
|
|
1367
|
-
* @param name - The phase name to ensure exists
|
|
1368
|
-
* @returns The phase name (may be auto-generated like 'before:render')
|
|
1369
|
-
*/
|
|
1370
|
-
ensurePhase(name) {
|
|
1371
|
-
if (this.phaseMap.has(name)) return name;
|
|
1372
|
-
const node = { name, isAutoGenerated: true };
|
|
1373
|
-
this.phases.push(node);
|
|
1374
|
-
this.phaseMap.set(name, node);
|
|
1375
|
-
this.invalidateCache();
|
|
1376
|
-
return name;
|
|
1377
|
-
}
|
|
1378
|
-
/**
|
|
1379
|
-
* Resolve where a job with before/after constraints should go.
|
|
1380
|
-
* Creates auto-generated phases if needed.
|
|
1381
|
-
*
|
|
1382
|
-
* @param before - Phase(s) to run before
|
|
1383
|
-
* @param after - Phase(s) to run after
|
|
1384
|
-
* @returns The resolved phase name
|
|
1385
|
-
*/
|
|
1386
|
-
resolveConstraintPhase(before, after) {
|
|
1387
|
-
const beforeArr = before ? Array.isArray(before) ? before : [before] : [];
|
|
1388
|
-
const afterArr = after ? Array.isArray(after) ? after : [after] : [];
|
|
1389
|
-
if (beforeArr.length > 0) {
|
|
1390
|
-
return this.ensureAutoPhase(beforeArr[0], "before", 0);
|
|
1391
|
-
}
|
|
1392
|
-
if (afterArr.length > 0) {
|
|
1393
|
-
return this.ensureAutoPhase(afterArr[0], "after", 1);
|
|
1394
|
-
}
|
|
1395
|
-
return "update";
|
|
1396
|
-
}
|
|
1397
|
-
/**
|
|
1398
|
-
* Ensure an auto-generated phase exists relative to a target phase.
|
|
1399
|
-
* Creates the phase if it doesn't exist, inserting it at the correct position.
|
|
1400
|
-
*
|
|
1401
|
-
* @param target - The target phase name to position relative to
|
|
1402
|
-
* @param prefix - Prefix for auto-generated phase name ('before' or 'after')
|
|
1403
|
-
* @param offset - Insertion offset (0 for before, 1 for after)
|
|
1404
|
-
* @returns The auto-generated phase name
|
|
1405
|
-
*/
|
|
1406
|
-
ensureAutoPhase(target, prefix, offset) {
|
|
1407
|
-
const autoName = `${prefix}:${target}`;
|
|
1408
|
-
if (this.phaseMap.has(autoName)) return autoName;
|
|
1409
|
-
const node = { name: autoName, isAutoGenerated: true };
|
|
1410
|
-
const targetIndex = this.getPhaseIndex(target);
|
|
1411
|
-
if (targetIndex !== -1) this.phases.splice(targetIndex + offset, 0, node);
|
|
1412
|
-
else this.phases.push(node);
|
|
1413
|
-
this.phaseMap.set(autoName, node);
|
|
1414
|
-
this.invalidateCache();
|
|
1415
|
-
return autoName;
|
|
1416
|
-
}
|
|
1417
|
-
// Internal --------------------------------
|
|
1418
|
-
invalidateCache() {
|
|
1419
|
-
this.orderedNamesCache = null;
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
|
|
1423
|
-
function rebuildSortedJobs(jobs, phaseGraph) {
|
|
1424
|
-
const orderedPhases = phaseGraph.getOrderedPhases();
|
|
1425
|
-
const buckets = /* @__PURE__ */ new Map();
|
|
1426
|
-
for (const phase of orderedPhases) {
|
|
1427
|
-
buckets.set(phase, []);
|
|
1428
|
-
}
|
|
1429
|
-
for (const job of jobs.values()) {
|
|
1430
|
-
if (!job.enabled) continue;
|
|
1431
|
-
let bucket = buckets.get(job.phase);
|
|
1432
|
-
if (!bucket) {
|
|
1433
|
-
bucket = [];
|
|
1434
|
-
buckets.set(job.phase, bucket);
|
|
1435
|
-
}
|
|
1436
|
-
bucket.push(job);
|
|
1437
|
-
}
|
|
1438
|
-
const sortedBuckets = [];
|
|
1439
|
-
for (const phase of orderedPhases) {
|
|
1440
|
-
const bucket = buckets.get(phase);
|
|
1441
|
-
if (!bucket || bucket.length === 0) continue;
|
|
1442
|
-
bucket.sort((a, b) => {
|
|
1443
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1444
|
-
return a.index - b.index;
|
|
1445
|
-
});
|
|
1446
|
-
sortedBuckets.push(hasCrossJobConstraints(bucket) ? topologicalSort(bucket) : bucket);
|
|
1447
|
-
}
|
|
1448
|
-
for (const [phase, bucket] of buckets) {
|
|
1449
|
-
if (!orderedPhases.includes(phase) && bucket.length > 0) {
|
|
1450
|
-
bucket.sort((a, b) => {
|
|
1451
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1452
|
-
return a.index - b.index;
|
|
1453
|
-
});
|
|
1454
|
-
sortedBuckets.push(bucket);
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
return sortedBuckets.flat();
|
|
1458
|
-
}
|
|
1459
|
-
function hasCrossJobConstraints(bucket) {
|
|
1460
|
-
const jobIds = new Set(bucket.map((j) => j.id));
|
|
1461
|
-
for (const job of bucket) {
|
|
1462
|
-
for (const ref of job.before) {
|
|
1463
|
-
if (jobIds.has(ref)) return true;
|
|
1464
|
-
}
|
|
1465
|
-
for (const ref of job.after) {
|
|
1466
|
-
if (jobIds.has(ref)) return true;
|
|
1467
|
-
}
|
|
1468
|
-
}
|
|
1469
|
-
return false;
|
|
1470
|
-
}
|
|
1471
|
-
function topologicalSort(jobs) {
|
|
1472
|
-
const n = jobs.length;
|
|
1473
|
-
if (n <= 1) return jobs;
|
|
1474
|
-
const jobMap = /* @__PURE__ */ new Map();
|
|
1475
|
-
const inDegree = /* @__PURE__ */ new Map();
|
|
1476
|
-
const adjacency = /* @__PURE__ */ new Map();
|
|
1477
|
-
for (const job of jobs) {
|
|
1478
|
-
jobMap.set(job.id, job);
|
|
1479
|
-
inDegree.set(job.id, 0);
|
|
1480
|
-
adjacency.set(job.id, []);
|
|
1481
|
-
}
|
|
1482
|
-
for (const job of jobs) {
|
|
1483
|
-
for (const ref of job.before) {
|
|
1484
|
-
if (jobMap.has(ref)) {
|
|
1485
|
-
adjacency.get(job.id).push(ref);
|
|
1486
|
-
inDegree.set(ref, inDegree.get(ref) + 1);
|
|
1487
|
-
}
|
|
1488
|
-
}
|
|
1489
|
-
for (const ref of job.after) {
|
|
1490
|
-
if (jobMap.has(ref)) {
|
|
1491
|
-
adjacency.get(ref).push(job.id);
|
|
1492
|
-
inDegree.set(job.id, inDegree.get(job.id) + 1);
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
}
|
|
1496
|
-
const queue = [];
|
|
1497
|
-
for (const job of jobs) {
|
|
1498
|
-
if (inDegree.get(job.id) === 0) {
|
|
1499
|
-
queue.push(job);
|
|
1500
|
-
}
|
|
1501
|
-
}
|
|
1502
|
-
queue.sort((a, b) => {
|
|
1503
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1504
|
-
return a.index - b.index;
|
|
1505
|
-
});
|
|
1506
|
-
const result = [];
|
|
1507
|
-
while (queue.length > 0) {
|
|
1508
|
-
const job = queue.shift();
|
|
1509
|
-
result.push(job);
|
|
1510
|
-
const neighbors = adjacency.get(job.id) || [];
|
|
1511
|
-
for (const neighborId of neighbors) {
|
|
1512
|
-
const newDegree = inDegree.get(neighborId) - 1;
|
|
1513
|
-
inDegree.set(neighborId, newDegree);
|
|
1514
|
-
if (newDegree === 0) {
|
|
1515
|
-
const neighbor = jobMap.get(neighborId);
|
|
1516
|
-
insertSorted(queue, neighbor);
|
|
1517
|
-
}
|
|
1518
|
-
}
|
|
1519
|
-
}
|
|
1520
|
-
if (result.length !== n) {
|
|
1521
|
-
console.warn("[useFrame] Circular dependency detected in job constraints");
|
|
1522
|
-
const resultIds = new Set(result.map((j) => j.id));
|
|
1523
|
-
for (const job of jobs) {
|
|
1524
|
-
if (!resultIds.has(job.id)) result.push(job);
|
|
1525
|
-
}
|
|
1526
|
-
}
|
|
1527
|
-
return result;
|
|
1528
|
-
}
|
|
1529
|
-
function insertSorted(arr, job) {
|
|
1530
|
-
let i = 0;
|
|
1531
|
-
while (i < arr.length) {
|
|
1532
|
-
const cmp = arr[i];
|
|
1533
|
-
if (job.priority > cmp.priority || job.priority === cmp.priority && job.index < cmp.index) {
|
|
1534
|
-
break;
|
|
1535
|
-
}
|
|
1536
|
-
i++;
|
|
1537
|
-
}
|
|
1538
|
-
arr.splice(i, 0, job);
|
|
1539
|
-
}
|
|
1540
|
-
|
|
1541
|
-
function shouldRun(job, now) {
|
|
1542
|
-
if (!job.enabled) return false;
|
|
1543
|
-
if (!job.fps) return true;
|
|
1544
|
-
const minInterval = 1e3 / job.fps;
|
|
1545
|
-
const lastRun = job.lastRun ?? 0;
|
|
1546
|
-
const elapsed = now - lastRun;
|
|
1547
|
-
if (elapsed < minInterval) return false;
|
|
1548
|
-
if (job.drop) {
|
|
1549
|
-
job.lastRun = now;
|
|
1550
|
-
} else {
|
|
1551
|
-
const steps = Math.floor(elapsed / minInterval);
|
|
1552
|
-
job.lastRun = lastRun + steps * minInterval;
|
|
1553
|
-
if (job.lastRun < now - minInterval) {
|
|
1554
|
-
job.lastRun = now;
|
|
1555
|
-
}
|
|
1556
|
-
}
|
|
1557
|
-
return true;
|
|
1558
|
-
}
|
|
1559
|
-
function resetJobTiming(job) {
|
|
1560
|
-
job.lastRun = void 0;
|
|
1561
|
-
}
|
|
1562
|
-
|
|
1563
|
-
var __defProp = Object.defineProperty;
|
|
1564
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1565
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1566
|
-
const hmrData = (() => {
|
|
1567
|
-
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return void 0;
|
|
1568
|
-
if (typeof import_meta_hot !== "undefined") return import_meta_hot;
|
|
1569
|
-
try {
|
|
1570
|
-
return (0, eval)("import.meta.hot");
|
|
1571
|
-
} catch {
|
|
1572
|
-
return void 0;
|
|
1573
|
-
}
|
|
1574
|
-
})();
|
|
1575
|
-
const _Scheduler = class _Scheduler {
|
|
1576
|
-
//* Constructor ================================
|
|
1577
|
-
constructor() {
|
|
1578
|
-
//* Critical State ================================
|
|
1579
|
-
__publicField(this, "roots", /* @__PURE__ */ new Map());
|
|
1580
|
-
__publicField(this, "phaseGraph");
|
|
1581
|
-
__publicField(this, "loopState", {
|
|
1582
|
-
running: false,
|
|
1583
|
-
rafHandle: null,
|
|
1584
|
-
lastTime: null,
|
|
1585
|
-
// null = uninitialized, 0+ = valid timestamp
|
|
1586
|
-
frameCount: 0,
|
|
1587
|
-
elapsedTime: 0,
|
|
1588
|
-
createdAt: performance.now()
|
|
1589
|
-
});
|
|
1590
|
-
__publicField(this, "stoppedTime", 0);
|
|
1591
|
-
//* Private State ================================
|
|
1592
|
-
__publicField(this, "nextRootIndex", 0);
|
|
1593
|
-
__publicField(this, "globalBeforeJobs", /* @__PURE__ */ new Map());
|
|
1594
|
-
__publicField(this, "globalAfterJobs", /* @__PURE__ */ new Map());
|
|
1595
|
-
__publicField(this, "nextGlobalIndex", 0);
|
|
1596
|
-
__publicField(this, "idleCallbacks", /* @__PURE__ */ new Set());
|
|
1597
|
-
__publicField(this, "nextJobIndex", 0);
|
|
1598
|
-
__publicField(this, "jobStateListeners", /* @__PURE__ */ new Map());
|
|
1599
|
-
__publicField(this, "pendingFrames", 0);
|
|
1600
|
-
__publicField(this, "_frameloop", "always");
|
|
1601
|
-
//* Independent Mode & Error Handling State ================================
|
|
1602
|
-
__publicField(this, "_independent", false);
|
|
1603
|
-
__publicField(this, "errorHandler", null);
|
|
1604
|
-
__publicField(this, "rootReadyCallbacks", /* @__PURE__ */ new Set());
|
|
1605
|
-
//* Core Loop Execution Methods ================================
|
|
1606
|
-
/**
|
|
1607
|
-
* Main RAF loop callback.
|
|
1608
|
-
* Executes frame, handles demand mode, and schedules next frame.
|
|
1609
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
1610
|
-
* @returns {void}
|
|
1611
|
-
* @private
|
|
1612
|
-
*/
|
|
1613
|
-
__publicField(this, "loop", (timestamp) => {
|
|
1614
|
-
if (!this.loopState.running) return;
|
|
1615
|
-
this.executeFrame(timestamp);
|
|
1616
|
-
if (this._frameloop === "demand") {
|
|
1617
|
-
this.pendingFrames = Math.max(0, this.pendingFrames - 1);
|
|
1618
|
-
if (this.pendingFrames === 0) {
|
|
1619
|
-
this.notifyIdle(timestamp);
|
|
1620
|
-
return this.stop();
|
|
1621
|
-
}
|
|
1622
|
-
}
|
|
1623
|
-
this.loopState.rafHandle = requestAnimationFrame(this.loop);
|
|
1624
|
-
});
|
|
1625
|
-
this.phaseGraph = new PhaseGraph();
|
|
1626
|
-
}
|
|
1627
|
-
static get instance() {
|
|
1628
|
-
return globalThis[_Scheduler.INSTANCE_KEY] ?? null;
|
|
1629
|
-
}
|
|
1630
|
-
static set instance(value) {
|
|
1631
|
-
globalThis[_Scheduler.INSTANCE_KEY] = value;
|
|
1632
|
-
}
|
|
1633
|
-
/**
|
|
1634
|
-
* Get the global scheduler instance (creates if doesn't exist).
|
|
1635
|
-
* Uses HMR data to preserve instance across hot reloads.
|
|
1636
|
-
* @returns {Scheduler} The singleton scheduler instance
|
|
1637
|
-
*/
|
|
1638
|
-
static get() {
|
|
1639
|
-
if (!_Scheduler.instance && hmrData?.data?.scheduler) {
|
|
1640
|
-
_Scheduler.instance = hmrData.data.scheduler;
|
|
1641
|
-
}
|
|
1642
|
-
if (!_Scheduler.instance) {
|
|
1643
|
-
_Scheduler.instance = new _Scheduler();
|
|
1644
|
-
if (hmrData?.data) {
|
|
1645
|
-
hmrData.data.scheduler = _Scheduler.instance;
|
|
1646
|
-
}
|
|
1647
|
-
}
|
|
1648
|
-
return _Scheduler.instance;
|
|
1649
|
-
}
|
|
1650
|
-
/**
|
|
1651
|
-
* Reset the singleton instance. Stops the loop and clears all state.
|
|
1652
|
-
* Primarily used for testing to ensure clean state between tests.
|
|
1653
|
-
* @returns {void}
|
|
1654
|
-
*/
|
|
1655
|
-
static reset() {
|
|
1656
|
-
if (_Scheduler.instance) {
|
|
1657
|
-
_Scheduler.instance.stop();
|
|
1658
|
-
_Scheduler.instance = null;
|
|
1659
|
-
}
|
|
1660
|
-
if (hmrData?.data) {
|
|
1661
|
-
hmrData.data.scheduler = null;
|
|
1662
|
-
}
|
|
1663
|
-
}
|
|
1664
|
-
//* Getters & Setters ================================
|
|
1665
|
-
get phases() {
|
|
1666
|
-
return this.phaseGraph.getOrderedPhases();
|
|
1667
|
-
}
|
|
1668
|
-
get frameloop() {
|
|
1669
|
-
return this._frameloop;
|
|
1670
|
-
}
|
|
1671
|
-
set frameloop(mode) {
|
|
1672
|
-
if (this._frameloop === mode) return;
|
|
1673
|
-
const wasAlways = this._frameloop === "always";
|
|
1674
|
-
this._frameloop = mode;
|
|
1675
|
-
if (mode === "always" && !this.loopState.running && this.roots.size > 0) this.start();
|
|
1676
|
-
else if (mode !== "always" && wasAlways) this.stop();
|
|
1677
|
-
}
|
|
1678
|
-
get isRunning() {
|
|
1679
|
-
return this.loopState.running;
|
|
1680
|
-
}
|
|
1681
|
-
get isReady() {
|
|
1682
|
-
return this.roots.size > 0;
|
|
1683
|
-
}
|
|
1684
|
-
get independent() {
|
|
1685
|
-
return this._independent;
|
|
1686
|
-
}
|
|
1687
|
-
set independent(value) {
|
|
1688
|
-
this._independent = value;
|
|
1689
|
-
if (value) this.ensureDefaultRoot();
|
|
1690
|
-
}
|
|
1691
|
-
//* Root Management Methods ================================
|
|
1692
|
-
/**
|
|
1693
|
-
* Register a root (Canvas) with the scheduler.
|
|
1694
|
-
* The first root to register starts the RAF loop (if frameloop='always').
|
|
1695
|
-
* @param {string} id - Unique identifier for this root
|
|
1696
|
-
* @param {RootOptions} [options] - Optional configuration with getState and onError callbacks
|
|
1697
|
-
* @returns {() => void} Unsubscribe function to remove this root
|
|
1698
|
-
*/
|
|
1699
|
-
registerRoot(id, options = {}) {
|
|
1700
|
-
if (this.roots.has(id)) {
|
|
1701
|
-
console.warn(`[Scheduler] Root "${id}" already registered`);
|
|
1702
|
-
return () => this.unregisterRoot(id);
|
|
1703
|
-
}
|
|
1704
|
-
const entry = {
|
|
1705
|
-
id,
|
|
1706
|
-
getState: options.getState ?? (() => ({})),
|
|
1707
|
-
jobs: /* @__PURE__ */ new Map(),
|
|
1708
|
-
sortedJobs: [],
|
|
1709
|
-
needsRebuild: false
|
|
1710
|
-
};
|
|
1711
|
-
if (options.onError) {
|
|
1712
|
-
this.errorHandler = options.onError;
|
|
1713
|
-
}
|
|
1714
|
-
this.roots.set(id, entry);
|
|
1715
|
-
if (this.roots.size === 1) {
|
|
1716
|
-
this.notifyRootReady();
|
|
1717
|
-
if (this._frameloop === "always") this.start();
|
|
1718
|
-
}
|
|
1719
|
-
return () => this.unregisterRoot(id);
|
|
1720
|
-
}
|
|
1721
|
-
/**
|
|
1722
|
-
* Unregister a root from the scheduler.
|
|
1723
|
-
* Cleans up all job state listeners for this root's jobs.
|
|
1724
|
-
* The last root to unregister stops the RAF loop.
|
|
1725
|
-
* @param {string} id - The root ID to unregister
|
|
1726
|
-
* @returns {void}
|
|
1727
|
-
*/
|
|
1728
|
-
unregisterRoot(id) {
|
|
1729
|
-
const root = this.roots.get(id);
|
|
1730
|
-
if (!root) return;
|
|
1731
|
-
for (const jobId of root.jobs.keys()) {
|
|
1732
|
-
this.jobStateListeners.delete(jobId);
|
|
1733
|
-
}
|
|
1734
|
-
this.roots.delete(id);
|
|
1735
|
-
if (this.roots.size === 0) {
|
|
1736
|
-
this.stop();
|
|
1737
|
-
this.errorHandler = null;
|
|
1738
|
-
}
|
|
1739
|
-
}
|
|
1740
|
-
/**
|
|
1741
|
-
* Subscribe to be notified when a root becomes available.
|
|
1742
|
-
* Fires immediately if a root already exists.
|
|
1743
|
-
* @param {() => void} callback - Function called when first root registers
|
|
1744
|
-
* @returns {() => void} Unsubscribe function
|
|
1745
|
-
*/
|
|
1746
|
-
onRootReady(callback) {
|
|
1747
|
-
if (this.roots.size > 0) {
|
|
1748
|
-
callback();
|
|
1749
|
-
return () => {
|
|
1750
|
-
};
|
|
1751
|
-
}
|
|
1752
|
-
this.rootReadyCallbacks.add(callback);
|
|
1753
|
-
return () => this.rootReadyCallbacks.delete(callback);
|
|
1754
|
-
}
|
|
1755
|
-
/**
|
|
1756
|
-
* Notify all registered root-ready callbacks.
|
|
1757
|
-
* Called when the first root registers.
|
|
1758
|
-
* @returns {void}
|
|
1759
|
-
* @private
|
|
1760
|
-
*/
|
|
1761
|
-
notifyRootReady() {
|
|
1762
|
-
for (const cb of this.rootReadyCallbacks) {
|
|
1763
|
-
try {
|
|
1764
|
-
cb();
|
|
1765
|
-
} catch (error) {
|
|
1766
|
-
console.error("[Scheduler] Error in root-ready callback:", error);
|
|
1767
|
-
}
|
|
1768
|
-
}
|
|
1769
|
-
this.rootReadyCallbacks.clear();
|
|
1770
|
-
}
|
|
1771
|
-
/**
|
|
1772
|
-
* Ensure a default root exists for independent mode.
|
|
1773
|
-
* Creates a minimal root with no state provider.
|
|
1774
|
-
* @returns {void}
|
|
1775
|
-
* @private
|
|
1776
|
-
*/
|
|
1777
|
-
ensureDefaultRoot() {
|
|
1778
|
-
if (!this.roots.has("__default__")) {
|
|
1779
|
-
this.registerRoot("__default__");
|
|
1780
|
-
}
|
|
1781
|
-
}
|
|
1782
|
-
/**
|
|
1783
|
-
* Trigger error handling for job errors.
|
|
1784
|
-
* Uses the bound error handler if available, otherwise logs to console.
|
|
1785
|
-
* @param {Error} error - The error to handle
|
|
1786
|
-
* @returns {void}
|
|
1787
|
-
*/
|
|
1788
|
-
triggerError(error) {
|
|
1789
|
-
if (this.errorHandler) this.errorHandler(error);
|
|
1790
|
-
else console.error("[Scheduler]", error);
|
|
1791
|
-
}
|
|
1792
|
-
//* Phase Management Methods ================================
|
|
1793
|
-
/**
|
|
1794
|
-
* Add a named phase to the scheduler's execution order.
|
|
1795
|
-
* Marks all roots for rebuild to incorporate the new phase.
|
|
1796
|
-
* @param {string} name - The phase name (e.g., 'physics', 'postprocess')
|
|
1797
|
-
* @param {AddPhaseOptions} [options] - Positioning options (before/after other phases)
|
|
1798
|
-
* @returns {void}
|
|
1799
|
-
* @example
|
|
1800
|
-
* scheduler.addPhase('physics', { before: 'update' });
|
|
1801
|
-
* scheduler.addPhase('postprocess', { after: 'render' });
|
|
1802
|
-
*/
|
|
1803
|
-
addPhase(name, options) {
|
|
1804
|
-
this.phaseGraph.addPhase(name, options);
|
|
1805
|
-
for (const root of this.roots.values()) {
|
|
1806
|
-
root.needsRebuild = true;
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1809
|
-
/**
|
|
1810
|
-
* Check if a phase exists in the scheduler.
|
|
1811
|
-
* @param {string} name - The phase name to check
|
|
1812
|
-
* @returns {boolean} True if the phase exists
|
|
1813
|
-
*/
|
|
1814
|
-
hasPhase(name) {
|
|
1815
|
-
return this.phaseGraph.hasPhase(name);
|
|
1816
|
-
}
|
|
1817
|
-
//* Global Job Registration Methods (Deprecated APIs) ================================
|
|
1818
|
-
/**
|
|
1819
|
-
* Register a global job that runs once per frame (not per-root).
|
|
1820
|
-
* Used internally by deprecated addEffect/addAfterEffect APIs.
|
|
1821
|
-
* @param {'before' | 'after'} phase - When to run: 'before' all roots or 'after' all roots
|
|
1822
|
-
* @param {string} id - Unique identifier for this global job
|
|
1823
|
-
* @param {(timestamp: number) => void} callback - Function called each frame with RAF timestamp
|
|
1824
|
-
* @returns {() => void} Unsubscribe function to remove this global job
|
|
1825
|
-
* @deprecated Use useFrame with phases instead
|
|
1826
|
-
*/
|
|
1827
|
-
registerGlobal(phase, id, callback) {
|
|
1828
|
-
const job = { id, callback };
|
|
1829
|
-
if (phase === "before") {
|
|
1830
|
-
this.globalBeforeJobs.set(id, job);
|
|
1831
|
-
} else {
|
|
1832
|
-
this.globalAfterJobs.set(id, job);
|
|
1833
|
-
}
|
|
1834
|
-
return () => {
|
|
1835
|
-
if (phase === "before") this.globalBeforeJobs.delete(id);
|
|
1836
|
-
else this.globalAfterJobs.delete(id);
|
|
1837
|
-
};
|
|
1838
|
-
}
|
|
1839
|
-
//* Idle Callback Methods (Deprecated API) ================================
|
|
1840
|
-
/**
|
|
1841
|
-
* Register an idle callback that fires when the loop stops.
|
|
1842
|
-
* Used internally by deprecated addTail API.
|
|
1843
|
-
* @param {(timestamp: number) => void} callback - Function called when loop becomes idle
|
|
1844
|
-
* @returns {() => void} Unsubscribe function to remove this idle callback
|
|
1845
|
-
* @deprecated Use demand mode with invalidate() instead
|
|
1846
|
-
*/
|
|
1847
|
-
onIdle(callback) {
|
|
1848
|
-
this.idleCallbacks.add(callback);
|
|
1849
|
-
return () => this.idleCallbacks.delete(callback);
|
|
1850
|
-
}
|
|
1851
|
-
/**
|
|
1852
|
-
* Notify all registered idle callbacks.
|
|
1853
|
-
* Called when the loop stops in demand mode.
|
|
1854
|
-
* @param {number} timestamp - The RAF timestamp when idle occurred
|
|
1855
|
-
* @returns {void}
|
|
1856
|
-
* @private
|
|
1857
|
-
*/
|
|
1858
|
-
notifyIdle(timestamp) {
|
|
1859
|
-
for (const cb of this.idleCallbacks) {
|
|
1860
|
-
try {
|
|
1861
|
-
cb(timestamp);
|
|
1862
|
-
} catch (error) {
|
|
1863
|
-
console.error("[Scheduler] Error in idle callback:", error);
|
|
1864
|
-
}
|
|
1865
|
-
}
|
|
1866
|
-
}
|
|
1867
|
-
//* Job Registration & Management Methods ================================
|
|
1868
|
-
/**
|
|
1869
|
-
* Register a job (frame callback) with a specific root.
|
|
1870
|
-
* This is the core registration method used by useFrame internally.
|
|
1871
|
-
* @param {FrameNextCallback} callback - The function to call each frame
|
|
1872
|
-
* @param {JobOptions & { rootId?: string; system?: boolean }} [options] - Job configuration
|
|
1873
|
-
* @param {string} [options.rootId] - Target root ID (defaults to first registered root)
|
|
1874
|
-
* @param {string} [options.id] - Unique job ID (auto-generated if not provided)
|
|
1875
|
-
* @param {string} [options.phase] - Execution phase (defaults to 'update')
|
|
1876
|
-
* @param {number} [options.priority] - Priority within phase (higher = earlier, default 0)
|
|
1877
|
-
* @param {number} [options.fps] - FPS throttle limit
|
|
1878
|
-
* @param {boolean} [options.drop] - Drop frames when behind (default true)
|
|
1879
|
-
* @param {boolean} [options.enabled] - Whether job is active (default true)
|
|
1880
|
-
* @param {boolean} [options.system] - Internal flag for system jobs (not user-facing)
|
|
1881
|
-
* @returns {() => void} Unsubscribe function to remove this job
|
|
1882
|
-
*/
|
|
1883
|
-
register(callback, options = {}) {
|
|
1884
|
-
const rootId = options.rootId;
|
|
1885
|
-
const root = rootId ? this.roots.get(rootId) : this.roots.values().next().value;
|
|
1886
|
-
if (!root) {
|
|
1887
|
-
console.warn("[Scheduler] No root registered. Is this inside a Canvas?");
|
|
1888
|
-
return () => {
|
|
1889
|
-
};
|
|
1890
|
-
}
|
|
1891
|
-
const id = options.id ?? this.generateJobId();
|
|
1892
|
-
let phase = options.phase ?? "update";
|
|
1893
|
-
if (!options.phase && (options.before || options.after)) {
|
|
1894
|
-
phase = this.phaseGraph.resolveConstraintPhase(options.before, options.after);
|
|
1895
|
-
}
|
|
1896
|
-
const before = this.normalizeConstraints(options.before);
|
|
1897
|
-
const after = this.normalizeConstraints(options.after);
|
|
1898
|
-
const job = {
|
|
1899
|
-
id,
|
|
1900
|
-
callback,
|
|
1901
|
-
phase,
|
|
1902
|
-
before,
|
|
1903
|
-
after,
|
|
1904
|
-
priority: options.priority ?? 0,
|
|
1905
|
-
index: this.nextJobIndex++,
|
|
1906
|
-
fps: options.fps,
|
|
1907
|
-
drop: options.drop ?? true,
|
|
1908
|
-
enabled: options.enabled ?? true,
|
|
1909
|
-
system: options.system ?? false
|
|
1910
|
-
};
|
|
1911
|
-
if (root.jobs.has(id)) {
|
|
1912
|
-
console.warn(`[useFrame] Job with id "${id}" already exists, replacing`);
|
|
1913
|
-
}
|
|
1914
|
-
root.jobs.set(id, job);
|
|
1915
|
-
root.needsRebuild = true;
|
|
1916
|
-
return () => this.unregister(id, root.id);
|
|
1917
|
-
}
|
|
1918
|
-
/**
|
|
1919
|
-
* Unregister a job by its ID.
|
|
1920
|
-
* Searches all roots if rootId is not provided.
|
|
1921
|
-
* @param {string} id - The job ID to unregister
|
|
1922
|
-
* @param {string} [rootId] - Optional root ID to search (searches all if not provided)
|
|
1923
|
-
* @returns {void}
|
|
1924
|
-
*/
|
|
1925
|
-
unregister(id, rootId) {
|
|
1926
|
-
const root = rootId ? this.roots.get(rootId) : Array.from(this.roots.values()).find((r) => r.jobs.has(id));
|
|
1927
|
-
if (root?.jobs.delete(id)) {
|
|
1928
|
-
root.needsRebuild = true;
|
|
1929
|
-
this.jobStateListeners.delete(id);
|
|
1930
|
-
}
|
|
1931
|
-
}
|
|
1932
|
-
/**
|
|
1933
|
-
* Update a job's options dynamically.
|
|
1934
|
-
* Searches all roots to find the job by ID.
|
|
1935
|
-
* Phase/constraint changes trigger a rebuild of the sorted job list.
|
|
1936
|
-
* @param {string} id - The job ID to update
|
|
1937
|
-
* @param {Partial<JobOptions>} options - The options to update
|
|
1938
|
-
* @returns {void}
|
|
1939
|
-
*/
|
|
1940
|
-
updateJob(id, options) {
|
|
1941
|
-
let job;
|
|
1942
|
-
let root;
|
|
1943
|
-
for (const r of this.roots.values()) {
|
|
1944
|
-
job = r.jobs.get(id);
|
|
1945
|
-
if (job) {
|
|
1946
|
-
root = r;
|
|
1947
|
-
break;
|
|
1948
|
-
}
|
|
1949
|
-
}
|
|
1950
|
-
if (!job || !root) return;
|
|
1951
|
-
if (options.priority !== void 0) job.priority = options.priority;
|
|
1952
|
-
if (options.fps !== void 0) job.fps = options.fps;
|
|
1953
|
-
if (options.drop !== void 0) job.drop = options.drop;
|
|
1954
|
-
if (options.enabled !== void 0) {
|
|
1955
|
-
const wasEnabled = job.enabled;
|
|
1956
|
-
job.enabled = options.enabled;
|
|
1957
|
-
if (!wasEnabled && job.enabled) resetJobTiming(job);
|
|
1958
|
-
if (wasEnabled !== job.enabled) root.needsRebuild = true;
|
|
1959
|
-
}
|
|
1960
|
-
if (options.phase !== void 0 || options.before !== void 0 || options.after !== void 0) {
|
|
1961
|
-
if (options.phase) job.phase = options.phase;
|
|
1962
|
-
if (options.before !== void 0) job.before = this.normalizeConstraints(options.before);
|
|
1963
|
-
if (options.after !== void 0) job.after = this.normalizeConstraints(options.after);
|
|
1964
|
-
root.needsRebuild = true;
|
|
1965
|
-
}
|
|
1966
|
-
}
|
|
1967
|
-
//* Job State Management Methods ================================
|
|
1968
|
-
/**
|
|
1969
|
-
* Check if a job is currently paused (disabled).
|
|
1970
|
-
* @param {string} id - The job ID to check
|
|
1971
|
-
* @returns {boolean} True if the job exists and is paused
|
|
1972
|
-
*/
|
|
1973
|
-
isJobPaused(id) {
|
|
1974
|
-
for (const root of this.roots.values()) {
|
|
1975
|
-
const job = root.jobs.get(id);
|
|
1976
|
-
if (job) return !job.enabled;
|
|
1977
|
-
}
|
|
1978
|
-
return false;
|
|
1979
|
-
}
|
|
1980
|
-
/**
|
|
1981
|
-
* Subscribe to state changes for a specific job.
|
|
1982
|
-
* Listener is called when job is paused or resumed.
|
|
1983
|
-
* @param {string} id - The job ID to subscribe to
|
|
1984
|
-
* @param {() => void} listener - Callback invoked on state changes
|
|
1985
|
-
* @returns {() => void} Unsubscribe function
|
|
1986
|
-
*/
|
|
1987
|
-
subscribeJobState(id, listener) {
|
|
1988
|
-
if (!this.jobStateListeners.has(id)) {
|
|
1989
|
-
this.jobStateListeners.set(id, /* @__PURE__ */ new Set());
|
|
1990
|
-
}
|
|
1991
|
-
this.jobStateListeners.get(id).add(listener);
|
|
1992
|
-
return () => {
|
|
1993
|
-
this.jobStateListeners.get(id)?.delete(listener);
|
|
1994
|
-
if (this.jobStateListeners.get(id)?.size === 0) {
|
|
1995
|
-
this.jobStateListeners.delete(id);
|
|
1996
|
-
}
|
|
1997
|
-
};
|
|
1998
|
-
}
|
|
1999
|
-
/**
|
|
2000
|
-
* Notify all listeners that a job's state has changed.
|
|
2001
|
-
* @param {string} id - The job ID that changed
|
|
2002
|
-
* @returns {void}
|
|
2003
|
-
* @private
|
|
2004
|
-
*/
|
|
2005
|
-
notifyJobStateChange(id) {
|
|
2006
|
-
this.jobStateListeners.get(id)?.forEach((listener) => listener());
|
|
2007
|
-
}
|
|
2008
|
-
/**
|
|
2009
|
-
* Pause a job by ID (sets enabled=false).
|
|
2010
|
-
* Notifies any subscribed state listeners.
|
|
2011
|
-
* @param {string} id - The job ID to pause
|
|
2012
|
-
* @returns {void}
|
|
2013
|
-
*/
|
|
2014
|
-
pauseJob(id) {
|
|
2015
|
-
this.updateJob(id, { enabled: false });
|
|
2016
|
-
this.notifyJobStateChange(id);
|
|
2017
|
-
}
|
|
2018
|
-
/**
|
|
2019
|
-
* Resume a paused job by ID (sets enabled=true).
|
|
2020
|
-
* Resets job timing to prevent frame accumulation.
|
|
2021
|
-
* Notifies any subscribed state listeners.
|
|
2022
|
-
* @param {string} id - The job ID to resume
|
|
2023
|
-
* @returns {void}
|
|
2024
|
-
*/
|
|
2025
|
-
resumeJob(id) {
|
|
2026
|
-
this.updateJob(id, { enabled: true });
|
|
2027
|
-
this.notifyJobStateChange(id);
|
|
2028
|
-
}
|
|
2029
|
-
//* Frame Loop Control Methods ================================
|
|
2030
|
-
/**
|
|
2031
|
-
* Start the requestAnimationFrame loop.
|
|
2032
|
-
* Resets timing state (elapsedTime, frameCount) on start.
|
|
2033
|
-
* No-op if already running.
|
|
2034
|
-
* @returns {void}
|
|
2035
|
-
*/
|
|
2036
|
-
start() {
|
|
2037
|
-
if (this.loopState.running) return;
|
|
2038
|
-
const { elapsedTime, createdAt } = this.loopState;
|
|
2039
|
-
let adjustedCreated = 0;
|
|
2040
|
-
if (this.stoppedTime > 0) {
|
|
2041
|
-
adjustedCreated = createdAt - (performance.now() - this.stoppedTime);
|
|
2042
|
-
this.stoppedTime = 0;
|
|
2043
|
-
}
|
|
2044
|
-
Object.assign(this.loopState, {
|
|
2045
|
-
running: true,
|
|
2046
|
-
elapsedTime: elapsedTime ?? 0,
|
|
2047
|
-
lastTime: performance.now(),
|
|
2048
|
-
createdAt: adjustedCreated > 0 ? adjustedCreated : performance.now(),
|
|
2049
|
-
frameCount: 0,
|
|
2050
|
-
rafHandle: requestAnimationFrame(this.loop)
|
|
2051
|
-
});
|
|
2052
|
-
}
|
|
2053
|
-
/**
|
|
2054
|
-
* Stop the requestAnimationFrame loop.
|
|
2055
|
-
* Cancels any pending RAF callback.
|
|
2056
|
-
* No-op if not running.
|
|
2057
|
-
* @returns {void}
|
|
2058
|
-
*/
|
|
2059
|
-
stop() {
|
|
2060
|
-
if (!this.loopState.running) return;
|
|
2061
|
-
this.loopState.running = false;
|
|
2062
|
-
if (this.loopState.rafHandle !== null) {
|
|
2063
|
-
cancelAnimationFrame(this.loopState.rafHandle);
|
|
2064
|
-
this.loopState.rafHandle = null;
|
|
2065
|
-
}
|
|
2066
|
-
this.stoppedTime = performance.now();
|
|
2067
|
-
}
|
|
2068
|
-
/**
|
|
2069
|
-
* Request frames to be rendered in demand mode.
|
|
2070
|
-
* Accumulates pending frames (capped at 60) and starts the loop if not running.
|
|
2071
|
-
* No-op if frameloop is not 'demand'.
|
|
2072
|
-
* @param {number} [frames=1] - Number of frames to request
|
|
2073
|
-
* @param {boolean} [stackFrames=false] - Whether to add frames to existing pending count
|
|
2074
|
-
* - `false` (default): Sets pending frames to the specified value (replaces existing count)
|
|
2075
|
-
* - `true`: Adds frames to existing pending count (useful for accumulating invalidations)
|
|
2076
|
-
* @returns {void}
|
|
2077
|
-
* @example
|
|
2078
|
-
* // Request a single frame render
|
|
2079
|
-
* scheduler.invalidate();
|
|
2080
|
-
*
|
|
2081
|
-
* @example
|
|
2082
|
-
* // Request 5 frames (e.g., for animations)
|
|
2083
|
-
* scheduler.invalidate(5);
|
|
2084
|
-
*
|
|
2085
|
-
* @example
|
|
2086
|
-
* // Set pending frames to exactly 3 (don't stack with existing)
|
|
2087
|
-
* scheduler.invalidate(3, false);
|
|
2088
|
-
*
|
|
2089
|
-
* @example
|
|
2090
|
-
* // Add 2 more frames to existing pending count
|
|
2091
|
-
* scheduler.invalidate(2, true);
|
|
2092
|
-
*/
|
|
2093
|
-
invalidate(frames = 1, stackFrames = false) {
|
|
2094
|
-
if (this._frameloop !== "demand") return;
|
|
2095
|
-
const baseFrames = stackFrames ? this.pendingFrames : 0;
|
|
2096
|
-
this.pendingFrames = Math.min(60, baseFrames + frames);
|
|
2097
|
-
if (!this.loopState.running && this.pendingFrames > 0) this.start();
|
|
2098
|
-
}
|
|
2099
|
-
/**
|
|
2100
|
-
* Reset timing state for deterministic testing.
|
|
2101
|
-
* Preserves jobs and roots but resets lastTime, frameCount, elapsedTime, etc.
|
|
2102
|
-
* @returns {void}
|
|
2103
|
-
*/
|
|
2104
|
-
resetTiming() {
|
|
2105
|
-
this.loopState.lastTime = null;
|
|
2106
|
-
this.loopState.frameCount = 0;
|
|
2107
|
-
this.loopState.elapsedTime = 0;
|
|
2108
|
-
this.loopState.createdAt = performance.now();
|
|
2109
|
-
}
|
|
2110
|
-
//* Manual Stepping Methods ================================
|
|
2111
|
-
/**
|
|
2112
|
-
* Manually execute a single frame for all roots.
|
|
2113
|
-
* Useful for frameloop='never' mode or testing scenarios.
|
|
2114
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2115
|
-
* @returns {void}
|
|
2116
|
-
* @example
|
|
2117
|
-
* // Manual control mode
|
|
2118
|
-
* scheduler.frameloop = 'never';
|
|
2119
|
-
* scheduler.step(); // Execute one frame
|
|
2120
|
-
*/
|
|
2121
|
-
step(timestamp) {
|
|
2122
|
-
const now = timestamp ?? performance.now();
|
|
2123
|
-
this.executeFrame(now);
|
|
2124
|
-
}
|
|
2125
|
-
/**
|
|
2126
|
-
* Manually execute a single job by its ID.
|
|
2127
|
-
* Useful for testing individual job callbacks in isolation.
|
|
2128
|
-
* @param {string} id - The job ID to step
|
|
2129
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2130
|
-
* @returns {void}
|
|
2131
|
-
*/
|
|
2132
|
-
stepJob(id, timestamp) {
|
|
2133
|
-
let job;
|
|
2134
|
-
let root;
|
|
2135
|
-
for (const r of this.roots.values()) {
|
|
2136
|
-
job = r.jobs.get(id);
|
|
2137
|
-
if (job) {
|
|
2138
|
-
root = r;
|
|
2139
|
-
break;
|
|
2140
|
-
}
|
|
2141
|
-
}
|
|
2142
|
-
if (!job || !root) {
|
|
2143
|
-
console.warn(`[Scheduler] Job "${id}" not found`);
|
|
2144
|
-
return;
|
|
2145
|
-
}
|
|
2146
|
-
const now = timestamp ?? performance.now();
|
|
2147
|
-
const deltaMs = this.loopState.lastTime !== null ? now - this.loopState.lastTime : 0;
|
|
2148
|
-
const delta = deltaMs / 1e3;
|
|
2149
|
-
const elapsed = now - this.loopState.createdAt;
|
|
2150
|
-
const providedState = root.getState?.() ?? {};
|
|
2151
|
-
const frameState = {
|
|
2152
|
-
...providedState,
|
|
2153
|
-
time: now,
|
|
2154
|
-
delta,
|
|
2155
|
-
elapsed,
|
|
2156
|
-
frame: this.loopState.frameCount
|
|
2157
|
-
};
|
|
2158
|
-
try {
|
|
2159
|
-
job.callback(frameState, delta);
|
|
2160
|
-
} catch (error) {
|
|
2161
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2162
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2163
|
-
}
|
|
2164
|
-
}
|
|
2165
|
-
/**
|
|
2166
|
-
* Execute a single frame across all roots.
|
|
2167
|
-
* Order: globalBefore → each root's jobs → globalAfter
|
|
2168
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2169
|
-
* @returns {void}
|
|
2170
|
-
* @private
|
|
2171
|
-
*/
|
|
2172
|
-
executeFrame(timestamp) {
|
|
2173
|
-
const deltaMs = this.loopState.lastTime !== null ? timestamp - this.loopState.lastTime : 0;
|
|
2174
|
-
const delta = deltaMs / 1e3;
|
|
2175
|
-
this.loopState.lastTime = timestamp;
|
|
2176
|
-
this.loopState.frameCount++;
|
|
2177
|
-
this.loopState.elapsedTime += deltaMs;
|
|
2178
|
-
this.runGlobalJobs(this.globalBeforeJobs, timestamp);
|
|
2179
|
-
for (const root of this.roots.values()) {
|
|
2180
|
-
this.tickRoot(root, timestamp, delta);
|
|
2181
|
-
}
|
|
2182
|
-
this.runGlobalJobs(this.globalAfterJobs, timestamp);
|
|
2183
|
-
}
|
|
2184
|
-
/**
|
|
2185
|
-
* Run all global jobs from a job map.
|
|
2186
|
-
* Catches and logs errors without stopping execution.
|
|
2187
|
-
* @param {Map<string, GlobalJob>} jobs - The global jobs map to execute
|
|
2188
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2189
|
-
* @returns {void}
|
|
2190
|
-
* @private
|
|
2191
|
-
*/
|
|
2192
|
-
runGlobalJobs(jobs, timestamp) {
|
|
2193
|
-
for (const job of jobs.values()) {
|
|
2194
|
-
try {
|
|
2195
|
-
job.callback(timestamp);
|
|
2196
|
-
} catch (error) {
|
|
2197
|
-
console.error(`[Scheduler] Error in global job "${job.id}":`, error);
|
|
2198
|
-
}
|
|
2199
|
-
}
|
|
2200
|
-
}
|
|
2201
|
-
/**
|
|
2202
|
-
* Execute all jobs for a single root in sorted order.
|
|
2203
|
-
* Rebuilds sorted job list if needed, then dispatches each job.
|
|
2204
|
-
* Errors are caught and propagated via triggerError.
|
|
2205
|
-
* @param {RootEntry} root - The root entry to tick
|
|
2206
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2207
|
-
* @param {number} delta - Time since last frame in seconds
|
|
2208
|
-
* @returns {void}
|
|
2209
|
-
* @private
|
|
2210
|
-
*/
|
|
2211
|
-
tickRoot(root, timestamp, delta) {
|
|
2212
|
-
if (root.needsRebuild) {
|
|
2213
|
-
root.sortedJobs = rebuildSortedJobs(root.jobs, this.phaseGraph);
|
|
2214
|
-
root.needsRebuild = false;
|
|
2215
|
-
}
|
|
2216
|
-
const providedState = root.getState?.() ?? {};
|
|
2217
|
-
const frameState = {
|
|
2218
|
-
...providedState,
|
|
2219
|
-
time: timestamp,
|
|
2220
|
-
delta,
|
|
2221
|
-
elapsed: this.loopState.elapsedTime / 1e3,
|
|
2222
|
-
// Convert ms to seconds
|
|
2223
|
-
frame: this.loopState.frameCount
|
|
2224
|
-
};
|
|
2225
|
-
for (const job of root.sortedJobs) {
|
|
2226
|
-
if (!shouldRun(job, timestamp)) continue;
|
|
2227
|
-
try {
|
|
2228
|
-
job.callback(frameState, delta);
|
|
2229
|
-
} catch (error) {
|
|
2230
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2231
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2232
|
-
}
|
|
2233
|
-
}
|
|
2234
|
-
}
|
|
2235
|
-
//* Debug & Inspection Methods ================================
|
|
2236
|
-
/**
|
|
2237
|
-
* Get the total number of registered jobs across all roots.
|
|
2238
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2239
|
-
* @returns {number} Total job count
|
|
2240
|
-
*/
|
|
2241
|
-
getJobCount() {
|
|
2242
|
-
let count = 0;
|
|
2243
|
-
for (const root of this.roots.values()) {
|
|
2244
|
-
count += root.jobs.size;
|
|
2245
|
-
}
|
|
2246
|
-
return count + this.globalBeforeJobs.size + this.globalAfterJobs.size;
|
|
2247
|
-
}
|
|
2248
|
-
/**
|
|
2249
|
-
* Get all registered job IDs across all roots.
|
|
2250
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2251
|
-
* @returns {string[]} Array of all job IDs
|
|
2252
|
-
*/
|
|
2253
|
-
getJobIds() {
|
|
2254
|
-
const ids = [];
|
|
2255
|
-
for (const root of this.roots.values()) {
|
|
2256
|
-
ids.push(...root.jobs.keys());
|
|
2257
|
-
}
|
|
2258
|
-
ids.push(...this.globalBeforeJobs.keys());
|
|
2259
|
-
ids.push(...this.globalAfterJobs.keys());
|
|
2260
|
-
return ids;
|
|
2261
|
-
}
|
|
2262
|
-
/**
|
|
2263
|
-
* Get the number of registered roots (Canvas instances).
|
|
2264
|
-
* @returns {number} Number of registered roots
|
|
2265
|
-
*/
|
|
2266
|
-
getRootCount() {
|
|
2267
|
-
return this.roots.size;
|
|
2268
|
-
}
|
|
2269
|
-
/**
|
|
2270
|
-
* Check if any user (non-system) jobs are registered in a specific phase.
|
|
2271
|
-
* Used by the default render job to know if a user has taken over rendering.
|
|
2272
|
-
*
|
|
2273
|
-
* @param phase The phase to check
|
|
2274
|
-
* @param rootId Optional root ID to check (checks all roots if not provided)
|
|
2275
|
-
* @returns true if any user jobs exist in the phase
|
|
2276
|
-
*/
|
|
2277
|
-
hasUserJobsInPhase(phase, rootId) {
|
|
2278
|
-
const rootsToCheck = rootId ? [this.roots.get(rootId)].filter(Boolean) : Array.from(this.roots.values());
|
|
2279
|
-
return rootsToCheck.some((root) => {
|
|
2280
|
-
if (!root) return false;
|
|
2281
|
-
for (const job of root.jobs.values()) {
|
|
2282
|
-
if (job.phase === phase && !job.system && job.enabled) return true;
|
|
2283
|
-
}
|
|
2284
|
-
return false;
|
|
2285
|
-
});
|
|
2286
|
-
}
|
|
2287
|
-
//* Utility Methods ================================
|
|
2288
|
-
/**
|
|
2289
|
-
* Generate a unique root ID for automatic root registration.
|
|
2290
|
-
* @returns {string} A unique root ID in the format 'root_N'
|
|
2291
|
-
*/
|
|
2292
|
-
generateRootId() {
|
|
2293
|
-
return `root_${this.nextRootIndex++}`;
|
|
2294
|
-
}
|
|
2295
|
-
/**
|
|
2296
|
-
* Generate a unique job ID.
|
|
2297
|
-
* @returns {string} A unique job ID in the format 'job_N'
|
|
2298
|
-
* @private
|
|
2299
|
-
*/
|
|
2300
|
-
generateJobId() {
|
|
2301
|
-
return `job_${this.nextJobIndex}`;
|
|
2302
|
-
}
|
|
2303
|
-
/**
|
|
2304
|
-
* Normalize before/after constraints to a Set.
|
|
2305
|
-
* Handles undefined, single string, or array inputs.
|
|
2306
|
-
* @param {string | string[] | undefined} value - The constraint value(s)
|
|
2307
|
-
* @returns {Set<string>} Normalized Set of constraint strings
|
|
2308
|
-
* @private
|
|
2309
|
-
*/
|
|
2310
|
-
normalizeConstraints(value) {
|
|
2311
|
-
if (!value) return /* @__PURE__ */ new Set();
|
|
2312
|
-
if (Array.isArray(value)) return new Set(value);
|
|
2313
|
-
return /* @__PURE__ */ new Set([value]);
|
|
2314
|
-
}
|
|
2315
|
-
};
|
|
2316
|
-
//* Static State & Methods (Singleton Usage) ================================
|
|
2317
|
-
//* Cross-Bundle Singleton Key ==============================
|
|
2318
|
-
// Use Symbol.for() to ensure scheduler is shared across bundle boundaries
|
|
2319
|
-
// This prevents issues when mixing imports from @react-three/fiber and @react-three/fiber/webgpu
|
|
2320
|
-
__publicField(_Scheduler, "INSTANCE_KEY", Symbol.for("@react-three/fiber.scheduler"));
|
|
2321
|
-
let Scheduler = _Scheduler;
|
|
2322
|
-
const getScheduler = () => Scheduler.get();
|
|
2323
|
-
if (hmrData) {
|
|
2324
|
-
hmrData.accept?.();
|
|
2325
|
-
}
|
|
2326
|
-
|
|
2327
2001
|
function useFrame(callback, priorityOrOptions) {
|
|
2328
2002
|
const store = React.useContext(context);
|
|
2329
2003
|
const isInsideCanvas = store !== null;
|
|
@@ -2396,20 +2070,14 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2396
2070
|
}
|
|
2397
2071
|
};
|
|
2398
2072
|
} else {
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
unregisterJob = registerOutside();
|
|
2408
|
-
});
|
|
2409
|
-
return () => {
|
|
2410
|
-
unsubReady();
|
|
2411
|
-
unregisterJob?.();
|
|
2412
|
-
};
|
|
2073
|
+
return scheduler.register(
|
|
2074
|
+
(state, delta) => {
|
|
2075
|
+
const frameState = state;
|
|
2076
|
+
if (!frameState.renderer) return;
|
|
2077
|
+
callbackRef.current?.(frameState, delta);
|
|
2078
|
+
},
|
|
2079
|
+
{ id, ...options }
|
|
2080
|
+
);
|
|
2413
2081
|
}
|
|
2414
2082
|
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2415
2083
|
const isPaused = React.useSyncExternalStore(
|
|
@@ -2500,22 +2168,29 @@ function buildFromCache(input, textureCache) {
|
|
|
2500
2168
|
function useTexture(input, optionsOrOnLoad) {
|
|
2501
2169
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
2502
2170
|
const store = useStore();
|
|
2503
|
-
const textureCache = useThree((state) => state.textures);
|
|
2504
2171
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
2505
|
-
const { onLoad, cache =
|
|
2172
|
+
const { onLoad, cache = true } = options;
|
|
2173
|
+
const onLoadRef = useRef(onLoad);
|
|
2174
|
+
onLoadRef.current = onLoad;
|
|
2175
|
+
const onLoadCalledForRef = useRef(null);
|
|
2506
2176
|
const urls = useMemo(() => getUrls(input), [input]);
|
|
2507
2177
|
const cachedResult = useMemo(() => {
|
|
2508
2178
|
if (!cache) return null;
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2179
|
+
const textures = store.getState().textures;
|
|
2180
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2181
|
+
return buildFromCache(input, textures);
|
|
2182
|
+
}, [cache, urls, input, store]);
|
|
2512
2183
|
const loadedTextures = useLoader(
|
|
2513
2184
|
TextureLoader,
|
|
2514
2185
|
IsObject(input) ? Object.values(input) : input
|
|
2515
2186
|
);
|
|
2187
|
+
const inputKey = urls.join("\0");
|
|
2516
2188
|
useLayoutEffect(() => {
|
|
2517
|
-
if (
|
|
2518
|
-
|
|
2189
|
+
if (cachedResult) return;
|
|
2190
|
+
if (onLoadCalledForRef.current === inputKey) return;
|
|
2191
|
+
onLoadCalledForRef.current = inputKey;
|
|
2192
|
+
onLoadRef.current?.(loadedTextures);
|
|
2193
|
+
}, [cachedResult, loadedTextures, inputKey]);
|
|
2519
2194
|
useEffect(() => {
|
|
2520
2195
|
if (cachedResult) return;
|
|
2521
2196
|
if ("initTexture" in renderer) {
|
|
@@ -2548,8 +2223,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
2548
2223
|
}, [input, loadedTextures, cachedResult]);
|
|
2549
2224
|
useEffect(() => {
|
|
2550
2225
|
if (!cache) return;
|
|
2551
|
-
if (cachedResult) return;
|
|
2552
|
-
const set = store.setState;
|
|
2553
2226
|
const urlTextureMap = [];
|
|
2554
2227
|
if (typeof input === "string") {
|
|
2555
2228
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -2563,18 +2236,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
2563
2236
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
2564
2237
|
}
|
|
2565
2238
|
}
|
|
2566
|
-
|
|
2567
|
-
const
|
|
2568
|
-
let
|
|
2239
|
+
store.setState((state) => {
|
|
2240
|
+
const refs = new Map(state._textureRefs);
|
|
2241
|
+
let textures = state.textures;
|
|
2242
|
+
let added = false;
|
|
2569
2243
|
for (const [url, texture] of urlTextureMap) {
|
|
2570
|
-
if (!
|
|
2571
|
-
|
|
2572
|
-
|
|
2244
|
+
if (!textures.has(url)) {
|
|
2245
|
+
if (!added) {
|
|
2246
|
+
textures = new Map(textures);
|
|
2247
|
+
added = true;
|
|
2248
|
+
}
|
|
2249
|
+
textures.set(url, texture);
|
|
2573
2250
|
}
|
|
2251
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
2574
2252
|
}
|
|
2575
|
-
return
|
|
2253
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
2254
|
+
});
|
|
2255
|
+
return () => store.setState((state) => {
|
|
2256
|
+
const refs = new Map(state._textureRefs);
|
|
2257
|
+
for (const [url] of urlTextureMap) {
|
|
2258
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2259
|
+
if (next <= 0) refs.delete(url);
|
|
2260
|
+
else refs.set(url, next);
|
|
2261
|
+
}
|
|
2262
|
+
return { _textureRefs: refs };
|
|
2576
2263
|
});
|
|
2577
|
-
}, [cache, input, mappedTextures, store
|
|
2264
|
+
}, [cache, input, mappedTextures, store]);
|
|
2578
2265
|
return mappedTextures;
|
|
2579
2266
|
}
|
|
2580
2267
|
useTexture.preload = (url) => useLoader.preload(TextureLoader, url);
|
|
@@ -2590,108 +2277,92 @@ const Texture = ({
|
|
|
2590
2277
|
return /* @__PURE__ */ jsx(Fragment, { children: children?.(ret) });
|
|
2591
2278
|
};
|
|
2592
2279
|
|
|
2593
|
-
function
|
|
2594
|
-
if (entry instanceof Texture$1) return entry;
|
|
2595
|
-
if (entry && typeof entry === "object" && "value" in entry && entry.value instanceof Texture$1) {
|
|
2596
|
-
return entry.value;
|
|
2597
|
-
}
|
|
2598
|
-
return null;
|
|
2599
|
-
}
|
|
2600
|
-
function useTextures() {
|
|
2280
|
+
function useTextures(selector) {
|
|
2601
2281
|
const store = useStore();
|
|
2602
|
-
|
|
2603
|
-
const set = store.setState;
|
|
2282
|
+
const registry = useMemo(() => {
|
|
2604
2283
|
const getState = store.getState;
|
|
2605
|
-
const
|
|
2606
|
-
|
|
2607
|
-
const newMap = new Map(state.textures);
|
|
2608
|
-
newMap.set(key, value);
|
|
2609
|
-
return { textures: newMap };
|
|
2610
|
-
});
|
|
2611
|
-
};
|
|
2612
|
-
const addMultiple = (items) => {
|
|
2613
|
-
set((state) => {
|
|
2614
|
-
const newMap = new Map(state.textures);
|
|
2615
|
-
const entries = items instanceof Map ? items.entries() : Object.entries(items);
|
|
2616
|
-
for (const [key, value] of entries) {
|
|
2617
|
-
newMap.set(key, value);
|
|
2618
|
-
}
|
|
2619
|
-
return { textures: newMap };
|
|
2620
|
-
});
|
|
2621
|
-
};
|
|
2622
|
-
const remove = (key) => {
|
|
2623
|
-
set((state) => {
|
|
2624
|
-
const newMap = new Map(state.textures);
|
|
2625
|
-
newMap.delete(key);
|
|
2626
|
-
return { textures: newMap };
|
|
2627
|
-
});
|
|
2628
|
-
};
|
|
2629
|
-
const removeMultiple = (keys) => {
|
|
2630
|
-
set((state) => {
|
|
2631
|
-
const newMap = new Map(state.textures);
|
|
2632
|
-
for (const key of keys) newMap.delete(key);
|
|
2633
|
-
return { textures: newMap };
|
|
2634
|
-
});
|
|
2635
|
-
};
|
|
2636
|
-
const dispose = (key) => {
|
|
2637
|
-
const entry = getState().textures.get(key);
|
|
2638
|
-
if (entry) {
|
|
2639
|
-
const tex = getTextureValue(entry);
|
|
2640
|
-
tex?.dispose();
|
|
2641
|
-
}
|
|
2642
|
-
remove(key);
|
|
2643
|
-
};
|
|
2644
|
-
const disposeMultiple = (keys) => {
|
|
2645
|
-
const textures = getState().textures;
|
|
2646
|
-
for (const key of keys) {
|
|
2647
|
-
const entry = textures.get(key);
|
|
2648
|
-
if (entry) {
|
|
2649
|
-
const tex = getTextureValue(entry);
|
|
2650
|
-
tex?.dispose();
|
|
2651
|
-
}
|
|
2652
|
-
}
|
|
2653
|
-
removeMultiple(keys);
|
|
2654
|
-
};
|
|
2655
|
-
const disposeAll = () => {
|
|
2656
|
-
const textures = getState().textures;
|
|
2657
|
-
for (const entry of textures.values()) {
|
|
2658
|
-
const tex = getTextureValue(entry);
|
|
2659
|
-
tex?.dispose();
|
|
2660
|
-
}
|
|
2661
|
-
set({ textures: /* @__PURE__ */ new Map() });
|
|
2662
|
-
};
|
|
2284
|
+
const setState = store.setState;
|
|
2285
|
+
const getOne = (key) => getState().textures.get(key);
|
|
2663
2286
|
return {
|
|
2664
|
-
|
|
2665
|
-
get textures() {
|
|
2287
|
+
get all() {
|
|
2666
2288
|
return getState().textures;
|
|
2667
2289
|
},
|
|
2668
|
-
|
|
2669
|
-
|
|
2290
|
+
get(input) {
|
|
2291
|
+
if (typeof input === "string") return getOne(input);
|
|
2292
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2293
|
+
const out = {};
|
|
2294
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2295
|
+
return out;
|
|
2296
|
+
},
|
|
2670
2297
|
has: (key) => getState().textures.has(key),
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2298
|
+
add(keyOrRecord, texture) {
|
|
2299
|
+
setState((state) => {
|
|
2300
|
+
const textures = new Map(state.textures);
|
|
2301
|
+
if (typeof keyOrRecord === "string") {
|
|
2302
|
+
textures.set(keyOrRecord, texture);
|
|
2303
|
+
} else {
|
|
2304
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2305
|
+
}
|
|
2306
|
+
return { textures };
|
|
2307
|
+
});
|
|
2308
|
+
},
|
|
2309
|
+
dispose(key, options) {
|
|
2310
|
+
const state = getState();
|
|
2311
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2312
|
+
if (refs > 0 && !options?.force) {
|
|
2313
|
+
console.warn(
|
|
2314
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2315
|
+
);
|
|
2316
|
+
return false;
|
|
2317
|
+
}
|
|
2318
|
+
state.textures.get(key)?.dispose();
|
|
2319
|
+
setState((s) => {
|
|
2320
|
+
const textures = new Map(s.textures);
|
|
2321
|
+
textures.delete(key);
|
|
2322
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2323
|
+
nextRefs.delete(key);
|
|
2324
|
+
return { textures, _textureRefs: nextRefs };
|
|
2325
|
+
});
|
|
2326
|
+
return true;
|
|
2327
|
+
},
|
|
2328
|
+
disposeAll() {
|
|
2329
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2330
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2331
|
+
}
|
|
2681
2332
|
};
|
|
2682
2333
|
}, [store]);
|
|
2334
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2335
|
+
const selected = useThree(subscribe);
|
|
2336
|
+
return selector ? selected : registry;
|
|
2683
2337
|
}
|
|
2684
2338
|
|
|
2685
|
-
function useRenderTarget(
|
|
2339
|
+
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
2686
2340
|
const isLegacy = useThree((s) => s.isLegacy);
|
|
2687
2341
|
const size = useThree((s) => s.size);
|
|
2342
|
+
let width;
|
|
2343
|
+
let height;
|
|
2344
|
+
let opts;
|
|
2345
|
+
if (typeof widthOrOptions === "object") {
|
|
2346
|
+
opts = widthOrOptions;
|
|
2347
|
+
} else if (typeof widthOrOptions === "number") {
|
|
2348
|
+
width = widthOrOptions;
|
|
2349
|
+
if (typeof heightOrOptions === "object") {
|
|
2350
|
+
height = widthOrOptions;
|
|
2351
|
+
opts = heightOrOptions;
|
|
2352
|
+
} else if (typeof heightOrOptions === "number") {
|
|
2353
|
+
height = heightOrOptions;
|
|
2354
|
+
opts = options;
|
|
2355
|
+
} else {
|
|
2356
|
+
height = widthOrOptions;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2688
2359
|
return useMemo(() => {
|
|
2689
2360
|
const w = width ?? size.width;
|
|
2690
2361
|
const h = height ?? size.height;
|
|
2691
2362
|
{
|
|
2692
|
-
return isLegacy ? new WebGLRenderTarget(w, h,
|
|
2363
|
+
return isLegacy ? new WebGLRenderTarget(w, h, opts) : new RenderTarget(w, h, opts);
|
|
2693
2364
|
}
|
|
2694
|
-
}, [width, height, size.width, size.height,
|
|
2365
|
+
}, [width, height, size.width, size.height, opts, isLegacy]);
|
|
2695
2366
|
}
|
|
2696
2367
|
|
|
2697
2368
|
function useStore() {
|
|
@@ -2741,28 +2412,18 @@ function addTail(callback) {
|
|
|
2741
2412
|
function invalidate(state, frames = 1, stackFrames = false) {
|
|
2742
2413
|
getScheduler().invalidate(frames, stackFrames);
|
|
2743
2414
|
}
|
|
2744
|
-
function advance(timestamp
|
|
2415
|
+
function advance(timestamp) {
|
|
2745
2416
|
getScheduler().step(timestamp);
|
|
2746
2417
|
}
|
|
2747
2418
|
|
|
2748
|
-
const version = "10.0.0-alpha.
|
|
2419
|
+
const version = "10.0.0-alpha.3";
|
|
2749
2420
|
const packageData = {
|
|
2750
2421
|
version: version};
|
|
2751
2422
|
|
|
2752
2423
|
function Xb(Tt) {
|
|
2753
2424
|
return Tt && Tt.__esModule && Object.prototype.hasOwnProperty.call(Tt, "default") ? Tt.default : Tt;
|
|
2754
2425
|
}
|
|
2755
|
-
var Rm = { exports: {} }, Og = { exports: {} };
|
|
2756
|
-
/**
|
|
2757
|
-
* @license React
|
|
2758
|
-
* react-reconciler.production.js
|
|
2759
|
-
*
|
|
2760
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2761
|
-
*
|
|
2762
|
-
* This source code is licensed under the MIT license found in the
|
|
2763
|
-
* LICENSE file in the root directory of this source tree.
|
|
2764
|
-
*/
|
|
2765
|
-
var _b;
|
|
2426
|
+
var Rm = { exports: {} }, Og = { exports: {} }, _b;
|
|
2766
2427
|
function Kb() {
|
|
2767
2428
|
return _b || (_b = 1, (function(Tt) {
|
|
2768
2429
|
Tt.exports = function(m) {
|
|
@@ -3834,7 +3495,6 @@ Error generating stack: ` + l.message + `
|
|
|
3834
3495
|
if (J === cl || J === jc) throw J;
|
|
3835
3496
|
var Ge = Yn(29, J, null, P.mode);
|
|
3836
3497
|
return Ge.lanes = H, Ge.return = P, Ge;
|
|
3837
|
-
} finally {
|
|
3838
3498
|
}
|
|
3839
3499
|
};
|
|
3840
3500
|
}
|
|
@@ -4488,7 +4148,6 @@ Error generating stack: ` + l.message + `
|
|
|
4488
4148
|
var h = r.lastRenderedState, y = d(h, a);
|
|
4489
4149
|
if (c.hasEagerState = true, c.eagerState = y, jn(y, h)) return go(t, r, c, 0), Ne === null && Bn(), false;
|
|
4490
4150
|
} catch {
|
|
4491
|
-
} finally {
|
|
4492
4151
|
}
|
|
4493
4152
|
if (a = yo(t, r, c, l), a !== null) return nt(a, t, l), ns(a, r, l), true;
|
|
4494
4153
|
}
|
|
@@ -6909,10 +6568,7 @@ Error generating stack: ` + l.message + `
|
|
|
6909
6568
|
function vr(t, r) {
|
|
6910
6569
|
Sf(t, r), (t = t.alternate) && Sf(t, r);
|
|
6911
6570
|
}
|
|
6912
|
-
var ie = {}, Fm = React__default, tt = Tb, Lt = Object.assign, hc = Symbol.for("react.element"), zs = Symbol.for("react.transitional.element"), sa = Symbol.for("react.portal"), $a = Symbol.for("react.fragment"), kf = Symbol.for("react.strict_mode"), Cs = Symbol.for("react.profiler"), mc = Symbol.for("react.consumer"), Io = Symbol.for("react.context"), Zi = Symbol.for("react.forward_ref"), Va = Symbol.for("react.suspense"), Te = Symbol.for("react.suspense_list"), wf = Symbol.for("react.memo"), ua = Symbol.for("react.lazy");
|
|
6913
|
-
var gc = Symbol.for("react.activity");
|
|
6914
|
-
var $r = Symbol.for("react.memo_cache_sentinel");
|
|
6915
|
-
var Pf = Symbol.iterator, xf = Symbol.for("react.client.reference"), ca = Array.isArray, M = Fm.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, Yp = m.rendererVersion, zf = m.rendererPackageName, Cf = m.extraDevToolsConfig, Ts = m.getPublicInstance, Hm = m.getRootHostContext, Xp = m.getChildHostContext, Am = m.prepareForCommit, _s = m.resetAfterCommit, Vr = m.createInstance;
|
|
6571
|
+
var ie = {}, Fm = React__default, tt = Tb, Lt = Object.assign, hc = Symbol.for("react.element"), zs = Symbol.for("react.transitional.element"), sa = Symbol.for("react.portal"), $a = Symbol.for("react.fragment"), kf = Symbol.for("react.strict_mode"), Cs = Symbol.for("react.profiler"), mc = Symbol.for("react.consumer"), Io = Symbol.for("react.context"), Zi = Symbol.for("react.forward_ref"), Va = Symbol.for("react.suspense"), Te = Symbol.for("react.suspense_list"), wf = Symbol.for("react.memo"), ua = Symbol.for("react.lazy"), gc = Symbol.for("react.activity"), $r = Symbol.for("react.memo_cache_sentinel"), Pf = Symbol.iterator, xf = Symbol.for("react.client.reference"), ca = Array.isArray, M = Fm.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, Yp = m.rendererVersion, zf = m.rendererPackageName, Cf = m.extraDevToolsConfig, Ts = m.getPublicInstance, Hm = m.getRootHostContext, Xp = m.getChildHostContext, Am = m.prepareForCommit, _s = m.resetAfterCommit, Vr = m.createInstance;
|
|
6916
6572
|
m.cloneMutableInstance;
|
|
6917
6573
|
var yc = m.appendInitialChild, Kp = m.finalizeInitialChildren, Rs = m.shouldSetTextContent, bc = m.createTextInstance;
|
|
6918
6574
|
m.cloneMutableTextInstance;
|
|
@@ -7281,17 +6937,7 @@ No matching component was found for:
|
|
|
7281
6937
|
}, Tt.exports.default = Tt.exports, Object.defineProperty(Tt.exports, "__esModule", { value: true });
|
|
7282
6938
|
})(Og)), Og.exports;
|
|
7283
6939
|
}
|
|
7284
|
-
var Mg = { exports: {} };
|
|
7285
|
-
/**
|
|
7286
|
-
* @license React
|
|
7287
|
-
* react-reconciler.development.js
|
|
7288
|
-
*
|
|
7289
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
7290
|
-
*
|
|
7291
|
-
* This source code is licensed under the MIT license found in the
|
|
7292
|
-
* LICENSE file in the root directory of this source tree.
|
|
7293
|
-
*/
|
|
7294
|
-
var Rb;
|
|
6940
|
+
var Mg = { exports: {} }, Rb;
|
|
7295
6941
|
function e0() {
|
|
7296
6942
|
return Rb || (Rb = 1, (function(Tt) {
|
|
7297
6943
|
process.env.NODE_ENV !== "production" && (Tt.exports = function(m) {
|
|
@@ -13058,10 +12704,7 @@ Check the render method of %s.`, G(di) || "Unknown")), i = zo(n), i.payload = {
|
|
|
13058
12704
|
function Ic() {
|
|
13059
12705
|
return di;
|
|
13060
12706
|
}
|
|
13061
|
-
var le = {}, qm = React__default, St = Tb, ze = Object.assign, Uh = Symbol.for("react.element"), Ho = Symbol.for("react.transitional.element"), Ao = Symbol.for("react.portal"), ol = Symbol.for("react.fragment"), Lc = Symbol.for("react.strict_mode"), Uf = Symbol.for("react.profiler"), ei = Symbol.for("react.consumer"), on = Symbol.for("react.context"), jn = Symbol.for("react.forward_ref"), Nc = Symbol.for("react.suspense"), Bf = Symbol.for("react.suspense_list"), al = Symbol.for("react.memo"), kt = Symbol.for("react.lazy");
|
|
13062
|
-
var Ds = Symbol.for("react.activity");
|
|
13063
|
-
var Bh = Symbol.for("react.memo_cache_sentinel");
|
|
13064
|
-
var ni = Symbol.iterator, il = Symbol.for("react.client.reference"), fn = Array.isArray, x = qm.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, Jt = m.rendererVersion, Zt = m.rendererPackageName, jo = m.extraDevToolsConfig, ot = m.getPublicInstance, Zr = m.getRootHostContext, Dn = m.getChildHostContext, Ws = m.prepareForCommit, pa = m.resetAfterCommit, Fc = m.createInstance;
|
|
12707
|
+
var le = {}, qm = React__default, St = Tb, ze = Object.assign, Uh = Symbol.for("react.element"), Ho = Symbol.for("react.transitional.element"), Ao = Symbol.for("react.portal"), ol = Symbol.for("react.fragment"), Lc = Symbol.for("react.strict_mode"), Uf = Symbol.for("react.profiler"), ei = Symbol.for("react.consumer"), on = Symbol.for("react.context"), jn = Symbol.for("react.forward_ref"), Nc = Symbol.for("react.suspense"), Bf = Symbol.for("react.suspense_list"), al = Symbol.for("react.memo"), kt = Symbol.for("react.lazy"), Ds = Symbol.for("react.activity"), Bh = Symbol.for("react.memo_cache_sentinel"), ni = Symbol.iterator, il = Symbol.for("react.client.reference"), fn = Array.isArray, x = qm.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, Jt = m.rendererVersion, Zt = m.rendererPackageName, jo = m.extraDevToolsConfig, ot = m.getPublicInstance, Zr = m.getRootHostContext, Dn = m.getChildHostContext, Ws = m.prepareForCommit, pa = m.resetAfterCommit, Fc = m.createInstance;
|
|
13065
12708
|
m.cloneMutableInstance;
|
|
13066
12709
|
var bn = m.appendInitialChild, Ue = m.finalizeInitialChildren, ue = m.shouldSetTextContent, Do = m.createTextInstance;
|
|
13067
12710
|
m.cloneMutableTextInstance;
|
|
@@ -14029,15 +13672,6 @@ function n0() {
|
|
|
14029
13672
|
var t0 = n0();
|
|
14030
13673
|
const r0 = Xb(t0);
|
|
14031
13674
|
|
|
14032
|
-
/**
|
|
14033
|
-
* @license React
|
|
14034
|
-
* react-reconciler-constants.production.js
|
|
14035
|
-
*
|
|
14036
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
14037
|
-
*
|
|
14038
|
-
* This source code is licensed under the MIT license found in the
|
|
14039
|
-
* LICENSE file in the root directory of this source tree.
|
|
14040
|
-
*/
|
|
14041
13675
|
const t = 1, o = 8, r = 32, e = 2;
|
|
14042
13676
|
|
|
14043
13677
|
function createReconciler(config) {
|
|
@@ -14064,10 +13698,11 @@ function extend(objects) {
|
|
|
14064
13698
|
function validateInstance(type, props) {
|
|
14065
13699
|
const name = toPascalCase(type);
|
|
14066
13700
|
const target = catalogue[name];
|
|
14067
|
-
if (type !== "primitive" && !target)
|
|
13701
|
+
if (type !== "primitive" && !target) {
|
|
14068
13702
|
throw new Error(
|
|
14069
13703
|
`R3F: ${name} is not part of the THREE namespace! Did you forget to extend? See: https://docs.pmnd.rs/react-three-fiber/api/objects#using-3rd-party-objects-declaratively`
|
|
14070
13704
|
);
|
|
13705
|
+
}
|
|
14071
13706
|
if (type === "primitive" && !props.object) throw new Error(`R3F: Primitives without 'object' are invalid!`);
|
|
14072
13707
|
if (props.args !== void 0 && !Array.isArray(props.args)) throw new Error("R3F: The args prop must be an array!");
|
|
14073
13708
|
}
|
|
@@ -14220,6 +13855,7 @@ function swapInstances() {
|
|
|
14220
13855
|
}
|
|
14221
13856
|
}
|
|
14222
13857
|
if (instance.isHidden) unhideInstance(instance);
|
|
13858
|
+
if (isObject3D(instance.object)) removeInteractivity(findInitialRoot(instance), instance.object);
|
|
14223
13859
|
if (instance.object.__r3f) delete instance.object.__r3f;
|
|
14224
13860
|
if (instance.type !== "primitive") disposeOnIdle(instance.object);
|
|
14225
13861
|
}
|
|
@@ -14231,6 +13867,9 @@ function swapInstances() {
|
|
|
14231
13867
|
instance.object = instance.props.object ?? new target(...instance.props.args ?? []);
|
|
14232
13868
|
instance.object.__r3f = instance;
|
|
14233
13869
|
setFiberRef(fiber, instance.object);
|
|
13870
|
+
delete instance.appliedOnce;
|
|
13871
|
+
instance.eventCount = 0;
|
|
13872
|
+
instance.handlers = {};
|
|
14234
13873
|
applyProps(instance.object, instance.props);
|
|
14235
13874
|
if (instance.props.attach) {
|
|
14236
13875
|
attach(parent, instance);
|
|
@@ -14304,8 +13943,22 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14304
13943
|
const isTailSibling = fiber.sibling === null || (fiber.flags & Update) === NoFlags;
|
|
14305
13944
|
if (isTailSibling) swapInstances();
|
|
14306
13945
|
},
|
|
14307
|
-
finalizeInitialChildren: () =>
|
|
14308
|
-
|
|
13946
|
+
finalizeInitialChildren: (instance) => {
|
|
13947
|
+
for (const prop in instance.props) {
|
|
13948
|
+
if (isFromRef(instance.props[prop])) return true;
|
|
13949
|
+
}
|
|
13950
|
+
return false;
|
|
13951
|
+
},
|
|
13952
|
+
commitMount(instance) {
|
|
13953
|
+
const resolved = {};
|
|
13954
|
+
for (const prop in instance.props) {
|
|
13955
|
+
const value = instance.props[prop];
|
|
13956
|
+
if (isFromRef(value)) {
|
|
13957
|
+
const ref = value[FROM_REF];
|
|
13958
|
+
if (ref.current != null) resolved[prop] = ref.current;
|
|
13959
|
+
}
|
|
13960
|
+
}
|
|
13961
|
+
if (Object.keys(resolved).length) applyProps(instance.object, resolved);
|
|
14309
13962
|
},
|
|
14310
13963
|
getPublicInstance: (instance) => instance?.object,
|
|
14311
13964
|
prepareForCommit: () => null,
|
|
@@ -14432,11 +14085,15 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14432
14085
|
cloneMutableTextInstance(textInstance) {
|
|
14433
14086
|
return textInstance;
|
|
14434
14087
|
},
|
|
14088
|
+
// React 19.2 added these for root-level view transitions. They only run for hosts that opt into
|
|
14089
|
+
// view transitions by returning a container from cloneRootViewTransitionContainer; R3F renders
|
|
14090
|
+
// into a WebGL/WebGPU canvas, where cloning the root has no meaning, so we never opt in and
|
|
14091
|
+
// React never calls them. They stay as no-ops rather than throws: the host config is shared by
|
|
14092
|
+
// every root, and a throw here would turn a React-internal probe into a hard crash for users.
|
|
14093
|
+
// Guarded by a test asserting a full render + unmount never reaches them.
|
|
14435
14094
|
cloneRootViewTransitionContainer(_rootContainer) {
|
|
14436
|
-
throw new Error("Not implemented.");
|
|
14437
14095
|
},
|
|
14438
14096
|
removeRootViewTransitionClone(_rootContainer, _clone) {
|
|
14439
|
-
throw new Error("Not implemented.");
|
|
14440
14097
|
},
|
|
14441
14098
|
// https://github.com/facebook/react/pull/32465
|
|
14442
14099
|
createFragmentInstance: (_fiber) => null,
|
|
@@ -14518,14 +14175,18 @@ function createRoot(canvas) {
|
|
|
14518
14175
|
if (!prevRoot) _roots.set(canvas, { fiber, store });
|
|
14519
14176
|
let onCreated;
|
|
14520
14177
|
let lastCamera;
|
|
14521
|
-
|
|
14178
|
+
const lastConfiguredProps = {};
|
|
14522
14179
|
let configured = false;
|
|
14523
14180
|
let pending = null;
|
|
14181
|
+
let rendererSetup = null;
|
|
14524
14182
|
return {
|
|
14525
14183
|
async configure(props = {}) {
|
|
14526
14184
|
let resolve;
|
|
14527
14185
|
pending = new Promise((_resolve) => resolve = _resolve);
|
|
14528
|
-
|
|
14186
|
+
const {
|
|
14187
|
+
id: canvasId,
|
|
14188
|
+
primaryCanvas,
|
|
14189
|
+
scheduler: schedulerConfig,
|
|
14529
14190
|
gl: glConfig,
|
|
14530
14191
|
renderer: rendererConfig,
|
|
14531
14192
|
size: propsSize,
|
|
@@ -14533,10 +14194,6 @@ function createRoot(canvas) {
|
|
|
14533
14194
|
events,
|
|
14534
14195
|
onCreated: onCreatedCallback,
|
|
14535
14196
|
shadows = false,
|
|
14536
|
-
linear = false,
|
|
14537
|
-
flat = false,
|
|
14538
|
-
textureColorSpace = SRGBColorSpace,
|
|
14539
|
-
legacy = false,
|
|
14540
14197
|
orthographic = false,
|
|
14541
14198
|
frameloop = "always",
|
|
14542
14199
|
dpr = [1, 2],
|
|
@@ -14547,8 +14204,11 @@ function createRoot(canvas) {
|
|
|
14547
14204
|
onDragOverMissed,
|
|
14548
14205
|
onDropMissed,
|
|
14549
14206
|
autoUpdateFrustum = true,
|
|
14550
|
-
occlusion = false
|
|
14207
|
+
occlusion = false,
|
|
14208
|
+
_sizeProps,
|
|
14209
|
+
forceEven
|
|
14551
14210
|
} = props;
|
|
14211
|
+
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || SRGBColorSpace;
|
|
14552
14212
|
let state = store.getState();
|
|
14553
14213
|
const defaultGLProps = {
|
|
14554
14214
|
canvas,
|
|
@@ -14557,7 +14217,8 @@ function createRoot(canvas) {
|
|
|
14557
14217
|
alpha: true
|
|
14558
14218
|
};
|
|
14559
14219
|
const defaultGPUProps = {
|
|
14560
|
-
canvas
|
|
14220
|
+
canvas,
|
|
14221
|
+
antialias: true
|
|
14561
14222
|
};
|
|
14562
14223
|
const wantsGL = (state.isLegacy || glConfig || !R3F_BUILD_WEBGPU || !rendererConfig);
|
|
14563
14224
|
if (glConfig && rendererConfig) {
|
|
@@ -14571,26 +14232,82 @@ function createRoot(canvas) {
|
|
|
14571
14232
|
});
|
|
14572
14233
|
}
|
|
14573
14234
|
let renderer = state.internal.actualRenderer;
|
|
14574
|
-
if (
|
|
14575
|
-
|
|
14576
|
-
|
|
14577
|
-
|
|
14578
|
-
}
|
|
14579
|
-
|
|
14580
|
-
if (!
|
|
14581
|
-
|
|
14582
|
-
|
|
14583
|
-
|
|
14584
|
-
|
|
14585
|
-
|
|
14586
|
-
|
|
14235
|
+
if (primaryCanvas && wantsGL) {
|
|
14236
|
+
throw new Error(
|
|
14237
|
+
"The `primaryCanvas` prop for multi-canvas rendering cannot be used with WebGL. Remove the `gl` prop or use WebGPU."
|
|
14238
|
+
);
|
|
14239
|
+
}
|
|
14240
|
+
if (!state.internal.actualRenderer) {
|
|
14241
|
+
if (!rendererSetup) {
|
|
14242
|
+
rendererSetup = (async () => {
|
|
14243
|
+
if (wantsGL) {
|
|
14244
|
+
renderer = await resolveRenderer(glConfig, defaultGLProps, WebGLRenderer);
|
|
14245
|
+
state.internal.actualRenderer = renderer;
|
|
14246
|
+
state.set({ isLegacy: true, gl: renderer, renderer, primaryStore: store });
|
|
14247
|
+
} else if (!wantsGL && primaryCanvas) {
|
|
14248
|
+
const primary = await waitForPrimary(primaryCanvas);
|
|
14249
|
+
renderer = primary.renderer;
|
|
14250
|
+
state.internal.actualRenderer = renderer;
|
|
14251
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14252
|
+
primary.store.setState((prev) => ({
|
|
14253
|
+
internal: { ...prev.internal, isMultiCanvas: true }
|
|
14254
|
+
}));
|
|
14255
|
+
state.set((prev) => ({
|
|
14256
|
+
webGPUSupported: primary.store.getState().webGPUSupported,
|
|
14257
|
+
renderer,
|
|
14258
|
+
primaryStore: primary.store,
|
|
14259
|
+
internal: {
|
|
14260
|
+
...prev.internal,
|
|
14261
|
+
canvasTarget,
|
|
14262
|
+
isMultiCanvas: true,
|
|
14263
|
+
isSecondary: true,
|
|
14264
|
+
targetId: primaryCanvas
|
|
14265
|
+
}
|
|
14266
|
+
}));
|
|
14267
|
+
} else if (!wantsGL) {
|
|
14268
|
+
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, WebGPURenderer);
|
|
14269
|
+
if (!renderer.hasInitialized?.()) {
|
|
14270
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14271
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14272
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14273
|
+
canvas.width = size2.width * pixelRatio;
|
|
14274
|
+
canvas.height = size2.height * pixelRatio;
|
|
14275
|
+
}
|
|
14276
|
+
await renderer.init();
|
|
14277
|
+
}
|
|
14278
|
+
const backend = renderer.backend;
|
|
14279
|
+
const isWebGPUBackend = backend && "isWebGPUBackend" in backend;
|
|
14280
|
+
state.internal.actualRenderer = renderer;
|
|
14281
|
+
state.set({ webGPUSupported: isWebGPUBackend, renderer, primaryStore: store });
|
|
14282
|
+
if (canvasId && !state.internal.isSecondary) {
|
|
14283
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14284
|
+
const unregisterPrimary = registerPrimary(canvasId, renderer, store);
|
|
14285
|
+
state.set((prev) => ({
|
|
14286
|
+
internal: {
|
|
14287
|
+
...prev.internal,
|
|
14288
|
+
canvasTarget,
|
|
14289
|
+
unregisterPrimary
|
|
14290
|
+
}
|
|
14291
|
+
}));
|
|
14292
|
+
}
|
|
14293
|
+
}
|
|
14294
|
+
})().catch((err) => {
|
|
14295
|
+
rendererSetup = null;
|
|
14296
|
+
throw err;
|
|
14297
|
+
});
|
|
14298
|
+
}
|
|
14299
|
+
await rendererSetup;
|
|
14300
|
+
state = store.getState();
|
|
14301
|
+
renderer = state.internal.actualRenderer;
|
|
14587
14302
|
}
|
|
14588
14303
|
let raycaster = state.raycaster;
|
|
14589
14304
|
if (!raycaster) state.set({ raycaster: raycaster = new Raycaster() });
|
|
14590
14305
|
const { params, ...options } = raycastOptions || {};
|
|
14591
14306
|
if (!is.equ(options, raycaster, shallowLoose)) applyProps(raycaster, { ...options });
|
|
14592
|
-
if (!is.equ(params, raycaster.params, shallowLoose))
|
|
14307
|
+
if (!is.equ(params, raycaster.params, shallowLoose)) {
|
|
14593
14308
|
applyProps(raycaster, { params: { ...raycaster.params, ...params } });
|
|
14309
|
+
}
|
|
14310
|
+
let tempCamera = state.camera;
|
|
14594
14311
|
if (!state.camera || state.camera === lastCamera && !is.equ(lastCamera, cameraOptions, shallowLoose)) {
|
|
14595
14312
|
lastCamera = cameraOptions;
|
|
14596
14313
|
const isCamera = cameraOptions?.isCamera;
|
|
@@ -14610,6 +14327,7 @@ function createRoot(canvas) {
|
|
|
14610
14327
|
if (!state.camera && !cameraOptions?.rotation) camera.lookAt(0, 0, 0);
|
|
14611
14328
|
}
|
|
14612
14329
|
state.set({ camera });
|
|
14330
|
+
tempCamera = camera;
|
|
14613
14331
|
raycaster.camera = camera;
|
|
14614
14332
|
}
|
|
14615
14333
|
if (!state.scene) {
|
|
@@ -14627,7 +14345,7 @@ function createRoot(canvas) {
|
|
|
14627
14345
|
rootScene: scene,
|
|
14628
14346
|
internal: { ...prev.internal, container: scene }
|
|
14629
14347
|
}));
|
|
14630
|
-
const camera =
|
|
14348
|
+
const camera = tempCamera;
|
|
14631
14349
|
if (camera && !camera.parent) scene.add(camera);
|
|
14632
14350
|
}
|
|
14633
14351
|
if (events && !state.events.handlers) {
|
|
@@ -14641,9 +14359,17 @@ function createRoot(canvas) {
|
|
|
14641
14359
|
wasEnabled = enabled;
|
|
14642
14360
|
});
|
|
14643
14361
|
}
|
|
14362
|
+
if (_sizeProps !== void 0) {
|
|
14363
|
+
state.set({ _sizeProps });
|
|
14364
|
+
}
|
|
14365
|
+
if (forceEven !== void 0 && state.internal.forceEven !== forceEven) {
|
|
14366
|
+
state.set((prev) => ({ internal: { ...prev.internal, forceEven } }));
|
|
14367
|
+
}
|
|
14644
14368
|
const size = computeInitialSize(canvas, propsSize);
|
|
14645
|
-
if (!is.equ(size, state.size, shallowLoose)) {
|
|
14369
|
+
if (!state._sizeImperative && !is.equ(size, state.size, shallowLoose)) {
|
|
14370
|
+
const wasImperative = state._sizeImperative;
|
|
14646
14371
|
state.setSize(size.width, size.height, size.top, size.left);
|
|
14372
|
+
if (!wasImperative) state.set({ _sizeImperative: false });
|
|
14647
14373
|
}
|
|
14648
14374
|
if (dpr !== void 0 && !is.equ(dpr, lastConfiguredProps.dpr, shallowLoose)) {
|
|
14649
14375
|
state.setDpr(dpr);
|
|
@@ -14665,10 +14391,10 @@ function createRoot(canvas) {
|
|
|
14665
14391
|
lastConfiguredProps.performance = performance;
|
|
14666
14392
|
}
|
|
14667
14393
|
if (!state.xr) {
|
|
14668
|
-
const handleXRFrame = (timestamp,
|
|
14394
|
+
const handleXRFrame = (timestamp, _frame) => {
|
|
14669
14395
|
const state2 = store.getState();
|
|
14670
14396
|
if (state2.frameloop === "never") return;
|
|
14671
|
-
advance(timestamp
|
|
14397
|
+
advance(timestamp);
|
|
14672
14398
|
};
|
|
14673
14399
|
const actualRenderer = state.internal.actualRenderer;
|
|
14674
14400
|
const handleSessionChange = () => {
|
|
@@ -14680,16 +14406,16 @@ function createRoot(canvas) {
|
|
|
14680
14406
|
};
|
|
14681
14407
|
const xr = {
|
|
14682
14408
|
connect() {
|
|
14683
|
-
const { gl, renderer: renderer2
|
|
14684
|
-
const
|
|
14685
|
-
|
|
14686
|
-
|
|
14409
|
+
const { gl, renderer: renderer2 } = store.getState();
|
|
14410
|
+
const xrManager = (renderer2 || gl).xr;
|
|
14411
|
+
xrManager.addEventListener("sessionstart", handleSessionChange);
|
|
14412
|
+
xrManager.addEventListener("sessionend", handleSessionChange);
|
|
14687
14413
|
},
|
|
14688
14414
|
disconnect() {
|
|
14689
|
-
const { gl, renderer: renderer2
|
|
14690
|
-
const
|
|
14691
|
-
|
|
14692
|
-
|
|
14415
|
+
const { gl, renderer: renderer2 } = store.getState();
|
|
14416
|
+
const xrManager = (renderer2 || gl).xr;
|
|
14417
|
+
xrManager.removeEventListener("sessionstart", handleSessionChange);
|
|
14418
|
+
xrManager.removeEventListener("sessionend", handleSessionChange);
|
|
14693
14419
|
}
|
|
14694
14420
|
};
|
|
14695
14421
|
if (typeof renderer.xr?.addEventListener === "function") xr.connect();
|
|
@@ -14701,69 +14427,92 @@ function createRoot(canvas) {
|
|
|
14701
14427
|
const oldType = renderer.shadowMap.type;
|
|
14702
14428
|
renderer.shadowMap.enabled = !!shadows;
|
|
14703
14429
|
if (is.boo(shadows)) {
|
|
14704
|
-
renderer.shadowMap.type =
|
|
14430
|
+
renderer.shadowMap.type = PCFShadowMap;
|
|
14705
14431
|
} else if (is.str(shadows)) {
|
|
14432
|
+
if (shadows === "soft") {
|
|
14433
|
+
notifyDepreciated({
|
|
14434
|
+
heading: 'shadows="soft" is deprecated',
|
|
14435
|
+
body: "Three has depreciated soft and improved basic PCFShadows, we converted for you.",
|
|
14436
|
+
link: "https://github.com/mrdoob/three.js/wiki/Migration-Guide?utm_source=chatgpt.com#181--182"
|
|
14437
|
+
});
|
|
14438
|
+
}
|
|
14706
14439
|
const types = {
|
|
14707
14440
|
basic: BasicShadowMap,
|
|
14708
14441
|
percentage: PCFShadowMap,
|
|
14709
|
-
soft:
|
|
14442
|
+
soft: PCFShadowMap,
|
|
14710
14443
|
variance: VSMShadowMap
|
|
14711
14444
|
};
|
|
14712
|
-
renderer.shadowMap.type = types[shadows] ??
|
|
14445
|
+
renderer.shadowMap.type = types[shadows] ?? PCFShadowMap;
|
|
14713
14446
|
} else if (is.obj(shadows)) {
|
|
14714
14447
|
Object.assign(renderer.shadowMap, shadows);
|
|
14715
14448
|
}
|
|
14716
|
-
if (oldEnabled !== renderer.shadowMap.enabled || oldType !== renderer.shadowMap.type)
|
|
14449
|
+
if (oldEnabled !== renderer.shadowMap.enabled || oldType !== renderer.shadowMap.type) {
|
|
14717
14450
|
renderer.shadowMap.needsUpdate = true;
|
|
14718
|
-
}
|
|
14719
|
-
{
|
|
14720
|
-
const legacyChanged = legacy !== lastConfiguredProps.legacy;
|
|
14721
|
-
const linearChanged = linear !== lastConfiguredProps.linear;
|
|
14722
|
-
const flatChanged = flat !== lastConfiguredProps.flat;
|
|
14723
|
-
if (legacyChanged) {
|
|
14724
|
-
if (legacy)
|
|
14725
|
-
notifyDepreciated({
|
|
14726
|
-
heading: "Legacy Color Management",
|
|
14727
|
-
body: "Legacy color management is deprecated and will be removed in a future version.",
|
|
14728
|
-
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
14729
|
-
});
|
|
14730
|
-
}
|
|
14731
|
-
if (legacyChanged) {
|
|
14732
|
-
ColorManagement.enabled = !legacy;
|
|
14733
|
-
lastConfiguredProps.legacy = legacy;
|
|
14734
14451
|
}
|
|
14735
|
-
|
|
14736
|
-
|
|
14737
|
-
|
|
14738
|
-
|
|
14739
|
-
if (!configured || flatChanged) {
|
|
14740
|
-
renderer.toneMapping = flat ? NoToneMapping : ACESFilmicToneMapping;
|
|
14741
|
-
lastConfiguredProps.flat = flat;
|
|
14742
|
-
}
|
|
14743
|
-
if (legacyChanged && state.legacy !== legacy) state.set(() => ({ legacy }));
|
|
14744
|
-
if (linearChanged && state.linear !== linear) state.set(() => ({ linear }));
|
|
14745
|
-
if (flatChanged && state.flat !== flat) state.set(() => ({ flat }));
|
|
14452
|
+
}
|
|
14453
|
+
if (!configured) {
|
|
14454
|
+
renderer.outputColorSpace = SRGBColorSpace;
|
|
14455
|
+
renderer.toneMapping = ACESFilmicToneMapping;
|
|
14746
14456
|
}
|
|
14747
14457
|
if (textureColorSpace !== lastConfiguredProps.textureColorSpace) {
|
|
14748
14458
|
if (state.textureColorSpace !== textureColorSpace) state.set(() => ({ textureColorSpace }));
|
|
14749
14459
|
lastConfiguredProps.textureColorSpace = textureColorSpace;
|
|
14750
14460
|
}
|
|
14751
|
-
|
|
14752
|
-
|
|
14461
|
+
const r3fProps = ["textureColorSpace"];
|
|
14462
|
+
const constructorOnlyProps = ["samples", "antialias", "alpha", "canvas", "powerPreference"];
|
|
14463
|
+
const nonApplyProps = [...r3fProps, ...constructorOnlyProps];
|
|
14464
|
+
if (glConfig && !is.fun(glConfig) && !isRenderer(glConfig) && !is.equ(glConfig, renderer, shallowLoose)) {
|
|
14465
|
+
const glProps = {};
|
|
14466
|
+
for (const key in glConfig) {
|
|
14467
|
+
if (!nonApplyProps.includes(key)) glProps[key] = glConfig[key];
|
|
14468
|
+
}
|
|
14469
|
+
applyProps(renderer, glProps);
|
|
14470
|
+
}
|
|
14753
14471
|
if (rendererConfig && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && state.renderer) {
|
|
14754
14472
|
const currentRenderer = state.renderer;
|
|
14755
14473
|
if (!is.equ(rendererConfig, currentRenderer, shallowLoose)) {
|
|
14756
|
-
|
|
14474
|
+
const rendererProps = {};
|
|
14475
|
+
for (const key in rendererConfig) {
|
|
14476
|
+
if (!nonApplyProps.includes(key)) rendererProps[key] = rendererConfig[key];
|
|
14477
|
+
}
|
|
14478
|
+
applyProps(currentRenderer, rendererProps);
|
|
14757
14479
|
}
|
|
14758
14480
|
}
|
|
14759
14481
|
const scheduler = getScheduler();
|
|
14760
14482
|
const rootId = state.internal.rootId;
|
|
14761
14483
|
if (!rootId) {
|
|
14762
|
-
const newRootId = scheduler.generateRootId();
|
|
14484
|
+
const newRootId = canvasId || scheduler.generateRootId();
|
|
14763
14485
|
const unregisterRoot = scheduler.registerRoot(newRootId, {
|
|
14764
14486
|
getState: () => store.getState(),
|
|
14765
14487
|
onError: (err) => store.getState().setError(err)
|
|
14766
14488
|
});
|
|
14489
|
+
const unregisterCanvasTarget = scheduler.register(
|
|
14490
|
+
() => {
|
|
14491
|
+
const state2 = store.getState();
|
|
14492
|
+
if (state2.internal.isMultiCanvas && state2.internal.canvasTarget) {
|
|
14493
|
+
const renderer2 = state2.internal.actualRenderer;
|
|
14494
|
+
renderer2.setCanvasTarget(state2.internal.canvasTarget);
|
|
14495
|
+
}
|
|
14496
|
+
},
|
|
14497
|
+
{
|
|
14498
|
+
id: `${newRootId}_canvasTarget`,
|
|
14499
|
+
rootId: newRootId,
|
|
14500
|
+
phase: "start",
|
|
14501
|
+
system: true
|
|
14502
|
+
}
|
|
14503
|
+
);
|
|
14504
|
+
const unregisterEventsFlush = scheduler.register(
|
|
14505
|
+
() => {
|
|
14506
|
+
const state2 = store.getState();
|
|
14507
|
+
state2.events.flush?.();
|
|
14508
|
+
},
|
|
14509
|
+
{
|
|
14510
|
+
id: `${newRootId}_events`,
|
|
14511
|
+
rootId: newRootId,
|
|
14512
|
+
phase: "input",
|
|
14513
|
+
system: true
|
|
14514
|
+
}
|
|
14515
|
+
);
|
|
14767
14516
|
const unregisterFrustum = scheduler.register(
|
|
14768
14517
|
() => {
|
|
14769
14518
|
const state2 = store.getState();
|
|
@@ -14774,7 +14523,7 @@ function createRoot(canvas) {
|
|
|
14774
14523
|
{
|
|
14775
14524
|
id: `${newRootId}_frustum`,
|
|
14776
14525
|
rootId: newRootId,
|
|
14777
|
-
|
|
14526
|
+
before: "render",
|
|
14778
14527
|
system: true
|
|
14779
14528
|
}
|
|
14780
14529
|
);
|
|
@@ -14786,7 +14535,7 @@ function createRoot(canvas) {
|
|
|
14786
14535
|
{
|
|
14787
14536
|
id: `${newRootId}_visibility`,
|
|
14788
14537
|
rootId: newRootId,
|
|
14789
|
-
|
|
14538
|
+
before: "render",
|
|
14790
14539
|
system: true,
|
|
14791
14540
|
after: `${newRootId}_frustum`
|
|
14792
14541
|
}
|
|
@@ -14798,18 +14547,22 @@ function createRoot(canvas) {
|
|
|
14798
14547
|
const userHandlesRender = scheduler.hasUserJobsInPhase("render", newRootId);
|
|
14799
14548
|
if (userHandlesRender || state2.internal.priority) return;
|
|
14800
14549
|
try {
|
|
14801
|
-
if (state2.
|
|
14550
|
+
if (state2.renderPipeline?.render) state2.renderPipeline.render();
|
|
14802
14551
|
else if (renderer2?.render) renderer2.render(state2.scene, state2.camera);
|
|
14803
14552
|
} catch (error) {
|
|
14804
14553
|
state2.setError(error instanceof Error ? error : new Error(String(error)));
|
|
14805
14554
|
}
|
|
14806
14555
|
},
|
|
14807
14556
|
{
|
|
14808
|
-
|
|
14557
|
+
// Use canvas ID directly as job ID if available, otherwise use generated rootId
|
|
14558
|
+
id: canvasId || `${newRootId}_render`,
|
|
14809
14559
|
rootId: newRootId,
|
|
14810
14560
|
phase: "render",
|
|
14811
|
-
system: true
|
|
14561
|
+
system: true,
|
|
14812
14562
|
// Internal flag: this is a system job, not user-controlled
|
|
14563
|
+
// Apply scheduler config for render ordering and rate limiting
|
|
14564
|
+
...schedulerConfig?.after && { after: schedulerConfig.after },
|
|
14565
|
+
...schedulerConfig?.fps && { fps: schedulerConfig.fps }
|
|
14813
14566
|
}
|
|
14814
14567
|
);
|
|
14815
14568
|
state.set((state2) => ({
|
|
@@ -14818,6 +14571,8 @@ function createRoot(canvas) {
|
|
|
14818
14571
|
rootId: newRootId,
|
|
14819
14572
|
unregisterRoot: () => {
|
|
14820
14573
|
unregisterRoot();
|
|
14574
|
+
unregisterCanvasTarget();
|
|
14575
|
+
unregisterEventsFlush();
|
|
14821
14576
|
unregisterFrustum();
|
|
14822
14577
|
unregisterVisibility();
|
|
14823
14578
|
unregisterRender();
|
|
@@ -14876,15 +14631,24 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
14876
14631
|
const renderer = state.internal.actualRenderer;
|
|
14877
14632
|
const unregisterRoot = state.internal.unregisterRoot;
|
|
14878
14633
|
if (unregisterRoot) unregisterRoot();
|
|
14634
|
+
const unregisterPrimary = state.internal.unregisterPrimary;
|
|
14635
|
+
if (unregisterPrimary) unregisterPrimary();
|
|
14636
|
+
const canvasTarget = state.internal.canvasTarget;
|
|
14637
|
+
if (canvasTarget?.dispose) canvasTarget.dispose();
|
|
14879
14638
|
state.events.disconnect?.();
|
|
14880
14639
|
cleanupHelperGroup(root.store);
|
|
14881
|
-
renderer
|
|
14882
|
-
|
|
14883
|
-
|
|
14640
|
+
if (state.isLegacy && renderer) {
|
|
14641
|
+
;
|
|
14642
|
+
renderer.renderLists?.dispose?.();
|
|
14643
|
+
renderer.forceContextLoss?.();
|
|
14644
|
+
}
|
|
14645
|
+
if (!state.internal.isSecondary) {
|
|
14646
|
+
if (renderer?.xr) state.xr.disconnect();
|
|
14647
|
+
}
|
|
14884
14648
|
dispose(state.scene);
|
|
14885
14649
|
_roots.delete(canvas);
|
|
14886
14650
|
if (callback) callback(canvas);
|
|
14887
|
-
} catch
|
|
14651
|
+
} catch {
|
|
14888
14652
|
}
|
|
14889
14653
|
}, 500);
|
|
14890
14654
|
}
|
|
@@ -14892,36 +14656,34 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
14892
14656
|
}
|
|
14893
14657
|
}
|
|
14894
14658
|
function createPortal(children, container, state) {
|
|
14895
|
-
return /* @__PURE__ */ jsx(
|
|
14659
|
+
return /* @__PURE__ */ jsx(Portal, { children, container, state });
|
|
14896
14660
|
}
|
|
14897
|
-
function
|
|
14661
|
+
function Portal({ children, container, state }) {
|
|
14898
14662
|
const isRef = useCallback((obj) => obj && "current" in obj, []);
|
|
14899
|
-
const [resolvedContainer,
|
|
14663
|
+
const [resolvedContainer, _setResolvedContainer] = useState(() => {
|
|
14900
14664
|
if (isRef(container)) return container.current ?? null;
|
|
14901
14665
|
return container;
|
|
14902
14666
|
});
|
|
14667
|
+
const setResolvedContainer = useCallback(
|
|
14668
|
+
(newContainer) => {
|
|
14669
|
+
if (!newContainer || newContainer === resolvedContainer) return;
|
|
14670
|
+
_setResolvedContainer(isRef(newContainer) ? newContainer.current : newContainer);
|
|
14671
|
+
},
|
|
14672
|
+
[resolvedContainer, _setResolvedContainer, isRef]
|
|
14673
|
+
);
|
|
14903
14674
|
useMemo(() => {
|
|
14904
|
-
if (isRef(container)) {
|
|
14905
|
-
|
|
14906
|
-
|
|
14907
|
-
|
|
14908
|
-
const updated = container.current;
|
|
14909
|
-
if (updated && updated !== resolvedContainer) {
|
|
14910
|
-
setResolvedContainer(updated);
|
|
14911
|
-
}
|
|
14912
|
-
});
|
|
14913
|
-
} else if (current !== resolvedContainer) {
|
|
14914
|
-
setResolvedContainer(current);
|
|
14915
|
-
}
|
|
14916
|
-
} else if (container !== resolvedContainer) {
|
|
14917
|
-
setResolvedContainer(container);
|
|
14675
|
+
if (isRef(container) && !container.current) {
|
|
14676
|
+
return queueMicrotask(() => {
|
|
14677
|
+
setResolvedContainer(container.current);
|
|
14678
|
+
});
|
|
14918
14679
|
}
|
|
14919
|
-
|
|
14680
|
+
setResolvedContainer(container);
|
|
14681
|
+
}, [container, isRef, setResolvedContainer]);
|
|
14920
14682
|
if (!resolvedContainer) return /* @__PURE__ */ jsx(Fragment, {});
|
|
14921
14683
|
const portalKey = resolvedContainer.uuid ?? `portal-${resolvedContainer.id ?? "unknown"}`;
|
|
14922
|
-
return /* @__PURE__ */ jsx(
|
|
14684
|
+
return /* @__PURE__ */ jsx(PortalInner, { children, container: resolvedContainer, state }, portalKey);
|
|
14923
14685
|
}
|
|
14924
|
-
function
|
|
14686
|
+
function PortalInner({ state = {}, children, container }) {
|
|
14925
14687
|
const { events, size, injectScene = true, ...rest } = state;
|
|
14926
14688
|
const previousRoot = useStore();
|
|
14927
14689
|
const [raycaster] = useState(() => new Raycaster());
|
|
@@ -14942,12 +14704,14 @@ function Portal({ state = {}, children, container }) {
|
|
|
14942
14704
|
};
|
|
14943
14705
|
}, [portalScene, container, injectScene]);
|
|
14944
14706
|
const inject = useMutableCallback((rootState, injectState) => {
|
|
14707
|
+
const resolvedSize = { ...rootState.size, ...injectState.size, ...size };
|
|
14945
14708
|
let viewport = void 0;
|
|
14946
|
-
if (injectState.camera && size) {
|
|
14709
|
+
if (injectState.camera && (size || injectState.size)) {
|
|
14947
14710
|
const camera = injectState.camera;
|
|
14948
|
-
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(),
|
|
14949
|
-
if (camera !== rootState.camera) updateCamera(camera,
|
|
14711
|
+
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(), resolvedSize);
|
|
14712
|
+
if (camera !== rootState.camera) updateCamera(camera, resolvedSize);
|
|
14950
14713
|
}
|
|
14714
|
+
const set = injectState.set;
|
|
14951
14715
|
return {
|
|
14952
14716
|
// The intersect consists of the previous root state
|
|
14953
14717
|
...rootState,
|
|
@@ -14963,10 +14727,10 @@ function Portal({ state = {}, children, container }) {
|
|
|
14963
14727
|
previousRoot,
|
|
14964
14728
|
// Events, size and viewport can be overridden by the inject layer
|
|
14965
14729
|
events: { ...rootState.events, ...injectState.events, ...events },
|
|
14966
|
-
size:
|
|
14730
|
+
size: resolvedSize,
|
|
14967
14731
|
viewport: { ...rootState.viewport, ...viewport },
|
|
14968
14732
|
// Layers are allowed to override events
|
|
14969
|
-
setEvents: (events2) =>
|
|
14733
|
+
setEvents: (events2) => set((state2) => ({ ...state2, events: { ...state2.events, ...events2 } })),
|
|
14970
14734
|
// Container for child attachment - the portalScene (injected or container itself)
|
|
14971
14735
|
internal: { ...rootState.internal, ...injectState.internal, container: portalScene }
|
|
14972
14736
|
};
|
|
@@ -14975,9 +14739,13 @@ function Portal({ state = {}, children, container }) {
|
|
|
14975
14739
|
const store = createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
14976
14740
|
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
14977
14741
|
onMutate(previousRoot.getState());
|
|
14978
|
-
previousRoot.subscribe(onMutate);
|
|
14979
14742
|
return store;
|
|
14980
14743
|
}, [previousRoot, container]);
|
|
14744
|
+
useIsomorphicLayoutEffect(() => {
|
|
14745
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14746
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14747
|
+
return unsubscribe;
|
|
14748
|
+
}, [previousRoot, usePortalStore]);
|
|
14981
14749
|
return (
|
|
14982
14750
|
// @ts-ignore, reconciler types are not maintained
|
|
14983
14751
|
/* @__PURE__ */ jsx(Fragment, { children: reconciler.createPortal(
|
|
@@ -14991,21 +14759,72 @@ function flushSync(fn) {
|
|
|
14991
14759
|
return reconciler.flushSyncFromReconciler(fn);
|
|
14992
14760
|
}
|
|
14993
14761
|
|
|
14762
|
+
function parseBackground(background) {
|
|
14763
|
+
if (!background) return null;
|
|
14764
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14765
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14766
|
+
return {
|
|
14767
|
+
...rest,
|
|
14768
|
+
preset,
|
|
14769
|
+
files: envMap || files,
|
|
14770
|
+
backgroundFiles: backgroundMap,
|
|
14771
|
+
background: true
|
|
14772
|
+
};
|
|
14773
|
+
}
|
|
14774
|
+
if (typeof background === "number") {
|
|
14775
|
+
return { color: background, background: true };
|
|
14776
|
+
}
|
|
14777
|
+
if (typeof background === "string") {
|
|
14778
|
+
if (background in presetsObj) {
|
|
14779
|
+
return { preset: background, background: true };
|
|
14780
|
+
}
|
|
14781
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14782
|
+
return { files: background, background: true };
|
|
14783
|
+
}
|
|
14784
|
+
return { color: background, background: true };
|
|
14785
|
+
}
|
|
14786
|
+
if (background.isColor) {
|
|
14787
|
+
return { color: background, background: true };
|
|
14788
|
+
}
|
|
14789
|
+
return null;
|
|
14790
|
+
}
|
|
14791
|
+
|
|
14792
|
+
function parseRendererConfig(rendererProp) {
|
|
14793
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14794
|
+
if (isRendererConfig) {
|
|
14795
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14796
|
+
return {
|
|
14797
|
+
primaryCanvas: pc,
|
|
14798
|
+
scheduler: sc,
|
|
14799
|
+
renderer: Object.keys(rest).length > 0 ? rest : rendererProp
|
|
14800
|
+
};
|
|
14801
|
+
}
|
|
14802
|
+
return { primaryCanvas: void 0, scheduler: void 0, renderer: rendererProp };
|
|
14803
|
+
}
|
|
14804
|
+
|
|
14805
|
+
function clearHmrCaches(store) {
|
|
14806
|
+
store.setState((state) => ({
|
|
14807
|
+
nodes: {},
|
|
14808
|
+
uniforms: {},
|
|
14809
|
+
buffers: {},
|
|
14810
|
+
gpuStorage: {},
|
|
14811
|
+
_hmrVersion: state._hmrVersion + 1
|
|
14812
|
+
}));
|
|
14813
|
+
}
|
|
14814
|
+
|
|
14994
14815
|
function CanvasImpl({
|
|
14995
14816
|
ref,
|
|
14996
14817
|
children,
|
|
14997
14818
|
fallback,
|
|
14998
14819
|
resize,
|
|
14999
14820
|
style,
|
|
14821
|
+
id,
|
|
15000
14822
|
gl,
|
|
15001
|
-
renderer,
|
|
14823
|
+
renderer: rendererProp,
|
|
15002
14824
|
events = createPointerEvents,
|
|
15003
14825
|
eventSource,
|
|
15004
14826
|
eventPrefix,
|
|
15005
14827
|
shadows,
|
|
15006
|
-
linear,
|
|
15007
|
-
flat,
|
|
15008
|
-
legacy,
|
|
15009
14828
|
orthographic,
|
|
15010
14829
|
frameloop,
|
|
15011
14830
|
dpr,
|
|
@@ -15013,14 +14832,23 @@ function CanvasImpl({
|
|
|
15013
14832
|
raycaster,
|
|
15014
14833
|
camera,
|
|
15015
14834
|
scene,
|
|
14835
|
+
autoUpdateFrustum,
|
|
14836
|
+
occlusion,
|
|
15016
14837
|
onPointerMissed,
|
|
15017
14838
|
onDragOverMissed,
|
|
15018
14839
|
onDropMissed,
|
|
15019
14840
|
onCreated,
|
|
14841
|
+
hmr,
|
|
14842
|
+
width,
|
|
14843
|
+
height,
|
|
14844
|
+
background,
|
|
14845
|
+
forceEven,
|
|
15020
14846
|
...props
|
|
15021
14847
|
}) {
|
|
14848
|
+
const { primaryCanvas, scheduler, renderer } = parseRendererConfig(rendererProp);
|
|
15022
14849
|
React.useMemo(() => extend(THREE), []);
|
|
15023
14850
|
const Bridge = useBridge();
|
|
14851
|
+
const backgroundProps = React.useMemo(() => parseBackground(background), [background]);
|
|
15024
14852
|
const hasInitialSizeRef = React.useRef(false);
|
|
15025
14853
|
const measureConfig = React.useMemo(() => {
|
|
15026
14854
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15037,7 +14865,21 @@ function CanvasImpl({
|
|
|
15037
14865
|
};
|
|
15038
14866
|
}, [resize, hasInitialSizeRef.current]);
|
|
15039
14867
|
const [containerRef, containerRect] = useMeasure(measureConfig);
|
|
15040
|
-
|
|
14868
|
+
const effectiveSize = React.useMemo(() => {
|
|
14869
|
+
let w = width ?? containerRect.width;
|
|
14870
|
+
let h = height ?? containerRect.height;
|
|
14871
|
+
if (forceEven) {
|
|
14872
|
+
w = Math.ceil(w / 2) * 2;
|
|
14873
|
+
h = Math.ceil(h / 2) * 2;
|
|
14874
|
+
}
|
|
14875
|
+
return {
|
|
14876
|
+
width: w,
|
|
14877
|
+
height: h,
|
|
14878
|
+
top: containerRect.top,
|
|
14879
|
+
left: containerRect.left
|
|
14880
|
+
};
|
|
14881
|
+
}, [width, height, containerRect, forceEven]);
|
|
14882
|
+
if (!hasInitialSizeRef.current && effectiveSize.width > 0 && effectiveSize.height > 0) {
|
|
15041
14883
|
hasInitialSizeRef.current = true;
|
|
15042
14884
|
}
|
|
15043
14885
|
const canvasRef = React.useRef(null);
|
|
@@ -15048,6 +14890,7 @@ function CanvasImpl({
|
|
|
15048
14890
|
const handleDropMissed = useMutableCallback(onDropMissed);
|
|
15049
14891
|
const [block, setBlock] = React.useState(false);
|
|
15050
14892
|
const [error, setError] = React.useState(false);
|
|
14893
|
+
const [fallbackVisible, setFallbackVisible] = React.useState(false);
|
|
15051
14894
|
if (block) throw block;
|
|
15052
14895
|
if (error) throw error;
|
|
15053
14896
|
const root = React.useRef(null);
|
|
@@ -15055,8 +14898,9 @@ function CanvasImpl({
|
|
|
15055
14898
|
const unsubscribeErrorRef = React.useRef(null);
|
|
15056
14899
|
useIsomorphicLayoutEffect(() => {
|
|
15057
14900
|
effectActiveRef.current = true;
|
|
14901
|
+
if (fallbackVisible) return;
|
|
15058
14902
|
const canvas = canvasRef.current;
|
|
15059
|
-
if (
|
|
14903
|
+
if (effectiveSize.width > 0 && effectiveSize.height > 0 && canvas) {
|
|
15060
14904
|
if (!root.current) {
|
|
15061
14905
|
root.current = createRoot(canvas);
|
|
15062
14906
|
notifyAlpha({
|
|
@@ -15075,22 +14919,27 @@ function CanvasImpl({
|
|
|
15075
14919
|
}
|
|
15076
14920
|
async function run() {
|
|
15077
14921
|
if (!effectActiveRef.current || !root.current) return;
|
|
15078
|
-
await root.current.configure({
|
|
14922
|
+
const configured = await root.current.configure({
|
|
14923
|
+
id,
|
|
14924
|
+
primaryCanvas,
|
|
14925
|
+
scheduler,
|
|
15079
14926
|
gl,
|
|
15080
14927
|
renderer,
|
|
15081
14928
|
scene,
|
|
15082
14929
|
events,
|
|
15083
14930
|
shadows,
|
|
15084
|
-
linear,
|
|
15085
|
-
flat,
|
|
15086
|
-
legacy,
|
|
15087
14931
|
orthographic,
|
|
15088
14932
|
frameloop,
|
|
15089
14933
|
dpr,
|
|
15090
14934
|
performance,
|
|
15091
14935
|
raycaster,
|
|
15092
14936
|
camera,
|
|
15093
|
-
|
|
14937
|
+
autoUpdateFrustum,
|
|
14938
|
+
occlusion,
|
|
14939
|
+
size: effectiveSize,
|
|
14940
|
+
// Store size props for reset functionality
|
|
14941
|
+
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
14942
|
+
forceEven,
|
|
15094
14943
|
// Pass mutable reference to onPointerMissed so it's free to update
|
|
15095
14944
|
onPointerMissed: (...args) => handlePointerMissed.current?.(...args),
|
|
15096
14945
|
onDragOverMissed: (...args) => handleDragOverMissed.current?.(...args),
|
|
@@ -15111,10 +14960,25 @@ function CanvasImpl({
|
|
|
15111
14960
|
}
|
|
15112
14961
|
onCreated?.(state);
|
|
15113
14962
|
}
|
|
15114
|
-
})
|
|
15115
|
-
|
|
14963
|
+
}).then(
|
|
14964
|
+
() => true,
|
|
14965
|
+
// Renderer setup failed (e.g. no WebGL/WebGPU support). The `fallback` prop lives
|
|
14966
|
+
// inside <canvas>, which browsers don't display, so surface it as visible DOM
|
|
14967
|
+
// instead; with no fallback, rethrow to an external error boundary. (#3757)
|
|
14968
|
+
(setupError) => {
|
|
14969
|
+
if (effectActiveRef.current) {
|
|
14970
|
+
if (fallback != null) setFallbackVisible(true);
|
|
14971
|
+
else setError(setupError);
|
|
14972
|
+
}
|
|
14973
|
+
return false;
|
|
14974
|
+
}
|
|
14975
|
+
);
|
|
14976
|
+
if (!configured || !effectActiveRef.current || !root.current) return;
|
|
15116
14977
|
root.current.render(
|
|
15117
|
-
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */
|
|
14978
|
+
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */ jsxs(React.Suspense, { fallback: /* @__PURE__ */ jsx(Block, { set: setBlock }), children: [
|
|
14979
|
+
backgroundProps && /* @__PURE__ */ jsx(Environment, { ...backgroundProps }),
|
|
14980
|
+
children ?? null
|
|
14981
|
+
] }) }) })
|
|
15118
14982
|
);
|
|
15119
14983
|
}
|
|
15120
14984
|
run();
|
|
@@ -15136,6 +15000,28 @@ function CanvasImpl({
|
|
|
15136
15000
|
};
|
|
15137
15001
|
}
|
|
15138
15002
|
}, []);
|
|
15003
|
+
React.useEffect(() => {
|
|
15004
|
+
if (hmr === false) return;
|
|
15005
|
+
const canvas = canvasRef.current;
|
|
15006
|
+
if (!canvas) return;
|
|
15007
|
+
const handleHMR = () => {
|
|
15008
|
+
queueMicrotask(() => {
|
|
15009
|
+
const rootEntry = _roots.get(canvas);
|
|
15010
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15011
|
+
});
|
|
15012
|
+
};
|
|
15013
|
+
if (typeof import.meta !== "undefined" && import.meta.hot) {
|
|
15014
|
+
const hot = import.meta.hot;
|
|
15015
|
+
hot.on("vite:afterUpdate", handleHMR);
|
|
15016
|
+
return () => hot.off?.("vite:afterUpdate", handleHMR);
|
|
15017
|
+
}
|
|
15018
|
+
if (typeof module !== "undefined" && module.hot) {
|
|
15019
|
+
const hot = module.hot;
|
|
15020
|
+
hot.addStatusHandler((status) => {
|
|
15021
|
+
if (status === "idle") handleHMR();
|
|
15022
|
+
});
|
|
15023
|
+
}
|
|
15024
|
+
}, [hmr]);
|
|
15139
15025
|
const pointerEvents = eventSource ? "none" : "auto";
|
|
15140
15026
|
return /* @__PURE__ */ jsx(
|
|
15141
15027
|
"div",
|
|
@@ -15150,7 +15036,20 @@ function CanvasImpl({
|
|
|
15150
15036
|
...style
|
|
15151
15037
|
},
|
|
15152
15038
|
...props,
|
|
15153
|
-
children:
|
|
15039
|
+
children: fallbackVisible ? (
|
|
15040
|
+
// Renderer setup failed: render the fallback as visible DOM. Inside <canvas> (below)
|
|
15041
|
+
// it exists in the tree but browsers never paint it, which is the whole bug (#3757).
|
|
15042
|
+
fallback
|
|
15043
|
+
) : /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
15044
|
+
"canvas",
|
|
15045
|
+
{
|
|
15046
|
+
ref: canvasRef,
|
|
15047
|
+
id,
|
|
15048
|
+
className: "r3f-canvas",
|
|
15049
|
+
style: { display: "block", width: "100%", height: "100%" },
|
|
15050
|
+
children: fallback
|
|
15051
|
+
}
|
|
15052
|
+
) })
|
|
15154
15053
|
}
|
|
15155
15054
|
);
|
|
15156
15055
|
}
|
|
@@ -15160,4 +15059,4 @@ function Canvas(props) {
|
|
|
15160
15059
|
|
|
15161
15060
|
extend(THREE);
|
|
15162
15061
|
|
|
15163
|
-
export { Block, Canvas, ErrorBoundary, IsObject, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS,
|
|
15062
|
+
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 };
|