@react-three/fiber 10.0.0-canary.2b511a5 → 10.0.0-canary.2c50459
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 +529 -1305
- package/dist/index.d.cts +246 -785
- package/dist/index.d.mts +246 -785
- package/dist/index.d.ts +246 -785
- package/dist/index.mjs +503 -1278
- package/dist/legacy.cjs +523 -1305
- package/dist/legacy.d.cts +246 -785
- package/dist/legacy.d.mts +246 -785
- package/dist/legacy.d.ts +246 -785
- package/dist/legacy.mjs +497 -1278
- package/dist/webgpu/index.cjs +937 -1331
- package/dist/webgpu/index.d.cts +331 -798
- package/dist/webgpu/index.d.mts +331 -798
- package/dist/webgpu/index.d.ts +331 -798
- package/dist/webgpu/index.mjs +906 -1303
- package/package.json +2 -1
- package/readme.md +2 -2
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;
|
|
@@ -160,7 +161,7 @@ function useEnvironment({
|
|
|
160
161
|
fiber.useLoader.clear(loader, multiFile ? [files] : files);
|
|
161
162
|
}
|
|
162
163
|
renderer.domElement.addEventListener("webglcontextlost", clearGainmapTexture, { once: true });
|
|
163
|
-
}, [files, renderer.domElement]);
|
|
164
|
+
}, [extension, files, loader, multiFile, renderer.domElement]);
|
|
164
165
|
const loaderResult = fiber.useLoader(
|
|
165
166
|
loader,
|
|
166
167
|
multiFile ? [files] : files,
|
|
@@ -360,7 +361,22 @@ function EnvironmentPortal({
|
|
|
360
361
|
environmentIntensity,
|
|
361
362
|
environmentRotation
|
|
362
363
|
});
|
|
363
|
-
}, [
|
|
364
|
+
}, [
|
|
365
|
+
children,
|
|
366
|
+
virtualScene,
|
|
367
|
+
fbo.texture,
|
|
368
|
+
scene,
|
|
369
|
+
defaultScene,
|
|
370
|
+
background,
|
|
371
|
+
frames,
|
|
372
|
+
gl,
|
|
373
|
+
blur,
|
|
374
|
+
backgroundBlurriness,
|
|
375
|
+
backgroundIntensity,
|
|
376
|
+
backgroundRotation,
|
|
377
|
+
environmentIntensity,
|
|
378
|
+
environmentRotation
|
|
379
|
+
]);
|
|
364
380
|
let count = 1;
|
|
365
381
|
fiber.useFrame(() => {
|
|
366
382
|
if (frames === Infinity || count < frames) {
|
|
@@ -431,9 +447,9 @@ function Environment(props) {
|
|
|
431
447
|
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
448
|
}
|
|
433
449
|
|
|
434
|
-
var __defProp
|
|
435
|
-
var __defNormalProp
|
|
436
|
-
var __publicField
|
|
450
|
+
var __defProp = Object.defineProperty;
|
|
451
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
452
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
437
453
|
const act = React__namespace["act"];
|
|
438
454
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
439
455
|
function useMutableCallback(fn) {
|
|
@@ -465,7 +481,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
465
481
|
return _a = class extends React__namespace.Component {
|
|
466
482
|
constructor() {
|
|
467
483
|
super(...arguments);
|
|
468
|
-
__publicField
|
|
484
|
+
__publicField(this, "state", { error: false });
|
|
469
485
|
}
|
|
470
486
|
componentDidCatch(err) {
|
|
471
487
|
this.props.set(err);
|
|
@@ -473,7 +489,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
473
489
|
render() {
|
|
474
490
|
return this.state.error ? null : this.props.children;
|
|
475
491
|
}
|
|
476
|
-
}, __publicField
|
|
492
|
+
}, __publicField(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
477
493
|
})();
|
|
478
494
|
|
|
479
495
|
const is = {
|
|
@@ -996,6 +1012,9 @@ function applyProps(object, props) {
|
|
|
996
1012
|
else target.set(value);
|
|
997
1013
|
} else {
|
|
998
1014
|
root[key] = value;
|
|
1015
|
+
if (key.endsWith("Node") && root.isMaterial) {
|
|
1016
|
+
root.needsUpdate = true;
|
|
1017
|
+
}
|
|
999
1018
|
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
1019
|
root[key].format === webgpu.RGBAFormat && root[key].type === webgpu.UnsignedByteType) {
|
|
1001
1020
|
root[key].colorSpace = rootState.textureColorSpace;
|
|
@@ -1029,38 +1048,60 @@ function applyProps(object, props) {
|
|
|
1029
1048
|
return object;
|
|
1030
1049
|
}
|
|
1031
1050
|
|
|
1051
|
+
const DEFAULT_POINTER_ID = 0;
|
|
1052
|
+
const XR_POINTER_ID_START = 1e3;
|
|
1053
|
+
function getPointerState(internal, pointerId) {
|
|
1054
|
+
let state = internal.pointerMap.get(pointerId);
|
|
1055
|
+
if (!state) {
|
|
1056
|
+
state = {
|
|
1057
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1058
|
+
captured: /* @__PURE__ */ new Map(),
|
|
1059
|
+
initialClick: [0, 0],
|
|
1060
|
+
initialHits: []
|
|
1061
|
+
};
|
|
1062
|
+
internal.pointerMap.set(pointerId, state);
|
|
1063
|
+
}
|
|
1064
|
+
return state;
|
|
1065
|
+
}
|
|
1066
|
+
function getPointerId(event) {
|
|
1067
|
+
return "pointerId" in event ? event.pointerId : DEFAULT_POINTER_ID;
|
|
1068
|
+
}
|
|
1032
1069
|
function makeId(event) {
|
|
1033
1070
|
return (event.eventObject || event.object).uuid + "/" + event.index + event.instanceId;
|
|
1034
1071
|
}
|
|
1035
|
-
function releaseInternalPointerCapture(
|
|
1036
|
-
const
|
|
1072
|
+
function releaseInternalPointerCapture(internal, obj, pointerId) {
|
|
1073
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1074
|
+
if (!pointerState) return;
|
|
1075
|
+
const captureData = pointerState.captured.get(obj);
|
|
1037
1076
|
if (captureData) {
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
capturedMap.delete(pointerId);
|
|
1041
|
-
captureData.target.releasePointerCapture(pointerId);
|
|
1042
|
-
}
|
|
1077
|
+
pointerState.captured.delete(obj);
|
|
1078
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
1043
1079
|
}
|
|
1044
1080
|
}
|
|
1045
1081
|
function removeInteractivity(store, object) {
|
|
1046
1082
|
const { internal } = store.getState();
|
|
1047
1083
|
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1084
|
+
for (const [pointerId, pointerState] of internal.pointerMap) {
|
|
1085
|
+
pointerState.initialHits = pointerState.initialHits.filter((o) => o !== object);
|
|
1086
|
+
pointerState.hovered.forEach((value, key) => {
|
|
1087
|
+
if (value.eventObject === object || value.object === object) {
|
|
1088
|
+
pointerState.hovered.delete(key);
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
if (pointerState.captured.has(object)) {
|
|
1092
|
+
releaseInternalPointerCapture(internal, object, pointerId);
|
|
1052
1093
|
}
|
|
1053
|
-
}
|
|
1054
|
-
internal.capturedMap.forEach((captures, pointerId) => {
|
|
1055
|
-
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
1056
|
-
});
|
|
1094
|
+
}
|
|
1057
1095
|
unregisterVisibility(store, object);
|
|
1058
1096
|
}
|
|
1059
1097
|
function createEvents(store) {
|
|
1060
|
-
function calculateDistance(event) {
|
|
1098
|
+
function calculateDistance(event, pointerId) {
|
|
1061
1099
|
const { internal } = store.getState();
|
|
1062
|
-
const
|
|
1063
|
-
|
|
1100
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1101
|
+
if (!pointerState) return 0;
|
|
1102
|
+
const [initialX, initialY] = pointerState.initialClick;
|
|
1103
|
+
const dx = event.offsetX - initialX;
|
|
1104
|
+
const dy = event.offsetY - initialY;
|
|
1064
1105
|
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
1065
1106
|
}
|
|
1066
1107
|
function filterPointerEvents(objects) {
|
|
@@ -1096,6 +1137,15 @@ function createEvents(store) {
|
|
|
1096
1137
|
return state2.raycaster.camera ? state2.raycaster.intersectObject(obj, true) : [];
|
|
1097
1138
|
}
|
|
1098
1139
|
let hits = eventsObjects.flatMap(handleRaycast).sort((a, b) => {
|
|
1140
|
+
const aInteractivePriority = a.object.userData?.interactivePriority;
|
|
1141
|
+
const bInteractivePriority = b.object.userData?.interactivePriority;
|
|
1142
|
+
if (aInteractivePriority !== void 0 || bInteractivePriority !== void 0) {
|
|
1143
|
+
if (aInteractivePriority !== void 0 && bInteractivePriority === void 0) return -1;
|
|
1144
|
+
if (bInteractivePriority !== void 0 && aInteractivePriority === void 0) return 1;
|
|
1145
|
+
if (aInteractivePriority !== bInteractivePriority) {
|
|
1146
|
+
return (bInteractivePriority ?? 0) - (aInteractivePriority ?? 0);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1099
1149
|
const aState = getRootState(a.object);
|
|
1100
1150
|
const bState = getRootState(b.object);
|
|
1101
1151
|
const aPriority = aState?.events?.priority ?? 1;
|
|
@@ -1117,9 +1167,13 @@ function createEvents(store) {
|
|
|
1117
1167
|
eventObject = eventObject.parent;
|
|
1118
1168
|
}
|
|
1119
1169
|
}
|
|
1120
|
-
if ("pointerId" in event
|
|
1121
|
-
|
|
1122
|
-
|
|
1170
|
+
if ("pointerId" in event) {
|
|
1171
|
+
const pointerId = event.pointerId;
|
|
1172
|
+
const pointerState = state.internal.pointerMap.get(pointerId);
|
|
1173
|
+
if (pointerState?.captured.size) {
|
|
1174
|
+
for (const captureData of pointerState.captured.values()) {
|
|
1175
|
+
if (!duplicates.has(makeId(captureData.intersection))) intersections.push(captureData.intersection);
|
|
1176
|
+
}
|
|
1123
1177
|
}
|
|
1124
1178
|
}
|
|
1125
1179
|
return intersections;
|
|
@@ -1132,27 +1186,25 @@ function createEvents(store) {
|
|
|
1132
1186
|
if (state) {
|
|
1133
1187
|
const { raycaster, pointer, camera, internal } = state;
|
|
1134
1188
|
const unprojectedPoint = new webgpu.Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
1135
|
-
const hasPointerCapture = (id) =>
|
|
1189
|
+
const hasPointerCapture = (id) => {
|
|
1190
|
+
const pointerState = internal.pointerMap.get(id);
|
|
1191
|
+
return pointerState?.captured.has(hit.eventObject) ?? false;
|
|
1192
|
+
};
|
|
1136
1193
|
const setPointerCapture = (id) => {
|
|
1137
1194
|
const captureData = { intersection: hit, target: event.target };
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
} else {
|
|
1141
|
-
internal.capturedMap.set(id, /* @__PURE__ */ new Map([[hit.eventObject, captureData]]));
|
|
1142
|
-
}
|
|
1195
|
+
const pointerState = getPointerState(internal, id);
|
|
1196
|
+
pointerState.captured.set(hit.eventObject, captureData);
|
|
1143
1197
|
event.target.setPointerCapture(id);
|
|
1144
1198
|
};
|
|
1145
1199
|
const releasePointerCapture = (id) => {
|
|
1146
|
-
|
|
1147
|
-
if (captures) {
|
|
1148
|
-
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
1149
|
-
}
|
|
1200
|
+
releaseInternalPointerCapture(internal, hit.eventObject, id);
|
|
1150
1201
|
};
|
|
1151
1202
|
const extractEventProps = {};
|
|
1152
1203
|
for (const prop in event) {
|
|
1153
1204
|
const property = event[prop];
|
|
1154
1205
|
if (typeof property !== "function") extractEventProps[prop] = property;
|
|
1155
1206
|
}
|
|
1207
|
+
const eventPointerId = "pointerId" in event ? event.pointerId : void 0;
|
|
1156
1208
|
const raycastEvent = {
|
|
1157
1209
|
...hit,
|
|
1158
1210
|
...extractEventProps,
|
|
@@ -1163,18 +1215,19 @@ function createEvents(store) {
|
|
|
1163
1215
|
unprojectedPoint,
|
|
1164
1216
|
ray: raycaster.ray,
|
|
1165
1217
|
camera,
|
|
1218
|
+
pointerId: eventPointerId,
|
|
1166
1219
|
// Hijack stopPropagation, which just sets a flag
|
|
1167
1220
|
stopPropagation() {
|
|
1168
|
-
const
|
|
1221
|
+
const pointerState = eventPointerId !== void 0 ? internal.pointerMap.get(eventPointerId) : void 0;
|
|
1169
1222
|
if (
|
|
1170
1223
|
// ...if this pointer hasn't been captured
|
|
1171
|
-
!
|
|
1172
|
-
|
|
1224
|
+
!pointerState?.captured.size || // ... or if the hit object is capturing the pointer
|
|
1225
|
+
pointerState.captured.has(hit.eventObject)
|
|
1173
1226
|
) {
|
|
1174
1227
|
raycastEvent.stopped = localState.stopped = true;
|
|
1175
|
-
if (
|
|
1228
|
+
if (pointerState?.hovered.size && Array.from(pointerState.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
1176
1229
|
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
1177
|
-
cancelPointer([...higher, hit]);
|
|
1230
|
+
cancelPointer([...higher, hit], eventPointerId);
|
|
1178
1231
|
}
|
|
1179
1232
|
}
|
|
1180
1233
|
},
|
|
@@ -1190,15 +1243,18 @@ function createEvents(store) {
|
|
|
1190
1243
|
}
|
|
1191
1244
|
return intersections;
|
|
1192
1245
|
}
|
|
1193
|
-
function cancelPointer(intersections) {
|
|
1246
|
+
function cancelPointer(intersections, pointerId) {
|
|
1194
1247
|
const { internal } = store.getState();
|
|
1195
|
-
|
|
1248
|
+
const pid = pointerId ?? DEFAULT_POINTER_ID;
|
|
1249
|
+
const pointerState = internal.pointerMap.get(pid);
|
|
1250
|
+
if (!pointerState) return;
|
|
1251
|
+
for (const [hoveredId, hoveredObj] of pointerState.hovered) {
|
|
1196
1252
|
if (!intersections.length || !intersections.find(
|
|
1197
1253
|
(hit) => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId
|
|
1198
1254
|
)) {
|
|
1199
1255
|
const eventObject = hoveredObj.eventObject;
|
|
1200
1256
|
const instance = eventObject.__r3f;
|
|
1201
|
-
|
|
1257
|
+
pointerState.hovered.delete(hoveredId);
|
|
1202
1258
|
if (instance?.eventCount) {
|
|
1203
1259
|
const handlers = instance.handlers;
|
|
1204
1260
|
const data = { ...hoveredObj, intersections };
|
|
@@ -1227,41 +1283,118 @@ function createEvents(store) {
|
|
|
1227
1283
|
instance?.handlers.onDropMissed?.(event);
|
|
1228
1284
|
}
|
|
1229
1285
|
}
|
|
1286
|
+
function cleanupPointer(pointerId) {
|
|
1287
|
+
const { internal } = store.getState();
|
|
1288
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1289
|
+
if (pointerState) {
|
|
1290
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1291
|
+
const eventObject = hoveredObj.eventObject;
|
|
1292
|
+
const instance = eventObject.__r3f;
|
|
1293
|
+
if (instance?.eventCount) {
|
|
1294
|
+
const handlers = instance.handlers;
|
|
1295
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1296
|
+
handlers.onPointerOut?.(data);
|
|
1297
|
+
handlers.onPointerLeave?.(data);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
internal.pointerMap.delete(pointerId);
|
|
1301
|
+
}
|
|
1302
|
+
internal.pointerDirty.delete(pointerId);
|
|
1303
|
+
}
|
|
1304
|
+
function processDeferredPointer(event, pointerId) {
|
|
1305
|
+
const state = store.getState();
|
|
1306
|
+
const { internal } = state;
|
|
1307
|
+
if (!state.events.enabled) return;
|
|
1308
|
+
const filter = filterPointerEvents;
|
|
1309
|
+
const hits = intersect(event, filter);
|
|
1310
|
+
cancelPointer(hits, pointerId);
|
|
1311
|
+
function onIntersect(data) {
|
|
1312
|
+
const eventObject = data.eventObject;
|
|
1313
|
+
const instance = eventObject.__r3f;
|
|
1314
|
+
if (!instance?.eventCount) return;
|
|
1315
|
+
const handlers = instance.handlers;
|
|
1316
|
+
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1317
|
+
const id = makeId(data);
|
|
1318
|
+
const pointerState = getPointerState(internal, pointerId);
|
|
1319
|
+
const hoveredItem = pointerState.hovered.get(id);
|
|
1320
|
+
if (!hoveredItem) {
|
|
1321
|
+
pointerState.hovered.set(id, data);
|
|
1322
|
+
handlers.onPointerOver?.(data);
|
|
1323
|
+
handlers.onPointerEnter?.(data);
|
|
1324
|
+
} else if (hoveredItem.stopped) {
|
|
1325
|
+
data.stopPropagation();
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
handlers.onPointerMove?.(data);
|
|
1329
|
+
}
|
|
1330
|
+
handleIntersects(hits, event, 0, onIntersect);
|
|
1331
|
+
}
|
|
1230
1332
|
function handlePointer(name) {
|
|
1231
1333
|
switch (name) {
|
|
1232
1334
|
case "onPointerLeave":
|
|
1233
|
-
case "onPointerCancel":
|
|
1234
1335
|
case "onDragLeave":
|
|
1235
1336
|
return () => cancelPointer([]);
|
|
1337
|
+
// Global cancel of these events
|
|
1338
|
+
case "onPointerCancel":
|
|
1339
|
+
return (event) => {
|
|
1340
|
+
const pointerId = getPointerId(event);
|
|
1341
|
+
cleanupPointer(pointerId);
|
|
1342
|
+
};
|
|
1236
1343
|
case "onLostPointerCapture":
|
|
1237
1344
|
return (event) => {
|
|
1238
1345
|
const { internal } = store.getState();
|
|
1239
|
-
|
|
1346
|
+
const pointerId = getPointerId(event);
|
|
1347
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1348
|
+
if (pointerState?.captured.size) {
|
|
1240
1349
|
requestAnimationFrame(() => {
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1350
|
+
const pointerState2 = internal.pointerMap.get(pointerId);
|
|
1351
|
+
if (pointerState2?.captured.size) {
|
|
1352
|
+
pointerState2.captured.clear();
|
|
1244
1353
|
}
|
|
1354
|
+
cancelPointer([], pointerId);
|
|
1245
1355
|
});
|
|
1246
1356
|
}
|
|
1247
1357
|
};
|
|
1248
1358
|
}
|
|
1249
1359
|
return function handleEvent(event) {
|
|
1250
1360
|
const state = store.getState();
|
|
1251
|
-
const { onPointerMissed, onDragOverMissed, onDropMissed, internal } = state;
|
|
1361
|
+
const { onPointerMissed, onDragOverMissed, onDropMissed, internal, events } = state;
|
|
1362
|
+
const pointerId = getPointerId(event);
|
|
1252
1363
|
internal.lastEvent.current = event;
|
|
1253
|
-
if (!
|
|
1364
|
+
if (!events.enabled) return;
|
|
1254
1365
|
const isPointerMove = name === "onPointerMove";
|
|
1255
1366
|
const isDragOver = name === "onDragOver";
|
|
1256
1367
|
const isDrop = name === "onDrop";
|
|
1257
1368
|
const isClickEvent = name === "onClick" || name === "onContextMenu" || name === "onDoubleClick";
|
|
1369
|
+
const isPointerDown = name === "onPointerDown";
|
|
1370
|
+
const isPointerUp = name === "onPointerUp";
|
|
1371
|
+
const isWheel = name === "onWheel";
|
|
1372
|
+
const canDeferRaycasts = events.frameTimedRaycasts && state.frameloop === "always";
|
|
1373
|
+
if (isPointerMove && canDeferRaycasts) {
|
|
1374
|
+
events.compute?.(event, state);
|
|
1375
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
if (isWheel && canDeferRaycasts && !events.alwaysFireOnScroll) {
|
|
1379
|
+
events.compute?.(event, state);
|
|
1380
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
if ((isClickEvent || isPointerDown || isPointerUp) && internal.pointerDirty.has(pointerId)) {
|
|
1384
|
+
const deferredEvent = internal.pointerDirty.get(pointerId);
|
|
1385
|
+
internal.pointerDirty.delete(pointerId);
|
|
1386
|
+
processDeferredPointer(deferredEvent, pointerId);
|
|
1387
|
+
}
|
|
1258
1388
|
const filter = isPointerMove || isDragOver || isDrop ? filterPointerEvents : void 0;
|
|
1259
1389
|
const hits = intersect(event, filter);
|
|
1260
|
-
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
1261
|
-
if (
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1390
|
+
const delta = isClickEvent ? calculateDistance(event, pointerId) : 0;
|
|
1391
|
+
if (isPointerDown) {
|
|
1392
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1393
|
+
pointerState2.initialClick = [event.offsetX, event.offsetY];
|
|
1394
|
+
pointerState2.initialHits = hits.map((hit) => hit.eventObject);
|
|
1395
|
+
}
|
|
1396
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1397
|
+
const initialHits = pointerState?.initialHits ?? [];
|
|
1265
1398
|
if (isClickEvent && !hits.length) {
|
|
1266
1399
|
if (delta <= 2) {
|
|
1267
1400
|
pointerMissed(event, internal.interaction);
|
|
@@ -1276,7 +1409,9 @@ function createEvents(store) {
|
|
|
1276
1409
|
dropMissed(event, internal.interaction);
|
|
1277
1410
|
if (onDropMissed) onDropMissed(event);
|
|
1278
1411
|
}
|
|
1279
|
-
if (isPointerMove || isDragOver)
|
|
1412
|
+
if (isPointerMove || isDragOver) {
|
|
1413
|
+
cancelPointer(hits, pointerId);
|
|
1414
|
+
}
|
|
1280
1415
|
function onIntersect(data) {
|
|
1281
1416
|
const eventObject = data.eventObject;
|
|
1282
1417
|
const instance = eventObject.__r3f;
|
|
@@ -1285,9 +1420,10 @@ function createEvents(store) {
|
|
|
1285
1420
|
if (isPointerMove) {
|
|
1286
1421
|
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1287
1422
|
const id = makeId(data);
|
|
1288
|
-
const
|
|
1423
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1424
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1289
1425
|
if (!hoveredItem) {
|
|
1290
|
-
|
|
1426
|
+
pointerState2.hovered.set(id, data);
|
|
1291
1427
|
handlers.onPointerOver?.(data);
|
|
1292
1428
|
handlers.onPointerEnter?.(data);
|
|
1293
1429
|
} else if (hoveredItem.stopped) {
|
|
@@ -1297,9 +1433,10 @@ function createEvents(store) {
|
|
|
1297
1433
|
handlers.onPointerMove?.(data);
|
|
1298
1434
|
} else if (isDragOver) {
|
|
1299
1435
|
const id = makeId(data);
|
|
1300
|
-
const
|
|
1436
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1437
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1301
1438
|
if (!hoveredItem) {
|
|
1302
|
-
|
|
1439
|
+
pointerState2.hovered.set(id, data);
|
|
1303
1440
|
handlers.onDragOverEnter?.(data);
|
|
1304
1441
|
} else if (hoveredItem.stopped) {
|
|
1305
1442
|
data.stopPropagation();
|
|
@@ -1310,18 +1447,18 @@ function createEvents(store) {
|
|
|
1310
1447
|
} else {
|
|
1311
1448
|
const handler = handlers[name];
|
|
1312
1449
|
if (handler) {
|
|
1313
|
-
if (!isClickEvent ||
|
|
1450
|
+
if (!isClickEvent || initialHits.includes(eventObject)) {
|
|
1314
1451
|
pointerMissed(
|
|
1315
1452
|
event,
|
|
1316
|
-
internal.interaction.filter((object) => !
|
|
1453
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1317
1454
|
);
|
|
1318
1455
|
handler(data);
|
|
1319
1456
|
}
|
|
1320
1457
|
} else {
|
|
1321
|
-
if (isClickEvent &&
|
|
1458
|
+
if (isClickEvent && initialHits.includes(eventObject)) {
|
|
1322
1459
|
pointerMissed(
|
|
1323
1460
|
event,
|
|
1324
|
-
internal.interaction.filter((object) => !
|
|
1461
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1325
1462
|
);
|
|
1326
1463
|
}
|
|
1327
1464
|
}
|
|
@@ -1330,7 +1467,15 @@ function createEvents(store) {
|
|
|
1330
1467
|
handleIntersects(hits, event, delta, onIntersect);
|
|
1331
1468
|
};
|
|
1332
1469
|
}
|
|
1333
|
-
|
|
1470
|
+
function flushDeferredPointers() {
|
|
1471
|
+
const { internal, events } = store.getState();
|
|
1472
|
+
if (!events.frameTimedRaycasts) return;
|
|
1473
|
+
for (const [pointerId, event] of internal.pointerDirty) {
|
|
1474
|
+
processDeferredPointer(event, pointerId);
|
|
1475
|
+
}
|
|
1476
|
+
internal.pointerDirty.clear();
|
|
1477
|
+
}
|
|
1478
|
+
return { handlePointer, flushDeferredPointers, processDeferredPointer };
|
|
1334
1479
|
}
|
|
1335
1480
|
const DOM_EVENTS = {
|
|
1336
1481
|
onClick: ["click", false],
|
|
@@ -1349,10 +1494,15 @@ const DOM_EVENTS = {
|
|
|
1349
1494
|
onLostPointerCapture: ["lostpointercapture", true]
|
|
1350
1495
|
};
|
|
1351
1496
|
function createPointerEvents(store) {
|
|
1352
|
-
const { handlePointer } = createEvents(store);
|
|
1497
|
+
const { handlePointer, flushDeferredPointers, processDeferredPointer } = createEvents(store);
|
|
1498
|
+
let nextXRPointerId = XR_POINTER_ID_START;
|
|
1499
|
+
const xrPointers = /* @__PURE__ */ new Map();
|
|
1353
1500
|
return {
|
|
1354
1501
|
priority: 1,
|
|
1355
1502
|
enabled: true,
|
|
1503
|
+
frameTimedRaycasts: true,
|
|
1504
|
+
alwaysFireOnScroll: true,
|
|
1505
|
+
updateOnFrame: false,
|
|
1356
1506
|
compute(event, state) {
|
|
1357
1507
|
state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
1358
1508
|
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
@@ -1362,11 +1512,33 @@ function createPointerEvents(store) {
|
|
|
1362
1512
|
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
1363
1513
|
{}
|
|
1364
1514
|
),
|
|
1365
|
-
update: () => {
|
|
1515
|
+
update: (pointerId) => {
|
|
1366
1516
|
const { events, internal } = store.getState();
|
|
1367
|
-
if (
|
|
1517
|
+
if (!events.handlers) return;
|
|
1518
|
+
if (pointerId !== void 0) {
|
|
1519
|
+
const event = internal.pointerDirty.get(pointerId);
|
|
1520
|
+
if (event) {
|
|
1521
|
+
internal.pointerDirty.delete(pointerId);
|
|
1522
|
+
processDeferredPointer(event, pointerId);
|
|
1523
|
+
} else if (internal.lastEvent?.current) {
|
|
1524
|
+
processDeferredPointer(internal.lastEvent.current, pointerId);
|
|
1525
|
+
}
|
|
1526
|
+
} else {
|
|
1527
|
+
flushDeferredPointers();
|
|
1528
|
+
if (internal.lastEvent?.current) {
|
|
1529
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
},
|
|
1533
|
+
flush: () => {
|
|
1534
|
+
const { events, internal } = store.getState();
|
|
1535
|
+
flushDeferredPointers();
|
|
1536
|
+
if (events.updateOnFrame && internal.lastEvent?.current && events.handlers) {
|
|
1537
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1538
|
+
}
|
|
1368
1539
|
},
|
|
1369
1540
|
connect: (target) => {
|
|
1541
|
+
if (!target) return;
|
|
1370
1542
|
const { set, events } = store.getState();
|
|
1371
1543
|
events.disconnect?.();
|
|
1372
1544
|
set((state) => ({ events: { ...state.events, connected: target } }));
|
|
@@ -1390,6 +1562,32 @@ function createPointerEvents(store) {
|
|
|
1390
1562
|
}
|
|
1391
1563
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
1392
1564
|
}
|
|
1565
|
+
},
|
|
1566
|
+
registerPointer: (config) => {
|
|
1567
|
+
const pointerId = nextXRPointerId++;
|
|
1568
|
+
xrPointers.set(pointerId, config);
|
|
1569
|
+
const { internal } = store.getState();
|
|
1570
|
+
getPointerState(internal, pointerId);
|
|
1571
|
+
return pointerId;
|
|
1572
|
+
},
|
|
1573
|
+
unregisterPointer: (pointerId) => {
|
|
1574
|
+
xrPointers.delete(pointerId);
|
|
1575
|
+
const { internal } = store.getState();
|
|
1576
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1577
|
+
if (pointerState) {
|
|
1578
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1579
|
+
const eventObject = hoveredObj.eventObject;
|
|
1580
|
+
const instance = eventObject.__r3f;
|
|
1581
|
+
if (instance?.eventCount) {
|
|
1582
|
+
const handlers = instance.handlers;
|
|
1583
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1584
|
+
handlers.onPointerOut?.(data);
|
|
1585
|
+
handlers.onPointerLeave?.(data);
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
internal.pointerMap.delete(pointerId);
|
|
1589
|
+
}
|
|
1590
|
+
internal.pointerDirty.delete(pointerId);
|
|
1393
1591
|
}
|
|
1394
1592
|
};
|
|
1395
1593
|
}
|
|
@@ -1451,1038 +1649,6 @@ function notifyAlpha({ message, link }) {
|
|
|
1451
1649
|
}
|
|
1452
1650
|
}
|
|
1453
1651
|
|
|
1454
|
-
var __defProp$1 = Object.defineProperty;
|
|
1455
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1456
|
-
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1457
|
-
const DEFAULT_PHASES = ["start", "input", "physics", "update", "render", "finish"];
|
|
1458
|
-
class PhaseGraph {
|
|
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?.();
|
|
2484
|
-
}
|
|
2485
|
-
|
|
2486
1652
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
2487
1653
|
const context = globalThis[R3F_CONTEXT] ?? (globalThis[R3F_CONTEXT] = React__namespace.createContext(null));
|
|
2488
1654
|
const createStore = (invalidate, advance) => {
|
|
@@ -2520,7 +1686,14 @@ const createStore = (invalidate, advance) => {
|
|
|
2520
1686
|
frustum: new webgpu.Frustum(),
|
|
2521
1687
|
autoUpdateFrustum: true,
|
|
2522
1688
|
raycaster: null,
|
|
2523
|
-
events: {
|
|
1689
|
+
events: {
|
|
1690
|
+
priority: 1,
|
|
1691
|
+
enabled: true,
|
|
1692
|
+
connected: false,
|
|
1693
|
+
frameTimedRaycasts: true,
|
|
1694
|
+
alwaysFireOnScroll: true,
|
|
1695
|
+
updateOnFrame: false
|
|
1696
|
+
},
|
|
2524
1697
|
scene: null,
|
|
2525
1698
|
rootScene: null,
|
|
2526
1699
|
xr: null,
|
|
@@ -2585,7 +1758,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2585
1758
|
size: newSize,
|
|
2586
1759
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, newSize) }
|
|
2587
1760
|
}));
|
|
2588
|
-
getScheduler().invalidate();
|
|
1761
|
+
scheduler.getScheduler().invalidate();
|
|
2589
1762
|
}
|
|
2590
1763
|
}
|
|
2591
1764
|
return;
|
|
@@ -2600,7 +1773,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2600
1773
|
viewport: { ...s.viewport, ...getCurrentViewport(state2.camera, defaultTarget, size) },
|
|
2601
1774
|
_sizeImperative: true
|
|
2602
1775
|
}));
|
|
2603
|
-
getScheduler().invalidate();
|
|
1776
|
+
scheduler.getScheduler().invalidate();
|
|
2604
1777
|
},
|
|
2605
1778
|
setDpr: (dpr) => set((state2) => {
|
|
2606
1779
|
const resolved = calculateDpr(dpr);
|
|
@@ -2611,11 +1784,14 @@ const createStore = (invalidate, advance) => {
|
|
|
2611
1784
|
},
|
|
2612
1785
|
setError: (error) => set(() => ({ error })),
|
|
2613
1786
|
error: null,
|
|
2614
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useTextures,
|
|
1787
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
2615
1788
|
uniforms: {},
|
|
2616
1789
|
nodes: {},
|
|
1790
|
+
buffers: {},
|
|
1791
|
+
gpuStorage: {},
|
|
2617
1792
|
textures: /* @__PURE__ */ new Map(),
|
|
2618
|
-
|
|
1793
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1794
|
+
renderPipeline: null,
|
|
2619
1795
|
passes: {},
|
|
2620
1796
|
_hmrVersion: 0,
|
|
2621
1797
|
_sizeImperative: false,
|
|
@@ -2624,12 +1800,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2624
1800
|
internal: {
|
|
2625
1801
|
// Events
|
|
2626
1802
|
interaction: [],
|
|
2627
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
2628
1803
|
subscribers: [],
|
|
1804
|
+
// Per-pointer state (new unified structure)
|
|
1805
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1806
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1807
|
+
lastEvent: React__namespace.createRef(),
|
|
1808
|
+
// Deprecated but kept for backwards compatibility
|
|
1809
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
2629
1810
|
initialClick: [0, 0],
|
|
2630
1811
|
initialHits: [],
|
|
2631
1812
|
capturedMap: /* @__PURE__ */ new Map(),
|
|
2632
|
-
lastEvent: React__namespace.createRef(),
|
|
2633
1813
|
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
2634
1814
|
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
2635
1815
|
// Occlusion system (WebGPU only)
|
|
@@ -2717,14 +1897,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2717
1897
|
oldSize = size;
|
|
2718
1898
|
oldDpr = viewport.dpr;
|
|
2719
1899
|
updateCamera(camera, size);
|
|
2720
|
-
if (canvasTarget) {
|
|
1900
|
+
if (internal.isSecondary && canvasTarget) {
|
|
2721
1901
|
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
2722
|
-
|
|
2723
|
-
canvasTarget.setSize(size.width, size.height, updateStyle);
|
|
1902
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
2724
1903
|
} else {
|
|
2725
1904
|
if (viewport.dpr > 0) actualRenderer.setPixelRatio(viewport.dpr);
|
|
2726
|
-
|
|
2727
|
-
|
|
1905
|
+
actualRenderer.setSize(size.width, size.height, false);
|
|
1906
|
+
if (canvasTarget) {
|
|
1907
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1908
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1909
|
+
}
|
|
2728
1910
|
}
|
|
2729
1911
|
}
|
|
2730
1912
|
if (camera !== oldCamera) {
|
|
@@ -2799,7 +1981,7 @@ useLoader.loader = getLoader;
|
|
|
2799
1981
|
function useFrame(callback, priorityOrOptions) {
|
|
2800
1982
|
const store = React__namespace.useContext(context);
|
|
2801
1983
|
const isInsideCanvas = store !== null;
|
|
2802
|
-
const scheduler = getScheduler();
|
|
1984
|
+
const scheduler$1 = scheduler.getScheduler();
|
|
2803
1985
|
const optionsKey = typeof priorityOrOptions === "number" ? `p:${priorityOrOptions}` : priorityOrOptions ? JSON.stringify({
|
|
2804
1986
|
id: priorityOrOptions.id,
|
|
2805
1987
|
phase: priorityOrOptions.phase,
|
|
@@ -2847,7 +2029,7 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2847
2029
|
};
|
|
2848
2030
|
callbackRef.current?.(mergedState, delta);
|
|
2849
2031
|
};
|
|
2850
|
-
const unregister = scheduler.register(wrappedCallback, {
|
|
2032
|
+
const unregister = scheduler$1.register(wrappedCallback, {
|
|
2851
2033
|
id,
|
|
2852
2034
|
rootId,
|
|
2853
2035
|
...options
|
|
@@ -2868,37 +2050,31 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2868
2050
|
}
|
|
2869
2051
|
};
|
|
2870
2052
|
} else {
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
unregisterJob = registerOutside();
|
|
2880
|
-
});
|
|
2881
|
-
return () => {
|
|
2882
|
-
unsubReady();
|
|
2883
|
-
unregisterJob?.();
|
|
2884
|
-
};
|
|
2053
|
+
return scheduler$1.register(
|
|
2054
|
+
(state, delta) => {
|
|
2055
|
+
const frameState = state;
|
|
2056
|
+
if (!frameState.renderer) return;
|
|
2057
|
+
callbackRef.current?.(frameState, delta);
|
|
2058
|
+
},
|
|
2059
|
+
{ id, ...options }
|
|
2060
|
+
);
|
|
2885
2061
|
}
|
|
2886
|
-
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2062
|
+
}, [store, scheduler$1, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2887
2063
|
const isPaused = React__namespace.useSyncExternalStore(
|
|
2888
2064
|
// Subscribe function
|
|
2889
2065
|
React__namespace.useCallback(
|
|
2890
2066
|
(onStoreChange) => {
|
|
2891
|
-
return getScheduler().subscribeJobState(id, onStoreChange);
|
|
2067
|
+
return scheduler.getScheduler().subscribeJobState(id, onStoreChange);
|
|
2892
2068
|
},
|
|
2893
2069
|
[id]
|
|
2894
2070
|
),
|
|
2895
2071
|
// getSnapshot function
|
|
2896
|
-
React__namespace.useCallback(() => getScheduler().isJobPaused(id), [id]),
|
|
2072
|
+
React__namespace.useCallback(() => scheduler.getScheduler().isJobPaused(id), [id]),
|
|
2897
2073
|
// getServerSnapshot function (SSR)
|
|
2898
2074
|
React__namespace.useCallback(() => false, [])
|
|
2899
2075
|
);
|
|
2900
2076
|
const controls = React__namespace.useMemo(() => {
|
|
2901
|
-
const scheduler2 = getScheduler();
|
|
2077
|
+
const scheduler2 = scheduler.getScheduler();
|
|
2902
2078
|
return {
|
|
2903
2079
|
/** The job's unique ID */
|
|
2904
2080
|
id,
|
|
@@ -2913,7 +2089,7 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2913
2089
|
* @param timestamp Optional timestamp (defaults to performance.now())
|
|
2914
2090
|
*/
|
|
2915
2091
|
step: (timestamp) => {
|
|
2916
|
-
getScheduler().stepJob(id, timestamp);
|
|
2092
|
+
scheduler.getScheduler().stepJob(id, timestamp);
|
|
2917
2093
|
},
|
|
2918
2094
|
/**
|
|
2919
2095
|
* Manually step ALL jobs in the scheduler.
|
|
@@ -2921,20 +2097,20 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2921
2097
|
* @param timestamp Optional timestamp (defaults to performance.now())
|
|
2922
2098
|
*/
|
|
2923
2099
|
stepAll: (timestamp) => {
|
|
2924
|
-
getScheduler().step(timestamp);
|
|
2100
|
+
scheduler.getScheduler().step(timestamp);
|
|
2925
2101
|
},
|
|
2926
2102
|
/**
|
|
2927
2103
|
* Pause this job (set enabled=false).
|
|
2928
2104
|
* Job remains registered but won't run.
|
|
2929
2105
|
*/
|
|
2930
2106
|
pause: () => {
|
|
2931
|
-
getScheduler().pauseJob(id);
|
|
2107
|
+
scheduler.getScheduler().pauseJob(id);
|
|
2932
2108
|
},
|
|
2933
2109
|
/**
|
|
2934
2110
|
* Resume this job (set enabled=true).
|
|
2935
2111
|
*/
|
|
2936
2112
|
resume: () => {
|
|
2937
|
-
getScheduler().resumeJob(id);
|
|
2113
|
+
scheduler.getScheduler().resumeJob(id);
|
|
2938
2114
|
},
|
|
2939
2115
|
/**
|
|
2940
2116
|
* Reactive paused state - automatically updates when pause/resume is called.
|
|
@@ -2972,18 +2148,18 @@ function buildFromCache(input, textureCache) {
|
|
|
2972
2148
|
function useTexture(input, optionsOrOnLoad) {
|
|
2973
2149
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
2974
2150
|
const store = useStore();
|
|
2975
|
-
const textureCache = useThree((state) => state.textures);
|
|
2976
2151
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
2977
|
-
const { onLoad, cache =
|
|
2152
|
+
const { onLoad, cache = true } = options;
|
|
2978
2153
|
const onLoadRef = React.useRef(onLoad);
|
|
2979
2154
|
onLoadRef.current = onLoad;
|
|
2980
2155
|
const onLoadCalledForRef = React.useRef(null);
|
|
2981
2156
|
const urls = React.useMemo(() => getUrls(input), [input]);
|
|
2982
2157
|
const cachedResult = React.useMemo(() => {
|
|
2983
2158
|
if (!cache) return null;
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2159
|
+
const textures = store.getState().textures;
|
|
2160
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2161
|
+
return buildFromCache(input, textures);
|
|
2162
|
+
}, [cache, urls, input, store]);
|
|
2987
2163
|
const loadedTextures = useLoader(
|
|
2988
2164
|
webgpu.TextureLoader,
|
|
2989
2165
|
IsObject(input) ? Object.values(input) : input
|
|
@@ -3027,8 +2203,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3027
2203
|
}, [input, loadedTextures, cachedResult]);
|
|
3028
2204
|
React.useEffect(() => {
|
|
3029
2205
|
if (!cache) return;
|
|
3030
|
-
if (cachedResult) return;
|
|
3031
|
-
const set = store.setState;
|
|
3032
2206
|
const urlTextureMap = [];
|
|
3033
2207
|
if (typeof input === "string") {
|
|
3034
2208
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -3042,18 +2216,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3042
2216
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
3043
2217
|
}
|
|
3044
2218
|
}
|
|
3045
|
-
|
|
3046
|
-
const
|
|
3047
|
-
let
|
|
2219
|
+
store.setState((state) => {
|
|
2220
|
+
const refs = new Map(state._textureRefs);
|
|
2221
|
+
let textures = state.textures;
|
|
2222
|
+
let added = false;
|
|
3048
2223
|
for (const [url, texture] of urlTextureMap) {
|
|
3049
|
-
if (!
|
|
3050
|
-
|
|
3051
|
-
|
|
2224
|
+
if (!textures.has(url)) {
|
|
2225
|
+
if (!added) {
|
|
2226
|
+
textures = new Map(textures);
|
|
2227
|
+
added = true;
|
|
2228
|
+
}
|
|
2229
|
+
textures.set(url, texture);
|
|
3052
2230
|
}
|
|
2231
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
3053
2232
|
}
|
|
3054
|
-
return
|
|
2233
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
3055
2234
|
});
|
|
3056
|
-
|
|
2235
|
+
return () => store.setState((state) => {
|
|
2236
|
+
const refs = new Map(state._textureRefs);
|
|
2237
|
+
for (const [url] of urlTextureMap) {
|
|
2238
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2239
|
+
if (next <= 0) refs.delete(url);
|
|
2240
|
+
else refs.set(url, next);
|
|
2241
|
+
}
|
|
2242
|
+
return { _textureRefs: refs };
|
|
2243
|
+
});
|
|
2244
|
+
}, [cache, input, mappedTextures, store]);
|
|
3057
2245
|
return mappedTextures;
|
|
3058
2246
|
}
|
|
3059
2247
|
useTexture.preload = (url) => useLoader.preload(webgpu.TextureLoader, url);
|
|
@@ -3069,96 +2257,63 @@ const Texture = ({
|
|
|
3069
2257
|
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: children?.(ret) });
|
|
3070
2258
|
};
|
|
3071
2259
|
|
|
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() {
|
|
2260
|
+
function useTextures(selector) {
|
|
3080
2261
|
const store = useStore();
|
|
3081
|
-
|
|
3082
|
-
const set = store.setState;
|
|
2262
|
+
const registry = React.useMemo(() => {
|
|
3083
2263
|
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
|
-
};
|
|
2264
|
+
const setState = store.setState;
|
|
2265
|
+
const getOne = (key) => getState().textures.get(key);
|
|
3142
2266
|
return {
|
|
3143
|
-
|
|
3144
|
-
get textures() {
|
|
2267
|
+
get all() {
|
|
3145
2268
|
return getState().textures;
|
|
3146
2269
|
},
|
|
3147
|
-
|
|
3148
|
-
|
|
2270
|
+
get(input) {
|
|
2271
|
+
if (typeof input === "string") return getOne(input);
|
|
2272
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2273
|
+
const out = {};
|
|
2274
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2275
|
+
return out;
|
|
2276
|
+
},
|
|
3149
2277
|
has: (key) => getState().textures.has(key),
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
2278
|
+
add(keyOrRecord, texture) {
|
|
2279
|
+
setState((state) => {
|
|
2280
|
+
const textures = new Map(state.textures);
|
|
2281
|
+
if (typeof keyOrRecord === "string") {
|
|
2282
|
+
textures.set(keyOrRecord, texture);
|
|
2283
|
+
} else {
|
|
2284
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2285
|
+
}
|
|
2286
|
+
return { textures };
|
|
2287
|
+
});
|
|
2288
|
+
},
|
|
2289
|
+
dispose(key, options) {
|
|
2290
|
+
const state = getState();
|
|
2291
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2292
|
+
if (refs > 0 && !options?.force) {
|
|
2293
|
+
console.warn(
|
|
2294
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2295
|
+
);
|
|
2296
|
+
return false;
|
|
2297
|
+
}
|
|
2298
|
+
state.textures.get(key)?.dispose();
|
|
2299
|
+
setState((s) => {
|
|
2300
|
+
const textures = new Map(s.textures);
|
|
2301
|
+
textures.delete(key);
|
|
2302
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2303
|
+
nextRefs.delete(key);
|
|
2304
|
+
return { textures, _textureRefs: nextRefs };
|
|
2305
|
+
});
|
|
2306
|
+
return true;
|
|
2307
|
+
},
|
|
2308
|
+
disposeAll() {
|
|
2309
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2310
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2311
|
+
}
|
|
3160
2312
|
};
|
|
3161
2313
|
}, [store]);
|
|
2314
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2315
|
+
const selected = useThree(subscribe);
|
|
2316
|
+
return selector ? selected : registry;
|
|
3162
2317
|
}
|
|
3163
2318
|
|
|
3164
2319
|
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
@@ -3215,7 +2370,7 @@ function addEffect(callback) {
|
|
|
3215
2370
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3216
2371
|
});
|
|
3217
2372
|
const id = `legacy_effect_${effectId++}`;
|
|
3218
|
-
return getScheduler().registerGlobal("before", id, callback);
|
|
2373
|
+
return scheduler.getScheduler().registerGlobal("before", id, callback);
|
|
3219
2374
|
}
|
|
3220
2375
|
function addAfterEffect(callback) {
|
|
3221
2376
|
notifyDepreciated({
|
|
@@ -3224,7 +2379,7 @@ function addAfterEffect(callback) {
|
|
|
3224
2379
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3225
2380
|
});
|
|
3226
2381
|
const id = `legacy_afterEffect_${effectId++}`;
|
|
3227
|
-
return getScheduler().registerGlobal("after", id, callback);
|
|
2382
|
+
return scheduler.getScheduler().registerGlobal("after", id, callback);
|
|
3228
2383
|
}
|
|
3229
2384
|
function addTail(callback) {
|
|
3230
2385
|
notifyDepreciated({
|
|
@@ -3232,13 +2387,13 @@ function addTail(callback) {
|
|
|
3232
2387
|
body: "Use scheduler.onIdle(callback) instead.\naddTail will be removed in a future version.",
|
|
3233
2388
|
link: "https://docs.pmnd.rs/react-three-fiber/api/hooks#useframe"
|
|
3234
2389
|
});
|
|
3235
|
-
return getScheduler().onIdle(callback);
|
|
2390
|
+
return scheduler.getScheduler().onIdle(callback);
|
|
3236
2391
|
}
|
|
3237
2392
|
function invalidate(state, frames = 1, stackFrames = false) {
|
|
3238
|
-
getScheduler().invalidate(frames, stackFrames);
|
|
2393
|
+
scheduler.getScheduler().invalidate(frames, stackFrames);
|
|
3239
2394
|
}
|
|
3240
2395
|
function advance(timestamp) {
|
|
3241
|
-
getScheduler().step(timestamp);
|
|
2396
|
+
scheduler.getScheduler().step(timestamp);
|
|
3242
2397
|
}
|
|
3243
2398
|
|
|
3244
2399
|
const version = "10.0.0-alpha.2";
|
|
@@ -15011,7 +14166,6 @@ function createRoot(canvas) {
|
|
|
15011
14166
|
events,
|
|
15012
14167
|
onCreated: onCreatedCallback,
|
|
15013
14168
|
shadows = false,
|
|
15014
|
-
textureColorSpace = webgpu.SRGBColorSpace,
|
|
15015
14169
|
orthographic = false,
|
|
15016
14170
|
frameloop = "always",
|
|
15017
14171
|
dpr = [1, 2],
|
|
@@ -15026,6 +14180,7 @@ function createRoot(canvas) {
|
|
|
15026
14180
|
_sizeProps,
|
|
15027
14181
|
forceEven
|
|
15028
14182
|
} = props;
|
|
14183
|
+
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || webgpu.SRGBColorSpace;
|
|
15029
14184
|
const state = store.getState();
|
|
15030
14185
|
const defaultGLProps = {
|
|
15031
14186
|
canvas,
|
|
@@ -15081,6 +14236,12 @@ function createRoot(canvas) {
|
|
|
15081
14236
|
} else if (!wantsGL && !state.internal.actualRenderer) {
|
|
15082
14237
|
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, webgpu.WebGPURenderer);
|
|
15083
14238
|
if (!renderer.hasInitialized?.()) {
|
|
14239
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14240
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14241
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14242
|
+
canvas.width = size2.width * pixelRatio;
|
|
14243
|
+
canvas.height = size2.height * pixelRatio;
|
|
14244
|
+
}
|
|
15084
14245
|
await renderer.init();
|
|
15085
14246
|
}
|
|
15086
14247
|
const backend = renderer.backend;
|
|
@@ -15190,7 +14351,7 @@ function createRoot(canvas) {
|
|
|
15190
14351
|
lastConfiguredProps.performance = performance;
|
|
15191
14352
|
}
|
|
15192
14353
|
if (!state.xr) {
|
|
15193
|
-
const handleXRFrame = (timestamp,
|
|
14354
|
+
const handleXRFrame = (timestamp, _frame) => {
|
|
15194
14355
|
const state2 = store.getState();
|
|
15195
14356
|
if (state2.frameloop === "never") return;
|
|
15196
14357
|
advance(timestamp);
|
|
@@ -15226,15 +14387,22 @@ function createRoot(canvas) {
|
|
|
15226
14387
|
const oldType = renderer.shadowMap.type;
|
|
15227
14388
|
renderer.shadowMap.enabled = !!shadows;
|
|
15228
14389
|
if (is.boo(shadows)) {
|
|
15229
|
-
renderer.shadowMap.type = webgpu.
|
|
14390
|
+
renderer.shadowMap.type = webgpu.PCFShadowMap;
|
|
15230
14391
|
} else if (is.str(shadows)) {
|
|
14392
|
+
if (shadows === "soft") {
|
|
14393
|
+
notifyDepreciated({
|
|
14394
|
+
heading: 'shadows="soft" is deprecated',
|
|
14395
|
+
body: "Three has depreciated soft and improved basic PCFShadows, we converted for you.",
|
|
14396
|
+
link: "https://github.com/mrdoob/three.js/wiki/Migration-Guide?utm_source=chatgpt.com#181--182"
|
|
14397
|
+
});
|
|
14398
|
+
}
|
|
15231
14399
|
const types = {
|
|
15232
14400
|
basic: webgpu.BasicShadowMap,
|
|
15233
14401
|
percentage: webgpu.PCFShadowMap,
|
|
15234
|
-
soft: webgpu.
|
|
14402
|
+
soft: webgpu.PCFShadowMap,
|
|
15235
14403
|
variance: webgpu.VSMShadowMap
|
|
15236
14404
|
};
|
|
15237
|
-
renderer.shadowMap.type = types[shadows] ?? webgpu.
|
|
14405
|
+
renderer.shadowMap.type = types[shadows] ?? webgpu.PCFShadowMap;
|
|
15238
14406
|
} else if (is.obj(shadows)) {
|
|
15239
14407
|
Object.assign(renderer.shadowMap, shadows);
|
|
15240
14408
|
}
|
|
@@ -15250,24 +14418,35 @@ function createRoot(canvas) {
|
|
|
15250
14418
|
if (state.textureColorSpace !== textureColorSpace) state.set(() => ({ textureColorSpace }));
|
|
15251
14419
|
lastConfiguredProps.textureColorSpace = textureColorSpace;
|
|
15252
14420
|
}
|
|
14421
|
+
const r3fProps = ["textureColorSpace"];
|
|
14422
|
+
const constructorOnlyProps = ["samples", "antialias", "alpha", "canvas", "powerPreference"];
|
|
14423
|
+
const nonApplyProps = [...r3fProps, ...constructorOnlyProps];
|
|
15253
14424
|
if (glConfig && !is.fun(glConfig) && !isRenderer(glConfig) && !is.equ(glConfig, renderer, shallowLoose)) {
|
|
15254
|
-
|
|
14425
|
+
const glProps = {};
|
|
14426
|
+
for (const key in glConfig) {
|
|
14427
|
+
if (!nonApplyProps.includes(key)) glProps[key] = glConfig[key];
|
|
14428
|
+
}
|
|
14429
|
+
applyProps(renderer, glProps);
|
|
15255
14430
|
}
|
|
15256
14431
|
if (rendererConfig && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && state.renderer) {
|
|
15257
14432
|
const currentRenderer = state.renderer;
|
|
15258
14433
|
if (!is.equ(rendererConfig, currentRenderer, shallowLoose)) {
|
|
15259
|
-
|
|
14434
|
+
const rendererProps = {};
|
|
14435
|
+
for (const key in rendererConfig) {
|
|
14436
|
+
if (!nonApplyProps.includes(key)) rendererProps[key] = rendererConfig[key];
|
|
14437
|
+
}
|
|
14438
|
+
applyProps(currentRenderer, rendererProps);
|
|
15260
14439
|
}
|
|
15261
14440
|
}
|
|
15262
|
-
const scheduler = getScheduler();
|
|
14441
|
+
const scheduler$1 = scheduler.getScheduler();
|
|
15263
14442
|
const rootId = state.internal.rootId;
|
|
15264
14443
|
if (!rootId) {
|
|
15265
|
-
const newRootId = canvasId || scheduler.generateRootId();
|
|
15266
|
-
const unregisterRoot = scheduler.registerRoot(newRootId, {
|
|
14444
|
+
const newRootId = canvasId || scheduler$1.generateRootId();
|
|
14445
|
+
const unregisterRoot = scheduler$1.registerRoot(newRootId, {
|
|
15267
14446
|
getState: () => store.getState(),
|
|
15268
14447
|
onError: (err) => store.getState().setError(err)
|
|
15269
14448
|
});
|
|
15270
|
-
const unregisterCanvasTarget = scheduler.register(
|
|
14449
|
+
const unregisterCanvasTarget = scheduler$1.register(
|
|
15271
14450
|
() => {
|
|
15272
14451
|
const state2 = store.getState();
|
|
15273
14452
|
if (state2.internal.isMultiCanvas && state2.internal.canvasTarget) {
|
|
@@ -15282,7 +14461,19 @@ function createRoot(canvas) {
|
|
|
15282
14461
|
system: true
|
|
15283
14462
|
}
|
|
15284
14463
|
);
|
|
15285
|
-
const
|
|
14464
|
+
const unregisterEventsFlush = scheduler$1.register(
|
|
14465
|
+
() => {
|
|
14466
|
+
const state2 = store.getState();
|
|
14467
|
+
state2.events.flush?.();
|
|
14468
|
+
},
|
|
14469
|
+
{
|
|
14470
|
+
id: `${newRootId}_events`,
|
|
14471
|
+
rootId: newRootId,
|
|
14472
|
+
phase: "input",
|
|
14473
|
+
system: true
|
|
14474
|
+
}
|
|
14475
|
+
);
|
|
14476
|
+
const unregisterFrustum = scheduler$1.register(
|
|
15286
14477
|
() => {
|
|
15287
14478
|
const state2 = store.getState();
|
|
15288
14479
|
if (state2.autoUpdateFrustum && state2.camera) {
|
|
@@ -15292,11 +14483,11 @@ function createRoot(canvas) {
|
|
|
15292
14483
|
{
|
|
15293
14484
|
id: `${newRootId}_frustum`,
|
|
15294
14485
|
rootId: newRootId,
|
|
15295
|
-
|
|
14486
|
+
before: "render",
|
|
15296
14487
|
system: true
|
|
15297
14488
|
}
|
|
15298
14489
|
);
|
|
15299
|
-
const unregisterVisibility = scheduler.register(
|
|
14490
|
+
const unregisterVisibility = scheduler$1.register(
|
|
15300
14491
|
() => {
|
|
15301
14492
|
const state2 = store.getState();
|
|
15302
14493
|
checkVisibility(state2);
|
|
@@ -15304,19 +14495,19 @@ function createRoot(canvas) {
|
|
|
15304
14495
|
{
|
|
15305
14496
|
id: `${newRootId}_visibility`,
|
|
15306
14497
|
rootId: newRootId,
|
|
15307
|
-
|
|
14498
|
+
before: "render",
|
|
15308
14499
|
system: true,
|
|
15309
14500
|
after: `${newRootId}_frustum`
|
|
15310
14501
|
}
|
|
15311
14502
|
);
|
|
15312
|
-
const unregisterRender = scheduler.register(
|
|
14503
|
+
const unregisterRender = scheduler$1.register(
|
|
15313
14504
|
() => {
|
|
15314
14505
|
const state2 = store.getState();
|
|
15315
14506
|
const renderer2 = state2.internal.actualRenderer;
|
|
15316
|
-
const userHandlesRender = scheduler.hasUserJobsInPhase("render", newRootId);
|
|
14507
|
+
const userHandlesRender = scheduler$1.hasUserJobsInPhase("render", newRootId);
|
|
15317
14508
|
if (userHandlesRender || state2.internal.priority) return;
|
|
15318
14509
|
try {
|
|
15319
|
-
if (state2.
|
|
14510
|
+
if (state2.renderPipeline?.render) state2.renderPipeline.render();
|
|
15320
14511
|
else if (renderer2?.render) renderer2.render(state2.scene, state2.camera);
|
|
15321
14512
|
} catch (error) {
|
|
15322
14513
|
state2.setError(error instanceof Error ? error : new Error(String(error)));
|
|
@@ -15341,15 +14532,16 @@ function createRoot(canvas) {
|
|
|
15341
14532
|
unregisterRoot: () => {
|
|
15342
14533
|
unregisterRoot();
|
|
15343
14534
|
unregisterCanvasTarget();
|
|
14535
|
+
unregisterEventsFlush();
|
|
15344
14536
|
unregisterFrustum();
|
|
15345
14537
|
unregisterVisibility();
|
|
15346
14538
|
unregisterRender();
|
|
15347
14539
|
},
|
|
15348
|
-
scheduler
|
|
14540
|
+
scheduler: scheduler$1
|
|
15349
14541
|
}
|
|
15350
14542
|
}));
|
|
15351
14543
|
}
|
|
15352
|
-
scheduler.frameloop = frameloop;
|
|
14544
|
+
scheduler$1.frameloop = frameloop;
|
|
15353
14545
|
onCreated = onCreatedCallback;
|
|
15354
14546
|
configured = true;
|
|
15355
14547
|
resolve();
|
|
@@ -15506,9 +14698,13 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15506
14698
|
const store = traditional.createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
15507
14699
|
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
15508
14700
|
onMutate(previousRoot.getState());
|
|
15509
|
-
previousRoot.subscribe(onMutate);
|
|
15510
14701
|
return store;
|
|
15511
14702
|
}, [previousRoot, container]);
|
|
14703
|
+
useIsomorphicLayoutEffect(() => {
|
|
14704
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14705
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14706
|
+
return unsubscribe;
|
|
14707
|
+
}, [previousRoot, usePortalStore]);
|
|
15512
14708
|
return (
|
|
15513
14709
|
// @ts-ignore, reconciler types are not maintained
|
|
15514
14710
|
/* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: reconciler.createPortal(
|
|
@@ -15522,6 +14718,46 @@ function flushSync(fn) {
|
|
|
15522
14718
|
return reconciler.flushSyncFromReconciler(fn);
|
|
15523
14719
|
}
|
|
15524
14720
|
|
|
14721
|
+
function parseBackground(background) {
|
|
14722
|
+
if (!background) return null;
|
|
14723
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14724
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14725
|
+
return {
|
|
14726
|
+
...rest,
|
|
14727
|
+
preset,
|
|
14728
|
+
files: envMap || files,
|
|
14729
|
+
backgroundFiles: backgroundMap,
|
|
14730
|
+
background: true
|
|
14731
|
+
};
|
|
14732
|
+
}
|
|
14733
|
+
if (typeof background === "number") {
|
|
14734
|
+
return { color: background, background: true };
|
|
14735
|
+
}
|
|
14736
|
+
if (typeof background === "string") {
|
|
14737
|
+
if (background in presetsObj) {
|
|
14738
|
+
return { preset: background, background: true };
|
|
14739
|
+
}
|
|
14740
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14741
|
+
return { files: background, background: true };
|
|
14742
|
+
}
|
|
14743
|
+
return { color: background, background: true };
|
|
14744
|
+
}
|
|
14745
|
+
if (background.isColor) {
|
|
14746
|
+
return { color: background, background: true };
|
|
14747
|
+
}
|
|
14748
|
+
return null;
|
|
14749
|
+
}
|
|
14750
|
+
|
|
14751
|
+
function clearHmrCaches(store) {
|
|
14752
|
+
store.setState((state) => ({
|
|
14753
|
+
nodes: {},
|
|
14754
|
+
uniforms: {},
|
|
14755
|
+
buffers: {},
|
|
14756
|
+
gpuStorage: {},
|
|
14757
|
+
_hmrVersion: state._hmrVersion + 1
|
|
14758
|
+
}));
|
|
14759
|
+
}
|
|
14760
|
+
|
|
15525
14761
|
function CanvasImpl({
|
|
15526
14762
|
ref,
|
|
15527
14763
|
children,
|
|
@@ -15542,6 +14778,8 @@ function CanvasImpl({
|
|
|
15542
14778
|
raycaster,
|
|
15543
14779
|
camera,
|
|
15544
14780
|
scene,
|
|
14781
|
+
autoUpdateFrustum,
|
|
14782
|
+
occlusion,
|
|
15545
14783
|
onPointerMissed,
|
|
15546
14784
|
onDragOverMissed,
|
|
15547
14785
|
onDropMissed,
|
|
@@ -15553,39 +14791,21 @@ function CanvasImpl({
|
|
|
15553
14791
|
forceEven,
|
|
15554
14792
|
...props
|
|
15555
14793
|
}) {
|
|
15556
|
-
const
|
|
15557
|
-
|
|
14794
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14795
|
+
let primaryCanvas;
|
|
14796
|
+
let scheduler;
|
|
14797
|
+
let renderer;
|
|
14798
|
+
if (isRendererConfig) {
|
|
14799
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14800
|
+
primaryCanvas = pc;
|
|
14801
|
+
scheduler = sc;
|
|
14802
|
+
renderer = Object.keys(rest).length > 0 ? rest : rendererProp;
|
|
14803
|
+
} else {
|
|
14804
|
+
renderer = rendererProp;
|
|
14805
|
+
}
|
|
15558
14806
|
React__namespace.useMemo(() => extend(THREE), []);
|
|
15559
14807
|
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]);
|
|
14808
|
+
const backgroundProps = React__namespace.useMemo(() => parseBackground(background), [background]);
|
|
15589
14809
|
const hasInitialSizeRef = React__namespace.useRef(false);
|
|
15590
14810
|
const measureConfig = React__namespace.useMemo(() => {
|
|
15591
14811
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15669,6 +14889,8 @@ function CanvasImpl({
|
|
|
15669
14889
|
performance,
|
|
15670
14890
|
raycaster,
|
|
15671
14891
|
camera,
|
|
14892
|
+
autoUpdateFrustum,
|
|
14893
|
+
occlusion,
|
|
15672
14894
|
size: effectiveSize,
|
|
15673
14895
|
// Store size props for reset functionality
|
|
15674
14896
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
@@ -15728,20 +14950,13 @@ function CanvasImpl({
|
|
|
15728
14950
|
const handleHMR = () => {
|
|
15729
14951
|
queueMicrotask(() => {
|
|
15730
14952
|
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
|
-
}
|
|
14953
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15738
14954
|
});
|
|
15739
14955
|
};
|
|
15740
14956
|
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
14957
|
const hot = undefined;
|
|
15742
14958
|
hot.on("vite:afterUpdate", handleHMR);
|
|
15743
|
-
return () => hot.
|
|
15744
|
-
});
|
|
14959
|
+
return () => hot.off?.("vite:afterUpdate", handleHMR);
|
|
15745
14960
|
}
|
|
15746
14961
|
if (typeof module !== "undefined" && module.hot) {
|
|
15747
14962
|
const hot = module.hot;
|
|
@@ -15764,7 +14979,16 @@ function CanvasImpl({
|
|
|
15764
14979
|
...style
|
|
15765
14980
|
},
|
|
15766
14981
|
...props,
|
|
15767
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
14982
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
14983
|
+
"canvas",
|
|
14984
|
+
{
|
|
14985
|
+
ref: canvasRef,
|
|
14986
|
+
id,
|
|
14987
|
+
className: "r3f-canvas",
|
|
14988
|
+
style: { display: "block", width: "100%", height: "100%" },
|
|
14989
|
+
children: fallback
|
|
14990
|
+
}
|
|
14991
|
+
) })
|
|
15768
14992
|
}
|
|
15769
14993
|
);
|
|
15770
14994
|
}
|
|
@@ -15774,6 +14998,8 @@ function Canvas(props) {
|
|
|
15774
14998
|
|
|
15775
14999
|
extend(THREE);
|
|
15776
15000
|
|
|
15001
|
+
exports.Scheduler = scheduler.Scheduler;
|
|
15002
|
+
exports.getScheduler = scheduler.getScheduler;
|
|
15777
15003
|
exports.Block = Block;
|
|
15778
15004
|
exports.Canvas = Canvas;
|
|
15779
15005
|
exports.Environment = Environment;
|
|
@@ -15789,7 +15015,6 @@ exports.R3F_BUILD_LEGACY = R3F_BUILD_LEGACY;
|
|
|
15789
15015
|
exports.R3F_BUILD_WEBGPU = R3F_BUILD_WEBGPU;
|
|
15790
15016
|
exports.REACT_INTERNAL_PROPS = REACT_INTERNAL_PROPS;
|
|
15791
15017
|
exports.RESERVED_PROPS = RESERVED_PROPS;
|
|
15792
|
-
exports.Scheduler = Scheduler;
|
|
15793
15018
|
exports.Texture = Texture;
|
|
15794
15019
|
exports._roots = _roots;
|
|
15795
15020
|
exports.act = act;
|
|
@@ -15819,7 +15044,6 @@ exports.getInstanceProps = getInstanceProps;
|
|
|
15819
15044
|
exports.getPrimary = getPrimary;
|
|
15820
15045
|
exports.getPrimaryIds = getPrimaryIds;
|
|
15821
15046
|
exports.getRootState = getRootState;
|
|
15822
|
-
exports.getScheduler = getScheduler;
|
|
15823
15047
|
exports.getUuidPrefix = getUuidPrefix;
|
|
15824
15048
|
exports.hasConstructor = hasConstructor;
|
|
15825
15049
|
exports.hasPrimary = hasPrimary;
|