@react-three/fiber 10.0.0-canary.dbbe704 → 10.0.0-canary.e881a4a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +714 -1383
- package/dist/index.d.cts +291 -806
- package/dist/index.d.mts +291 -806
- package/dist/index.d.ts +291 -806
- package/dist/index.mjs +689 -1357
- package/dist/legacy.cjs +669 -1344
- package/dist/legacy.d.cts +292 -806
- package/dist/legacy.d.mts +292 -806
- package/dist/legacy.d.ts +292 -806
- package/dist/legacy.mjs +643 -1317
- package/dist/webgpu/index.cjs +1170 -1698
- package/dist/webgpu/index.d.cts +407 -820
- package/dist/webgpu/index.d.mts +407 -820
- package/dist/webgpu/index.d.ts +407 -820
- package/dist/webgpu/index.mjs +1139 -1670
- 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,9 +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
|
|
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);
|
|
437
457
|
const act = React__namespace["act"];
|
|
438
458
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
439
459
|
function useMutableCallback(fn) {
|
|
@@ -465,7 +485,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
465
485
|
return _a = class extends React__namespace.Component {
|
|
466
486
|
constructor() {
|
|
467
487
|
super(...arguments);
|
|
468
|
-
__publicField
|
|
488
|
+
__publicField(this, "state", { error: false });
|
|
469
489
|
}
|
|
470
490
|
componentDidCatch(err) {
|
|
471
491
|
this.props.set(err);
|
|
@@ -473,7 +493,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
473
493
|
render() {
|
|
474
494
|
return this.state.error ? null : this.props.children;
|
|
475
495
|
}
|
|
476
|
-
}, __publicField
|
|
496
|
+
}, __publicField(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
477
497
|
})();
|
|
478
498
|
|
|
479
499
|
const is = {
|
|
@@ -610,8 +630,7 @@ function prepare(target, root, type, props) {
|
|
|
610
630
|
object,
|
|
611
631
|
eventCount: 0,
|
|
612
632
|
handlers: {},
|
|
613
|
-
isHidden: false
|
|
614
|
-
deferredRefs: []
|
|
633
|
+
isHidden: false
|
|
615
634
|
};
|
|
616
635
|
if (object) object.__r3f = instance;
|
|
617
636
|
}
|
|
@@ -768,6 +787,17 @@ function unregisterVisibility(store, object) {
|
|
|
768
787
|
internal.visibilityRegistry.delete(object.uuid);
|
|
769
788
|
internal.occlusionCache.delete(object);
|
|
770
789
|
}
|
|
790
|
+
function updateVisibilityHandlers(store, object, handlers) {
|
|
791
|
+
const { internal } = store.getState();
|
|
792
|
+
const entry = internal.visibilityRegistry.get(object.uuid);
|
|
793
|
+
if (!entry) return false;
|
|
794
|
+
entry.handlers = handlers;
|
|
795
|
+
if ((handlers.onOccluded || handlers.onVisible) && !internal.occlusionEnabled) {
|
|
796
|
+
object.occlusionTest = true;
|
|
797
|
+
enableOcclusion(store);
|
|
798
|
+
}
|
|
799
|
+
return true;
|
|
800
|
+
}
|
|
771
801
|
function checkVisibility(state) {
|
|
772
802
|
const { internal, camera } = state;
|
|
773
803
|
const registry = internal.visibilityRegistry;
|
|
@@ -955,10 +985,7 @@ function applyProps(object, props) {
|
|
|
955
985
|
continue;
|
|
956
986
|
}
|
|
957
987
|
if (value === void 0) continue;
|
|
958
|
-
if (isFromRef(value))
|
|
959
|
-
instance?.deferredRefs?.push({ prop, ref: value[FROM_REF] });
|
|
960
|
-
continue;
|
|
961
|
-
}
|
|
988
|
+
if (isFromRef(value)) continue;
|
|
962
989
|
if (isOnce(value)) {
|
|
963
990
|
if (instance?.appliedOnce?.has(prop)) continue;
|
|
964
991
|
if (instance) {
|
|
@@ -994,8 +1021,24 @@ function applyProps(object, props) {
|
|
|
994
1021
|
} else if (isVectorLike(target) && is.num(value)) {
|
|
995
1022
|
if ("setScalar" in target && typeof target.setScalar === "function") target.setScalar(value);
|
|
996
1023
|
else target.set(value);
|
|
1024
|
+
} else if (root instanceof webgpu.ShaderMaterial && key === "uniforms" && is.obj(value)) {
|
|
1025
|
+
if (!is.obj(root.uniforms)) root.uniforms = {};
|
|
1026
|
+
const uniforms = root.uniforms;
|
|
1027
|
+
const nextUniforms = value;
|
|
1028
|
+
for (const name in nextUniforms) {
|
|
1029
|
+
const uniform = nextUniforms[name];
|
|
1030
|
+
const targetUniform = uniforms[name];
|
|
1031
|
+
if (targetUniform) Object.assign(targetUniform, uniform);
|
|
1032
|
+
else {
|
|
1033
|
+
const nextUniform = uniform instanceof webgpu.Uniform ? uniform.clone() : new webgpu.Uniform(uniform.value);
|
|
1034
|
+
uniforms[name] = nextUniform;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
997
1037
|
} else {
|
|
998
1038
|
root[key] = value;
|
|
1039
|
+
if (key.endsWith("Node") && root.isMaterial) {
|
|
1040
|
+
root.needsUpdate = true;
|
|
1041
|
+
}
|
|
999
1042
|
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
1043
|
root[key].format === webgpu.RGBAFormat && root[key].type === webgpu.UnsignedByteType) {
|
|
1001
1044
|
root[key].colorSpace = rootState.textureColorSpace;
|
|
@@ -1009,6 +1052,9 @@ function applyProps(object, props) {
|
|
|
1009
1052
|
if (instance.eventCount && object2.raycast !== null) {
|
|
1010
1053
|
rootState.internal.interaction.push(object2);
|
|
1011
1054
|
}
|
|
1055
|
+
}
|
|
1056
|
+
if (instance?.parent && rootState?.internal && instance.object?.isObject3D) {
|
|
1057
|
+
const object2 = instance.object;
|
|
1012
1058
|
const root = findInitialRoot(instance);
|
|
1013
1059
|
const visibilityHandlers = {
|
|
1014
1060
|
onFramed: instance.handlers.onFramed,
|
|
@@ -1016,7 +1062,9 @@ function applyProps(object, props) {
|
|
|
1016
1062
|
onVisible: instance.handlers.onVisible
|
|
1017
1063
|
};
|
|
1018
1064
|
if (hasVisibilityHandlers(visibilityHandlers)) {
|
|
1019
|
-
|
|
1065
|
+
if (!updateVisibilityHandlers(root, object2, visibilityHandlers)) {
|
|
1066
|
+
registerVisibility(root, object2, visibilityHandlers);
|
|
1067
|
+
}
|
|
1020
1068
|
} else {
|
|
1021
1069
|
unregisterVisibility(root, object2);
|
|
1022
1070
|
}
|
|
@@ -1029,38 +1077,60 @@ function applyProps(object, props) {
|
|
|
1029
1077
|
return object;
|
|
1030
1078
|
}
|
|
1031
1079
|
|
|
1080
|
+
const DEFAULT_POINTER_ID = 0;
|
|
1081
|
+
const XR_POINTER_ID_START = 1e3;
|
|
1082
|
+
function getPointerState(internal, pointerId) {
|
|
1083
|
+
let state = internal.pointerMap.get(pointerId);
|
|
1084
|
+
if (!state) {
|
|
1085
|
+
state = {
|
|
1086
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1087
|
+
captured: /* @__PURE__ */ new Map(),
|
|
1088
|
+
initialClick: [0, 0],
|
|
1089
|
+
initialHits: []
|
|
1090
|
+
};
|
|
1091
|
+
internal.pointerMap.set(pointerId, state);
|
|
1092
|
+
}
|
|
1093
|
+
return state;
|
|
1094
|
+
}
|
|
1095
|
+
function getPointerId(event) {
|
|
1096
|
+
return "pointerId" in event ? event.pointerId : DEFAULT_POINTER_ID;
|
|
1097
|
+
}
|
|
1032
1098
|
function makeId(event) {
|
|
1033
1099
|
return (event.eventObject || event.object).uuid + "/" + event.index + event.instanceId;
|
|
1034
1100
|
}
|
|
1035
|
-
function releaseInternalPointerCapture(
|
|
1036
|
-
const
|
|
1101
|
+
function releaseInternalPointerCapture(internal, obj, pointerId) {
|
|
1102
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1103
|
+
if (!pointerState) return;
|
|
1104
|
+
const captureData = pointerState.captured.get(obj);
|
|
1037
1105
|
if (captureData) {
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
capturedMap.delete(pointerId);
|
|
1041
|
-
captureData.target.releasePointerCapture(pointerId);
|
|
1042
|
-
}
|
|
1106
|
+
pointerState.captured.delete(obj);
|
|
1107
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
1043
1108
|
}
|
|
1044
1109
|
}
|
|
1045
1110
|
function removeInteractivity(store, object) {
|
|
1046
1111
|
const { internal } = store.getState();
|
|
1047
1112
|
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1113
|
+
for (const [pointerId, pointerState] of internal.pointerMap) {
|
|
1114
|
+
pointerState.initialHits = pointerState.initialHits.filter((o) => o !== object);
|
|
1115
|
+
pointerState.hovered.forEach((value, key) => {
|
|
1116
|
+
if (value.eventObject === object || value.object === object) {
|
|
1117
|
+
pointerState.hovered.delete(key);
|
|
1118
|
+
}
|
|
1119
|
+
});
|
|
1120
|
+
if (pointerState.captured.has(object)) {
|
|
1121
|
+
releaseInternalPointerCapture(internal, object, pointerId);
|
|
1052
1122
|
}
|
|
1053
|
-
}
|
|
1054
|
-
internal.capturedMap.forEach((captures, pointerId) => {
|
|
1055
|
-
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
1056
|
-
});
|
|
1123
|
+
}
|
|
1057
1124
|
unregisterVisibility(store, object);
|
|
1058
1125
|
}
|
|
1059
1126
|
function createEvents(store) {
|
|
1060
|
-
function calculateDistance(event) {
|
|
1127
|
+
function calculateDistance(event, pointerId) {
|
|
1061
1128
|
const { internal } = store.getState();
|
|
1062
|
-
const
|
|
1063
|
-
|
|
1129
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1130
|
+
if (!pointerState) return 0;
|
|
1131
|
+
const [initialX, initialY] = pointerState.initialClick;
|
|
1132
|
+
const dx = event.offsetX - initialX;
|
|
1133
|
+
const dy = event.offsetY - initialY;
|
|
1064
1134
|
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
1065
1135
|
}
|
|
1066
1136
|
function filterPointerEvents(objects) {
|
|
@@ -1096,6 +1166,15 @@ function createEvents(store) {
|
|
|
1096
1166
|
return state2.raycaster.camera ? state2.raycaster.intersectObject(obj, true) : [];
|
|
1097
1167
|
}
|
|
1098
1168
|
let hits = eventsObjects.flatMap(handleRaycast).sort((a, b) => {
|
|
1169
|
+
const aInteractivePriority = a.object.userData?.interactivePriority;
|
|
1170
|
+
const bInteractivePriority = b.object.userData?.interactivePriority;
|
|
1171
|
+
if (aInteractivePriority !== void 0 || bInteractivePriority !== void 0) {
|
|
1172
|
+
if (aInteractivePriority !== void 0 && bInteractivePriority === void 0) return -1;
|
|
1173
|
+
if (bInteractivePriority !== void 0 && aInteractivePriority === void 0) return 1;
|
|
1174
|
+
if (aInteractivePriority !== bInteractivePriority) {
|
|
1175
|
+
return (bInteractivePriority ?? 0) - (aInteractivePriority ?? 0);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1099
1178
|
const aState = getRootState(a.object);
|
|
1100
1179
|
const bState = getRootState(b.object);
|
|
1101
1180
|
const aPriority = aState?.events?.priority ?? 1;
|
|
@@ -1117,9 +1196,13 @@ function createEvents(store) {
|
|
|
1117
1196
|
eventObject = eventObject.parent;
|
|
1118
1197
|
}
|
|
1119
1198
|
}
|
|
1120
|
-
if ("pointerId" in event
|
|
1121
|
-
|
|
1122
|
-
|
|
1199
|
+
if ("pointerId" in event) {
|
|
1200
|
+
const pointerId = event.pointerId;
|
|
1201
|
+
const pointerState = state.internal.pointerMap.get(pointerId);
|
|
1202
|
+
if (pointerState?.captured.size) {
|
|
1203
|
+
for (const captureData of pointerState.captured.values()) {
|
|
1204
|
+
if (!duplicates.has(makeId(captureData.intersection))) intersections.push(captureData.intersection);
|
|
1205
|
+
}
|
|
1123
1206
|
}
|
|
1124
1207
|
}
|
|
1125
1208
|
return intersections;
|
|
@@ -1132,27 +1215,25 @@ function createEvents(store) {
|
|
|
1132
1215
|
if (state) {
|
|
1133
1216
|
const { raycaster, pointer, camera, internal } = state;
|
|
1134
1217
|
const unprojectedPoint = new webgpu.Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
1135
|
-
const hasPointerCapture = (id) =>
|
|
1218
|
+
const hasPointerCapture = (id) => {
|
|
1219
|
+
const pointerState = internal.pointerMap.get(id);
|
|
1220
|
+
return pointerState?.captured.has(hit.eventObject) ?? false;
|
|
1221
|
+
};
|
|
1136
1222
|
const setPointerCapture = (id) => {
|
|
1137
1223
|
const captureData = { intersection: hit, target: event.target };
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
} else {
|
|
1141
|
-
internal.capturedMap.set(id, /* @__PURE__ */ new Map([[hit.eventObject, captureData]]));
|
|
1142
|
-
}
|
|
1224
|
+
const pointerState = getPointerState(internal, id);
|
|
1225
|
+
pointerState.captured.set(hit.eventObject, captureData);
|
|
1143
1226
|
event.target.setPointerCapture(id);
|
|
1144
1227
|
};
|
|
1145
1228
|
const releasePointerCapture = (id) => {
|
|
1146
|
-
|
|
1147
|
-
if (captures) {
|
|
1148
|
-
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
1149
|
-
}
|
|
1229
|
+
releaseInternalPointerCapture(internal, hit.eventObject, id);
|
|
1150
1230
|
};
|
|
1151
1231
|
const extractEventProps = {};
|
|
1152
1232
|
for (const prop in event) {
|
|
1153
1233
|
const property = event[prop];
|
|
1154
1234
|
if (typeof property !== "function") extractEventProps[prop] = property;
|
|
1155
1235
|
}
|
|
1236
|
+
const eventPointerId = "pointerId" in event ? event.pointerId : void 0;
|
|
1156
1237
|
const raycastEvent = {
|
|
1157
1238
|
...hit,
|
|
1158
1239
|
...extractEventProps,
|
|
@@ -1163,18 +1244,19 @@ function createEvents(store) {
|
|
|
1163
1244
|
unprojectedPoint,
|
|
1164
1245
|
ray: raycaster.ray,
|
|
1165
1246
|
camera,
|
|
1247
|
+
pointerId: eventPointerId,
|
|
1166
1248
|
// Hijack stopPropagation, which just sets a flag
|
|
1167
1249
|
stopPropagation() {
|
|
1168
|
-
const
|
|
1250
|
+
const pointerState = eventPointerId !== void 0 ? internal.pointerMap.get(eventPointerId) : void 0;
|
|
1169
1251
|
if (
|
|
1170
1252
|
// ...if this pointer hasn't been captured
|
|
1171
|
-
!
|
|
1172
|
-
|
|
1253
|
+
!pointerState?.captured.size || // ... or if the hit object is capturing the pointer
|
|
1254
|
+
pointerState.captured.has(hit.eventObject)
|
|
1173
1255
|
) {
|
|
1174
1256
|
raycastEvent.stopped = localState.stopped = true;
|
|
1175
|
-
if (
|
|
1257
|
+
if (pointerState?.hovered.size && Array.from(pointerState.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
1176
1258
|
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
1177
|
-
cancelPointer([...higher, hit]);
|
|
1259
|
+
cancelPointer([...higher, hit], eventPointerId);
|
|
1178
1260
|
}
|
|
1179
1261
|
}
|
|
1180
1262
|
},
|
|
@@ -1190,15 +1272,18 @@ function createEvents(store) {
|
|
|
1190
1272
|
}
|
|
1191
1273
|
return intersections;
|
|
1192
1274
|
}
|
|
1193
|
-
function cancelPointer(intersections) {
|
|
1275
|
+
function cancelPointer(intersections, pointerId) {
|
|
1194
1276
|
const { internal } = store.getState();
|
|
1195
|
-
|
|
1277
|
+
const pid = pointerId ?? DEFAULT_POINTER_ID;
|
|
1278
|
+
const pointerState = internal.pointerMap.get(pid);
|
|
1279
|
+
if (!pointerState) return;
|
|
1280
|
+
for (const [hoveredId, hoveredObj] of pointerState.hovered) {
|
|
1196
1281
|
if (!intersections.length || !intersections.find(
|
|
1197
1282
|
(hit) => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId
|
|
1198
1283
|
)) {
|
|
1199
1284
|
const eventObject = hoveredObj.eventObject;
|
|
1200
1285
|
const instance = eventObject.__r3f;
|
|
1201
|
-
|
|
1286
|
+
pointerState.hovered.delete(hoveredId);
|
|
1202
1287
|
if (instance?.eventCount) {
|
|
1203
1288
|
const handlers = instance.handlers;
|
|
1204
1289
|
const data = { ...hoveredObj, intersections };
|
|
@@ -1227,41 +1312,118 @@ function createEvents(store) {
|
|
|
1227
1312
|
instance?.handlers.onDropMissed?.(event);
|
|
1228
1313
|
}
|
|
1229
1314
|
}
|
|
1315
|
+
function cleanupPointer(pointerId) {
|
|
1316
|
+
const { internal } = store.getState();
|
|
1317
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1318
|
+
if (pointerState) {
|
|
1319
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1320
|
+
const eventObject = hoveredObj.eventObject;
|
|
1321
|
+
const instance = eventObject.__r3f;
|
|
1322
|
+
if (instance?.eventCount) {
|
|
1323
|
+
const handlers = instance.handlers;
|
|
1324
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1325
|
+
handlers.onPointerOut?.(data);
|
|
1326
|
+
handlers.onPointerLeave?.(data);
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
internal.pointerMap.delete(pointerId);
|
|
1330
|
+
}
|
|
1331
|
+
internal.pointerDirty.delete(pointerId);
|
|
1332
|
+
}
|
|
1333
|
+
function processDeferredPointer(event, pointerId) {
|
|
1334
|
+
const state = store.getState();
|
|
1335
|
+
const { internal } = state;
|
|
1336
|
+
if (!state.events.enabled) return;
|
|
1337
|
+
const filter = filterPointerEvents;
|
|
1338
|
+
const hits = intersect(event, filter);
|
|
1339
|
+
cancelPointer(hits, pointerId);
|
|
1340
|
+
function onIntersect(data) {
|
|
1341
|
+
const eventObject = data.eventObject;
|
|
1342
|
+
const instance = eventObject.__r3f;
|
|
1343
|
+
if (!instance?.eventCount) return;
|
|
1344
|
+
const handlers = instance.handlers;
|
|
1345
|
+
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1346
|
+
const id = makeId(data);
|
|
1347
|
+
const pointerState = getPointerState(internal, pointerId);
|
|
1348
|
+
const hoveredItem = pointerState.hovered.get(id);
|
|
1349
|
+
if (!hoveredItem) {
|
|
1350
|
+
pointerState.hovered.set(id, data);
|
|
1351
|
+
handlers.onPointerOver?.(data);
|
|
1352
|
+
handlers.onPointerEnter?.(data);
|
|
1353
|
+
} else if (hoveredItem.stopped) {
|
|
1354
|
+
data.stopPropagation();
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
handlers.onPointerMove?.(data);
|
|
1358
|
+
}
|
|
1359
|
+
handleIntersects(hits, event, 0, onIntersect);
|
|
1360
|
+
}
|
|
1230
1361
|
function handlePointer(name) {
|
|
1231
1362
|
switch (name) {
|
|
1232
1363
|
case "onPointerLeave":
|
|
1233
|
-
case "onPointerCancel":
|
|
1234
1364
|
case "onDragLeave":
|
|
1235
1365
|
return () => cancelPointer([]);
|
|
1366
|
+
// Global cancel of these events
|
|
1367
|
+
case "onPointerCancel":
|
|
1368
|
+
return (event) => {
|
|
1369
|
+
const pointerId = getPointerId(event);
|
|
1370
|
+
cleanupPointer(pointerId);
|
|
1371
|
+
};
|
|
1236
1372
|
case "onLostPointerCapture":
|
|
1237
1373
|
return (event) => {
|
|
1238
1374
|
const { internal } = store.getState();
|
|
1239
|
-
|
|
1375
|
+
const pointerId = getPointerId(event);
|
|
1376
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1377
|
+
if (pointerState?.captured.size) {
|
|
1240
1378
|
requestAnimationFrame(() => {
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1379
|
+
const pointerState2 = internal.pointerMap.get(pointerId);
|
|
1380
|
+
if (pointerState2?.captured.size) {
|
|
1381
|
+
pointerState2.captured.clear();
|
|
1244
1382
|
}
|
|
1383
|
+
cancelPointer([], pointerId);
|
|
1245
1384
|
});
|
|
1246
1385
|
}
|
|
1247
1386
|
};
|
|
1248
1387
|
}
|
|
1249
1388
|
return function handleEvent(event) {
|
|
1250
1389
|
const state = store.getState();
|
|
1251
|
-
const { onPointerMissed, onDragOverMissed, onDropMissed, internal } = state;
|
|
1390
|
+
const { onPointerMissed, onDragOverMissed, onDropMissed, internal, events } = state;
|
|
1391
|
+
const pointerId = getPointerId(event);
|
|
1252
1392
|
internal.lastEvent.current = event;
|
|
1253
|
-
if (!
|
|
1393
|
+
if (!events.enabled) return;
|
|
1254
1394
|
const isPointerMove = name === "onPointerMove";
|
|
1255
1395
|
const isDragOver = name === "onDragOver";
|
|
1256
1396
|
const isDrop = name === "onDrop";
|
|
1257
1397
|
const isClickEvent = name === "onClick" || name === "onContextMenu" || name === "onDoubleClick";
|
|
1398
|
+
const isPointerDown = name === "onPointerDown";
|
|
1399
|
+
const isPointerUp = name === "onPointerUp";
|
|
1400
|
+
const isWheel = name === "onWheel";
|
|
1401
|
+
const canDeferRaycasts = events.frameTimedRaycasts && state.frameloop === "always";
|
|
1402
|
+
if (isPointerMove && canDeferRaycasts) {
|
|
1403
|
+
events.compute?.(event, state);
|
|
1404
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1405
|
+
return;
|
|
1406
|
+
}
|
|
1407
|
+
if (isWheel && canDeferRaycasts && !events.alwaysFireOnScroll) {
|
|
1408
|
+
events.compute?.(event, state);
|
|
1409
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
if ((isClickEvent || isPointerDown || isPointerUp) && internal.pointerDirty.has(pointerId)) {
|
|
1413
|
+
const deferredEvent = internal.pointerDirty.get(pointerId);
|
|
1414
|
+
internal.pointerDirty.delete(pointerId);
|
|
1415
|
+
processDeferredPointer(deferredEvent, pointerId);
|
|
1416
|
+
}
|
|
1258
1417
|
const filter = isPointerMove || isDragOver || isDrop ? filterPointerEvents : void 0;
|
|
1259
1418
|
const hits = intersect(event, filter);
|
|
1260
|
-
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
1261
|
-
if (
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1419
|
+
const delta = isClickEvent ? calculateDistance(event, pointerId) : 0;
|
|
1420
|
+
if (isPointerDown) {
|
|
1421
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1422
|
+
pointerState2.initialClick = [event.offsetX, event.offsetY];
|
|
1423
|
+
pointerState2.initialHits = hits.map((hit) => hit.eventObject);
|
|
1424
|
+
}
|
|
1425
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1426
|
+
const initialHits = pointerState?.initialHits ?? [];
|
|
1265
1427
|
if (isClickEvent && !hits.length) {
|
|
1266
1428
|
if (delta <= 2) {
|
|
1267
1429
|
pointerMissed(event, internal.interaction);
|
|
@@ -1276,7 +1438,9 @@ function createEvents(store) {
|
|
|
1276
1438
|
dropMissed(event, internal.interaction);
|
|
1277
1439
|
if (onDropMissed) onDropMissed(event);
|
|
1278
1440
|
}
|
|
1279
|
-
if (isPointerMove || isDragOver)
|
|
1441
|
+
if (isPointerMove || isDragOver) {
|
|
1442
|
+
cancelPointer(hits, pointerId);
|
|
1443
|
+
}
|
|
1280
1444
|
function onIntersect(data) {
|
|
1281
1445
|
const eventObject = data.eventObject;
|
|
1282
1446
|
const instance = eventObject.__r3f;
|
|
@@ -1285,9 +1449,10 @@ function createEvents(store) {
|
|
|
1285
1449
|
if (isPointerMove) {
|
|
1286
1450
|
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1287
1451
|
const id = makeId(data);
|
|
1288
|
-
const
|
|
1452
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1453
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1289
1454
|
if (!hoveredItem) {
|
|
1290
|
-
|
|
1455
|
+
pointerState2.hovered.set(id, data);
|
|
1291
1456
|
handlers.onPointerOver?.(data);
|
|
1292
1457
|
handlers.onPointerEnter?.(data);
|
|
1293
1458
|
} else if (hoveredItem.stopped) {
|
|
@@ -1297,9 +1462,10 @@ function createEvents(store) {
|
|
|
1297
1462
|
handlers.onPointerMove?.(data);
|
|
1298
1463
|
} else if (isDragOver) {
|
|
1299
1464
|
const id = makeId(data);
|
|
1300
|
-
const
|
|
1465
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1466
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1301
1467
|
if (!hoveredItem) {
|
|
1302
|
-
|
|
1468
|
+
pointerState2.hovered.set(id, data);
|
|
1303
1469
|
handlers.onDragOverEnter?.(data);
|
|
1304
1470
|
} else if (hoveredItem.stopped) {
|
|
1305
1471
|
data.stopPropagation();
|
|
@@ -1310,18 +1476,18 @@ function createEvents(store) {
|
|
|
1310
1476
|
} else {
|
|
1311
1477
|
const handler = handlers[name];
|
|
1312
1478
|
if (handler) {
|
|
1313
|
-
if (!isClickEvent ||
|
|
1479
|
+
if (!isClickEvent || initialHits.includes(eventObject)) {
|
|
1314
1480
|
pointerMissed(
|
|
1315
1481
|
event,
|
|
1316
|
-
internal.interaction.filter((object) => !
|
|
1482
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1317
1483
|
);
|
|
1318
1484
|
handler(data);
|
|
1319
1485
|
}
|
|
1320
1486
|
} else {
|
|
1321
|
-
if (isClickEvent &&
|
|
1487
|
+
if (isClickEvent && initialHits.includes(eventObject)) {
|
|
1322
1488
|
pointerMissed(
|
|
1323
1489
|
event,
|
|
1324
|
-
internal.interaction.filter((object) => !
|
|
1490
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1325
1491
|
);
|
|
1326
1492
|
}
|
|
1327
1493
|
}
|
|
@@ -1330,7 +1496,15 @@ function createEvents(store) {
|
|
|
1330
1496
|
handleIntersects(hits, event, delta, onIntersect);
|
|
1331
1497
|
};
|
|
1332
1498
|
}
|
|
1333
|
-
|
|
1499
|
+
function flushDeferredPointers() {
|
|
1500
|
+
const { internal, events } = store.getState();
|
|
1501
|
+
if (!events.frameTimedRaycasts) return;
|
|
1502
|
+
for (const [pointerId, event] of internal.pointerDirty) {
|
|
1503
|
+
processDeferredPointer(event, pointerId);
|
|
1504
|
+
}
|
|
1505
|
+
internal.pointerDirty.clear();
|
|
1506
|
+
}
|
|
1507
|
+
return { handlePointer, flushDeferredPointers, processDeferredPointer };
|
|
1334
1508
|
}
|
|
1335
1509
|
const DOM_EVENTS = {
|
|
1336
1510
|
onClick: ["click", false],
|
|
@@ -1349,10 +1523,15 @@ const DOM_EVENTS = {
|
|
|
1349
1523
|
onLostPointerCapture: ["lostpointercapture", true]
|
|
1350
1524
|
};
|
|
1351
1525
|
function createPointerEvents(store) {
|
|
1352
|
-
const { handlePointer } = createEvents(store);
|
|
1526
|
+
const { handlePointer, flushDeferredPointers, processDeferredPointer } = createEvents(store);
|
|
1527
|
+
let nextXRPointerId = XR_POINTER_ID_START;
|
|
1528
|
+
const xrPointers = /* @__PURE__ */ new Map();
|
|
1353
1529
|
return {
|
|
1354
1530
|
priority: 1,
|
|
1355
1531
|
enabled: true,
|
|
1532
|
+
frameTimedRaycasts: true,
|
|
1533
|
+
alwaysFireOnScroll: true,
|
|
1534
|
+
updateOnFrame: false,
|
|
1356
1535
|
compute(event, state) {
|
|
1357
1536
|
state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
1358
1537
|
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
@@ -1362,11 +1541,33 @@ function createPointerEvents(store) {
|
|
|
1362
1541
|
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
1363
1542
|
{}
|
|
1364
1543
|
),
|
|
1365
|
-
update: () => {
|
|
1544
|
+
update: (pointerId) => {
|
|
1366
1545
|
const { events, internal } = store.getState();
|
|
1367
|
-
if (
|
|
1546
|
+
if (!events.handlers) return;
|
|
1547
|
+
if (pointerId !== void 0) {
|
|
1548
|
+
const event = internal.pointerDirty.get(pointerId);
|
|
1549
|
+
if (event) {
|
|
1550
|
+
internal.pointerDirty.delete(pointerId);
|
|
1551
|
+
processDeferredPointer(event, pointerId);
|
|
1552
|
+
} else if (internal.lastEvent?.current) {
|
|
1553
|
+
processDeferredPointer(internal.lastEvent.current, pointerId);
|
|
1554
|
+
}
|
|
1555
|
+
} else {
|
|
1556
|
+
flushDeferredPointers();
|
|
1557
|
+
if (internal.lastEvent?.current) {
|
|
1558
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
},
|
|
1562
|
+
flush: () => {
|
|
1563
|
+
const { events, internal } = store.getState();
|
|
1564
|
+
flushDeferredPointers();
|
|
1565
|
+
if (events.updateOnFrame && internal.lastEvent?.current && events.handlers) {
|
|
1566
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1567
|
+
}
|
|
1368
1568
|
},
|
|
1369
1569
|
connect: (target) => {
|
|
1570
|
+
if (!target) return;
|
|
1370
1571
|
const { set, events } = store.getState();
|
|
1371
1572
|
events.disconnect?.();
|
|
1372
1573
|
set((state) => ({ events: { ...state.events, connected: target } }));
|
|
@@ -1390,6 +1591,33 @@ function createPointerEvents(store) {
|
|
|
1390
1591
|
}
|
|
1391
1592
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
1392
1593
|
}
|
|
1594
|
+
},
|
|
1595
|
+
// PREVIEW (A8): the XR pointer registration API may change before stable.
|
|
1596
|
+
registerPointer: (config) => {
|
|
1597
|
+
const pointerId = nextXRPointerId++;
|
|
1598
|
+
xrPointers.set(pointerId, config);
|
|
1599
|
+
const { internal } = store.getState();
|
|
1600
|
+
getPointerState(internal, pointerId);
|
|
1601
|
+
return pointerId;
|
|
1602
|
+
},
|
|
1603
|
+
unregisterPointer: (pointerId) => {
|
|
1604
|
+
xrPointers.delete(pointerId);
|
|
1605
|
+
const { internal } = store.getState();
|
|
1606
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1607
|
+
if (pointerState) {
|
|
1608
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1609
|
+
const eventObject = hoveredObj.eventObject;
|
|
1610
|
+
const instance = eventObject.__r3f;
|
|
1611
|
+
if (instance?.eventCount) {
|
|
1612
|
+
const handlers = instance.handlers;
|
|
1613
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1614
|
+
handlers.onPointerOut?.(data);
|
|
1615
|
+
handlers.onPointerLeave?.(data);
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
internal.pointerMap.delete(pointerId);
|
|
1619
|
+
}
|
|
1620
|
+
internal.pointerDirty.delete(pointerId);
|
|
1393
1621
|
}
|
|
1394
1622
|
};
|
|
1395
1623
|
}
|
|
@@ -1451,1036 +1679,11 @@ function notifyAlpha({ message, link }) {
|
|
|
1451
1679
|
}
|
|
1452
1680
|
}
|
|
1453
1681
|
|
|
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?.();
|
|
1682
|
+
const INTERNAL_LIB_MARKERS = ["zustand", "setState", "Object.assign"];
|
|
1683
|
+
function isInternalRendererAccess(stack, callerDepth = 2) {
|
|
1684
|
+
if (!stack) return false;
|
|
1685
|
+
const caller = stack.split("\n")[callerDepth] ?? "";
|
|
1686
|
+
return INTERNAL_LIB_MARKERS.some((m) => caller.includes(m));
|
|
2484
1687
|
}
|
|
2485
1688
|
|
|
2486
1689
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
@@ -2520,7 +1723,14 @@ const createStore = (invalidate, advance) => {
|
|
|
2520
1723
|
frustum: new webgpu.Frustum(),
|
|
2521
1724
|
autoUpdateFrustum: true,
|
|
2522
1725
|
raycaster: null,
|
|
2523
|
-
events: {
|
|
1726
|
+
events: {
|
|
1727
|
+
priority: 1,
|
|
1728
|
+
enabled: true,
|
|
1729
|
+
connected: false,
|
|
1730
|
+
frameTimedRaycasts: true,
|
|
1731
|
+
alwaysFireOnScroll: true,
|
|
1732
|
+
updateOnFrame: false
|
|
1733
|
+
},
|
|
2524
1734
|
scene: null,
|
|
2525
1735
|
rootScene: null,
|
|
2526
1736
|
xr: null,
|
|
@@ -2585,7 +1795,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2585
1795
|
size: newSize,
|
|
2586
1796
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
2587
1797
|
}));
|
|
2588
|
-
getScheduler().invalidate();
|
|
1798
|
+
scheduler.getScheduler().invalidate();
|
|
2589
1799
|
}
|
|
2590
1800
|
}
|
|
2591
1801
|
return;
|
|
@@ -2600,7 +1810,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2600
1810
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
2601
1811
|
_sizeImperative: true
|
|
2602
1812
|
}));
|
|
2603
|
-
getScheduler().invalidate();
|
|
1813
|
+
scheduler.getScheduler().invalidate();
|
|
2604
1814
|
},
|
|
2605
1815
|
setDpr: (dpr) => set((state2) => {
|
|
2606
1816
|
const resolved = calculateDpr(dpr);
|
|
@@ -2608,14 +1818,18 @@ const createStore = (invalidate, advance) => {
|
|
|
2608
1818
|
}),
|
|
2609
1819
|
setFrameloop: (frameloop = "always") => {
|
|
2610
1820
|
set(() => ({ frameloop }));
|
|
1821
|
+
scheduler.getScheduler().frameloop = frameloop;
|
|
2611
1822
|
},
|
|
2612
1823
|
setError: (error) => set(() => ({ error })),
|
|
2613
1824
|
error: null,
|
|
2614
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useTextures,
|
|
1825
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
2615
1826
|
uniforms: {},
|
|
2616
1827
|
nodes: {},
|
|
1828
|
+
buffers: {},
|
|
1829
|
+
gpuStorage: {},
|
|
2617
1830
|
textures: /* @__PURE__ */ new Map(),
|
|
2618
|
-
|
|
1831
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1832
|
+
renderPipeline: null,
|
|
2619
1833
|
passes: {},
|
|
2620
1834
|
_hmrVersion: 0,
|
|
2621
1835
|
_sizeImperative: false,
|
|
@@ -2624,12 +1838,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2624
1838
|
internal: {
|
|
2625
1839
|
// Events
|
|
2626
1840
|
interaction: [],
|
|
2627
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
2628
1841
|
subscribers: [],
|
|
1842
|
+
// Per-pointer state (new unified structure)
|
|
1843
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1844
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1845
|
+
lastEvent: React__namespace.createRef(),
|
|
1846
|
+
// Deprecated but kept for backwards compatibility
|
|
1847
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
2629
1848
|
initialClick: [0, 0],
|
|
2630
1849
|
initialHits: [],
|
|
2631
1850
|
capturedMap: /* @__PURE__ */ new Map(),
|
|
2632
|
-
lastEvent: React__namespace.createRef(),
|
|
2633
1851
|
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
2634
1852
|
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
2635
1853
|
// Occlusion system (WebGPU only)
|
|
@@ -2668,8 +1886,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2668
1886
|
const currentState = rootStore.getState();
|
|
2669
1887
|
if (!currentState.isLegacy && currentState.internal.actualRenderer) {
|
|
2670
1888
|
const stack = new Error().stack || "";
|
|
2671
|
-
|
|
2672
|
-
if (!isInternalAccess) {
|
|
1889
|
+
if (!isInternalRendererAccess(stack)) {
|
|
2673
1890
|
const cleanedStack = stack.split("\n").slice(2).join("\n") || "Stack trace unavailable";
|
|
2674
1891
|
notifyDepreciated({
|
|
2675
1892
|
heading: "Accessing state.gl in WebGPU mode",
|
|
@@ -2717,14 +1934,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2717
1934
|
oldSize = size;
|
|
2718
1935
|
oldDpr = viewport.dpr;
|
|
2719
1936
|
updateCamera(camera, size);
|
|
2720
|
-
if (canvasTarget) {
|
|
1937
|
+
if (internal.isSecondary && canvasTarget) {
|
|
2721
1938
|
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
2722
|
-
|
|
2723
|
-
canvasTarget.setSize(size.width, size.height, updateStyle);
|
|
1939
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
2724
1940
|
} else {
|
|
2725
1941
|
if (viewport.dpr > 0) actualRenderer.setPixelRatio(viewport.dpr);
|
|
2726
|
-
|
|
2727
|
-
|
|
1942
|
+
actualRenderer.setSize(size.width, size.height, false);
|
|
1943
|
+
if (canvasTarget) {
|
|
1944
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1945
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1946
|
+
}
|
|
2728
1947
|
}
|
|
2729
1948
|
}
|
|
2730
1949
|
if (camera !== oldCamera) {
|
|
@@ -2780,26 +1999,32 @@ function loadingFn(extensions, onProgress) {
|
|
|
2780
1999
|
);
|
|
2781
2000
|
};
|
|
2782
2001
|
}
|
|
2783
|
-
function useLoader(loader, input, extensions, onProgress) {
|
|
2784
|
-
const
|
|
2002
|
+
function useLoader(loader, input, extensions, onProgress, cacheKey) {
|
|
2003
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
2004
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2785
2005
|
const fn = loadingFn(extensions, onProgress);
|
|
2786
|
-
const results =
|
|
2006
|
+
const results = cacheKeys.map(
|
|
2007
|
+
(key, index) => suspendReact.suspend(() => fn(loader, inputs[index]), [loader, key], { equal: is.equ })
|
|
2008
|
+
);
|
|
2787
2009
|
return Array.isArray(input) ? results : results[0];
|
|
2788
2010
|
}
|
|
2789
|
-
useLoader.preload = function(loader, input, extensions, onProgress) {
|
|
2790
|
-
const
|
|
2791
|
-
|
|
2011
|
+
useLoader.preload = function(loader, input, extensions, onProgress, cacheKey) {
|
|
2012
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
2013
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2014
|
+
cacheKeys.forEach(
|
|
2015
|
+
(key, index) => suspendReact.preload(() => loadingFn(extensions, onProgress)(loader, inputs[index]), [loader, key])
|
|
2016
|
+
);
|
|
2792
2017
|
};
|
|
2793
|
-
useLoader.clear = function(loader, input) {
|
|
2794
|
-
const
|
|
2795
|
-
|
|
2018
|
+
useLoader.clear = function(loader, input, cacheKey) {
|
|
2019
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2020
|
+
cacheKeys.forEach((key) => suspendReact.clear([loader, key]));
|
|
2796
2021
|
};
|
|
2797
2022
|
useLoader.loader = getLoader;
|
|
2798
2023
|
|
|
2799
2024
|
function useFrame(callback, priorityOrOptions) {
|
|
2800
2025
|
const store = React__namespace.useContext(context);
|
|
2801
2026
|
const isInsideCanvas = store !== null;
|
|
2802
|
-
const scheduler = getScheduler();
|
|
2027
|
+
const scheduler$1 = scheduler.getScheduler();
|
|
2803
2028
|
const optionsKey = typeof priorityOrOptions === "number" ? `p:${priorityOrOptions}` : priorityOrOptions ? JSON.stringify({
|
|
2804
2029
|
id: priorityOrOptions.id,
|
|
2805
2030
|
phase: priorityOrOptions.phase,
|
|
@@ -2847,7 +2072,7 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2847
2072
|
};
|
|
2848
2073
|
callbackRef.current?.(mergedState, delta);
|
|
2849
2074
|
};
|
|
2850
|
-
const unregister = scheduler.register(wrappedCallback, {
|
|
2075
|
+
const unregister = scheduler$1.register(wrappedCallback, {
|
|
2851
2076
|
id,
|
|
2852
2077
|
rootId,
|
|
2853
2078
|
...options
|
|
@@ -2868,37 +2093,31 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2868
2093
|
}
|
|
2869
2094
|
};
|
|
2870
2095
|
} else {
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
unregisterJob = registerOutside();
|
|
2880
|
-
});
|
|
2881
|
-
return () => {
|
|
2882
|
-
unsubReady();
|
|
2883
|
-
unregisterJob?.();
|
|
2884
|
-
};
|
|
2096
|
+
return scheduler$1.register(
|
|
2097
|
+
(state, delta) => {
|
|
2098
|
+
const frameState = state;
|
|
2099
|
+
if (!frameState.renderer) return;
|
|
2100
|
+
callbackRef.current?.(frameState, delta);
|
|
2101
|
+
},
|
|
2102
|
+
{ id, ...options }
|
|
2103
|
+
);
|
|
2885
2104
|
}
|
|
2886
|
-
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2105
|
+
}, [store, scheduler$1, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2887
2106
|
const isPaused = React__namespace.useSyncExternalStore(
|
|
2888
2107
|
// Subscribe function
|
|
2889
2108
|
React__namespace.useCallback(
|
|
2890
2109
|
(onStoreChange) => {
|
|
2891
|
-
return getScheduler().subscribeJobState(id, onStoreChange);
|
|
2110
|
+
return scheduler.getScheduler().subscribeJobState(id, onStoreChange);
|
|
2892
2111
|
},
|
|
2893
2112
|
[id]
|
|
2894
2113
|
),
|
|
2895
2114
|
// getSnapshot function
|
|
2896
|
-
React__namespace.useCallback(() => getScheduler().isJobPaused(id), [id]),
|
|
2115
|
+
React__namespace.useCallback(() => scheduler.getScheduler().isJobPaused(id), [id]),
|
|
2897
2116
|
// getServerSnapshot function (SSR)
|
|
2898
2117
|
React__namespace.useCallback(() => false, [])
|
|
2899
2118
|
);
|
|
2900
2119
|
const controls = React__namespace.useMemo(() => {
|
|
2901
|
-
const scheduler2 = getScheduler();
|
|
2120
|
+
const scheduler2 = scheduler.getScheduler();
|
|
2902
2121
|
return {
|
|
2903
2122
|
/** The job's unique ID */
|
|
2904
2123
|
id,
|
|
@@ -2913,7 +2132,7 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2913
2132
|
* @param timestamp Optional timestamp (defaults to performance.now())
|
|
2914
2133
|
*/
|
|
2915
2134
|
step: (timestamp) => {
|
|
2916
|
-
getScheduler().stepJob(id, timestamp);
|
|
2135
|
+
scheduler.getScheduler().stepJob(id, timestamp);
|
|
2917
2136
|
},
|
|
2918
2137
|
/**
|
|
2919
2138
|
* Manually step ALL jobs in the scheduler.
|
|
@@ -2921,20 +2140,20 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2921
2140
|
* @param timestamp Optional timestamp (defaults to performance.now())
|
|
2922
2141
|
*/
|
|
2923
2142
|
stepAll: (timestamp) => {
|
|
2924
|
-
getScheduler().step(timestamp);
|
|
2143
|
+
scheduler.getScheduler().step(timestamp);
|
|
2925
2144
|
},
|
|
2926
2145
|
/**
|
|
2927
2146
|
* Pause this job (set enabled=false).
|
|
2928
2147
|
* Job remains registered but won't run.
|
|
2929
2148
|
*/
|
|
2930
2149
|
pause: () => {
|
|
2931
|
-
getScheduler().pauseJob(id);
|
|
2150
|
+
scheduler.getScheduler().pauseJob(id);
|
|
2932
2151
|
},
|
|
2933
2152
|
/**
|
|
2934
2153
|
* Resume this job (set enabled=true).
|
|
2935
2154
|
*/
|
|
2936
2155
|
resume: () => {
|
|
2937
|
-
getScheduler().resumeJob(id);
|
|
2156
|
+
scheduler.getScheduler().resumeJob(id);
|
|
2938
2157
|
},
|
|
2939
2158
|
/**
|
|
2940
2159
|
* Reactive paused state - automatically updates when pause/resume is called.
|
|
@@ -2972,18 +2191,18 @@ function buildFromCache(input, textureCache) {
|
|
|
2972
2191
|
function useTexture(input, optionsOrOnLoad) {
|
|
2973
2192
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
2974
2193
|
const store = useStore();
|
|
2975
|
-
const textureCache = useThree((state) => state.textures);
|
|
2976
2194
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
2977
|
-
const { onLoad, cache =
|
|
2195
|
+
const { onLoad, cache = true } = options;
|
|
2978
2196
|
const onLoadRef = React.useRef(onLoad);
|
|
2979
2197
|
onLoadRef.current = onLoad;
|
|
2980
2198
|
const onLoadCalledForRef = React.useRef(null);
|
|
2981
2199
|
const urls = React.useMemo(() => getUrls(input), [input]);
|
|
2982
2200
|
const cachedResult = React.useMemo(() => {
|
|
2983
2201
|
if (!cache) return null;
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2202
|
+
const textures = store.getState().textures;
|
|
2203
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2204
|
+
return buildFromCache(input, textures);
|
|
2205
|
+
}, [cache, urls, input, store]);
|
|
2987
2206
|
const loadedTextures = useLoader(
|
|
2988
2207
|
webgpu.TextureLoader,
|
|
2989
2208
|
IsObject(input) ? Object.values(input) : input
|
|
@@ -3027,8 +2246,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3027
2246
|
}, [input, loadedTextures, cachedResult]);
|
|
3028
2247
|
React.useEffect(() => {
|
|
3029
2248
|
if (!cache) return;
|
|
3030
|
-
if (cachedResult) return;
|
|
3031
|
-
const set = store.setState;
|
|
3032
2249
|
const urlTextureMap = [];
|
|
3033
2250
|
if (typeof input === "string") {
|
|
3034
2251
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -3042,18 +2259,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3042
2259
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
3043
2260
|
}
|
|
3044
2261
|
}
|
|
3045
|
-
|
|
3046
|
-
const
|
|
3047
|
-
let
|
|
2262
|
+
store.setState((state) => {
|
|
2263
|
+
const refs = new Map(state._textureRefs);
|
|
2264
|
+
let textures = state.textures;
|
|
2265
|
+
let added = false;
|
|
3048
2266
|
for (const [url, texture] of urlTextureMap) {
|
|
3049
|
-
if (!
|
|
3050
|
-
|
|
3051
|
-
|
|
2267
|
+
if (!textures.has(url)) {
|
|
2268
|
+
if (!added) {
|
|
2269
|
+
textures = new Map(textures);
|
|
2270
|
+
added = true;
|
|
2271
|
+
}
|
|
2272
|
+
textures.set(url, texture);
|
|
3052
2273
|
}
|
|
2274
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
3053
2275
|
}
|
|
3054
|
-
return
|
|
2276
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
3055
2277
|
});
|
|
3056
|
-
|
|
2278
|
+
return () => store.setState((state) => {
|
|
2279
|
+
const refs = new Map(state._textureRefs);
|
|
2280
|
+
for (const [url] of urlTextureMap) {
|
|
2281
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2282
|
+
if (next <= 0) refs.delete(url);
|
|
2283
|
+
else refs.set(url, next);
|
|
2284
|
+
}
|
|
2285
|
+
return { _textureRefs: refs };
|
|
2286
|
+
});
|
|
2287
|
+
}, [cache, input, mappedTextures, store]);
|
|
3057
2288
|
return mappedTextures;
|
|
3058
2289
|
}
|
|
3059
2290
|
useTexture.preload = (url) => useLoader.preload(webgpu.TextureLoader, url);
|
|
@@ -3069,96 +2300,63 @@ const Texture = ({
|
|
|
3069
2300
|
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: children?.(ret) });
|
|
3070
2301
|
};
|
|
3071
2302
|
|
|
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() {
|
|
2303
|
+
function useTextures(selector) {
|
|
3080
2304
|
const store = useStore();
|
|
3081
|
-
|
|
3082
|
-
const set = store.setState;
|
|
2305
|
+
const registry = React.useMemo(() => {
|
|
3083
2306
|
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
|
-
};
|
|
2307
|
+
const setState = store.setState;
|
|
2308
|
+
const getOne = (key) => getState().textures.get(key);
|
|
3142
2309
|
return {
|
|
3143
|
-
|
|
3144
|
-
get textures() {
|
|
2310
|
+
get all() {
|
|
3145
2311
|
return getState().textures;
|
|
3146
2312
|
},
|
|
3147
|
-
|
|
3148
|
-
|
|
2313
|
+
get(input) {
|
|
2314
|
+
if (typeof input === "string") return getOne(input);
|
|
2315
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2316
|
+
const out = {};
|
|
2317
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2318
|
+
return out;
|
|
2319
|
+
},
|
|
3149
2320
|
has: (key) => getState().textures.has(key),
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
2321
|
+
add(keyOrRecord, texture) {
|
|
2322
|
+
setState((state) => {
|
|
2323
|
+
const textures = new Map(state.textures);
|
|
2324
|
+
if (typeof keyOrRecord === "string") {
|
|
2325
|
+
textures.set(keyOrRecord, texture);
|
|
2326
|
+
} else {
|
|
2327
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2328
|
+
}
|
|
2329
|
+
return { textures };
|
|
2330
|
+
});
|
|
2331
|
+
},
|
|
2332
|
+
dispose(key, options) {
|
|
2333
|
+
const state = getState();
|
|
2334
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2335
|
+
if (refs > 0 && !options?.force) {
|
|
2336
|
+
console.warn(
|
|
2337
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2338
|
+
);
|
|
2339
|
+
return false;
|
|
2340
|
+
}
|
|
2341
|
+
state.textures.get(key)?.dispose();
|
|
2342
|
+
setState((s) => {
|
|
2343
|
+
const textures = new Map(s.textures);
|
|
2344
|
+
textures.delete(key);
|
|
2345
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2346
|
+
nextRefs.delete(key);
|
|
2347
|
+
return { textures, _textureRefs: nextRefs };
|
|
2348
|
+
});
|
|
2349
|
+
return true;
|
|
2350
|
+
},
|
|
2351
|
+
disposeAll() {
|
|
2352
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2353
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2354
|
+
}
|
|
3160
2355
|
};
|
|
3161
2356
|
}, [store]);
|
|
2357
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2358
|
+
const selected = useThree(subscribe);
|
|
2359
|
+
return selector ? selected : registry;
|
|
3162
2360
|
}
|
|
3163
2361
|
|
|
3164
2362
|
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
@@ -3215,7 +2413,7 @@ function addEffect(callback) {
|
|
|
3215
2413
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3216
2414
|
});
|
|
3217
2415
|
const id = `legacy_effect_${effectId++}`;
|
|
3218
|
-
return getScheduler().registerGlobal("before", id, callback);
|
|
2416
|
+
return scheduler.getScheduler().registerGlobal("before", id, callback);
|
|
3219
2417
|
}
|
|
3220
2418
|
function addAfterEffect(callback) {
|
|
3221
2419
|
notifyDepreciated({
|
|
@@ -3224,7 +2422,7 @@ function addAfterEffect(callback) {
|
|
|
3224
2422
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3225
2423
|
});
|
|
3226
2424
|
const id = `legacy_afterEffect_${effectId++}`;
|
|
3227
|
-
return getScheduler().registerGlobal("after", id, callback);
|
|
2425
|
+
return scheduler.getScheduler().registerGlobal("after", id, callback);
|
|
3228
2426
|
}
|
|
3229
2427
|
function addTail(callback) {
|
|
3230
2428
|
notifyDepreciated({
|
|
@@ -3232,16 +2430,16 @@ function addTail(callback) {
|
|
|
3232
2430
|
body: "Use scheduler.onIdle(callback) instead.\naddTail will be removed in a future version.",
|
|
3233
2431
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3234
2432
|
});
|
|
3235
|
-
return getScheduler().onIdle(callback);
|
|
2433
|
+
return scheduler.getScheduler().onIdle(callback);
|
|
3236
2434
|
}
|
|
3237
2435
|
function invalidate(state, frames = 1, stackFrames = false) {
|
|
3238
|
-
getScheduler().invalidate(frames, stackFrames);
|
|
2436
|
+
scheduler.getScheduler().invalidate(frames, stackFrames);
|
|
3239
2437
|
}
|
|
3240
2438
|
function advance(timestamp) {
|
|
3241
|
-
getScheduler().step(timestamp);
|
|
2439
|
+
scheduler.getScheduler().step(timestamp);
|
|
3242
2440
|
}
|
|
3243
2441
|
|
|
3244
|
-
const version = "10.0.0-alpha.
|
|
2442
|
+
const version = "10.0.0-alpha.3";
|
|
3245
2443
|
const packageData = {
|
|
3246
2444
|
version: version};
|
|
3247
2445
|
|
|
@@ -14680,6 +13878,7 @@ function swapInstances() {
|
|
|
14680
13878
|
}
|
|
14681
13879
|
}
|
|
14682
13880
|
if (instance.isHidden) unhideInstance(instance);
|
|
13881
|
+
if (isObject3D(instance.object)) removeInteractivity(findInitialRoot(instance), instance.object);
|
|
14683
13882
|
if (instance.object.__r3f) delete instance.object.__r3f;
|
|
14684
13883
|
if (instance.type !== "primitive") disposeOnIdle(instance.object);
|
|
14685
13884
|
}
|
|
@@ -14692,6 +13891,8 @@ function swapInstances() {
|
|
|
14692
13891
|
instance.object.__r3f = instance;
|
|
14693
13892
|
setFiberRef(fiber, instance.object);
|
|
14694
13893
|
delete instance.appliedOnce;
|
|
13894
|
+
instance.eventCount = 0;
|
|
13895
|
+
instance.handlers = {};
|
|
14695
13896
|
applyProps(instance.object, instance.props);
|
|
14696
13897
|
if (instance.props.attach) {
|
|
14697
13898
|
attach(parent, instance);
|
|
@@ -14907,11 +14108,15 @@ const reconciler = /* @__PURE__ */ createReconciler({
|
|
|
14907
14108
|
cloneMutableTextInstance(textInstance) {
|
|
14908
14109
|
return textInstance;
|
|
14909
14110
|
},
|
|
14111
|
+
// React 19.2 added these for root-level view transitions. They only run for hosts that opt into
|
|
14112
|
+
// view transitions by returning a container from cloneRootViewTransitionContainer; R3F renders
|
|
14113
|
+
// into a WebGL/WebGPU canvas, where cloning the root has no meaning, so we never opt in and
|
|
14114
|
+
// React never calls them. They stay as no-ops rather than throws: the host config is shared by
|
|
14115
|
+
// every root, and a throw here would turn a React-internal probe into a hard crash for users.
|
|
14116
|
+
// Guarded by a test asserting a full render + unmount never reaches them.
|
|
14910
14117
|
cloneRootViewTransitionContainer(_rootContainer) {
|
|
14911
|
-
throw new Error("Not implemented.");
|
|
14912
14118
|
},
|
|
14913
14119
|
removeRootViewTransitionClone(_rootContainer, _clone) {
|
|
14914
|
-
throw new Error("Not implemented.");
|
|
14915
14120
|
},
|
|
14916
14121
|
// https://github.com/facebook/react/pull/32465
|
|
14917
14122
|
createFragmentInstance: (_fiber) => null,
|
|
@@ -14996,6 +14201,7 @@ function createRoot(canvas) {
|
|
|
14996
14201
|
const lastConfiguredProps = {};
|
|
14997
14202
|
let configured = false;
|
|
14998
14203
|
let pending = null;
|
|
14204
|
+
let rendererSetup = null;
|
|
14999
14205
|
return {
|
|
15000
14206
|
async configure(props = {}) {
|
|
15001
14207
|
let resolve;
|
|
@@ -15011,7 +14217,6 @@ function createRoot(canvas) {
|
|
|
15011
14217
|
events,
|
|
15012
14218
|
onCreated: onCreatedCallback,
|
|
15013
14219
|
shadows = false,
|
|
15014
|
-
textureColorSpace = webgpu.SRGBColorSpace,
|
|
15015
14220
|
orthographic = false,
|
|
15016
14221
|
frameloop = "always",
|
|
15017
14222
|
dpr = [1, 2],
|
|
@@ -15026,7 +14231,8 @@ function createRoot(canvas) {
|
|
|
15026
14231
|
_sizeProps,
|
|
15027
14232
|
forceEven
|
|
15028
14233
|
} = props;
|
|
15029
|
-
const
|
|
14234
|
+
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || webgpu.SRGBColorSpace;
|
|
14235
|
+
let state = store.getState();
|
|
15030
14236
|
const defaultGLProps = {
|
|
15031
14237
|
canvas,
|
|
15032
14238
|
powerPreference: "high-performance",
|
|
@@ -15054,50 +14260,68 @@ function createRoot(canvas) {
|
|
|
15054
14260
|
"The `primaryCanvas` prop for multi-canvas rendering cannot be used with WebGL. Remove the `gl` prop or use WebGPU."
|
|
15055
14261
|
);
|
|
15056
14262
|
}
|
|
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
|
-
|
|
14263
|
+
if (!state.internal.actualRenderer) {
|
|
14264
|
+
if (!rendererSetup) {
|
|
14265
|
+
rendererSetup = (async () => {
|
|
14266
|
+
if (wantsGL) {
|
|
14267
|
+
renderer = await resolveRenderer(glConfig, defaultGLProps, three.WebGLRenderer);
|
|
14268
|
+
state.internal.actualRenderer = renderer;
|
|
14269
|
+
state.set({ isLegacy: true, gl: renderer, renderer, primaryStore: store });
|
|
14270
|
+
} else if (!wantsGL && primaryCanvas) {
|
|
14271
|
+
const primary = await waitForPrimary(primaryCanvas);
|
|
14272
|
+
renderer = primary.renderer;
|
|
14273
|
+
state.internal.actualRenderer = renderer;
|
|
14274
|
+
const canvasTarget = new webgpu.CanvasTarget(canvas);
|
|
14275
|
+
primary.store.setState((prev) => ({
|
|
14276
|
+
internal: { ...prev.internal, isMultiCanvas: true }
|
|
14277
|
+
}));
|
|
14278
|
+
state.set((prev) => ({
|
|
14279
|
+
webGPUSupported: primary.store.getState().webGPUSupported,
|
|
14280
|
+
renderer,
|
|
14281
|
+
primaryStore: primary.store,
|
|
14282
|
+
internal: {
|
|
14283
|
+
...prev.internal,
|
|
14284
|
+
canvasTarget,
|
|
14285
|
+
isMultiCanvas: true,
|
|
14286
|
+
isSecondary: true,
|
|
14287
|
+
targetId: primaryCanvas
|
|
14288
|
+
}
|
|
14289
|
+
}));
|
|
14290
|
+
} else if (!wantsGL) {
|
|
14291
|
+
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, webgpu.WebGPURenderer);
|
|
14292
|
+
if (!renderer.hasInitialized?.()) {
|
|
14293
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14294
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14295
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14296
|
+
canvas.width = size2.width * pixelRatio;
|
|
14297
|
+
canvas.height = size2.height * pixelRatio;
|
|
14298
|
+
}
|
|
14299
|
+
await renderer.init();
|
|
14300
|
+
}
|
|
14301
|
+
const backend = renderer.backend;
|
|
14302
|
+
const isWebGPUBackend = backend && "isWebGPUBackend" in backend;
|
|
14303
|
+
state.internal.actualRenderer = renderer;
|
|
14304
|
+
state.set({ webGPUSupported: isWebGPUBackend, renderer, primaryStore: store });
|
|
14305
|
+
if (canvasId && !state.internal.isSecondary) {
|
|
14306
|
+
const canvasTarget = new webgpu.CanvasTarget(canvas);
|
|
14307
|
+
const unregisterPrimary = registerPrimary(canvasId, renderer, store);
|
|
14308
|
+
state.set((prev) => ({
|
|
14309
|
+
internal: {
|
|
14310
|
+
...prev.internal,
|
|
14311
|
+
canvasTarget,
|
|
14312
|
+
unregisterPrimary
|
|
14313
|
+
}
|
|
14314
|
+
}));
|
|
14315
|
+
}
|
|
15098
14316
|
}
|
|
15099
|
-
}))
|
|
14317
|
+
})().catch((err) => {
|
|
14318
|
+
rendererSetup = null;
|
|
14319
|
+
throw err;
|
|
14320
|
+
});
|
|
15100
14321
|
}
|
|
14322
|
+
await rendererSetup;
|
|
14323
|
+
state = store.getState();
|
|
14324
|
+
renderer = state.internal.actualRenderer;
|
|
15101
14325
|
}
|
|
15102
14326
|
let raycaster = state.raycaster;
|
|
15103
14327
|
if (!raycaster) state.set({ raycaster: raycaster = new webgpu.Raycaster() });
|
|
@@ -15190,7 +14414,7 @@ function createRoot(canvas) {
|
|
|
15190
14414
|
lastConfiguredProps.performance = performance;
|
|
15191
14415
|
}
|
|
15192
14416
|
if (!state.xr) {
|
|
15193
|
-
const handleXRFrame = (timestamp,
|
|
14417
|
+
const handleXRFrame = (timestamp, _frame) => {
|
|
15194
14418
|
const state2 = store.getState();
|
|
15195
14419
|
if (state2.frameloop === "never") return;
|
|
15196
14420
|
advance(timestamp);
|
|
@@ -15226,15 +14450,22 @@ function createRoot(canvas) {
|
|
|
15226
14450
|
const oldType = renderer.shadowMap.type;
|
|
15227
14451
|
renderer.shadowMap.enabled = !!shadows;
|
|
15228
14452
|
if (is.boo(shadows)) {
|
|
15229
|
-
renderer.shadowMap.type = webgpu.
|
|
14453
|
+
renderer.shadowMap.type = webgpu.PCFShadowMap;
|
|
15230
14454
|
} else if (is.str(shadows)) {
|
|
14455
|
+
if (shadows === "soft") {
|
|
14456
|
+
notifyDepreciated({
|
|
14457
|
+
heading: 'shadows="soft" is deprecated',
|
|
14458
|
+
body: "Three has depreciated soft and improved basic PCFShadows, we converted for you.",
|
|
14459
|
+
link: "https://github.com/mrdoob/three.js/wiki/Migration-Guide?utm_source=chatgpt.com#181--182"
|
|
14460
|
+
});
|
|
14461
|
+
}
|
|
15231
14462
|
const types = {
|
|
15232
14463
|
basic: webgpu.BasicShadowMap,
|
|
15233
14464
|
percentage: webgpu.PCFShadowMap,
|
|
15234
|
-
soft: webgpu.
|
|
14465
|
+
soft: webgpu.PCFShadowMap,
|
|
15235
14466
|
variance: webgpu.VSMShadowMap
|
|
15236
14467
|
};
|
|
15237
|
-
renderer.shadowMap.type = types[shadows] ?? webgpu.
|
|
14468
|
+
renderer.shadowMap.type = types[shadows] ?? webgpu.PCFShadowMap;
|
|
15238
14469
|
} else if (is.obj(shadows)) {
|
|
15239
14470
|
Object.assign(renderer.shadowMap, shadows);
|
|
15240
14471
|
}
|
|
@@ -15250,24 +14481,35 @@ function createRoot(canvas) {
|
|
|
15250
14481
|
if (state.textureColorSpace !== textureColorSpace) state.set(() => ({ textureColorSpace }));
|
|
15251
14482
|
lastConfiguredProps.textureColorSpace = textureColorSpace;
|
|
15252
14483
|
}
|
|
14484
|
+
const r3fProps = ["textureColorSpace"];
|
|
14485
|
+
const constructorOnlyProps = ["samples", "antialias", "alpha", "canvas", "powerPreference"];
|
|
14486
|
+
const nonApplyProps = [...r3fProps, ...constructorOnlyProps];
|
|
15253
14487
|
if (glConfig && !is.fun(glConfig) && !isRenderer(glConfig) && !is.equ(glConfig, renderer, shallowLoose)) {
|
|
15254
|
-
|
|
14488
|
+
const glProps = {};
|
|
14489
|
+
for (const key in glConfig) {
|
|
14490
|
+
if (!nonApplyProps.includes(key)) glProps[key] = glConfig[key];
|
|
14491
|
+
}
|
|
14492
|
+
applyProps(renderer, glProps);
|
|
15255
14493
|
}
|
|
15256
14494
|
if (rendererConfig && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && state.renderer) {
|
|
15257
14495
|
const currentRenderer = state.renderer;
|
|
15258
14496
|
if (!is.equ(rendererConfig, currentRenderer, shallowLoose)) {
|
|
15259
|
-
|
|
14497
|
+
const rendererProps = {};
|
|
14498
|
+
for (const key in rendererConfig) {
|
|
14499
|
+
if (!nonApplyProps.includes(key)) rendererProps[key] = rendererConfig[key];
|
|
14500
|
+
}
|
|
14501
|
+
applyProps(currentRenderer, rendererProps);
|
|
15260
14502
|
}
|
|
15261
14503
|
}
|
|
15262
|
-
const scheduler = getScheduler();
|
|
14504
|
+
const scheduler$1 = scheduler.getScheduler();
|
|
15263
14505
|
const rootId = state.internal.rootId;
|
|
15264
14506
|
if (!rootId) {
|
|
15265
|
-
const newRootId = canvasId || scheduler.generateRootId();
|
|
15266
|
-
const unregisterRoot = scheduler.registerRoot(newRootId, {
|
|
14507
|
+
const newRootId = canvasId || scheduler$1.generateRootId();
|
|
14508
|
+
const unregisterRoot = scheduler$1.registerRoot(newRootId, {
|
|
15267
14509
|
getState: () => store.getState(),
|
|
15268
14510
|
onError: (err) => store.getState().setError(err)
|
|
15269
14511
|
});
|
|
15270
|
-
const unregisterCanvasTarget = scheduler.register(
|
|
14512
|
+
const unregisterCanvasTarget = scheduler$1.register(
|
|
15271
14513
|
() => {
|
|
15272
14514
|
const state2 = store.getState();
|
|
15273
14515
|
if (state2.internal.isMultiCanvas && state2.internal.canvasTarget) {
|
|
@@ -15282,7 +14524,19 @@ function createRoot(canvas) {
|
|
|
15282
14524
|
system: true
|
|
15283
14525
|
}
|
|
15284
14526
|
);
|
|
15285
|
-
const
|
|
14527
|
+
const unregisterEventsFlush = scheduler$1.register(
|
|
14528
|
+
() => {
|
|
14529
|
+
const state2 = store.getState();
|
|
14530
|
+
state2.events.flush?.();
|
|
14531
|
+
},
|
|
14532
|
+
{
|
|
14533
|
+
id: `${newRootId}_events`,
|
|
14534
|
+
rootId: newRootId,
|
|
14535
|
+
phase: "input",
|
|
14536
|
+
system: true
|
|
14537
|
+
}
|
|
14538
|
+
);
|
|
14539
|
+
const unregisterFrustum = scheduler$1.register(
|
|
15286
14540
|
() => {
|
|
15287
14541
|
const state2 = store.getState();
|
|
15288
14542
|
if (state2.autoUpdateFrustum && state2.camera) {
|
|
@@ -15292,11 +14546,11 @@ function createRoot(canvas) {
|
|
|
15292
14546
|
{
|
|
15293
14547
|
id: `${newRootId}_frustum`,
|
|
15294
14548
|
rootId: newRootId,
|
|
15295
|
-
|
|
14549
|
+
before: "render",
|
|
15296
14550
|
system: true
|
|
15297
14551
|
}
|
|
15298
14552
|
);
|
|
15299
|
-
const unregisterVisibility = scheduler.register(
|
|
14553
|
+
const unregisterVisibility = scheduler$1.register(
|
|
15300
14554
|
() => {
|
|
15301
14555
|
const state2 = store.getState();
|
|
15302
14556
|
checkVisibility(state2);
|
|
@@ -15304,19 +14558,19 @@ function createRoot(canvas) {
|
|
|
15304
14558
|
{
|
|
15305
14559
|
id: `${newRootId}_visibility`,
|
|
15306
14560
|
rootId: newRootId,
|
|
15307
|
-
|
|
14561
|
+
before: "render",
|
|
15308
14562
|
system: true,
|
|
15309
14563
|
after: `${newRootId}_frustum`
|
|
15310
14564
|
}
|
|
15311
14565
|
);
|
|
15312
|
-
const unregisterRender = scheduler.register(
|
|
14566
|
+
const unregisterRender = scheduler$1.register(
|
|
15313
14567
|
() => {
|
|
15314
14568
|
const state2 = store.getState();
|
|
15315
14569
|
const renderer2 = state2.internal.actualRenderer;
|
|
15316
|
-
const userHandlesRender = scheduler.hasUserJobsInPhase("render", newRootId);
|
|
14570
|
+
const userHandlesRender = scheduler$1.hasUserJobsInPhase("render", newRootId);
|
|
15317
14571
|
if (userHandlesRender || state2.internal.priority) return;
|
|
15318
14572
|
try {
|
|
15319
|
-
if (state2.
|
|
14573
|
+
if (state2.renderPipeline?.render) state2.renderPipeline.render();
|
|
15320
14574
|
else if (renderer2?.render) renderer2.render(state2.scene, state2.camera);
|
|
15321
14575
|
} catch (error) {
|
|
15322
14576
|
state2.setError(error instanceof Error ? error : new Error(String(error)));
|
|
@@ -15341,15 +14595,16 @@ function createRoot(canvas) {
|
|
|
15341
14595
|
unregisterRoot: () => {
|
|
15342
14596
|
unregisterRoot();
|
|
15343
14597
|
unregisterCanvasTarget();
|
|
14598
|
+
unregisterEventsFlush();
|
|
15344
14599
|
unregisterFrustum();
|
|
15345
14600
|
unregisterVisibility();
|
|
15346
14601
|
unregisterRender();
|
|
15347
14602
|
},
|
|
15348
|
-
scheduler
|
|
14603
|
+
scheduler: scheduler$1
|
|
15349
14604
|
}
|
|
15350
14605
|
}));
|
|
15351
14606
|
}
|
|
15352
|
-
scheduler.frameloop = frameloop;
|
|
14607
|
+
scheduler$1.frameloop = frameloop;
|
|
15353
14608
|
onCreated = onCreatedCallback;
|
|
15354
14609
|
configured = true;
|
|
15355
14610
|
resolve();
|
|
@@ -15416,7 +14671,8 @@ function unmountComponentAtNode(canvas, callback) {
|
|
|
15416
14671
|
dispose(state.scene);
|
|
15417
14672
|
_roots.delete(canvas);
|
|
15418
14673
|
if (callback) callback(canvas);
|
|
15419
|
-
} catch {
|
|
14674
|
+
} catch (error) {
|
|
14675
|
+
console.warn("[R3F] Error while unmounting root; teardown may be incomplete:", error);
|
|
15420
14676
|
}
|
|
15421
14677
|
}, 500);
|
|
15422
14678
|
}
|
|
@@ -15479,6 +14735,7 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15479
14735
|
viewport = rootState.viewport.getCurrentViewport(camera, new webgpu.Vector3(), resolvedSize);
|
|
15480
14736
|
if (camera !== rootState.camera) updateCamera(camera, resolvedSize);
|
|
15481
14737
|
}
|
|
14738
|
+
const set = injectState.set;
|
|
15482
14739
|
return {
|
|
15483
14740
|
// The intersect consists of the previous root state
|
|
15484
14741
|
...rootState,
|
|
@@ -15497,7 +14754,7 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15497
14754
|
size: resolvedSize,
|
|
15498
14755
|
viewport: { ...rootState.viewport, ...viewport },
|
|
15499
14756
|
// Layers are allowed to override events
|
|
15500
|
-
setEvents: (events2) =>
|
|
14757
|
+
setEvents: (events2) => set((state2) => ({ ...state2, events: { ...state2.events, ...events2 } })),
|
|
15501
14758
|
// Container for child attachment - the portalScene (injected or container itself)
|
|
15502
14759
|
internal: { ...rootState.internal, ...injectState.internal, container: portalScene }
|
|
15503
14760
|
};
|
|
@@ -15506,9 +14763,13 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15506
14763
|
const store = traditional.createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
15507
14764
|
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
15508
14765
|
onMutate(previousRoot.getState());
|
|
15509
|
-
previousRoot.subscribe(onMutate);
|
|
15510
14766
|
return store;
|
|
15511
14767
|
}, [previousRoot, container]);
|
|
14768
|
+
useIsomorphicLayoutEffect(() => {
|
|
14769
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14770
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14771
|
+
return unsubscribe;
|
|
14772
|
+
}, [previousRoot, usePortalStore]);
|
|
15512
14773
|
return (
|
|
15513
14774
|
// @ts-ignore, reconciler types are not maintained
|
|
15514
14775
|
/* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: reconciler.createPortal(
|
|
@@ -15522,6 +14783,81 @@ function flushSync(fn) {
|
|
|
15522
14783
|
return reconciler.flushSyncFromReconciler(fn);
|
|
15523
14784
|
}
|
|
15524
14785
|
|
|
14786
|
+
function parseBackground(background) {
|
|
14787
|
+
if (!background) return null;
|
|
14788
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14789
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14790
|
+
return {
|
|
14791
|
+
...rest,
|
|
14792
|
+
preset,
|
|
14793
|
+
files: envMap || files,
|
|
14794
|
+
backgroundFiles: backgroundMap,
|
|
14795
|
+
background: true
|
|
14796
|
+
};
|
|
14797
|
+
}
|
|
14798
|
+
if (typeof background === "number") {
|
|
14799
|
+
return { color: background, background: true };
|
|
14800
|
+
}
|
|
14801
|
+
if (typeof background === "string") {
|
|
14802
|
+
if (background in presetsObj) {
|
|
14803
|
+
return { preset: background, background: true };
|
|
14804
|
+
}
|
|
14805
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14806
|
+
return { files: background, background: true };
|
|
14807
|
+
}
|
|
14808
|
+
return { color: background, background: true };
|
|
14809
|
+
}
|
|
14810
|
+
if (background.isColor) {
|
|
14811
|
+
return { color: background, background: true };
|
|
14812
|
+
}
|
|
14813
|
+
return null;
|
|
14814
|
+
}
|
|
14815
|
+
|
|
14816
|
+
function parseRendererConfig(rendererProp) {
|
|
14817
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14818
|
+
if (isRendererConfig) {
|
|
14819
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14820
|
+
return {
|
|
14821
|
+
primaryCanvas: pc,
|
|
14822
|
+
scheduler: sc,
|
|
14823
|
+
renderer: Object.keys(rest).length > 0 ? rest : rendererProp
|
|
14824
|
+
};
|
|
14825
|
+
}
|
|
14826
|
+
return { primaryCanvas: void 0, scheduler: void 0, renderer: rendererProp };
|
|
14827
|
+
}
|
|
14828
|
+
|
|
14829
|
+
const RESOURCE_KINDS = ["nodes", "uniforms", "buffers", "gpuStorage"];
|
|
14830
|
+
const registries = /* @__PURE__ */ new WeakMap();
|
|
14831
|
+
function getKindRegistry(store, kind) {
|
|
14832
|
+
let registry = registries.get(store);
|
|
14833
|
+
if (!registry) {
|
|
14834
|
+
registry = {
|
|
14835
|
+
nodes: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14836
|
+
uniforms: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14837
|
+
buffers: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() },
|
|
14838
|
+
gpuStorage: { valid: /* @__PURE__ */ new Set(), staged: /* @__PURE__ */ new Map() }
|
|
14839
|
+
};
|
|
14840
|
+
registries.set(store, registry);
|
|
14841
|
+
}
|
|
14842
|
+
return registry[kind];
|
|
14843
|
+
}
|
|
14844
|
+
function invalidateResource(store, kind, scopeKey) {
|
|
14845
|
+
const registry = getKindRegistry(store, kind);
|
|
14846
|
+
{
|
|
14847
|
+
registry.valid.clear();
|
|
14848
|
+
registry.staged.clear();
|
|
14849
|
+
}
|
|
14850
|
+
}
|
|
14851
|
+
function invalidateAllResources(store) {
|
|
14852
|
+
for (const kind of RESOURCE_KINDS) invalidateResource(store, kind);
|
|
14853
|
+
}
|
|
14854
|
+
|
|
14855
|
+
function clearHmrCaches(store) {
|
|
14856
|
+
const primary = store.getState().primaryStore ?? store;
|
|
14857
|
+
invalidateAllResources(primary);
|
|
14858
|
+
primary.setState((state) => ({ _hmrVersion: state._hmrVersion + 1 }));
|
|
14859
|
+
}
|
|
14860
|
+
|
|
15525
14861
|
function CanvasImpl({
|
|
15526
14862
|
ref,
|
|
15527
14863
|
children,
|
|
@@ -15542,6 +14878,8 @@ function CanvasImpl({
|
|
|
15542
14878
|
raycaster,
|
|
15543
14879
|
camera,
|
|
15544
14880
|
scene,
|
|
14881
|
+
autoUpdateFrustum,
|
|
14882
|
+
occlusion,
|
|
15545
14883
|
onPointerMissed,
|
|
15546
14884
|
onDragOverMissed,
|
|
15547
14885
|
onDropMissed,
|
|
@@ -15553,39 +14891,10 @@ function CanvasImpl({
|
|
|
15553
14891
|
forceEven,
|
|
15554
14892
|
...props
|
|
15555
14893
|
}) {
|
|
15556
|
-
const { primaryCanvas, scheduler,
|
|
15557
|
-
const renderer = Object.keys(rendererConfig).length > 0 ? rendererConfig : rendererProp;
|
|
14894
|
+
const { primaryCanvas, scheduler, renderer } = parseRendererConfig(rendererProp);
|
|
15558
14895
|
React__namespace.useMemo(() => extend(THREE), []);
|
|
15559
14896
|
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]);
|
|
14897
|
+
const backgroundProps = React__namespace.useMemo(() => parseBackground(background), [background]);
|
|
15589
14898
|
const hasInitialSizeRef = React__namespace.useRef(false);
|
|
15590
14899
|
const measureConfig = React__namespace.useMemo(() => {
|
|
15591
14900
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15627,6 +14936,7 @@ function CanvasImpl({
|
|
|
15627
14936
|
const handleDropMissed = useMutableCallback(onDropMissed);
|
|
15628
14937
|
const [block, setBlock] = React__namespace.useState(false);
|
|
15629
14938
|
const [error, setError] = React__namespace.useState(false);
|
|
14939
|
+
const [fallbackVisible, setFallbackVisible] = React__namespace.useState(false);
|
|
15630
14940
|
if (block) throw block;
|
|
15631
14941
|
if (error) throw error;
|
|
15632
14942
|
const root = React__namespace.useRef(null);
|
|
@@ -15634,6 +14944,7 @@ function CanvasImpl({
|
|
|
15634
14944
|
const unsubscribeErrorRef = React__namespace.useRef(null);
|
|
15635
14945
|
useIsomorphicLayoutEffect(() => {
|
|
15636
14946
|
effectActiveRef.current = true;
|
|
14947
|
+
if (fallbackVisible) return;
|
|
15637
14948
|
const canvas = canvasRef.current;
|
|
15638
14949
|
if (effectiveSize.width > 0 && effectiveSize.height > 0 && canvas) {
|
|
15639
14950
|
if (!root.current) {
|
|
@@ -15654,7 +14965,7 @@ function CanvasImpl({
|
|
|
15654
14965
|
}
|
|
15655
14966
|
async function run() {
|
|
15656
14967
|
if (!effectActiveRef.current || !root.current) return;
|
|
15657
|
-
await root.current.configure({
|
|
14968
|
+
const configured = await root.current.configure({
|
|
15658
14969
|
id,
|
|
15659
14970
|
primaryCanvas,
|
|
15660
14971
|
scheduler,
|
|
@@ -15669,6 +14980,8 @@ function CanvasImpl({
|
|
|
15669
14980
|
performance,
|
|
15670
14981
|
raycaster,
|
|
15671
14982
|
camera,
|
|
14983
|
+
autoUpdateFrustum,
|
|
14984
|
+
occlusion,
|
|
15672
14985
|
size: effectiveSize,
|
|
15673
14986
|
// Store size props for reset functionality
|
|
15674
14987
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
@@ -15693,8 +15006,20 @@ function CanvasImpl({
|
|
|
15693
15006
|
}
|
|
15694
15007
|
onCreated?.(state);
|
|
15695
15008
|
}
|
|
15696
|
-
})
|
|
15697
|
-
|
|
15009
|
+
}).then(
|
|
15010
|
+
() => true,
|
|
15011
|
+
// Renderer setup failed (e.g. no WebGL/WebGPU support). The `fallback` prop lives
|
|
15012
|
+
// inside <canvas>, which browsers don't display, so surface it as visible DOM
|
|
15013
|
+
// instead; with no fallback, rethrow to an external error boundary. (#3757)
|
|
15014
|
+
(setupError) => {
|
|
15015
|
+
if (effectActiveRef.current) {
|
|
15016
|
+
if (fallback != null) setFallbackVisible(true);
|
|
15017
|
+
else setError(setupError);
|
|
15018
|
+
}
|
|
15019
|
+
return false;
|
|
15020
|
+
}
|
|
15021
|
+
);
|
|
15022
|
+
if (!configured || !effectActiveRef.current || !root.current) return;
|
|
15698
15023
|
root.current.render(
|
|
15699
15024
|
/* @__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
15025
|
backgroundProps && /* @__PURE__ */ jsxRuntime.jsx(Environment, { ...backgroundProps }),
|
|
@@ -15728,20 +15053,13 @@ function CanvasImpl({
|
|
|
15728
15053
|
const handleHMR = () => {
|
|
15729
15054
|
queueMicrotask(() => {
|
|
15730
15055
|
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
|
-
}
|
|
15056
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15738
15057
|
});
|
|
15739
15058
|
};
|
|
15740
15059
|
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
15060
|
const hot = undefined;
|
|
15742
15061
|
hot.on("vite:afterUpdate", handleHMR);
|
|
15743
|
-
return () => hot.
|
|
15744
|
-
});
|
|
15062
|
+
return () => hot.off?.("vite:afterUpdate", handleHMR);
|
|
15745
15063
|
}
|
|
15746
15064
|
if (typeof module !== "undefined" && module.hot) {
|
|
15747
15065
|
const hot = module.hot;
|
|
@@ -15764,7 +15082,20 @@ function CanvasImpl({
|
|
|
15764
15082
|
...style
|
|
15765
15083
|
},
|
|
15766
15084
|
...props,
|
|
15767
|
-
children:
|
|
15085
|
+
children: fallbackVisible ? (
|
|
15086
|
+
// Renderer setup failed: render the fallback as visible DOM. Inside <canvas> (below)
|
|
15087
|
+
// it exists in the tree but browsers never paint it, which is the whole bug (#3757).
|
|
15088
|
+
fallback
|
|
15089
|
+
) : /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
15090
|
+
"canvas",
|
|
15091
|
+
{
|
|
15092
|
+
ref: canvasRef,
|
|
15093
|
+
id,
|
|
15094
|
+
className: "r3f-canvas",
|
|
15095
|
+
style: { display: "block", width: "100%", height: "100%" },
|
|
15096
|
+
children: fallback
|
|
15097
|
+
}
|
|
15098
|
+
) })
|
|
15768
15099
|
}
|
|
15769
15100
|
);
|
|
15770
15101
|
}
|
|
@@ -15774,6 +15105,8 @@ function Canvas(props) {
|
|
|
15774
15105
|
|
|
15775
15106
|
extend(THREE);
|
|
15776
15107
|
|
|
15108
|
+
exports.Scheduler = scheduler.Scheduler;
|
|
15109
|
+
exports.getScheduler = scheduler.getScheduler;
|
|
15777
15110
|
exports.Block = Block;
|
|
15778
15111
|
exports.Canvas = Canvas;
|
|
15779
15112
|
exports.Environment = Environment;
|
|
@@ -15789,7 +15122,6 @@ exports.R3F_BUILD_LEGACY = R3F_BUILD_LEGACY;
|
|
|
15789
15122
|
exports.R3F_BUILD_WEBGPU = R3F_BUILD_WEBGPU;
|
|
15790
15123
|
exports.REACT_INTERNAL_PROPS = REACT_INTERNAL_PROPS;
|
|
15791
15124
|
exports.RESERVED_PROPS = RESERVED_PROPS;
|
|
15792
|
-
exports.Scheduler = Scheduler;
|
|
15793
15125
|
exports.Texture = Texture;
|
|
15794
15126
|
exports._roots = _roots;
|
|
15795
15127
|
exports.act = act;
|
|
@@ -15819,7 +15151,6 @@ exports.getInstanceProps = getInstanceProps;
|
|
|
15819
15151
|
exports.getPrimary = getPrimary;
|
|
15820
15152
|
exports.getPrimaryIds = getPrimaryIds;
|
|
15821
15153
|
exports.getRootState = getRootState;
|
|
15822
|
-
exports.getScheduler = getScheduler;
|
|
15823
15154
|
exports.getUuidPrefix = getUuidPrefix;
|
|
15824
15155
|
exports.hasConstructor = hasConstructor;
|
|
15825
15156
|
exports.hasPrimary = hasPrimary;
|