@react-three/fiber 10.0.0-alpha.1 → 10.0.0-canary.164c7be
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 +1256 -1419
- package/dist/index.d.cts +1887 -1699
- package/dist/index.d.mts +1887 -1699
- package/dist/index.d.ts +1887 -1699
- package/dist/index.mjs +1216 -1398
- package/dist/legacy.cjs +1222 -1410
- package/dist/legacy.d.cts +1888 -1700
- package/dist/legacy.d.mts +1888 -1700
- package/dist/legacy.d.ts +1888 -1700
- package/dist/legacy.mjs +1182 -1389
- package/dist/webgpu/index.cjs +1937 -1470
- package/dist/webgpu/index.d.cts +2145 -1722
- package/dist/webgpu/index.d.mts +2145 -1722
- package/dist/webgpu/index.d.ts +2145 -1722
- package/dist/webgpu/index.mjs +1890 -1449
- 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, 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 = {
|
|
@@ -215,7 +606,8 @@ function prepare(target, root, type, props) {
|
|
|
215
606
|
object,
|
|
216
607
|
eventCount: 0,
|
|
217
608
|
handlers: {},
|
|
218
|
-
isHidden: false
|
|
609
|
+
isHidden: false,
|
|
610
|
+
deferredRefs: []
|
|
219
611
|
};
|
|
220
612
|
if (object) object.__r3f = instance;
|
|
221
613
|
}
|
|
@@ -264,7 +656,7 @@ function createOcclusionObserverNode(store, uniform) {
|
|
|
264
656
|
let occlusionSetupPromise = null;
|
|
265
657
|
function enableOcclusion(store) {
|
|
266
658
|
const state = store.getState();
|
|
267
|
-
const { internal, renderer
|
|
659
|
+
const { internal, renderer } = state;
|
|
268
660
|
if (internal.occlusionEnabled || occlusionSetupPromise) return;
|
|
269
661
|
const hasOcclusionSupport = typeof renderer?.isOccluded === "function";
|
|
270
662
|
if (!hasOcclusionSupport) {
|
|
@@ -427,6 +819,22 @@ function hasVisibilityHandlers(handlers) {
|
|
|
427
819
|
return !!(handlers.onFramed || handlers.onOccluded || handlers.onVisible);
|
|
428
820
|
}
|
|
429
821
|
|
|
822
|
+
const FROM_REF = Symbol.for("@react-three/fiber.fromRef");
|
|
823
|
+
function fromRef(ref) {
|
|
824
|
+
return { [FROM_REF]: ref };
|
|
825
|
+
}
|
|
826
|
+
function isFromRef(value) {
|
|
827
|
+
return value !== null && typeof value === "object" && FROM_REF in value;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const ONCE = Symbol.for("@react-three/fiber.once");
|
|
831
|
+
function once(...args) {
|
|
832
|
+
return { [ONCE]: args.length ? args : true };
|
|
833
|
+
}
|
|
834
|
+
function isOnce(value) {
|
|
835
|
+
return value !== null && typeof value === "object" && ONCE in value;
|
|
836
|
+
}
|
|
837
|
+
|
|
430
838
|
const RESERVED_PROPS = [
|
|
431
839
|
"children",
|
|
432
840
|
"key",
|
|
@@ -497,7 +905,7 @@ function getMemoizedPrototype(root) {
|
|
|
497
905
|
ctor = new root.constructor();
|
|
498
906
|
MEMOIZED_PROTOTYPES.set(root.constructor, ctor);
|
|
499
907
|
}
|
|
500
|
-
} catch
|
|
908
|
+
} catch {
|
|
501
909
|
}
|
|
502
910
|
return ctor;
|
|
503
911
|
}
|
|
@@ -528,7 +936,7 @@ function applyProps(object, props) {
|
|
|
528
936
|
const rootState = instance && findInitialRoot(instance).getState();
|
|
529
937
|
const prevHandlers = instance?.eventCount;
|
|
530
938
|
for (const prop in props) {
|
|
531
|
-
|
|
939
|
+
const value = props[prop];
|
|
532
940
|
if (RESERVED_PROPS.includes(prop)) continue;
|
|
533
941
|
if (instance && EVENT_REGEX.test(prop)) {
|
|
534
942
|
if (typeof value === "function") instance.handlers[prop] = value;
|
|
@@ -543,6 +951,25 @@ function applyProps(object, props) {
|
|
|
543
951
|
continue;
|
|
544
952
|
}
|
|
545
953
|
if (value === void 0) continue;
|
|
954
|
+
if (isFromRef(value)) {
|
|
955
|
+
instance?.deferredRefs?.push({ prop, ref: value[FROM_REF] });
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
if (isOnce(value)) {
|
|
959
|
+
if (instance?.appliedOnce?.has(prop)) continue;
|
|
960
|
+
if (instance) {
|
|
961
|
+
instance.appliedOnce ?? (instance.appliedOnce = /* @__PURE__ */ new Set());
|
|
962
|
+
instance.appliedOnce.add(prop);
|
|
963
|
+
}
|
|
964
|
+
const { root: targetRoot, key: targetKey } = resolve(object, prop);
|
|
965
|
+
const args = value[ONCE];
|
|
966
|
+
if (typeof targetRoot[targetKey] === "function") {
|
|
967
|
+
targetRoot[targetKey](...args === true ? [] : args);
|
|
968
|
+
} else if (args !== true && args.length > 0) {
|
|
969
|
+
targetRoot[targetKey] = args[0];
|
|
970
|
+
}
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
546
973
|
let { root, key, target } = resolve(object, prop);
|
|
547
974
|
if (target === void 0 && (typeof root !== "object" || root === null)) {
|
|
548
975
|
throw Error(`R3F: Cannot set "${prop}". Ensure it is an object before setting "${key}".`);
|
|
@@ -565,7 +992,10 @@ function applyProps(object, props) {
|
|
|
565
992
|
else target.set(value);
|
|
566
993
|
} else {
|
|
567
994
|
root[key] = value;
|
|
568
|
-
if (
|
|
995
|
+
if (key.endsWith("Node") && root.isMaterial) {
|
|
996
|
+
root.needsUpdate = true;
|
|
997
|
+
}
|
|
998
|
+
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
999
|
root[key].format === RGBAFormat && root[key].type === UnsignedByteType) {
|
|
570
1000
|
root[key].colorSpace = rootState.textureColorSpace;
|
|
571
1001
|
}
|
|
@@ -598,38 +1028,60 @@ function applyProps(object, props) {
|
|
|
598
1028
|
return object;
|
|
599
1029
|
}
|
|
600
1030
|
|
|
1031
|
+
const DEFAULT_POINTER_ID = 0;
|
|
1032
|
+
const XR_POINTER_ID_START = 1e3;
|
|
1033
|
+
function getPointerState(internal, pointerId) {
|
|
1034
|
+
let state = internal.pointerMap.get(pointerId);
|
|
1035
|
+
if (!state) {
|
|
1036
|
+
state = {
|
|
1037
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1038
|
+
captured: /* @__PURE__ */ new Map(),
|
|
1039
|
+
initialClick: [0, 0],
|
|
1040
|
+
initialHits: []
|
|
1041
|
+
};
|
|
1042
|
+
internal.pointerMap.set(pointerId, state);
|
|
1043
|
+
}
|
|
1044
|
+
return state;
|
|
1045
|
+
}
|
|
1046
|
+
function getPointerId(event) {
|
|
1047
|
+
return "pointerId" in event ? event.pointerId : DEFAULT_POINTER_ID;
|
|
1048
|
+
}
|
|
601
1049
|
function makeId(event) {
|
|
602
1050
|
return (event.eventObject || event.object).uuid + "/" + event.index + event.instanceId;
|
|
603
1051
|
}
|
|
604
|
-
function releaseInternalPointerCapture(
|
|
605
|
-
const
|
|
1052
|
+
function releaseInternalPointerCapture(internal, obj, pointerId) {
|
|
1053
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1054
|
+
if (!pointerState) return;
|
|
1055
|
+
const captureData = pointerState.captured.get(obj);
|
|
606
1056
|
if (captureData) {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
capturedMap.delete(pointerId);
|
|
610
|
-
captureData.target.releasePointerCapture(pointerId);
|
|
611
|
-
}
|
|
1057
|
+
pointerState.captured.delete(obj);
|
|
1058
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
612
1059
|
}
|
|
613
1060
|
}
|
|
614
1061
|
function removeInteractivity(store, object) {
|
|
615
1062
|
const { internal } = store.getState();
|
|
616
1063
|
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
1064
|
+
for (const [pointerId, pointerState] of internal.pointerMap) {
|
|
1065
|
+
pointerState.initialHits = pointerState.initialHits.filter((o) => o !== object);
|
|
1066
|
+
pointerState.hovered.forEach((value, key) => {
|
|
1067
|
+
if (value.eventObject === object || value.object === object) {
|
|
1068
|
+
pointerState.hovered.delete(key);
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1071
|
+
if (pointerState.captured.has(object)) {
|
|
1072
|
+
releaseInternalPointerCapture(internal, object, pointerId);
|
|
621
1073
|
}
|
|
622
|
-
}
|
|
623
|
-
internal.capturedMap.forEach((captures, pointerId) => {
|
|
624
|
-
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
625
|
-
});
|
|
1074
|
+
}
|
|
626
1075
|
unregisterVisibility(store, object);
|
|
627
1076
|
}
|
|
628
1077
|
function createEvents(store) {
|
|
629
|
-
function calculateDistance(event) {
|
|
1078
|
+
function calculateDistance(event, pointerId) {
|
|
630
1079
|
const { internal } = store.getState();
|
|
631
|
-
const
|
|
632
|
-
|
|
1080
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1081
|
+
if (!pointerState) return 0;
|
|
1082
|
+
const [initialX, initialY] = pointerState.initialClick;
|
|
1083
|
+
const dx = event.offsetX - initialX;
|
|
1084
|
+
const dy = event.offsetY - initialY;
|
|
633
1085
|
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
634
1086
|
}
|
|
635
1087
|
function filterPointerEvents(objects) {
|
|
@@ -665,6 +1117,15 @@ function createEvents(store) {
|
|
|
665
1117
|
return state2.raycaster.camera ? state2.raycaster.intersectObject(obj, true) : [];
|
|
666
1118
|
}
|
|
667
1119
|
let hits = eventsObjects.flatMap(handleRaycast).sort((a, b) => {
|
|
1120
|
+
const aInteractivePriority = a.object.userData?.interactivePriority;
|
|
1121
|
+
const bInteractivePriority = b.object.userData?.interactivePriority;
|
|
1122
|
+
if (aInteractivePriority !== void 0 || bInteractivePriority !== void 0) {
|
|
1123
|
+
if (aInteractivePriority !== void 0 && bInteractivePriority === void 0) return -1;
|
|
1124
|
+
if (bInteractivePriority !== void 0 && aInteractivePriority === void 0) return 1;
|
|
1125
|
+
if (aInteractivePriority !== bInteractivePriority) {
|
|
1126
|
+
return (bInteractivePriority ?? 0) - (aInteractivePriority ?? 0);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
668
1129
|
const aState = getRootState(a.object);
|
|
669
1130
|
const bState = getRootState(b.object);
|
|
670
1131
|
const aPriority = aState?.events?.priority ?? 1;
|
|
@@ -680,14 +1141,19 @@ function createEvents(store) {
|
|
|
680
1141
|
for (const hit of hits) {
|
|
681
1142
|
let eventObject = hit.object;
|
|
682
1143
|
while (eventObject) {
|
|
683
|
-
if (eventObject.__r3f?.eventCount)
|
|
1144
|
+
if (eventObject.__r3f?.eventCount) {
|
|
684
1145
|
intersections.push({ ...hit, eventObject });
|
|
1146
|
+
}
|
|
685
1147
|
eventObject = eventObject.parent;
|
|
686
1148
|
}
|
|
687
1149
|
}
|
|
688
|
-
if ("pointerId" in event
|
|
689
|
-
|
|
690
|
-
|
|
1150
|
+
if ("pointerId" in event) {
|
|
1151
|
+
const pointerId = event.pointerId;
|
|
1152
|
+
const pointerState = state.internal.pointerMap.get(pointerId);
|
|
1153
|
+
if (pointerState?.captured.size) {
|
|
1154
|
+
for (const captureData of pointerState.captured.values()) {
|
|
1155
|
+
if (!duplicates.has(makeId(captureData.intersection))) intersections.push(captureData.intersection);
|
|
1156
|
+
}
|
|
691
1157
|
}
|
|
692
1158
|
}
|
|
693
1159
|
return intersections;
|
|
@@ -700,28 +1166,26 @@ function createEvents(store) {
|
|
|
700
1166
|
if (state) {
|
|
701
1167
|
const { raycaster, pointer, camera, internal } = state;
|
|
702
1168
|
const unprojectedPoint = new Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
703
|
-
const hasPointerCapture = (id) =>
|
|
1169
|
+
const hasPointerCapture = (id) => {
|
|
1170
|
+
const pointerState = internal.pointerMap.get(id);
|
|
1171
|
+
return pointerState?.captured.has(hit.eventObject) ?? false;
|
|
1172
|
+
};
|
|
704
1173
|
const setPointerCapture = (id) => {
|
|
705
1174
|
const captureData = { intersection: hit, target: event.target };
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
} else {
|
|
709
|
-
internal.capturedMap.set(id, /* @__PURE__ */ new Map([[hit.eventObject, captureData]]));
|
|
710
|
-
}
|
|
1175
|
+
const pointerState = getPointerState(internal, id);
|
|
1176
|
+
pointerState.captured.set(hit.eventObject, captureData);
|
|
711
1177
|
event.target.setPointerCapture(id);
|
|
712
1178
|
};
|
|
713
1179
|
const releasePointerCapture = (id) => {
|
|
714
|
-
|
|
715
|
-
if (captures) {
|
|
716
|
-
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
717
|
-
}
|
|
1180
|
+
releaseInternalPointerCapture(internal, hit.eventObject, id);
|
|
718
1181
|
};
|
|
719
|
-
|
|
720
|
-
for (
|
|
721
|
-
|
|
1182
|
+
const extractEventProps = {};
|
|
1183
|
+
for (const prop in event) {
|
|
1184
|
+
const property = event[prop];
|
|
722
1185
|
if (typeof property !== "function") extractEventProps[prop] = property;
|
|
723
1186
|
}
|
|
724
|
-
|
|
1187
|
+
const eventPointerId = "pointerId" in event ? event.pointerId : void 0;
|
|
1188
|
+
const raycastEvent = {
|
|
725
1189
|
...hit,
|
|
726
1190
|
...extractEventProps,
|
|
727
1191
|
pointer,
|
|
@@ -731,18 +1195,19 @@ function createEvents(store) {
|
|
|
731
1195
|
unprojectedPoint,
|
|
732
1196
|
ray: raycaster.ray,
|
|
733
1197
|
camera,
|
|
1198
|
+
pointerId: eventPointerId,
|
|
734
1199
|
// Hijack stopPropagation, which just sets a flag
|
|
735
1200
|
stopPropagation() {
|
|
736
|
-
const
|
|
1201
|
+
const pointerState = eventPointerId !== void 0 ? internal.pointerMap.get(eventPointerId) : void 0;
|
|
737
1202
|
if (
|
|
738
1203
|
// ...if this pointer hasn't been captured
|
|
739
|
-
!
|
|
740
|
-
|
|
1204
|
+
!pointerState?.captured.size || // ... or if the hit object is capturing the pointer
|
|
1205
|
+
pointerState.captured.has(hit.eventObject)
|
|
741
1206
|
) {
|
|
742
1207
|
raycastEvent.stopped = localState.stopped = true;
|
|
743
|
-
if (
|
|
1208
|
+
if (pointerState?.hovered.size && Array.from(pointerState.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
744
1209
|
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
745
|
-
cancelPointer([...higher, hit]);
|
|
1210
|
+
cancelPointer([...higher, hit], eventPointerId);
|
|
746
1211
|
}
|
|
747
1212
|
}
|
|
748
1213
|
},
|
|
@@ -758,15 +1223,18 @@ function createEvents(store) {
|
|
|
758
1223
|
}
|
|
759
1224
|
return intersections;
|
|
760
1225
|
}
|
|
761
|
-
function cancelPointer(intersections) {
|
|
1226
|
+
function cancelPointer(intersections, pointerId) {
|
|
762
1227
|
const { internal } = store.getState();
|
|
763
|
-
|
|
1228
|
+
const pid = pointerId ?? DEFAULT_POINTER_ID;
|
|
1229
|
+
const pointerState = internal.pointerMap.get(pid);
|
|
1230
|
+
if (!pointerState) return;
|
|
1231
|
+
for (const [hoveredId, hoveredObj] of pointerState.hovered) {
|
|
764
1232
|
if (!intersections.length || !intersections.find(
|
|
765
1233
|
(hit) => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId
|
|
766
1234
|
)) {
|
|
767
1235
|
const eventObject = hoveredObj.eventObject;
|
|
768
1236
|
const instance = eventObject.__r3f;
|
|
769
|
-
|
|
1237
|
+
pointerState.hovered.delete(hoveredId);
|
|
770
1238
|
if (instance?.eventCount) {
|
|
771
1239
|
const handlers = instance.handlers;
|
|
772
1240
|
const data = { ...hoveredObj, intersections };
|
|
@@ -795,41 +1263,118 @@ function createEvents(store) {
|
|
|
795
1263
|
instance?.handlers.onDropMissed?.(event);
|
|
796
1264
|
}
|
|
797
1265
|
}
|
|
1266
|
+
function cleanupPointer(pointerId) {
|
|
1267
|
+
const { internal } = store.getState();
|
|
1268
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1269
|
+
if (pointerState) {
|
|
1270
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1271
|
+
const eventObject = hoveredObj.eventObject;
|
|
1272
|
+
const instance = eventObject.__r3f;
|
|
1273
|
+
if (instance?.eventCount) {
|
|
1274
|
+
const handlers = instance.handlers;
|
|
1275
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1276
|
+
handlers.onPointerOut?.(data);
|
|
1277
|
+
handlers.onPointerLeave?.(data);
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
internal.pointerMap.delete(pointerId);
|
|
1281
|
+
}
|
|
1282
|
+
internal.pointerDirty.delete(pointerId);
|
|
1283
|
+
}
|
|
1284
|
+
function processDeferredPointer(event, pointerId) {
|
|
1285
|
+
const state = store.getState();
|
|
1286
|
+
const { internal } = state;
|
|
1287
|
+
if (!state.events.enabled) return;
|
|
1288
|
+
const filter = filterPointerEvents;
|
|
1289
|
+
const hits = intersect(event, filter);
|
|
1290
|
+
cancelPointer(hits, pointerId);
|
|
1291
|
+
function onIntersect(data) {
|
|
1292
|
+
const eventObject = data.eventObject;
|
|
1293
|
+
const instance = eventObject.__r3f;
|
|
1294
|
+
if (!instance?.eventCount) return;
|
|
1295
|
+
const handlers = instance.handlers;
|
|
1296
|
+
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1297
|
+
const id = makeId(data);
|
|
1298
|
+
const pointerState = getPointerState(internal, pointerId);
|
|
1299
|
+
const hoveredItem = pointerState.hovered.get(id);
|
|
1300
|
+
if (!hoveredItem) {
|
|
1301
|
+
pointerState.hovered.set(id, data);
|
|
1302
|
+
handlers.onPointerOver?.(data);
|
|
1303
|
+
handlers.onPointerEnter?.(data);
|
|
1304
|
+
} else if (hoveredItem.stopped) {
|
|
1305
|
+
data.stopPropagation();
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
handlers.onPointerMove?.(data);
|
|
1309
|
+
}
|
|
1310
|
+
handleIntersects(hits, event, 0, onIntersect);
|
|
1311
|
+
}
|
|
798
1312
|
function handlePointer(name) {
|
|
799
1313
|
switch (name) {
|
|
800
1314
|
case "onPointerLeave":
|
|
801
|
-
case "onPointerCancel":
|
|
802
1315
|
case "onDragLeave":
|
|
803
1316
|
return () => cancelPointer([]);
|
|
1317
|
+
// Global cancel of these events
|
|
1318
|
+
case "onPointerCancel":
|
|
1319
|
+
return (event) => {
|
|
1320
|
+
const pointerId = getPointerId(event);
|
|
1321
|
+
cleanupPointer(pointerId);
|
|
1322
|
+
};
|
|
804
1323
|
case "onLostPointerCapture":
|
|
805
1324
|
return (event) => {
|
|
806
1325
|
const { internal } = store.getState();
|
|
807
|
-
|
|
1326
|
+
const pointerId = getPointerId(event);
|
|
1327
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1328
|
+
if (pointerState?.captured.size) {
|
|
808
1329
|
requestAnimationFrame(() => {
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
1330
|
+
const pointerState2 = internal.pointerMap.get(pointerId);
|
|
1331
|
+
if (pointerState2?.captured.size) {
|
|
1332
|
+
pointerState2.captured.clear();
|
|
812
1333
|
}
|
|
1334
|
+
cancelPointer([], pointerId);
|
|
813
1335
|
});
|
|
814
1336
|
}
|
|
815
1337
|
};
|
|
816
1338
|
}
|
|
817
1339
|
return function handleEvent(event) {
|
|
818
1340
|
const state = store.getState();
|
|
819
|
-
const { onPointerMissed, onDragOverMissed, onDropMissed, internal } = state;
|
|
1341
|
+
const { onPointerMissed, onDragOverMissed, onDropMissed, internal, events } = state;
|
|
1342
|
+
const pointerId = getPointerId(event);
|
|
820
1343
|
internal.lastEvent.current = event;
|
|
821
|
-
if (!
|
|
1344
|
+
if (!events.enabled) return;
|
|
822
1345
|
const isPointerMove = name === "onPointerMove";
|
|
823
1346
|
const isDragOver = name === "onDragOver";
|
|
824
1347
|
const isDrop = name === "onDrop";
|
|
825
1348
|
const isClickEvent = name === "onClick" || name === "onContextMenu" || name === "onDoubleClick";
|
|
1349
|
+
const isPointerDown = name === "onPointerDown";
|
|
1350
|
+
const isPointerUp = name === "onPointerUp";
|
|
1351
|
+
const isWheel = name === "onWheel";
|
|
1352
|
+
const canDeferRaycasts = events.frameTimedRaycasts && state.frameloop === "always";
|
|
1353
|
+
if (isPointerMove && canDeferRaycasts) {
|
|
1354
|
+
events.compute?.(event, state);
|
|
1355
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
if (isWheel && canDeferRaycasts && !events.alwaysFireOnScroll) {
|
|
1359
|
+
events.compute?.(event, state);
|
|
1360
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1361
|
+
return;
|
|
1362
|
+
}
|
|
1363
|
+
if ((isClickEvent || isPointerDown || isPointerUp) && internal.pointerDirty.has(pointerId)) {
|
|
1364
|
+
const deferredEvent = internal.pointerDirty.get(pointerId);
|
|
1365
|
+
internal.pointerDirty.delete(pointerId);
|
|
1366
|
+
processDeferredPointer(deferredEvent, pointerId);
|
|
1367
|
+
}
|
|
826
1368
|
const filter = isPointerMove || isDragOver || isDrop ? filterPointerEvents : void 0;
|
|
827
1369
|
const hits = intersect(event, filter);
|
|
828
|
-
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
829
|
-
if (
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
1370
|
+
const delta = isClickEvent ? calculateDistance(event, pointerId) : 0;
|
|
1371
|
+
if (isPointerDown) {
|
|
1372
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1373
|
+
pointerState2.initialClick = [event.offsetX, event.offsetY];
|
|
1374
|
+
pointerState2.initialHits = hits.map((hit) => hit.eventObject);
|
|
1375
|
+
}
|
|
1376
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1377
|
+
const initialHits = pointerState?.initialHits ?? [];
|
|
833
1378
|
if (isClickEvent && !hits.length) {
|
|
834
1379
|
if (delta <= 2) {
|
|
835
1380
|
pointerMissed(event, internal.interaction);
|
|
@@ -844,7 +1389,9 @@ function createEvents(store) {
|
|
|
844
1389
|
dropMissed(event, internal.interaction);
|
|
845
1390
|
if (onDropMissed) onDropMissed(event);
|
|
846
1391
|
}
|
|
847
|
-
if (isPointerMove || isDragOver)
|
|
1392
|
+
if (isPointerMove || isDragOver) {
|
|
1393
|
+
cancelPointer(hits, pointerId);
|
|
1394
|
+
}
|
|
848
1395
|
function onIntersect(data) {
|
|
849
1396
|
const eventObject = data.eventObject;
|
|
850
1397
|
const instance = eventObject.__r3f;
|
|
@@ -853,9 +1400,10 @@ function createEvents(store) {
|
|
|
853
1400
|
if (isPointerMove) {
|
|
854
1401
|
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
855
1402
|
const id = makeId(data);
|
|
856
|
-
const
|
|
1403
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1404
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
857
1405
|
if (!hoveredItem) {
|
|
858
|
-
|
|
1406
|
+
pointerState2.hovered.set(id, data);
|
|
859
1407
|
handlers.onPointerOver?.(data);
|
|
860
1408
|
handlers.onPointerEnter?.(data);
|
|
861
1409
|
} else if (hoveredItem.stopped) {
|
|
@@ -865,9 +1413,10 @@ function createEvents(store) {
|
|
|
865
1413
|
handlers.onPointerMove?.(data);
|
|
866
1414
|
} else if (isDragOver) {
|
|
867
1415
|
const id = makeId(data);
|
|
868
|
-
const
|
|
1416
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1417
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
869
1418
|
if (!hoveredItem) {
|
|
870
|
-
|
|
1419
|
+
pointerState2.hovered.set(id, data);
|
|
871
1420
|
handlers.onDragOverEnter?.(data);
|
|
872
1421
|
} else if (hoveredItem.stopped) {
|
|
873
1422
|
data.stopPropagation();
|
|
@@ -878,18 +1427,18 @@ function createEvents(store) {
|
|
|
878
1427
|
} else {
|
|
879
1428
|
const handler = handlers[name];
|
|
880
1429
|
if (handler) {
|
|
881
|
-
if (!isClickEvent ||
|
|
1430
|
+
if (!isClickEvent || initialHits.includes(eventObject)) {
|
|
882
1431
|
pointerMissed(
|
|
883
1432
|
event,
|
|
884
|
-
internal.interaction.filter((object) => !
|
|
1433
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
885
1434
|
);
|
|
886
1435
|
handler(data);
|
|
887
1436
|
}
|
|
888
1437
|
} else {
|
|
889
|
-
if (isClickEvent &&
|
|
1438
|
+
if (isClickEvent && initialHits.includes(eventObject)) {
|
|
890
1439
|
pointerMissed(
|
|
891
1440
|
event,
|
|
892
|
-
internal.interaction.filter((object) => !
|
|
1441
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
893
1442
|
);
|
|
894
1443
|
}
|
|
895
1444
|
}
|
|
@@ -898,7 +1447,15 @@ function createEvents(store) {
|
|
|
898
1447
|
handleIntersects(hits, event, delta, onIntersect);
|
|
899
1448
|
};
|
|
900
1449
|
}
|
|
901
|
-
|
|
1450
|
+
function flushDeferredPointers() {
|
|
1451
|
+
const { internal, events } = store.getState();
|
|
1452
|
+
if (!events.frameTimedRaycasts) return;
|
|
1453
|
+
for (const [pointerId, event] of internal.pointerDirty) {
|
|
1454
|
+
processDeferredPointer(event, pointerId);
|
|
1455
|
+
}
|
|
1456
|
+
internal.pointerDirty.clear();
|
|
1457
|
+
}
|
|
1458
|
+
return { handlePointer, flushDeferredPointers, processDeferredPointer };
|
|
902
1459
|
}
|
|
903
1460
|
const DOM_EVENTS = {
|
|
904
1461
|
onClick: ["click", false],
|
|
@@ -917,11 +1474,16 @@ const DOM_EVENTS = {
|
|
|
917
1474
|
onLostPointerCapture: ["lostpointercapture", true]
|
|
918
1475
|
};
|
|
919
1476
|
function createPointerEvents(store) {
|
|
920
|
-
const { handlePointer } = createEvents(store);
|
|
1477
|
+
const { handlePointer, flushDeferredPointers, processDeferredPointer } = createEvents(store);
|
|
1478
|
+
let nextXRPointerId = XR_POINTER_ID_START;
|
|
1479
|
+
const xrPointers = /* @__PURE__ */ new Map();
|
|
921
1480
|
return {
|
|
922
1481
|
priority: 1,
|
|
923
1482
|
enabled: true,
|
|
924
|
-
|
|
1483
|
+
frameTimedRaycasts: true,
|
|
1484
|
+
alwaysFireOnScroll: true,
|
|
1485
|
+
updateOnFrame: false,
|
|
1486
|
+
compute(event, state) {
|
|
925
1487
|
state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
926
1488
|
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
927
1489
|
},
|
|
@@ -930,11 +1492,33 @@ function createPointerEvents(store) {
|
|
|
930
1492
|
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
931
1493
|
{}
|
|
932
1494
|
),
|
|
933
|
-
update: () => {
|
|
1495
|
+
update: (pointerId) => {
|
|
1496
|
+
const { events, internal } = store.getState();
|
|
1497
|
+
if (!events.handlers) return;
|
|
1498
|
+
if (pointerId !== void 0) {
|
|
1499
|
+
const event = internal.pointerDirty.get(pointerId);
|
|
1500
|
+
if (event) {
|
|
1501
|
+
internal.pointerDirty.delete(pointerId);
|
|
1502
|
+
processDeferredPointer(event, pointerId);
|
|
1503
|
+
} else if (internal.lastEvent?.current) {
|
|
1504
|
+
processDeferredPointer(internal.lastEvent.current, pointerId);
|
|
1505
|
+
}
|
|
1506
|
+
} else {
|
|
1507
|
+
flushDeferredPointers();
|
|
1508
|
+
if (internal.lastEvent?.current) {
|
|
1509
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
},
|
|
1513
|
+
flush: () => {
|
|
934
1514
|
const { events, internal } = store.getState();
|
|
935
|
-
|
|
1515
|
+
flushDeferredPointers();
|
|
1516
|
+
if (events.updateOnFrame && internal.lastEvent?.current && events.handlers) {
|
|
1517
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1518
|
+
}
|
|
936
1519
|
},
|
|
937
1520
|
connect: (target) => {
|
|
1521
|
+
if (!target) return;
|
|
938
1522
|
const { set, events } = store.getState();
|
|
939
1523
|
events.disconnect?.();
|
|
940
1524
|
set((state) => ({ events: { ...state.events, connected: target } }));
|
|
@@ -958,6 +1542,32 @@ function createPointerEvents(store) {
|
|
|
958
1542
|
}
|
|
959
1543
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
960
1544
|
}
|
|
1545
|
+
},
|
|
1546
|
+
registerPointer: (config) => {
|
|
1547
|
+
const pointerId = nextXRPointerId++;
|
|
1548
|
+
xrPointers.set(pointerId, config);
|
|
1549
|
+
const { internal } = store.getState();
|
|
1550
|
+
getPointerState(internal, pointerId);
|
|
1551
|
+
return pointerId;
|
|
1552
|
+
},
|
|
1553
|
+
unregisterPointer: (pointerId) => {
|
|
1554
|
+
xrPointers.delete(pointerId);
|
|
1555
|
+
const { internal } = store.getState();
|
|
1556
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1557
|
+
if (pointerState) {
|
|
1558
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1559
|
+
const eventObject = hoveredObj.eventObject;
|
|
1560
|
+
const instance = eventObject.__r3f;
|
|
1561
|
+
if (instance?.eventCount) {
|
|
1562
|
+
const handlers = instance.handlers;
|
|
1563
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1564
|
+
handlers.onPointerOut?.(data);
|
|
1565
|
+
handlers.onPointerLeave?.(data);
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
internal.pointerMap.delete(pointerId);
|
|
1569
|
+
}
|
|
1570
|
+
internal.pointerDirty.delete(pointerId);
|
|
961
1571
|
}
|
|
962
1572
|
};
|
|
963
1573
|
}
|
|
@@ -1048,23 +1658,29 @@ const createStore = (invalidate, advance) => {
|
|
|
1048
1658
|
set,
|
|
1049
1659
|
get,
|
|
1050
1660
|
// Mock objects that have to be configured
|
|
1661
|
+
// primaryStore is set after store creation (self-reference for primary, primary's store for secondary)
|
|
1662
|
+
primaryStore: null,
|
|
1051
1663
|
gl: null,
|
|
1052
1664
|
renderer: null,
|
|
1053
1665
|
camera: null,
|
|
1054
1666
|
frustum: new Frustum(),
|
|
1055
1667
|
autoUpdateFrustum: true,
|
|
1056
1668
|
raycaster: null,
|
|
1057
|
-
events: {
|
|
1669
|
+
events: {
|
|
1670
|
+
priority: 1,
|
|
1671
|
+
enabled: true,
|
|
1672
|
+
connected: false,
|
|
1673
|
+
frameTimedRaycasts: true,
|
|
1674
|
+
alwaysFireOnScroll: true,
|
|
1675
|
+
updateOnFrame: false
|
|
1676
|
+
},
|
|
1058
1677
|
scene: null,
|
|
1059
1678
|
rootScene: null,
|
|
1060
1679
|
xr: null,
|
|
1061
1680
|
inspector: null,
|
|
1062
1681
|
invalidate: (frames = 1, stackFrames = false) => invalidate(get(), frames, stackFrames),
|
|
1063
1682
|
advance: (timestamp, runGlobalEffects) => advance(timestamp, runGlobalEffects, get()),
|
|
1064
|
-
|
|
1065
|
-
linear: false,
|
|
1066
|
-
flat: false,
|
|
1067
|
-
textureColorSpace: "srgb",
|
|
1683
|
+
textureColorSpace: SRGBColorSpace,
|
|
1068
1684
|
isLegacy: false,
|
|
1069
1685
|
webGPUSupported: false,
|
|
1070
1686
|
isNative: false,
|
|
@@ -1104,10 +1720,40 @@ const createStore = (invalidate, advance) => {
|
|
|
1104
1720
|
getCurrentViewport
|
|
1105
1721
|
},
|
|
1106
1722
|
setEvents: (events) => set((state2) => ({ ...state2, events: { ...state2.events, ...events } })),
|
|
1107
|
-
setSize: (width, height, top
|
|
1108
|
-
const
|
|
1109
|
-
|
|
1110
|
-
|
|
1723
|
+
setSize: (width, height, top, left) => {
|
|
1724
|
+
const state2 = get();
|
|
1725
|
+
if (width === void 0) {
|
|
1726
|
+
set({ _sizeImperative: false });
|
|
1727
|
+
if (state2._sizeProps) {
|
|
1728
|
+
const { width: propW, height: propH } = state2._sizeProps;
|
|
1729
|
+
if (propW !== void 0 || propH !== void 0) {
|
|
1730
|
+
const currentSize = state2.size;
|
|
1731
|
+
const newSize = {
|
|
1732
|
+
width: propW ?? currentSize.width,
|
|
1733
|
+
height: propH ?? currentSize.height,
|
|
1734
|
+
top: currentSize.top,
|
|
1735
|
+
left: currentSize.left
|
|
1736
|
+
};
|
|
1737
|
+
set((s) => ({
|
|
1738
|
+
size: newSize,
|
|
1739
|
+
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
1740
|
+
}));
|
|
1741
|
+
getScheduler().invalidate();
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
return;
|
|
1745
|
+
}
|
|
1746
|
+
const w = width;
|
|
1747
|
+
const h = height ?? width;
|
|
1748
|
+
const t = top ?? state2.size.top;
|
|
1749
|
+
const l = left ?? state2.size.left;
|
|
1750
|
+
const size = { width: w, height: h, top: t, left: l };
|
|
1751
|
+
set((s) => ({
|
|
1752
|
+
size,
|
|
1753
|
+
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
1754
|
+
_sizeImperative: true
|
|
1755
|
+
}));
|
|
1756
|
+
getScheduler().invalidate();
|
|
1111
1757
|
},
|
|
1112
1758
|
setDpr: (dpr) => set((state2) => {
|
|
1113
1759
|
const resolved = calculateDpr(dpr);
|
|
@@ -1118,22 +1764,32 @@ const createStore = (invalidate, advance) => {
|
|
|
1118
1764
|
},
|
|
1119
1765
|
setError: (error) => set(() => ({ error })),
|
|
1120
1766
|
error: null,
|
|
1121
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useTextures,
|
|
1767
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
1122
1768
|
uniforms: {},
|
|
1123
1769
|
nodes: {},
|
|
1770
|
+
buffers: {},
|
|
1771
|
+
gpuStorage: {},
|
|
1124
1772
|
textures: /* @__PURE__ */ new Map(),
|
|
1125
|
-
|
|
1773
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1774
|
+
renderPipeline: null,
|
|
1126
1775
|
passes: {},
|
|
1776
|
+
_hmrVersion: 0,
|
|
1777
|
+
_sizeImperative: false,
|
|
1778
|
+
_sizeProps: null,
|
|
1127
1779
|
previousRoot: void 0,
|
|
1128
1780
|
internal: {
|
|
1129
1781
|
// Events
|
|
1130
1782
|
interaction: [],
|
|
1131
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
1132
1783
|
subscribers: [],
|
|
1784
|
+
// Per-pointer state (new unified structure)
|
|
1785
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1786
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1787
|
+
lastEvent: React.createRef(),
|
|
1788
|
+
// Deprecated but kept for backwards compatibility
|
|
1789
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1133
1790
|
initialClick: [0, 0],
|
|
1134
1791
|
initialHits: [],
|
|
1135
1792
|
capturedMap: /* @__PURE__ */ new Map(),
|
|
1136
|
-
lastEvent: React.createRef(),
|
|
1137
1793
|
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
1138
1794
|
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
1139
1795
|
// Occlusion system (WebGPU only)
|
|
@@ -1216,13 +1872,22 @@ const createStore = (invalidate, advance) => {
|
|
|
1216
1872
|
rootStore.subscribe(() => {
|
|
1217
1873
|
const { camera, size, viewport, set, internal } = rootStore.getState();
|
|
1218
1874
|
const actualRenderer = internal.actualRenderer;
|
|
1875
|
+
const canvasTarget = internal.canvasTarget;
|
|
1219
1876
|
if (size.width !== oldSize.width || size.height !== oldSize.height || viewport.dpr !== oldDpr) {
|
|
1220
1877
|
oldSize = size;
|
|
1221
1878
|
oldDpr = viewport.dpr;
|
|
1222
1879
|
updateCamera(camera, size);
|
|
1223
|
-
if (
|
|
1224
|
-
|
|
1225
|
-
|
|
1880
|
+
if (internal.isSecondary && canvasTarget) {
|
|
1881
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1882
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1883
|
+
} else {
|
|
1884
|
+
if (viewport.dpr > 0) actualRenderer.setPixelRatio(viewport.dpr);
|
|
1885
|
+
actualRenderer.setSize(size.width, size.height, false);
|
|
1886
|
+
if (canvasTarget) {
|
|
1887
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1888
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1226
1891
|
}
|
|
1227
1892
|
if (camera !== oldCamera) {
|
|
1228
1893
|
oldCamera = camera;
|
|
@@ -1293,1037 +1958,6 @@ useLoader.clear = function(loader, input) {
|
|
|
1293
1958
|
};
|
|
1294
1959
|
useLoader.loader = getLoader;
|
|
1295
1960
|
|
|
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
1961
|
function useFrame(callback, priorityOrOptions) {
|
|
2328
1962
|
const store = React.useContext(context);
|
|
2329
1963
|
const isInsideCanvas = store !== null;
|
|
@@ -2396,20 +2030,14 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2396
2030
|
}
|
|
2397
2031
|
};
|
|
2398
2032
|
} else {
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
unregisterJob = registerOutside();
|
|
2408
|
-
});
|
|
2409
|
-
return () => {
|
|
2410
|
-
unsubReady();
|
|
2411
|
-
unregisterJob?.();
|
|
2412
|
-
};
|
|
2033
|
+
return scheduler.register(
|
|
2034
|
+
(state, delta) => {
|
|
2035
|
+
const frameState = state;
|
|
2036
|
+
if (!frameState.renderer) return;
|
|
2037
|
+
callbackRef.current?.(frameState, delta);
|
|
2038
|
+
},
|
|
2039
|
+
{ id, ...options }
|
|
2040
|
+
);
|
|
2413
2041
|
}
|
|
2414
2042
|
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2415
2043
|
const isPaused = React.useSyncExternalStore(
|
|
@@ -2500,22 +2128,29 @@ function buildFromCache(input, textureCache) {
|
|
|
2500
2128
|
function useTexture(input, optionsOrOnLoad) {
|
|
2501
2129
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
2502
2130
|
const store = useStore();
|
|
2503
|
-
const textureCache = useThree((state) => state.textures);
|
|
2504
2131
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
2505
|
-
const { onLoad, cache =
|
|
2132
|
+
const { onLoad, cache = true } = options;
|
|
2133
|
+
const onLoadRef = useRef(onLoad);
|
|
2134
|
+
onLoadRef.current = onLoad;
|
|
2135
|
+
const onLoadCalledForRef = useRef(null);
|
|
2506
2136
|
const urls = useMemo(() => getUrls(input), [input]);
|
|
2507
2137
|
const cachedResult = useMemo(() => {
|
|
2508
2138
|
if (!cache) return null;
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2139
|
+
const textures = store.getState().textures;
|
|
2140
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2141
|
+
return buildFromCache(input, textures);
|
|
2142
|
+
}, [cache, urls, input, store]);
|
|
2512
2143
|
const loadedTextures = useLoader(
|
|
2513
2144
|
TextureLoader,
|
|
2514
2145
|
IsObject(input) ? Object.values(input) : input
|
|
2515
2146
|
);
|
|
2147
|
+
const inputKey = urls.join("\0");
|
|
2516
2148
|
useLayoutEffect(() => {
|
|
2517
|
-
if (
|
|
2518
|
-
|
|
2149
|
+
if (cachedResult) return;
|
|
2150
|
+
if (onLoadCalledForRef.current === inputKey) return;
|
|
2151
|
+
onLoadCalledForRef.current = inputKey;
|
|
2152
|
+
onLoadRef.current?.(loadedTextures);
|
|
2153
|
+
}, [cachedResult, loadedTextures, inputKey]);
|
|
2519
2154
|
useEffect(() => {
|
|
2520
2155
|
if (cachedResult) return;
|
|
2521
2156
|
if ("initTexture" in renderer) {
|
|
@@ -2548,8 +2183,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
2548
2183
|
}, [input, loadedTextures, cachedResult]);
|
|
2549
2184
|
useEffect(() => {
|
|
2550
2185
|
if (!cache) return;
|
|
2551
|
-
if (cachedResult) return;
|
|
2552
|
-
const set = store.setState;
|
|
2553
2186
|
const urlTextureMap = [];
|
|
2554
2187
|
if (typeof input === "string") {
|
|
2555
2188
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -2563,18 +2196,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
2563
2196
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
2564
2197
|
}
|
|
2565
2198
|
}
|
|
2566
|
-
|
|
2567
|
-
const
|
|
2568
|
-
let
|
|
2199
|
+
store.setState((state) => {
|
|
2200
|
+
const refs = new Map(state._textureRefs);
|
|
2201
|
+
let textures = state.textures;
|
|
2202
|
+
let added = false;
|
|
2569
2203
|
for (const [url, texture] of urlTextureMap) {
|
|
2570
|
-
if (!
|
|
2571
|
-
|
|
2572
|
-
|
|
2204
|
+
if (!textures.has(url)) {
|
|
2205
|
+
if (!added) {
|
|
2206
|
+
textures = new Map(textures);
|
|
2207
|
+
added = true;
|
|
2208
|
+
}
|
|
2209
|
+
textures.set(url, texture);
|
|
2573
2210
|
}
|
|
2211
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
2574
2212
|
}
|
|
2575
|
-
return
|
|
2213
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
2214
|
+
});
|
|
2215
|
+
return () => store.setState((state) => {
|
|
2216
|
+
const refs = new Map(state._textureRefs);
|
|
2217
|
+
for (const [url] of urlTextureMap) {
|
|
2218
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2219
|
+
if (next <= 0) refs.delete(url);
|
|
2220
|
+
else refs.set(url, next);
|
|
2221
|
+
}
|
|
2222
|
+
return { _textureRefs: refs };
|
|
2576
2223
|
});
|
|
2577
|
-
}, [cache, input, mappedTextures, store
|
|
2224
|
+
}, [cache, input, mappedTextures, store]);
|
|
2578
2225
|
return mappedTextures;
|
|
2579
2226
|
}
|
|
2580
2227
|
useTexture.preload = (url) => useLoader.preload(TextureLoader, url);
|
|
@@ -2590,108 +2237,92 @@ const Texture = ({
|
|
|
2590
2237
|
return /* @__PURE__ */ jsx(Fragment, { children: children?.(ret) });
|
|
2591
2238
|
};
|
|
2592
2239
|
|
|
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() {
|
|
2240
|
+
function useTextures(selector) {
|
|
2601
2241
|
const store = useStore();
|
|
2602
|
-
|
|
2603
|
-
const set = store.setState;
|
|
2242
|
+
const registry = useMemo(() => {
|
|
2604
2243
|
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
|
-
};
|
|
2244
|
+
const setState = store.setState;
|
|
2245
|
+
const getOne = (key) => getState().textures.get(key);
|
|
2663
2246
|
return {
|
|
2664
|
-
|
|
2665
|
-
get textures() {
|
|
2247
|
+
get all() {
|
|
2666
2248
|
return getState().textures;
|
|
2667
2249
|
},
|
|
2668
|
-
|
|
2669
|
-
|
|
2250
|
+
get(input) {
|
|
2251
|
+
if (typeof input === "string") return getOne(input);
|
|
2252
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2253
|
+
const out = {};
|
|
2254
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2255
|
+
return out;
|
|
2256
|
+
},
|
|
2670
2257
|
has: (key) => getState().textures.has(key),
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2258
|
+
add(keyOrRecord, texture) {
|
|
2259
|
+
setState((state) => {
|
|
2260
|
+
const textures = new Map(state.textures);
|
|
2261
|
+
if (typeof keyOrRecord === "string") {
|
|
2262
|
+
textures.set(keyOrRecord, texture);
|
|
2263
|
+
} else {
|
|
2264
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2265
|
+
}
|
|
2266
|
+
return { textures };
|
|
2267
|
+
});
|
|
2268
|
+
},
|
|
2269
|
+
dispose(key, options) {
|
|
2270
|
+
const state = getState();
|
|
2271
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2272
|
+
if (refs > 0 && !options?.force) {
|
|
2273
|
+
console.warn(
|
|
2274
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2275
|
+
);
|
|
2276
|
+
return false;
|
|
2277
|
+
}
|
|
2278
|
+
state.textures.get(key)?.dispose();
|
|
2279
|
+
setState((s) => {
|
|
2280
|
+
const textures = new Map(s.textures);
|
|
2281
|
+
textures.delete(key);
|
|
2282
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2283
|
+
nextRefs.delete(key);
|
|
2284
|
+
return { textures, _textureRefs: nextRefs };
|
|
2285
|
+
});
|
|
2286
|
+
return true;
|
|
2287
|
+
},
|
|
2288
|
+
disposeAll() {
|
|
2289
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2290
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2291
|
+
}
|
|
2681
2292
|
};
|
|
2682
2293
|
}, [store]);
|
|
2294
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2295
|
+
const selected = useThree(subscribe);
|
|
2296
|
+
return selector ? selected : registry;
|
|
2683
2297
|
}
|
|
2684
2298
|
|
|
2685
|
-
function useRenderTarget(
|
|
2299
|
+
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
2686
2300
|
const isLegacy = useThree((s) => s.isLegacy);
|
|
2687
2301
|
const size = useThree((s) => s.size);
|
|
2302
|
+
let width;
|
|
2303
|
+
let height;
|
|
2304
|
+
let opts;
|
|
2305
|
+
if (typeof widthOrOptions === "object") {
|
|
2306
|
+
opts = widthOrOptions;
|
|
2307
|
+
} else if (typeof widthOrOptions === "number") {
|
|
2308
|
+
width = widthOrOptions;
|
|
2309
|
+
if (typeof heightOrOptions === "object") {
|
|
2310
|
+
height = widthOrOptions;
|
|
2311
|
+
opts = heightOrOptions;
|
|
2312
|
+
} else if (typeof heightOrOptions === "number") {
|
|
2313
|
+
height = heightOrOptions;
|
|
2314
|
+
opts = options;
|
|
2315
|
+
} else {
|
|
2316
|
+
height = widthOrOptions;
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2688
2319
|
return useMemo(() => {
|
|
2689
2320
|
const w = width ?? size.width;
|
|
2690
2321
|
const h = height ?? size.height;
|
|
2691
2322
|
{
|
|
2692
|
-
return isLegacy ? new WebGLRenderTarget(w, h,
|
|
2323
|
+
return isLegacy ? new WebGLRenderTarget(w, h, opts) : new RenderTarget(w, h, opts);
|
|
2693
2324
|
}
|
|
2694
|
-
}, [width, height, size.width, size.height,
|
|
2325
|
+
}, [width, height, size.width, size.height, opts, isLegacy]);
|
|
2695
2326
|
}
|
|
2696
2327
|
|
|
2697
2328
|
function useStore() {
|
|
@@ -2741,28 +2372,18 @@ function addTail(callback) {
|
|
|
2741
2372
|
function invalidate(state, frames = 1, stackFrames = false) {
|
|
2742
2373
|
getScheduler().invalidate(frames, stackFrames);
|
|
2743
2374
|
}
|
|
2744
|
-
function advance(timestamp
|
|
2375
|
+
function advance(timestamp) {
|
|
2745
2376
|
getScheduler().step(timestamp);
|
|
2746
2377
|
}
|
|
2747
2378
|
|
|
2748
|
-
const version = "10.0.0-alpha.
|
|
2379
|
+
const version = "10.0.0-alpha.2";
|
|
2749
2380
|
const packageData = {
|
|
2750
2381
|
version: version};
|
|
2751
2382
|
|
|
2752
2383
|
function Xb(Tt) {
|
|
2753
2384
|
return Tt && Tt.__esModule && Object.prototype.hasOwnProperty.call(Tt, "default") ? Tt.default : Tt;
|
|
2754
2385
|
}
|
|
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;
|
|
2386
|
+
var Rm = { exports: {} }, Og = { exports: {} }, _b;
|
|
2766
2387
|
function Kb() {
|
|
2767
2388
|
return _b || (_b = 1, (function(Tt) {
|
|
2768
2389
|
Tt.exports = function(m) {
|
|
@@ -3834,7 +3455,6 @@ Error generating stack: ` + l.message + `
|
|
|
3834
3455
|
if (J === cl || J === jc) throw J;
|
|
3835
3456
|
var Ge = Yn(29, J, null, P.mode);
|
|
3836
3457
|
return Ge.lanes = H, Ge.return = P, Ge;
|
|
3837
|
-
} finally {
|
|
3838
3458
|
}
|
|
3839
3459
|
};
|
|
3840
3460
|
}
|
|
@@ -4488,7 +4108,6 @@ Error generating stack: ` + l.message + `
|
|
|
4488
4108
|
var h = r.lastRenderedState, y = d(h, a);
|
|
4489
4109
|
if (c.hasEagerState = true, c.eagerState = y, jn(y, h)) return go(t, r, c, 0), Ne === null && Bn(), false;
|
|
4490
4110
|
} catch {
|
|
4491
|
-
} finally {
|
|
4492
4111
|
}
|
|
4493
4112
|
if (a = yo(t, r, c, l), a !== null) return nt(a, t, l), ns(a, r, l), true;
|
|
4494
4113
|
}
|
|
@@ -6909,10 +6528,7 @@ Error generating stack: ` + l.message + `
|
|
|
6909
6528
|
function vr(t, r) {
|
|
6910
6529
|
Sf(t, r), (t = t.alternate) && Sf(t, r);
|
|
6911
6530
|
}
|
|
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;
|
|
6531
|
+
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
6532
|
m.cloneMutableInstance;
|
|
6917
6533
|
var yc = m.appendInitialChild, Kp = m.finalizeInitialChildren, Rs = m.shouldSetTextContent, bc = m.createTextInstance;
|
|
6918
6534
|
m.cloneMutableTextInstance;
|
|
@@ -7281,17 +6897,7 @@ No matching component was found for:
|
|
|
7281
6897
|
}, Tt.exports.default = Tt.exports, Object.defineProperty(Tt.exports, "__esModule", { value: true });
|
|
7282
6898
|
})(Og)), Og.exports;
|
|
7283
6899
|
}
|
|
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;
|
|
6900
|
+
var Mg = { exports: {} }, Rb;
|
|
7295
6901
|
function e0() {
|
|
7296
6902
|
return Rb || (Rb = 1, (function(Tt) {
|
|
7297
6903
|
process.env.NODE_ENV !== "production" && (Tt.exports = function(m) {
|
|
@@ -13058,10 +12664,7 @@ Check the render method of %s.`, G(di) || "Unknown")), i = zo(n), i.payload = {
|
|
|
13058
12664
|
function Ic() {
|
|
13059
12665
|
return di;
|
|
13060
12666
|
}
|
|
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;
|
|
12667
|
+
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
12668
|
m.cloneMutableInstance;
|
|
13066
12669
|
var bn = m.appendInitialChild, Ue = m.finalizeInitialChildren, ue = m.shouldSetTextContent, Do = m.createTextInstance;
|
|
13067
12670
|
m.cloneMutableTextInstance;
|
|
@@ -14029,15 +13632,6 @@ function n0() {
|
|
|
14029
13632
|
var t0 = n0();
|
|
14030
13633
|
const r0 = Xb(t0);
|
|
14031
13634
|
|
|
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
13635
|
const t = 1, o = 8, r = 32, e = 2;
|
|
14042
13636
|
|
|
14043
13637
|
function createReconciler(config) {
|
|
@@ -14064,10 +13658,11 @@ function extend(objects) {
|
|
|
14064
13658
|
function validateInstance(type, props) {
|
|
14065
13659
|
const name = toPascalCase(type);
|
|
14066
13660
|
const target = catalogue[name];
|
|
14067
|
-
if (type !== "primitive" && !target)
|
|
13661
|
+
if (type !== "primitive" && !target) {
|
|
14068
13662
|
throw new Error(
|
|
14069
13663
|
`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
13664
|
);
|
|
13665
|
+
}
|
|
14071
13666
|
if (type === "primitive" && !props.object) throw new Error(`R3F: Primitives without 'object' are invalid!`);
|
|
14072
13667
|
if (props.args !== void 0 && !Array.isArray(props.args)) throw new Error("R3F: The args prop must be an array!");
|
|
14073
13668
|
}
|
|
@@ -14231,6 +13826,7 @@ function swapInstances() {
|
|
|
14231
13826
|
instance.object = instance.props.object ?? new target(...instance.props.args ?? []);
|
|
14232
13827
|
instance.object.__r3f = instance;
|
|
14233
13828
|
setFiberRef(fiber, instance.object);
|
|
13829
|
+
delete instance.appliedOnce;
|
|
14234
13830
|
applyProps(instance.object, instance.props);
|
|
14235
13831
|
if (instance.props.attach) {
|
|
14236
13832
|
attach(parent, instance);
|
|
@@ -14304,8 +13900,22 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14304
13900
|
const isTailSibling = fiber.sibling === null || (fiber.flags & Update) === NoFlags;
|
|
14305
13901
|
if (isTailSibling) swapInstances();
|
|
14306
13902
|
},
|
|
14307
|
-
finalizeInitialChildren: () =>
|
|
14308
|
-
|
|
13903
|
+
finalizeInitialChildren: (instance) => {
|
|
13904
|
+
for (const prop in instance.props) {
|
|
13905
|
+
if (isFromRef(instance.props[prop])) return true;
|
|
13906
|
+
}
|
|
13907
|
+
return false;
|
|
13908
|
+
},
|
|
13909
|
+
commitMount(instance) {
|
|
13910
|
+
const resolved = {};
|
|
13911
|
+
for (const prop in instance.props) {
|
|
13912
|
+
const value = instance.props[prop];
|
|
13913
|
+
if (isFromRef(value)) {
|
|
13914
|
+
const ref = value[FROM_REF];
|
|
13915
|
+
if (ref.current != null) resolved[prop] = ref.current;
|
|
13916
|
+
}
|
|
13917
|
+
}
|
|
13918
|
+
if (Object.keys(resolved).length) applyProps(instance.object, resolved);
|
|
14309
13919
|
},
|
|
14310
13920
|
getPublicInstance: (instance) => instance?.object,
|
|
14311
13921
|
prepareForCommit: () => null,
|
|
@@ -14518,14 +14128,17 @@ function createRoot(canvas) {
|
|
|
14518
14128
|
if (!prevRoot) _roots.set(canvas, { fiber, store });
|
|
14519
14129
|
let onCreated;
|
|
14520
14130
|
let lastCamera;
|
|
14521
|
-
|
|
14131
|
+
const lastConfiguredProps = {};
|
|
14522
14132
|
let configured = false;
|
|
14523
14133
|
let pending = null;
|
|
14524
14134
|
return {
|
|
14525
14135
|
async configure(props = {}) {
|
|
14526
14136
|
let resolve;
|
|
14527
14137
|
pending = new Promise((_resolve) => resolve = _resolve);
|
|
14528
|
-
|
|
14138
|
+
const {
|
|
14139
|
+
id: canvasId,
|
|
14140
|
+
primaryCanvas,
|
|
14141
|
+
scheduler: schedulerConfig,
|
|
14529
14142
|
gl: glConfig,
|
|
14530
14143
|
renderer: rendererConfig,
|
|
14531
14144
|
size: propsSize,
|
|
@@ -14533,10 +14146,6 @@ function createRoot(canvas) {
|
|
|
14533
14146
|
events,
|
|
14534
14147
|
onCreated: onCreatedCallback,
|
|
14535
14148
|
shadows = false,
|
|
14536
|
-
linear = false,
|
|
14537
|
-
flat = false,
|
|
14538
|
-
textureColorSpace = SRGBColorSpace,
|
|
14539
|
-
legacy = false,
|
|
14540
14149
|
orthographic = false,
|
|
14541
14150
|
frameloop = "always",
|
|
14542
14151
|
dpr = [1, 2],
|
|
@@ -14547,9 +14156,12 @@ function createRoot(canvas) {
|
|
|
14547
14156
|
onDragOverMissed,
|
|
14548
14157
|
onDropMissed,
|
|
14549
14158
|
autoUpdateFrustum = true,
|
|
14550
|
-
occlusion = false
|
|
14159
|
+
occlusion = false,
|
|
14160
|
+
_sizeProps,
|
|
14161
|
+
forceEven
|
|
14551
14162
|
} = props;
|
|
14552
|
-
|
|
14163
|
+
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || SRGBColorSpace;
|
|
14164
|
+
const state = store.getState();
|
|
14553
14165
|
const defaultGLProps = {
|
|
14554
14166
|
canvas,
|
|
14555
14167
|
powerPreference: "high-performance",
|
|
@@ -14557,7 +14169,8 @@ function createRoot(canvas) {
|
|
|
14557
14169
|
alpha: true
|
|
14558
14170
|
};
|
|
14559
14171
|
const defaultGPUProps = {
|
|
14560
|
-
canvas
|
|
14172
|
+
canvas,
|
|
14173
|
+
antialias: true
|
|
14561
14174
|
};
|
|
14562
14175
|
const wantsGL = (state.isLegacy || glConfig || !R3F_BUILD_WEBGPU || !rendererConfig);
|
|
14563
14176
|
if (glConfig && rendererConfig) {
|
|
@@ -14571,26 +14184,70 @@ function createRoot(canvas) {
|
|
|
14571
14184
|
});
|
|
14572
14185
|
}
|
|
14573
14186
|
let renderer = state.internal.actualRenderer;
|
|
14187
|
+
if (primaryCanvas && wantsGL) {
|
|
14188
|
+
throw new Error(
|
|
14189
|
+
"The `primaryCanvas` prop for multi-canvas rendering cannot be used with WebGL. Remove the `gl` prop or use WebGPU."
|
|
14190
|
+
);
|
|
14191
|
+
}
|
|
14574
14192
|
if (wantsGL && !state.internal.actualRenderer) {
|
|
14575
14193
|
renderer = await resolveRenderer(glConfig, defaultGLProps, WebGLRenderer);
|
|
14576
14194
|
state.internal.actualRenderer = renderer;
|
|
14577
|
-
state.set({ isLegacy: true, gl: renderer, renderer });
|
|
14195
|
+
state.set({ isLegacy: true, gl: renderer, renderer, primaryStore: store });
|
|
14196
|
+
} else if (!wantsGL && primaryCanvas && !state.internal.actualRenderer) {
|
|
14197
|
+
const primary = await waitForPrimary(primaryCanvas);
|
|
14198
|
+
renderer = primary.renderer;
|
|
14199
|
+
state.internal.actualRenderer = renderer;
|
|
14200
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14201
|
+
primary.store.setState((prev) => ({
|
|
14202
|
+
internal: { ...prev.internal, isMultiCanvas: true }
|
|
14203
|
+
}));
|
|
14204
|
+
state.set((prev) => ({
|
|
14205
|
+
webGPUSupported: primary.store.getState().webGPUSupported,
|
|
14206
|
+
renderer,
|
|
14207
|
+
primaryStore: primary.store,
|
|
14208
|
+
internal: {
|
|
14209
|
+
...prev.internal,
|
|
14210
|
+
canvasTarget,
|
|
14211
|
+
isMultiCanvas: true,
|
|
14212
|
+
isSecondary: true,
|
|
14213
|
+
targetId: primaryCanvas
|
|
14214
|
+
}
|
|
14215
|
+
}));
|
|
14578
14216
|
} else if (!wantsGL && !state.internal.actualRenderer) {
|
|
14579
14217
|
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, WebGPURenderer);
|
|
14580
14218
|
if (!renderer.hasInitialized?.()) {
|
|
14219
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14220
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14221
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14222
|
+
canvas.width = size2.width * pixelRatio;
|
|
14223
|
+
canvas.height = size2.height * pixelRatio;
|
|
14224
|
+
}
|
|
14581
14225
|
await renderer.init();
|
|
14582
14226
|
}
|
|
14583
14227
|
const backend = renderer.backend;
|
|
14584
14228
|
const isWebGPUBackend = backend && "isWebGPUBackend" in backend;
|
|
14585
14229
|
state.internal.actualRenderer = renderer;
|
|
14586
|
-
state.set({ webGPUSupported: isWebGPUBackend, renderer });
|
|
14230
|
+
state.set({ webGPUSupported: isWebGPUBackend, renderer, primaryStore: store });
|
|
14231
|
+
if (canvasId && !state.internal.isSecondary) {
|
|
14232
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14233
|
+
const unregisterPrimary = registerPrimary(canvasId, renderer, store);
|
|
14234
|
+
state.set((prev) => ({
|
|
14235
|
+
internal: {
|
|
14236
|
+
...prev.internal,
|
|
14237
|
+
canvasTarget,
|
|
14238
|
+
unregisterPrimary
|
|
14239
|
+
}
|
|
14240
|
+
}));
|
|
14241
|
+
}
|
|
14587
14242
|
}
|
|
14588
14243
|
let raycaster = state.raycaster;
|
|
14589
14244
|
if (!raycaster) state.set({ raycaster: raycaster = new Raycaster() });
|
|
14590
14245
|
const { params, ...options } = raycastOptions || {};
|
|
14591
14246
|
if (!is.equ(options, raycaster, shallowLoose)) applyProps(raycaster, { ...options });
|
|
14592
|
-
if (!is.equ(params, raycaster.params, shallowLoose))
|
|
14247
|
+
if (!is.equ(params, raycaster.params, shallowLoose)) {
|
|
14593
14248
|
applyProps(raycaster, { params: { ...raycaster.params, ...params } });
|
|
14249
|
+
}
|
|
14250
|
+
let tempCamera = state.camera;
|
|
14594
14251
|
if (!state.camera || state.camera === lastCamera && !is.equ(lastCamera, cameraOptions, shallowLoose)) {
|
|
14595
14252
|
lastCamera = cameraOptions;
|
|
14596
14253
|
const isCamera = cameraOptions?.isCamera;
|
|
@@ -14610,6 +14267,7 @@ function createRoot(canvas) {
|
|
|
14610
14267
|
if (!state.camera && !cameraOptions?.rotation) camera.lookAt(0, 0, 0);
|
|
14611
14268
|
}
|
|
14612
14269
|
state.set({ camera });
|
|
14270
|
+
tempCamera = camera;
|
|
14613
14271
|
raycaster.camera = camera;
|
|
14614
14272
|
}
|
|
14615
14273
|
if (!state.scene) {
|
|
@@ -14627,7 +14285,7 @@ function createRoot(canvas) {
|
|
|
14627
14285
|
rootScene: scene,
|
|
14628
14286
|
internal: { ...prev.internal, container: scene }
|
|
14629
14287
|
}));
|
|
14630
|
-
const camera =
|
|
14288
|
+
const camera = tempCamera;
|
|
14631
14289
|
if (camera && !camera.parent) scene.add(camera);
|
|
14632
14290
|
}
|
|
14633
14291
|
if (events && !state.events.handlers) {
|
|
@@ -14641,9 +14299,17 @@ function createRoot(canvas) {
|
|
|
14641
14299
|
wasEnabled = enabled;
|
|
14642
14300
|
});
|
|
14643
14301
|
}
|
|
14302
|
+
if (_sizeProps !== void 0) {
|
|
14303
|
+
state.set({ _sizeProps });
|
|
14304
|
+
}
|
|
14305
|
+
if (forceEven !== void 0 && state.internal.forceEven !== forceEven) {
|
|
14306
|
+
state.set((prev) => ({ internal: { ...prev.internal, forceEven } }));
|
|
14307
|
+
}
|
|
14644
14308
|
const size = computeInitialSize(canvas, propsSize);
|
|
14645
|
-
if (!is.equ(size, state.size, shallowLoose)) {
|
|
14309
|
+
if (!state._sizeImperative && !is.equ(size, state.size, shallowLoose)) {
|
|
14310
|
+
const wasImperative = state._sizeImperative;
|
|
14646
14311
|
state.setSize(size.width, size.height, size.top, size.left);
|
|
14312
|
+
if (!wasImperative) state.set({ _sizeImperative: false });
|
|
14647
14313
|
}
|
|
14648
14314
|
if (dpr !== void 0 && !is.equ(dpr, lastConfiguredProps.dpr, shallowLoose)) {
|
|
14649
14315
|
state.setDpr(dpr);
|
|
@@ -14665,10 +14331,10 @@ function createRoot(canvas) {
|
|
|
14665
14331
|
lastConfiguredProps.performance = performance;
|
|
14666
14332
|
}
|
|
14667
14333
|
if (!state.xr) {
|
|
14668
|
-
const handleXRFrame = (timestamp,
|
|
14334
|
+
const handleXRFrame = (timestamp, _frame) => {
|
|
14669
14335
|
const state2 = store.getState();
|
|
14670
14336
|
if (state2.frameloop === "never") return;
|
|
14671
|
-
advance(timestamp
|
|
14337
|
+
advance(timestamp);
|
|
14672
14338
|
};
|
|
14673
14339
|
const actualRenderer = state.internal.actualRenderer;
|
|
14674
14340
|
const handleSessionChange = () => {
|
|
@@ -14680,16 +14346,16 @@ function createRoot(canvas) {
|
|
|
14680
14346
|
};
|
|
14681
14347
|
const xr = {
|
|
14682
14348
|
connect() {
|
|
14683
|
-
const { gl, renderer: renderer2
|
|
14684
|
-
const
|
|
14685
|
-
|
|
14686
|
-
|
|
14349
|
+
const { gl, renderer: renderer2 } = store.getState();
|
|
14350
|
+
const xrManager = (renderer2 || gl).xr;
|
|
14351
|
+
xrManager.addEventListener("sessionstart", handleSessionChange);
|
|
14352
|
+
xrManager.addEventListener("sessionend", handleSessionChange);
|
|
14687
14353
|
},
|
|
14688
14354
|
disconnect() {
|
|
14689
|
-
const { gl, renderer: renderer2
|
|
14690
|
-
const
|
|
14691
|
-
|
|
14692
|
-
|
|
14355
|
+
const { gl, renderer: renderer2 } = store.getState();
|
|
14356
|
+
const xrManager = (renderer2 || gl).xr;
|
|
14357
|
+
xrManager.removeEventListener("sessionstart", handleSessionChange);
|
|
14358
|
+
xrManager.removeEventListener("sessionend", handleSessionChange);
|
|
14693
14359
|
}
|
|
14694
14360
|
};
|
|
14695
14361
|
if (typeof renderer.xr?.addEventListener === "function") xr.connect();
|
|
@@ -14701,69 +14367,92 @@ function createRoot(canvas) {
|
|
|
14701
14367
|
const oldType = renderer.shadowMap.type;
|
|
14702
14368
|
renderer.shadowMap.enabled = !!shadows;
|
|
14703
14369
|
if (is.boo(shadows)) {
|
|
14704
|
-
renderer.shadowMap.type =
|
|
14370
|
+
renderer.shadowMap.type = PCFShadowMap;
|
|
14705
14371
|
} else if (is.str(shadows)) {
|
|
14372
|
+
if (shadows === "soft") {
|
|
14373
|
+
notifyDepreciated({
|
|
14374
|
+
heading: 'shadows="soft" is deprecated',
|
|
14375
|
+
body: "Three has depreciated soft and improved basic PCFShadows, we converted for you.",
|
|
14376
|
+
link: "https://github.com/mrdoob/three.js/wiki/Migration-Guide?utm_source=chatgpt.com#181--182"
|
|
14377
|
+
});
|
|
14378
|
+
}
|
|
14706
14379
|
const types = {
|
|
14707
14380
|
basic: BasicShadowMap,
|
|
14708
14381
|
percentage: PCFShadowMap,
|
|
14709
|
-
soft:
|
|
14382
|
+
soft: PCFShadowMap,
|
|
14710
14383
|
variance: VSMShadowMap
|
|
14711
14384
|
};
|
|
14712
|
-
renderer.shadowMap.type = types[shadows] ??
|
|
14385
|
+
renderer.shadowMap.type = types[shadows] ?? PCFShadowMap;
|
|
14713
14386
|
} else if (is.obj(shadows)) {
|
|
14714
14387
|
Object.assign(renderer.shadowMap, shadows);
|
|
14715
14388
|
}
|
|
14716
|
-
if (oldEnabled !== renderer.shadowMap.enabled || oldType !== renderer.shadowMap.type)
|
|
14389
|
+
if (oldEnabled !== renderer.shadowMap.enabled || oldType !== renderer.shadowMap.type) {
|
|
14717
14390
|
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
|
-
}
|
|
14735
|
-
if (!configured || linearChanged) {
|
|
14736
|
-
renderer.outputColorSpace = linear ? LinearSRGBColorSpace : SRGBColorSpace;
|
|
14737
|
-
lastConfiguredProps.linear = linear;
|
|
14738
14391
|
}
|
|
14739
|
-
|
|
14740
|
-
|
|
14741
|
-
|
|
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 }));
|
|
14392
|
+
}
|
|
14393
|
+
if (!configured) {
|
|
14394
|
+
renderer.outputColorSpace = SRGBColorSpace;
|
|
14395
|
+
renderer.toneMapping = ACESFilmicToneMapping;
|
|
14746
14396
|
}
|
|
14747
14397
|
if (textureColorSpace !== lastConfiguredProps.textureColorSpace) {
|
|
14748
14398
|
if (state.textureColorSpace !== textureColorSpace) state.set(() => ({ textureColorSpace }));
|
|
14749
14399
|
lastConfiguredProps.textureColorSpace = textureColorSpace;
|
|
14750
14400
|
}
|
|
14751
|
-
|
|
14752
|
-
|
|
14401
|
+
const r3fProps = ["textureColorSpace"];
|
|
14402
|
+
const constructorOnlyProps = ["samples", "antialias", "alpha", "canvas", "powerPreference"];
|
|
14403
|
+
const nonApplyProps = [...r3fProps, ...constructorOnlyProps];
|
|
14404
|
+
if (glConfig && !is.fun(glConfig) && !isRenderer(glConfig) && !is.equ(glConfig, renderer, shallowLoose)) {
|
|
14405
|
+
const glProps = {};
|
|
14406
|
+
for (const key in glConfig) {
|
|
14407
|
+
if (!nonApplyProps.includes(key)) glProps[key] = glConfig[key];
|
|
14408
|
+
}
|
|
14409
|
+
applyProps(renderer, glProps);
|
|
14410
|
+
}
|
|
14753
14411
|
if (rendererConfig && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && state.renderer) {
|
|
14754
14412
|
const currentRenderer = state.renderer;
|
|
14755
14413
|
if (!is.equ(rendererConfig, currentRenderer, shallowLoose)) {
|
|
14756
|
-
|
|
14414
|
+
const rendererProps = {};
|
|
14415
|
+
for (const key in rendererConfig) {
|
|
14416
|
+
if (!nonApplyProps.includes(key)) rendererProps[key] = rendererConfig[key];
|
|
14417
|
+
}
|
|
14418
|
+
applyProps(currentRenderer, rendererProps);
|
|
14757
14419
|
}
|
|
14758
14420
|
}
|
|
14759
14421
|
const scheduler = getScheduler();
|
|
14760
14422
|
const rootId = state.internal.rootId;
|
|
14761
14423
|
if (!rootId) {
|
|
14762
|
-
const newRootId = scheduler.generateRootId();
|
|
14424
|
+
const newRootId = canvasId || scheduler.generateRootId();
|
|
14763
14425
|
const unregisterRoot = scheduler.registerRoot(newRootId, {
|
|
14764
14426
|
getState: () => store.getState(),
|
|
14765
14427
|
onError: (err) => store.getState().setError(err)
|
|
14766
14428
|
});
|
|
14429
|
+
const unregisterCanvasTarget = scheduler.register(
|
|
14430
|
+
() => {
|
|
14431
|
+
const state2 = store.getState();
|
|
14432
|
+
if (state2.internal.isMultiCanvas && state2.internal.canvasTarget) {
|
|
14433
|
+
const renderer2 = state2.internal.actualRenderer;
|
|
14434
|
+
renderer2.setCanvasTarget(state2.internal.canvasTarget);
|
|
14435
|
+
}
|
|
14436
|
+
},
|
|
14437
|
+
{
|
|
14438
|
+
id: `${newRootId}_canvasTarget`,
|
|
14439
|
+
rootId: newRootId,
|
|
14440
|
+
phase: "start",
|
|
14441
|
+
system: true
|
|
14442
|
+
}
|
|
14443
|
+
);
|
|
14444
|
+
const unregisterEventsFlush = scheduler.register(
|
|
14445
|
+
() => {
|
|
14446
|
+
const state2 = store.getState();
|
|
14447
|
+
state2.events.flush?.();
|
|
14448
|
+
},
|
|
14449
|
+
{
|
|
14450
|
+
id: `${newRootId}_events`,
|
|
14451
|
+
rootId: newRootId,
|
|
14452
|
+
phase: "input",
|
|
14453
|
+
system: true
|
|
14454
|
+
}
|
|
14455
|
+
);
|
|
14767
14456
|
const unregisterFrustum = scheduler.register(
|
|
14768
14457
|
() => {
|
|
14769
14458
|
const state2 = store.getState();
|
|
@@ -14774,7 +14463,7 @@ function createRoot(canvas) {
|
|
|
14774
14463
|
{
|
|
14775
14464
|
id: `${newRootId}_frustum`,
|
|
14776
14465
|
rootId: newRootId,
|
|
14777
|
-
|
|
14466
|
+
before: "render",
|
|
14778
14467
|
system: true
|
|
14779
14468
|
}
|
|
14780
14469
|
);
|
|
@@ -14786,7 +14475,7 @@ function createRoot(canvas) {
|
|
|
14786
14475
|
{
|
|
14787
14476
|
id: `${newRootId}_visibility`,
|
|
14788
14477
|
rootId: newRootId,
|
|
14789
|
-
|
|
14478
|
+
before: "render",
|
|
14790
14479
|
system: true,
|
|
14791
14480
|
after: `${newRootId}_frustum`
|
|
14792
14481
|
}
|
|
@@ -14798,18 +14487,22 @@ function createRoot(canvas) {
|
|
|
14798
14487
|
const userHandlesRender = scheduler.hasUserJobsInPhase("render", newRootId);
|
|
14799
14488
|
if (userHandlesRender || state2.internal.priority) return;
|
|
14800
14489
|
try {
|
|
14801
|
-
if (state2.
|
|
14490
|
+
if (state2.renderPipeline?.render) state2.renderPipeline.render();
|
|
14802
14491
|
else if (renderer2?.render) renderer2.render(state2.scene, state2.camera);
|
|
14803
14492
|
} catch (error) {
|
|
14804
14493
|
state2.setError(error instanceof Error ? error : new Error(String(error)));
|
|
14805
14494
|
}
|
|
14806
14495
|
},
|
|
14807
14496
|
{
|
|
14808
|
-
|
|
14497
|
+
// Use canvas ID directly as job ID if available, otherwise use generated rootId
|
|
14498
|
+
id: canvasId || `${newRootId}_render`,
|
|
14809
14499
|
rootId: newRootId,
|
|
14810
14500
|
phase: "render",
|
|
14811
|
-
system: true
|
|
14501
|
+
system: true,
|
|
14812
14502
|
// Internal flag: this is a system job, not user-controlled
|
|
14503
|
+
// Apply scheduler config for render ordering and rate limiting
|
|
14504
|
+
...schedulerConfig?.after && { after: schedulerConfig.after },
|
|
14505
|
+
...schedulerConfig?.fps && { fps: schedulerConfig.fps }
|
|
14813
14506
|
}
|
|
14814
14507
|
);
|
|
14815
14508
|
state.set((state2) => ({
|
|
@@ -14818,6 +14511,8 @@ function createRoot(canvas) {
|
|
|
14818
14511
|
rootId: newRootId,
|
|
14819
14512
|
unregisterRoot: () => {
|
|
14820
14513
|
unregisterRoot();
|
|
14514
|
+
unregisterCanvasTarget();
|
|
14515
|
+
unregisterEventsFlush();
|
|
14821
14516
|
unregisterFrustum();
|
|
14822
14517
|
unregisterVisibility();
|
|
14823
14518
|
unregisterRender();
|
|
@@ -14876,15 +14571,24 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
14876
14571
|
const renderer = state.internal.actualRenderer;
|
|
14877
14572
|
const unregisterRoot = state.internal.unregisterRoot;
|
|
14878
14573
|
if (unregisterRoot) unregisterRoot();
|
|
14574
|
+
const unregisterPrimary = state.internal.unregisterPrimary;
|
|
14575
|
+
if (unregisterPrimary) unregisterPrimary();
|
|
14576
|
+
const canvasTarget = state.internal.canvasTarget;
|
|
14577
|
+
if (canvasTarget?.dispose) canvasTarget.dispose();
|
|
14879
14578
|
state.events.disconnect?.();
|
|
14880
14579
|
cleanupHelperGroup(root.store);
|
|
14881
|
-
renderer
|
|
14882
|
-
|
|
14883
|
-
|
|
14580
|
+
if (state.isLegacy && renderer) {
|
|
14581
|
+
;
|
|
14582
|
+
renderer.renderLists?.dispose?.();
|
|
14583
|
+
renderer.forceContextLoss?.();
|
|
14584
|
+
}
|
|
14585
|
+
if (!state.internal.isSecondary) {
|
|
14586
|
+
if (renderer?.xr) state.xr.disconnect();
|
|
14587
|
+
}
|
|
14884
14588
|
dispose(state.scene);
|
|
14885
14589
|
_roots.delete(canvas);
|
|
14886
14590
|
if (callback) callback(canvas);
|
|
14887
|
-
} catch
|
|
14591
|
+
} catch {
|
|
14888
14592
|
}
|
|
14889
14593
|
}, 500);
|
|
14890
14594
|
}
|
|
@@ -14892,36 +14596,34 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
14892
14596
|
}
|
|
14893
14597
|
}
|
|
14894
14598
|
function createPortal(children, container, state) {
|
|
14895
|
-
return /* @__PURE__ */ jsx(
|
|
14599
|
+
return /* @__PURE__ */ jsx(Portal, { children, container, state });
|
|
14896
14600
|
}
|
|
14897
|
-
function
|
|
14601
|
+
function Portal({ children, container, state }) {
|
|
14898
14602
|
const isRef = useCallback((obj) => obj && "current" in obj, []);
|
|
14899
|
-
const [resolvedContainer,
|
|
14603
|
+
const [resolvedContainer, _setResolvedContainer] = useState(() => {
|
|
14900
14604
|
if (isRef(container)) return container.current ?? null;
|
|
14901
14605
|
return container;
|
|
14902
14606
|
});
|
|
14607
|
+
const setResolvedContainer = useCallback(
|
|
14608
|
+
(newContainer) => {
|
|
14609
|
+
if (!newContainer || newContainer === resolvedContainer) return;
|
|
14610
|
+
_setResolvedContainer(isRef(newContainer) ? newContainer.current : newContainer);
|
|
14611
|
+
},
|
|
14612
|
+
[resolvedContainer, _setResolvedContainer, isRef]
|
|
14613
|
+
);
|
|
14903
14614
|
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);
|
|
14615
|
+
if (isRef(container) && !container.current) {
|
|
14616
|
+
return queueMicrotask(() => {
|
|
14617
|
+
setResolvedContainer(container.current);
|
|
14618
|
+
});
|
|
14918
14619
|
}
|
|
14919
|
-
|
|
14620
|
+
setResolvedContainer(container);
|
|
14621
|
+
}, [container, isRef, setResolvedContainer]);
|
|
14920
14622
|
if (!resolvedContainer) return /* @__PURE__ */ jsx(Fragment, {});
|
|
14921
14623
|
const portalKey = resolvedContainer.uuid ?? `portal-${resolvedContainer.id ?? "unknown"}`;
|
|
14922
|
-
return /* @__PURE__ */ jsx(
|
|
14624
|
+
return /* @__PURE__ */ jsx(PortalInner, { children, container: resolvedContainer, state }, portalKey);
|
|
14923
14625
|
}
|
|
14924
|
-
function
|
|
14626
|
+
function PortalInner({ state = {}, children, container }) {
|
|
14925
14627
|
const { events, size, injectScene = true, ...rest } = state;
|
|
14926
14628
|
const previousRoot = useStore();
|
|
14927
14629
|
const [raycaster] = useState(() => new Raycaster());
|
|
@@ -14942,11 +14644,12 @@ function Portal({ state = {}, children, container }) {
|
|
|
14942
14644
|
};
|
|
14943
14645
|
}, [portalScene, container, injectScene]);
|
|
14944
14646
|
const inject = useMutableCallback((rootState, injectState) => {
|
|
14647
|
+
const resolvedSize = { ...rootState.size, ...injectState.size, ...size };
|
|
14945
14648
|
let viewport = void 0;
|
|
14946
|
-
if (injectState.camera && size) {
|
|
14649
|
+
if (injectState.camera && (size || injectState.size)) {
|
|
14947
14650
|
const camera = injectState.camera;
|
|
14948
|
-
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(),
|
|
14949
|
-
if (camera !== rootState.camera) updateCamera(camera,
|
|
14651
|
+
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(), resolvedSize);
|
|
14652
|
+
if (camera !== rootState.camera) updateCamera(camera, resolvedSize);
|
|
14950
14653
|
}
|
|
14951
14654
|
return {
|
|
14952
14655
|
// The intersect consists of the previous root state
|
|
@@ -14963,7 +14666,7 @@ function Portal({ state = {}, children, container }) {
|
|
|
14963
14666
|
previousRoot,
|
|
14964
14667
|
// Events, size and viewport can be overridden by the inject layer
|
|
14965
14668
|
events: { ...rootState.events, ...injectState.events, ...events },
|
|
14966
|
-
size:
|
|
14669
|
+
size: resolvedSize,
|
|
14967
14670
|
viewport: { ...rootState.viewport, ...viewport },
|
|
14968
14671
|
// Layers are allowed to override events
|
|
14969
14672
|
setEvents: (events2) => injectState.set((state2) => ({ ...state2, events: { ...state2.events, ...events2 } })),
|
|
@@ -14975,9 +14678,13 @@ function Portal({ state = {}, children, container }) {
|
|
|
14975
14678
|
const store = createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
14976
14679
|
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
14977
14680
|
onMutate(previousRoot.getState());
|
|
14978
|
-
previousRoot.subscribe(onMutate);
|
|
14979
14681
|
return store;
|
|
14980
14682
|
}, [previousRoot, container]);
|
|
14683
|
+
useIsomorphicLayoutEffect(() => {
|
|
14684
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14685
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14686
|
+
return unsubscribe;
|
|
14687
|
+
}, [previousRoot, usePortalStore]);
|
|
14981
14688
|
return (
|
|
14982
14689
|
// @ts-ignore, reconciler types are not maintained
|
|
14983
14690
|
/* @__PURE__ */ jsx(Fragment, { children: reconciler.createPortal(
|
|
@@ -14991,21 +14698,59 @@ function flushSync(fn) {
|
|
|
14991
14698
|
return reconciler.flushSyncFromReconciler(fn);
|
|
14992
14699
|
}
|
|
14993
14700
|
|
|
14701
|
+
function parseBackground(background) {
|
|
14702
|
+
if (!background) return null;
|
|
14703
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14704
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14705
|
+
return {
|
|
14706
|
+
...rest,
|
|
14707
|
+
preset,
|
|
14708
|
+
files: envMap || files,
|
|
14709
|
+
backgroundFiles: backgroundMap,
|
|
14710
|
+
background: true
|
|
14711
|
+
};
|
|
14712
|
+
}
|
|
14713
|
+
if (typeof background === "number") {
|
|
14714
|
+
return { color: background, background: true };
|
|
14715
|
+
}
|
|
14716
|
+
if (typeof background === "string") {
|
|
14717
|
+
if (background in presetsObj) {
|
|
14718
|
+
return { preset: background, background: true };
|
|
14719
|
+
}
|
|
14720
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14721
|
+
return { files: background, background: true };
|
|
14722
|
+
}
|
|
14723
|
+
return { color: background, background: true };
|
|
14724
|
+
}
|
|
14725
|
+
if (background.isColor) {
|
|
14726
|
+
return { color: background, background: true };
|
|
14727
|
+
}
|
|
14728
|
+
return null;
|
|
14729
|
+
}
|
|
14730
|
+
|
|
14731
|
+
function clearHmrCaches(store) {
|
|
14732
|
+
store.setState((state) => ({
|
|
14733
|
+
nodes: {},
|
|
14734
|
+
uniforms: {},
|
|
14735
|
+
buffers: {},
|
|
14736
|
+
gpuStorage: {},
|
|
14737
|
+
_hmrVersion: state._hmrVersion + 1
|
|
14738
|
+
}));
|
|
14739
|
+
}
|
|
14740
|
+
|
|
14994
14741
|
function CanvasImpl({
|
|
14995
14742
|
ref,
|
|
14996
14743
|
children,
|
|
14997
14744
|
fallback,
|
|
14998
14745
|
resize,
|
|
14999
14746
|
style,
|
|
14747
|
+
id,
|
|
15000
14748
|
gl,
|
|
15001
|
-
renderer,
|
|
14749
|
+
renderer: rendererProp,
|
|
15002
14750
|
events = createPointerEvents,
|
|
15003
14751
|
eventSource,
|
|
15004
14752
|
eventPrefix,
|
|
15005
14753
|
shadows,
|
|
15006
|
-
linear,
|
|
15007
|
-
flat,
|
|
15008
|
-
legacy,
|
|
15009
14754
|
orthographic,
|
|
15010
14755
|
frameloop,
|
|
15011
14756
|
dpr,
|
|
@@ -15013,14 +14758,34 @@ function CanvasImpl({
|
|
|
15013
14758
|
raycaster,
|
|
15014
14759
|
camera,
|
|
15015
14760
|
scene,
|
|
14761
|
+
autoUpdateFrustum,
|
|
14762
|
+
occlusion,
|
|
15016
14763
|
onPointerMissed,
|
|
15017
14764
|
onDragOverMissed,
|
|
15018
14765
|
onDropMissed,
|
|
15019
14766
|
onCreated,
|
|
14767
|
+
hmr,
|
|
14768
|
+
width,
|
|
14769
|
+
height,
|
|
14770
|
+
background,
|
|
14771
|
+
forceEven,
|
|
15020
14772
|
...props
|
|
15021
14773
|
}) {
|
|
14774
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14775
|
+
let primaryCanvas;
|
|
14776
|
+
let scheduler;
|
|
14777
|
+
let renderer;
|
|
14778
|
+
if (isRendererConfig) {
|
|
14779
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14780
|
+
primaryCanvas = pc;
|
|
14781
|
+
scheduler = sc;
|
|
14782
|
+
renderer = Object.keys(rest).length > 0 ? rest : rendererProp;
|
|
14783
|
+
} else {
|
|
14784
|
+
renderer = rendererProp;
|
|
14785
|
+
}
|
|
15022
14786
|
React.useMemo(() => extend(THREE), []);
|
|
15023
14787
|
const Bridge = useBridge();
|
|
14788
|
+
const backgroundProps = React.useMemo(() => parseBackground(background), [background]);
|
|
15024
14789
|
const hasInitialSizeRef = React.useRef(false);
|
|
15025
14790
|
const measureConfig = React.useMemo(() => {
|
|
15026
14791
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15037,7 +14802,21 @@ function CanvasImpl({
|
|
|
15037
14802
|
};
|
|
15038
14803
|
}, [resize, hasInitialSizeRef.current]);
|
|
15039
14804
|
const [containerRef, containerRect] = useMeasure(measureConfig);
|
|
15040
|
-
|
|
14805
|
+
const effectiveSize = React.useMemo(() => {
|
|
14806
|
+
let w = width ?? containerRect.width;
|
|
14807
|
+
let h = height ?? containerRect.height;
|
|
14808
|
+
if (forceEven) {
|
|
14809
|
+
w = Math.ceil(w / 2) * 2;
|
|
14810
|
+
h = Math.ceil(h / 2) * 2;
|
|
14811
|
+
}
|
|
14812
|
+
return {
|
|
14813
|
+
width: w,
|
|
14814
|
+
height: h,
|
|
14815
|
+
top: containerRect.top,
|
|
14816
|
+
left: containerRect.left
|
|
14817
|
+
};
|
|
14818
|
+
}, [width, height, containerRect, forceEven]);
|
|
14819
|
+
if (!hasInitialSizeRef.current && effectiveSize.width > 0 && effectiveSize.height > 0) {
|
|
15041
14820
|
hasInitialSizeRef.current = true;
|
|
15042
14821
|
}
|
|
15043
14822
|
const canvasRef = React.useRef(null);
|
|
@@ -15056,7 +14835,7 @@ function CanvasImpl({
|
|
|
15056
14835
|
useIsomorphicLayoutEffect(() => {
|
|
15057
14836
|
effectActiveRef.current = true;
|
|
15058
14837
|
const canvas = canvasRef.current;
|
|
15059
|
-
if (
|
|
14838
|
+
if (effectiveSize.width > 0 && effectiveSize.height > 0 && canvas) {
|
|
15060
14839
|
if (!root.current) {
|
|
15061
14840
|
root.current = createRoot(canvas);
|
|
15062
14841
|
notifyAlpha({
|
|
@@ -15076,21 +14855,26 @@ function CanvasImpl({
|
|
|
15076
14855
|
async function run() {
|
|
15077
14856
|
if (!effectActiveRef.current || !root.current) return;
|
|
15078
14857
|
await root.current.configure({
|
|
14858
|
+
id,
|
|
14859
|
+
primaryCanvas,
|
|
14860
|
+
scheduler,
|
|
15079
14861
|
gl,
|
|
15080
14862
|
renderer,
|
|
15081
14863
|
scene,
|
|
15082
14864
|
events,
|
|
15083
14865
|
shadows,
|
|
15084
|
-
linear,
|
|
15085
|
-
flat,
|
|
15086
|
-
legacy,
|
|
15087
14866
|
orthographic,
|
|
15088
14867
|
frameloop,
|
|
15089
14868
|
dpr,
|
|
15090
14869
|
performance,
|
|
15091
14870
|
raycaster,
|
|
15092
14871
|
camera,
|
|
15093
|
-
|
|
14872
|
+
autoUpdateFrustum,
|
|
14873
|
+
occlusion,
|
|
14874
|
+
size: effectiveSize,
|
|
14875
|
+
// Store size props for reset functionality
|
|
14876
|
+
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
14877
|
+
forceEven,
|
|
15094
14878
|
// Pass mutable reference to onPointerMissed so it's free to update
|
|
15095
14879
|
onPointerMissed: (...args) => handlePointerMissed.current?.(...args),
|
|
15096
14880
|
onDragOverMissed: (...args) => handleDragOverMissed.current?.(...args),
|
|
@@ -15114,7 +14898,10 @@ function CanvasImpl({
|
|
|
15114
14898
|
});
|
|
15115
14899
|
if (!effectActiveRef.current || !root.current) return;
|
|
15116
14900
|
root.current.render(
|
|
15117
|
-
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */
|
|
14901
|
+
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */ jsxs(React.Suspense, { fallback: /* @__PURE__ */ jsx(Block, { set: setBlock }), children: [
|
|
14902
|
+
backgroundProps && /* @__PURE__ */ jsx(Environment, { ...backgroundProps }),
|
|
14903
|
+
children ?? null
|
|
14904
|
+
] }) }) })
|
|
15118
14905
|
);
|
|
15119
14906
|
}
|
|
15120
14907
|
run();
|
|
@@ -15136,6 +14923,28 @@ function CanvasImpl({
|
|
|
15136
14923
|
};
|
|
15137
14924
|
}
|
|
15138
14925
|
}, []);
|
|
14926
|
+
React.useEffect(() => {
|
|
14927
|
+
if (hmr === false) return;
|
|
14928
|
+
const canvas = canvasRef.current;
|
|
14929
|
+
if (!canvas) return;
|
|
14930
|
+
const handleHMR = () => {
|
|
14931
|
+
queueMicrotask(() => {
|
|
14932
|
+
const rootEntry = _roots.get(canvas);
|
|
14933
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
14934
|
+
});
|
|
14935
|
+
};
|
|
14936
|
+
if (typeof import.meta !== "undefined" && import.meta.hot) {
|
|
14937
|
+
const hot = import.meta.hot;
|
|
14938
|
+
hot.on("vite:afterUpdate", handleHMR);
|
|
14939
|
+
return () => hot.off?.("vite:afterUpdate", handleHMR);
|
|
14940
|
+
}
|
|
14941
|
+
if (typeof module !== "undefined" && module.hot) {
|
|
14942
|
+
const hot = module.hot;
|
|
14943
|
+
hot.addStatusHandler((status) => {
|
|
14944
|
+
if (status === "idle") handleHMR();
|
|
14945
|
+
});
|
|
14946
|
+
}
|
|
14947
|
+
}, [hmr]);
|
|
15139
14948
|
const pointerEvents = eventSource ? "none" : "auto";
|
|
15140
14949
|
return /* @__PURE__ */ jsx(
|
|
15141
14950
|
"div",
|
|
@@ -15150,7 +14959,16 @@ function CanvasImpl({
|
|
|
15150
14959
|
...style
|
|
15151
14960
|
},
|
|
15152
14961
|
...props,
|
|
15153
|
-
children: /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
14962
|
+
children: /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
14963
|
+
"canvas",
|
|
14964
|
+
{
|
|
14965
|
+
ref: canvasRef,
|
|
14966
|
+
id,
|
|
14967
|
+
className: "r3f-canvas",
|
|
14968
|
+
style: { display: "block", width: "100%", height: "100%" },
|
|
14969
|
+
children: fallback
|
|
14970
|
+
}
|
|
14971
|
+
) })
|
|
15154
14972
|
}
|
|
15155
14973
|
);
|
|
15156
14974
|
}
|
|
@@ -15160,4 +14978,4 @@ function Canvas(props) {
|
|
|
15160
14978
|
|
|
15161
14979
|
extend(THREE);
|
|
15162
14980
|
|
|
15163
|
-
export { Block, Canvas, ErrorBoundary, IsObject, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS,
|
|
14981
|
+
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 };
|