@react-three/fiber 10.0.0-canary.d6fdf72 → 10.0.0-canary.e881a4a
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/dist/index.cjs +297 -1218
- package/dist/index.d.cts +71 -742
- package/dist/index.d.mts +71 -742
- package/dist/index.d.ts +71 -742
- package/dist/index.mjs +271 -1191
- package/dist/legacy.cjs +252 -1173
- package/dist/legacy.d.cts +71 -741
- package/dist/legacy.d.mts +71 -741
- package/dist/legacy.d.ts +71 -741
- package/dist/legacy.mjs +225 -1145
- package/dist/webgpu/index.cjs +818 -1962
- package/dist/webgpu/index.d.cts +104 -745
- package/dist/webgpu/index.d.mts +104 -745
- package/dist/webgpu/index.d.ts +104 -745
- package/dist/webgpu/index.mjs +793 -1936
- package/package.json +4 -3
- package/readme.md +2 -3
package/dist/webgpu/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as webgpu from 'three/webgpu';
|
|
2
|
-
import { RenderTarget, CubeReflectionMapping, EquirectangularReflectionMapping, CubeTextureLoader, Scene,
|
|
2
|
+
import { CubeRenderTarget, RenderTarget, CubeReflectionMapping, EquirectangularReflectionMapping, CubeTextureLoader, Scene, HalfFloatType, Color, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, ShaderMaterial, Uniform, SRGBColorSpace, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1, CanvasTarget, Raycaster, OrthographicCamera, PerspectiveCamera, PCFShadowMap, VSMShadowMap, BasicShadowMap, ACESFilmicToneMapping, WebGPURenderer, Vector4, RenderPipeline } from 'three/webgpu';
|
|
3
3
|
import { Inspector } from 'three/addons/inspector/Inspector.js';
|
|
4
4
|
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
5
5
|
import * as React from 'react';
|
|
@@ -14,6 +14,8 @@ import { UltraHDRLoader } from 'three/examples/jsm/loaders/UltraHDRLoader.js';
|
|
|
14
14
|
import { GainMapLoader } from '@monogrid/gainmap-js';
|
|
15
15
|
import Tb, { unstable_scheduleCallback, unstable_IdlePriority } from 'scheduler';
|
|
16
16
|
import { createWithEqualityFn } from 'zustand/traditional';
|
|
17
|
+
import { getScheduler } from '@pmndrs/scheduler';
|
|
18
|
+
export { Scheduler, getScheduler } from '@pmndrs/scheduler';
|
|
17
19
|
import { suspend, preload, clear } from 'suspend-react';
|
|
18
20
|
import { dequal } from 'dequal/lite';
|
|
19
21
|
import 'zustand/shallow';
|
|
@@ -41,13 +43,16 @@ const WebGLRenderer = class WebGLRenderer2 {
|
|
|
41
43
|
}
|
|
42
44
|
};
|
|
43
45
|
const WebGLRenderTarget = null;
|
|
46
|
+
const WebGLCubeRenderTarget = null;
|
|
44
47
|
|
|
45
48
|
const THREE = /*#__PURE__*/_mergeNamespaces({
|
|
46
49
|
__proto__: null,
|
|
50
|
+
CubeRenderTargetCompat: CubeRenderTarget,
|
|
47
51
|
Inspector: Inspector,
|
|
48
52
|
R3F_BUILD_LEGACY: R3F_BUILD_LEGACY,
|
|
49
53
|
R3F_BUILD_WEBGPU: R3F_BUILD_WEBGPU,
|
|
50
54
|
RenderTargetCompat: RenderTarget,
|
|
55
|
+
WebGLCubeRenderTarget: WebGLCubeRenderTarget,
|
|
51
56
|
WebGLRenderTarget: WebGLRenderTarget,
|
|
52
57
|
WebGLRenderer: WebGLRenderer
|
|
53
58
|
}, [webgpu]);
|
|
@@ -321,15 +326,16 @@ function EnvironmentPortal({
|
|
|
321
326
|
preset = void 0,
|
|
322
327
|
extensions
|
|
323
328
|
}) {
|
|
324
|
-
const
|
|
329
|
+
const renderer = useThree$1((state) => state.renderer);
|
|
330
|
+
const isLegacy = useThree$1((state) => state.isLegacy);
|
|
325
331
|
const defaultScene = useThree$1((state) => state.scene);
|
|
326
332
|
const camera = React.useRef(null);
|
|
327
333
|
const [virtualScene] = React.useState(() => new Scene());
|
|
328
334
|
const fbo = React.useMemo(() => {
|
|
329
|
-
const fbo2 = new WebGLCubeRenderTarget(resolution);
|
|
335
|
+
const fbo2 = isLegacy ? new WebGLCubeRenderTarget(resolution) : new CubeRenderTarget(resolution);
|
|
330
336
|
fbo2.texture.type = HalfFloatType;
|
|
331
337
|
return fbo2;
|
|
332
|
-
}, [resolution]);
|
|
338
|
+
}, [resolution, isLegacy]);
|
|
333
339
|
React.useEffect(() => {
|
|
334
340
|
return () => {
|
|
335
341
|
fbo.dispose();
|
|
@@ -337,10 +343,10 @@ function EnvironmentPortal({
|
|
|
337
343
|
}, [fbo]);
|
|
338
344
|
React.useLayoutEffect(() => {
|
|
339
345
|
if (frames === 1) {
|
|
340
|
-
const autoClear =
|
|
341
|
-
|
|
342
|
-
camera.current.update(
|
|
343
|
-
|
|
346
|
+
const autoClear = renderer.autoClear;
|
|
347
|
+
renderer.autoClear = true;
|
|
348
|
+
camera.current.update(renderer, virtualScene);
|
|
349
|
+
renderer.autoClear = autoClear;
|
|
344
350
|
}
|
|
345
351
|
return setEnvProps(background, scene, defaultScene, fbo.texture, {
|
|
346
352
|
backgroundBlurriness: blur ?? backgroundBlurriness,
|
|
@@ -357,7 +363,7 @@ function EnvironmentPortal({
|
|
|
357
363
|
defaultScene,
|
|
358
364
|
background,
|
|
359
365
|
frames,
|
|
360
|
-
|
|
366
|
+
renderer,
|
|
361
367
|
blur,
|
|
362
368
|
backgroundBlurriness,
|
|
363
369
|
backgroundIntensity,
|
|
@@ -368,10 +374,10 @@ function EnvironmentPortal({
|
|
|
368
374
|
let count = 1;
|
|
369
375
|
useFrame$1(() => {
|
|
370
376
|
if (frames === Infinity || count < frames) {
|
|
371
|
-
const autoClear =
|
|
372
|
-
|
|
373
|
-
camera.current.update(
|
|
374
|
-
|
|
377
|
+
const autoClear = renderer.autoClear;
|
|
378
|
+
renderer.autoClear = true;
|
|
379
|
+
camera.current.update(renderer, virtualScene);
|
|
380
|
+
renderer.autoClear = autoClear;
|
|
375
381
|
count++;
|
|
376
382
|
}
|
|
377
383
|
});
|
|
@@ -435,9 +441,9 @@ function Environment(props) {
|
|
|
435
441
|
return props.ground ? /* @__PURE__ */ jsx(EnvironmentGround, { ...props }) : props.map ? /* @__PURE__ */ jsx(EnvironmentMap, { ...props }) : props.children ? /* @__PURE__ */ jsx(EnvironmentPortal, { ...props }) : /* @__PURE__ */ jsx(EnvironmentCube, { ...props });
|
|
436
442
|
}
|
|
437
443
|
|
|
438
|
-
var __defProp$
|
|
439
|
-
var __defNormalProp$
|
|
440
|
-
var __publicField$
|
|
444
|
+
var __defProp$1 = Object.defineProperty;
|
|
445
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
446
|
+
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
441
447
|
const act = React["act"];
|
|
442
448
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React.useLayoutEffect : React.useEffect;
|
|
443
449
|
function useMutableCallback(fn) {
|
|
@@ -469,7 +475,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
469
475
|
return _a = class extends React.Component {
|
|
470
476
|
constructor() {
|
|
471
477
|
super(...arguments);
|
|
472
|
-
__publicField$
|
|
478
|
+
__publicField$1(this, "state", { error: false });
|
|
473
479
|
}
|
|
474
480
|
componentDidCatch(err) {
|
|
475
481
|
this.props.set(err);
|
|
@@ -477,7 +483,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
477
483
|
render() {
|
|
478
484
|
return this.state.error ? null : this.props.children;
|
|
479
485
|
}
|
|
480
|
-
}, __publicField$
|
|
486
|
+
}, __publicField$1(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
481
487
|
})();
|
|
482
488
|
|
|
483
489
|
const is = {
|
|
@@ -614,8 +620,7 @@ function prepare(target, root, type, props) {
|
|
|
614
620
|
object,
|
|
615
621
|
eventCount: 0,
|
|
616
622
|
handlers: {},
|
|
617
|
-
isHidden: false
|
|
618
|
-
deferredRefs: []
|
|
623
|
+
isHidden: false
|
|
619
624
|
};
|
|
620
625
|
if (object) object.__r3f = instance;
|
|
621
626
|
}
|
|
@@ -772,6 +777,17 @@ function unregisterVisibility(store, object) {
|
|
|
772
777
|
internal.visibilityRegistry.delete(object.uuid);
|
|
773
778
|
internal.occlusionCache.delete(object);
|
|
774
779
|
}
|
|
780
|
+
function updateVisibilityHandlers(store, object, handlers) {
|
|
781
|
+
const { internal } = store.getState();
|
|
782
|
+
const entry = internal.visibilityRegistry.get(object.uuid);
|
|
783
|
+
if (!entry) return false;
|
|
784
|
+
entry.handlers = handlers;
|
|
785
|
+
if ((handlers.onOccluded || handlers.onVisible) && !internal.occlusionEnabled) {
|
|
786
|
+
object.occlusionTest = true;
|
|
787
|
+
enableOcclusion(store);
|
|
788
|
+
}
|
|
789
|
+
return true;
|
|
790
|
+
}
|
|
775
791
|
function checkVisibility(state) {
|
|
776
792
|
const { internal, camera } = state;
|
|
777
793
|
const registry = internal.visibilityRegistry;
|
|
@@ -959,10 +975,7 @@ function applyProps(object, props) {
|
|
|
959
975
|
continue;
|
|
960
976
|
}
|
|
961
977
|
if (value === void 0) continue;
|
|
962
|
-
if (isFromRef(value))
|
|
963
|
-
instance?.deferredRefs?.push({ prop, ref: value[FROM_REF] });
|
|
964
|
-
continue;
|
|
965
|
-
}
|
|
978
|
+
if (isFromRef(value)) continue;
|
|
966
979
|
if (isOnce(value)) {
|
|
967
980
|
if (instance?.appliedOnce?.has(prop)) continue;
|
|
968
981
|
if (instance) {
|
|
@@ -998,6 +1011,19 @@ function applyProps(object, props) {
|
|
|
998
1011
|
} else if (isVectorLike(target) && is.num(value)) {
|
|
999
1012
|
if ("setScalar" in target && typeof target.setScalar === "function") target.setScalar(value);
|
|
1000
1013
|
else target.set(value);
|
|
1014
|
+
} else if (root instanceof ShaderMaterial && key === "uniforms" && is.obj(value)) {
|
|
1015
|
+
if (!is.obj(root.uniforms)) root.uniforms = {};
|
|
1016
|
+
const uniforms = root.uniforms;
|
|
1017
|
+
const nextUniforms = value;
|
|
1018
|
+
for (const name in nextUniforms) {
|
|
1019
|
+
const uniform = nextUniforms[name];
|
|
1020
|
+
const targetUniform = uniforms[name];
|
|
1021
|
+
if (targetUniform) Object.assign(targetUniform, uniform);
|
|
1022
|
+
else {
|
|
1023
|
+
const nextUniform = uniform instanceof Uniform ? uniform.clone() : new Uniform(uniform.value);
|
|
1024
|
+
uniforms[name] = nextUniform;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1001
1027
|
} else {
|
|
1002
1028
|
root[key] = value;
|
|
1003
1029
|
if (key.endsWith("Node") && root.isMaterial) {
|
|
@@ -1016,6 +1042,9 @@ function applyProps(object, props) {
|
|
|
1016
1042
|
if (instance.eventCount && object2.raycast !== null) {
|
|
1017
1043
|
rootState.internal.interaction.push(object2);
|
|
1018
1044
|
}
|
|
1045
|
+
}
|
|
1046
|
+
if (instance?.parent && rootState?.internal && instance.object?.isObject3D) {
|
|
1047
|
+
const object2 = instance.object;
|
|
1019
1048
|
const root = findInitialRoot(instance);
|
|
1020
1049
|
const visibilityHandlers = {
|
|
1021
1050
|
onFramed: instance.handlers.onFramed,
|
|
@@ -1023,7 +1052,9 @@ function applyProps(object, props) {
|
|
|
1023
1052
|
onVisible: instance.handlers.onVisible
|
|
1024
1053
|
};
|
|
1025
1054
|
if (hasVisibilityHandlers(visibilityHandlers)) {
|
|
1026
|
-
|
|
1055
|
+
if (!updateVisibilityHandlers(root, object2, visibilityHandlers)) {
|
|
1056
|
+
registerVisibility(root, object2, visibilityHandlers);
|
|
1057
|
+
}
|
|
1027
1058
|
} else {
|
|
1028
1059
|
unregisterVisibility(root, object2);
|
|
1029
1060
|
}
|
|
@@ -1551,6 +1582,7 @@ function createPointerEvents(store) {
|
|
|
1551
1582
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
1552
1583
|
}
|
|
1553
1584
|
},
|
|
1585
|
+
// PREVIEW (A8): the XR pointer registration API may change before stable.
|
|
1554
1586
|
registerPointer: (config) => {
|
|
1555
1587
|
const pointerId = nextXRPointerId++;
|
|
1556
1588
|
xrPointers.set(pointerId, config);
|
|
@@ -1637,1230 +1669,206 @@ function notifyAlpha({ message, link }) {
|
|
|
1637
1669
|
}
|
|
1638
1670
|
}
|
|
1639
1671
|
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
const
|
|
1644
|
-
|
|
1645
|
-
constructor() {
|
|
1646
|
-
/** Ordered list of phase nodes */
|
|
1647
|
-
__publicField$2(this, "phases", []);
|
|
1648
|
-
/** Quick lookup by name */
|
|
1649
|
-
__publicField$2(this, "phaseMap", /* @__PURE__ */ new Map());
|
|
1650
|
-
/** Cached ordered names (invalidated on changes) */
|
|
1651
|
-
__publicField$2(this, "orderedNamesCache", null);
|
|
1652
|
-
this.initializeDefaultPhases();
|
|
1653
|
-
}
|
|
1654
|
-
//* Initialization --------------------------------
|
|
1655
|
-
initializeDefaultPhases() {
|
|
1656
|
-
for (const name of DEFAULT_PHASES) {
|
|
1657
|
-
const node = { name, isAutoGenerated: false };
|
|
1658
|
-
this.phases.push(node);
|
|
1659
|
-
this.phaseMap.set(name, node);
|
|
1660
|
-
}
|
|
1661
|
-
this.invalidateCache();
|
|
1662
|
-
}
|
|
1663
|
-
//* Public API --------------------------------
|
|
1664
|
-
/**
|
|
1665
|
-
* Add a named phase to the graph
|
|
1666
|
-
* @param name - Phase name (must be unique)
|
|
1667
|
-
* @param options - Position options (before or after another phase)
|
|
1668
|
-
*/
|
|
1669
|
-
addPhase(name, options = {}) {
|
|
1670
|
-
if (this.phaseMap.has(name)) {
|
|
1671
|
-
console.warn(`[useFrame] Phase "${name}" already exists`);
|
|
1672
|
-
return;
|
|
1673
|
-
}
|
|
1674
|
-
const { before, after } = options;
|
|
1675
|
-
const node = { name, isAutoGenerated: false };
|
|
1676
|
-
let insertIndex = this.phases.length;
|
|
1677
|
-
const targetIndex = this.getPhaseIndex(before ?? after);
|
|
1678
|
-
if (targetIndex !== -1) {
|
|
1679
|
-
insertIndex = before ? targetIndex : targetIndex + 1;
|
|
1680
|
-
} else {
|
|
1681
|
-
const constraintType = before ? "before" : "after";
|
|
1682
|
-
console.warn(`[useFrame] Phase "${before ?? after}" not found for '${constraintType}' constraint`);
|
|
1683
|
-
}
|
|
1684
|
-
this.phases.splice(insertIndex, 0, node);
|
|
1685
|
-
this.phaseMap.set(name, node);
|
|
1686
|
-
this.invalidateCache();
|
|
1687
|
-
}
|
|
1688
|
-
/**
|
|
1689
|
-
* Get ordered list of phase names
|
|
1690
|
-
*/
|
|
1691
|
-
getOrderedPhases() {
|
|
1692
|
-
if (this.orderedNamesCache === null) this.orderedNamesCache = this.phases.map((p) => p.name);
|
|
1693
|
-
return this.orderedNamesCache;
|
|
1694
|
-
}
|
|
1695
|
-
/**
|
|
1696
|
-
* Check if a phase exists
|
|
1697
|
-
*/
|
|
1698
|
-
hasPhase(name) {
|
|
1699
|
-
return this.phaseMap.has(name);
|
|
1700
|
-
}
|
|
1701
|
-
/**
|
|
1702
|
-
* Get the index of a phase (-1 if not found)
|
|
1703
|
-
*/
|
|
1704
|
-
getPhaseIndex(name) {
|
|
1705
|
-
if (!name) return -1;
|
|
1706
|
-
return this.phases.findIndex((p) => p.name === name);
|
|
1707
|
-
}
|
|
1708
|
-
/**
|
|
1709
|
-
* Ensure a phase exists, creating an auto-generated one if needed.
|
|
1710
|
-
* Used for resolving before/after constraints.
|
|
1711
|
-
*
|
|
1712
|
-
* @param name - The phase name to ensure exists
|
|
1713
|
-
* @returns The phase name (may be auto-generated like 'before:render')
|
|
1714
|
-
*/
|
|
1715
|
-
ensurePhase(name) {
|
|
1716
|
-
if (this.phaseMap.has(name)) return name;
|
|
1717
|
-
const node = { name, isAutoGenerated: true };
|
|
1718
|
-
this.phases.push(node);
|
|
1719
|
-
this.phaseMap.set(name, node);
|
|
1720
|
-
this.invalidateCache();
|
|
1721
|
-
return name;
|
|
1722
|
-
}
|
|
1723
|
-
/**
|
|
1724
|
-
* Resolve where a job with before/after constraints should go.
|
|
1725
|
-
* Creates auto-generated phases if needed.
|
|
1726
|
-
*
|
|
1727
|
-
* @param before - Phase(s) to run before
|
|
1728
|
-
* @param after - Phase(s) to run after
|
|
1729
|
-
* @returns The resolved phase name
|
|
1730
|
-
*/
|
|
1731
|
-
resolveConstraintPhase(before, after) {
|
|
1732
|
-
const beforeArr = before ? Array.isArray(before) ? before : [before] : [];
|
|
1733
|
-
const afterArr = after ? Array.isArray(after) ? after : [after] : [];
|
|
1734
|
-
if (beforeArr.length > 0) {
|
|
1735
|
-
return this.ensureAutoPhase(beforeArr[0], "before", 0);
|
|
1736
|
-
}
|
|
1737
|
-
if (afterArr.length > 0) {
|
|
1738
|
-
return this.ensureAutoPhase(afterArr[0], "after", 1);
|
|
1739
|
-
}
|
|
1740
|
-
return "update";
|
|
1741
|
-
}
|
|
1742
|
-
/**
|
|
1743
|
-
* Ensure an auto-generated phase exists relative to a target phase.
|
|
1744
|
-
* Creates the phase if it doesn't exist, inserting it at the correct position.
|
|
1745
|
-
*
|
|
1746
|
-
* @param target - The target phase name to position relative to
|
|
1747
|
-
* @param prefix - Prefix for auto-generated phase name ('before' or 'after')
|
|
1748
|
-
* @param offset - Insertion offset (0 for before, 1 for after)
|
|
1749
|
-
* @returns The auto-generated phase name
|
|
1750
|
-
*/
|
|
1751
|
-
ensureAutoPhase(target, prefix, offset) {
|
|
1752
|
-
const autoName = `${prefix}:${target}`;
|
|
1753
|
-
if (this.phaseMap.has(autoName)) return autoName;
|
|
1754
|
-
const node = { name: autoName, isAutoGenerated: true };
|
|
1755
|
-
const targetIndex = this.getPhaseIndex(target);
|
|
1756
|
-
if (targetIndex !== -1) this.phases.splice(targetIndex + offset, 0, node);
|
|
1757
|
-
else this.phases.push(node);
|
|
1758
|
-
this.phaseMap.set(autoName, node);
|
|
1759
|
-
this.invalidateCache();
|
|
1760
|
-
return autoName;
|
|
1761
|
-
}
|
|
1762
|
-
// Internal --------------------------------
|
|
1763
|
-
invalidateCache() {
|
|
1764
|
-
this.orderedNamesCache = null;
|
|
1765
|
-
}
|
|
1672
|
+
const INTERNAL_LIB_MARKERS = ["zustand", "setState", "Object.assign"];
|
|
1673
|
+
function isInternalRendererAccess(stack, callerDepth = 2) {
|
|
1674
|
+
if (!stack) return false;
|
|
1675
|
+
const caller = stack.split("\n")[callerDepth] ?? "";
|
|
1676
|
+
return INTERNAL_LIB_MARKERS.some((m) => caller.includes(m));
|
|
1766
1677
|
}
|
|
1767
1678
|
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1789
|
-
return a.index - b.index;
|
|
1790
|
-
});
|
|
1791
|
-
sortedBuckets.push(hasCrossJobConstraints(bucket) ? topologicalSort(bucket) : bucket);
|
|
1792
|
-
}
|
|
1793
|
-
for (const [phase, bucket] of buckets) {
|
|
1794
|
-
if (!orderedPhases.includes(phase) && bucket.length > 0) {
|
|
1795
|
-
bucket.sort((a, b) => {
|
|
1796
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1797
|
-
return a.index - b.index;
|
|
1798
|
-
});
|
|
1799
|
-
sortedBuckets.push(bucket);
|
|
1800
|
-
}
|
|
1801
|
-
}
|
|
1802
|
-
return sortedBuckets.flat();
|
|
1803
|
-
}
|
|
1804
|
-
function hasCrossJobConstraints(bucket) {
|
|
1805
|
-
const jobIds = new Set(bucket.map((j) => j.id));
|
|
1806
|
-
for (const job of bucket) {
|
|
1807
|
-
for (const ref of job.before) {
|
|
1808
|
-
if (jobIds.has(ref)) return true;
|
|
1809
|
-
}
|
|
1810
|
-
for (const ref of job.after) {
|
|
1811
|
-
if (jobIds.has(ref)) return true;
|
|
1812
|
-
}
|
|
1813
|
-
}
|
|
1814
|
-
return false;
|
|
1815
|
-
}
|
|
1816
|
-
function topologicalSort(jobs) {
|
|
1817
|
-
const n = jobs.length;
|
|
1818
|
-
if (n <= 1) return jobs;
|
|
1819
|
-
const jobMap = /* @__PURE__ */ new Map();
|
|
1820
|
-
const inDegree = /* @__PURE__ */ new Map();
|
|
1821
|
-
const adjacency = /* @__PURE__ */ new Map();
|
|
1822
|
-
for (const job of jobs) {
|
|
1823
|
-
jobMap.set(job.id, job);
|
|
1824
|
-
inDegree.set(job.id, 0);
|
|
1825
|
-
adjacency.set(job.id, []);
|
|
1826
|
-
}
|
|
1827
|
-
for (const job of jobs) {
|
|
1828
|
-
for (const ref of job.before) {
|
|
1829
|
-
if (jobMap.has(ref)) {
|
|
1830
|
-
adjacency.get(job.id).push(ref);
|
|
1831
|
-
inDegree.set(ref, inDegree.get(ref) + 1);
|
|
1679
|
+
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
1680
|
+
const context = globalThis[R3F_CONTEXT] ?? (globalThis[R3F_CONTEXT] = React.createContext(null));
|
|
1681
|
+
const createStore = (invalidate, advance) => {
|
|
1682
|
+
const rootStore = createWithEqualityFn((set, get) => {
|
|
1683
|
+
const position = new Vector3();
|
|
1684
|
+
const defaultTarget = new Vector3();
|
|
1685
|
+
const tempTarget = new Vector3();
|
|
1686
|
+
function getCurrentViewport(camera = get().camera, target = defaultTarget, size = get().size) {
|
|
1687
|
+
const { width, height, top, left } = size;
|
|
1688
|
+
const aspect = width / height;
|
|
1689
|
+
if (target.isVector3) tempTarget.copy(target);
|
|
1690
|
+
else tempTarget.set(...target);
|
|
1691
|
+
const distance = camera.getWorldPosition(position).distanceTo(tempTarget);
|
|
1692
|
+
if (isOrthographicCamera(camera)) {
|
|
1693
|
+
return { width: width / camera.zoom, height: height / camera.zoom, top, left, factor: 1, distance, aspect };
|
|
1694
|
+
} else {
|
|
1695
|
+
const fov = camera.fov * Math.PI / 180;
|
|
1696
|
+
const h = 2 * Math.tan(fov / 2) * distance;
|
|
1697
|
+
const w = h * (width / height);
|
|
1698
|
+
return { width: w, height: h, top, left, factor: width / w, distance, aspect };
|
|
1832
1699
|
}
|
|
1833
1700
|
}
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1701
|
+
let performanceTimeout = void 0;
|
|
1702
|
+
const setPerformanceCurrent = (current) => set((state2) => ({ performance: { ...state2.performance, current } }));
|
|
1703
|
+
const pointer = new Vector2();
|
|
1704
|
+
const rootState = {
|
|
1705
|
+
set,
|
|
1706
|
+
get,
|
|
1707
|
+
// Mock objects that have to be configured
|
|
1708
|
+
// primaryStore is set after store creation (self-reference for primary, primary's store for secondary)
|
|
1709
|
+
primaryStore: null,
|
|
1710
|
+
gl: null,
|
|
1711
|
+
renderer: null,
|
|
1712
|
+
camera: null,
|
|
1713
|
+
frustum: new Frustum(),
|
|
1714
|
+
autoUpdateFrustum: true,
|
|
1715
|
+
raycaster: null,
|
|
1716
|
+
events: {
|
|
1717
|
+
priority: 1,
|
|
1718
|
+
enabled: true,
|
|
1719
|
+
connected: false,
|
|
1720
|
+
frameTimedRaycasts: true,
|
|
1721
|
+
alwaysFireOnScroll: true,
|
|
1722
|
+
updateOnFrame: false
|
|
1723
|
+
},
|
|
1724
|
+
scene: null,
|
|
1725
|
+
rootScene: null,
|
|
1726
|
+
xr: null,
|
|
1727
|
+
inspector: null,
|
|
1728
|
+
invalidate: (frames = 1, stackFrames = false) => invalidate(get(), frames, stackFrames),
|
|
1729
|
+
advance: (timestamp, runGlobalEffects) => advance(timestamp, runGlobalEffects, get()),
|
|
1730
|
+
textureColorSpace: SRGBColorSpace,
|
|
1731
|
+
isLegacy: false,
|
|
1732
|
+
webGPUSupported: false,
|
|
1733
|
+
isNative: false,
|
|
1734
|
+
controls: null,
|
|
1735
|
+
pointer,
|
|
1736
|
+
mouse: pointer,
|
|
1737
|
+
frameloop: "always",
|
|
1738
|
+
onPointerMissed: void 0,
|
|
1739
|
+
onDragOverMissed: void 0,
|
|
1740
|
+
onDropMissed: void 0,
|
|
1741
|
+
performance: {
|
|
1742
|
+
current: 1,
|
|
1743
|
+
min: 0.5,
|
|
1744
|
+
max: 1,
|
|
1745
|
+
debounce: 200,
|
|
1746
|
+
regress: () => {
|
|
1747
|
+
const state2 = get();
|
|
1748
|
+
if (performanceTimeout) clearTimeout(performanceTimeout);
|
|
1749
|
+
if (state2.performance.current !== state2.performance.min) setPerformanceCurrent(state2.performance.min);
|
|
1750
|
+
performanceTimeout = setTimeout(
|
|
1751
|
+
() => setPerformanceCurrent(get().performance.max),
|
|
1752
|
+
state2.performance.debounce
|
|
1753
|
+
);
|
|
1754
|
+
}
|
|
1755
|
+
},
|
|
1756
|
+
size: { width: 0, height: 0, top: 0, left: 0 },
|
|
1757
|
+
viewport: {
|
|
1758
|
+
initialDpr: 0,
|
|
1759
|
+
dpr: 0,
|
|
1760
|
+
width: 0,
|
|
1761
|
+
height: 0,
|
|
1762
|
+
top: 0,
|
|
1763
|
+
left: 0,
|
|
1764
|
+
aspect: 0,
|
|
1765
|
+
distance: 0,
|
|
1766
|
+
factor: 0,
|
|
1767
|
+
getCurrentViewport
|
|
1768
|
+
},
|
|
1769
|
+
setEvents: (events) => set((state2) => ({ ...state2, events: { ...state2.events, ...events } })),
|
|
1770
|
+
setSize: (width, height, top, left) => {
|
|
1771
|
+
const state2 = get();
|
|
1772
|
+
if (width === void 0) {
|
|
1773
|
+
set({ _sizeImperative: false });
|
|
1774
|
+
if (state2._sizeProps) {
|
|
1775
|
+
const { width: propW, height: propH } = state2._sizeProps;
|
|
1776
|
+
if (propW !== void 0 || propH !== void 0) {
|
|
1777
|
+
const currentSize = state2.size;
|
|
1778
|
+
const newSize = {
|
|
1779
|
+
width: propW ?? currentSize.width,
|
|
1780
|
+
height: propH ?? currentSize.height,
|
|
1781
|
+
top: currentSize.top,
|
|
1782
|
+
left: currentSize.left
|
|
1783
|
+
};
|
|
1784
|
+
set((s) => ({
|
|
1785
|
+
size: newSize,
|
|
1786
|
+
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
1787
|
+
}));
|
|
1788
|
+
getScheduler().invalidate();
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
return;
|
|
1792
|
+
}
|
|
1793
|
+
const w = width;
|
|
1794
|
+
const h = height ?? width;
|
|
1795
|
+
const t = top ?? state2.size.top;
|
|
1796
|
+
const l = left ?? state2.size.left;
|
|
1797
|
+
const size = { width: w, height: h, top: t, left: l };
|
|
1798
|
+
set((s) => ({
|
|
1799
|
+
size,
|
|
1800
|
+
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
1801
|
+
_sizeImperative: true
|
|
1802
|
+
}));
|
|
1803
|
+
getScheduler().invalidate();
|
|
1804
|
+
},
|
|
1805
|
+
setDpr: (dpr) => set((state2) => {
|
|
1806
|
+
const resolved = calculateDpr(dpr);
|
|
1807
|
+
return { viewport: { ...state2.viewport, dpr: resolved, initialDpr: state2.viewport.initialDpr || resolved } };
|
|
1808
|
+
}),
|
|
1809
|
+
setFrameloop: (frameloop = "always") => {
|
|
1810
|
+
set(() => ({ frameloop }));
|
|
1811
|
+
getScheduler().frameloop = frameloop;
|
|
1812
|
+
},
|
|
1813
|
+
setError: (error) => set(() => ({ error })),
|
|
1814
|
+
error: null,
|
|
1815
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
1816
|
+
uniforms: {},
|
|
1817
|
+
nodes: {},
|
|
1818
|
+
buffers: {},
|
|
1819
|
+
gpuStorage: {},
|
|
1820
|
+
textures: /* @__PURE__ */ new Map(),
|
|
1821
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1822
|
+
renderPipeline: null,
|
|
1823
|
+
passes: {},
|
|
1824
|
+
_hmrVersion: 0,
|
|
1825
|
+
_sizeImperative: false,
|
|
1826
|
+
_sizeProps: null,
|
|
1827
|
+
previousRoot: void 0,
|
|
1828
|
+
internal: {
|
|
1829
|
+
// Events
|
|
1830
|
+
interaction: [],
|
|
1831
|
+
subscribers: [],
|
|
1832
|
+
// Per-pointer state (new unified structure)
|
|
1833
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1834
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1835
|
+
lastEvent: React.createRef(),
|
|
1836
|
+
// Deprecated but kept for backwards compatibility
|
|
1837
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1838
|
+
initialClick: [0, 0],
|
|
1839
|
+
initialHits: [],
|
|
1840
|
+
capturedMap: /* @__PURE__ */ new Map(),
|
|
1841
|
+
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
1842
|
+
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
1843
|
+
// Occlusion system (WebGPU only)
|
|
1844
|
+
occlusionEnabled: false,
|
|
1845
|
+
occlusionObserver: null,
|
|
1846
|
+
occlusionCache: /* @__PURE__ */ new Map(),
|
|
1847
|
+
helperGroup: null,
|
|
1848
|
+
// Updates
|
|
1849
|
+
active: false,
|
|
1850
|
+
frames: 0,
|
|
1851
|
+
priority: 0,
|
|
1852
|
+
subscribe: (ref, priority, store) => {
|
|
1853
|
+
const internal = get().internal;
|
|
1854
|
+
internal.priority = internal.priority + (priority > 0 ? 1 : 0);
|
|
1855
|
+
internal.subscribers.push({ ref, priority, store });
|
|
1856
|
+
internal.subscribers = internal.subscribers.sort((a, b) => a.priority - b.priority);
|
|
1857
|
+
return () => {
|
|
1858
|
+
const internal2 = get().internal;
|
|
1859
|
+
if (internal2?.subscribers) {
|
|
1860
|
+
internal2.priority = internal2.priority - (priority > 0 ? 1 : 0);
|
|
1861
|
+
internal2.subscribers = internal2.subscribers.filter((s) => s.ref !== ref);
|
|
1862
|
+
}
|
|
1863
|
+
};
|
|
1864
|
+
},
|
|
1865
|
+
// Renderer Storage (single source of truth)
|
|
1866
|
+
actualRenderer: null,
|
|
1867
|
+
// Scheduler for useFrameNext (initialized in renderer.tsx)
|
|
1868
|
+
scheduler: null
|
|
1838
1869
|
}
|
|
1839
|
-
}
|
|
1840
|
-
|
|
1841
|
-
const queue = [];
|
|
1842
|
-
for (const job of jobs) {
|
|
1843
|
-
if (inDegree.get(job.id) === 0) {
|
|
1844
|
-
queue.push(job);
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1847
|
-
queue.sort((a, b) => {
|
|
1848
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1849
|
-
return a.index - b.index;
|
|
1850
|
-
});
|
|
1851
|
-
const result = [];
|
|
1852
|
-
while (queue.length > 0) {
|
|
1853
|
-
const job = queue.shift();
|
|
1854
|
-
result.push(job);
|
|
1855
|
-
const neighbors = adjacency.get(job.id) || [];
|
|
1856
|
-
for (const neighborId of neighbors) {
|
|
1857
|
-
const newDegree = inDegree.get(neighborId) - 1;
|
|
1858
|
-
inDegree.set(neighborId, newDegree);
|
|
1859
|
-
if (newDegree === 0) {
|
|
1860
|
-
const neighbor = jobMap.get(neighborId);
|
|
1861
|
-
insertSorted(queue, neighbor);
|
|
1862
|
-
}
|
|
1863
|
-
}
|
|
1864
|
-
}
|
|
1865
|
-
if (result.length !== n) {
|
|
1866
|
-
console.warn("[useFrame] Circular dependency detected in job constraints");
|
|
1867
|
-
const resultIds = new Set(result.map((j) => j.id));
|
|
1868
|
-
for (const job of jobs) {
|
|
1869
|
-
if (!resultIds.has(job.id)) result.push(job);
|
|
1870
|
-
}
|
|
1871
|
-
}
|
|
1872
|
-
return result;
|
|
1873
|
-
}
|
|
1874
|
-
function insertSorted(arr, job) {
|
|
1875
|
-
let i = 0;
|
|
1876
|
-
while (i < arr.length) {
|
|
1877
|
-
const cmp = arr[i];
|
|
1878
|
-
if (job.priority > cmp.priority || job.priority === cmp.priority && job.index < cmp.index) {
|
|
1879
|
-
break;
|
|
1880
|
-
}
|
|
1881
|
-
i++;
|
|
1882
|
-
}
|
|
1883
|
-
arr.splice(i, 0, job);
|
|
1884
|
-
}
|
|
1885
|
-
|
|
1886
|
-
function shouldRun(job, now) {
|
|
1887
|
-
if (!job.enabled) return false;
|
|
1888
|
-
if (!job.fps) return true;
|
|
1889
|
-
const minInterval = 1e3 / job.fps;
|
|
1890
|
-
const lastRun = job.lastRun ?? 0;
|
|
1891
|
-
const elapsed = now - lastRun;
|
|
1892
|
-
if (elapsed < minInterval - 1) return false;
|
|
1893
|
-
if (job.drop) {
|
|
1894
|
-
job.lastRun = now;
|
|
1895
|
-
} else {
|
|
1896
|
-
const steps = Math.floor(elapsed / minInterval);
|
|
1897
|
-
job.lastRun = lastRun + steps * minInterval;
|
|
1898
|
-
if (job.lastRun < now - minInterval) {
|
|
1899
|
-
job.lastRun = now;
|
|
1900
|
-
}
|
|
1901
|
-
}
|
|
1902
|
-
return true;
|
|
1903
|
-
}
|
|
1904
|
-
function resetJobTiming(job) {
|
|
1905
|
-
job.lastRun = void 0;
|
|
1906
|
-
}
|
|
1907
|
-
|
|
1908
|
-
var __defProp$1 = Object.defineProperty;
|
|
1909
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1910
|
-
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1911
|
-
const hmrData = (() => {
|
|
1912
|
-
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return void 0;
|
|
1913
|
-
if (typeof import_meta_hot !== "undefined") return import_meta_hot;
|
|
1914
|
-
try {
|
|
1915
|
-
return (0, eval)("import.meta.hot");
|
|
1916
|
-
} catch {
|
|
1917
|
-
return void 0;
|
|
1918
|
-
}
|
|
1919
|
-
})();
|
|
1920
|
-
const _Scheduler = class _Scheduler {
|
|
1921
|
-
//* Constructor ================================
|
|
1922
|
-
constructor() {
|
|
1923
|
-
//* Critical State ================================
|
|
1924
|
-
__publicField$1(this, "roots", /* @__PURE__ */ new Map());
|
|
1925
|
-
__publicField$1(this, "phaseGraph");
|
|
1926
|
-
__publicField$1(this, "loopState", {
|
|
1927
|
-
running: false,
|
|
1928
|
-
rafHandle: null,
|
|
1929
|
-
lastTime: null,
|
|
1930
|
-
// null = uninitialized, 0+ = valid timestamp
|
|
1931
|
-
frameCount: 0,
|
|
1932
|
-
elapsedTime: 0,
|
|
1933
|
-
createdAt: performance.now()
|
|
1934
|
-
});
|
|
1935
|
-
__publicField$1(this, "stoppedTime", 0);
|
|
1936
|
-
//* Private State ================================
|
|
1937
|
-
__publicField$1(this, "nextRootIndex", 0);
|
|
1938
|
-
__publicField$1(this, "globalBeforeJobs", /* @__PURE__ */ new Map());
|
|
1939
|
-
__publicField$1(this, "globalAfterJobs", /* @__PURE__ */ new Map());
|
|
1940
|
-
__publicField$1(this, "nextGlobalIndex", 0);
|
|
1941
|
-
__publicField$1(this, "idleCallbacks", /* @__PURE__ */ new Set());
|
|
1942
|
-
__publicField$1(this, "nextJobIndex", 0);
|
|
1943
|
-
__publicField$1(this, "jobStateListeners", /* @__PURE__ */ new Map());
|
|
1944
|
-
__publicField$1(this, "pendingFrames", 0);
|
|
1945
|
-
__publicField$1(this, "_frameloop", "always");
|
|
1946
|
-
//* Independent Mode & Error Handling State ================================
|
|
1947
|
-
__publicField$1(this, "_independent", false);
|
|
1948
|
-
__publicField$1(this, "errorHandler", null);
|
|
1949
|
-
__publicField$1(this, "rootReadyCallbacks", /* @__PURE__ */ new Set());
|
|
1950
|
-
//* Core Loop Execution Methods ================================
|
|
1951
|
-
/**
|
|
1952
|
-
* Main RAF loop callback.
|
|
1953
|
-
* Executes frame, handles demand mode, and schedules next frame.
|
|
1954
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
1955
|
-
* @returns {void}
|
|
1956
|
-
* @private
|
|
1957
|
-
*/
|
|
1958
|
-
__publicField$1(this, "loop", (timestamp) => {
|
|
1959
|
-
if (!this.loopState.running) return;
|
|
1960
|
-
this.executeFrame(timestamp);
|
|
1961
|
-
if (this._frameloop === "demand") {
|
|
1962
|
-
this.pendingFrames = Math.max(0, this.pendingFrames - 1);
|
|
1963
|
-
if (this.pendingFrames === 0) {
|
|
1964
|
-
this.notifyIdle(timestamp);
|
|
1965
|
-
return this.stop();
|
|
1966
|
-
}
|
|
1967
|
-
}
|
|
1968
|
-
this.loopState.rafHandle = requestAnimationFrame(this.loop);
|
|
1969
|
-
});
|
|
1970
|
-
this.phaseGraph = new PhaseGraph();
|
|
1971
|
-
}
|
|
1972
|
-
static get instance() {
|
|
1973
|
-
return globalThis[_Scheduler.INSTANCE_KEY] ?? null;
|
|
1974
|
-
}
|
|
1975
|
-
static set instance(value) {
|
|
1976
|
-
globalThis[_Scheduler.INSTANCE_KEY] = value;
|
|
1977
|
-
}
|
|
1978
|
-
/**
|
|
1979
|
-
* Get the global scheduler instance (creates if doesn't exist).
|
|
1980
|
-
* Uses HMR data to preserve instance across hot reloads.
|
|
1981
|
-
* @returns {Scheduler} The singleton scheduler instance
|
|
1982
|
-
*/
|
|
1983
|
-
static get() {
|
|
1984
|
-
if (!_Scheduler.instance && hmrData?.data?.scheduler) {
|
|
1985
|
-
_Scheduler.instance = hmrData.data.scheduler;
|
|
1986
|
-
}
|
|
1987
|
-
if (!_Scheduler.instance) {
|
|
1988
|
-
_Scheduler.instance = new _Scheduler();
|
|
1989
|
-
if (hmrData?.data) {
|
|
1990
|
-
hmrData.data.scheduler = _Scheduler.instance;
|
|
1991
|
-
}
|
|
1992
|
-
}
|
|
1993
|
-
return _Scheduler.instance;
|
|
1994
|
-
}
|
|
1995
|
-
/**
|
|
1996
|
-
* Reset the singleton instance. Stops the loop and clears all state.
|
|
1997
|
-
* Primarily used for testing to ensure clean state between tests.
|
|
1998
|
-
* @returns {void}
|
|
1999
|
-
*/
|
|
2000
|
-
static reset() {
|
|
2001
|
-
if (_Scheduler.instance) {
|
|
2002
|
-
_Scheduler.instance.stop();
|
|
2003
|
-
_Scheduler.instance = null;
|
|
2004
|
-
}
|
|
2005
|
-
if (hmrData?.data) {
|
|
2006
|
-
hmrData.data.scheduler = null;
|
|
2007
|
-
}
|
|
2008
|
-
}
|
|
2009
|
-
//* Getters & Setters ================================
|
|
2010
|
-
get phases() {
|
|
2011
|
-
return this.phaseGraph.getOrderedPhases();
|
|
2012
|
-
}
|
|
2013
|
-
get frameloop() {
|
|
2014
|
-
return this._frameloop;
|
|
2015
|
-
}
|
|
2016
|
-
set frameloop(mode) {
|
|
2017
|
-
if (this._frameloop === mode) return;
|
|
2018
|
-
const wasAlways = this._frameloop === "always";
|
|
2019
|
-
this._frameloop = mode;
|
|
2020
|
-
if (mode === "always" && !this.loopState.running && this.roots.size > 0) this.start();
|
|
2021
|
-
else if (mode !== "always" && wasAlways) this.stop();
|
|
2022
|
-
}
|
|
2023
|
-
get isRunning() {
|
|
2024
|
-
return this.loopState.running;
|
|
2025
|
-
}
|
|
2026
|
-
get isReady() {
|
|
2027
|
-
return this.roots.size > 0;
|
|
2028
|
-
}
|
|
2029
|
-
get independent() {
|
|
2030
|
-
return this._independent;
|
|
2031
|
-
}
|
|
2032
|
-
set independent(value) {
|
|
2033
|
-
this._independent = value;
|
|
2034
|
-
if (value) this.ensureDefaultRoot();
|
|
2035
|
-
}
|
|
2036
|
-
//* Root Management Methods ================================
|
|
2037
|
-
/**
|
|
2038
|
-
* Register a root (Canvas) with the scheduler.
|
|
2039
|
-
* The first root to register starts the RAF loop (if frameloop='always').
|
|
2040
|
-
* @param {string} id - Unique identifier for this root
|
|
2041
|
-
* @param {RootOptions} [options] - Optional configuration with getState and onError callbacks
|
|
2042
|
-
* @returns {() => void} Unsubscribe function to remove this root
|
|
2043
|
-
*/
|
|
2044
|
-
registerRoot(id, options = {}) {
|
|
2045
|
-
if (this.roots.has(id)) {
|
|
2046
|
-
console.warn(`[Scheduler] Root "${id}" already registered`);
|
|
2047
|
-
return () => this.unregisterRoot(id);
|
|
2048
|
-
}
|
|
2049
|
-
const entry = {
|
|
2050
|
-
id,
|
|
2051
|
-
getState: options.getState ?? (() => ({})),
|
|
2052
|
-
jobs: /* @__PURE__ */ new Map(),
|
|
2053
|
-
sortedJobs: [],
|
|
2054
|
-
needsRebuild: false
|
|
2055
|
-
};
|
|
2056
|
-
if (options.onError) {
|
|
2057
|
-
this.errorHandler = options.onError;
|
|
2058
|
-
}
|
|
2059
|
-
this.roots.set(id, entry);
|
|
2060
|
-
if (this.roots.size === 1) {
|
|
2061
|
-
this.notifyRootReady();
|
|
2062
|
-
if (this._frameloop === "always") this.start();
|
|
2063
|
-
}
|
|
2064
|
-
return () => this.unregisterRoot(id);
|
|
2065
|
-
}
|
|
2066
|
-
/**
|
|
2067
|
-
* Unregister a root from the scheduler.
|
|
2068
|
-
* Cleans up all job state listeners for this root's jobs.
|
|
2069
|
-
* The last root to unregister stops the RAF loop.
|
|
2070
|
-
* @param {string} id - The root ID to unregister
|
|
2071
|
-
* @returns {void}
|
|
2072
|
-
*/
|
|
2073
|
-
unregisterRoot(id) {
|
|
2074
|
-
const root = this.roots.get(id);
|
|
2075
|
-
if (!root) return;
|
|
2076
|
-
for (const jobId of root.jobs.keys()) {
|
|
2077
|
-
this.jobStateListeners.delete(jobId);
|
|
2078
|
-
}
|
|
2079
|
-
this.roots.delete(id);
|
|
2080
|
-
if (this.roots.size === 0) {
|
|
2081
|
-
this.stop();
|
|
2082
|
-
this.errorHandler = null;
|
|
2083
|
-
}
|
|
2084
|
-
}
|
|
2085
|
-
/**
|
|
2086
|
-
* Subscribe to be notified when a root becomes available.
|
|
2087
|
-
* Fires immediately if a root already exists.
|
|
2088
|
-
* @param {() => void} callback - Function called when first root registers
|
|
2089
|
-
* @returns {() => void} Unsubscribe function
|
|
2090
|
-
*/
|
|
2091
|
-
onRootReady(callback) {
|
|
2092
|
-
if (this.roots.size > 0) {
|
|
2093
|
-
callback();
|
|
2094
|
-
return () => {
|
|
2095
|
-
};
|
|
2096
|
-
}
|
|
2097
|
-
this.rootReadyCallbacks.add(callback);
|
|
2098
|
-
return () => this.rootReadyCallbacks.delete(callback);
|
|
2099
|
-
}
|
|
2100
|
-
/**
|
|
2101
|
-
* Notify all registered root-ready callbacks.
|
|
2102
|
-
* Called when the first root registers.
|
|
2103
|
-
* @returns {void}
|
|
2104
|
-
* @private
|
|
2105
|
-
*/
|
|
2106
|
-
notifyRootReady() {
|
|
2107
|
-
for (const cb of this.rootReadyCallbacks) {
|
|
2108
|
-
try {
|
|
2109
|
-
cb();
|
|
2110
|
-
} catch (error) {
|
|
2111
|
-
console.error("[Scheduler] Error in root-ready callback:", error);
|
|
2112
|
-
}
|
|
2113
|
-
}
|
|
2114
|
-
this.rootReadyCallbacks.clear();
|
|
2115
|
-
}
|
|
2116
|
-
/**
|
|
2117
|
-
* Ensure a default root exists for independent mode.
|
|
2118
|
-
* Creates a minimal root with no state provider.
|
|
2119
|
-
* @returns {void}
|
|
2120
|
-
* @private
|
|
2121
|
-
*/
|
|
2122
|
-
ensureDefaultRoot() {
|
|
2123
|
-
if (!this.roots.has("__default__")) {
|
|
2124
|
-
this.registerRoot("__default__");
|
|
2125
|
-
}
|
|
2126
|
-
}
|
|
2127
|
-
/**
|
|
2128
|
-
* Trigger error handling for job errors.
|
|
2129
|
-
* Uses the bound error handler if available, otherwise logs to console.
|
|
2130
|
-
* @param {Error} error - The error to handle
|
|
2131
|
-
* @returns {void}
|
|
2132
|
-
*/
|
|
2133
|
-
triggerError(error) {
|
|
2134
|
-
if (this.errorHandler) this.errorHandler(error);
|
|
2135
|
-
else console.error("[Scheduler]", error);
|
|
2136
|
-
}
|
|
2137
|
-
//* Phase Management Methods ================================
|
|
2138
|
-
/**
|
|
2139
|
-
* Add a named phase to the scheduler's execution order.
|
|
2140
|
-
* Marks all roots for rebuild to incorporate the new phase.
|
|
2141
|
-
* @param {string} name - The phase name (e.g., 'physics', 'postprocess')
|
|
2142
|
-
* @param {AddPhaseOptions} [options] - Positioning options (before/after other phases)
|
|
2143
|
-
* @returns {void}
|
|
2144
|
-
* @example
|
|
2145
|
-
* scheduler.addPhase('physics', { before: 'update' });
|
|
2146
|
-
* scheduler.addPhase('postprocess', { after: 'render' });
|
|
2147
|
-
*/
|
|
2148
|
-
addPhase(name, options) {
|
|
2149
|
-
this.phaseGraph.addPhase(name, options);
|
|
2150
|
-
for (const root of this.roots.values()) {
|
|
2151
|
-
root.needsRebuild = true;
|
|
2152
|
-
}
|
|
2153
|
-
}
|
|
2154
|
-
/**
|
|
2155
|
-
* Check if a phase exists in the scheduler.
|
|
2156
|
-
* @param {string} name - The phase name to check
|
|
2157
|
-
* @returns {boolean} True if the phase exists
|
|
2158
|
-
*/
|
|
2159
|
-
hasPhase(name) {
|
|
2160
|
-
return this.phaseGraph.hasPhase(name);
|
|
2161
|
-
}
|
|
2162
|
-
//* Global Job Registration Methods (Deprecated APIs) ================================
|
|
2163
|
-
/**
|
|
2164
|
-
* Register a global job that runs once per frame (not per-root).
|
|
2165
|
-
* Used internally by deprecated addEffect/addAfterEffect APIs.
|
|
2166
|
-
* @param {'before' | 'after'} phase - When to run: 'before' all roots or 'after' all roots
|
|
2167
|
-
* @param {string} id - Unique identifier for this global job
|
|
2168
|
-
* @param {(timestamp: number) => void} callback - Function called each frame with RAF timestamp
|
|
2169
|
-
* @returns {() => void} Unsubscribe function to remove this global job
|
|
2170
|
-
* @deprecated Use useFrame with phases instead
|
|
2171
|
-
*/
|
|
2172
|
-
registerGlobal(phase, id, callback) {
|
|
2173
|
-
const job = { id, callback };
|
|
2174
|
-
if (phase === "before") {
|
|
2175
|
-
this.globalBeforeJobs.set(id, job);
|
|
2176
|
-
} else {
|
|
2177
|
-
this.globalAfterJobs.set(id, job);
|
|
2178
|
-
}
|
|
2179
|
-
return () => {
|
|
2180
|
-
if (phase === "before") this.globalBeforeJobs.delete(id);
|
|
2181
|
-
else this.globalAfterJobs.delete(id);
|
|
2182
|
-
};
|
|
2183
|
-
}
|
|
2184
|
-
//* Idle Callback Methods (Deprecated API) ================================
|
|
2185
|
-
/**
|
|
2186
|
-
* Register an idle callback that fires when the loop stops.
|
|
2187
|
-
* Used internally by deprecated addTail API.
|
|
2188
|
-
* @param {(timestamp: number) => void} callback - Function called when loop becomes idle
|
|
2189
|
-
* @returns {() => void} Unsubscribe function to remove this idle callback
|
|
2190
|
-
* @deprecated Use demand mode with invalidate() instead
|
|
2191
|
-
*/
|
|
2192
|
-
onIdle(callback) {
|
|
2193
|
-
this.idleCallbacks.add(callback);
|
|
2194
|
-
return () => this.idleCallbacks.delete(callback);
|
|
2195
|
-
}
|
|
2196
|
-
/**
|
|
2197
|
-
* Notify all registered idle callbacks.
|
|
2198
|
-
* Called when the loop stops in demand mode.
|
|
2199
|
-
* @param {number} timestamp - The RAF timestamp when idle occurred
|
|
2200
|
-
* @returns {void}
|
|
2201
|
-
* @private
|
|
2202
|
-
*/
|
|
2203
|
-
notifyIdle(timestamp) {
|
|
2204
|
-
for (const cb of this.idleCallbacks) {
|
|
2205
|
-
try {
|
|
2206
|
-
cb(timestamp);
|
|
2207
|
-
} catch (error) {
|
|
2208
|
-
console.error("[Scheduler] Error in idle callback:", error);
|
|
2209
|
-
}
|
|
2210
|
-
}
|
|
2211
|
-
}
|
|
2212
|
-
//* Job Registration & Management Methods ================================
|
|
2213
|
-
/**
|
|
2214
|
-
* Register a job (frame callback) with a specific root.
|
|
2215
|
-
* This is the core registration method used by useFrame internally.
|
|
2216
|
-
* @param {FrameNextCallback} callback - The function to call each frame
|
|
2217
|
-
* @param {JobOptions & { rootId?: string; system?: boolean }} [options] - Job configuration
|
|
2218
|
-
* @param {string} [options.rootId] - Target root ID (defaults to first registered root)
|
|
2219
|
-
* @param {string} [options.id] - Unique job ID (auto-generated if not provided)
|
|
2220
|
-
* @param {string} [options.phase] - Execution phase (defaults to 'update')
|
|
2221
|
-
* @param {number} [options.priority] - Priority within phase (higher = earlier, default 0)
|
|
2222
|
-
* @param {number} [options.fps] - FPS throttle limit
|
|
2223
|
-
* @param {boolean} [options.drop] - Drop frames when behind (default true)
|
|
2224
|
-
* @param {boolean} [options.enabled] - Whether job is active (default true)
|
|
2225
|
-
* @param {boolean} [options.system] - Internal flag for system jobs (not user-facing)
|
|
2226
|
-
* @returns {() => void} Unsubscribe function to remove this job
|
|
2227
|
-
*/
|
|
2228
|
-
register(callback, options = {}) {
|
|
2229
|
-
const rootId = options.rootId;
|
|
2230
|
-
const root = rootId ? this.roots.get(rootId) : this.roots.values().next().value;
|
|
2231
|
-
if (!root) {
|
|
2232
|
-
console.warn("[Scheduler] No root registered. Is this inside a Canvas?");
|
|
2233
|
-
return () => {
|
|
2234
|
-
};
|
|
2235
|
-
}
|
|
2236
|
-
const id = options.id ?? this.generateJobId();
|
|
2237
|
-
let phase = options.phase ?? "update";
|
|
2238
|
-
if (!options.phase && (options.before || options.after)) {
|
|
2239
|
-
phase = this.phaseGraph.resolveConstraintPhase(options.before, options.after);
|
|
2240
|
-
}
|
|
2241
|
-
const before = this.normalizeConstraints(options.before);
|
|
2242
|
-
const after = this.normalizeConstraints(options.after);
|
|
2243
|
-
const job = {
|
|
2244
|
-
id,
|
|
2245
|
-
callback,
|
|
2246
|
-
phase,
|
|
2247
|
-
before,
|
|
2248
|
-
after,
|
|
2249
|
-
priority: options.priority ?? 0,
|
|
2250
|
-
index: this.nextJobIndex++,
|
|
2251
|
-
fps: options.fps,
|
|
2252
|
-
drop: options.drop ?? true,
|
|
2253
|
-
enabled: options.enabled ?? true,
|
|
2254
|
-
system: options.system ?? false
|
|
2255
|
-
};
|
|
2256
|
-
if (root.jobs.has(id)) {
|
|
2257
|
-
console.warn(`[useFrame] Job with id "${id}" already exists, replacing`);
|
|
2258
|
-
}
|
|
2259
|
-
root.jobs.set(id, job);
|
|
2260
|
-
root.needsRebuild = true;
|
|
2261
|
-
return () => this.unregister(id, root.id);
|
|
2262
|
-
}
|
|
2263
|
-
/**
|
|
2264
|
-
* Unregister a job by its ID.
|
|
2265
|
-
* Searches all roots if rootId is not provided.
|
|
2266
|
-
* @param {string} id - The job ID to unregister
|
|
2267
|
-
* @param {string} [rootId] - Optional root ID to search (searches all if not provided)
|
|
2268
|
-
* @returns {void}
|
|
2269
|
-
*/
|
|
2270
|
-
unregister(id, rootId) {
|
|
2271
|
-
const root = rootId ? this.roots.get(rootId) : Array.from(this.roots.values()).find((r) => r.jobs.has(id));
|
|
2272
|
-
if (root?.jobs.delete(id)) {
|
|
2273
|
-
root.needsRebuild = true;
|
|
2274
|
-
this.jobStateListeners.delete(id);
|
|
2275
|
-
}
|
|
2276
|
-
}
|
|
2277
|
-
/**
|
|
2278
|
-
* Update a job's options dynamically.
|
|
2279
|
-
* Searches all roots to find the job by ID.
|
|
2280
|
-
* Phase/constraint changes trigger a rebuild of the sorted job list.
|
|
2281
|
-
* @param {string} id - The job ID to update
|
|
2282
|
-
* @param {Partial<JobOptions>} options - The options to update
|
|
2283
|
-
* @returns {void}
|
|
2284
|
-
*/
|
|
2285
|
-
updateJob(id, options) {
|
|
2286
|
-
let job;
|
|
2287
|
-
let root;
|
|
2288
|
-
for (const r of this.roots.values()) {
|
|
2289
|
-
job = r.jobs.get(id);
|
|
2290
|
-
if (job) {
|
|
2291
|
-
root = r;
|
|
2292
|
-
break;
|
|
2293
|
-
}
|
|
2294
|
-
}
|
|
2295
|
-
if (!job || !root) return;
|
|
2296
|
-
if (options.priority !== void 0) job.priority = options.priority;
|
|
2297
|
-
if (options.fps !== void 0) job.fps = options.fps;
|
|
2298
|
-
if (options.drop !== void 0) job.drop = options.drop;
|
|
2299
|
-
if (options.enabled !== void 0) {
|
|
2300
|
-
const wasEnabled = job.enabled;
|
|
2301
|
-
job.enabled = options.enabled;
|
|
2302
|
-
if (!wasEnabled && job.enabled) resetJobTiming(job);
|
|
2303
|
-
if (wasEnabled !== job.enabled) root.needsRebuild = true;
|
|
2304
|
-
}
|
|
2305
|
-
if (options.phase !== void 0 || options.before !== void 0 || options.after !== void 0) {
|
|
2306
|
-
if (options.phase) job.phase = options.phase;
|
|
2307
|
-
if (options.before !== void 0) job.before = this.normalizeConstraints(options.before);
|
|
2308
|
-
if (options.after !== void 0) job.after = this.normalizeConstraints(options.after);
|
|
2309
|
-
root.needsRebuild = true;
|
|
2310
|
-
}
|
|
2311
|
-
}
|
|
2312
|
-
//* Job State Management Methods ================================
|
|
2313
|
-
/**
|
|
2314
|
-
* Check if a job is currently paused (disabled).
|
|
2315
|
-
* @param {string} id - The job ID to check
|
|
2316
|
-
* @returns {boolean} True if the job exists and is paused
|
|
2317
|
-
*/
|
|
2318
|
-
isJobPaused(id) {
|
|
2319
|
-
for (const root of this.roots.values()) {
|
|
2320
|
-
const job = root.jobs.get(id);
|
|
2321
|
-
if (job) return !job.enabled;
|
|
2322
|
-
}
|
|
2323
|
-
return false;
|
|
2324
|
-
}
|
|
2325
|
-
/**
|
|
2326
|
-
* Subscribe to state changes for a specific job.
|
|
2327
|
-
* Listener is called when job is paused or resumed.
|
|
2328
|
-
* @param {string} id - The job ID to subscribe to
|
|
2329
|
-
* @param {() => void} listener - Callback invoked on state changes
|
|
2330
|
-
* @returns {() => void} Unsubscribe function
|
|
2331
|
-
*/
|
|
2332
|
-
subscribeJobState(id, listener) {
|
|
2333
|
-
if (!this.jobStateListeners.has(id)) {
|
|
2334
|
-
this.jobStateListeners.set(id, /* @__PURE__ */ new Set());
|
|
2335
|
-
}
|
|
2336
|
-
this.jobStateListeners.get(id).add(listener);
|
|
2337
|
-
return () => {
|
|
2338
|
-
this.jobStateListeners.get(id)?.delete(listener);
|
|
2339
|
-
if (this.jobStateListeners.get(id)?.size === 0) {
|
|
2340
|
-
this.jobStateListeners.delete(id);
|
|
2341
|
-
}
|
|
2342
|
-
};
|
|
2343
|
-
}
|
|
2344
|
-
/**
|
|
2345
|
-
* Notify all listeners that a job's state has changed.
|
|
2346
|
-
* @param {string} id - The job ID that changed
|
|
2347
|
-
* @returns {void}
|
|
2348
|
-
* @private
|
|
2349
|
-
*/
|
|
2350
|
-
notifyJobStateChange(id) {
|
|
2351
|
-
this.jobStateListeners.get(id)?.forEach((listener) => listener());
|
|
2352
|
-
}
|
|
2353
|
-
/**
|
|
2354
|
-
* Pause a job by ID (sets enabled=false).
|
|
2355
|
-
* Notifies any subscribed state listeners.
|
|
2356
|
-
* @param {string} id - The job ID to pause
|
|
2357
|
-
* @returns {void}
|
|
2358
|
-
*/
|
|
2359
|
-
pauseJob(id) {
|
|
2360
|
-
this.updateJob(id, { enabled: false });
|
|
2361
|
-
this.notifyJobStateChange(id);
|
|
2362
|
-
}
|
|
2363
|
-
/**
|
|
2364
|
-
* Resume a paused job by ID (sets enabled=true).
|
|
2365
|
-
* Resets job timing to prevent frame accumulation.
|
|
2366
|
-
* Notifies any subscribed state listeners.
|
|
2367
|
-
* @param {string} id - The job ID to resume
|
|
2368
|
-
* @returns {void}
|
|
2369
|
-
*/
|
|
2370
|
-
resumeJob(id) {
|
|
2371
|
-
this.updateJob(id, { enabled: true });
|
|
2372
|
-
this.notifyJobStateChange(id);
|
|
2373
|
-
}
|
|
2374
|
-
//* Frame Loop Control Methods ================================
|
|
2375
|
-
/**
|
|
2376
|
-
* Start the requestAnimationFrame loop.
|
|
2377
|
-
* Resets timing state (elapsedTime, frameCount) on start.
|
|
2378
|
-
* No-op if already running.
|
|
2379
|
-
* @returns {void}
|
|
2380
|
-
*/
|
|
2381
|
-
start() {
|
|
2382
|
-
if (this.loopState.running) return;
|
|
2383
|
-
const { elapsedTime, createdAt } = this.loopState;
|
|
2384
|
-
let adjustedCreated = 0;
|
|
2385
|
-
if (this.stoppedTime > 0) {
|
|
2386
|
-
adjustedCreated = createdAt - (performance.now() - this.stoppedTime);
|
|
2387
|
-
this.stoppedTime = 0;
|
|
2388
|
-
}
|
|
2389
|
-
Object.assign(this.loopState, {
|
|
2390
|
-
running: true,
|
|
2391
|
-
elapsedTime: elapsedTime ?? 0,
|
|
2392
|
-
lastTime: performance.now(),
|
|
2393
|
-
createdAt: adjustedCreated > 0 ? adjustedCreated : performance.now(),
|
|
2394
|
-
frameCount: 0,
|
|
2395
|
-
rafHandle: requestAnimationFrame(this.loop)
|
|
2396
|
-
});
|
|
2397
|
-
}
|
|
2398
|
-
/**
|
|
2399
|
-
* Stop the requestAnimationFrame loop.
|
|
2400
|
-
* Cancels any pending RAF callback.
|
|
2401
|
-
* No-op if not running.
|
|
2402
|
-
* @returns {void}
|
|
2403
|
-
*/
|
|
2404
|
-
stop() {
|
|
2405
|
-
if (!this.loopState.running) return;
|
|
2406
|
-
this.loopState.running = false;
|
|
2407
|
-
if (this.loopState.rafHandle !== null) {
|
|
2408
|
-
cancelAnimationFrame(this.loopState.rafHandle);
|
|
2409
|
-
this.loopState.rafHandle = null;
|
|
2410
|
-
}
|
|
2411
|
-
this.stoppedTime = performance.now();
|
|
2412
|
-
}
|
|
2413
|
-
/**
|
|
2414
|
-
* Request frames to be rendered in demand mode.
|
|
2415
|
-
* Accumulates pending frames (capped at 60) and starts the loop if not running.
|
|
2416
|
-
* No-op if frameloop is not 'demand'.
|
|
2417
|
-
* @param {number} [frames=1] - Number of frames to request
|
|
2418
|
-
* @param {boolean} [stackFrames=false] - Whether to add frames to existing pending count
|
|
2419
|
-
* - `false` (default): Sets pending frames to the specified value (replaces existing count)
|
|
2420
|
-
* - `true`: Adds frames to existing pending count (useful for accumulating invalidations)
|
|
2421
|
-
* @returns {void}
|
|
2422
|
-
* @example
|
|
2423
|
-
* // Request a single frame render
|
|
2424
|
-
* scheduler.invalidate();
|
|
2425
|
-
*
|
|
2426
|
-
* @example
|
|
2427
|
-
* // Request 5 frames (e.g., for animations)
|
|
2428
|
-
* scheduler.invalidate(5);
|
|
2429
|
-
*
|
|
2430
|
-
* @example
|
|
2431
|
-
* // Set pending frames to exactly 3 (don't stack with existing)
|
|
2432
|
-
* scheduler.invalidate(3, false);
|
|
2433
|
-
*
|
|
2434
|
-
* @example
|
|
2435
|
-
* // Add 2 more frames to existing pending count
|
|
2436
|
-
* scheduler.invalidate(2, true);
|
|
2437
|
-
*/
|
|
2438
|
-
invalidate(frames = 1, stackFrames = false) {
|
|
2439
|
-
if (this._frameloop !== "demand") return;
|
|
2440
|
-
const baseFrames = stackFrames ? this.pendingFrames : 0;
|
|
2441
|
-
this.pendingFrames = Math.min(60, baseFrames + frames);
|
|
2442
|
-
if (!this.loopState.running && this.pendingFrames > 0) this.start();
|
|
2443
|
-
}
|
|
2444
|
-
/**
|
|
2445
|
-
* Reset timing state for deterministic testing.
|
|
2446
|
-
* Preserves jobs and roots but resets lastTime, frameCount, elapsedTime, etc.
|
|
2447
|
-
* @returns {void}
|
|
2448
|
-
*/
|
|
2449
|
-
resetTiming() {
|
|
2450
|
-
this.loopState.lastTime = null;
|
|
2451
|
-
this.loopState.frameCount = 0;
|
|
2452
|
-
this.loopState.elapsedTime = 0;
|
|
2453
|
-
this.loopState.createdAt = performance.now();
|
|
2454
|
-
}
|
|
2455
|
-
//* Manual Stepping Methods ================================
|
|
2456
|
-
/**
|
|
2457
|
-
* Manually execute a single frame for all roots.
|
|
2458
|
-
* Useful for frameloop='never' mode or testing scenarios.
|
|
2459
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2460
|
-
* @returns {void}
|
|
2461
|
-
* @example
|
|
2462
|
-
* // Manual control mode
|
|
2463
|
-
* scheduler.frameloop = 'never';
|
|
2464
|
-
* scheduler.step(); // Execute one frame
|
|
2465
|
-
*/
|
|
2466
|
-
step(timestamp) {
|
|
2467
|
-
const now = timestamp ?? performance.now();
|
|
2468
|
-
this.executeFrame(now);
|
|
2469
|
-
}
|
|
2470
|
-
/**
|
|
2471
|
-
* Manually execute a single job by its ID.
|
|
2472
|
-
* Useful for testing individual job callbacks in isolation.
|
|
2473
|
-
* @param {string} id - The job ID to step
|
|
2474
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2475
|
-
* @returns {void}
|
|
2476
|
-
*/
|
|
2477
|
-
stepJob(id, timestamp) {
|
|
2478
|
-
let job;
|
|
2479
|
-
let root;
|
|
2480
|
-
for (const r of this.roots.values()) {
|
|
2481
|
-
job = r.jobs.get(id);
|
|
2482
|
-
if (job) {
|
|
2483
|
-
root = r;
|
|
2484
|
-
break;
|
|
2485
|
-
}
|
|
2486
|
-
}
|
|
2487
|
-
if (!job || !root) {
|
|
2488
|
-
console.warn(`[Scheduler] Job "${id}" not found`);
|
|
2489
|
-
return;
|
|
2490
|
-
}
|
|
2491
|
-
const now = timestamp ?? performance.now();
|
|
2492
|
-
const deltaMs = this.loopState.lastTime !== null ? now - this.loopState.lastTime : 0;
|
|
2493
|
-
const delta = deltaMs / 1e3;
|
|
2494
|
-
const elapsed = now - this.loopState.createdAt;
|
|
2495
|
-
const providedState = root.getState?.() ?? {};
|
|
2496
|
-
const frameState = {
|
|
2497
|
-
...providedState,
|
|
2498
|
-
time: now,
|
|
2499
|
-
delta,
|
|
2500
|
-
elapsed,
|
|
2501
|
-
frame: this.loopState.frameCount
|
|
2502
|
-
};
|
|
2503
|
-
try {
|
|
2504
|
-
job.callback(frameState, delta);
|
|
2505
|
-
} catch (error) {
|
|
2506
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2507
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2508
|
-
}
|
|
2509
|
-
}
|
|
2510
|
-
/**
|
|
2511
|
-
* Execute a single frame across all roots.
|
|
2512
|
-
* Order: globalBefore → each root's jobs → globalAfter
|
|
2513
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2514
|
-
* @returns {void}
|
|
2515
|
-
* @private
|
|
2516
|
-
*/
|
|
2517
|
-
executeFrame(timestamp) {
|
|
2518
|
-
const deltaMs = this.loopState.lastTime !== null ? timestamp - this.loopState.lastTime : 0;
|
|
2519
|
-
const delta = deltaMs / 1e3;
|
|
2520
|
-
this.loopState.lastTime = timestamp;
|
|
2521
|
-
this.loopState.frameCount++;
|
|
2522
|
-
this.loopState.elapsedTime += deltaMs;
|
|
2523
|
-
this.runGlobalJobs(this.globalBeforeJobs, timestamp);
|
|
2524
|
-
for (const root of this.roots.values()) {
|
|
2525
|
-
this.tickRoot(root, timestamp, delta);
|
|
2526
|
-
}
|
|
2527
|
-
this.runGlobalJobs(this.globalAfterJobs, timestamp);
|
|
2528
|
-
}
|
|
2529
|
-
/**
|
|
2530
|
-
* Run all global jobs from a job map.
|
|
2531
|
-
* Catches and logs errors without stopping execution.
|
|
2532
|
-
* @param {Map<string, GlobalJob>} jobs - The global jobs map to execute
|
|
2533
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2534
|
-
* @returns {void}
|
|
2535
|
-
* @private
|
|
2536
|
-
*/
|
|
2537
|
-
runGlobalJobs(jobs, timestamp) {
|
|
2538
|
-
for (const job of jobs.values()) {
|
|
2539
|
-
try {
|
|
2540
|
-
job.callback(timestamp);
|
|
2541
|
-
} catch (error) {
|
|
2542
|
-
console.error(`[Scheduler] Error in global job "${job.id}":`, error);
|
|
2543
|
-
}
|
|
2544
|
-
}
|
|
2545
|
-
}
|
|
2546
|
-
/**
|
|
2547
|
-
* Execute all jobs for a single root in sorted order.
|
|
2548
|
-
* Rebuilds sorted job list if needed, then dispatches each job.
|
|
2549
|
-
* Errors are caught and propagated via triggerError.
|
|
2550
|
-
* @param {RootEntry} root - The root entry to tick
|
|
2551
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2552
|
-
* @param {number} delta - Time since last frame in seconds
|
|
2553
|
-
* @returns {void}
|
|
2554
|
-
* @private
|
|
2555
|
-
*/
|
|
2556
|
-
tickRoot(root, timestamp, delta) {
|
|
2557
|
-
if (root.needsRebuild) {
|
|
2558
|
-
root.sortedJobs = rebuildSortedJobs(root.jobs, this.phaseGraph);
|
|
2559
|
-
root.needsRebuild = false;
|
|
2560
|
-
}
|
|
2561
|
-
const providedState = root.getState?.() ?? {};
|
|
2562
|
-
const frameState = {
|
|
2563
|
-
...providedState,
|
|
2564
|
-
time: timestamp,
|
|
2565
|
-
delta,
|
|
2566
|
-
elapsed: this.loopState.elapsedTime / 1e3,
|
|
2567
|
-
// Convert ms to seconds
|
|
2568
|
-
frame: this.loopState.frameCount
|
|
2569
|
-
};
|
|
2570
|
-
for (const job of root.sortedJobs) {
|
|
2571
|
-
if (!shouldRun(job, timestamp)) continue;
|
|
2572
|
-
try {
|
|
2573
|
-
job.callback(frameState, delta);
|
|
2574
|
-
} catch (error) {
|
|
2575
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2576
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2577
|
-
}
|
|
2578
|
-
}
|
|
2579
|
-
}
|
|
2580
|
-
//* Debug & Inspection Methods ================================
|
|
2581
|
-
/**
|
|
2582
|
-
* Get the total number of registered jobs across all roots.
|
|
2583
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2584
|
-
* @returns {number} Total job count
|
|
2585
|
-
*/
|
|
2586
|
-
getJobCount() {
|
|
2587
|
-
let count = 0;
|
|
2588
|
-
for (const root of this.roots.values()) {
|
|
2589
|
-
count += root.jobs.size;
|
|
2590
|
-
}
|
|
2591
|
-
return count + this.globalBeforeJobs.size + this.globalAfterJobs.size;
|
|
2592
|
-
}
|
|
2593
|
-
/**
|
|
2594
|
-
* Get all registered job IDs across all roots.
|
|
2595
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2596
|
-
* @returns {string[]} Array of all job IDs
|
|
2597
|
-
*/
|
|
2598
|
-
getJobIds() {
|
|
2599
|
-
const ids = [];
|
|
2600
|
-
for (const root of this.roots.values()) {
|
|
2601
|
-
ids.push(...root.jobs.keys());
|
|
2602
|
-
}
|
|
2603
|
-
ids.push(...this.globalBeforeJobs.keys());
|
|
2604
|
-
ids.push(...this.globalAfterJobs.keys());
|
|
2605
|
-
return ids;
|
|
2606
|
-
}
|
|
2607
|
-
/**
|
|
2608
|
-
* Get the number of registered roots (Canvas instances).
|
|
2609
|
-
* @returns {number} Number of registered roots
|
|
2610
|
-
*/
|
|
2611
|
-
getRootCount() {
|
|
2612
|
-
return this.roots.size;
|
|
2613
|
-
}
|
|
2614
|
-
/**
|
|
2615
|
-
* Check if any user (non-system) jobs are registered in a specific phase.
|
|
2616
|
-
* Used by the default render job to know if a user has taken over rendering.
|
|
2617
|
-
*
|
|
2618
|
-
* @param phase The phase to check
|
|
2619
|
-
* @param rootId Optional root ID to check (checks all roots if not provided)
|
|
2620
|
-
* @returns true if any user jobs exist in the phase
|
|
2621
|
-
*/
|
|
2622
|
-
hasUserJobsInPhase(phase, rootId) {
|
|
2623
|
-
const rootsToCheck = rootId ? [this.roots.get(rootId)].filter(Boolean) : Array.from(this.roots.values());
|
|
2624
|
-
return rootsToCheck.some((root) => {
|
|
2625
|
-
if (!root) return false;
|
|
2626
|
-
for (const job of root.jobs.values()) {
|
|
2627
|
-
if (job.phase === phase && !job.system && job.enabled) return true;
|
|
2628
|
-
}
|
|
2629
|
-
return false;
|
|
2630
|
-
});
|
|
2631
|
-
}
|
|
2632
|
-
//* Utility Methods ================================
|
|
2633
|
-
/**
|
|
2634
|
-
* Generate a unique root ID for automatic root registration.
|
|
2635
|
-
* @returns {string} A unique root ID in the format 'root_N'
|
|
2636
|
-
*/
|
|
2637
|
-
generateRootId() {
|
|
2638
|
-
return `root_${this.nextRootIndex++}`;
|
|
2639
|
-
}
|
|
2640
|
-
/**
|
|
2641
|
-
* Generate a unique job ID.
|
|
2642
|
-
* @returns {string} A unique job ID in the format 'job_N'
|
|
2643
|
-
* @private
|
|
2644
|
-
*/
|
|
2645
|
-
generateJobId() {
|
|
2646
|
-
return `job_${this.nextJobIndex}`;
|
|
2647
|
-
}
|
|
2648
|
-
/**
|
|
2649
|
-
* Normalize before/after constraints to a Set.
|
|
2650
|
-
* Handles undefined, single string, or array inputs.
|
|
2651
|
-
* @param {string | string[] | undefined} value - The constraint value(s)
|
|
2652
|
-
* @returns {Set<string>} Normalized Set of constraint strings
|
|
2653
|
-
* @private
|
|
2654
|
-
*/
|
|
2655
|
-
normalizeConstraints(value) {
|
|
2656
|
-
if (!value) return /* @__PURE__ */ new Set();
|
|
2657
|
-
if (Array.isArray(value)) return new Set(value);
|
|
2658
|
-
return /* @__PURE__ */ new Set([value]);
|
|
2659
|
-
}
|
|
2660
|
-
};
|
|
2661
|
-
//* Static State & Methods (Singleton Usage) ================================
|
|
2662
|
-
//* Cross-Bundle Singleton Key ==============================
|
|
2663
|
-
// Use Symbol.for() to ensure scheduler is shared across bundle boundaries
|
|
2664
|
-
// This prevents issues when mixing imports from @react-three/fiber and @react-three/fiber/webgpu
|
|
2665
|
-
__publicField$1(_Scheduler, "INSTANCE_KEY", Symbol.for("@react-three/fiber.scheduler"));
|
|
2666
|
-
let Scheduler = _Scheduler;
|
|
2667
|
-
const getScheduler = () => Scheduler.get();
|
|
2668
|
-
if (hmrData) {
|
|
2669
|
-
hmrData.accept?.();
|
|
2670
|
-
}
|
|
2671
|
-
|
|
2672
|
-
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
2673
|
-
const context = globalThis[R3F_CONTEXT] ?? (globalThis[R3F_CONTEXT] = React.createContext(null));
|
|
2674
|
-
const createStore = (invalidate, advance) => {
|
|
2675
|
-
const rootStore = createWithEqualityFn((set, get) => {
|
|
2676
|
-
const position = new Vector3();
|
|
2677
|
-
const defaultTarget = new Vector3();
|
|
2678
|
-
const tempTarget = new Vector3();
|
|
2679
|
-
function getCurrentViewport(camera = get().camera, target = defaultTarget, size = get().size) {
|
|
2680
|
-
const { width, height, top, left } = size;
|
|
2681
|
-
const aspect = width / height;
|
|
2682
|
-
if (target.isVector3) tempTarget.copy(target);
|
|
2683
|
-
else tempTarget.set(...target);
|
|
2684
|
-
const distance = camera.getWorldPosition(position).distanceTo(tempTarget);
|
|
2685
|
-
if (isOrthographicCamera(camera)) {
|
|
2686
|
-
return { width: width / camera.zoom, height: height / camera.zoom, top, left, factor: 1, distance, aspect };
|
|
2687
|
-
} else {
|
|
2688
|
-
const fov = camera.fov * Math.PI / 180;
|
|
2689
|
-
const h = 2 * Math.tan(fov / 2) * distance;
|
|
2690
|
-
const w = h * (width / height);
|
|
2691
|
-
return { width: w, height: h, top, left, factor: width / w, distance, aspect };
|
|
2692
|
-
}
|
|
2693
|
-
}
|
|
2694
|
-
let performanceTimeout = void 0;
|
|
2695
|
-
const setPerformanceCurrent = (current) => set((state2) => ({ performance: { ...state2.performance, current } }));
|
|
2696
|
-
const pointer = new Vector2();
|
|
2697
|
-
const rootState = {
|
|
2698
|
-
set,
|
|
2699
|
-
get,
|
|
2700
|
-
// Mock objects that have to be configured
|
|
2701
|
-
// primaryStore is set after store creation (self-reference for primary, primary's store for secondary)
|
|
2702
|
-
primaryStore: null,
|
|
2703
|
-
gl: null,
|
|
2704
|
-
renderer: null,
|
|
2705
|
-
camera: null,
|
|
2706
|
-
frustum: new Frustum(),
|
|
2707
|
-
autoUpdateFrustum: true,
|
|
2708
|
-
raycaster: null,
|
|
2709
|
-
events: {
|
|
2710
|
-
priority: 1,
|
|
2711
|
-
enabled: true,
|
|
2712
|
-
connected: false,
|
|
2713
|
-
frameTimedRaycasts: true,
|
|
2714
|
-
alwaysFireOnScroll: true,
|
|
2715
|
-
updateOnFrame: false
|
|
2716
|
-
},
|
|
2717
|
-
scene: null,
|
|
2718
|
-
rootScene: null,
|
|
2719
|
-
xr: null,
|
|
2720
|
-
inspector: null,
|
|
2721
|
-
invalidate: (frames = 1, stackFrames = false) => invalidate(get(), frames, stackFrames),
|
|
2722
|
-
advance: (timestamp, runGlobalEffects) => advance(timestamp, runGlobalEffects, get()),
|
|
2723
|
-
textureColorSpace: SRGBColorSpace,
|
|
2724
|
-
isLegacy: false,
|
|
2725
|
-
webGPUSupported: false,
|
|
2726
|
-
isNative: false,
|
|
2727
|
-
controls: null,
|
|
2728
|
-
pointer,
|
|
2729
|
-
mouse: pointer,
|
|
2730
|
-
frameloop: "always",
|
|
2731
|
-
onPointerMissed: void 0,
|
|
2732
|
-
onDragOverMissed: void 0,
|
|
2733
|
-
onDropMissed: void 0,
|
|
2734
|
-
performance: {
|
|
2735
|
-
current: 1,
|
|
2736
|
-
min: 0.5,
|
|
2737
|
-
max: 1,
|
|
2738
|
-
debounce: 200,
|
|
2739
|
-
regress: () => {
|
|
2740
|
-
const state2 = get();
|
|
2741
|
-
if (performanceTimeout) clearTimeout(performanceTimeout);
|
|
2742
|
-
if (state2.performance.current !== state2.performance.min) setPerformanceCurrent(state2.performance.min);
|
|
2743
|
-
performanceTimeout = setTimeout(
|
|
2744
|
-
() => setPerformanceCurrent(get().performance.max),
|
|
2745
|
-
state2.performance.debounce
|
|
2746
|
-
);
|
|
2747
|
-
}
|
|
2748
|
-
},
|
|
2749
|
-
size: { width: 0, height: 0, top: 0, left: 0 },
|
|
2750
|
-
viewport: {
|
|
2751
|
-
initialDpr: 0,
|
|
2752
|
-
dpr: 0,
|
|
2753
|
-
width: 0,
|
|
2754
|
-
height: 0,
|
|
2755
|
-
top: 0,
|
|
2756
|
-
left: 0,
|
|
2757
|
-
aspect: 0,
|
|
2758
|
-
distance: 0,
|
|
2759
|
-
factor: 0,
|
|
2760
|
-
getCurrentViewport
|
|
2761
|
-
},
|
|
2762
|
-
setEvents: (events) => set((state2) => ({ ...state2, events: { ...state2.events, ...events } })),
|
|
2763
|
-
setSize: (width, height, top, left) => {
|
|
2764
|
-
const state2 = get();
|
|
2765
|
-
if (width === void 0) {
|
|
2766
|
-
set({ _sizeImperative: false });
|
|
2767
|
-
if (state2._sizeProps) {
|
|
2768
|
-
const { width: propW, height: propH } = state2._sizeProps;
|
|
2769
|
-
if (propW !== void 0 || propH !== void 0) {
|
|
2770
|
-
const currentSize = state2.size;
|
|
2771
|
-
const newSize = {
|
|
2772
|
-
width: propW ?? currentSize.width,
|
|
2773
|
-
height: propH ?? currentSize.height,
|
|
2774
|
-
top: currentSize.top,
|
|
2775
|
-
left: currentSize.left
|
|
2776
|
-
};
|
|
2777
|
-
set((s) => ({
|
|
2778
|
-
size: newSize,
|
|
2779
|
-
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
2780
|
-
}));
|
|
2781
|
-
getScheduler().invalidate();
|
|
2782
|
-
}
|
|
2783
|
-
}
|
|
2784
|
-
return;
|
|
2785
|
-
}
|
|
2786
|
-
const w = width;
|
|
2787
|
-
const h = height ?? width;
|
|
2788
|
-
const t = top ?? state2.size.top;
|
|
2789
|
-
const l = left ?? state2.size.left;
|
|
2790
|
-
const size = { width: w, height: h, top: t, left: l };
|
|
2791
|
-
set((s) => ({
|
|
2792
|
-
size,
|
|
2793
|
-
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
2794
|
-
_sizeImperative: true
|
|
2795
|
-
}));
|
|
2796
|
-
getScheduler().invalidate();
|
|
2797
|
-
},
|
|
2798
|
-
setDpr: (dpr) => set((state2) => {
|
|
2799
|
-
const resolved = calculateDpr(dpr);
|
|
2800
|
-
return { viewport: { ...state2.viewport, dpr: resolved, initialDpr: state2.viewport.initialDpr || resolved } };
|
|
2801
|
-
}),
|
|
2802
|
-
setFrameloop: (frameloop = "always") => {
|
|
2803
|
-
set(() => ({ frameloop }));
|
|
2804
|
-
},
|
|
2805
|
-
setError: (error) => set(() => ({ error })),
|
|
2806
|
-
error: null,
|
|
2807
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
2808
|
-
uniforms: {},
|
|
2809
|
-
nodes: {},
|
|
2810
|
-
buffers: {},
|
|
2811
|
-
gpuStorage: {},
|
|
2812
|
-
textures: /* @__PURE__ */ new Map(),
|
|
2813
|
-
_textureRefs: /* @__PURE__ */ new Map(),
|
|
2814
|
-
renderPipeline: null,
|
|
2815
|
-
passes: {},
|
|
2816
|
-
_hmrVersion: 0,
|
|
2817
|
-
_sizeImperative: false,
|
|
2818
|
-
_sizeProps: null,
|
|
2819
|
-
previousRoot: void 0,
|
|
2820
|
-
internal: {
|
|
2821
|
-
// Events
|
|
2822
|
-
interaction: [],
|
|
2823
|
-
subscribers: [],
|
|
2824
|
-
// Per-pointer state (new unified structure)
|
|
2825
|
-
pointerMap: /* @__PURE__ */ new Map(),
|
|
2826
|
-
pointerDirty: /* @__PURE__ */ new Map(),
|
|
2827
|
-
lastEvent: React.createRef(),
|
|
2828
|
-
// Deprecated but kept for backwards compatibility
|
|
2829
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
2830
|
-
initialClick: [0, 0],
|
|
2831
|
-
initialHits: [],
|
|
2832
|
-
capturedMap: /* @__PURE__ */ new Map(),
|
|
2833
|
-
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
2834
|
-
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
2835
|
-
// Occlusion system (WebGPU only)
|
|
2836
|
-
occlusionEnabled: false,
|
|
2837
|
-
occlusionObserver: null,
|
|
2838
|
-
occlusionCache: /* @__PURE__ */ new Map(),
|
|
2839
|
-
helperGroup: null,
|
|
2840
|
-
// Updates
|
|
2841
|
-
active: false,
|
|
2842
|
-
frames: 0,
|
|
2843
|
-
priority: 0,
|
|
2844
|
-
subscribe: (ref, priority, store) => {
|
|
2845
|
-
const internal = get().internal;
|
|
2846
|
-
internal.priority = internal.priority + (priority > 0 ? 1 : 0);
|
|
2847
|
-
internal.subscribers.push({ ref, priority, store });
|
|
2848
|
-
internal.subscribers = internal.subscribers.sort((a, b) => a.priority - b.priority);
|
|
2849
|
-
return () => {
|
|
2850
|
-
const internal2 = get().internal;
|
|
2851
|
-
if (internal2?.subscribers) {
|
|
2852
|
-
internal2.priority = internal2.priority - (priority > 0 ? 1 : 0);
|
|
2853
|
-
internal2.subscribers = internal2.subscribers.filter((s) => s.ref !== ref);
|
|
2854
|
-
}
|
|
2855
|
-
};
|
|
2856
|
-
},
|
|
2857
|
-
// Renderer Storage (single source of truth)
|
|
2858
|
-
actualRenderer: null,
|
|
2859
|
-
// Scheduler for useFrameNext (initialized in renderer.tsx)
|
|
2860
|
-
scheduler: null
|
|
2861
|
-
}
|
|
2862
|
-
};
|
|
2863
|
-
return rootState;
|
|
1870
|
+
};
|
|
1871
|
+
return rootState;
|
|
2864
1872
|
});
|
|
2865
1873
|
const state = rootStore.getState();
|
|
2866
1874
|
Object.defineProperty(state, "gl", {
|
|
@@ -2868,8 +1876,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2868
1876
|
const currentState = rootStore.getState();
|
|
2869
1877
|
if (!currentState.isLegacy && currentState.internal.actualRenderer) {
|
|
2870
1878
|
const stack = new Error().stack || "";
|
|
2871
|
-
|
|
2872
|
-
if (!isInternalAccess) {
|
|
1879
|
+
if (!isInternalRendererAccess(stack)) {
|
|
2873
1880
|
const cleanedStack = stack.split("\n").slice(2).join("\n") || "Stack trace unavailable";
|
|
2874
1881
|
notifyDepreciated({
|
|
2875
1882
|
heading: "Accessing state.gl in WebGPU mode",
|
|
@@ -2982,19 +1989,25 @@ function loadingFn(extensions, onProgress) {
|
|
|
2982
1989
|
);
|
|
2983
1990
|
};
|
|
2984
1991
|
}
|
|
2985
|
-
function useLoader(loader, input, extensions, onProgress) {
|
|
2986
|
-
const
|
|
1992
|
+
function useLoader(loader, input, extensions, onProgress, cacheKey) {
|
|
1993
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
1994
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2987
1995
|
const fn = loadingFn(extensions, onProgress);
|
|
2988
|
-
const results =
|
|
1996
|
+
const results = cacheKeys.map(
|
|
1997
|
+
(key, index) => suspend(() => fn(loader, inputs[index]), [loader, key], { equal: is.equ })
|
|
1998
|
+
);
|
|
2989
1999
|
return Array.isArray(input) ? results : results[0];
|
|
2990
2000
|
}
|
|
2991
|
-
useLoader.preload = function(loader, input, extensions, onProgress) {
|
|
2992
|
-
const
|
|
2993
|
-
|
|
2001
|
+
useLoader.preload = function(loader, input, extensions, onProgress, cacheKey) {
|
|
2002
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
2003
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2004
|
+
cacheKeys.forEach(
|
|
2005
|
+
(key, index) => preload(() => loadingFn(extensions, onProgress)(loader, inputs[index]), [loader, key])
|
|
2006
|
+
);
|
|
2994
2007
|
};
|
|
2995
|
-
useLoader.clear = function(loader, input) {
|
|
2996
|
-
const
|
|
2997
|
-
|
|
2008
|
+
useLoader.clear = function(loader, input, cacheKey) {
|
|
2009
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2010
|
+
cacheKeys.forEach((key) => clear([loader, key]));
|
|
2998
2011
|
};
|
|
2999
2012
|
useLoader.loader = getLoader;
|
|
3000
2013
|
|
|
@@ -3070,20 +2083,14 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
3070
2083
|
}
|
|
3071
2084
|
};
|
|
3072
2085
|
} else {
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
unregisterJob = registerOutside();
|
|
3082
|
-
});
|
|
3083
|
-
return () => {
|
|
3084
|
-
unsubReady();
|
|
3085
|
-
unregisterJob?.();
|
|
3086
|
-
};
|
|
2086
|
+
return scheduler.register(
|
|
2087
|
+
(state, delta) => {
|
|
2088
|
+
const frameState = state;
|
|
2089
|
+
if (!frameState.renderer) return;
|
|
2090
|
+
callbackRef.current?.(frameState, delta);
|
|
2091
|
+
},
|
|
2092
|
+
{ id, ...options }
|
|
2093
|
+
);
|
|
3087
2094
|
}
|
|
3088
2095
|
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
3089
2096
|
const isPaused = React.useSyncExternalStore(
|
|
@@ -3420,7 +2427,7 @@ function advance(timestamp) {
|
|
|
3420
2427
|
getScheduler().step(timestamp);
|
|
3421
2428
|
}
|
|
3422
2429
|
|
|
3423
|
-
const version = "10.0.0-alpha.
|
|
2430
|
+
const version = "10.0.0-alpha.3";
|
|
3424
2431
|
const packageData = {
|
|
3425
2432
|
version: version};
|
|
3426
2433
|
|
|
@@ -14859,6 +13866,7 @@ function swapInstances() {
|
|
|
14859
13866
|
}
|
|
14860
13867
|
}
|
|
14861
13868
|
if (instance.isHidden) unhideInstance(instance);
|
|
13869
|
+
if (isObject3D(instance.object)) removeInteractivity(findInitialRoot(instance), instance.object);
|
|
14862
13870
|
if (instance.object.__r3f) delete instance.object.__r3f;
|
|
14863
13871
|
if (instance.type !== "primitive") disposeOnIdle(instance.object);
|
|
14864
13872
|
}
|
|
@@ -14871,6 +13879,8 @@ function swapInstances() {
|
|
|
14871
13879
|
instance.object.__r3f = instance;
|
|
14872
13880
|
setFiberRef(fiber, instance.object);
|
|
14873
13881
|
delete instance.appliedOnce;
|
|
13882
|
+
instance.eventCount = 0;
|
|
13883
|
+
instance.handlers = {};
|
|
14874
13884
|
applyProps(instance.object, instance.props);
|
|
14875
13885
|
if (instance.props.attach) {
|
|
14876
13886
|
attach(parent, instance);
|
|
@@ -15086,11 +14096,15 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
15086
14096
|
cloneMutableTextInstance(textInstance) {
|
|
15087
14097
|
return textInstance;
|
|
15088
14098
|
},
|
|
14099
|
+
// React 19.2 added these for root-level view transitions. They only run for hosts that opt into
|
|
14100
|
+
// view transitions by returning a container from cloneRootViewTransitionContainer; R3F renders
|
|
14101
|
+
// into a WebGL/WebGPU canvas, where cloning the root has no meaning, so we never opt in and
|
|
14102
|
+
// React never calls them. They stay as no-ops rather than throws: the host config is shared by
|
|
14103
|
+
// every root, and a throw here would turn a React-internal probe into a hard crash for users.
|
|
14104
|
+
// Guarded by a test asserting a full render + unmount never reaches them.
|
|
15089
14105
|
cloneRootViewTransitionContainer(_rootContainer) {
|
|
15090
|
-
throw new Error("Not implemented.");
|
|
15091
14106
|
},
|
|
15092
14107
|
removeRootViewTransitionClone(_rootContainer, _clone) {
|
|
15093
|
-
throw new Error("Not implemented.");
|
|
15094
14108
|
},
|
|
15095
14109
|
// https://github.com/facebook/react/pull/32465
|
|
15096
14110
|
createFragmentInstance: (_fiber) => null,
|
|
@@ -15175,6 +14189,7 @@ function createRoot(canvas) {
|
|
|
15175
14189
|
const lastConfiguredProps = {};
|
|
15176
14190
|
let configured = false;
|
|
15177
14191
|
let pending = null;
|
|
14192
|
+
let rendererSetup = null;
|
|
15178
14193
|
return {
|
|
15179
14194
|
async configure(props = {}) {
|
|
15180
14195
|
let resolve;
|
|
@@ -15205,7 +14220,7 @@ function createRoot(canvas) {
|
|
|
15205
14220
|
forceEven
|
|
15206
14221
|
} = props;
|
|
15207
14222
|
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || SRGBColorSpace;
|
|
15208
|
-
|
|
14223
|
+
let state = store.getState();
|
|
15209
14224
|
const defaultGPUProps = {
|
|
15210
14225
|
canvas,
|
|
15211
14226
|
antialias: true
|
|
@@ -15219,52 +14234,64 @@ function createRoot(canvas) {
|
|
|
15219
14234
|
throw new Error("Cannot use both gl and renderer props at the same time");
|
|
15220
14235
|
}
|
|
15221
14236
|
let renderer = state.internal.actualRenderer;
|
|
15222
|
-
if (
|
|
15223
|
-
|
|
15224
|
-
|
|
15225
|
-
|
|
15226
|
-
|
|
15227
|
-
|
|
15228
|
-
|
|
15229
|
-
|
|
15230
|
-
|
|
15231
|
-
|
|
15232
|
-
|
|
15233
|
-
|
|
15234
|
-
|
|
15235
|
-
|
|
15236
|
-
|
|
15237
|
-
|
|
15238
|
-
|
|
15239
|
-
|
|
15240
|
-
|
|
15241
|
-
|
|
15242
|
-
|
|
15243
|
-
|
|
15244
|
-
|
|
15245
|
-
|
|
15246
|
-
|
|
15247
|
-
|
|
15248
|
-
|
|
15249
|
-
|
|
15250
|
-
|
|
15251
|
-
|
|
15252
|
-
|
|
15253
|
-
|
|
15254
|
-
|
|
15255
|
-
|
|
15256
|
-
|
|
15257
|
-
|
|
15258
|
-
|
|
15259
|
-
|
|
15260
|
-
|
|
15261
|
-
|
|
15262
|
-
|
|
15263
|
-
|
|
15264
|
-
|
|
14237
|
+
if (!state.internal.actualRenderer) {
|
|
14238
|
+
if (!rendererSetup) {
|
|
14239
|
+
rendererSetup = (async () => {
|
|
14240
|
+
if (primaryCanvas) {
|
|
14241
|
+
const primary = await waitForPrimary(primaryCanvas);
|
|
14242
|
+
renderer = primary.renderer;
|
|
14243
|
+
state.internal.actualRenderer = renderer;
|
|
14244
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14245
|
+
primary.store.setState((prev) => ({
|
|
14246
|
+
internal: { ...prev.internal, isMultiCanvas: true }
|
|
14247
|
+
}));
|
|
14248
|
+
state.set((prev) => ({
|
|
14249
|
+
webGPUSupported: primary.store.getState().webGPUSupported,
|
|
14250
|
+
renderer,
|
|
14251
|
+
primaryStore: primary.store,
|
|
14252
|
+
internal: {
|
|
14253
|
+
...prev.internal,
|
|
14254
|
+
canvasTarget,
|
|
14255
|
+
isMultiCanvas: true,
|
|
14256
|
+
isSecondary: true,
|
|
14257
|
+
targetId: primaryCanvas
|
|
14258
|
+
}
|
|
14259
|
+
}));
|
|
14260
|
+
} else {
|
|
14261
|
+
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, WebGPURenderer);
|
|
14262
|
+
if (!renderer.hasInitialized?.()) {
|
|
14263
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14264
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14265
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14266
|
+
canvas.width = size2.width * pixelRatio;
|
|
14267
|
+
canvas.height = size2.height * pixelRatio;
|
|
14268
|
+
}
|
|
14269
|
+
await renderer.init();
|
|
14270
|
+
}
|
|
14271
|
+
const backend = renderer.backend;
|
|
14272
|
+
const isWebGPUBackend = backend && "isWebGPUBackend" in backend;
|
|
14273
|
+
state.internal.actualRenderer = renderer;
|
|
14274
|
+
state.set({ webGPUSupported: isWebGPUBackend, renderer, primaryStore: store });
|
|
14275
|
+
if (canvasId && !state.internal.isSecondary) {
|
|
14276
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14277
|
+
const unregisterPrimary = registerPrimary(canvasId, renderer, store);
|
|
14278
|
+
state.set((prev) => ({
|
|
14279
|
+
internal: {
|
|
14280
|
+
...prev.internal,
|
|
14281
|
+
canvasTarget,
|
|
14282
|
+
unregisterPrimary
|
|
14283
|
+
}
|
|
14284
|
+
}));
|
|
14285
|
+
}
|
|
15265
14286
|
}
|
|
15266
|
-
}))
|
|
14287
|
+
})().catch((err) => {
|
|
14288
|
+
rendererSetup = null;
|
|
14289
|
+
throw err;
|
|
14290
|
+
});
|
|
15267
14291
|
}
|
|
14292
|
+
await rendererSetup;
|
|
14293
|
+
state = store.getState();
|
|
14294
|
+
renderer = state.internal.actualRenderer;
|
|
15268
14295
|
}
|
|
15269
14296
|
let raycaster = state.raycaster;
|
|
15270
14297
|
if (!raycaster) state.set({ raycaster: raycaster = new Raycaster() });
|
|
@@ -15489,7 +14516,7 @@ function createRoot(canvas) {
|
|
|
15489
14516
|
{
|
|
15490
14517
|
id: `${newRootId}_frustum`,
|
|
15491
14518
|
rootId: newRootId,
|
|
15492
|
-
|
|
14519
|
+
before: "render",
|
|
15493
14520
|
system: true
|
|
15494
14521
|
}
|
|
15495
14522
|
);
|
|
@@ -15501,7 +14528,7 @@ function createRoot(canvas) {
|
|
|
15501
14528
|
{
|
|
15502
14529
|
id: `${newRootId}_visibility`,
|
|
15503
14530
|
rootId: newRootId,
|
|
15504
|
-
|
|
14531
|
+
before: "render",
|
|
15505
14532
|
system: true,
|
|
15506
14533
|
after: `${newRootId}_frustum`
|
|
15507
14534
|
}
|
|
@@ -15614,7 +14641,8 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
15614
14641
|
dispose(state.scene);
|
|
15615
14642
|
_roots.delete(canvas);
|
|
15616
14643
|
if (callback) callback(canvas);
|
|
15617
|
-
} catch {
|
|
14644
|
+
} catch (error) {
|
|
14645
|
+
console.warn("[R3F] Error while unmounting root; teardown may be incomplete:", error);
|
|
15618
14646
|
}
|
|
15619
14647
|
}, 500);
|
|
15620
14648
|
}
|
|
@@ -15677,6 +14705,7 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15677
14705
|
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(), resolvedSize);
|
|
15678
14706
|
if (camera !== rootState.camera) updateCamera(camera, resolvedSize);
|
|
15679
14707
|
}
|
|
14708
|
+
const set = injectState.set;
|
|
15680
14709
|
return {
|
|
15681
14710
|
// The intersect consists of the previous root state
|
|
15682
14711
|
...rootState,
|
|
@@ -15695,33 +14724,205 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15695
14724
|
size: resolvedSize,
|
|
15696
14725
|
viewport: { ...rootState.viewport, ...viewport },
|
|
15697
14726
|
// Layers are allowed to override events
|
|
15698
|
-
setEvents: (events2) =>
|
|
14727
|
+
setEvents: (events2) => set((state2) => ({ ...state2, events: { ...state2.events, ...events2 } })),
|
|
15699
14728
|
// Container for child attachment - the portalScene (injected or container itself)
|
|
15700
14729
|
internal: { ...rootState.internal, ...injectState.internal, container: portalScene }
|
|
15701
14730
|
};
|
|
15702
14731
|
});
|
|
15703
|
-
const usePortalStore = useMemo(() => {
|
|
15704
|
-
const store = createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
15705
|
-
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
15706
|
-
onMutate(previousRoot.getState());
|
|
15707
|
-
return store;
|
|
15708
|
-
}, [previousRoot, container]);
|
|
15709
|
-
useIsomorphicLayoutEffect(() => {
|
|
15710
|
-
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
15711
|
-
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
15712
|
-
return unsubscribe;
|
|
15713
|
-
}, [previousRoot, usePortalStore]);
|
|
15714
|
-
return (
|
|
15715
|
-
// @ts-ignore, reconciler types are not maintained
|
|
15716
|
-
/* @__PURE__ */ jsx(Fragment, { children: reconciler.createPortal(
|
|
15717
|
-
/* @__PURE__ */ jsx(context.Provider, { value: usePortalStore, children }),
|
|
15718
|
-
usePortalStore,
|
|
15719
|
-
null
|
|
15720
|
-
) })
|
|
15721
|
-
);
|
|
14732
|
+
const usePortalStore = useMemo(() => {
|
|
14733
|
+
const store = createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
14734
|
+
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
14735
|
+
onMutate(previousRoot.getState());
|
|
14736
|
+
return store;
|
|
14737
|
+
}, [previousRoot, container]);
|
|
14738
|
+
useIsomorphicLayoutEffect(() => {
|
|
14739
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14740
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14741
|
+
return unsubscribe;
|
|
14742
|
+
}, [previousRoot, usePortalStore]);
|
|
14743
|
+
return (
|
|
14744
|
+
// @ts-ignore, reconciler types are not maintained
|
|
14745
|
+
/* @__PURE__ */ jsx(Fragment, { children: reconciler.createPortal(
|
|
14746
|
+
/* @__PURE__ */ jsx(context.Provider, { value: usePortalStore, children }),
|
|
14747
|
+
usePortalStore,
|
|
14748
|
+
null
|
|
14749
|
+
) })
|
|
14750
|
+
);
|
|
14751
|
+
}
|
|
14752
|
+
function flushSync(fn) {
|
|
14753
|
+
return reconciler.flushSyncFromReconciler(fn);
|
|
14754
|
+
}
|
|
14755
|
+
|
|
14756
|
+
function parseBackground(background) {
|
|
14757
|
+
if (!background) return null;
|
|
14758
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14759
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14760
|
+
return {
|
|
14761
|
+
...rest,
|
|
14762
|
+
preset,
|
|
14763
|
+
files: envMap || files,
|
|
14764
|
+
backgroundFiles: backgroundMap,
|
|
14765
|
+
background: true
|
|
14766
|
+
};
|
|
14767
|
+
}
|
|
14768
|
+
if (typeof background === "number") {
|
|
14769
|
+
return { color: background, background: true };
|
|
14770
|
+
}
|
|
14771
|
+
if (typeof background === "string") {
|
|
14772
|
+
if (background in presetsObj) {
|
|
14773
|
+
return { preset: background, background: true };
|
|
14774
|
+
}
|
|
14775
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14776
|
+
return { files: background, background: true };
|
|
14777
|
+
}
|
|
14778
|
+
return { color: background, background: true };
|
|
14779
|
+
}
|
|
14780
|
+
if (background.isColor) {
|
|
14781
|
+
return { color: background, background: true };
|
|
14782
|
+
}
|
|
14783
|
+
return null;
|
|
14784
|
+
}
|
|
14785
|
+
|
|
14786
|
+
function parseRendererConfig(rendererProp) {
|
|
14787
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14788
|
+
if (isRendererConfig) {
|
|
14789
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14790
|
+
return {
|
|
14791
|
+
primaryCanvas: pc,
|
|
14792
|
+
scheduler: sc,
|
|
14793
|
+
renderer: Object.keys(rest).length > 0 ? rest : rendererProp
|
|
14794
|
+
};
|
|
14795
|
+
}
|
|
14796
|
+
return { primaryCanvas: void 0, scheduler: void 0, renderer: rendererProp };
|
|
14797
|
+
}
|
|
14798
|
+
|
|
14799
|
+
const RESOURCE_KINDS = ["nodes", "uniforms", "buffers", "gpuStorage"];
|
|
14800
|
+
const ROOT_SCOPE = "";
|
|
14801
|
+
const registries = /* @__PURE__ */ new WeakMap();
|
|
14802
|
+
function getKindRegistry(store, kind) {
|
|
14803
|
+
let registry = registries.get(store);
|
|
14804
|
+
if (!registry) {
|
|
14805
|
+
registry = {
|
|
14806
|
+
nodes: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14807
|
+
uniforms: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14808
|
+
buffers: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14809
|
+
gpuStorage: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() }
|
|
14810
|
+
};
|
|
14811
|
+
registries.set(store, registry);
|
|
14812
|
+
}
|
|
14813
|
+
return registry[kind];
|
|
14814
|
+
}
|
|
14815
|
+
function resolvePrimary$1(store) {
|
|
14816
|
+
return store.getState().primaryStore ?? store;
|
|
14817
|
+
}
|
|
14818
|
+
function isScopeValid(store, kind, scopeKey) {
|
|
14819
|
+
return getKindRegistry(store, kind).valid.has(scopeKey);
|
|
14820
|
+
}
|
|
14821
|
+
function peekStaged(store, kind, scopeKey) {
|
|
14822
|
+
return getKindRegistry(store, kind).staged.get(scopeKey);
|
|
14823
|
+
}
|
|
14824
|
+
function stageEntries(store, kind, scopeKey, entries) {
|
|
14825
|
+
const { staged } = getKindRegistry(store, kind);
|
|
14826
|
+
const current = staged.get(scopeKey);
|
|
14827
|
+
if (current) Object.assign(current, entries);
|
|
14828
|
+
else staged.set(scopeKey, { ...entries });
|
|
14829
|
+
}
|
|
14830
|
+
function readCommittedScope(map, scopeKey, isLeaf) {
|
|
14831
|
+
if (scopeKey === ROOT_SCOPE) {
|
|
14832
|
+
const leaves = {};
|
|
14833
|
+
for (const [key, value] of Object.entries(map)) if (isLeaf(value)) leaves[key] = value;
|
|
14834
|
+
return leaves;
|
|
14835
|
+
}
|
|
14836
|
+
const scopeData = map[scopeKey];
|
|
14837
|
+
if (scopeData && typeof scopeData === "object" && !isLeaf(scopeData)) return scopeData;
|
|
14838
|
+
return {};
|
|
14839
|
+
}
|
|
14840
|
+
function withStagedOverlay(store, kind, committed) {
|
|
14841
|
+
const { staged } = getKindRegistry(store, kind);
|
|
14842
|
+
if (staged.size === 0) return committed;
|
|
14843
|
+
const view = { ...committed };
|
|
14844
|
+
for (const [scopeKey, entries] of staged) {
|
|
14845
|
+
if (scopeKey === ROOT_SCOPE) Object.assign(view, entries);
|
|
14846
|
+
else view[scopeKey] = { ...view[scopeKey], ...entries };
|
|
14847
|
+
}
|
|
14848
|
+
return view;
|
|
14849
|
+
}
|
|
14850
|
+
function flushStagedScope(store, kind, scopeKey, isLeaf) {
|
|
14851
|
+
const registry = getKindRegistry(store, kind);
|
|
14852
|
+
const staged = registry.staged.get(scopeKey);
|
|
14853
|
+
if (!staged) return;
|
|
14854
|
+
registry.staged.delete(scopeKey);
|
|
14855
|
+
const replace = !registry.valid.has(scopeKey);
|
|
14856
|
+
store.setState((state) => {
|
|
14857
|
+
const map = state[kind];
|
|
14858
|
+
if (scopeKey === ROOT_SCOPE) {
|
|
14859
|
+
if (!replace) return { [kind]: { ...map, ...staged } };
|
|
14860
|
+
const next = {};
|
|
14861
|
+
for (const [key, value] of Object.entries(map)) if (!isLeaf(value)) next[key] = value;
|
|
14862
|
+
return { [kind]: { ...next, ...staged } };
|
|
14863
|
+
}
|
|
14864
|
+
const current = replace ? {} : map[scopeKey] ?? {};
|
|
14865
|
+
return { [kind]: { ...map, [scopeKey]: { ...current, ...staged } } };
|
|
14866
|
+
});
|
|
14867
|
+
registry.valid.add(scopeKey);
|
|
14868
|
+
}
|
|
14869
|
+
function invalidateResource(store, kind, scopeKey) {
|
|
14870
|
+
const registry = getKindRegistry(store, kind);
|
|
14871
|
+
if (scopeKey === void 0) {
|
|
14872
|
+
registry.valid.clear();
|
|
14873
|
+
registry.staged.clear();
|
|
14874
|
+
} else {
|
|
14875
|
+
registry.valid.delete(scopeKey);
|
|
14876
|
+
registry.staged.delete(scopeKey);
|
|
14877
|
+
}
|
|
14878
|
+
}
|
|
14879
|
+
function rebuildResource(store, kind, userScope) {
|
|
14880
|
+
const primary = resolvePrimary$1(store);
|
|
14881
|
+
invalidateResource(primary, kind, userScope === "root" ? ROOT_SCOPE : userScope);
|
|
14882
|
+
primary.setState((state) => ({ _hmrVersion: state._hmrVersion + 1 }));
|
|
14883
|
+
}
|
|
14884
|
+
function invalidateAllResources(store) {
|
|
14885
|
+
for (const kind of RESOURCE_KINDS) invalidateResource(store, kind);
|
|
14886
|
+
}
|
|
14887
|
+
function removeResourceEntries(store, kind, names, scope, isLeaf) {
|
|
14888
|
+
const staged = peekStaged(store, kind, scope ?? ROOT_SCOPE);
|
|
14889
|
+
if (staged) for (const name of names) delete staged[name];
|
|
14890
|
+
store.setState((state) => {
|
|
14891
|
+
const map = state[kind];
|
|
14892
|
+
if (scope) {
|
|
14893
|
+
const currentScope = { ...map[scope] };
|
|
14894
|
+
for (const name of names) delete currentScope[name];
|
|
14895
|
+
return { [kind]: { ...map, [scope]: currentScope } };
|
|
14896
|
+
}
|
|
14897
|
+
const next = { ...map };
|
|
14898
|
+
for (const name of names) if (isLeaf(next[name])) delete next[name];
|
|
14899
|
+
return { [kind]: next };
|
|
14900
|
+
});
|
|
15722
14901
|
}
|
|
15723
|
-
function
|
|
15724
|
-
|
|
14902
|
+
function clearResourceEntries(store, kind, scope, isLeaf) {
|
|
14903
|
+
const registry = getKindRegistry(store, kind);
|
|
14904
|
+
if (scope && scope !== "root") registry.staged.delete(scope);
|
|
14905
|
+
else if (scope === "root") registry.staged.delete(ROOT_SCOPE);
|
|
14906
|
+
else registry.staged.clear();
|
|
14907
|
+
store.setState((state) => {
|
|
14908
|
+
const map = state[kind];
|
|
14909
|
+
if (scope && scope !== "root") {
|
|
14910
|
+
const { [scope]: _, ...rest } = map;
|
|
14911
|
+
return { [kind]: rest };
|
|
14912
|
+
}
|
|
14913
|
+
if (scope === "root") {
|
|
14914
|
+
const next = {};
|
|
14915
|
+
for (const [key, value] of Object.entries(map)) if (!isLeaf(value)) next[key] = value;
|
|
14916
|
+
return { [kind]: next };
|
|
14917
|
+
}
|
|
14918
|
+
return { [kind]: {} };
|
|
14919
|
+
});
|
|
14920
|
+
}
|
|
14921
|
+
|
|
14922
|
+
function clearHmrCaches(store) {
|
|
14923
|
+
const primary = store.getState().primaryStore ?? store;
|
|
14924
|
+
invalidateAllResources(primary);
|
|
14925
|
+
primary.setState((state) => ({ _hmrVersion: state._hmrVersion + 1 }));
|
|
15725
14926
|
}
|
|
15726
14927
|
|
|
15727
14928
|
function CanvasImpl({
|
|
@@ -15744,6 +14945,8 @@ function CanvasImpl({
|
|
|
15744
14945
|
raycaster,
|
|
15745
14946
|
camera,
|
|
15746
14947
|
scene,
|
|
14948
|
+
autoUpdateFrustum,
|
|
14949
|
+
occlusion,
|
|
15747
14950
|
onPointerMissed,
|
|
15748
14951
|
onDragOverMissed,
|
|
15749
14952
|
onDropMissed,
|
|
@@ -15755,49 +14958,10 @@ function CanvasImpl({
|
|
|
15755
14958
|
forceEven,
|
|
15756
14959
|
...props
|
|
15757
14960
|
}) {
|
|
15758
|
-
const
|
|
15759
|
-
let primaryCanvas;
|
|
15760
|
-
let scheduler;
|
|
15761
|
-
let renderer;
|
|
15762
|
-
if (isRendererConfig) {
|
|
15763
|
-
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
15764
|
-
primaryCanvas = pc;
|
|
15765
|
-
scheduler = sc;
|
|
15766
|
-
renderer = Object.keys(rest).length > 0 ? rest : rendererProp;
|
|
15767
|
-
} else {
|
|
15768
|
-
renderer = rendererProp;
|
|
15769
|
-
}
|
|
14961
|
+
const { primaryCanvas, scheduler, renderer } = parseRendererConfig(rendererProp);
|
|
15770
14962
|
React.useMemo(() => extend(THREE), []);
|
|
15771
14963
|
const Bridge = useBridge();
|
|
15772
|
-
const backgroundProps = React.useMemo(() =>
|
|
15773
|
-
if (!background) return null;
|
|
15774
|
-
if (typeof background === "object" && !background.isColor) {
|
|
15775
|
-
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
15776
|
-
return {
|
|
15777
|
-
...rest,
|
|
15778
|
-
preset,
|
|
15779
|
-
files: envMap || files,
|
|
15780
|
-
backgroundFiles: backgroundMap,
|
|
15781
|
-
background: true
|
|
15782
|
-
};
|
|
15783
|
-
}
|
|
15784
|
-
if (typeof background === "number") {
|
|
15785
|
-
return { color: background, background: true };
|
|
15786
|
-
}
|
|
15787
|
-
if (typeof background === "string") {
|
|
15788
|
-
if (background in presetsObj) {
|
|
15789
|
-
return { preset: background, background: true };
|
|
15790
|
-
}
|
|
15791
|
-
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
15792
|
-
return { files: background, background: true };
|
|
15793
|
-
}
|
|
15794
|
-
return { color: background, background: true };
|
|
15795
|
-
}
|
|
15796
|
-
if (background.isColor) {
|
|
15797
|
-
return { color: background, background: true };
|
|
15798
|
-
}
|
|
15799
|
-
return null;
|
|
15800
|
-
}, [background]);
|
|
14964
|
+
const backgroundProps = React.useMemo(() => parseBackground(background), [background]);
|
|
15801
14965
|
const hasInitialSizeRef = React.useRef(false);
|
|
15802
14966
|
const measureConfig = React.useMemo(() => {
|
|
15803
14967
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15839,6 +15003,7 @@ function CanvasImpl({
|
|
|
15839
15003
|
const handleDropMissed = useMutableCallback(onDropMissed);
|
|
15840
15004
|
const [block, setBlock] = React.useState(false);
|
|
15841
15005
|
const [error, setError] = React.useState(false);
|
|
15006
|
+
const [fallbackVisible, setFallbackVisible] = React.useState(false);
|
|
15842
15007
|
if (block) throw block;
|
|
15843
15008
|
if (error) throw error;
|
|
15844
15009
|
const root = React.useRef(null);
|
|
@@ -15846,6 +15011,7 @@ function CanvasImpl({
|
|
|
15846
15011
|
const unsubscribeErrorRef = React.useRef(null);
|
|
15847
15012
|
useIsomorphicLayoutEffect(() => {
|
|
15848
15013
|
effectActiveRef.current = true;
|
|
15014
|
+
if (fallbackVisible) return;
|
|
15849
15015
|
const canvas = canvasRef.current;
|
|
15850
15016
|
if (effectiveSize.width > 0 && effectiveSize.height > 0 && canvas) {
|
|
15851
15017
|
if (!root.current) {
|
|
@@ -15866,7 +15032,7 @@ function CanvasImpl({
|
|
|
15866
15032
|
}
|
|
15867
15033
|
async function run() {
|
|
15868
15034
|
if (!effectActiveRef.current || !root.current) return;
|
|
15869
|
-
await root.current.configure({
|
|
15035
|
+
const configured = await root.current.configure({
|
|
15870
15036
|
id,
|
|
15871
15037
|
primaryCanvas,
|
|
15872
15038
|
scheduler,
|
|
@@ -15881,6 +15047,8 @@ function CanvasImpl({
|
|
|
15881
15047
|
performance,
|
|
15882
15048
|
raycaster,
|
|
15883
15049
|
camera,
|
|
15050
|
+
autoUpdateFrustum,
|
|
15051
|
+
occlusion,
|
|
15884
15052
|
size: effectiveSize,
|
|
15885
15053
|
// Store size props for reset functionality
|
|
15886
15054
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
@@ -15905,8 +15073,20 @@ function CanvasImpl({
|
|
|
15905
15073
|
}
|
|
15906
15074
|
onCreated?.(state);
|
|
15907
15075
|
}
|
|
15908
|
-
})
|
|
15909
|
-
|
|
15076
|
+
}).then(
|
|
15077
|
+
() => true,
|
|
15078
|
+
// Renderer setup failed (e.g. no WebGL/WebGPU support). The `fallback` prop lives
|
|
15079
|
+
// inside <canvas>, which browsers don't display, so surface it as visible DOM
|
|
15080
|
+
// instead; with no fallback, rethrow to an external error boundary. (#3757)
|
|
15081
|
+
(setupError) => {
|
|
15082
|
+
if (effectActiveRef.current) {
|
|
15083
|
+
if (fallback != null) setFallbackVisible(true);
|
|
15084
|
+
else setError(setupError);
|
|
15085
|
+
}
|
|
15086
|
+
return false;
|
|
15087
|
+
}
|
|
15088
|
+
);
|
|
15089
|
+
if (!configured || !effectActiveRef.current || !root.current) return;
|
|
15910
15090
|
root.current.render(
|
|
15911
15091
|
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */ jsxs(React.Suspense, { fallback: /* @__PURE__ */ jsx(Block, { set: setBlock }), children: [
|
|
15912
15092
|
backgroundProps && /* @__PURE__ */ jsx(Environment, { ...backgroundProps }),
|
|
@@ -15940,14 +15120,7 @@ function CanvasImpl({
|
|
|
15940
15120
|
const handleHMR = () => {
|
|
15941
15121
|
queueMicrotask(() => {
|
|
15942
15122
|
const rootEntry = _roots.get(canvas);
|
|
15943
|
-
if (rootEntry?.store)
|
|
15944
|
-
console.log("[R3F] HMR detected \u2014 rebuilding nodes/uniforms");
|
|
15945
|
-
rootEntry.store.setState((state) => ({
|
|
15946
|
-
nodes: {},
|
|
15947
|
-
uniforms: {},
|
|
15948
|
-
_hmrVersion: state._hmrVersion + 1
|
|
15949
|
-
}));
|
|
15950
|
-
}
|
|
15123
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15951
15124
|
});
|
|
15952
15125
|
};
|
|
15953
15126
|
if (typeof import.meta !== "undefined" && import.meta.hot) {
|
|
@@ -15976,7 +15149,11 @@ function CanvasImpl({
|
|
|
15976
15149
|
...style
|
|
15977
15150
|
},
|
|
15978
15151
|
...props,
|
|
15979
|
-
children:
|
|
15152
|
+
children: fallbackVisible ? (
|
|
15153
|
+
// Renderer setup failed: render the fallback as visible DOM. Inside <canvas> (below)
|
|
15154
|
+
// it exists in the tree but browsers never paint it, which is the whole bug (#3757).
|
|
15155
|
+
fallback
|
|
15156
|
+
) : /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
15980
15157
|
"canvas",
|
|
15981
15158
|
{
|
|
15982
15159
|
ref: canvasRef,
|
|
@@ -16058,35 +15235,50 @@ let ScopedStore = _ScopedStore;
|
|
|
16058
15235
|
function createScopedStore(data) {
|
|
16059
15236
|
return new ScopedStore(data);
|
|
16060
15237
|
}
|
|
16061
|
-
function createLazyCreatorState(state) {
|
|
15238
|
+
function createLazyCreatorState(state, store) {
|
|
16062
15239
|
let _uniforms = null;
|
|
16063
15240
|
let _nodes = null;
|
|
16064
15241
|
let _buffers = null;
|
|
16065
15242
|
let _gpuStorage = null;
|
|
15243
|
+
const view = (kind) => store ? withStagedOverlay(store, kind, state[kind]) : state[kind];
|
|
16066
15244
|
return Object.create(state, {
|
|
16067
15245
|
uniforms: {
|
|
16068
15246
|
get() {
|
|
16069
|
-
return _uniforms ?? (_uniforms = createScopedStore(
|
|
15247
|
+
return _uniforms ?? (_uniforms = createScopedStore(view("uniforms")));
|
|
16070
15248
|
}
|
|
16071
15249
|
},
|
|
16072
15250
|
nodes: {
|
|
16073
15251
|
get() {
|
|
16074
|
-
return _nodes ?? (_nodes = createScopedStore(
|
|
15252
|
+
return _nodes ?? (_nodes = createScopedStore(view("nodes")));
|
|
16075
15253
|
}
|
|
16076
15254
|
},
|
|
16077
15255
|
buffers: {
|
|
16078
15256
|
get() {
|
|
16079
|
-
return _buffers ?? (_buffers = createScopedStore(
|
|
15257
|
+
return _buffers ?? (_buffers = createScopedStore(view("buffers")));
|
|
16080
15258
|
}
|
|
16081
15259
|
},
|
|
16082
15260
|
gpuStorage: {
|
|
16083
15261
|
get() {
|
|
16084
|
-
return _gpuStorage ?? (_gpuStorage = createScopedStore(
|
|
15262
|
+
return _gpuStorage ?? (_gpuStorage = createScopedStore(view("gpuStorage")));
|
|
16085
15263
|
}
|
|
16086
15264
|
}
|
|
16087
15265
|
});
|
|
16088
15266
|
}
|
|
16089
15267
|
|
|
15268
|
+
function resolvePrimary(local) {
|
|
15269
|
+
return local.getState().primaryStore ?? local;
|
|
15270
|
+
}
|
|
15271
|
+
function usePrimaryStore() {
|
|
15272
|
+
const local = useStore();
|
|
15273
|
+
const primary = local.getState().primaryStore;
|
|
15274
|
+
return useMemo(() => primary ?? local, [primary, local]);
|
|
15275
|
+
}
|
|
15276
|
+
function usePrimaryThree(selector = (state) => state, equalityFn) {
|
|
15277
|
+
const local = useStore();
|
|
15278
|
+
const primary = resolvePrimary(local);
|
|
15279
|
+
return primary(selector, equalityFn);
|
|
15280
|
+
}
|
|
15281
|
+
|
|
16090
15282
|
function addTexture(set, key, value) {
|
|
16091
15283
|
set((state) => {
|
|
16092
15284
|
const newMap = new Map(state.textures);
|
|
@@ -16190,65 +15382,56 @@ function useCompareMemoize(value, deep) {
|
|
|
16190
15382
|
return ref.current;
|
|
16191
15383
|
}
|
|
16192
15384
|
|
|
15385
|
+
function useScopedResource(options) {
|
|
15386
|
+
const { store, kind, scope, isLeaf, create, prepare, reconcile, input } = options;
|
|
15387
|
+
const scopeKey = scope ?? ROOT_SCOPE;
|
|
15388
|
+
const version = store((s) => s._hmrVersion);
|
|
15389
|
+
const entries = useMemo(() => {
|
|
15390
|
+
const staged = peekStaged(store, kind, scopeKey);
|
|
15391
|
+
const reusable = isScopeValid(store, kind, scopeKey);
|
|
15392
|
+
const map = store.getState()[kind];
|
|
15393
|
+
const committed = reusable ? readCommittedScope(map, scopeKey, isLeaf) : void 0;
|
|
15394
|
+
const result = {};
|
|
15395
|
+
let fresh = null;
|
|
15396
|
+
for (const [name, candidate] of Object.entries(create())) {
|
|
15397
|
+
const existing = staged?.[name] ?? committed?.[name];
|
|
15398
|
+
if (existing !== void 0) {
|
|
15399
|
+
result[name] = existing;
|
|
15400
|
+
reconcile?.(existing, candidate);
|
|
15401
|
+
} else {
|
|
15402
|
+
const value = prepare ? prepare(name, candidate) : candidate;
|
|
15403
|
+
result[name] = value;
|
|
15404
|
+
(fresh ?? (fresh = {}))[name] = value;
|
|
15405
|
+
}
|
|
15406
|
+
}
|
|
15407
|
+
if (fresh) stageEntries(store, kind, scopeKey, fresh);
|
|
15408
|
+
return result;
|
|
15409
|
+
}, [store, kind, scopeKey, version, input]);
|
|
15410
|
+
useIsomorphicLayoutEffect(() => {
|
|
15411
|
+
flushStagedScope(store, kind, scopeKey, isLeaf);
|
|
15412
|
+
});
|
|
15413
|
+
return entries;
|
|
15414
|
+
}
|
|
15415
|
+
|
|
16193
15416
|
const isUniformNode$1 = (value) => value !== null && typeof value === "object" && "value" in value && "uuid" in value;
|
|
16194
15417
|
function useUniforms(creatorOrScope, scope) {
|
|
16195
|
-
const store =
|
|
15418
|
+
const store = usePrimaryStore();
|
|
16196
15419
|
const removeUniforms2 = useCallback(
|
|
16197
|
-
(names, targetScope) =>
|
|
16198
|
-
const nameArray = Array.isArray(names) ? names : [names];
|
|
16199
|
-
store.setState((state) => {
|
|
16200
|
-
if (targetScope) {
|
|
16201
|
-
const currentScope = { ...state.uniforms[targetScope] };
|
|
16202
|
-
for (const name of nameArray) delete currentScope[name];
|
|
16203
|
-
return { uniforms: { ...state.uniforms, [targetScope]: currentScope } };
|
|
16204
|
-
}
|
|
16205
|
-
const uniforms2 = { ...state.uniforms };
|
|
16206
|
-
for (const name of nameArray) if (isUniformNode$1(uniforms2[name])) delete uniforms2[name];
|
|
16207
|
-
return { uniforms: uniforms2 };
|
|
16208
|
-
});
|
|
16209
|
-
},
|
|
15420
|
+
(names, targetScope) => removeResourceEntries(store, "uniforms", Array.isArray(names) ? names : [names], targetScope, isUniformNode$1),
|
|
16210
15421
|
[store]
|
|
16211
15422
|
);
|
|
16212
15423
|
const clearUniforms = useCallback(
|
|
16213
|
-
(targetScope) =>
|
|
16214
|
-
store.setState((state) => {
|
|
16215
|
-
if (targetScope && targetScope !== "root") {
|
|
16216
|
-
const { [targetScope]: _, ...rest } = state.uniforms;
|
|
16217
|
-
return { uniforms: rest };
|
|
16218
|
-
}
|
|
16219
|
-
if (targetScope === "root") {
|
|
16220
|
-
const uniforms2 = {};
|
|
16221
|
-
for (const [key, value] of Object.entries(state.uniforms)) {
|
|
16222
|
-
if (!isUniformNode$1(value)) uniforms2[key] = value;
|
|
16223
|
-
}
|
|
16224
|
-
return { uniforms: uniforms2 };
|
|
16225
|
-
}
|
|
16226
|
-
return { uniforms: {} };
|
|
16227
|
-
});
|
|
16228
|
-
},
|
|
15424
|
+
(targetScope) => clearResourceEntries(store, "uniforms", targetScope, isUniformNode$1),
|
|
16229
15425
|
[store]
|
|
16230
15426
|
);
|
|
16231
15427
|
const rebuildUniforms = useCallback(
|
|
16232
|
-
(targetScope) =>
|
|
16233
|
-
store.setState((state) => {
|
|
16234
|
-
let newUniforms = {};
|
|
16235
|
-
if (targetScope && targetScope !== "root") {
|
|
16236
|
-
const { [targetScope]: _, ...rest } = state.uniforms;
|
|
16237
|
-
newUniforms = rest;
|
|
16238
|
-
} else if (targetScope === "root") {
|
|
16239
|
-
for (const [key, value] of Object.entries(state.uniforms)) {
|
|
16240
|
-
if (!isUniformNode$1(value)) newUniforms[key] = value;
|
|
16241
|
-
}
|
|
16242
|
-
}
|
|
16243
|
-
return { uniforms: newUniforms, _hmrVersion: state._hmrVersion + 1 };
|
|
16244
|
-
});
|
|
16245
|
-
},
|
|
15428
|
+
(targetScope) => rebuildResource(store, "uniforms", targetScope),
|
|
16246
15429
|
[store]
|
|
16247
15430
|
);
|
|
16248
15431
|
const inputForMemoization = useMemo(() => {
|
|
16249
15432
|
let raw = creatorOrScope;
|
|
16250
15433
|
if (is.fun(creatorOrScope)) {
|
|
16251
|
-
const wrappedState = createLazyCreatorState(store.getState());
|
|
15434
|
+
const wrappedState = createLazyCreatorState(store.getState(), store);
|
|
16252
15435
|
raw = creatorOrScope(wrappedState);
|
|
16253
15436
|
}
|
|
16254
15437
|
if (raw && typeof raw === "object" && !Array.isArray(raw)) {
|
|
@@ -16262,80 +15445,44 @@ function useUniforms(creatorOrScope, scope) {
|
|
|
16262
15445
|
}, [creatorOrScope, store]);
|
|
16263
15446
|
const memoizedInput = useCompareMemoize(inputForMemoization);
|
|
16264
15447
|
const isReader = memoizedInput === void 0 || typeof memoizedInput === "string";
|
|
16265
|
-
const storeUniforms =
|
|
16266
|
-
const
|
|
16267
|
-
|
|
16268
|
-
|
|
16269
|
-
|
|
16270
|
-
|
|
16271
|
-
|
|
16272
|
-
|
|
16273
|
-
|
|
16274
|
-
|
|
16275
|
-
|
|
16276
|
-
|
|
16277
|
-
|
|
16278
|
-
|
|
16279
|
-
|
|
16280
|
-
|
|
16281
|
-
|
|
16282
|
-
|
|
16283
|
-
|
|
16284
|
-
|
|
16285
|
-
|
|
16286
|
-
|
|
16287
|
-
if (scope) {
|
|
16288
|
-
if (!state.uniforms[scope]) state.uniforms[scope] = {};
|
|
16289
|
-
targetRecord = state.uniforms[scope];
|
|
16290
|
-
}
|
|
16291
|
-
for (const [name, node] of Object.entries(created)) {
|
|
16292
|
-
if (targetRecord[name]) {
|
|
16293
|
-
result[name] = targetRecord[name];
|
|
16294
|
-
const existingVal = result[name].value;
|
|
16295
|
-
const newVal = vectorize(node);
|
|
16296
|
-
let equals = newVal === existingVal;
|
|
16297
|
-
if (!equals && hasEqualsMethod(existingVal) && hasEqualsMethod(newVal)) {
|
|
16298
|
-
if (isThreeVector(existingVal) && isThreeVector(newVal)) {
|
|
16299
|
-
equals = vectorEquals(existingVal, newVal);
|
|
16300
|
-
} else if (isSameThreeType(existingVal, newVal)) {
|
|
16301
|
-
equals = existingVal.equals(newVal);
|
|
16302
|
-
}
|
|
15448
|
+
const storeUniforms = usePrimaryThree((s) => s.uniforms);
|
|
15449
|
+
const created = useScopedResource({
|
|
15450
|
+
store,
|
|
15451
|
+
kind: "uniforms",
|
|
15452
|
+
scope: isReader ? void 0 : scope,
|
|
15453
|
+
isLeaf: isUniformNode$1,
|
|
15454
|
+
input: memoizedInput,
|
|
15455
|
+
create: () => {
|
|
15456
|
+
if (isReader) return {};
|
|
15457
|
+
if (typeof memoizedInput !== "object" || memoizedInput === null) throw new Error("Invalid uniform input");
|
|
15458
|
+
return memoizedInput;
|
|
15459
|
+
},
|
|
15460
|
+
prepare: (name, candidate) => createUniform(name, candidate, scope),
|
|
15461
|
+
reconcile: (existing, candidate) => {
|
|
15462
|
+
const existingVal = existing.value;
|
|
15463
|
+
const newVal = vectorize(candidate);
|
|
15464
|
+
let equals = newVal === existingVal;
|
|
15465
|
+
if (!equals && hasEqualsMethod(existingVal) && hasEqualsMethod(newVal)) {
|
|
15466
|
+
if (isThreeVector(existingVal) && isThreeVector(newVal)) {
|
|
15467
|
+
equals = vectorEquals(existingVal, newVal);
|
|
15468
|
+
} else if (isSameThreeType(existingVal, newVal)) {
|
|
15469
|
+
equals = existingVal.equals(newVal);
|
|
16303
15470
|
}
|
|
16304
|
-
if (!equals) result[name].value = newVal;
|
|
16305
|
-
} else {
|
|
16306
|
-
result[name] = createUniform(name, node, scope);
|
|
16307
|
-
hasNewUniforms = true;
|
|
16308
|
-
}
|
|
16309
|
-
}
|
|
16310
|
-
if (hasNewUniforms) {
|
|
16311
|
-
if (scope) {
|
|
16312
|
-
set((s) => ({
|
|
16313
|
-
uniforms: {
|
|
16314
|
-
...s.uniforms,
|
|
16315
|
-
[scope]: { ...s.uniforms[scope], ...result }
|
|
16316
|
-
}
|
|
16317
|
-
}));
|
|
16318
|
-
} else {
|
|
16319
|
-
set((s) => ({ uniforms: { ...s.uniforms, ...result } }));
|
|
16320
15471
|
}
|
|
15472
|
+
if (!equals) existing.value = newVal;
|
|
16321
15473
|
}
|
|
16322
|
-
|
|
16323
|
-
|
|
15474
|
+
});
|
|
15475
|
+
let uniforms = created;
|
|
15476
|
+
if (memoizedInput === void 0) {
|
|
15477
|
+
uniforms = storeUniforms;
|
|
15478
|
+
} else if (typeof memoizedInput === "string") {
|
|
15479
|
+
const scopeData = storeUniforms[memoizedInput];
|
|
15480
|
+
uniforms = scopeData && !isUniformNode$1(scopeData) ? scopeData : {};
|
|
15481
|
+
}
|
|
16324
15482
|
return { ...uniforms, removeUniforms: removeUniforms2, clearUniforms, rebuildUniforms };
|
|
16325
15483
|
}
|
|
16326
15484
|
function rebuildAllUniforms(store, scope) {
|
|
16327
|
-
store
|
|
16328
|
-
let newUniforms = {};
|
|
16329
|
-
if (scope && scope !== "root") {
|
|
16330
|
-
const { [scope]: _, ...rest } = state.uniforms;
|
|
16331
|
-
newUniforms = rest;
|
|
16332
|
-
} else if (scope === "root") {
|
|
16333
|
-
for (const [key, value] of Object.entries(state.uniforms)) {
|
|
16334
|
-
if (!isUniformNode$1(value)) newUniforms[key] = value;
|
|
16335
|
-
}
|
|
16336
|
-
}
|
|
16337
|
-
return { uniforms: newUniforms, _hmrVersion: state._hmrVersion + 1 };
|
|
16338
|
-
});
|
|
15485
|
+
rebuildResource(store, "uniforms", scope);
|
|
16339
15486
|
}
|
|
16340
15487
|
function removeUniforms(set, names, scope) {
|
|
16341
15488
|
set((state) => {
|
|
@@ -16399,191 +15546,91 @@ function isSameThreeType(a, b) {
|
|
|
16399
15546
|
return obj_a.isColor && obj_b.isColor || obj_a.isMatrix3 && obj_b.isMatrix3 || obj_a.isMatrix4 && obj_b.isMatrix4 || obj_a.isEuler && obj_b.isEuler || obj_a.isQuaternion && obj_b.isQuaternion;
|
|
16400
15547
|
}
|
|
16401
15548
|
|
|
15549
|
+
const uniformOf = uniform;
|
|
16402
15550
|
const isUniformNode = (value) => value !== null && typeof value === "object" && "value" in value && "uuid" in value;
|
|
16403
15551
|
const isTSLNode$1 = (value) => value !== null && typeof value === "object" && "uuid" in value && "nodeType" in value;
|
|
16404
15552
|
function useUniform(name, value) {
|
|
16405
15553
|
const store = useStore();
|
|
16406
|
-
const
|
|
16407
|
-
|
|
16408
|
-
|
|
16409
|
-
|
|
16410
|
-
|
|
16411
|
-
|
|
15554
|
+
const registered = useScopedResource({
|
|
15555
|
+
store,
|
|
15556
|
+
kind: "uniforms",
|
|
15557
|
+
scope: void 0,
|
|
15558
|
+
isLeaf: isUniformNode,
|
|
15559
|
+
input: name,
|
|
15560
|
+
create: () => value === void 0 ? {} : { [name]: value },
|
|
15561
|
+
prepare: createNamedUniform,
|
|
15562
|
+
reconcile: (existing2) => {
|
|
16412
15563
|
if (value !== void 0 && !isTSLNode$1(value) && !isUniformNode(value)) {
|
|
16413
|
-
|
|
15564
|
+
existing2.value = typeof value === "string" ? new Color(value) : value;
|
|
16414
15565
|
}
|
|
16415
|
-
return existing;
|
|
16416
|
-
}
|
|
16417
|
-
if (value === void 0) {
|
|
16418
|
-
throw new Error(
|
|
16419
|
-
`[useUniform] Uniform "${name}" not found. Create it first with: useUniform('${name}', initialValue)`
|
|
16420
|
-
);
|
|
16421
|
-
}
|
|
16422
|
-
if (isUniformNode(value)) {
|
|
16423
|
-
const node2 = value;
|
|
16424
|
-
if (typeof node2.setName === "function") {
|
|
16425
|
-
node2.setName(name);
|
|
16426
|
-
}
|
|
16427
|
-
set((s) => ({
|
|
16428
|
-
uniforms: { ...s.uniforms, [name]: node2 }
|
|
16429
|
-
}));
|
|
16430
|
-
return node2;
|
|
16431
|
-
}
|
|
16432
|
-
let node;
|
|
16433
|
-
if (isTSLNode$1(value)) {
|
|
16434
|
-
node = uniform(value);
|
|
16435
|
-
} else if (typeof value === "string") {
|
|
16436
|
-
node = uniform(new Color(value));
|
|
16437
|
-
} else {
|
|
16438
|
-
node = uniform(value);
|
|
16439
|
-
}
|
|
16440
|
-
if (typeof node.setName === "function") {
|
|
16441
|
-
node.setName(name);
|
|
16442
15566
|
}
|
|
16443
|
-
|
|
16444
|
-
|
|
16445
|
-
|
|
16446
|
-
|
|
16447
|
-
|
|
16448
|
-
})
|
|
16449
|
-
|
|
16450
|
-
|
|
15567
|
+
});
|
|
15568
|
+
if (registered[name]) return registered[name];
|
|
15569
|
+
const existing = peekStaged(store, "uniforms", ROOT_SCOPE)?.[name] ?? store.getState().uniforms[name];
|
|
15570
|
+
if (existing && isUniformNode(existing)) return existing;
|
|
15571
|
+
throw new Error(
|
|
15572
|
+
`[useUniform] Uniform "${name}" not found. Create it first with: useUniform('${name}', initialValue)`
|
|
15573
|
+
);
|
|
15574
|
+
}
|
|
15575
|
+
function createNamedUniform(name, value) {
|
|
15576
|
+
let node;
|
|
15577
|
+
if (isUniformNode(value)) {
|
|
15578
|
+
node = value;
|
|
15579
|
+
} else if (isTSLNode$1(value)) {
|
|
15580
|
+
node = uniformOf(value);
|
|
15581
|
+
} else if (typeof value === "string") {
|
|
15582
|
+
node = uniform(new Color(value));
|
|
15583
|
+
} else {
|
|
15584
|
+
node = uniformOf(value);
|
|
15585
|
+
}
|
|
15586
|
+
if (typeof node.setName === "function") {
|
|
15587
|
+
node.setName(name);
|
|
15588
|
+
}
|
|
15589
|
+
return node;
|
|
16451
15590
|
}
|
|
16452
15591
|
|
|
16453
15592
|
const isTSLNode = (value) => value !== null && typeof value === "object" && ("uuid" in value || "nodeType" in value);
|
|
16454
15593
|
function useNodes(creatorOrScope, scope) {
|
|
16455
|
-
const store =
|
|
15594
|
+
const store = usePrimaryStore();
|
|
16456
15595
|
const removeNodes2 = useCallback(
|
|
16457
|
-
(names, targetScope) =>
|
|
16458
|
-
const nameArray = Array.isArray(names) ? names : [names];
|
|
16459
|
-
store.setState((state) => {
|
|
16460
|
-
if (targetScope) {
|
|
16461
|
-
const currentScope = { ...state.nodes[targetScope] };
|
|
16462
|
-
for (const name of nameArray) delete currentScope[name];
|
|
16463
|
-
return { nodes: { ...state.nodes, [targetScope]: currentScope } };
|
|
16464
|
-
}
|
|
16465
|
-
const nodes2 = { ...state.nodes };
|
|
16466
|
-
for (const name of nameArray) if (isTSLNode(nodes2[name])) delete nodes2[name];
|
|
16467
|
-
return { nodes: nodes2 };
|
|
16468
|
-
});
|
|
16469
|
-
},
|
|
15596
|
+
(names, targetScope) => removeResourceEntries(store, "nodes", Array.isArray(names) ? names : [names], targetScope, isTSLNode),
|
|
16470
15597
|
[store]
|
|
16471
15598
|
);
|
|
16472
15599
|
const clearNodes = useCallback(
|
|
16473
|
-
(targetScope) =>
|
|
16474
|
-
store.setState((state) => {
|
|
16475
|
-
if (targetScope && targetScope !== "root") {
|
|
16476
|
-
const { [targetScope]: _, ...rest } = state.nodes;
|
|
16477
|
-
return { nodes: rest };
|
|
16478
|
-
}
|
|
16479
|
-
if (targetScope === "root") {
|
|
16480
|
-
const nodes2 = {};
|
|
16481
|
-
for (const [key, value] of Object.entries(state.nodes)) {
|
|
16482
|
-
if (!isTSLNode(value)) nodes2[key] = value;
|
|
16483
|
-
}
|
|
16484
|
-
return { nodes: nodes2 };
|
|
16485
|
-
}
|
|
16486
|
-
return { nodes: {} };
|
|
16487
|
-
});
|
|
16488
|
-
},
|
|
15600
|
+
(targetScope) => clearResourceEntries(store, "nodes", targetScope, isTSLNode),
|
|
16489
15601
|
[store]
|
|
16490
15602
|
);
|
|
16491
15603
|
const rebuildNodes = useCallback(
|
|
16492
|
-
(targetScope) =>
|
|
16493
|
-
store.setState((state) => {
|
|
16494
|
-
let newNodes = state.nodes;
|
|
16495
|
-
if (targetScope && targetScope !== "root") {
|
|
16496
|
-
const { [targetScope]: _, ...rest } = state.nodes;
|
|
16497
|
-
newNodes = rest;
|
|
16498
|
-
} else if (targetScope === "root") {
|
|
16499
|
-
newNodes = {};
|
|
16500
|
-
for (const [key, value] of Object.entries(state.nodes)) {
|
|
16501
|
-
if (!isTSLNode(value)) newNodes[key] = value;
|
|
16502
|
-
}
|
|
16503
|
-
} else {
|
|
16504
|
-
newNodes = {};
|
|
16505
|
-
}
|
|
16506
|
-
return { nodes: newNodes, _hmrVersion: state._hmrVersion + 1 };
|
|
16507
|
-
});
|
|
16508
|
-
},
|
|
15604
|
+
(targetScope) => rebuildResource(store, "nodes", targetScope),
|
|
16509
15605
|
[store]
|
|
16510
15606
|
);
|
|
16511
15607
|
const isReader = creatorOrScope === void 0 || typeof creatorOrScope === "string";
|
|
16512
|
-
const storeNodes =
|
|
16513
|
-
const
|
|
16514
|
-
|
|
16515
|
-
|
|
16516
|
-
|
|
16517
|
-
|
|
16518
|
-
|
|
16519
|
-
return
|
|
16520
|
-
|
|
16521
|
-
|
|
16522
|
-
|
|
16523
|
-
|
|
16524
|
-
return
|
|
16525
|
-
}
|
|
16526
|
-
const state = store.getState();
|
|
16527
|
-
const set = store.setState;
|
|
16528
|
-
const creator = creatorOrScope;
|
|
16529
|
-
const wrappedState = createLazyCreatorState(state);
|
|
16530
|
-
const created = creator(wrappedState);
|
|
16531
|
-
const result = {};
|
|
16532
|
-
let hasNewNodes = false;
|
|
16533
|
-
if (scope) {
|
|
16534
|
-
const currentScope = state.nodes[scope] ?? {};
|
|
16535
|
-
for (const [name, node] of Object.entries(created)) {
|
|
16536
|
-
if (currentScope[name]) {
|
|
16537
|
-
result[name] = currentScope[name];
|
|
16538
|
-
} else {
|
|
16539
|
-
node.setName?.(`${scope}.${name}`);
|
|
16540
|
-
result[name] = node;
|
|
16541
|
-
hasNewNodes = true;
|
|
16542
|
-
}
|
|
16543
|
-
}
|
|
16544
|
-
if (hasNewNodes) {
|
|
16545
|
-
set((s) => ({
|
|
16546
|
-
nodes: {
|
|
16547
|
-
...s.nodes,
|
|
16548
|
-
[scope]: { ...s.nodes[scope], ...result }
|
|
16549
|
-
}
|
|
16550
|
-
}));
|
|
16551
|
-
}
|
|
16552
|
-
return result;
|
|
16553
|
-
}
|
|
16554
|
-
for (const [name, node] of Object.entries(created)) {
|
|
16555
|
-
const existing = state.nodes[name];
|
|
16556
|
-
if (existing && isTSLNode(existing)) {
|
|
16557
|
-
result[name] = existing;
|
|
16558
|
-
} else {
|
|
16559
|
-
node.setName?.(name);
|
|
16560
|
-
result[name] = node;
|
|
16561
|
-
hasNewNodes = true;
|
|
16562
|
-
}
|
|
16563
|
-
}
|
|
16564
|
-
if (hasNewNodes) {
|
|
16565
|
-
set((s) => ({ nodes: { ...s.nodes, ...result } }));
|
|
15608
|
+
const storeNodes = usePrimaryThree((s) => s.nodes);
|
|
15609
|
+
const created = useScopedResource({
|
|
15610
|
+
store,
|
|
15611
|
+
kind: "nodes",
|
|
15612
|
+
scope: isReader ? void 0 : scope,
|
|
15613
|
+
isLeaf: isTSLNode,
|
|
15614
|
+
create: () => {
|
|
15615
|
+
if (isReader) return {};
|
|
15616
|
+
return creatorOrScope(createLazyCreatorState(store.getState(), store));
|
|
15617
|
+
},
|
|
15618
|
+
prepare: (name, node) => {
|
|
15619
|
+
node.setName?.(scope ? `${scope}.${name}` : name);
|
|
15620
|
+
return node;
|
|
16566
15621
|
}
|
|
16567
|
-
|
|
16568
|
-
|
|
15622
|
+
});
|
|
15623
|
+
let nodes = created;
|
|
15624
|
+
if (creatorOrScope === void 0) {
|
|
15625
|
+
nodes = storeNodes;
|
|
15626
|
+
} else if (typeof creatorOrScope === "string") {
|
|
15627
|
+
const scopeData = storeNodes[creatorOrScope];
|
|
15628
|
+
nodes = scopeData && !isTSLNode(scopeData) ? scopeData : {};
|
|
15629
|
+
}
|
|
16569
15630
|
return { ...nodes, removeNodes: removeNodes2, clearNodes, rebuildNodes };
|
|
16570
15631
|
}
|
|
16571
15632
|
function rebuildAllNodes(store, scope) {
|
|
16572
|
-
store
|
|
16573
|
-
let newNodes = state.nodes;
|
|
16574
|
-
if (scope && scope !== "root") {
|
|
16575
|
-
const { [scope]: _, ...rest } = state.nodes;
|
|
16576
|
-
newNodes = rest;
|
|
16577
|
-
} else if (scope === "root") {
|
|
16578
|
-
newNodes = {};
|
|
16579
|
-
for (const [key, value] of Object.entries(state.nodes)) {
|
|
16580
|
-
if (!isTSLNode(value)) newNodes[key] = value;
|
|
16581
|
-
}
|
|
16582
|
-
} else {
|
|
16583
|
-
newNodes = {};
|
|
16584
|
-
}
|
|
16585
|
-
return { nodes: newNodes, _hmrVersion: state._hmrVersion + 1 };
|
|
16586
|
-
});
|
|
15633
|
+
rebuildResource(store, "nodes", scope);
|
|
16587
15634
|
}
|
|
16588
15635
|
function removeNodes(set, names, scope) {
|
|
16589
15636
|
set((state) => {
|
|
@@ -16613,13 +15660,13 @@ function clearRootNodes(set) {
|
|
|
16613
15660
|
});
|
|
16614
15661
|
}
|
|
16615
15662
|
function useLocalNodes(creator) {
|
|
16616
|
-
const store =
|
|
16617
|
-
const uniforms =
|
|
16618
|
-
const nodes =
|
|
16619
|
-
const textures =
|
|
16620
|
-
const hmrVersion =
|
|
15663
|
+
const store = usePrimaryStore();
|
|
15664
|
+
const uniforms = usePrimaryThree((s) => s.uniforms);
|
|
15665
|
+
const nodes = usePrimaryThree((s) => s.nodes);
|
|
15666
|
+
const textures = usePrimaryThree((s) => s.textures);
|
|
15667
|
+
const hmrVersion = usePrimaryThree((s) => s._hmrVersion);
|
|
16621
15668
|
return useMemo(() => {
|
|
16622
|
-
const wrappedState = createLazyCreatorState(store.getState());
|
|
15669
|
+
const wrappedState = createLazyCreatorState(store.getState(), store);
|
|
16623
15670
|
return creator(wrappedState);
|
|
16624
15671
|
}, [store, creator, uniforms, nodes, textures, hmrVersion]);
|
|
16625
15672
|
}
|
|
@@ -16638,60 +15685,17 @@ const disposeBuffer = (buffer) => {
|
|
|
16638
15685
|
}
|
|
16639
15686
|
};
|
|
16640
15687
|
function useBuffers(creatorOrScope, scope) {
|
|
16641
|
-
const store =
|
|
15688
|
+
const store = usePrimaryStore();
|
|
16642
15689
|
const removeBuffers = useCallback(
|
|
16643
|
-
(names, targetScope) =>
|
|
16644
|
-
const nameArray = Array.isArray(names) ? names : [names];
|
|
16645
|
-
store.setState((state) => {
|
|
16646
|
-
if (targetScope) {
|
|
16647
|
-
const currentScope = { ...state.buffers[targetScope] };
|
|
16648
|
-
for (const name of nameArray) delete currentScope[name];
|
|
16649
|
-
return { buffers: { ...state.buffers, [targetScope]: currentScope } };
|
|
16650
|
-
}
|
|
16651
|
-
const buffers2 = { ...state.buffers };
|
|
16652
|
-
for (const name of nameArray) if (isBufferLike(buffers2[name])) delete buffers2[name];
|
|
16653
|
-
return { buffers: buffers2 };
|
|
16654
|
-
});
|
|
16655
|
-
},
|
|
15690
|
+
(names, targetScope) => removeResourceEntries(store, "buffers", Array.isArray(names) ? names : [names], targetScope, isBufferLike),
|
|
16656
15691
|
[store]
|
|
16657
15692
|
);
|
|
16658
15693
|
const clearBuffers = useCallback(
|
|
16659
|
-
(targetScope) =>
|
|
16660
|
-
store.setState((state) => {
|
|
16661
|
-
if (targetScope && targetScope !== "root") {
|
|
16662
|
-
const { [targetScope]: _, ...rest } = state.buffers;
|
|
16663
|
-
return { buffers: rest };
|
|
16664
|
-
}
|
|
16665
|
-
if (targetScope === "root") {
|
|
16666
|
-
const buffers2 = {};
|
|
16667
|
-
for (const [key, value] of Object.entries(state.buffers)) {
|
|
16668
|
-
if (!isBufferLike(value)) buffers2[key] = value;
|
|
16669
|
-
}
|
|
16670
|
-
return { buffers: buffers2 };
|
|
16671
|
-
}
|
|
16672
|
-
return { buffers: {} };
|
|
16673
|
-
});
|
|
16674
|
-
},
|
|
15694
|
+
(targetScope) => clearResourceEntries(store, "buffers", targetScope, isBufferLike),
|
|
16675
15695
|
[store]
|
|
16676
15696
|
);
|
|
16677
15697
|
const rebuildBuffers = useCallback(
|
|
16678
|
-
(targetScope) =>
|
|
16679
|
-
store.setState((state) => {
|
|
16680
|
-
let newBuffers = state.buffers;
|
|
16681
|
-
if (targetScope && targetScope !== "root") {
|
|
16682
|
-
const { [targetScope]: _, ...rest } = state.buffers;
|
|
16683
|
-
newBuffers = rest;
|
|
16684
|
-
} else if (targetScope === "root") {
|
|
16685
|
-
newBuffers = {};
|
|
16686
|
-
for (const [key, value] of Object.entries(state.buffers)) {
|
|
16687
|
-
if (!isBufferLike(value)) newBuffers[key] = value;
|
|
16688
|
-
}
|
|
16689
|
-
} else {
|
|
16690
|
-
newBuffers = {};
|
|
16691
|
-
}
|
|
16692
|
-
return { buffers: newBuffers, _hmrVersion: state._hmrVersion + 1 };
|
|
16693
|
-
});
|
|
16694
|
-
},
|
|
15698
|
+
(targetScope) => rebuildResource(store, "buffers", targetScope),
|
|
16695
15699
|
[store]
|
|
16696
15700
|
);
|
|
16697
15701
|
const disposeBuffers = useCallback(
|
|
@@ -16709,85 +15713,34 @@ function useBuffers(creatorOrScope, scope) {
|
|
|
16709
15713
|
[store, removeBuffers]
|
|
16710
15714
|
);
|
|
16711
15715
|
const isReader = creatorOrScope === void 0 || typeof creatorOrScope === "string";
|
|
16712
|
-
const storeBuffers =
|
|
16713
|
-
const
|
|
16714
|
-
|
|
16715
|
-
|
|
16716
|
-
|
|
16717
|
-
|
|
16718
|
-
|
|
16719
|
-
return
|
|
16720
|
-
|
|
16721
|
-
|
|
16722
|
-
|
|
16723
|
-
if (
|
|
16724
|
-
|
|
16725
|
-
}
|
|
16726
|
-
const state = store.getState();
|
|
16727
|
-
const set = store.setState;
|
|
16728
|
-
const creator = creatorOrScope;
|
|
16729
|
-
const wrappedState = createLazyCreatorState(state);
|
|
16730
|
-
const created = creator(wrappedState);
|
|
16731
|
-
const result = {};
|
|
16732
|
-
let hasNewBuffers = false;
|
|
16733
|
-
if (scope) {
|
|
16734
|
-
const currentScope = state.buffers[scope] ?? {};
|
|
16735
|
-
for (const [name, buffer] of Object.entries(created)) {
|
|
16736
|
-
if (currentScope[name]) {
|
|
16737
|
-
result[name] = currentScope[name];
|
|
16738
|
-
} else {
|
|
16739
|
-
if ("setName" in buffer && typeof buffer.setName === "function") {
|
|
16740
|
-
buffer.setName(`${scope}.${name}`);
|
|
16741
|
-
}
|
|
16742
|
-
result[name] = buffer;
|
|
16743
|
-
hasNewBuffers = true;
|
|
16744
|
-
}
|
|
16745
|
-
}
|
|
16746
|
-
if (hasNewBuffers) {
|
|
16747
|
-
set((s) => ({
|
|
16748
|
-
buffers: {
|
|
16749
|
-
...s.buffers,
|
|
16750
|
-
[scope]: { ...s.buffers[scope], ...result }
|
|
16751
|
-
}
|
|
16752
|
-
}));
|
|
16753
|
-
}
|
|
16754
|
-
return result;
|
|
16755
|
-
}
|
|
16756
|
-
for (const [name, buffer] of Object.entries(created)) {
|
|
16757
|
-
const existing = state.buffers[name];
|
|
16758
|
-
if (existing && isBufferLike(existing)) {
|
|
16759
|
-
result[name] = existing;
|
|
16760
|
-
} else {
|
|
16761
|
-
if ("setName" in buffer && typeof buffer.setName === "function") {
|
|
16762
|
-
buffer.setName(name);
|
|
16763
|
-
}
|
|
16764
|
-
result[name] = buffer;
|
|
16765
|
-
hasNewBuffers = true;
|
|
15716
|
+
const storeBuffers = usePrimaryThree((s) => s.buffers);
|
|
15717
|
+
const created = useScopedResource({
|
|
15718
|
+
store,
|
|
15719
|
+
kind: "buffers",
|
|
15720
|
+
scope: isReader ? void 0 : scope,
|
|
15721
|
+
isLeaf: isBufferLike,
|
|
15722
|
+
create: () => {
|
|
15723
|
+
if (isReader) return {};
|
|
15724
|
+
return creatorOrScope(createLazyCreatorState(store.getState(), store));
|
|
15725
|
+
},
|
|
15726
|
+
prepare: (name, buffer) => {
|
|
15727
|
+
if ("setName" in buffer && typeof buffer.setName === "function") {
|
|
15728
|
+
buffer.setName(scope ? `${scope}.${name}` : name);
|
|
16766
15729
|
}
|
|
15730
|
+
return buffer;
|
|
16767
15731
|
}
|
|
16768
|
-
|
|
16769
|
-
|
|
16770
|
-
|
|
16771
|
-
|
|
16772
|
-
}
|
|
15732
|
+
});
|
|
15733
|
+
let buffers = created;
|
|
15734
|
+
if (creatorOrScope === void 0) {
|
|
15735
|
+
buffers = storeBuffers;
|
|
15736
|
+
} else if (typeof creatorOrScope === "string") {
|
|
15737
|
+
const scopeData = storeBuffers[creatorOrScope];
|
|
15738
|
+
buffers = scopeData && !isBufferLike(scopeData) ? scopeData : {};
|
|
15739
|
+
}
|
|
16773
15740
|
return { ...buffers, removeBuffers, clearBuffers, rebuildBuffers, disposeBuffers };
|
|
16774
15741
|
}
|
|
16775
15742
|
function rebuildAllBuffers(store, scope) {
|
|
16776
|
-
store
|
|
16777
|
-
let newBuffers = state.buffers;
|
|
16778
|
-
if (scope && scope !== "root") {
|
|
16779
|
-
const { [scope]: _, ...rest } = state.buffers;
|
|
16780
|
-
newBuffers = rest;
|
|
16781
|
-
} else if (scope === "root") {
|
|
16782
|
-
newBuffers = {};
|
|
16783
|
-
for (const [key, value] of Object.entries(state.buffers)) {
|
|
16784
|
-
if (!isBufferLike(value)) newBuffers[key] = value;
|
|
16785
|
-
}
|
|
16786
|
-
} else {
|
|
16787
|
-
newBuffers = {};
|
|
16788
|
-
}
|
|
16789
|
-
return { buffers: newBuffers, _hmrVersion: state._hmrVersion + 1 };
|
|
16790
|
-
});
|
|
15743
|
+
rebuildResource(store, "buffers", scope);
|
|
16791
15744
|
}
|
|
16792
15745
|
|
|
16793
15746
|
const isStorageLike = (value) => {
|
|
@@ -16804,60 +15757,17 @@ const disposeStorage = (storage) => {
|
|
|
16804
15757
|
}
|
|
16805
15758
|
};
|
|
16806
15759
|
function useGPUStorage(creatorOrScope, scope) {
|
|
16807
|
-
const store =
|
|
15760
|
+
const store = usePrimaryStore();
|
|
16808
15761
|
const removeStorage = useCallback(
|
|
16809
|
-
(names, targetScope) =>
|
|
16810
|
-
const nameArray = Array.isArray(names) ? names : [names];
|
|
16811
|
-
store.setState((state) => {
|
|
16812
|
-
if (targetScope) {
|
|
16813
|
-
const currentScope = { ...state.gpuStorage[targetScope] };
|
|
16814
|
-
for (const name of nameArray) delete currentScope[name];
|
|
16815
|
-
return { gpuStorage: { ...state.gpuStorage, [targetScope]: currentScope } };
|
|
16816
|
-
}
|
|
16817
|
-
const gpuStorage2 = { ...state.gpuStorage };
|
|
16818
|
-
for (const name of nameArray) if (isStorageLike(gpuStorage2[name])) delete gpuStorage2[name];
|
|
16819
|
-
return { gpuStorage: gpuStorage2 };
|
|
16820
|
-
});
|
|
16821
|
-
},
|
|
15762
|
+
(names, targetScope) => removeResourceEntries(store, "gpuStorage", Array.isArray(names) ? names : [names], targetScope, isStorageLike),
|
|
16822
15763
|
[store]
|
|
16823
15764
|
);
|
|
16824
15765
|
const clearStorage = useCallback(
|
|
16825
|
-
(targetScope) =>
|
|
16826
|
-
store.setState((state) => {
|
|
16827
|
-
if (targetScope && targetScope !== "root") {
|
|
16828
|
-
const { [targetScope]: _, ...rest } = state.gpuStorage;
|
|
16829
|
-
return { gpuStorage: rest };
|
|
16830
|
-
}
|
|
16831
|
-
if (targetScope === "root") {
|
|
16832
|
-
const gpuStorage2 = {};
|
|
16833
|
-
for (const [key, value] of Object.entries(state.gpuStorage)) {
|
|
16834
|
-
if (!isStorageLike(value)) gpuStorage2[key] = value;
|
|
16835
|
-
}
|
|
16836
|
-
return { gpuStorage: gpuStorage2 };
|
|
16837
|
-
}
|
|
16838
|
-
return { gpuStorage: {} };
|
|
16839
|
-
});
|
|
16840
|
-
},
|
|
15766
|
+
(targetScope) => clearResourceEntries(store, "gpuStorage", targetScope, isStorageLike),
|
|
16841
15767
|
[store]
|
|
16842
15768
|
);
|
|
16843
15769
|
const rebuildStorage = useCallback(
|
|
16844
|
-
(targetScope) =>
|
|
16845
|
-
store.setState((state) => {
|
|
16846
|
-
let newStorage = state.gpuStorage;
|
|
16847
|
-
if (targetScope && targetScope !== "root") {
|
|
16848
|
-
const { [targetScope]: _, ...rest } = state.gpuStorage;
|
|
16849
|
-
newStorage = rest;
|
|
16850
|
-
} else if (targetScope === "root") {
|
|
16851
|
-
newStorage = {};
|
|
16852
|
-
for (const [key, value] of Object.entries(state.gpuStorage)) {
|
|
16853
|
-
if (!isStorageLike(value)) newStorage[key] = value;
|
|
16854
|
-
}
|
|
16855
|
-
} else {
|
|
16856
|
-
newStorage = {};
|
|
16857
|
-
}
|
|
16858
|
-
return { gpuStorage: newStorage, _hmrVersion: state._hmrVersion + 1 };
|
|
16859
|
-
});
|
|
16860
|
-
},
|
|
15770
|
+
(targetScope) => rebuildResource(store, "gpuStorage", targetScope),
|
|
16861
15771
|
[store]
|
|
16862
15772
|
);
|
|
16863
15773
|
const disposeStorageFn = useCallback(
|
|
@@ -16875,73 +15785,34 @@ function useGPUStorage(creatorOrScope, scope) {
|
|
|
16875
15785
|
[store, removeStorage]
|
|
16876
15786
|
);
|
|
16877
15787
|
const isReader = creatorOrScope === void 0 || typeof creatorOrScope === "string";
|
|
16878
|
-
const storeStorage =
|
|
16879
|
-
const
|
|
16880
|
-
|
|
16881
|
-
|
|
16882
|
-
|
|
16883
|
-
|
|
16884
|
-
|
|
16885
|
-
return
|
|
16886
|
-
|
|
16887
|
-
|
|
16888
|
-
|
|
16889
|
-
|
|
16890
|
-
|
|
16891
|
-
|
|
16892
|
-
const state = store.getState();
|
|
16893
|
-
const set = store.setState;
|
|
16894
|
-
const creator = creatorOrScope;
|
|
16895
|
-
const wrappedState = createLazyCreatorState(state);
|
|
16896
|
-
const created = creator(wrappedState);
|
|
16897
|
-
const result = {};
|
|
16898
|
-
let hasNewStorage = false;
|
|
16899
|
-
if (scope) {
|
|
16900
|
-
const currentScope = state.gpuStorage[scope] ?? {};
|
|
16901
|
-
for (const [name, storage] of Object.entries(created)) {
|
|
16902
|
-
if (currentScope[name]) {
|
|
16903
|
-
result[name] = currentScope[name];
|
|
16904
|
-
} else {
|
|
16905
|
-
if ("setName" in storage && typeof storage.setName === "function") {
|
|
16906
|
-
storage.setName(`${scope}.${name}`);
|
|
16907
|
-
}
|
|
16908
|
-
if ("name" in storage && typeof storage.name === "string") {
|
|
16909
|
-
storage.name = `${scope}.${name}`;
|
|
16910
|
-
}
|
|
16911
|
-
result[name] = storage;
|
|
16912
|
-
hasNewStorage = true;
|
|
16913
|
-
}
|
|
16914
|
-
}
|
|
16915
|
-
if (hasNewStorage) {
|
|
16916
|
-
set((s) => ({
|
|
16917
|
-
gpuStorage: {
|
|
16918
|
-
...s.gpuStorage,
|
|
16919
|
-
[scope]: { ...s.gpuStorage[scope], ...result }
|
|
16920
|
-
}
|
|
16921
|
-
}));
|
|
15788
|
+
const storeStorage = usePrimaryThree((s) => s.gpuStorage);
|
|
15789
|
+
const created = useScopedResource({
|
|
15790
|
+
store,
|
|
15791
|
+
kind: "gpuStorage",
|
|
15792
|
+
scope: isReader ? void 0 : scope,
|
|
15793
|
+
isLeaf: isStorageLike,
|
|
15794
|
+
create: () => {
|
|
15795
|
+
if (isReader) return {};
|
|
15796
|
+
return creatorOrScope(createLazyCreatorState(store.getState(), store));
|
|
15797
|
+
},
|
|
15798
|
+
prepare: (name, storage) => {
|
|
15799
|
+
const label = scope ? `${scope}.${name}` : name;
|
|
15800
|
+
if ("setName" in storage && typeof storage.setName === "function") {
|
|
15801
|
+
storage.setName(label);
|
|
16922
15802
|
}
|
|
16923
|
-
|
|
16924
|
-
|
|
16925
|
-
for (const [name, storage] of Object.entries(created)) {
|
|
16926
|
-
const existing = state.gpuStorage[name];
|
|
16927
|
-
if (existing && isStorageLike(existing)) {
|
|
16928
|
-
result[name] = existing;
|
|
16929
|
-
} else {
|
|
16930
|
-
if ("setName" in storage && typeof storage.setName === "function") {
|
|
16931
|
-
storage.setName(name);
|
|
16932
|
-
}
|
|
16933
|
-
if ("name" in storage && typeof storage.name === "string") {
|
|
16934
|
-
storage.name = name;
|
|
16935
|
-
}
|
|
16936
|
-
result[name] = storage;
|
|
16937
|
-
hasNewStorage = true;
|
|
15803
|
+
if ("name" in storage && typeof storage.name === "string") {
|
|
15804
|
+
storage.name = label;
|
|
16938
15805
|
}
|
|
15806
|
+
return storage;
|
|
16939
15807
|
}
|
|
16940
|
-
|
|
16941
|
-
|
|
16942
|
-
|
|
16943
|
-
|
|
16944
|
-
}
|
|
15808
|
+
});
|
|
15809
|
+
let gpuStorage = created;
|
|
15810
|
+
if (creatorOrScope === void 0) {
|
|
15811
|
+
gpuStorage = storeStorage;
|
|
15812
|
+
} else if (typeof creatorOrScope === "string") {
|
|
15813
|
+
const scopeData = storeStorage[creatorOrScope];
|
|
15814
|
+
gpuStorage = scopeData && !isStorageLike(scopeData) ? scopeData : {};
|
|
15815
|
+
}
|
|
16945
15816
|
return {
|
|
16946
15817
|
...gpuStorage,
|
|
16947
15818
|
removeStorage,
|
|
@@ -16951,21 +15822,7 @@ function useGPUStorage(creatorOrScope, scope) {
|
|
|
16951
15822
|
};
|
|
16952
15823
|
}
|
|
16953
15824
|
function rebuildAllStorage(store, scope) {
|
|
16954
|
-
store
|
|
16955
|
-
let newStorage = state.gpuStorage;
|
|
16956
|
-
if (scope && scope !== "root") {
|
|
16957
|
-
const { [scope]: _, ...rest } = state.gpuStorage;
|
|
16958
|
-
newStorage = rest;
|
|
16959
|
-
} else if (scope === "root") {
|
|
16960
|
-
newStorage = {};
|
|
16961
|
-
for (const [key, value] of Object.entries(state.gpuStorage)) {
|
|
16962
|
-
if (!isStorageLike(value)) newStorage[key] = value;
|
|
16963
|
-
}
|
|
16964
|
-
} else {
|
|
16965
|
-
newStorage = {};
|
|
16966
|
-
}
|
|
16967
|
-
return { gpuStorage: newStorage, _hmrVersion: state._hmrVersion + 1 };
|
|
16968
|
-
});
|
|
15825
|
+
rebuildResource(store, "gpuStorage", scope);
|
|
16969
15826
|
}
|
|
16970
15827
|
|
|
16971
15828
|
function useRenderPipeline(mainCB, setupCB) {
|
|
@@ -17006,12 +15863,12 @@ function useRenderPipeline(mainCB, setupCB) {
|
|
|
17006
15863
|
const state = store.getState();
|
|
17007
15864
|
const set = store.setState;
|
|
17008
15865
|
try {
|
|
17009
|
-
let
|
|
15866
|
+
let pipeline = state.renderPipeline;
|
|
17010
15867
|
let currentPasses = { ...state.passes };
|
|
17011
|
-
let
|
|
17012
|
-
if (!
|
|
17013
|
-
|
|
17014
|
-
|
|
15868
|
+
let justCreatedPipeline = false;
|
|
15869
|
+
if (!pipeline) {
|
|
15870
|
+
pipeline = new RenderPipeline(renderer);
|
|
15871
|
+
justCreatedPipeline = true;
|
|
17015
15872
|
}
|
|
17016
15873
|
const cacheValid = scenePassCacheRef.current && scenePassCacheRef.current.sceneUuid === scene.uuid && scenePassCacheRef.current.cameraUuid === camera.uuid;
|
|
17017
15874
|
let scenePass;
|
|
@@ -17022,9 +15879,9 @@ function useRenderPipeline(mainCB, setupCB) {
|
|
|
17022
15879
|
scenePassCacheRef.current = { sceneUuid: scene.uuid, cameraUuid: camera.uuid, scenePass };
|
|
17023
15880
|
}
|
|
17024
15881
|
currentPasses.scenePass = scenePass;
|
|
17025
|
-
if (!
|
|
17026
|
-
set({ renderPipeline:
|
|
17027
|
-
const shouldRunCallbacks =
|
|
15882
|
+
if (!pipeline.outputNode || justCreatedPipeline) pipeline.outputNode = scenePass;
|
|
15883
|
+
set({ renderPipeline: pipeline, passes: currentPasses });
|
|
15884
|
+
const shouldRunCallbacks = justCreatedPipeline || !callbacksRanRef.current || !cacheValid;
|
|
17028
15885
|
if (shouldRunCallbacks) {
|
|
17029
15886
|
if (setupCBRef.current) {
|
|
17030
15887
|
const freshState = store.getState();
|
|
@@ -17045,7 +15902,7 @@ function useRenderPipeline(mainCB, setupCB) {
|
|
|
17045
15902
|
callbacksRanRef.current = true;
|
|
17046
15903
|
}
|
|
17047
15904
|
} catch (error) {
|
|
17048
|
-
console.
|
|
15905
|
+
console.warn("[useRenderPipeline] Setup failed; the render pipeline was not configured:", error);
|
|
17049
15906
|
}
|
|
17050
15907
|
}, [store, renderer, scene, camera, isLegacy, rebuildVersion]);
|
|
17051
15908
|
const passes = useThree((s) => s.passes);
|
|
@@ -17063,4 +15920,4 @@ function useRenderPipeline(mainCB, setupCB) {
|
|
|
17063
15920
|
|
|
17064
15921
|
extend(THREE);
|
|
17065
15922
|
|
|
17066
|
-
export { Block, Canvas, Environment, EnvironmentCube, EnvironmentMap, EnvironmentPortal, ErrorBoundary, FROM_REF, IsObject, ONCE, Portal, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS,
|
|
15923
|
+
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, clearNodeScope, clearRootNodes, clearRootUniforms, clearScope, context, createEvents, createPointerEvents, createPortal, createRoot, createScopedStore, createStore, createTextureOperations, 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, rebuildAllBuffers, rebuildAllNodes, rebuildAllStorage, rebuildAllUniforms, reconciler, registerPrimary, removeInteractivity, removeNodes, removeUniforms, resolve, unmountComponentAtNode, unregisterPrimary, updateCamera, updateFrustum, useBridge, useBuffers, useEnvironment, useFrame, useGPUStorage, useGraph, useInstanceHandle, useIsomorphicLayoutEffect, useLoader, useLocalNodes, useMutableCallback, useNodes, useRenderPipeline, useRenderTarget, useStore, useTexture, useTextures, useThree, useUniform, useUniforms, waitForPrimary };
|