@react-three/fiber 10.0.0-canary.604355a → 10.0.0-canary.6d6e5ab
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 +844 -1423
- package/dist/index.d.cts +291 -811
- package/dist/index.d.mts +291 -811
- package/dist/index.d.ts +291 -811
- package/dist/index.mjs +820 -1397
- package/dist/legacy.cjs +799 -1384
- package/dist/legacy.d.cts +292 -811
- package/dist/legacy.d.mts +292 -811
- package/dist/legacy.d.ts +292 -811
- package/dist/legacy.mjs +774 -1357
- package/dist/webgpu/index.cjs +1300 -1738
- package/dist/webgpu/index.d.cts +407 -825
- package/dist/webgpu/index.d.mts +407 -825
- package/dist/webgpu/index.d.ts +407 -825
- package/dist/webgpu/index.mjs +1270 -1710
- package/package.json +4 -3
- package/readme.md +2 -3
package/dist/index.cjs
CHANGED
|
@@ -15,6 +15,7 @@ const UltraHDRLoader_js = require('three/examples/jsm/loaders/UltraHDRLoader.js'
|
|
|
15
15
|
const gainmapJs = require('@monogrid/gainmap-js');
|
|
16
16
|
const Tb = require('scheduler');
|
|
17
17
|
const traditional = require('zustand/traditional');
|
|
18
|
+
const scheduler = require('@pmndrs/scheduler');
|
|
18
19
|
const suspendReact = require('suspend-react');
|
|
19
20
|
|
|
20
21
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
@@ -55,10 +56,13 @@ const R3F_BUILD_WEBGPU = true;
|
|
|
55
56
|
|
|
56
57
|
const THREE = /*#__PURE__*/_mergeNamespaces({
|
|
57
58
|
__proto__: null,
|
|
59
|
+
CubeRenderTarget: webgpu.CubeRenderTarget,
|
|
60
|
+
CubeRenderTargetCompat: webgpu.CubeRenderTarget,
|
|
58
61
|
Inspector: Inspector_js.Inspector,
|
|
59
62
|
R3F_BUILD_LEGACY: R3F_BUILD_LEGACY,
|
|
60
63
|
R3F_BUILD_WEBGPU: R3F_BUILD_WEBGPU,
|
|
61
64
|
RenderTargetCompat: webgpu.RenderTarget,
|
|
65
|
+
WebGLCubeRenderTarget: three.WebGLCubeRenderTarget,
|
|
62
66
|
WebGLRenderTarget: three.WebGLRenderTarget,
|
|
63
67
|
WebGLRenderer: three.WebGLRenderer
|
|
64
68
|
}, [webgpu__namespace]);
|
|
@@ -160,7 +164,7 @@ function useEnvironment({
|
|
|
160
164
|
fiber.useLoader.clear(loader, multiFile ? [files] : files);
|
|
161
165
|
}
|
|
162
166
|
renderer.domElement.addEventListener("webglcontextlost", clearGainmapTexture, { once: true });
|
|
163
|
-
}, [files, renderer.domElement]);
|
|
167
|
+
}, [extension, files, loader, multiFile, renderer.domElement]);
|
|
164
168
|
const loaderResult = fiber.useLoader(
|
|
165
169
|
loader,
|
|
166
170
|
multiFile ? [files] : files,
|
|
@@ -332,15 +336,16 @@ function EnvironmentPortal({
|
|
|
332
336
|
preset = void 0,
|
|
333
337
|
extensions
|
|
334
338
|
}) {
|
|
335
|
-
const
|
|
339
|
+
const renderer = fiber.useThree((state) => state.renderer);
|
|
340
|
+
const isLegacy = fiber.useThree((state) => state.isLegacy);
|
|
336
341
|
const defaultScene = fiber.useThree((state) => state.scene);
|
|
337
342
|
const camera = React__namespace.useRef(null);
|
|
338
343
|
const [virtualScene] = React__namespace.useState(() => new webgpu.Scene());
|
|
339
344
|
const fbo = React__namespace.useMemo(() => {
|
|
340
|
-
const fbo2 = new
|
|
345
|
+
const fbo2 = isLegacy ? new three.WebGLCubeRenderTarget(resolution) : new webgpu.CubeRenderTarget(resolution);
|
|
341
346
|
fbo2.texture.type = webgpu.HalfFloatType;
|
|
342
347
|
return fbo2;
|
|
343
|
-
}, [resolution]);
|
|
348
|
+
}, [resolution, isLegacy]);
|
|
344
349
|
React__namespace.useEffect(() => {
|
|
345
350
|
return () => {
|
|
346
351
|
fbo.dispose();
|
|
@@ -348,10 +353,10 @@ function EnvironmentPortal({
|
|
|
348
353
|
}, [fbo]);
|
|
349
354
|
React__namespace.useLayoutEffect(() => {
|
|
350
355
|
if (frames === 1) {
|
|
351
|
-
const autoClear =
|
|
352
|
-
|
|
353
|
-
camera.current.update(
|
|
354
|
-
|
|
356
|
+
const autoClear = renderer.autoClear;
|
|
357
|
+
renderer.autoClear = true;
|
|
358
|
+
camera.current.update(renderer, virtualScene);
|
|
359
|
+
renderer.autoClear = autoClear;
|
|
355
360
|
}
|
|
356
361
|
return setEnvProps(background, scene, defaultScene, fbo.texture, {
|
|
357
362
|
backgroundBlurriness: blur ?? backgroundBlurriness,
|
|
@@ -360,14 +365,29 @@ function EnvironmentPortal({
|
|
|
360
365
|
environmentIntensity,
|
|
361
366
|
environmentRotation
|
|
362
367
|
});
|
|
363
|
-
}, [
|
|
368
|
+
}, [
|
|
369
|
+
children,
|
|
370
|
+
virtualScene,
|
|
371
|
+
fbo.texture,
|
|
372
|
+
scene,
|
|
373
|
+
defaultScene,
|
|
374
|
+
background,
|
|
375
|
+
frames,
|
|
376
|
+
renderer,
|
|
377
|
+
blur,
|
|
378
|
+
backgroundBlurriness,
|
|
379
|
+
backgroundIntensity,
|
|
380
|
+
backgroundRotation,
|
|
381
|
+
environmentIntensity,
|
|
382
|
+
environmentRotation
|
|
383
|
+
]);
|
|
364
384
|
let count = 1;
|
|
365
385
|
fiber.useFrame(() => {
|
|
366
386
|
if (frames === Infinity || count < frames) {
|
|
367
|
-
const autoClear =
|
|
368
|
-
|
|
369
|
-
camera.current.update(
|
|
370
|
-
|
|
387
|
+
const autoClear = renderer.autoClear;
|
|
388
|
+
renderer.autoClear = true;
|
|
389
|
+
camera.current.update(renderer, virtualScene);
|
|
390
|
+
renderer.autoClear = autoClear;
|
|
371
391
|
count++;
|
|
372
392
|
}
|
|
373
393
|
});
|
|
@@ -431,10 +451,9 @@ function Environment(props) {
|
|
|
431
451
|
return props.ground ? /* @__PURE__ */ jsxRuntime.jsx(EnvironmentGround, { ...props }) : props.map ? /* @__PURE__ */ jsxRuntime.jsx(EnvironmentMap, { ...props }) : props.children ? /* @__PURE__ */ jsxRuntime.jsx(EnvironmentPortal, { ...props }) : /* @__PURE__ */ jsxRuntime.jsx(EnvironmentCube, { ...props });
|
|
432
452
|
}
|
|
433
453
|
|
|
434
|
-
var __defProp
|
|
435
|
-
var __defNormalProp
|
|
436
|
-
var __publicField
|
|
437
|
-
const act = React__namespace["act"];
|
|
454
|
+
var __defProp = Object.defineProperty;
|
|
455
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
456
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
438
457
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
439
458
|
function useMutableCallback(fn) {
|
|
440
459
|
const ref = React__namespace.useRef(fn);
|
|
@@ -465,7 +484,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
465
484
|
return _a = class extends React__namespace.Component {
|
|
466
485
|
constructor() {
|
|
467
486
|
super(...arguments);
|
|
468
|
-
__publicField
|
|
487
|
+
__publicField(this, "state", { error: false });
|
|
469
488
|
}
|
|
470
489
|
componentDidCatch(err) {
|
|
471
490
|
this.props.set(err);
|
|
@@ -473,7 +492,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
473
492
|
render() {
|
|
474
493
|
return this.state.error ? null : this.props.children;
|
|
475
494
|
}
|
|
476
|
-
}, __publicField
|
|
495
|
+
}, __publicField(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
477
496
|
})();
|
|
478
497
|
|
|
479
498
|
const is = {
|
|
@@ -610,8 +629,7 @@ function prepare(target, root, type, props) {
|
|
|
610
629
|
object,
|
|
611
630
|
eventCount: 0,
|
|
612
631
|
handlers: {},
|
|
613
|
-
isHidden: false
|
|
614
|
-
deferredRefs: []
|
|
632
|
+
isHidden: false
|
|
615
633
|
};
|
|
616
634
|
if (object) object.__r3f = instance;
|
|
617
635
|
}
|
|
@@ -768,6 +786,17 @@ function unregisterVisibility(store, object) {
|
|
|
768
786
|
internal.visibilityRegistry.delete(object.uuid);
|
|
769
787
|
internal.occlusionCache.delete(object);
|
|
770
788
|
}
|
|
789
|
+
function updateVisibilityHandlers(store, object, handlers) {
|
|
790
|
+
const { internal } = store.getState();
|
|
791
|
+
const entry = internal.visibilityRegistry.get(object.uuid);
|
|
792
|
+
if (!entry) return false;
|
|
793
|
+
entry.handlers = handlers;
|
|
794
|
+
if ((handlers.onOccluded || handlers.onVisible) && !internal.occlusionEnabled) {
|
|
795
|
+
object.occlusionTest = true;
|
|
796
|
+
enableOcclusion(store);
|
|
797
|
+
}
|
|
798
|
+
return true;
|
|
799
|
+
}
|
|
771
800
|
function checkVisibility(state) {
|
|
772
801
|
const { internal, camera } = state;
|
|
773
802
|
const registry = internal.visibilityRegistry;
|
|
@@ -928,9 +957,9 @@ function diffProps(instance, newProps) {
|
|
|
928
957
|
const { root, key } = resolve(instance.object, prop);
|
|
929
958
|
if (root.constructor && root.constructor.length === 0) {
|
|
930
959
|
const ctor = getMemoizedPrototype(root);
|
|
931
|
-
if (!is.und(ctor)) changedProps[
|
|
960
|
+
if (!is.und(ctor)) changedProps[prop] = ctor[key];
|
|
932
961
|
} else {
|
|
933
|
-
changedProps[
|
|
962
|
+
changedProps[prop] = 0;
|
|
934
963
|
}
|
|
935
964
|
}
|
|
936
965
|
return changedProps;
|
|
@@ -955,10 +984,7 @@ function applyProps(object, props) {
|
|
|
955
984
|
continue;
|
|
956
985
|
}
|
|
957
986
|
if (value === void 0) continue;
|
|
958
|
-
if (isFromRef(value))
|
|
959
|
-
instance?.deferredRefs?.push({ prop, ref: value[FROM_REF] });
|
|
960
|
-
continue;
|
|
961
|
-
}
|
|
987
|
+
if (isFromRef(value)) continue;
|
|
962
988
|
if (isOnce(value)) {
|
|
963
989
|
if (instance?.appliedOnce?.has(prop)) continue;
|
|
964
990
|
if (instance) {
|
|
@@ -994,8 +1020,24 @@ function applyProps(object, props) {
|
|
|
994
1020
|
} else if (isVectorLike(target) && is.num(value)) {
|
|
995
1021
|
if ("setScalar" in target && typeof target.setScalar === "function") target.setScalar(value);
|
|
996
1022
|
else target.set(value);
|
|
1023
|
+
} else if (root instanceof webgpu.ShaderMaterial && key === "uniforms" && is.obj(value)) {
|
|
1024
|
+
if (!is.obj(root.uniforms)) root.uniforms = {};
|
|
1025
|
+
const uniforms = root.uniforms;
|
|
1026
|
+
const nextUniforms = value;
|
|
1027
|
+
for (const name in nextUniforms) {
|
|
1028
|
+
const uniform = nextUniforms[name];
|
|
1029
|
+
const targetUniform = uniforms[name];
|
|
1030
|
+
if (targetUniform) Object.assign(targetUniform, uniform);
|
|
1031
|
+
else {
|
|
1032
|
+
const nextUniform = uniform instanceof webgpu.Uniform ? uniform.clone() : new webgpu.Uniform(uniform.value);
|
|
1033
|
+
uniforms[name] = nextUniform;
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
997
1036
|
} else {
|
|
998
1037
|
root[key] = value;
|
|
1038
|
+
if (key.endsWith("Node") && root.isMaterial) {
|
|
1039
|
+
root.needsUpdate = true;
|
|
1040
|
+
}
|
|
999
1041
|
if (rootState && rootState.renderer?.outputColorSpace === webgpu.SRGBColorSpace && colorMaps.includes(key) && isTexture(value) && root[key]?.isTexture && // sRGB textures must be RGBA8 since r137 https://github.com/mrdoob/three.js/pull/23129
|
|
1000
1042
|
root[key].format === webgpu.RGBAFormat && root[key].type === webgpu.UnsignedByteType) {
|
|
1001
1043
|
root[key].colorSpace = rootState.textureColorSpace;
|
|
@@ -1009,6 +1051,9 @@ function applyProps(object, props) {
|
|
|
1009
1051
|
if (instance.eventCount && object2.raycast !== null) {
|
|
1010
1052
|
rootState.internal.interaction.push(object2);
|
|
1011
1053
|
}
|
|
1054
|
+
}
|
|
1055
|
+
if (instance?.parent && rootState?.internal && instance.object?.isObject3D) {
|
|
1056
|
+
const object2 = instance.object;
|
|
1012
1057
|
const root = findInitialRoot(instance);
|
|
1013
1058
|
const visibilityHandlers = {
|
|
1014
1059
|
onFramed: instance.handlers.onFramed,
|
|
@@ -1016,7 +1061,9 @@ function applyProps(object, props) {
|
|
|
1016
1061
|
onVisible: instance.handlers.onVisible
|
|
1017
1062
|
};
|
|
1018
1063
|
if (hasVisibilityHandlers(visibilityHandlers)) {
|
|
1019
|
-
|
|
1064
|
+
if (!updateVisibilityHandlers(root, object2, visibilityHandlers)) {
|
|
1065
|
+
registerVisibility(root, object2, visibilityHandlers);
|
|
1066
|
+
}
|
|
1020
1067
|
} else {
|
|
1021
1068
|
unregisterVisibility(root, object2);
|
|
1022
1069
|
}
|
|
@@ -1029,38 +1076,60 @@ function applyProps(object, props) {
|
|
|
1029
1076
|
return object;
|
|
1030
1077
|
}
|
|
1031
1078
|
|
|
1079
|
+
const DEFAULT_POINTER_ID = 0;
|
|
1080
|
+
const XR_POINTER_ID_START = 1e3;
|
|
1081
|
+
function getPointerState(internal, pointerId) {
|
|
1082
|
+
let state = internal.pointerMap.get(pointerId);
|
|
1083
|
+
if (!state) {
|
|
1084
|
+
state = {
|
|
1085
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1086
|
+
captured: /* @__PURE__ */ new Map(),
|
|
1087
|
+
initialClick: [0, 0],
|
|
1088
|
+
initialHits: []
|
|
1089
|
+
};
|
|
1090
|
+
internal.pointerMap.set(pointerId, state);
|
|
1091
|
+
}
|
|
1092
|
+
return state;
|
|
1093
|
+
}
|
|
1094
|
+
function getPointerId(event) {
|
|
1095
|
+
return "pointerId" in event ? event.pointerId : DEFAULT_POINTER_ID;
|
|
1096
|
+
}
|
|
1032
1097
|
function makeId(event) {
|
|
1033
1098
|
return (event.eventObject || event.object).uuid + "/" + event.index + event.instanceId;
|
|
1034
1099
|
}
|
|
1035
|
-
function releaseInternalPointerCapture(
|
|
1036
|
-
const
|
|
1100
|
+
function releaseInternalPointerCapture(internal, obj, pointerId) {
|
|
1101
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1102
|
+
if (!pointerState) return;
|
|
1103
|
+
const captureData = pointerState.captured.get(obj);
|
|
1037
1104
|
if (captureData) {
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
capturedMap.delete(pointerId);
|
|
1041
|
-
captureData.target.releasePointerCapture(pointerId);
|
|
1042
|
-
}
|
|
1105
|
+
pointerState.captured.delete(obj);
|
|
1106
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
1043
1107
|
}
|
|
1044
1108
|
}
|
|
1045
1109
|
function removeInteractivity(store, object) {
|
|
1046
1110
|
const { internal } = store.getState();
|
|
1047
1111
|
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1112
|
+
for (const [pointerId, pointerState] of internal.pointerMap) {
|
|
1113
|
+
pointerState.initialHits = pointerState.initialHits.filter((o) => o !== object);
|
|
1114
|
+
pointerState.hovered.forEach((value, key) => {
|
|
1115
|
+
if (value.eventObject === object || value.object === object) {
|
|
1116
|
+
pointerState.hovered.delete(key);
|
|
1117
|
+
}
|
|
1118
|
+
});
|
|
1119
|
+
if (pointerState.captured.has(object)) {
|
|
1120
|
+
releaseInternalPointerCapture(internal, object, pointerId);
|
|
1052
1121
|
}
|
|
1053
|
-
}
|
|
1054
|
-
internal.capturedMap.forEach((captures, pointerId) => {
|
|
1055
|
-
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
1056
|
-
});
|
|
1122
|
+
}
|
|
1057
1123
|
unregisterVisibility(store, object);
|
|
1058
1124
|
}
|
|
1059
1125
|
function createEvents(store) {
|
|
1060
|
-
function calculateDistance(event) {
|
|
1126
|
+
function calculateDistance(event, pointerId) {
|
|
1061
1127
|
const { internal } = store.getState();
|
|
1062
|
-
const
|
|
1063
|
-
|
|
1128
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1129
|
+
if (!pointerState) return 0;
|
|
1130
|
+
const [initialX, initialY] = pointerState.initialClick;
|
|
1131
|
+
const dx = event.offsetX - initialX;
|
|
1132
|
+
const dy = event.offsetY - initialY;
|
|
1064
1133
|
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
1065
1134
|
}
|
|
1066
1135
|
function filterPointerEvents(objects) {
|
|
@@ -1096,6 +1165,15 @@ function createEvents(store) {
|
|
|
1096
1165
|
return state2.raycaster.camera ? state2.raycaster.intersectObject(obj, true) : [];
|
|
1097
1166
|
}
|
|
1098
1167
|
let hits = eventsObjects.flatMap(handleRaycast).sort((a, b) => {
|
|
1168
|
+
const aInteractivePriority = a.object.userData?.interactivePriority;
|
|
1169
|
+
const bInteractivePriority = b.object.userData?.interactivePriority;
|
|
1170
|
+
if (aInteractivePriority !== void 0 || bInteractivePriority !== void 0) {
|
|
1171
|
+
if (aInteractivePriority !== void 0 && bInteractivePriority === void 0) return -1;
|
|
1172
|
+
if (bInteractivePriority !== void 0 && aInteractivePriority === void 0) return 1;
|
|
1173
|
+
if (aInteractivePriority !== bInteractivePriority) {
|
|
1174
|
+
return (bInteractivePriority ?? 0) - (aInteractivePriority ?? 0);
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1099
1177
|
const aState = getRootState(a.object);
|
|
1100
1178
|
const bState = getRootState(b.object);
|
|
1101
1179
|
const aPriority = aState?.events?.priority ?? 1;
|
|
@@ -1117,9 +1195,13 @@ function createEvents(store) {
|
|
|
1117
1195
|
eventObject = eventObject.parent;
|
|
1118
1196
|
}
|
|
1119
1197
|
}
|
|
1120
|
-
if ("pointerId" in event
|
|
1121
|
-
|
|
1122
|
-
|
|
1198
|
+
if ("pointerId" in event) {
|
|
1199
|
+
const pointerId = event.pointerId;
|
|
1200
|
+
const pointerState = state.internal.pointerMap.get(pointerId);
|
|
1201
|
+
if (pointerState?.captured.size) {
|
|
1202
|
+
for (const captureData of pointerState.captured.values()) {
|
|
1203
|
+
if (!duplicates.has(makeId(captureData.intersection))) intersections.push(captureData.intersection);
|
|
1204
|
+
}
|
|
1123
1205
|
}
|
|
1124
1206
|
}
|
|
1125
1207
|
return intersections;
|
|
@@ -1132,27 +1214,25 @@ function createEvents(store) {
|
|
|
1132
1214
|
if (state) {
|
|
1133
1215
|
const { raycaster, pointer, camera, internal } = state;
|
|
1134
1216
|
const unprojectedPoint = new webgpu.Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
1135
|
-
const hasPointerCapture = (id) =>
|
|
1217
|
+
const hasPointerCapture = (id) => {
|
|
1218
|
+
const pointerState = internal.pointerMap.get(id);
|
|
1219
|
+
return pointerState?.captured.has(hit.eventObject) ?? false;
|
|
1220
|
+
};
|
|
1136
1221
|
const setPointerCapture = (id) => {
|
|
1137
1222
|
const captureData = { intersection: hit, target: event.target };
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
} else {
|
|
1141
|
-
internal.capturedMap.set(id, /* @__PURE__ */ new Map([[hit.eventObject, captureData]]));
|
|
1142
|
-
}
|
|
1223
|
+
const pointerState = getPointerState(internal, id);
|
|
1224
|
+
pointerState.captured.set(hit.eventObject, captureData);
|
|
1143
1225
|
event.target.setPointerCapture(id);
|
|
1144
1226
|
};
|
|
1145
1227
|
const releasePointerCapture = (id) => {
|
|
1146
|
-
|
|
1147
|
-
if (captures) {
|
|
1148
|
-
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
1149
|
-
}
|
|
1228
|
+
releaseInternalPointerCapture(internal, hit.eventObject, id);
|
|
1150
1229
|
};
|
|
1151
1230
|
const extractEventProps = {};
|
|
1152
1231
|
for (const prop in event) {
|
|
1153
1232
|
const property = event[prop];
|
|
1154
1233
|
if (typeof property !== "function") extractEventProps[prop] = property;
|
|
1155
1234
|
}
|
|
1235
|
+
const eventPointerId = "pointerId" in event ? event.pointerId : void 0;
|
|
1156
1236
|
const raycastEvent = {
|
|
1157
1237
|
...hit,
|
|
1158
1238
|
...extractEventProps,
|
|
@@ -1163,18 +1243,19 @@ function createEvents(store) {
|
|
|
1163
1243
|
unprojectedPoint,
|
|
1164
1244
|
ray: raycaster.ray,
|
|
1165
1245
|
camera,
|
|
1246
|
+
pointerId: eventPointerId,
|
|
1166
1247
|
// Hijack stopPropagation, which just sets a flag
|
|
1167
1248
|
stopPropagation() {
|
|
1168
|
-
const
|
|
1249
|
+
const pointerState = eventPointerId !== void 0 ? internal.pointerMap.get(eventPointerId) : void 0;
|
|
1169
1250
|
if (
|
|
1170
1251
|
// ...if this pointer hasn't been captured
|
|
1171
|
-
!
|
|
1172
|
-
|
|
1252
|
+
!pointerState?.captured.size || // ... or if the hit object is capturing the pointer
|
|
1253
|
+
pointerState.captured.has(hit.eventObject)
|
|
1173
1254
|
) {
|
|
1174
1255
|
raycastEvent.stopped = localState.stopped = true;
|
|
1175
|
-
if (
|
|
1256
|
+
if (pointerState?.hovered.size && Array.from(pointerState.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
1176
1257
|
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
1177
|
-
cancelPointer([...higher, hit]);
|
|
1258
|
+
cancelPointer([...higher, hit], eventPointerId);
|
|
1178
1259
|
}
|
|
1179
1260
|
}
|
|
1180
1261
|
},
|
|
@@ -1190,15 +1271,18 @@ function createEvents(store) {
|
|
|
1190
1271
|
}
|
|
1191
1272
|
return intersections;
|
|
1192
1273
|
}
|
|
1193
|
-
function cancelPointer(intersections) {
|
|
1274
|
+
function cancelPointer(intersections, pointerId) {
|
|
1194
1275
|
const { internal } = store.getState();
|
|
1195
|
-
|
|
1276
|
+
const pid = pointerId ?? DEFAULT_POINTER_ID;
|
|
1277
|
+
const pointerState = internal.pointerMap.get(pid);
|
|
1278
|
+
if (!pointerState) return;
|
|
1279
|
+
for (const [hoveredId, hoveredObj] of pointerState.hovered) {
|
|
1196
1280
|
if (!intersections.length || !intersections.find(
|
|
1197
1281
|
(hit) => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId
|
|
1198
1282
|
)) {
|
|
1199
1283
|
const eventObject = hoveredObj.eventObject;
|
|
1200
1284
|
const instance = eventObject.__r3f;
|
|
1201
|
-
|
|
1285
|
+
pointerState.hovered.delete(hoveredId);
|
|
1202
1286
|
if (instance?.eventCount) {
|
|
1203
1287
|
const handlers = instance.handlers;
|
|
1204
1288
|
const data = { ...hoveredObj, intersections };
|
|
@@ -1227,41 +1311,118 @@ function createEvents(store) {
|
|
|
1227
1311
|
instance?.handlers.onDropMissed?.(event);
|
|
1228
1312
|
}
|
|
1229
1313
|
}
|
|
1314
|
+
function cleanupPointer(pointerId) {
|
|
1315
|
+
const { internal } = store.getState();
|
|
1316
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1317
|
+
if (pointerState) {
|
|
1318
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1319
|
+
const eventObject = hoveredObj.eventObject;
|
|
1320
|
+
const instance = eventObject.__r3f;
|
|
1321
|
+
if (instance?.eventCount) {
|
|
1322
|
+
const handlers = instance.handlers;
|
|
1323
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1324
|
+
handlers.onPointerOut?.(data);
|
|
1325
|
+
handlers.onPointerLeave?.(data);
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
internal.pointerMap.delete(pointerId);
|
|
1329
|
+
}
|
|
1330
|
+
internal.pointerDirty.delete(pointerId);
|
|
1331
|
+
}
|
|
1332
|
+
function processDeferredPointer(event, pointerId) {
|
|
1333
|
+
const state = store.getState();
|
|
1334
|
+
const { internal } = state;
|
|
1335
|
+
if (!state.events.enabled) return;
|
|
1336
|
+
const filter = filterPointerEvents;
|
|
1337
|
+
const hits = intersect(event, filter);
|
|
1338
|
+
cancelPointer(hits, pointerId);
|
|
1339
|
+
function onIntersect(data) {
|
|
1340
|
+
const eventObject = data.eventObject;
|
|
1341
|
+
const instance = eventObject.__r3f;
|
|
1342
|
+
if (!instance?.eventCount) return;
|
|
1343
|
+
const handlers = instance.handlers;
|
|
1344
|
+
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1345
|
+
const id = makeId(data);
|
|
1346
|
+
const pointerState = getPointerState(internal, pointerId);
|
|
1347
|
+
const hoveredItem = pointerState.hovered.get(id);
|
|
1348
|
+
if (!hoveredItem) {
|
|
1349
|
+
pointerState.hovered.set(id, data);
|
|
1350
|
+
handlers.onPointerOver?.(data);
|
|
1351
|
+
handlers.onPointerEnter?.(data);
|
|
1352
|
+
} else if (hoveredItem.stopped) {
|
|
1353
|
+
data.stopPropagation();
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
handlers.onPointerMove?.(data);
|
|
1357
|
+
}
|
|
1358
|
+
handleIntersects(hits, event, 0, onIntersect);
|
|
1359
|
+
}
|
|
1230
1360
|
function handlePointer(name) {
|
|
1231
1361
|
switch (name) {
|
|
1232
1362
|
case "onPointerLeave":
|
|
1233
|
-
case "onPointerCancel":
|
|
1234
1363
|
case "onDragLeave":
|
|
1235
1364
|
return () => cancelPointer([]);
|
|
1365
|
+
// Global cancel of these events
|
|
1366
|
+
case "onPointerCancel":
|
|
1367
|
+
return (event) => {
|
|
1368
|
+
const pointerId = getPointerId(event);
|
|
1369
|
+
cleanupPointer(pointerId);
|
|
1370
|
+
};
|
|
1236
1371
|
case "onLostPointerCapture":
|
|
1237
1372
|
return (event) => {
|
|
1238
1373
|
const { internal } = store.getState();
|
|
1239
|
-
|
|
1374
|
+
const pointerId = getPointerId(event);
|
|
1375
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1376
|
+
if (pointerState?.captured.size) {
|
|
1240
1377
|
requestAnimationFrame(() => {
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1378
|
+
const pointerState2 = internal.pointerMap.get(pointerId);
|
|
1379
|
+
if (pointerState2?.captured.size) {
|
|
1380
|
+
pointerState2.captured.clear();
|
|
1244
1381
|
}
|
|
1382
|
+
cancelPointer([], pointerId);
|
|
1245
1383
|
});
|
|
1246
1384
|
}
|
|
1247
1385
|
};
|
|
1248
1386
|
}
|
|
1249
1387
|
return function handleEvent(event) {
|
|
1250
1388
|
const state = store.getState();
|
|
1251
|
-
const { onPointerMissed, onDragOverMissed, onDropMissed, internal } = state;
|
|
1389
|
+
const { onPointerMissed, onDragOverMissed, onDropMissed, internal, events } = state;
|
|
1390
|
+
const pointerId = getPointerId(event);
|
|
1252
1391
|
internal.lastEvent.current = event;
|
|
1253
|
-
if (!
|
|
1392
|
+
if (!events.enabled) return;
|
|
1254
1393
|
const isPointerMove = name === "onPointerMove";
|
|
1255
1394
|
const isDragOver = name === "onDragOver";
|
|
1256
1395
|
const isDrop = name === "onDrop";
|
|
1257
1396
|
const isClickEvent = name === "onClick" || name === "onContextMenu" || name === "onDoubleClick";
|
|
1397
|
+
const isPointerDown = name === "onPointerDown";
|
|
1398
|
+
const isPointerUp = name === "onPointerUp";
|
|
1399
|
+
const isWheel = name === "onWheel";
|
|
1400
|
+
const canDeferRaycasts = events.frameTimedRaycasts && state.frameloop === "always";
|
|
1401
|
+
if (isPointerMove && canDeferRaycasts) {
|
|
1402
|
+
events.compute?.(event, state);
|
|
1403
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
if (isWheel && canDeferRaycasts && !events.alwaysFireOnScroll) {
|
|
1407
|
+
events.compute?.(event, state);
|
|
1408
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
if ((isClickEvent || isPointerDown || isPointerUp) && internal.pointerDirty.has(pointerId)) {
|
|
1412
|
+
const deferredEvent = internal.pointerDirty.get(pointerId);
|
|
1413
|
+
internal.pointerDirty.delete(pointerId);
|
|
1414
|
+
processDeferredPointer(deferredEvent, pointerId);
|
|
1415
|
+
}
|
|
1258
1416
|
const filter = isPointerMove || isDragOver || isDrop ? filterPointerEvents : void 0;
|
|
1259
1417
|
const hits = intersect(event, filter);
|
|
1260
|
-
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
1261
|
-
if (
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1418
|
+
const delta = isClickEvent ? calculateDistance(event, pointerId) : 0;
|
|
1419
|
+
if (isPointerDown) {
|
|
1420
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1421
|
+
pointerState2.initialClick = [event.offsetX, event.offsetY];
|
|
1422
|
+
pointerState2.initialHits = hits.map((hit) => hit.eventObject);
|
|
1423
|
+
}
|
|
1424
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1425
|
+
const initialHits = pointerState?.initialHits ?? [];
|
|
1265
1426
|
if (isClickEvent && !hits.length) {
|
|
1266
1427
|
if (delta <= 2) {
|
|
1267
1428
|
pointerMissed(event, internal.interaction);
|
|
@@ -1276,7 +1437,9 @@ function createEvents(store) {
|
|
|
1276
1437
|
dropMissed(event, internal.interaction);
|
|
1277
1438
|
if (onDropMissed) onDropMissed(event);
|
|
1278
1439
|
}
|
|
1279
|
-
if (isPointerMove || isDragOver)
|
|
1440
|
+
if (isPointerMove || isDragOver) {
|
|
1441
|
+
cancelPointer(hits, pointerId);
|
|
1442
|
+
}
|
|
1280
1443
|
function onIntersect(data) {
|
|
1281
1444
|
const eventObject = data.eventObject;
|
|
1282
1445
|
const instance = eventObject.__r3f;
|
|
@@ -1285,9 +1448,10 @@ function createEvents(store) {
|
|
|
1285
1448
|
if (isPointerMove) {
|
|
1286
1449
|
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1287
1450
|
const id = makeId(data);
|
|
1288
|
-
const
|
|
1451
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1452
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1289
1453
|
if (!hoveredItem) {
|
|
1290
|
-
|
|
1454
|
+
pointerState2.hovered.set(id, data);
|
|
1291
1455
|
handlers.onPointerOver?.(data);
|
|
1292
1456
|
handlers.onPointerEnter?.(data);
|
|
1293
1457
|
} else if (hoveredItem.stopped) {
|
|
@@ -1297,9 +1461,10 @@ function createEvents(store) {
|
|
|
1297
1461
|
handlers.onPointerMove?.(data);
|
|
1298
1462
|
} else if (isDragOver) {
|
|
1299
1463
|
const id = makeId(data);
|
|
1300
|
-
const
|
|
1464
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1465
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1301
1466
|
if (!hoveredItem) {
|
|
1302
|
-
|
|
1467
|
+
pointerState2.hovered.set(id, data);
|
|
1303
1468
|
handlers.onDragOverEnter?.(data);
|
|
1304
1469
|
} else if (hoveredItem.stopped) {
|
|
1305
1470
|
data.stopPropagation();
|
|
@@ -1310,18 +1475,18 @@ function createEvents(store) {
|
|
|
1310
1475
|
} else {
|
|
1311
1476
|
const handler = handlers[name];
|
|
1312
1477
|
if (handler) {
|
|
1313
|
-
if (!isClickEvent ||
|
|
1478
|
+
if (!isClickEvent || initialHits.includes(eventObject)) {
|
|
1314
1479
|
pointerMissed(
|
|
1315
1480
|
event,
|
|
1316
|
-
internal.interaction.filter((object) => !
|
|
1481
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1317
1482
|
);
|
|
1318
1483
|
handler(data);
|
|
1319
1484
|
}
|
|
1320
1485
|
} else {
|
|
1321
|
-
if (isClickEvent &&
|
|
1486
|
+
if (isClickEvent && initialHits.includes(eventObject)) {
|
|
1322
1487
|
pointerMissed(
|
|
1323
1488
|
event,
|
|
1324
|
-
internal.interaction.filter((object) => !
|
|
1489
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1325
1490
|
);
|
|
1326
1491
|
}
|
|
1327
1492
|
}
|
|
@@ -1330,7 +1495,15 @@ function createEvents(store) {
|
|
|
1330
1495
|
handleIntersects(hits, event, delta, onIntersect);
|
|
1331
1496
|
};
|
|
1332
1497
|
}
|
|
1333
|
-
|
|
1498
|
+
function flushDeferredPointers() {
|
|
1499
|
+
const { internal, events } = store.getState();
|
|
1500
|
+
if (!events.frameTimedRaycasts) return;
|
|
1501
|
+
for (const [pointerId, event] of internal.pointerDirty) {
|
|
1502
|
+
processDeferredPointer(event, pointerId);
|
|
1503
|
+
}
|
|
1504
|
+
internal.pointerDirty.clear();
|
|
1505
|
+
}
|
|
1506
|
+
return { handlePointer, flushDeferredPointers, processDeferredPointer };
|
|
1334
1507
|
}
|
|
1335
1508
|
const DOM_EVENTS = {
|
|
1336
1509
|
onClick: ["click", false],
|
|
@@ -1349,10 +1522,15 @@ const DOM_EVENTS = {
|
|
|
1349
1522
|
onLostPointerCapture: ["lostpointercapture", true]
|
|
1350
1523
|
};
|
|
1351
1524
|
function createPointerEvents(store) {
|
|
1352
|
-
const { handlePointer } = createEvents(store);
|
|
1525
|
+
const { handlePointer, flushDeferredPointers, processDeferredPointer } = createEvents(store);
|
|
1526
|
+
let nextXRPointerId = XR_POINTER_ID_START;
|
|
1527
|
+
const xrPointers = /* @__PURE__ */ new Map();
|
|
1353
1528
|
return {
|
|
1354
1529
|
priority: 1,
|
|
1355
1530
|
enabled: true,
|
|
1531
|
+
frameTimedRaycasts: true,
|
|
1532
|
+
alwaysFireOnScroll: true,
|
|
1533
|
+
updateOnFrame: false,
|
|
1356
1534
|
compute(event, state) {
|
|
1357
1535
|
state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
1358
1536
|
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
@@ -1362,11 +1540,33 @@ function createPointerEvents(store) {
|
|
|
1362
1540
|
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
1363
1541
|
{}
|
|
1364
1542
|
),
|
|
1365
|
-
update: () => {
|
|
1543
|
+
update: (pointerId) => {
|
|
1366
1544
|
const { events, internal } = store.getState();
|
|
1367
|
-
if (
|
|
1545
|
+
if (!events.handlers) return;
|
|
1546
|
+
if (pointerId !== void 0) {
|
|
1547
|
+
const event = internal.pointerDirty.get(pointerId);
|
|
1548
|
+
if (event) {
|
|
1549
|
+
internal.pointerDirty.delete(pointerId);
|
|
1550
|
+
processDeferredPointer(event, pointerId);
|
|
1551
|
+
} else if (internal.lastEvent?.current) {
|
|
1552
|
+
processDeferredPointer(internal.lastEvent.current, pointerId);
|
|
1553
|
+
}
|
|
1554
|
+
} else {
|
|
1555
|
+
flushDeferredPointers();
|
|
1556
|
+
if (internal.lastEvent?.current) {
|
|
1557
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
},
|
|
1561
|
+
flush: () => {
|
|
1562
|
+
const { events, internal } = store.getState();
|
|
1563
|
+
flushDeferredPointers();
|
|
1564
|
+
if (events.updateOnFrame && internal.lastEvent?.current && events.handlers) {
|
|
1565
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1566
|
+
}
|
|
1368
1567
|
},
|
|
1369
1568
|
connect: (target) => {
|
|
1569
|
+
if (!target) return;
|
|
1370
1570
|
const { set, events } = store.getState();
|
|
1371
1571
|
events.disconnect?.();
|
|
1372
1572
|
set((state) => ({ events: { ...state.events, connected: target } }));
|
|
@@ -1390,6 +1590,33 @@ function createPointerEvents(store) {
|
|
|
1390
1590
|
}
|
|
1391
1591
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
1392
1592
|
}
|
|
1593
|
+
},
|
|
1594
|
+
// PREVIEW (A8): the XR pointer registration API may change before stable.
|
|
1595
|
+
registerPointer: (config) => {
|
|
1596
|
+
const pointerId = nextXRPointerId++;
|
|
1597
|
+
xrPointers.set(pointerId, config);
|
|
1598
|
+
const { internal } = store.getState();
|
|
1599
|
+
getPointerState(internal, pointerId);
|
|
1600
|
+
return pointerId;
|
|
1601
|
+
},
|
|
1602
|
+
unregisterPointer: (pointerId) => {
|
|
1603
|
+
xrPointers.delete(pointerId);
|
|
1604
|
+
const { internal } = store.getState();
|
|
1605
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1606
|
+
if (pointerState) {
|
|
1607
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1608
|
+
const eventObject = hoveredObj.eventObject;
|
|
1609
|
+
const instance = eventObject.__r3f;
|
|
1610
|
+
if (instance?.eventCount) {
|
|
1611
|
+
const handlers = instance.handlers;
|
|
1612
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1613
|
+
handlers.onPointerOut?.(data);
|
|
1614
|
+
handlers.onPointerLeave?.(data);
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
internal.pointerMap.delete(pointerId);
|
|
1618
|
+
}
|
|
1619
|
+
internal.pointerDirty.delete(pointerId);
|
|
1393
1620
|
}
|
|
1394
1621
|
};
|
|
1395
1622
|
}
|
|
@@ -1451,1036 +1678,11 @@ function notifyAlpha({ message, link }) {
|
|
|
1451
1678
|
}
|
|
1452
1679
|
}
|
|
1453
1680
|
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
const
|
|
1458
|
-
|
|
1459
|
-
constructor() {
|
|
1460
|
-
/** Ordered list of phase nodes */
|
|
1461
|
-
__publicField$1(this, "phases", []);
|
|
1462
|
-
/** Quick lookup by name */
|
|
1463
|
-
__publicField$1(this, "phaseMap", /* @__PURE__ */ new Map());
|
|
1464
|
-
/** Cached ordered names (invalidated on changes) */
|
|
1465
|
-
__publicField$1(this, "orderedNamesCache", null);
|
|
1466
|
-
this.initializeDefaultPhases();
|
|
1467
|
-
}
|
|
1468
|
-
//* Initialization --------------------------------
|
|
1469
|
-
initializeDefaultPhases() {
|
|
1470
|
-
for (const name of DEFAULT_PHASES) {
|
|
1471
|
-
const node = { name, isAutoGenerated: false };
|
|
1472
|
-
this.phases.push(node);
|
|
1473
|
-
this.phaseMap.set(name, node);
|
|
1474
|
-
}
|
|
1475
|
-
this.invalidateCache();
|
|
1476
|
-
}
|
|
1477
|
-
//* Public API --------------------------------
|
|
1478
|
-
/**
|
|
1479
|
-
* Add a named phase to the graph
|
|
1480
|
-
* @param name - Phase name (must be unique)
|
|
1481
|
-
* @param options - Position options (before or after another phase)
|
|
1482
|
-
*/
|
|
1483
|
-
addPhase(name, options = {}) {
|
|
1484
|
-
if (this.phaseMap.has(name)) {
|
|
1485
|
-
console.warn(`[useFrame] Phase "${name}" already exists`);
|
|
1486
|
-
return;
|
|
1487
|
-
}
|
|
1488
|
-
const { before, after } = options;
|
|
1489
|
-
const node = { name, isAutoGenerated: false };
|
|
1490
|
-
let insertIndex = this.phases.length;
|
|
1491
|
-
const targetIndex = this.getPhaseIndex(before ?? after);
|
|
1492
|
-
if (targetIndex !== -1) {
|
|
1493
|
-
insertIndex = before ? targetIndex : targetIndex + 1;
|
|
1494
|
-
} else {
|
|
1495
|
-
const constraintType = before ? "before" : "after";
|
|
1496
|
-
console.warn(`[useFrame] Phase "${before ?? after}" not found for '${constraintType}' constraint`);
|
|
1497
|
-
}
|
|
1498
|
-
this.phases.splice(insertIndex, 0, node);
|
|
1499
|
-
this.phaseMap.set(name, node);
|
|
1500
|
-
this.invalidateCache();
|
|
1501
|
-
}
|
|
1502
|
-
/**
|
|
1503
|
-
* Get ordered list of phase names
|
|
1504
|
-
*/
|
|
1505
|
-
getOrderedPhases() {
|
|
1506
|
-
if (this.orderedNamesCache === null) this.orderedNamesCache = this.phases.map((p) => p.name);
|
|
1507
|
-
return this.orderedNamesCache;
|
|
1508
|
-
}
|
|
1509
|
-
/**
|
|
1510
|
-
* Check if a phase exists
|
|
1511
|
-
*/
|
|
1512
|
-
hasPhase(name) {
|
|
1513
|
-
return this.phaseMap.has(name);
|
|
1514
|
-
}
|
|
1515
|
-
/**
|
|
1516
|
-
* Get the index of a phase (-1 if not found)
|
|
1517
|
-
*/
|
|
1518
|
-
getPhaseIndex(name) {
|
|
1519
|
-
if (!name) return -1;
|
|
1520
|
-
return this.phases.findIndex((p) => p.name === name);
|
|
1521
|
-
}
|
|
1522
|
-
/**
|
|
1523
|
-
* Ensure a phase exists, creating an auto-generated one if needed.
|
|
1524
|
-
* Used for resolving before/after constraints.
|
|
1525
|
-
*
|
|
1526
|
-
* @param name - The phase name to ensure exists
|
|
1527
|
-
* @returns The phase name (may be auto-generated like 'before:render')
|
|
1528
|
-
*/
|
|
1529
|
-
ensurePhase(name) {
|
|
1530
|
-
if (this.phaseMap.has(name)) return name;
|
|
1531
|
-
const node = { name, isAutoGenerated: true };
|
|
1532
|
-
this.phases.push(node);
|
|
1533
|
-
this.phaseMap.set(name, node);
|
|
1534
|
-
this.invalidateCache();
|
|
1535
|
-
return name;
|
|
1536
|
-
}
|
|
1537
|
-
/**
|
|
1538
|
-
* Resolve where a job with before/after constraints should go.
|
|
1539
|
-
* Creates auto-generated phases if needed.
|
|
1540
|
-
*
|
|
1541
|
-
* @param before - Phase(s) to run before
|
|
1542
|
-
* @param after - Phase(s) to run after
|
|
1543
|
-
* @returns The resolved phase name
|
|
1544
|
-
*/
|
|
1545
|
-
resolveConstraintPhase(before, after) {
|
|
1546
|
-
const beforeArr = before ? Array.isArray(before) ? before : [before] : [];
|
|
1547
|
-
const afterArr = after ? Array.isArray(after) ? after : [after] : [];
|
|
1548
|
-
if (beforeArr.length > 0) {
|
|
1549
|
-
return this.ensureAutoPhase(beforeArr[0], "before", 0);
|
|
1550
|
-
}
|
|
1551
|
-
if (afterArr.length > 0) {
|
|
1552
|
-
return this.ensureAutoPhase(afterArr[0], "after", 1);
|
|
1553
|
-
}
|
|
1554
|
-
return "update";
|
|
1555
|
-
}
|
|
1556
|
-
/**
|
|
1557
|
-
* Ensure an auto-generated phase exists relative to a target phase.
|
|
1558
|
-
* Creates the phase if it doesn't exist, inserting it at the correct position.
|
|
1559
|
-
*
|
|
1560
|
-
* @param target - The target phase name to position relative to
|
|
1561
|
-
* @param prefix - Prefix for auto-generated phase name ('before' or 'after')
|
|
1562
|
-
* @param offset - Insertion offset (0 for before, 1 for after)
|
|
1563
|
-
* @returns The auto-generated phase name
|
|
1564
|
-
*/
|
|
1565
|
-
ensureAutoPhase(target, prefix, offset) {
|
|
1566
|
-
const autoName = `${prefix}:${target}`;
|
|
1567
|
-
if (this.phaseMap.has(autoName)) return autoName;
|
|
1568
|
-
const node = { name: autoName, isAutoGenerated: true };
|
|
1569
|
-
const targetIndex = this.getPhaseIndex(target);
|
|
1570
|
-
if (targetIndex !== -1) this.phases.splice(targetIndex + offset, 0, node);
|
|
1571
|
-
else this.phases.push(node);
|
|
1572
|
-
this.phaseMap.set(autoName, node);
|
|
1573
|
-
this.invalidateCache();
|
|
1574
|
-
return autoName;
|
|
1575
|
-
}
|
|
1576
|
-
// Internal --------------------------------
|
|
1577
|
-
invalidateCache() {
|
|
1578
|
-
this.orderedNamesCache = null;
|
|
1579
|
-
}
|
|
1580
|
-
}
|
|
1581
|
-
|
|
1582
|
-
function rebuildSortedJobs(jobs, phaseGraph) {
|
|
1583
|
-
const orderedPhases = phaseGraph.getOrderedPhases();
|
|
1584
|
-
const buckets = /* @__PURE__ */ new Map();
|
|
1585
|
-
for (const phase of orderedPhases) {
|
|
1586
|
-
buckets.set(phase, []);
|
|
1587
|
-
}
|
|
1588
|
-
for (const job of jobs.values()) {
|
|
1589
|
-
if (!job.enabled) continue;
|
|
1590
|
-
let bucket = buckets.get(job.phase);
|
|
1591
|
-
if (!bucket) {
|
|
1592
|
-
bucket = [];
|
|
1593
|
-
buckets.set(job.phase, bucket);
|
|
1594
|
-
}
|
|
1595
|
-
bucket.push(job);
|
|
1596
|
-
}
|
|
1597
|
-
const sortedBuckets = [];
|
|
1598
|
-
for (const phase of orderedPhases) {
|
|
1599
|
-
const bucket = buckets.get(phase);
|
|
1600
|
-
if (!bucket || bucket.length === 0) continue;
|
|
1601
|
-
bucket.sort((a, b) => {
|
|
1602
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1603
|
-
return a.index - b.index;
|
|
1604
|
-
});
|
|
1605
|
-
sortedBuckets.push(hasCrossJobConstraints(bucket) ? topologicalSort(bucket) : bucket);
|
|
1606
|
-
}
|
|
1607
|
-
for (const [phase, bucket] of buckets) {
|
|
1608
|
-
if (!orderedPhases.includes(phase) && bucket.length > 0) {
|
|
1609
|
-
bucket.sort((a, b) => {
|
|
1610
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1611
|
-
return a.index - b.index;
|
|
1612
|
-
});
|
|
1613
|
-
sortedBuckets.push(bucket);
|
|
1614
|
-
}
|
|
1615
|
-
}
|
|
1616
|
-
return sortedBuckets.flat();
|
|
1617
|
-
}
|
|
1618
|
-
function hasCrossJobConstraints(bucket) {
|
|
1619
|
-
const jobIds = new Set(bucket.map((j) => j.id));
|
|
1620
|
-
for (const job of bucket) {
|
|
1621
|
-
for (const ref of job.before) {
|
|
1622
|
-
if (jobIds.has(ref)) return true;
|
|
1623
|
-
}
|
|
1624
|
-
for (const ref of job.after) {
|
|
1625
|
-
if (jobIds.has(ref)) return true;
|
|
1626
|
-
}
|
|
1627
|
-
}
|
|
1628
|
-
return false;
|
|
1629
|
-
}
|
|
1630
|
-
function topologicalSort(jobs) {
|
|
1631
|
-
const n = jobs.length;
|
|
1632
|
-
if (n <= 1) return jobs;
|
|
1633
|
-
const jobMap = /* @__PURE__ */ new Map();
|
|
1634
|
-
const inDegree = /* @__PURE__ */ new Map();
|
|
1635
|
-
const adjacency = /* @__PURE__ */ new Map();
|
|
1636
|
-
for (const job of jobs) {
|
|
1637
|
-
jobMap.set(job.id, job);
|
|
1638
|
-
inDegree.set(job.id, 0);
|
|
1639
|
-
adjacency.set(job.id, []);
|
|
1640
|
-
}
|
|
1641
|
-
for (const job of jobs) {
|
|
1642
|
-
for (const ref of job.before) {
|
|
1643
|
-
if (jobMap.has(ref)) {
|
|
1644
|
-
adjacency.get(job.id).push(ref);
|
|
1645
|
-
inDegree.set(ref, inDegree.get(ref) + 1);
|
|
1646
|
-
}
|
|
1647
|
-
}
|
|
1648
|
-
for (const ref of job.after) {
|
|
1649
|
-
if (jobMap.has(ref)) {
|
|
1650
|
-
adjacency.get(ref).push(job.id);
|
|
1651
|
-
inDegree.set(job.id, inDegree.get(job.id) + 1);
|
|
1652
|
-
}
|
|
1653
|
-
}
|
|
1654
|
-
}
|
|
1655
|
-
const queue = [];
|
|
1656
|
-
for (const job of jobs) {
|
|
1657
|
-
if (inDegree.get(job.id) === 0) {
|
|
1658
|
-
queue.push(job);
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
queue.sort((a, b) => {
|
|
1662
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1663
|
-
return a.index - b.index;
|
|
1664
|
-
});
|
|
1665
|
-
const result = [];
|
|
1666
|
-
while (queue.length > 0) {
|
|
1667
|
-
const job = queue.shift();
|
|
1668
|
-
result.push(job);
|
|
1669
|
-
const neighbors = adjacency.get(job.id) || [];
|
|
1670
|
-
for (const neighborId of neighbors) {
|
|
1671
|
-
const newDegree = inDegree.get(neighborId) - 1;
|
|
1672
|
-
inDegree.set(neighborId, newDegree);
|
|
1673
|
-
if (newDegree === 0) {
|
|
1674
|
-
const neighbor = jobMap.get(neighborId);
|
|
1675
|
-
insertSorted(queue, neighbor);
|
|
1676
|
-
}
|
|
1677
|
-
}
|
|
1678
|
-
}
|
|
1679
|
-
if (result.length !== n) {
|
|
1680
|
-
console.warn("[useFrame] Circular dependency detected in job constraints");
|
|
1681
|
-
const resultIds = new Set(result.map((j) => j.id));
|
|
1682
|
-
for (const job of jobs) {
|
|
1683
|
-
if (!resultIds.has(job.id)) result.push(job);
|
|
1684
|
-
}
|
|
1685
|
-
}
|
|
1686
|
-
return result;
|
|
1687
|
-
}
|
|
1688
|
-
function insertSorted(arr, job) {
|
|
1689
|
-
let i = 0;
|
|
1690
|
-
while (i < arr.length) {
|
|
1691
|
-
const cmp = arr[i];
|
|
1692
|
-
if (job.priority > cmp.priority || job.priority === cmp.priority && job.index < cmp.index) {
|
|
1693
|
-
break;
|
|
1694
|
-
}
|
|
1695
|
-
i++;
|
|
1696
|
-
}
|
|
1697
|
-
arr.splice(i, 0, job);
|
|
1698
|
-
}
|
|
1699
|
-
|
|
1700
|
-
function shouldRun(job, now) {
|
|
1701
|
-
if (!job.enabled) return false;
|
|
1702
|
-
if (!job.fps) return true;
|
|
1703
|
-
const minInterval = 1e3 / job.fps;
|
|
1704
|
-
const lastRun = job.lastRun ?? 0;
|
|
1705
|
-
const elapsed = now - lastRun;
|
|
1706
|
-
if (elapsed < minInterval) return false;
|
|
1707
|
-
if (job.drop) {
|
|
1708
|
-
job.lastRun = now;
|
|
1709
|
-
} else {
|
|
1710
|
-
const steps = Math.floor(elapsed / minInterval);
|
|
1711
|
-
job.lastRun = lastRun + steps * minInterval;
|
|
1712
|
-
if (job.lastRun < now - minInterval) {
|
|
1713
|
-
job.lastRun = now;
|
|
1714
|
-
}
|
|
1715
|
-
}
|
|
1716
|
-
return true;
|
|
1717
|
-
}
|
|
1718
|
-
function resetJobTiming(job) {
|
|
1719
|
-
job.lastRun = void 0;
|
|
1720
|
-
}
|
|
1721
|
-
|
|
1722
|
-
var __defProp = Object.defineProperty;
|
|
1723
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1724
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1725
|
-
const hmrData = (() => {
|
|
1726
|
-
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return void 0;
|
|
1727
|
-
if (typeof import_meta_hot !== "undefined") return import_meta_hot;
|
|
1728
|
-
try {
|
|
1729
|
-
return (0, eval)("import.meta.hot");
|
|
1730
|
-
} catch {
|
|
1731
|
-
return void 0;
|
|
1732
|
-
}
|
|
1733
|
-
})();
|
|
1734
|
-
const _Scheduler = class _Scheduler {
|
|
1735
|
-
//* Constructor ================================
|
|
1736
|
-
constructor() {
|
|
1737
|
-
//* Critical State ================================
|
|
1738
|
-
__publicField(this, "roots", /* @__PURE__ */ new Map());
|
|
1739
|
-
__publicField(this, "phaseGraph");
|
|
1740
|
-
__publicField(this, "loopState", {
|
|
1741
|
-
running: false,
|
|
1742
|
-
rafHandle: null,
|
|
1743
|
-
lastTime: null,
|
|
1744
|
-
// null = uninitialized, 0+ = valid timestamp
|
|
1745
|
-
frameCount: 0,
|
|
1746
|
-
elapsedTime: 0,
|
|
1747
|
-
createdAt: performance.now()
|
|
1748
|
-
});
|
|
1749
|
-
__publicField(this, "stoppedTime", 0);
|
|
1750
|
-
//* Private State ================================
|
|
1751
|
-
__publicField(this, "nextRootIndex", 0);
|
|
1752
|
-
__publicField(this, "globalBeforeJobs", /* @__PURE__ */ new Map());
|
|
1753
|
-
__publicField(this, "globalAfterJobs", /* @__PURE__ */ new Map());
|
|
1754
|
-
__publicField(this, "nextGlobalIndex", 0);
|
|
1755
|
-
__publicField(this, "idleCallbacks", /* @__PURE__ */ new Set());
|
|
1756
|
-
__publicField(this, "nextJobIndex", 0);
|
|
1757
|
-
__publicField(this, "jobStateListeners", /* @__PURE__ */ new Map());
|
|
1758
|
-
__publicField(this, "pendingFrames", 0);
|
|
1759
|
-
__publicField(this, "_frameloop", "always");
|
|
1760
|
-
//* Independent Mode & Error Handling State ================================
|
|
1761
|
-
__publicField(this, "_independent", false);
|
|
1762
|
-
__publicField(this, "errorHandler", null);
|
|
1763
|
-
__publicField(this, "rootReadyCallbacks", /* @__PURE__ */ new Set());
|
|
1764
|
-
//* Core Loop Execution Methods ================================
|
|
1765
|
-
/**
|
|
1766
|
-
* Main RAF loop callback.
|
|
1767
|
-
* Executes frame, handles demand mode, and schedules next frame.
|
|
1768
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
1769
|
-
* @returns {void}
|
|
1770
|
-
* @private
|
|
1771
|
-
*/
|
|
1772
|
-
__publicField(this, "loop", (timestamp) => {
|
|
1773
|
-
if (!this.loopState.running) return;
|
|
1774
|
-
this.executeFrame(timestamp);
|
|
1775
|
-
if (this._frameloop === "demand") {
|
|
1776
|
-
this.pendingFrames = Math.max(0, this.pendingFrames - 1);
|
|
1777
|
-
if (this.pendingFrames === 0) {
|
|
1778
|
-
this.notifyIdle(timestamp);
|
|
1779
|
-
return this.stop();
|
|
1780
|
-
}
|
|
1781
|
-
}
|
|
1782
|
-
this.loopState.rafHandle = requestAnimationFrame(this.loop);
|
|
1783
|
-
});
|
|
1784
|
-
this.phaseGraph = new PhaseGraph();
|
|
1785
|
-
}
|
|
1786
|
-
static get instance() {
|
|
1787
|
-
return globalThis[_Scheduler.INSTANCE_KEY] ?? null;
|
|
1788
|
-
}
|
|
1789
|
-
static set instance(value) {
|
|
1790
|
-
globalThis[_Scheduler.INSTANCE_KEY] = value;
|
|
1791
|
-
}
|
|
1792
|
-
/**
|
|
1793
|
-
* Get the global scheduler instance (creates if doesn't exist).
|
|
1794
|
-
* Uses HMR data to preserve instance across hot reloads.
|
|
1795
|
-
* @returns {Scheduler} The singleton scheduler instance
|
|
1796
|
-
*/
|
|
1797
|
-
static get() {
|
|
1798
|
-
if (!_Scheduler.instance && hmrData?.data?.scheduler) {
|
|
1799
|
-
_Scheduler.instance = hmrData.data.scheduler;
|
|
1800
|
-
}
|
|
1801
|
-
if (!_Scheduler.instance) {
|
|
1802
|
-
_Scheduler.instance = new _Scheduler();
|
|
1803
|
-
if (hmrData?.data) {
|
|
1804
|
-
hmrData.data.scheduler = _Scheduler.instance;
|
|
1805
|
-
}
|
|
1806
|
-
}
|
|
1807
|
-
return _Scheduler.instance;
|
|
1808
|
-
}
|
|
1809
|
-
/**
|
|
1810
|
-
* Reset the singleton instance. Stops the loop and clears all state.
|
|
1811
|
-
* Primarily used for testing to ensure clean state between tests.
|
|
1812
|
-
* @returns {void}
|
|
1813
|
-
*/
|
|
1814
|
-
static reset() {
|
|
1815
|
-
if (_Scheduler.instance) {
|
|
1816
|
-
_Scheduler.instance.stop();
|
|
1817
|
-
_Scheduler.instance = null;
|
|
1818
|
-
}
|
|
1819
|
-
if (hmrData?.data) {
|
|
1820
|
-
hmrData.data.scheduler = null;
|
|
1821
|
-
}
|
|
1822
|
-
}
|
|
1823
|
-
//* Getters & Setters ================================
|
|
1824
|
-
get phases() {
|
|
1825
|
-
return this.phaseGraph.getOrderedPhases();
|
|
1826
|
-
}
|
|
1827
|
-
get frameloop() {
|
|
1828
|
-
return this._frameloop;
|
|
1829
|
-
}
|
|
1830
|
-
set frameloop(mode) {
|
|
1831
|
-
if (this._frameloop === mode) return;
|
|
1832
|
-
const wasAlways = this._frameloop === "always";
|
|
1833
|
-
this._frameloop = mode;
|
|
1834
|
-
if (mode === "always" && !this.loopState.running && this.roots.size > 0) this.start();
|
|
1835
|
-
else if (mode !== "always" && wasAlways) this.stop();
|
|
1836
|
-
}
|
|
1837
|
-
get isRunning() {
|
|
1838
|
-
return this.loopState.running;
|
|
1839
|
-
}
|
|
1840
|
-
get isReady() {
|
|
1841
|
-
return this.roots.size > 0;
|
|
1842
|
-
}
|
|
1843
|
-
get independent() {
|
|
1844
|
-
return this._independent;
|
|
1845
|
-
}
|
|
1846
|
-
set independent(value) {
|
|
1847
|
-
this._independent = value;
|
|
1848
|
-
if (value) this.ensureDefaultRoot();
|
|
1849
|
-
}
|
|
1850
|
-
//* Root Management Methods ================================
|
|
1851
|
-
/**
|
|
1852
|
-
* Register a root (Canvas) with the scheduler.
|
|
1853
|
-
* The first root to register starts the RAF loop (if frameloop='always').
|
|
1854
|
-
* @param {string} id - Unique identifier for this root
|
|
1855
|
-
* @param {RootOptions} [options] - Optional configuration with getState and onError callbacks
|
|
1856
|
-
* @returns {() => void} Unsubscribe function to remove this root
|
|
1857
|
-
*/
|
|
1858
|
-
registerRoot(id, options = {}) {
|
|
1859
|
-
if (this.roots.has(id)) {
|
|
1860
|
-
console.warn(`[Scheduler] Root "${id}" already registered`);
|
|
1861
|
-
return () => this.unregisterRoot(id);
|
|
1862
|
-
}
|
|
1863
|
-
const entry = {
|
|
1864
|
-
id,
|
|
1865
|
-
getState: options.getState ?? (() => ({})),
|
|
1866
|
-
jobs: /* @__PURE__ */ new Map(),
|
|
1867
|
-
sortedJobs: [],
|
|
1868
|
-
needsRebuild: false
|
|
1869
|
-
};
|
|
1870
|
-
if (options.onError) {
|
|
1871
|
-
this.errorHandler = options.onError;
|
|
1872
|
-
}
|
|
1873
|
-
this.roots.set(id, entry);
|
|
1874
|
-
if (this.roots.size === 1) {
|
|
1875
|
-
this.notifyRootReady();
|
|
1876
|
-
if (this._frameloop === "always") this.start();
|
|
1877
|
-
}
|
|
1878
|
-
return () => this.unregisterRoot(id);
|
|
1879
|
-
}
|
|
1880
|
-
/**
|
|
1881
|
-
* Unregister a root from the scheduler.
|
|
1882
|
-
* Cleans up all job state listeners for this root's jobs.
|
|
1883
|
-
* The last root to unregister stops the RAF loop.
|
|
1884
|
-
* @param {string} id - The root ID to unregister
|
|
1885
|
-
* @returns {void}
|
|
1886
|
-
*/
|
|
1887
|
-
unregisterRoot(id) {
|
|
1888
|
-
const root = this.roots.get(id);
|
|
1889
|
-
if (!root) return;
|
|
1890
|
-
for (const jobId of root.jobs.keys()) {
|
|
1891
|
-
this.jobStateListeners.delete(jobId);
|
|
1892
|
-
}
|
|
1893
|
-
this.roots.delete(id);
|
|
1894
|
-
if (this.roots.size === 0) {
|
|
1895
|
-
this.stop();
|
|
1896
|
-
this.errorHandler = null;
|
|
1897
|
-
}
|
|
1898
|
-
}
|
|
1899
|
-
/**
|
|
1900
|
-
* Subscribe to be notified when a root becomes available.
|
|
1901
|
-
* Fires immediately if a root already exists.
|
|
1902
|
-
* @param {() => void} callback - Function called when first root registers
|
|
1903
|
-
* @returns {() => void} Unsubscribe function
|
|
1904
|
-
*/
|
|
1905
|
-
onRootReady(callback) {
|
|
1906
|
-
if (this.roots.size > 0) {
|
|
1907
|
-
callback();
|
|
1908
|
-
return () => {
|
|
1909
|
-
};
|
|
1910
|
-
}
|
|
1911
|
-
this.rootReadyCallbacks.add(callback);
|
|
1912
|
-
return () => this.rootReadyCallbacks.delete(callback);
|
|
1913
|
-
}
|
|
1914
|
-
/**
|
|
1915
|
-
* Notify all registered root-ready callbacks.
|
|
1916
|
-
* Called when the first root registers.
|
|
1917
|
-
* @returns {void}
|
|
1918
|
-
* @private
|
|
1919
|
-
*/
|
|
1920
|
-
notifyRootReady() {
|
|
1921
|
-
for (const cb of this.rootReadyCallbacks) {
|
|
1922
|
-
try {
|
|
1923
|
-
cb();
|
|
1924
|
-
} catch (error) {
|
|
1925
|
-
console.error("[Scheduler] Error in root-ready callback:", error);
|
|
1926
|
-
}
|
|
1927
|
-
}
|
|
1928
|
-
this.rootReadyCallbacks.clear();
|
|
1929
|
-
}
|
|
1930
|
-
/**
|
|
1931
|
-
* Ensure a default root exists for independent mode.
|
|
1932
|
-
* Creates a minimal root with no state provider.
|
|
1933
|
-
* @returns {void}
|
|
1934
|
-
* @private
|
|
1935
|
-
*/
|
|
1936
|
-
ensureDefaultRoot() {
|
|
1937
|
-
if (!this.roots.has("__default__")) {
|
|
1938
|
-
this.registerRoot("__default__");
|
|
1939
|
-
}
|
|
1940
|
-
}
|
|
1941
|
-
/**
|
|
1942
|
-
* Trigger error handling for job errors.
|
|
1943
|
-
* Uses the bound error handler if available, otherwise logs to console.
|
|
1944
|
-
* @param {Error} error - The error to handle
|
|
1945
|
-
* @returns {void}
|
|
1946
|
-
*/
|
|
1947
|
-
triggerError(error) {
|
|
1948
|
-
if (this.errorHandler) this.errorHandler(error);
|
|
1949
|
-
else console.error("[Scheduler]", error);
|
|
1950
|
-
}
|
|
1951
|
-
//* Phase Management Methods ================================
|
|
1952
|
-
/**
|
|
1953
|
-
* Add a named phase to the scheduler's execution order.
|
|
1954
|
-
* Marks all roots for rebuild to incorporate the new phase.
|
|
1955
|
-
* @param {string} name - The phase name (e.g., 'physics', 'postprocess')
|
|
1956
|
-
* @param {AddPhaseOptions} [options] - Positioning options (before/after other phases)
|
|
1957
|
-
* @returns {void}
|
|
1958
|
-
* @example
|
|
1959
|
-
* scheduler.addPhase('physics', { before: 'update' });
|
|
1960
|
-
* scheduler.addPhase('postprocess', { after: 'render' });
|
|
1961
|
-
*/
|
|
1962
|
-
addPhase(name, options) {
|
|
1963
|
-
this.phaseGraph.addPhase(name, options);
|
|
1964
|
-
for (const root of this.roots.values()) {
|
|
1965
|
-
root.needsRebuild = true;
|
|
1966
|
-
}
|
|
1967
|
-
}
|
|
1968
|
-
/**
|
|
1969
|
-
* Check if a phase exists in the scheduler.
|
|
1970
|
-
* @param {string} name - The phase name to check
|
|
1971
|
-
* @returns {boolean} True if the phase exists
|
|
1972
|
-
*/
|
|
1973
|
-
hasPhase(name) {
|
|
1974
|
-
return this.phaseGraph.hasPhase(name);
|
|
1975
|
-
}
|
|
1976
|
-
//* Global Job Registration Methods (Deprecated APIs) ================================
|
|
1977
|
-
/**
|
|
1978
|
-
* Register a global job that runs once per frame (not per-root).
|
|
1979
|
-
* Used internally by deprecated addEffect/addAfterEffect APIs.
|
|
1980
|
-
* @param {'before' | 'after'} phase - When to run: 'before' all roots or 'after' all roots
|
|
1981
|
-
* @param {string} id - Unique identifier for this global job
|
|
1982
|
-
* @param {(timestamp: number) => void} callback - Function called each frame with RAF timestamp
|
|
1983
|
-
* @returns {() => void} Unsubscribe function to remove this global job
|
|
1984
|
-
* @deprecated Use useFrame with phases instead
|
|
1985
|
-
*/
|
|
1986
|
-
registerGlobal(phase, id, callback) {
|
|
1987
|
-
const job = { id, callback };
|
|
1988
|
-
if (phase === "before") {
|
|
1989
|
-
this.globalBeforeJobs.set(id, job);
|
|
1990
|
-
} else {
|
|
1991
|
-
this.globalAfterJobs.set(id, job);
|
|
1992
|
-
}
|
|
1993
|
-
return () => {
|
|
1994
|
-
if (phase === "before") this.globalBeforeJobs.delete(id);
|
|
1995
|
-
else this.globalAfterJobs.delete(id);
|
|
1996
|
-
};
|
|
1997
|
-
}
|
|
1998
|
-
//* Idle Callback Methods (Deprecated API) ================================
|
|
1999
|
-
/**
|
|
2000
|
-
* Register an idle callback that fires when the loop stops.
|
|
2001
|
-
* Used internally by deprecated addTail API.
|
|
2002
|
-
* @param {(timestamp: number) => void} callback - Function called when loop becomes idle
|
|
2003
|
-
* @returns {() => void} Unsubscribe function to remove this idle callback
|
|
2004
|
-
* @deprecated Use demand mode with invalidate() instead
|
|
2005
|
-
*/
|
|
2006
|
-
onIdle(callback) {
|
|
2007
|
-
this.idleCallbacks.add(callback);
|
|
2008
|
-
return () => this.idleCallbacks.delete(callback);
|
|
2009
|
-
}
|
|
2010
|
-
/**
|
|
2011
|
-
* Notify all registered idle callbacks.
|
|
2012
|
-
* Called when the loop stops in demand mode.
|
|
2013
|
-
* @param {number} timestamp - The RAF timestamp when idle occurred
|
|
2014
|
-
* @returns {void}
|
|
2015
|
-
* @private
|
|
2016
|
-
*/
|
|
2017
|
-
notifyIdle(timestamp) {
|
|
2018
|
-
for (const cb of this.idleCallbacks) {
|
|
2019
|
-
try {
|
|
2020
|
-
cb(timestamp);
|
|
2021
|
-
} catch (error) {
|
|
2022
|
-
console.error("[Scheduler] Error in idle callback:", error);
|
|
2023
|
-
}
|
|
2024
|
-
}
|
|
2025
|
-
}
|
|
2026
|
-
//* Job Registration & Management Methods ================================
|
|
2027
|
-
/**
|
|
2028
|
-
* Register a job (frame callback) with a specific root.
|
|
2029
|
-
* This is the core registration method used by useFrame internally.
|
|
2030
|
-
* @param {FrameNextCallback} callback - The function to call each frame
|
|
2031
|
-
* @param {JobOptions & { rootId?: string; system?: boolean }} [options] - Job configuration
|
|
2032
|
-
* @param {string} [options.rootId] - Target root ID (defaults to first registered root)
|
|
2033
|
-
* @param {string} [options.id] - Unique job ID (auto-generated if not provided)
|
|
2034
|
-
* @param {string} [options.phase] - Execution phase (defaults to 'update')
|
|
2035
|
-
* @param {number} [options.priority] - Priority within phase (higher = earlier, default 0)
|
|
2036
|
-
* @param {number} [options.fps] - FPS throttle limit
|
|
2037
|
-
* @param {boolean} [options.drop] - Drop frames when behind (default true)
|
|
2038
|
-
* @param {boolean} [options.enabled] - Whether job is active (default true)
|
|
2039
|
-
* @param {boolean} [options.system] - Internal flag for system jobs (not user-facing)
|
|
2040
|
-
* @returns {() => void} Unsubscribe function to remove this job
|
|
2041
|
-
*/
|
|
2042
|
-
register(callback, options = {}) {
|
|
2043
|
-
const rootId = options.rootId;
|
|
2044
|
-
const root = rootId ? this.roots.get(rootId) : this.roots.values().next().value;
|
|
2045
|
-
if (!root) {
|
|
2046
|
-
console.warn("[Scheduler] No root registered. Is this inside a Canvas?");
|
|
2047
|
-
return () => {
|
|
2048
|
-
};
|
|
2049
|
-
}
|
|
2050
|
-
const id = options.id ?? this.generateJobId();
|
|
2051
|
-
let phase = options.phase ?? "update";
|
|
2052
|
-
if (!options.phase && (options.before || options.after)) {
|
|
2053
|
-
phase = this.phaseGraph.resolveConstraintPhase(options.before, options.after);
|
|
2054
|
-
}
|
|
2055
|
-
const before = this.normalizeConstraints(options.before);
|
|
2056
|
-
const after = this.normalizeConstraints(options.after);
|
|
2057
|
-
const job = {
|
|
2058
|
-
id,
|
|
2059
|
-
callback,
|
|
2060
|
-
phase,
|
|
2061
|
-
before,
|
|
2062
|
-
after,
|
|
2063
|
-
priority: options.priority ?? 0,
|
|
2064
|
-
index: this.nextJobIndex++,
|
|
2065
|
-
fps: options.fps,
|
|
2066
|
-
drop: options.drop ?? true,
|
|
2067
|
-
enabled: options.enabled ?? true,
|
|
2068
|
-
system: options.system ?? false
|
|
2069
|
-
};
|
|
2070
|
-
if (root.jobs.has(id)) {
|
|
2071
|
-
console.warn(`[useFrame] Job with id "${id}" already exists, replacing`);
|
|
2072
|
-
}
|
|
2073
|
-
root.jobs.set(id, job);
|
|
2074
|
-
root.needsRebuild = true;
|
|
2075
|
-
return () => this.unregister(id, root.id);
|
|
2076
|
-
}
|
|
2077
|
-
/**
|
|
2078
|
-
* Unregister a job by its ID.
|
|
2079
|
-
* Searches all roots if rootId is not provided.
|
|
2080
|
-
* @param {string} id - The job ID to unregister
|
|
2081
|
-
* @param {string} [rootId] - Optional root ID to search (searches all if not provided)
|
|
2082
|
-
* @returns {void}
|
|
2083
|
-
*/
|
|
2084
|
-
unregister(id, rootId) {
|
|
2085
|
-
const root = rootId ? this.roots.get(rootId) : Array.from(this.roots.values()).find((r) => r.jobs.has(id));
|
|
2086
|
-
if (root?.jobs.delete(id)) {
|
|
2087
|
-
root.needsRebuild = true;
|
|
2088
|
-
this.jobStateListeners.delete(id);
|
|
2089
|
-
}
|
|
2090
|
-
}
|
|
2091
|
-
/**
|
|
2092
|
-
* Update a job's options dynamically.
|
|
2093
|
-
* Searches all roots to find the job by ID.
|
|
2094
|
-
* Phase/constraint changes trigger a rebuild of the sorted job list.
|
|
2095
|
-
* @param {string} id - The job ID to update
|
|
2096
|
-
* @param {Partial<JobOptions>} options - The options to update
|
|
2097
|
-
* @returns {void}
|
|
2098
|
-
*/
|
|
2099
|
-
updateJob(id, options) {
|
|
2100
|
-
let job;
|
|
2101
|
-
let root;
|
|
2102
|
-
for (const r of this.roots.values()) {
|
|
2103
|
-
job = r.jobs.get(id);
|
|
2104
|
-
if (job) {
|
|
2105
|
-
root = r;
|
|
2106
|
-
break;
|
|
2107
|
-
}
|
|
2108
|
-
}
|
|
2109
|
-
if (!job || !root) return;
|
|
2110
|
-
if (options.priority !== void 0) job.priority = options.priority;
|
|
2111
|
-
if (options.fps !== void 0) job.fps = options.fps;
|
|
2112
|
-
if (options.drop !== void 0) job.drop = options.drop;
|
|
2113
|
-
if (options.enabled !== void 0) {
|
|
2114
|
-
const wasEnabled = job.enabled;
|
|
2115
|
-
job.enabled = options.enabled;
|
|
2116
|
-
if (!wasEnabled && job.enabled) resetJobTiming(job);
|
|
2117
|
-
if (wasEnabled !== job.enabled) root.needsRebuild = true;
|
|
2118
|
-
}
|
|
2119
|
-
if (options.phase !== void 0 || options.before !== void 0 || options.after !== void 0) {
|
|
2120
|
-
if (options.phase) job.phase = options.phase;
|
|
2121
|
-
if (options.before !== void 0) job.before = this.normalizeConstraints(options.before);
|
|
2122
|
-
if (options.after !== void 0) job.after = this.normalizeConstraints(options.after);
|
|
2123
|
-
root.needsRebuild = true;
|
|
2124
|
-
}
|
|
2125
|
-
}
|
|
2126
|
-
//* Job State Management Methods ================================
|
|
2127
|
-
/**
|
|
2128
|
-
* Check if a job is currently paused (disabled).
|
|
2129
|
-
* @param {string} id - The job ID to check
|
|
2130
|
-
* @returns {boolean} True if the job exists and is paused
|
|
2131
|
-
*/
|
|
2132
|
-
isJobPaused(id) {
|
|
2133
|
-
for (const root of this.roots.values()) {
|
|
2134
|
-
const job = root.jobs.get(id);
|
|
2135
|
-
if (job) return !job.enabled;
|
|
2136
|
-
}
|
|
2137
|
-
return false;
|
|
2138
|
-
}
|
|
2139
|
-
/**
|
|
2140
|
-
* Subscribe to state changes for a specific job.
|
|
2141
|
-
* Listener is called when job is paused or resumed.
|
|
2142
|
-
* @param {string} id - The job ID to subscribe to
|
|
2143
|
-
* @param {() => void} listener - Callback invoked on state changes
|
|
2144
|
-
* @returns {() => void} Unsubscribe function
|
|
2145
|
-
*/
|
|
2146
|
-
subscribeJobState(id, listener) {
|
|
2147
|
-
if (!this.jobStateListeners.has(id)) {
|
|
2148
|
-
this.jobStateListeners.set(id, /* @__PURE__ */ new Set());
|
|
2149
|
-
}
|
|
2150
|
-
this.jobStateListeners.get(id).add(listener);
|
|
2151
|
-
return () => {
|
|
2152
|
-
this.jobStateListeners.get(id)?.delete(listener);
|
|
2153
|
-
if (this.jobStateListeners.get(id)?.size === 0) {
|
|
2154
|
-
this.jobStateListeners.delete(id);
|
|
2155
|
-
}
|
|
2156
|
-
};
|
|
2157
|
-
}
|
|
2158
|
-
/**
|
|
2159
|
-
* Notify all listeners that a job's state has changed.
|
|
2160
|
-
* @param {string} id - The job ID that changed
|
|
2161
|
-
* @returns {void}
|
|
2162
|
-
* @private
|
|
2163
|
-
*/
|
|
2164
|
-
notifyJobStateChange(id) {
|
|
2165
|
-
this.jobStateListeners.get(id)?.forEach((listener) => listener());
|
|
2166
|
-
}
|
|
2167
|
-
/**
|
|
2168
|
-
* Pause a job by ID (sets enabled=false).
|
|
2169
|
-
* Notifies any subscribed state listeners.
|
|
2170
|
-
* @param {string} id - The job ID to pause
|
|
2171
|
-
* @returns {void}
|
|
2172
|
-
*/
|
|
2173
|
-
pauseJob(id) {
|
|
2174
|
-
this.updateJob(id, { enabled: false });
|
|
2175
|
-
this.notifyJobStateChange(id);
|
|
2176
|
-
}
|
|
2177
|
-
/**
|
|
2178
|
-
* Resume a paused job by ID (sets enabled=true).
|
|
2179
|
-
* Resets job timing to prevent frame accumulation.
|
|
2180
|
-
* Notifies any subscribed state listeners.
|
|
2181
|
-
* @param {string} id - The job ID to resume
|
|
2182
|
-
* @returns {void}
|
|
2183
|
-
*/
|
|
2184
|
-
resumeJob(id) {
|
|
2185
|
-
this.updateJob(id, { enabled: true });
|
|
2186
|
-
this.notifyJobStateChange(id);
|
|
2187
|
-
}
|
|
2188
|
-
//* Frame Loop Control Methods ================================
|
|
2189
|
-
/**
|
|
2190
|
-
* Start the requestAnimationFrame loop.
|
|
2191
|
-
* Resets timing state (elapsedTime, frameCount) on start.
|
|
2192
|
-
* No-op if already running.
|
|
2193
|
-
* @returns {void}
|
|
2194
|
-
*/
|
|
2195
|
-
start() {
|
|
2196
|
-
if (this.loopState.running) return;
|
|
2197
|
-
const { elapsedTime, createdAt } = this.loopState;
|
|
2198
|
-
let adjustedCreated = 0;
|
|
2199
|
-
if (this.stoppedTime > 0) {
|
|
2200
|
-
adjustedCreated = createdAt - (performance.now() - this.stoppedTime);
|
|
2201
|
-
this.stoppedTime = 0;
|
|
2202
|
-
}
|
|
2203
|
-
Object.assign(this.loopState, {
|
|
2204
|
-
running: true,
|
|
2205
|
-
elapsedTime: elapsedTime ?? 0,
|
|
2206
|
-
lastTime: performance.now(),
|
|
2207
|
-
createdAt: adjustedCreated > 0 ? adjustedCreated : performance.now(),
|
|
2208
|
-
frameCount: 0,
|
|
2209
|
-
rafHandle: requestAnimationFrame(this.loop)
|
|
2210
|
-
});
|
|
2211
|
-
}
|
|
2212
|
-
/**
|
|
2213
|
-
* Stop the requestAnimationFrame loop.
|
|
2214
|
-
* Cancels any pending RAF callback.
|
|
2215
|
-
* No-op if not running.
|
|
2216
|
-
* @returns {void}
|
|
2217
|
-
*/
|
|
2218
|
-
stop() {
|
|
2219
|
-
if (!this.loopState.running) return;
|
|
2220
|
-
this.loopState.running = false;
|
|
2221
|
-
if (this.loopState.rafHandle !== null) {
|
|
2222
|
-
cancelAnimationFrame(this.loopState.rafHandle);
|
|
2223
|
-
this.loopState.rafHandle = null;
|
|
2224
|
-
}
|
|
2225
|
-
this.stoppedTime = performance.now();
|
|
2226
|
-
}
|
|
2227
|
-
/**
|
|
2228
|
-
* Request frames to be rendered in demand mode.
|
|
2229
|
-
* Accumulates pending frames (capped at 60) and starts the loop if not running.
|
|
2230
|
-
* No-op if frameloop is not 'demand'.
|
|
2231
|
-
* @param {number} [frames=1] - Number of frames to request
|
|
2232
|
-
* @param {boolean} [stackFrames=false] - Whether to add frames to existing pending count
|
|
2233
|
-
* - `false` (default): Sets pending frames to the specified value (replaces existing count)
|
|
2234
|
-
* - `true`: Adds frames to existing pending count (useful for accumulating invalidations)
|
|
2235
|
-
* @returns {void}
|
|
2236
|
-
* @example
|
|
2237
|
-
* // Request a single frame render
|
|
2238
|
-
* scheduler.invalidate();
|
|
2239
|
-
*
|
|
2240
|
-
* @example
|
|
2241
|
-
* // Request 5 frames (e.g., for animations)
|
|
2242
|
-
* scheduler.invalidate(5);
|
|
2243
|
-
*
|
|
2244
|
-
* @example
|
|
2245
|
-
* // Set pending frames to exactly 3 (don't stack with existing)
|
|
2246
|
-
* scheduler.invalidate(3, false);
|
|
2247
|
-
*
|
|
2248
|
-
* @example
|
|
2249
|
-
* // Add 2 more frames to existing pending count
|
|
2250
|
-
* scheduler.invalidate(2, true);
|
|
2251
|
-
*/
|
|
2252
|
-
invalidate(frames = 1, stackFrames = false) {
|
|
2253
|
-
if (this._frameloop !== "demand") return;
|
|
2254
|
-
const baseFrames = stackFrames ? this.pendingFrames : 0;
|
|
2255
|
-
this.pendingFrames = Math.min(60, baseFrames + frames);
|
|
2256
|
-
if (!this.loopState.running && this.pendingFrames > 0) this.start();
|
|
2257
|
-
}
|
|
2258
|
-
/**
|
|
2259
|
-
* Reset timing state for deterministic testing.
|
|
2260
|
-
* Preserves jobs and roots but resets lastTime, frameCount, elapsedTime, etc.
|
|
2261
|
-
* @returns {void}
|
|
2262
|
-
*/
|
|
2263
|
-
resetTiming() {
|
|
2264
|
-
this.loopState.lastTime = null;
|
|
2265
|
-
this.loopState.frameCount = 0;
|
|
2266
|
-
this.loopState.elapsedTime = 0;
|
|
2267
|
-
this.loopState.createdAt = performance.now();
|
|
2268
|
-
}
|
|
2269
|
-
//* Manual Stepping Methods ================================
|
|
2270
|
-
/**
|
|
2271
|
-
* Manually execute a single frame for all roots.
|
|
2272
|
-
* Useful for frameloop='never' mode or testing scenarios.
|
|
2273
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2274
|
-
* @returns {void}
|
|
2275
|
-
* @example
|
|
2276
|
-
* // Manual control mode
|
|
2277
|
-
* scheduler.frameloop = 'never';
|
|
2278
|
-
* scheduler.step(); // Execute one frame
|
|
2279
|
-
*/
|
|
2280
|
-
step(timestamp) {
|
|
2281
|
-
const now = timestamp ?? performance.now();
|
|
2282
|
-
this.executeFrame(now);
|
|
2283
|
-
}
|
|
2284
|
-
/**
|
|
2285
|
-
* Manually execute a single job by its ID.
|
|
2286
|
-
* Useful for testing individual job callbacks in isolation.
|
|
2287
|
-
* @param {string} id - The job ID to step
|
|
2288
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2289
|
-
* @returns {void}
|
|
2290
|
-
*/
|
|
2291
|
-
stepJob(id, timestamp) {
|
|
2292
|
-
let job;
|
|
2293
|
-
let root;
|
|
2294
|
-
for (const r of this.roots.values()) {
|
|
2295
|
-
job = r.jobs.get(id);
|
|
2296
|
-
if (job) {
|
|
2297
|
-
root = r;
|
|
2298
|
-
break;
|
|
2299
|
-
}
|
|
2300
|
-
}
|
|
2301
|
-
if (!job || !root) {
|
|
2302
|
-
console.warn(`[Scheduler] Job "${id}" not found`);
|
|
2303
|
-
return;
|
|
2304
|
-
}
|
|
2305
|
-
const now = timestamp ?? performance.now();
|
|
2306
|
-
const deltaMs = this.loopState.lastTime !== null ? now - this.loopState.lastTime : 0;
|
|
2307
|
-
const delta = deltaMs / 1e3;
|
|
2308
|
-
const elapsed = now - this.loopState.createdAt;
|
|
2309
|
-
const providedState = root.getState?.() ?? {};
|
|
2310
|
-
const frameState = {
|
|
2311
|
-
...providedState,
|
|
2312
|
-
time: now,
|
|
2313
|
-
delta,
|
|
2314
|
-
elapsed,
|
|
2315
|
-
frame: this.loopState.frameCount
|
|
2316
|
-
};
|
|
2317
|
-
try {
|
|
2318
|
-
job.callback(frameState, delta);
|
|
2319
|
-
} catch (error) {
|
|
2320
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2321
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2322
|
-
}
|
|
2323
|
-
}
|
|
2324
|
-
/**
|
|
2325
|
-
* Execute a single frame across all roots.
|
|
2326
|
-
* Order: globalBefore → each root's jobs → globalAfter
|
|
2327
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2328
|
-
* @returns {void}
|
|
2329
|
-
* @private
|
|
2330
|
-
*/
|
|
2331
|
-
executeFrame(timestamp) {
|
|
2332
|
-
const deltaMs = this.loopState.lastTime !== null ? timestamp - this.loopState.lastTime : 0;
|
|
2333
|
-
const delta = deltaMs / 1e3;
|
|
2334
|
-
this.loopState.lastTime = timestamp;
|
|
2335
|
-
this.loopState.frameCount++;
|
|
2336
|
-
this.loopState.elapsedTime += deltaMs;
|
|
2337
|
-
this.runGlobalJobs(this.globalBeforeJobs, timestamp);
|
|
2338
|
-
for (const root of this.roots.values()) {
|
|
2339
|
-
this.tickRoot(root, timestamp, delta);
|
|
2340
|
-
}
|
|
2341
|
-
this.runGlobalJobs(this.globalAfterJobs, timestamp);
|
|
2342
|
-
}
|
|
2343
|
-
/**
|
|
2344
|
-
* Run all global jobs from a job map.
|
|
2345
|
-
* Catches and logs errors without stopping execution.
|
|
2346
|
-
* @param {Map<string, GlobalJob>} jobs - The global jobs map to execute
|
|
2347
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2348
|
-
* @returns {void}
|
|
2349
|
-
* @private
|
|
2350
|
-
*/
|
|
2351
|
-
runGlobalJobs(jobs, timestamp) {
|
|
2352
|
-
for (const job of jobs.values()) {
|
|
2353
|
-
try {
|
|
2354
|
-
job.callback(timestamp);
|
|
2355
|
-
} catch (error) {
|
|
2356
|
-
console.error(`[Scheduler] Error in global job "${job.id}":`, error);
|
|
2357
|
-
}
|
|
2358
|
-
}
|
|
2359
|
-
}
|
|
2360
|
-
/**
|
|
2361
|
-
* Execute all jobs for a single root in sorted order.
|
|
2362
|
-
* Rebuilds sorted job list if needed, then dispatches each job.
|
|
2363
|
-
* Errors are caught and propagated via triggerError.
|
|
2364
|
-
* @param {RootEntry} root - The root entry to tick
|
|
2365
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2366
|
-
* @param {number} delta - Time since last frame in seconds
|
|
2367
|
-
* @returns {void}
|
|
2368
|
-
* @private
|
|
2369
|
-
*/
|
|
2370
|
-
tickRoot(root, timestamp, delta) {
|
|
2371
|
-
if (root.needsRebuild) {
|
|
2372
|
-
root.sortedJobs = rebuildSortedJobs(root.jobs, this.phaseGraph);
|
|
2373
|
-
root.needsRebuild = false;
|
|
2374
|
-
}
|
|
2375
|
-
const providedState = root.getState?.() ?? {};
|
|
2376
|
-
const frameState = {
|
|
2377
|
-
...providedState,
|
|
2378
|
-
time: timestamp,
|
|
2379
|
-
delta,
|
|
2380
|
-
elapsed: this.loopState.elapsedTime / 1e3,
|
|
2381
|
-
// Convert ms to seconds
|
|
2382
|
-
frame: this.loopState.frameCount
|
|
2383
|
-
};
|
|
2384
|
-
for (const job of root.sortedJobs) {
|
|
2385
|
-
if (!shouldRun(job, timestamp)) continue;
|
|
2386
|
-
try {
|
|
2387
|
-
job.callback(frameState, delta);
|
|
2388
|
-
} catch (error) {
|
|
2389
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2390
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2391
|
-
}
|
|
2392
|
-
}
|
|
2393
|
-
}
|
|
2394
|
-
//* Debug & Inspection Methods ================================
|
|
2395
|
-
/**
|
|
2396
|
-
* Get the total number of registered jobs across all roots.
|
|
2397
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2398
|
-
* @returns {number} Total job count
|
|
2399
|
-
*/
|
|
2400
|
-
getJobCount() {
|
|
2401
|
-
let count = 0;
|
|
2402
|
-
for (const root of this.roots.values()) {
|
|
2403
|
-
count += root.jobs.size;
|
|
2404
|
-
}
|
|
2405
|
-
return count + this.globalBeforeJobs.size + this.globalAfterJobs.size;
|
|
2406
|
-
}
|
|
2407
|
-
/**
|
|
2408
|
-
* Get all registered job IDs across all roots.
|
|
2409
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2410
|
-
* @returns {string[]} Array of all job IDs
|
|
2411
|
-
*/
|
|
2412
|
-
getJobIds() {
|
|
2413
|
-
const ids = [];
|
|
2414
|
-
for (const root of this.roots.values()) {
|
|
2415
|
-
ids.push(...root.jobs.keys());
|
|
2416
|
-
}
|
|
2417
|
-
ids.push(...this.globalBeforeJobs.keys());
|
|
2418
|
-
ids.push(...this.globalAfterJobs.keys());
|
|
2419
|
-
return ids;
|
|
2420
|
-
}
|
|
2421
|
-
/**
|
|
2422
|
-
* Get the number of registered roots (Canvas instances).
|
|
2423
|
-
* @returns {number} Number of registered roots
|
|
2424
|
-
*/
|
|
2425
|
-
getRootCount() {
|
|
2426
|
-
return this.roots.size;
|
|
2427
|
-
}
|
|
2428
|
-
/**
|
|
2429
|
-
* Check if any user (non-system) jobs are registered in a specific phase.
|
|
2430
|
-
* Used by the default render job to know if a user has taken over rendering.
|
|
2431
|
-
*
|
|
2432
|
-
* @param phase The phase to check
|
|
2433
|
-
* @param rootId Optional root ID to check (checks all roots if not provided)
|
|
2434
|
-
* @returns true if any user jobs exist in the phase
|
|
2435
|
-
*/
|
|
2436
|
-
hasUserJobsInPhase(phase, rootId) {
|
|
2437
|
-
const rootsToCheck = rootId ? [this.roots.get(rootId)].filter(Boolean) : Array.from(this.roots.values());
|
|
2438
|
-
return rootsToCheck.some((root) => {
|
|
2439
|
-
if (!root) return false;
|
|
2440
|
-
for (const job of root.jobs.values()) {
|
|
2441
|
-
if (job.phase === phase && !job.system && job.enabled) return true;
|
|
2442
|
-
}
|
|
2443
|
-
return false;
|
|
2444
|
-
});
|
|
2445
|
-
}
|
|
2446
|
-
//* Utility Methods ================================
|
|
2447
|
-
/**
|
|
2448
|
-
* Generate a unique root ID for automatic root registration.
|
|
2449
|
-
* @returns {string} A unique root ID in the format 'root_N'
|
|
2450
|
-
*/
|
|
2451
|
-
generateRootId() {
|
|
2452
|
-
return `root_${this.nextRootIndex++}`;
|
|
2453
|
-
}
|
|
2454
|
-
/**
|
|
2455
|
-
* Generate a unique job ID.
|
|
2456
|
-
* @returns {string} A unique job ID in the format 'job_N'
|
|
2457
|
-
* @private
|
|
2458
|
-
*/
|
|
2459
|
-
generateJobId() {
|
|
2460
|
-
return `job_${this.nextJobIndex}`;
|
|
2461
|
-
}
|
|
2462
|
-
/**
|
|
2463
|
-
* Normalize before/after constraints to a Set.
|
|
2464
|
-
* Handles undefined, single string, or array inputs.
|
|
2465
|
-
* @param {string | string[] | undefined} value - The constraint value(s)
|
|
2466
|
-
* @returns {Set<string>} Normalized Set of constraint strings
|
|
2467
|
-
* @private
|
|
2468
|
-
*/
|
|
2469
|
-
normalizeConstraints(value) {
|
|
2470
|
-
if (!value) return /* @__PURE__ */ new Set();
|
|
2471
|
-
if (Array.isArray(value)) return new Set(value);
|
|
2472
|
-
return /* @__PURE__ */ new Set([value]);
|
|
2473
|
-
}
|
|
2474
|
-
};
|
|
2475
|
-
//* Static State & Methods (Singleton Usage) ================================
|
|
2476
|
-
//* Cross-Bundle Singleton Key ==============================
|
|
2477
|
-
// Use Symbol.for() to ensure scheduler is shared across bundle boundaries
|
|
2478
|
-
// This prevents issues when mixing imports from @react-three/fiber and @react-three/fiber/webgpu
|
|
2479
|
-
__publicField(_Scheduler, "INSTANCE_KEY", Symbol.for("@react-three/fiber.scheduler"));
|
|
2480
|
-
let Scheduler = _Scheduler;
|
|
2481
|
-
const getScheduler = () => Scheduler.get();
|
|
2482
|
-
if (hmrData) {
|
|
2483
|
-
hmrData.accept?.();
|
|
1681
|
+
const INTERNAL_LIB_MARKERS = ["zustand", "setState", "Object.assign"];
|
|
1682
|
+
function isInternalRendererAccess(stack, callerDepth = 2) {
|
|
1683
|
+
if (!stack) return false;
|
|
1684
|
+
const caller = stack.split("\n")[callerDepth] ?? "";
|
|
1685
|
+
return INTERNAL_LIB_MARKERS.some((m) => caller.includes(m));
|
|
2484
1686
|
}
|
|
2485
1687
|
|
|
2486
1688
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
@@ -2520,7 +1722,14 @@ const createStore = (invalidate, advance) => {
|
|
|
2520
1722
|
frustum: new webgpu.Frustum(),
|
|
2521
1723
|
autoUpdateFrustum: true,
|
|
2522
1724
|
raycaster: null,
|
|
2523
|
-
events: {
|
|
1725
|
+
events: {
|
|
1726
|
+
priority: 1,
|
|
1727
|
+
enabled: true,
|
|
1728
|
+
connected: false,
|
|
1729
|
+
frameTimedRaycasts: true,
|
|
1730
|
+
alwaysFireOnScroll: true,
|
|
1731
|
+
updateOnFrame: false
|
|
1732
|
+
},
|
|
2524
1733
|
scene: null,
|
|
2525
1734
|
rootScene: null,
|
|
2526
1735
|
xr: null,
|
|
@@ -2585,7 +1794,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2585
1794
|
size: newSize,
|
|
2586
1795
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
2587
1796
|
}));
|
|
2588
|
-
getScheduler().invalidate();
|
|
1797
|
+
scheduler.getScheduler().invalidate();
|
|
2589
1798
|
}
|
|
2590
1799
|
}
|
|
2591
1800
|
return;
|
|
@@ -2600,7 +1809,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2600
1809
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
2601
1810
|
_sizeImperative: true
|
|
2602
1811
|
}));
|
|
2603
|
-
getScheduler().invalidate();
|
|
1812
|
+
scheduler.getScheduler().invalidate();
|
|
2604
1813
|
},
|
|
2605
1814
|
setDpr: (dpr) => set((state2) => {
|
|
2606
1815
|
const resolved = calculateDpr(dpr);
|
|
@@ -2608,14 +1817,18 @@ const createStore = (invalidate, advance) => {
|
|
|
2608
1817
|
}),
|
|
2609
1818
|
setFrameloop: (frameloop = "always") => {
|
|
2610
1819
|
set(() => ({ frameloop }));
|
|
1820
|
+
scheduler.getScheduler().frameloop = frameloop;
|
|
2611
1821
|
},
|
|
2612
1822
|
setError: (error) => set(() => ({ error })),
|
|
2613
1823
|
error: null,
|
|
2614
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useTextures,
|
|
1824
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
2615
1825
|
uniforms: {},
|
|
2616
1826
|
nodes: {},
|
|
1827
|
+
buffers: {},
|
|
1828
|
+
gpuStorage: {},
|
|
2617
1829
|
textures: /* @__PURE__ */ new Map(),
|
|
2618
|
-
|
|
1830
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1831
|
+
renderPipeline: null,
|
|
2619
1832
|
passes: {},
|
|
2620
1833
|
_hmrVersion: 0,
|
|
2621
1834
|
_sizeImperative: false,
|
|
@@ -2624,12 +1837,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2624
1837
|
internal: {
|
|
2625
1838
|
// Events
|
|
2626
1839
|
interaction: [],
|
|
2627
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
2628
1840
|
subscribers: [],
|
|
1841
|
+
// Per-pointer state (new unified structure)
|
|
1842
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1843
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1844
|
+
lastEvent: React__namespace.createRef(),
|
|
1845
|
+
// Deprecated but kept for backwards compatibility
|
|
1846
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
2629
1847
|
initialClick: [0, 0],
|
|
2630
1848
|
initialHits: [],
|
|
2631
1849
|
capturedMap: /* @__PURE__ */ new Map(),
|
|
2632
|
-
lastEvent: React__namespace.createRef(),
|
|
2633
1850
|
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
2634
1851
|
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
2635
1852
|
// Occlusion system (WebGPU only)
|
|
@@ -2668,8 +1885,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2668
1885
|
const currentState = rootStore.getState();
|
|
2669
1886
|
if (!currentState.isLegacy && currentState.internal.actualRenderer) {
|
|
2670
1887
|
const stack = new Error().stack || "";
|
|
2671
|
-
|
|
2672
|
-
if (!isInternalAccess) {
|
|
1888
|
+
if (!isInternalRendererAccess(stack)) {
|
|
2673
1889
|
const cleanedStack = stack.split("\n").slice(2).join("\n") || "Stack trace unavailable";
|
|
2674
1890
|
notifyDepreciated({
|
|
2675
1891
|
heading: "Accessing state.gl in WebGPU mode",
|
|
@@ -2717,14 +1933,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2717
1933
|
oldSize = size;
|
|
2718
1934
|
oldDpr = viewport.dpr;
|
|
2719
1935
|
updateCamera(camera, size);
|
|
2720
|
-
if (canvasTarget) {
|
|
1936
|
+
if (internal.isSecondary && canvasTarget) {
|
|
2721
1937
|
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
2722
|
-
|
|
2723
|
-
canvasTarget.setSize(size.width, size.height, updateStyle);
|
|
1938
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
2724
1939
|
} else {
|
|
2725
1940
|
if (viewport.dpr > 0) actualRenderer.setPixelRatio(viewport.dpr);
|
|
2726
|
-
|
|
2727
|
-
|
|
1941
|
+
actualRenderer.setSize(size.width, size.height, false);
|
|
1942
|
+
if (canvasTarget) {
|
|
1943
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1944
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1945
|
+
}
|
|
2728
1946
|
}
|
|
2729
1947
|
}
|
|
2730
1948
|
if (camera !== oldCamera) {
|
|
@@ -2780,26 +1998,32 @@ function loadingFn(extensions, onProgress) {
|
|
|
2780
1998
|
);
|
|
2781
1999
|
};
|
|
2782
2000
|
}
|
|
2783
|
-
function useLoader(loader, input, extensions, onProgress) {
|
|
2784
|
-
const
|
|
2001
|
+
function useLoader(loader, input, extensions, onProgress, cacheKey) {
|
|
2002
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
2003
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2785
2004
|
const fn = loadingFn(extensions, onProgress);
|
|
2786
|
-
const results =
|
|
2005
|
+
const results = cacheKeys.map(
|
|
2006
|
+
(key, index) => suspendReact.suspend(() => fn(loader, inputs[index]), [loader, key], { equal: is.equ })
|
|
2007
|
+
);
|
|
2787
2008
|
return Array.isArray(input) ? results : results[0];
|
|
2788
2009
|
}
|
|
2789
|
-
useLoader.preload = function(loader, input, extensions, onProgress) {
|
|
2790
|
-
const
|
|
2791
|
-
|
|
2010
|
+
useLoader.preload = function(loader, input, extensions, onProgress, cacheKey) {
|
|
2011
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
2012
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2013
|
+
cacheKeys.forEach(
|
|
2014
|
+
(key, index) => suspendReact.preload(() => loadingFn(extensions, onProgress)(loader, inputs[index]), [loader, key])
|
|
2015
|
+
);
|
|
2792
2016
|
};
|
|
2793
|
-
useLoader.clear = function(loader, input) {
|
|
2794
|
-
const
|
|
2795
|
-
|
|
2017
|
+
useLoader.clear = function(loader, input, cacheKey) {
|
|
2018
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2019
|
+
cacheKeys.forEach((key) => suspendReact.clear([loader, key]));
|
|
2796
2020
|
};
|
|
2797
2021
|
useLoader.loader = getLoader;
|
|
2798
2022
|
|
|
2799
2023
|
function useFrame(callback, priorityOrOptions) {
|
|
2800
2024
|
const store = React__namespace.useContext(context);
|
|
2801
2025
|
const isInsideCanvas = store !== null;
|
|
2802
|
-
const scheduler = getScheduler();
|
|
2026
|
+
const scheduler$1 = scheduler.getScheduler();
|
|
2803
2027
|
const optionsKey = typeof priorityOrOptions === "number" ? `p:${priorityOrOptions}` : priorityOrOptions ? JSON.stringify({
|
|
2804
2028
|
id: priorityOrOptions.id,
|
|
2805
2029
|
phase: priorityOrOptions.phase,
|
|
@@ -2847,7 +2071,7 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2847
2071
|
};
|
|
2848
2072
|
callbackRef.current?.(mergedState, delta);
|
|
2849
2073
|
};
|
|
2850
|
-
const unregister = scheduler.register(wrappedCallback, {
|
|
2074
|
+
const unregister = scheduler$1.register(wrappedCallback, {
|
|
2851
2075
|
id,
|
|
2852
2076
|
rootId,
|
|
2853
2077
|
...options
|
|
@@ -2868,37 +2092,31 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2868
2092
|
}
|
|
2869
2093
|
};
|
|
2870
2094
|
} else {
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
unregisterJob = registerOutside();
|
|
2880
|
-
});
|
|
2881
|
-
return () => {
|
|
2882
|
-
unsubReady();
|
|
2883
|
-
unregisterJob?.();
|
|
2884
|
-
};
|
|
2095
|
+
return scheduler$1.register(
|
|
2096
|
+
(state, delta) => {
|
|
2097
|
+
const frameState = state;
|
|
2098
|
+
if (!frameState.renderer) return;
|
|
2099
|
+
callbackRef.current?.(frameState, delta);
|
|
2100
|
+
},
|
|
2101
|
+
{ id, ...options }
|
|
2102
|
+
);
|
|
2885
2103
|
}
|
|
2886
|
-
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2104
|
+
}, [store, scheduler$1, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2887
2105
|
const isPaused = React__namespace.useSyncExternalStore(
|
|
2888
2106
|
// Subscribe function
|
|
2889
2107
|
React__namespace.useCallback(
|
|
2890
2108
|
(onStoreChange) => {
|
|
2891
|
-
return getScheduler().subscribeJobState(id, onStoreChange);
|
|
2109
|
+
return scheduler.getScheduler().subscribeJobState(id, onStoreChange);
|
|
2892
2110
|
},
|
|
2893
2111
|
[id]
|
|
2894
2112
|
),
|
|
2895
2113
|
// getSnapshot function
|
|
2896
|
-
React__namespace.useCallback(() => getScheduler().isJobPaused(id), [id]),
|
|
2114
|
+
React__namespace.useCallback(() => scheduler.getScheduler().isJobPaused(id), [id]),
|
|
2897
2115
|
// getServerSnapshot function (SSR)
|
|
2898
2116
|
React__namespace.useCallback(() => false, [])
|
|
2899
2117
|
);
|
|
2900
2118
|
const controls = React__namespace.useMemo(() => {
|
|
2901
|
-
const scheduler2 = getScheduler();
|
|
2119
|
+
const scheduler2 = scheduler.getScheduler();
|
|
2902
2120
|
return {
|
|
2903
2121
|
/** The job's unique ID */
|
|
2904
2122
|
id,
|
|
@@ -2913,7 +2131,7 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2913
2131
|
* @param timestamp Optional timestamp (defaults to performance.now())
|
|
2914
2132
|
*/
|
|
2915
2133
|
step: (timestamp) => {
|
|
2916
|
-
getScheduler().stepJob(id, timestamp);
|
|
2134
|
+
scheduler.getScheduler().stepJob(id, timestamp);
|
|
2917
2135
|
},
|
|
2918
2136
|
/**
|
|
2919
2137
|
* Manually step ALL jobs in the scheduler.
|
|
@@ -2921,20 +2139,20 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2921
2139
|
* @param timestamp Optional timestamp (defaults to performance.now())
|
|
2922
2140
|
*/
|
|
2923
2141
|
stepAll: (timestamp) => {
|
|
2924
|
-
getScheduler().step(timestamp);
|
|
2142
|
+
scheduler.getScheduler().step(timestamp);
|
|
2925
2143
|
},
|
|
2926
2144
|
/**
|
|
2927
2145
|
* Pause this job (set enabled=false).
|
|
2928
2146
|
* Job remains registered but won't run.
|
|
2929
2147
|
*/
|
|
2930
2148
|
pause: () => {
|
|
2931
|
-
getScheduler().pauseJob(id);
|
|
2149
|
+
scheduler.getScheduler().pauseJob(id);
|
|
2932
2150
|
},
|
|
2933
2151
|
/**
|
|
2934
2152
|
* Resume this job (set enabled=true).
|
|
2935
2153
|
*/
|
|
2936
2154
|
resume: () => {
|
|
2937
|
-
getScheduler().resumeJob(id);
|
|
2155
|
+
scheduler.getScheduler().resumeJob(id);
|
|
2938
2156
|
},
|
|
2939
2157
|
/**
|
|
2940
2158
|
* Reactive paused state - automatically updates when pause/resume is called.
|
|
@@ -2972,18 +2190,18 @@ function buildFromCache(input, textureCache) {
|
|
|
2972
2190
|
function useTexture(input, optionsOrOnLoad) {
|
|
2973
2191
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
2974
2192
|
const store = useStore();
|
|
2975
|
-
const textureCache = useThree((state) => state.textures);
|
|
2976
2193
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
2977
|
-
const { onLoad, cache =
|
|
2194
|
+
const { onLoad, cache = true } = options;
|
|
2978
2195
|
const onLoadRef = React.useRef(onLoad);
|
|
2979
2196
|
onLoadRef.current = onLoad;
|
|
2980
2197
|
const onLoadCalledForRef = React.useRef(null);
|
|
2981
2198
|
const urls = React.useMemo(() => getUrls(input), [input]);
|
|
2982
2199
|
const cachedResult = React.useMemo(() => {
|
|
2983
2200
|
if (!cache) return null;
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2201
|
+
const textures = store.getState().textures;
|
|
2202
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2203
|
+
return buildFromCache(input, textures);
|
|
2204
|
+
}, [cache, urls, input, store]);
|
|
2987
2205
|
const loadedTextures = useLoader(
|
|
2988
2206
|
webgpu.TextureLoader,
|
|
2989
2207
|
IsObject(input) ? Object.values(input) : input
|
|
@@ -3027,8 +2245,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3027
2245
|
}, [input, loadedTextures, cachedResult]);
|
|
3028
2246
|
React.useEffect(() => {
|
|
3029
2247
|
if (!cache) return;
|
|
3030
|
-
if (cachedResult) return;
|
|
3031
|
-
const set = store.setState;
|
|
3032
2248
|
const urlTextureMap = [];
|
|
3033
2249
|
if (typeof input === "string") {
|
|
3034
2250
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -3042,18 +2258,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3042
2258
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
3043
2259
|
}
|
|
3044
2260
|
}
|
|
3045
|
-
|
|
3046
|
-
const
|
|
3047
|
-
let
|
|
2261
|
+
store.setState((state) => {
|
|
2262
|
+
const refs = new Map(state._textureRefs);
|
|
2263
|
+
let textures = state.textures;
|
|
2264
|
+
let added = false;
|
|
3048
2265
|
for (const [url, texture] of urlTextureMap) {
|
|
3049
|
-
if (!
|
|
3050
|
-
|
|
3051
|
-
|
|
2266
|
+
if (!textures.has(url)) {
|
|
2267
|
+
if (!added) {
|
|
2268
|
+
textures = new Map(textures);
|
|
2269
|
+
added = true;
|
|
2270
|
+
}
|
|
2271
|
+
textures.set(url, texture);
|
|
3052
2272
|
}
|
|
2273
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
3053
2274
|
}
|
|
3054
|
-
return
|
|
2275
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
3055
2276
|
});
|
|
3056
|
-
|
|
2277
|
+
return () => store.setState((state) => {
|
|
2278
|
+
const refs = new Map(state._textureRefs);
|
|
2279
|
+
for (const [url] of urlTextureMap) {
|
|
2280
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2281
|
+
if (next <= 0) refs.delete(url);
|
|
2282
|
+
else refs.set(url, next);
|
|
2283
|
+
}
|
|
2284
|
+
return { _textureRefs: refs };
|
|
2285
|
+
});
|
|
2286
|
+
}, [cache, input, mappedTextures, store]);
|
|
3057
2287
|
return mappedTextures;
|
|
3058
2288
|
}
|
|
3059
2289
|
useTexture.preload = (url) => useLoader.preload(webgpu.TextureLoader, url);
|
|
@@ -3069,96 +2299,63 @@ const Texture = ({
|
|
|
3069
2299
|
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: children?.(ret) });
|
|
3070
2300
|
};
|
|
3071
2301
|
|
|
3072
|
-
function
|
|
3073
|
-
if (entry instanceof webgpu.Texture) return entry;
|
|
3074
|
-
if (entry && typeof entry === "object" && "value" in entry && entry.value instanceof webgpu.Texture) {
|
|
3075
|
-
return entry.value;
|
|
3076
|
-
}
|
|
3077
|
-
return null;
|
|
3078
|
-
}
|
|
3079
|
-
function useTextures() {
|
|
2302
|
+
function useTextures(selector) {
|
|
3080
2303
|
const store = useStore();
|
|
3081
|
-
|
|
3082
|
-
const set = store.setState;
|
|
2304
|
+
const registry = React.useMemo(() => {
|
|
3083
2305
|
const getState = store.getState;
|
|
3084
|
-
const
|
|
3085
|
-
|
|
3086
|
-
const newMap = new Map(state.textures);
|
|
3087
|
-
newMap.set(key, value);
|
|
3088
|
-
return { textures: newMap };
|
|
3089
|
-
});
|
|
3090
|
-
};
|
|
3091
|
-
const addMultiple = (items) => {
|
|
3092
|
-
set((state) => {
|
|
3093
|
-
const newMap = new Map(state.textures);
|
|
3094
|
-
const entries = items instanceof Map ? items.entries() : Object.entries(items);
|
|
3095
|
-
for (const [key, value] of entries) {
|
|
3096
|
-
newMap.set(key, value);
|
|
3097
|
-
}
|
|
3098
|
-
return { textures: newMap };
|
|
3099
|
-
});
|
|
3100
|
-
};
|
|
3101
|
-
const remove = (key) => {
|
|
3102
|
-
set((state) => {
|
|
3103
|
-
const newMap = new Map(state.textures);
|
|
3104
|
-
newMap.delete(key);
|
|
3105
|
-
return { textures: newMap };
|
|
3106
|
-
});
|
|
3107
|
-
};
|
|
3108
|
-
const removeMultiple = (keys) => {
|
|
3109
|
-
set((state) => {
|
|
3110
|
-
const newMap = new Map(state.textures);
|
|
3111
|
-
for (const key of keys) newMap.delete(key);
|
|
3112
|
-
return { textures: newMap };
|
|
3113
|
-
});
|
|
3114
|
-
};
|
|
3115
|
-
const dispose = (key) => {
|
|
3116
|
-
const entry = getState().textures.get(key);
|
|
3117
|
-
if (entry) {
|
|
3118
|
-
const tex = getTextureValue(entry);
|
|
3119
|
-
tex?.dispose();
|
|
3120
|
-
}
|
|
3121
|
-
remove(key);
|
|
3122
|
-
};
|
|
3123
|
-
const disposeMultiple = (keys) => {
|
|
3124
|
-
const textures = getState().textures;
|
|
3125
|
-
for (const key of keys) {
|
|
3126
|
-
const entry = textures.get(key);
|
|
3127
|
-
if (entry) {
|
|
3128
|
-
const tex = getTextureValue(entry);
|
|
3129
|
-
tex?.dispose();
|
|
3130
|
-
}
|
|
3131
|
-
}
|
|
3132
|
-
removeMultiple(keys);
|
|
3133
|
-
};
|
|
3134
|
-
const disposeAll = () => {
|
|
3135
|
-
const textures = getState().textures;
|
|
3136
|
-
for (const entry of textures.values()) {
|
|
3137
|
-
const tex = getTextureValue(entry);
|
|
3138
|
-
tex?.dispose();
|
|
3139
|
-
}
|
|
3140
|
-
set({ textures: /* @__PURE__ */ new Map() });
|
|
3141
|
-
};
|
|
2306
|
+
const setState = store.setState;
|
|
2307
|
+
const getOne = (key) => getState().textures.get(key);
|
|
3142
2308
|
return {
|
|
3143
|
-
|
|
3144
|
-
get textures() {
|
|
2309
|
+
get all() {
|
|
3145
2310
|
return getState().textures;
|
|
3146
2311
|
},
|
|
3147
|
-
|
|
3148
|
-
|
|
2312
|
+
get(input) {
|
|
2313
|
+
if (typeof input === "string") return getOne(input);
|
|
2314
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2315
|
+
const out = {};
|
|
2316
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2317
|
+
return out;
|
|
2318
|
+
},
|
|
3149
2319
|
has: (key) => getState().textures.has(key),
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
2320
|
+
add(keyOrRecord, texture) {
|
|
2321
|
+
setState((state) => {
|
|
2322
|
+
const textures = new Map(state.textures);
|
|
2323
|
+
if (typeof keyOrRecord === "string") {
|
|
2324
|
+
textures.set(keyOrRecord, texture);
|
|
2325
|
+
} else {
|
|
2326
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2327
|
+
}
|
|
2328
|
+
return { textures };
|
|
2329
|
+
});
|
|
2330
|
+
},
|
|
2331
|
+
dispose(key, options) {
|
|
2332
|
+
const state = getState();
|
|
2333
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2334
|
+
if (refs > 0 && !options?.force) {
|
|
2335
|
+
console.warn(
|
|
2336
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2337
|
+
);
|
|
2338
|
+
return false;
|
|
2339
|
+
}
|
|
2340
|
+
state.textures.get(key)?.dispose();
|
|
2341
|
+
setState((s) => {
|
|
2342
|
+
const textures = new Map(s.textures);
|
|
2343
|
+
textures.delete(key);
|
|
2344
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2345
|
+
nextRefs.delete(key);
|
|
2346
|
+
return { textures, _textureRefs: nextRefs };
|
|
2347
|
+
});
|
|
2348
|
+
return true;
|
|
2349
|
+
},
|
|
2350
|
+
disposeAll() {
|
|
2351
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2352
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2353
|
+
}
|
|
3160
2354
|
};
|
|
3161
2355
|
}, [store]);
|
|
2356
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2357
|
+
const selected = useThree(subscribe);
|
|
2358
|
+
return selector ? selected : registry;
|
|
3162
2359
|
}
|
|
3163
2360
|
|
|
3164
2361
|
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
@@ -3215,7 +2412,7 @@ function addEffect(callback) {
|
|
|
3215
2412
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3216
2413
|
});
|
|
3217
2414
|
const id = `legacy_effect_${effectId++}`;
|
|
3218
|
-
return getScheduler().registerGlobal("before", id, callback);
|
|
2415
|
+
return scheduler.getScheduler().registerGlobal("before", id, callback);
|
|
3219
2416
|
}
|
|
3220
2417
|
function addAfterEffect(callback) {
|
|
3221
2418
|
notifyDepreciated({
|
|
@@ -3224,7 +2421,7 @@ function addAfterEffect(callback) {
|
|
|
3224
2421
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3225
2422
|
});
|
|
3226
2423
|
const id = `legacy_afterEffect_${effectId++}`;
|
|
3227
|
-
return getScheduler().registerGlobal("after", id, callback);
|
|
2424
|
+
return scheduler.getScheduler().registerGlobal("after", id, callback);
|
|
3228
2425
|
}
|
|
3229
2426
|
function addTail(callback) {
|
|
3230
2427
|
notifyDepreciated({
|
|
@@ -3232,16 +2429,16 @@ function addTail(callback) {
|
|
|
3232
2429
|
body: "Use scheduler.onIdle(callback) instead.\naddTail will be removed in a future version.",
|
|
3233
2430
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3234
2431
|
});
|
|
3235
|
-
return getScheduler().onIdle(callback);
|
|
2432
|
+
return scheduler.getScheduler().onIdle(callback);
|
|
3236
2433
|
}
|
|
3237
2434
|
function invalidate(state, frames = 1, stackFrames = false) {
|
|
3238
|
-
getScheduler().invalidate(frames, stackFrames);
|
|
2435
|
+
scheduler.getScheduler().invalidate(frames, stackFrames);
|
|
3239
2436
|
}
|
|
3240
2437
|
function advance(timestamp) {
|
|
3241
|
-
getScheduler().step(timestamp);
|
|
2438
|
+
scheduler.getScheduler().step(timestamp);
|
|
3242
2439
|
}
|
|
3243
2440
|
|
|
3244
|
-
const version = "10.0.0-alpha.
|
|
2441
|
+
const version = "10.0.0-alpha.3";
|
|
3245
2442
|
const packageData = {
|
|
3246
2443
|
version: version};
|
|
3247
2444
|
|
|
@@ -14497,7 +13694,7 @@ function n0() {
|
|
|
14497
13694
|
var t0 = n0();
|
|
14498
13695
|
const r0 = Xb(t0);
|
|
14499
13696
|
|
|
14500
|
-
const t = 1, o = 8, r = 32, e = 2;
|
|
13697
|
+
const t = 1, o = 8, r = 32, e = 2, i$1 = 268435456;
|
|
14501
13698
|
|
|
14502
13699
|
function createReconciler(config) {
|
|
14503
13700
|
const reconciler2 = r0(config);
|
|
@@ -14662,7 +13859,15 @@ function setFiberRef(fiber, publicInstance) {
|
|
|
14662
13859
|
}
|
|
14663
13860
|
}
|
|
14664
13861
|
const reconstructed = [];
|
|
14665
|
-
function
|
|
13862
|
+
function flushReconstructedInstances() {
|
|
13863
|
+
if (reconstructed.length === 0) return;
|
|
13864
|
+
try {
|
|
13865
|
+
swapReconstructedInstances();
|
|
13866
|
+
} finally {
|
|
13867
|
+
reconstructed.length = 0;
|
|
13868
|
+
}
|
|
13869
|
+
}
|
|
13870
|
+
function swapReconstructedInstances() {
|
|
14666
13871
|
for (const [instance] of reconstructed) {
|
|
14667
13872
|
const parent = instance.parent;
|
|
14668
13873
|
if (parent) {
|
|
@@ -14680,6 +13885,7 @@ function swapInstances() {
|
|
|
14680
13885
|
}
|
|
14681
13886
|
}
|
|
14682
13887
|
if (instance.isHidden) unhideInstance(instance);
|
|
13888
|
+
if (isObject3D(instance.object)) removeInteractivity(findInitialRoot(instance), instance.object);
|
|
14683
13889
|
if (instance.object.__r3f) delete instance.object.__r3f;
|
|
14684
13890
|
if (instance.type !== "primitive") disposeOnIdle(instance.object);
|
|
14685
13891
|
}
|
|
@@ -14692,6 +13898,8 @@ function swapInstances() {
|
|
|
14692
13898
|
instance.object.__r3f = instance;
|
|
14693
13899
|
setFiberRef(fiber, instance.object);
|
|
14694
13900
|
delete instance.appliedOnce;
|
|
13901
|
+
instance.eventCount = 0;
|
|
13902
|
+
instance.handlers = {};
|
|
14695
13903
|
applyProps(instance.object, instance.props);
|
|
14696
13904
|
if (instance.props.attach) {
|
|
14697
13905
|
attach(parent, instance);
|
|
@@ -14708,14 +13916,114 @@ function swapInstances() {
|
|
|
14708
13916
|
invalidateInstance(instance);
|
|
14709
13917
|
}
|
|
14710
13918
|
}
|
|
14711
|
-
reconstructed.length = 0;
|
|
14712
13919
|
}
|
|
14713
13920
|
const handleTextInstance = () => {
|
|
14714
13921
|
};
|
|
14715
13922
|
const NO_CONTEXT = {};
|
|
14716
13923
|
let currentUpdatePriority = NoEventPriority;
|
|
14717
|
-
|
|
14718
|
-
|
|
13924
|
+
function getEventPriority(type) {
|
|
13925
|
+
switch (type) {
|
|
13926
|
+
case "beforetoggle":
|
|
13927
|
+
case "cancel":
|
|
13928
|
+
case "click":
|
|
13929
|
+
case "close":
|
|
13930
|
+
case "contextmenu":
|
|
13931
|
+
case "copy":
|
|
13932
|
+
case "cut":
|
|
13933
|
+
case "auxclick":
|
|
13934
|
+
case "dblclick":
|
|
13935
|
+
case "dragend":
|
|
13936
|
+
case "dragstart":
|
|
13937
|
+
case "drop":
|
|
13938
|
+
case "focusin":
|
|
13939
|
+
case "focusout":
|
|
13940
|
+
case "input":
|
|
13941
|
+
case "invalid":
|
|
13942
|
+
case "keydown":
|
|
13943
|
+
case "keypress":
|
|
13944
|
+
case "keyup":
|
|
13945
|
+
case "mousedown":
|
|
13946
|
+
case "mouseup":
|
|
13947
|
+
case "paste":
|
|
13948
|
+
case "pause":
|
|
13949
|
+
case "play":
|
|
13950
|
+
case "pointercancel":
|
|
13951
|
+
case "pointerdown":
|
|
13952
|
+
case "pointerup":
|
|
13953
|
+
case "ratechange":
|
|
13954
|
+
case "reset":
|
|
13955
|
+
case "resize":
|
|
13956
|
+
case "seeked":
|
|
13957
|
+
case "submit":
|
|
13958
|
+
case "touchcancel":
|
|
13959
|
+
case "touchend":
|
|
13960
|
+
case "touchstart":
|
|
13961
|
+
case "volumechange":
|
|
13962
|
+
case "change":
|
|
13963
|
+
case "selectionchange":
|
|
13964
|
+
case "compositionstart":
|
|
13965
|
+
case "compositionend":
|
|
13966
|
+
case "compositionupdate":
|
|
13967
|
+
case "beforeinput":
|
|
13968
|
+
case "blur":
|
|
13969
|
+
case "fullscreenchange":
|
|
13970
|
+
case "focus":
|
|
13971
|
+
case "hashchange":
|
|
13972
|
+
case "popstate":
|
|
13973
|
+
case "select":
|
|
13974
|
+
case "selectstart":
|
|
13975
|
+
return e;
|
|
13976
|
+
case "drag":
|
|
13977
|
+
case "dragenter":
|
|
13978
|
+
case "dragexit":
|
|
13979
|
+
case "dragleave":
|
|
13980
|
+
case "dragover":
|
|
13981
|
+
case "mousemove":
|
|
13982
|
+
case "mouseout":
|
|
13983
|
+
case "mouseover":
|
|
13984
|
+
case "pointermove":
|
|
13985
|
+
case "pointerout":
|
|
13986
|
+
case "pointerover":
|
|
13987
|
+
case "scroll":
|
|
13988
|
+
case "touchmove":
|
|
13989
|
+
case "wheel":
|
|
13990
|
+
case "mouseenter":
|
|
13991
|
+
case "mouseleave":
|
|
13992
|
+
case "pointerenter":
|
|
13993
|
+
case "pointerleave":
|
|
13994
|
+
return o;
|
|
13995
|
+
case "message": {
|
|
13996
|
+
switch (Tb.unstable_getCurrentPriorityLevel()) {
|
|
13997
|
+
case Tb.unstable_ImmediatePriority:
|
|
13998
|
+
return e;
|
|
13999
|
+
case Tb.unstable_UserBlockingPriority:
|
|
14000
|
+
return o;
|
|
14001
|
+
case Tb.unstable_NormalPriority:
|
|
14002
|
+
case Tb.unstable_LowPriority:
|
|
14003
|
+
return r;
|
|
14004
|
+
case Tb.unstable_IdlePriority:
|
|
14005
|
+
return i$1;
|
|
14006
|
+
default:
|
|
14007
|
+
return r;
|
|
14008
|
+
}
|
|
14009
|
+
}
|
|
14010
|
+
default:
|
|
14011
|
+
return r;
|
|
14012
|
+
}
|
|
14013
|
+
}
|
|
14014
|
+
function scheduleMicrotask(callback) {
|
|
14015
|
+
if (typeof queueMicrotask === "function") {
|
|
14016
|
+
queueMicrotask(callback);
|
|
14017
|
+
} else if (typeof Promise !== "undefined") {
|
|
14018
|
+
Promise.resolve().then(callback).catch((error) => {
|
|
14019
|
+
setTimeout(() => {
|
|
14020
|
+
throw error;
|
|
14021
|
+
});
|
|
14022
|
+
});
|
|
14023
|
+
} else {
|
|
14024
|
+
setTimeout(callback);
|
|
14025
|
+
}
|
|
14026
|
+
}
|
|
14719
14027
|
const reconciler = /* @__PURE__ */ createReconciler({
|
|
14720
14028
|
isPrimaryRenderer: false,
|
|
14721
14029
|
warnsIfNotActing: false,
|
|
@@ -14754,16 +14062,15 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14754
14062
|
else if (newProps.args?.length !== oldProps.args?.length) reconstruct = true;
|
|
14755
14063
|
else if (newProps.args?.some((value, index) => value !== oldProps.args?.[index])) reconstruct = true;
|
|
14756
14064
|
if (reconstruct) {
|
|
14757
|
-
reconstructed.push([instance,
|
|
14065
|
+
reconstructed.push([instance, getInstanceProps(newProps), fiber]);
|
|
14758
14066
|
} else {
|
|
14759
14067
|
const changedProps = diffProps(instance, newProps);
|
|
14760
|
-
|
|
14761
|
-
|
|
14762
|
-
|
|
14763
|
-
|
|
14068
|
+
const attach2 = instance.props.attach;
|
|
14069
|
+
instance.props = getInstanceProps(newProps);
|
|
14070
|
+
if (attach2 !== void 0) instance.props.attach = attach2;
|
|
14071
|
+
else delete instance.props.attach;
|
|
14072
|
+
if (Object.keys(changedProps).length) applyProps(instance.object, changedProps);
|
|
14764
14073
|
}
|
|
14765
|
-
const isTailSibling = fiber.sibling === null || (fiber.flags & Update) === NoFlags;
|
|
14766
|
-
if (isTailSibling) swapInstances();
|
|
14767
14074
|
},
|
|
14768
14075
|
finalizeInitialChildren: (instance) => {
|
|
14769
14076
|
for (const prop in instance.props) {
|
|
@@ -14788,8 +14095,9 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14788
14095
|
const target = container.getState().internal.container ?? container.getState().scene;
|
|
14789
14096
|
return prepare(target, container, "", {});
|
|
14790
14097
|
},
|
|
14791
|
-
|
|
14792
|
-
|
|
14098
|
+
// Reconstructed instances are swapped once all mutations are committed,
|
|
14099
|
+
// before layout effects run so refs point to the new objects
|
|
14100
|
+
resetAfterCommit: flushReconstructedInstances,
|
|
14793
14101
|
shouldSetTextContent: () => false,
|
|
14794
14102
|
clearContainer: () => false,
|
|
14795
14103
|
hideInstance,
|
|
@@ -14797,6 +14105,9 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14797
14105
|
createTextInstance: handleTextInstance,
|
|
14798
14106
|
hideTextInstance: handleTextInstance,
|
|
14799
14107
|
unhideTextInstance: handleTextInstance,
|
|
14108
|
+
// Mirrors react-dom, which flushes discrete work in microtasks
|
|
14109
|
+
supportsMicrotasks: true,
|
|
14110
|
+
scheduleMicrotask,
|
|
14800
14111
|
scheduleTimeout: typeof setTimeout === "function" ? setTimeout : void 0,
|
|
14801
14112
|
cancelTimeout: typeof clearTimeout === "function" ? clearTimeout : void 0,
|
|
14802
14113
|
noTimeout: -1,
|
|
@@ -14837,28 +14148,9 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14837
14148
|
},
|
|
14838
14149
|
resolveUpdatePriority() {
|
|
14839
14150
|
if (currentUpdatePriority !== NoEventPriority) return currentUpdatePriority;
|
|
14840
|
-
|
|
14841
|
-
|
|
14842
|
-
|
|
14843
|
-
case "dblclick":
|
|
14844
|
-
case "dragenter":
|
|
14845
|
-
case "dragleave":
|
|
14846
|
-
case "drop":
|
|
14847
|
-
case "pointercancel":
|
|
14848
|
-
case "pointerdown":
|
|
14849
|
-
case "pointerup":
|
|
14850
|
-
return e;
|
|
14851
|
-
case "dragover":
|
|
14852
|
-
case "pointermove":
|
|
14853
|
-
case "pointerout":
|
|
14854
|
-
case "pointerover":
|
|
14855
|
-
case "pointerenter":
|
|
14856
|
-
case "pointerleave":
|
|
14857
|
-
case "wheel":
|
|
14858
|
-
return o;
|
|
14859
|
-
default:
|
|
14860
|
-
return r;
|
|
14861
|
-
}
|
|
14151
|
+
const eventType = typeof window !== "undefined" ? window.event?.type : void 0;
|
|
14152
|
+
if (eventType === void 0) return r;
|
|
14153
|
+
return getEventPriority(eventType);
|
|
14862
14154
|
},
|
|
14863
14155
|
resetFormInstance() {
|
|
14864
14156
|
},
|
|
@@ -14907,11 +14199,15 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14907
14199
|
cloneMutableTextInstance(textInstance) {
|
|
14908
14200
|
return textInstance;
|
|
14909
14201
|
},
|
|
14202
|
+
// React 19.2 added these for root-level view transitions. They only run for hosts that opt into
|
|
14203
|
+
// view transitions by returning a container from cloneRootViewTransitionContainer; R3F renders
|
|
14204
|
+
// into a WebGL/WebGPU canvas, where cloning the root has no meaning, so we never opt in and
|
|
14205
|
+
// React never calls them. They stay as no-ops rather than throws: the host config is shared by
|
|
14206
|
+
// every root, and a throw here would turn a React-internal probe into a hard crash for users.
|
|
14207
|
+
// Guarded by a test asserting a full render + unmount never reaches them.
|
|
14910
14208
|
cloneRootViewTransitionContainer(_rootContainer) {
|
|
14911
|
-
throw new Error("Not implemented.");
|
|
14912
14209
|
},
|
|
14913
14210
|
removeRootViewTransitionClone(_rootContainer, _clone) {
|
|
14914
|
-
throw new Error("Not implemented.");
|
|
14915
14211
|
},
|
|
14916
14212
|
// https://github.com/facebook/react/pull/32465
|
|
14917
14213
|
createFragmentInstance: (_fiber) => null,
|
|
@@ -14996,6 +14292,7 @@ function createRoot(canvas) {
|
|
|
14996
14292
|
const lastConfiguredProps = {};
|
|
14997
14293
|
let configured = false;
|
|
14998
14294
|
let pending = null;
|
|
14295
|
+
let rendererSetup = null;
|
|
14999
14296
|
return {
|
|
15000
14297
|
async configure(props = {}) {
|
|
15001
14298
|
let resolve;
|
|
@@ -15011,7 +14308,6 @@ function createRoot(canvas) {
|
|
|
15011
14308
|
events,
|
|
15012
14309
|
onCreated: onCreatedCallback,
|
|
15013
14310
|
shadows = false,
|
|
15014
|
-
textureColorSpace = webgpu.SRGBColorSpace,
|
|
15015
14311
|
orthographic = false,
|
|
15016
14312
|
frameloop = "always",
|
|
15017
14313
|
dpr = [1, 2],
|
|
@@ -15026,7 +14322,8 @@ function createRoot(canvas) {
|
|
|
15026
14322
|
_sizeProps,
|
|
15027
14323
|
forceEven
|
|
15028
14324
|
} = props;
|
|
15029
|
-
const
|
|
14325
|
+
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || webgpu.SRGBColorSpace;
|
|
14326
|
+
let state = store.getState();
|
|
15030
14327
|
const defaultGLProps = {
|
|
15031
14328
|
canvas,
|
|
15032
14329
|
powerPreference: "high-performance",
|
|
@@ -15054,50 +14351,68 @@ function createRoot(canvas) {
|
|
|
15054
14351
|
"The `primaryCanvas` prop for multi-canvas rendering cannot be used with WebGL. Remove the `gl` prop or use WebGPU."
|
|
15055
14352
|
);
|
|
15056
14353
|
}
|
|
15057
|
-
if (
|
|
15058
|
-
|
|
15059
|
-
|
|
15060
|
-
|
|
15061
|
-
|
|
15062
|
-
|
|
15063
|
-
|
|
15064
|
-
|
|
15065
|
-
|
|
15066
|
-
|
|
15067
|
-
|
|
15068
|
-
|
|
15069
|
-
|
|
15070
|
-
|
|
15071
|
-
|
|
15072
|
-
|
|
15073
|
-
|
|
15074
|
-
|
|
15075
|
-
|
|
15076
|
-
|
|
15077
|
-
|
|
15078
|
-
|
|
15079
|
-
|
|
15080
|
-
|
|
15081
|
-
|
|
15082
|
-
|
|
15083
|
-
|
|
15084
|
-
|
|
15085
|
-
|
|
15086
|
-
|
|
15087
|
-
|
|
15088
|
-
|
|
15089
|
-
|
|
15090
|
-
|
|
15091
|
-
|
|
15092
|
-
|
|
15093
|
-
|
|
15094
|
-
|
|
15095
|
-
|
|
15096
|
-
|
|
15097
|
-
|
|
14354
|
+
if (!state.internal.actualRenderer) {
|
|
14355
|
+
if (!rendererSetup) {
|
|
14356
|
+
rendererSetup = (async () => {
|
|
14357
|
+
if (wantsGL) {
|
|
14358
|
+
renderer = await resolveRenderer(glConfig, defaultGLProps, three.WebGLRenderer);
|
|
14359
|
+
state.internal.actualRenderer = renderer;
|
|
14360
|
+
state.set({ isLegacy: true, gl: renderer, renderer, primaryStore: store });
|
|
14361
|
+
} else if (!wantsGL && primaryCanvas) {
|
|
14362
|
+
const primary = await waitForPrimary(primaryCanvas);
|
|
14363
|
+
renderer = primary.renderer;
|
|
14364
|
+
state.internal.actualRenderer = renderer;
|
|
14365
|
+
const canvasTarget = new webgpu.CanvasTarget(canvas);
|
|
14366
|
+
primary.store.setState((prev) => ({
|
|
14367
|
+
internal: { ...prev.internal, isMultiCanvas: true }
|
|
14368
|
+
}));
|
|
14369
|
+
state.set((prev) => ({
|
|
14370
|
+
webGPUSupported: primary.store.getState().webGPUSupported,
|
|
14371
|
+
renderer,
|
|
14372
|
+
primaryStore: primary.store,
|
|
14373
|
+
internal: {
|
|
14374
|
+
...prev.internal,
|
|
14375
|
+
canvasTarget,
|
|
14376
|
+
isMultiCanvas: true,
|
|
14377
|
+
isSecondary: true,
|
|
14378
|
+
targetId: primaryCanvas
|
|
14379
|
+
}
|
|
14380
|
+
}));
|
|
14381
|
+
} else if (!wantsGL) {
|
|
14382
|
+
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, webgpu.WebGPURenderer);
|
|
14383
|
+
if (!renderer.hasInitialized?.()) {
|
|
14384
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14385
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14386
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14387
|
+
canvas.width = size2.width * pixelRatio;
|
|
14388
|
+
canvas.height = size2.height * pixelRatio;
|
|
14389
|
+
}
|
|
14390
|
+
await renderer.init();
|
|
14391
|
+
}
|
|
14392
|
+
const backend = renderer.backend;
|
|
14393
|
+
const isWebGPUBackend = backend && "isWebGPUBackend" in backend;
|
|
14394
|
+
state.internal.actualRenderer = renderer;
|
|
14395
|
+
state.set({ webGPUSupported: isWebGPUBackend, renderer, primaryStore: store });
|
|
14396
|
+
if (canvasId && !state.internal.isSecondary) {
|
|
14397
|
+
const canvasTarget = new webgpu.CanvasTarget(canvas);
|
|
14398
|
+
const unregisterPrimary = registerPrimary(canvasId, renderer, store);
|
|
14399
|
+
state.set((prev) => ({
|
|
14400
|
+
internal: {
|
|
14401
|
+
...prev.internal,
|
|
14402
|
+
canvasTarget,
|
|
14403
|
+
unregisterPrimary
|
|
14404
|
+
}
|
|
14405
|
+
}));
|
|
14406
|
+
}
|
|
15098
14407
|
}
|
|
15099
|
-
}))
|
|
14408
|
+
})().catch((err) => {
|
|
14409
|
+
rendererSetup = null;
|
|
14410
|
+
throw err;
|
|
14411
|
+
});
|
|
15100
14412
|
}
|
|
14413
|
+
await rendererSetup;
|
|
14414
|
+
state = store.getState();
|
|
14415
|
+
renderer = state.internal.actualRenderer;
|
|
15101
14416
|
}
|
|
15102
14417
|
let raycaster = state.raycaster;
|
|
15103
14418
|
if (!raycaster) state.set({ raycaster: raycaster = new webgpu.Raycaster() });
|
|
@@ -15190,7 +14505,7 @@ function createRoot(canvas) {
|
|
|
15190
14505
|
lastConfiguredProps.performance = performance;
|
|
15191
14506
|
}
|
|
15192
14507
|
if (!state.xr) {
|
|
15193
|
-
const handleXRFrame = (timestamp,
|
|
14508
|
+
const handleXRFrame = (timestamp, _frame) => {
|
|
15194
14509
|
const state2 = store.getState();
|
|
15195
14510
|
if (state2.frameloop === "never") return;
|
|
15196
14511
|
advance(timestamp);
|
|
@@ -15226,15 +14541,22 @@ function createRoot(canvas) {
|
|
|
15226
14541
|
const oldType = renderer.shadowMap.type;
|
|
15227
14542
|
renderer.shadowMap.enabled = !!shadows;
|
|
15228
14543
|
if (is.boo(shadows)) {
|
|
15229
|
-
renderer.shadowMap.type = webgpu.
|
|
14544
|
+
renderer.shadowMap.type = webgpu.PCFShadowMap;
|
|
15230
14545
|
} else if (is.str(shadows)) {
|
|
14546
|
+
if (shadows === "soft") {
|
|
14547
|
+
notifyDepreciated({
|
|
14548
|
+
heading: 'shadows="soft" is deprecated',
|
|
14549
|
+
body: "Three has depreciated soft and improved basic PCFShadows, we converted for you.",
|
|
14550
|
+
link: "https://github.com/mrdoob/three.js/wiki/Migration-Guide?utm_source=chatgpt.com#181--182"
|
|
14551
|
+
});
|
|
14552
|
+
}
|
|
15231
14553
|
const types = {
|
|
15232
14554
|
basic: webgpu.BasicShadowMap,
|
|
15233
14555
|
percentage: webgpu.PCFShadowMap,
|
|
15234
|
-
soft: webgpu.
|
|
14556
|
+
soft: webgpu.PCFShadowMap,
|
|
15235
14557
|
variance: webgpu.VSMShadowMap
|
|
15236
14558
|
};
|
|
15237
|
-
renderer.shadowMap.type = types[shadows] ?? webgpu.
|
|
14559
|
+
renderer.shadowMap.type = types[shadows] ?? webgpu.PCFShadowMap;
|
|
15238
14560
|
} else if (is.obj(shadows)) {
|
|
15239
14561
|
Object.assign(renderer.shadowMap, shadows);
|
|
15240
14562
|
}
|
|
@@ -15250,24 +14572,35 @@ function createRoot(canvas) {
|
|
|
15250
14572
|
if (state.textureColorSpace !== textureColorSpace) state.set(() => ({ textureColorSpace }));
|
|
15251
14573
|
lastConfiguredProps.textureColorSpace = textureColorSpace;
|
|
15252
14574
|
}
|
|
14575
|
+
const r3fProps = ["textureColorSpace"];
|
|
14576
|
+
const constructorOnlyProps = ["samples", "antialias", "alpha", "canvas", "powerPreference"];
|
|
14577
|
+
const nonApplyProps = [...r3fProps, ...constructorOnlyProps];
|
|
15253
14578
|
if (glConfig && !is.fun(glConfig) && !isRenderer(glConfig) && !is.equ(glConfig, renderer, shallowLoose)) {
|
|
15254
|
-
|
|
14579
|
+
const glProps = {};
|
|
14580
|
+
for (const key in glConfig) {
|
|
14581
|
+
if (!nonApplyProps.includes(key)) glProps[key] = glConfig[key];
|
|
14582
|
+
}
|
|
14583
|
+
applyProps(renderer, glProps);
|
|
15255
14584
|
}
|
|
15256
14585
|
if (rendererConfig && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && state.renderer) {
|
|
15257
14586
|
const currentRenderer = state.renderer;
|
|
15258
14587
|
if (!is.equ(rendererConfig, currentRenderer, shallowLoose)) {
|
|
15259
|
-
|
|
14588
|
+
const rendererProps = {};
|
|
14589
|
+
for (const key in rendererConfig) {
|
|
14590
|
+
if (!nonApplyProps.includes(key)) rendererProps[key] = rendererConfig[key];
|
|
14591
|
+
}
|
|
14592
|
+
applyProps(currentRenderer, rendererProps);
|
|
15260
14593
|
}
|
|
15261
14594
|
}
|
|
15262
|
-
const scheduler = getScheduler();
|
|
14595
|
+
const scheduler$1 = scheduler.getScheduler();
|
|
15263
14596
|
const rootId = state.internal.rootId;
|
|
15264
14597
|
if (!rootId) {
|
|
15265
|
-
const newRootId = canvasId || scheduler.generateRootId();
|
|
15266
|
-
const unregisterRoot = scheduler.registerRoot(newRootId, {
|
|
14598
|
+
const newRootId = canvasId || scheduler$1.generateRootId();
|
|
14599
|
+
const unregisterRoot = scheduler$1.registerRoot(newRootId, {
|
|
15267
14600
|
getState: () => store.getState(),
|
|
15268
14601
|
onError: (err) => store.getState().setError(err)
|
|
15269
14602
|
});
|
|
15270
|
-
const unregisterCanvasTarget = scheduler.register(
|
|
14603
|
+
const unregisterCanvasTarget = scheduler$1.register(
|
|
15271
14604
|
() => {
|
|
15272
14605
|
const state2 = store.getState();
|
|
15273
14606
|
if (state2.internal.isMultiCanvas && state2.internal.canvasTarget) {
|
|
@@ -15282,7 +14615,19 @@ function createRoot(canvas) {
|
|
|
15282
14615
|
system: true
|
|
15283
14616
|
}
|
|
15284
14617
|
);
|
|
15285
|
-
const
|
|
14618
|
+
const unregisterEventsFlush = scheduler$1.register(
|
|
14619
|
+
() => {
|
|
14620
|
+
const state2 = store.getState();
|
|
14621
|
+
state2.events.flush?.();
|
|
14622
|
+
},
|
|
14623
|
+
{
|
|
14624
|
+
id: `${newRootId}_events`,
|
|
14625
|
+
rootId: newRootId,
|
|
14626
|
+
phase: "input",
|
|
14627
|
+
system: true
|
|
14628
|
+
}
|
|
14629
|
+
);
|
|
14630
|
+
const unregisterFrustum = scheduler$1.register(
|
|
15286
14631
|
() => {
|
|
15287
14632
|
const state2 = store.getState();
|
|
15288
14633
|
if (state2.autoUpdateFrustum && state2.camera) {
|
|
@@ -15292,11 +14637,11 @@ function createRoot(canvas) {
|
|
|
15292
14637
|
{
|
|
15293
14638
|
id: `${newRootId}_frustum`,
|
|
15294
14639
|
rootId: newRootId,
|
|
15295
|
-
|
|
14640
|
+
before: "render",
|
|
15296
14641
|
system: true
|
|
15297
14642
|
}
|
|
15298
14643
|
);
|
|
15299
|
-
const unregisterVisibility = scheduler.register(
|
|
14644
|
+
const unregisterVisibility = scheduler$1.register(
|
|
15300
14645
|
() => {
|
|
15301
14646
|
const state2 = store.getState();
|
|
15302
14647
|
checkVisibility(state2);
|
|
@@ -15304,19 +14649,19 @@ function createRoot(canvas) {
|
|
|
15304
14649
|
{
|
|
15305
14650
|
id: `${newRootId}_visibility`,
|
|
15306
14651
|
rootId: newRootId,
|
|
15307
|
-
|
|
14652
|
+
before: "render",
|
|
15308
14653
|
system: true,
|
|
15309
14654
|
after: `${newRootId}_frustum`
|
|
15310
14655
|
}
|
|
15311
14656
|
);
|
|
15312
|
-
const unregisterRender = scheduler.register(
|
|
14657
|
+
const unregisterRender = scheduler$1.register(
|
|
15313
14658
|
() => {
|
|
15314
14659
|
const state2 = store.getState();
|
|
15315
14660
|
const renderer2 = state2.internal.actualRenderer;
|
|
15316
|
-
const userHandlesRender = scheduler.hasUserJobsInPhase("render", newRootId);
|
|
14661
|
+
const userHandlesRender = scheduler$1.hasUserJobsInPhase("render", newRootId);
|
|
15317
14662
|
if (userHandlesRender || state2.internal.priority) return;
|
|
15318
14663
|
try {
|
|
15319
|
-
if (state2.
|
|
14664
|
+
if (state2.renderPipeline?.render) state2.renderPipeline.render();
|
|
15320
14665
|
else if (renderer2?.render) renderer2.render(state2.scene, state2.camera);
|
|
15321
14666
|
} catch (error) {
|
|
15322
14667
|
state2.setError(error instanceof Error ? error : new Error(String(error)));
|
|
@@ -15341,15 +14686,16 @@ function createRoot(canvas) {
|
|
|
15341
14686
|
unregisterRoot: () => {
|
|
15342
14687
|
unregisterRoot();
|
|
15343
14688
|
unregisterCanvasTarget();
|
|
14689
|
+
unregisterEventsFlush();
|
|
15344
14690
|
unregisterFrustum();
|
|
15345
14691
|
unregisterVisibility();
|
|
15346
14692
|
unregisterRender();
|
|
15347
14693
|
},
|
|
15348
|
-
scheduler
|
|
14694
|
+
scheduler: scheduler$1
|
|
15349
14695
|
}
|
|
15350
14696
|
}));
|
|
15351
14697
|
}
|
|
15352
|
-
scheduler.frameloop = frameloop;
|
|
14698
|
+
scheduler$1.frameloop = frameloop;
|
|
15353
14699
|
onCreated = onCreatedCallback;
|
|
15354
14700
|
configured = true;
|
|
15355
14701
|
resolve();
|
|
@@ -15416,7 +14762,8 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
15416
14762
|
dispose(state.scene);
|
|
15417
14763
|
_roots.delete(canvas);
|
|
15418
14764
|
if (callback) callback(canvas);
|
|
15419
|
-
} catch {
|
|
14765
|
+
} catch (error) {
|
|
14766
|
+
console.warn("[R3F] Error while unmounting root; teardown may be incomplete:", error);
|
|
15420
14767
|
}
|
|
15421
14768
|
}, 500);
|
|
15422
14769
|
}
|
|
@@ -15479,6 +14826,7 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15479
14826
|
viewport = rootState.viewport.getCurrentViewport(camera, new webgpu.Vector3(), resolvedSize);
|
|
15480
14827
|
if (camera !== rootState.camera) updateCamera(camera, resolvedSize);
|
|
15481
14828
|
}
|
|
14829
|
+
const set = injectState.set;
|
|
15482
14830
|
return {
|
|
15483
14831
|
// The intersect consists of the previous root state
|
|
15484
14832
|
...rootState,
|
|
@@ -15497,7 +14845,7 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15497
14845
|
size: resolvedSize,
|
|
15498
14846
|
viewport: { ...rootState.viewport, ...viewport },
|
|
15499
14847
|
// Layers are allowed to override events
|
|
15500
|
-
setEvents: (events2) =>
|
|
14848
|
+
setEvents: (events2) => set((state2) => ({ ...state2, events: { ...state2.events, ...events2 } })),
|
|
15501
14849
|
// Container for child attachment - the portalScene (injected or container itself)
|
|
15502
14850
|
internal: { ...rootState.internal, ...injectState.internal, container: portalScene }
|
|
15503
14851
|
};
|
|
@@ -15506,9 +14854,13 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15506
14854
|
const store = traditional.createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
15507
14855
|
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
15508
14856
|
onMutate(previousRoot.getState());
|
|
15509
|
-
previousRoot.subscribe(onMutate);
|
|
15510
14857
|
return store;
|
|
15511
14858
|
}, [previousRoot, container]);
|
|
14859
|
+
useIsomorphicLayoutEffect(() => {
|
|
14860
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14861
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14862
|
+
return unsubscribe;
|
|
14863
|
+
}, [previousRoot, usePortalStore]);
|
|
15512
14864
|
return (
|
|
15513
14865
|
// @ts-ignore, reconciler types are not maintained
|
|
15514
14866
|
/* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: reconciler.createPortal(
|
|
@@ -15522,6 +14874,81 @@ function flushSync(fn) {
|
|
|
15522
14874
|
return reconciler.flushSyncFromReconciler(fn);
|
|
15523
14875
|
}
|
|
15524
14876
|
|
|
14877
|
+
function parseBackground(background) {
|
|
14878
|
+
if (!background) return null;
|
|
14879
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14880
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14881
|
+
return {
|
|
14882
|
+
...rest,
|
|
14883
|
+
preset,
|
|
14884
|
+
files: envMap || files,
|
|
14885
|
+
backgroundFiles: backgroundMap,
|
|
14886
|
+
background: true
|
|
14887
|
+
};
|
|
14888
|
+
}
|
|
14889
|
+
if (typeof background === "number") {
|
|
14890
|
+
return { color: background, background: true };
|
|
14891
|
+
}
|
|
14892
|
+
if (typeof background === "string") {
|
|
14893
|
+
if (background in presetsObj) {
|
|
14894
|
+
return { preset: background, background: true };
|
|
14895
|
+
}
|
|
14896
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14897
|
+
return { files: background, background: true };
|
|
14898
|
+
}
|
|
14899
|
+
return { color: background, background: true };
|
|
14900
|
+
}
|
|
14901
|
+
if (background.isColor) {
|
|
14902
|
+
return { color: background, background: true };
|
|
14903
|
+
}
|
|
14904
|
+
return null;
|
|
14905
|
+
}
|
|
14906
|
+
|
|
14907
|
+
function parseRendererConfig(rendererProp) {
|
|
14908
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14909
|
+
if (isRendererConfig) {
|
|
14910
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14911
|
+
return {
|
|
14912
|
+
primaryCanvas: pc,
|
|
14913
|
+
scheduler: sc,
|
|
14914
|
+
renderer: Object.keys(rest).length > 0 ? rest : rendererProp
|
|
14915
|
+
};
|
|
14916
|
+
}
|
|
14917
|
+
return { primaryCanvas: void 0, scheduler: void 0, renderer: rendererProp };
|
|
14918
|
+
}
|
|
14919
|
+
|
|
14920
|
+
const RESOURCE_KINDS = ["nodes", "uniforms", "buffers", "gpuStorage"];
|
|
14921
|
+
const registries = /* @__PURE__ */ new WeakMap();
|
|
14922
|
+
function getKindRegistry(store, kind) {
|
|
14923
|
+
let registry = registries.get(store);
|
|
14924
|
+
if (!registry) {
|
|
14925
|
+
registry = {
|
|
14926
|
+
nodes: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14927
|
+
uniforms: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14928
|
+
buffers: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14929
|
+
gpuStorage: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() }
|
|
14930
|
+
};
|
|
14931
|
+
registries.set(store, registry);
|
|
14932
|
+
}
|
|
14933
|
+
return registry[kind];
|
|
14934
|
+
}
|
|
14935
|
+
function invalidateResource(store, kind, scopeKey) {
|
|
14936
|
+
const registry = getKindRegistry(store, kind);
|
|
14937
|
+
{
|
|
14938
|
+
registry.valid.clear();
|
|
14939
|
+
registry.staged.clear();
|
|
14940
|
+
}
|
|
14941
|
+
}
|
|
14942
|
+
function invalidateAllResources(store) {
|
|
14943
|
+
for (const kind of RESOURCE_KINDS) invalidateResource(store, kind);
|
|
14944
|
+
}
|
|
14945
|
+
|
|
14946
|
+
function clearHmrCaches(store) {
|
|
14947
|
+
const primary = store.getState().primaryStore ?? store;
|
|
14948
|
+
invalidateAllResources(primary);
|
|
14949
|
+
primary.setState((state) => ({ _hmrVersion: state._hmrVersion + 1 }));
|
|
14950
|
+
}
|
|
14951
|
+
|
|
15525
14952
|
function CanvasImpl({
|
|
15526
14953
|
ref,
|
|
15527
14954
|
children,
|
|
@@ -15542,6 +14969,8 @@ function CanvasImpl({
|
|
|
15542
14969
|
raycaster,
|
|
15543
14970
|
camera,
|
|
15544
14971
|
scene,
|
|
14972
|
+
autoUpdateFrustum,
|
|
14973
|
+
occlusion,
|
|
15545
14974
|
onPointerMissed,
|
|
15546
14975
|
onDragOverMissed,
|
|
15547
14976
|
onDropMissed,
|
|
@@ -15553,39 +14982,10 @@ function CanvasImpl({
|
|
|
15553
14982
|
forceEven,
|
|
15554
14983
|
...props
|
|
15555
14984
|
}) {
|
|
15556
|
-
const { primaryCanvas, scheduler,
|
|
15557
|
-
const renderer = Object.keys(rendererConfig).length > 0 ? rendererConfig : rendererProp;
|
|
14985
|
+
const { primaryCanvas, scheduler, renderer } = parseRendererConfig(rendererProp);
|
|
15558
14986
|
React__namespace.useMemo(() => extend(THREE), []);
|
|
15559
14987
|
const Bridge = useBridge();
|
|
15560
|
-
const backgroundProps = React__namespace.useMemo(() =>
|
|
15561
|
-
if (!background) return null;
|
|
15562
|
-
if (typeof background === "object" && !background.isColor) {
|
|
15563
|
-
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
15564
|
-
return {
|
|
15565
|
-
...rest,
|
|
15566
|
-
preset,
|
|
15567
|
-
files: envMap || files,
|
|
15568
|
-
backgroundFiles: backgroundMap,
|
|
15569
|
-
background: true
|
|
15570
|
-
};
|
|
15571
|
-
}
|
|
15572
|
-
if (typeof background === "number") {
|
|
15573
|
-
return { color: background, background: true };
|
|
15574
|
-
}
|
|
15575
|
-
if (typeof background === "string") {
|
|
15576
|
-
if (background in presetsObj) {
|
|
15577
|
-
return { preset: background, background: true };
|
|
15578
|
-
}
|
|
15579
|
-
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
15580
|
-
return { files: background, background: true };
|
|
15581
|
-
}
|
|
15582
|
-
return { color: background, background: true };
|
|
15583
|
-
}
|
|
15584
|
-
if (background.isColor) {
|
|
15585
|
-
return { color: background, background: true };
|
|
15586
|
-
}
|
|
15587
|
-
return null;
|
|
15588
|
-
}, [background]);
|
|
14988
|
+
const backgroundProps = React__namespace.useMemo(() => parseBackground(background), [background]);
|
|
15589
14989
|
const hasInitialSizeRef = React__namespace.useRef(false);
|
|
15590
14990
|
const measureConfig = React__namespace.useMemo(() => {
|
|
15591
14991
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15627,6 +15027,7 @@ function CanvasImpl({
|
|
|
15627
15027
|
const handleDropMissed = useMutableCallback(onDropMissed);
|
|
15628
15028
|
const [block, setBlock] = React__namespace.useState(false);
|
|
15629
15029
|
const [error, setError] = React__namespace.useState(false);
|
|
15030
|
+
const [fallbackVisible, setFallbackVisible] = React__namespace.useState(false);
|
|
15630
15031
|
if (block) throw block;
|
|
15631
15032
|
if (error) throw error;
|
|
15632
15033
|
const root = React__namespace.useRef(null);
|
|
@@ -15634,6 +15035,7 @@ function CanvasImpl({
|
|
|
15634
15035
|
const unsubscribeErrorRef = React__namespace.useRef(null);
|
|
15635
15036
|
useIsomorphicLayoutEffect(() => {
|
|
15636
15037
|
effectActiveRef.current = true;
|
|
15038
|
+
if (fallbackVisible) return;
|
|
15637
15039
|
const canvas = canvasRef.current;
|
|
15638
15040
|
if (effectiveSize.width > 0 && effectiveSize.height > 0 && canvas) {
|
|
15639
15041
|
if (!root.current) {
|
|
@@ -15654,7 +15056,7 @@ function CanvasImpl({
|
|
|
15654
15056
|
}
|
|
15655
15057
|
async function run() {
|
|
15656
15058
|
if (!effectActiveRef.current || !root.current) return;
|
|
15657
|
-
await root.current.configure({
|
|
15059
|
+
const configured = await root.current.configure({
|
|
15658
15060
|
id,
|
|
15659
15061
|
primaryCanvas,
|
|
15660
15062
|
scheduler,
|
|
@@ -15669,6 +15071,8 @@ function CanvasImpl({
|
|
|
15669
15071
|
performance,
|
|
15670
15072
|
raycaster,
|
|
15671
15073
|
camera,
|
|
15074
|
+
autoUpdateFrustum,
|
|
15075
|
+
occlusion,
|
|
15672
15076
|
size: effectiveSize,
|
|
15673
15077
|
// Store size props for reset functionality
|
|
15674
15078
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
@@ -15693,8 +15097,20 @@ function CanvasImpl({
|
|
|
15693
15097
|
}
|
|
15694
15098
|
onCreated?.(state);
|
|
15695
15099
|
}
|
|
15696
|
-
})
|
|
15697
|
-
|
|
15100
|
+
}).then(
|
|
15101
|
+
() => true,
|
|
15102
|
+
// Renderer setup failed (e.g. no WebGL/WebGPU support). The `fallback` prop lives
|
|
15103
|
+
// inside <canvas>, which browsers don't display, so surface it as visible DOM
|
|
15104
|
+
// instead; with no fallback, rethrow to an external error boundary. (#3757)
|
|
15105
|
+
(setupError) => {
|
|
15106
|
+
if (effectActiveRef.current) {
|
|
15107
|
+
if (fallback != null) setFallbackVisible(true);
|
|
15108
|
+
else setError(setupError);
|
|
15109
|
+
}
|
|
15110
|
+
return false;
|
|
15111
|
+
}
|
|
15112
|
+
);
|
|
15113
|
+
if (!configured || !effectActiveRef.current || !root.current) return;
|
|
15698
15114
|
root.current.render(
|
|
15699
15115
|
/* @__PURE__ */ jsxRuntime.jsx(Bridge, { children: /* @__PURE__ */ jsxRuntime.jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */ jsxRuntime.jsxs(React__namespace.Suspense, { fallback: /* @__PURE__ */ jsxRuntime.jsx(Block, { set: setBlock }), children: [
|
|
15700
15116
|
backgroundProps && /* @__PURE__ */ jsxRuntime.jsx(Environment, { ...backgroundProps }),
|
|
@@ -15728,20 +15144,13 @@ function CanvasImpl({
|
|
|
15728
15144
|
const handleHMR = () => {
|
|
15729
15145
|
queueMicrotask(() => {
|
|
15730
15146
|
const rootEntry = _roots.get(canvas);
|
|
15731
|
-
if (rootEntry?.store)
|
|
15732
|
-
rootEntry.store.setState((state) => ({
|
|
15733
|
-
nodes: {},
|
|
15734
|
-
uniforms: {},
|
|
15735
|
-
_hmrVersion: state._hmrVersion + 1
|
|
15736
|
-
}));
|
|
15737
|
-
}
|
|
15147
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15738
15148
|
});
|
|
15739
15149
|
};
|
|
15740
15150
|
if (typeof ({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)) }) !== "undefined" && undefined) {
|
|
15741
15151
|
const hot = undefined;
|
|
15742
15152
|
hot.on("vite:afterUpdate", handleHMR);
|
|
15743
|
-
return () => hot.
|
|
15744
|
-
});
|
|
15153
|
+
return () => hot.off?.("vite:afterUpdate", handleHMR);
|
|
15745
15154
|
}
|
|
15746
15155
|
if (typeof module !== "undefined" && module.hot) {
|
|
15747
15156
|
const hot = module.hot;
|
|
@@ -15764,7 +15173,20 @@ function CanvasImpl({
|
|
|
15764
15173
|
...style
|
|
15765
15174
|
},
|
|
15766
15175
|
...props,
|
|
15767
|
-
children:
|
|
15176
|
+
children: fallbackVisible ? (
|
|
15177
|
+
// Renderer setup failed: render the fallback as visible DOM. Inside <canvas> (below)
|
|
15178
|
+
// it exists in the tree but browsers never paint it, which is the whole bug (#3757).
|
|
15179
|
+
fallback
|
|
15180
|
+
) : /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
15181
|
+
"canvas",
|
|
15182
|
+
{
|
|
15183
|
+
ref: canvasRef,
|
|
15184
|
+
id,
|
|
15185
|
+
className: "r3f-canvas",
|
|
15186
|
+
style: { display: "block", width: "100%", height: "100%" },
|
|
15187
|
+
children: fallback
|
|
15188
|
+
}
|
|
15189
|
+
) })
|
|
15768
15190
|
}
|
|
15769
15191
|
);
|
|
15770
15192
|
}
|
|
@@ -15774,6 +15196,8 @@ function Canvas(props) {
|
|
|
15774
15196
|
|
|
15775
15197
|
extend(THREE);
|
|
15776
15198
|
|
|
15199
|
+
exports.Scheduler = scheduler.Scheduler;
|
|
15200
|
+
exports.getScheduler = scheduler.getScheduler;
|
|
15777
15201
|
exports.Block = Block;
|
|
15778
15202
|
exports.Canvas = Canvas;
|
|
15779
15203
|
exports.Environment = Environment;
|
|
@@ -15789,10 +15213,8 @@ exports.R3F_BUILD_LEGACY = R3F_BUILD_LEGACY;
|
|
|
15789
15213
|
exports.R3F_BUILD_WEBGPU = R3F_BUILD_WEBGPU;
|
|
15790
15214
|
exports.REACT_INTERNAL_PROPS = REACT_INTERNAL_PROPS;
|
|
15791
15215
|
exports.RESERVED_PROPS = RESERVED_PROPS;
|
|
15792
|
-
exports.Scheduler = Scheduler;
|
|
15793
15216
|
exports.Texture = Texture;
|
|
15794
15217
|
exports._roots = _roots;
|
|
15795
|
-
exports.act = act;
|
|
15796
15218
|
exports.addAfterEffect = addAfterEffect;
|
|
15797
15219
|
exports.addEffect = addEffect;
|
|
15798
15220
|
exports.addTail = addTail;
|
|
@@ -15819,7 +15241,6 @@ exports.getInstanceProps = getInstanceProps;
|
|
|
15819
15241
|
exports.getPrimary = getPrimary;
|
|
15820
15242
|
exports.getPrimaryIds = getPrimaryIds;
|
|
15821
15243
|
exports.getRootState = getRootState;
|
|
15822
|
-
exports.getScheduler = getScheduler;
|
|
15823
15244
|
exports.getUuidPrefix = getUuidPrefix;
|
|
15824
15245
|
exports.hasConstructor = hasConstructor;
|
|
15825
15246
|
exports.hasPrimary = hasPrimary;
|