@react-three/fiber 10.0.0-canary.2b511a5 → 10.0.0-canary.5c62eb9
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 +538 -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 +512 -1278
- package/dist/legacy.cjs +532 -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 +506 -1278
- package/dist/webgpu/index.cjs +943 -1328
- 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 +912 -1300
- package/package.json +2 -1
- package/readme.md +2 -2
package/dist/webgpu/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as webgpu from 'three/webgpu';
|
|
2
|
-
import { RenderTarget, CubeReflectionMapping, EquirectangularReflectionMapping, CubeTextureLoader, Scene, WebGLCubeRenderTarget, HalfFloatType, Color, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, SRGBColorSpace, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1, CanvasTarget, Raycaster, OrthographicCamera, PerspectiveCamera,
|
|
2
|
+
import { RenderTarget, CubeReflectionMapping, EquirectangularReflectionMapping, CubeTextureLoader, Scene, WebGLCubeRenderTarget, HalfFloatType, Color, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, SRGBColorSpace, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1, CanvasTarget, Raycaster, OrthographicCamera, PerspectiveCamera, PCFShadowMap, VSMShadowMap, BasicShadowMap, ACESFilmicToneMapping, WebGPURenderer, Vector4, PostProcessing } from 'three/webgpu';
|
|
3
3
|
import { Inspector } from 'three/addons/inspector/Inspector.js';
|
|
4
4
|
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
5
5
|
import * as React from 'react';
|
|
@@ -14,6 +14,8 @@ import { UltraHDRLoader } from 'three/examples/jsm/loaders/UltraHDRLoader.js';
|
|
|
14
14
|
import { GainMapLoader } from '@monogrid/gainmap-js';
|
|
15
15
|
import Tb, { unstable_scheduleCallback, unstable_IdlePriority } from 'scheduler';
|
|
16
16
|
import { createWithEqualityFn } from 'zustand/traditional';
|
|
17
|
+
import { getScheduler } from '@pmndrs/scheduler';
|
|
18
|
+
export { Scheduler, getScheduler } from '@pmndrs/scheduler';
|
|
17
19
|
import { suspend, preload, clear } from 'suspend-react';
|
|
18
20
|
import { dequal } from 'dequal/lite';
|
|
19
21
|
import 'zustand/shallow';
|
|
@@ -149,7 +151,7 @@ function useEnvironment({
|
|
|
149
151
|
useLoader$1.clear(loader, multiFile ? [files] : files);
|
|
150
152
|
}
|
|
151
153
|
renderer.domElement.addEventListener("webglcontextlost", clearGainmapTexture, { once: true });
|
|
152
|
-
}, [files, renderer.domElement]);
|
|
154
|
+
}, [extension, files, loader, multiFile, renderer.domElement]);
|
|
153
155
|
const loaderResult = useLoader$1(
|
|
154
156
|
loader,
|
|
155
157
|
multiFile ? [files] : files,
|
|
@@ -349,7 +351,22 @@ function EnvironmentPortal({
|
|
|
349
351
|
environmentIntensity,
|
|
350
352
|
environmentRotation
|
|
351
353
|
});
|
|
352
|
-
}, [
|
|
354
|
+
}, [
|
|
355
|
+
children,
|
|
356
|
+
virtualScene,
|
|
357
|
+
fbo.texture,
|
|
358
|
+
scene,
|
|
359
|
+
defaultScene,
|
|
360
|
+
background,
|
|
361
|
+
frames,
|
|
362
|
+
gl,
|
|
363
|
+
blur,
|
|
364
|
+
backgroundBlurriness,
|
|
365
|
+
backgroundIntensity,
|
|
366
|
+
backgroundRotation,
|
|
367
|
+
environmentIntensity,
|
|
368
|
+
environmentRotation
|
|
369
|
+
]);
|
|
353
370
|
let count = 1;
|
|
354
371
|
useFrame$1(() => {
|
|
355
372
|
if (frames === Infinity || count < frames) {
|
|
@@ -420,9 +437,9 @@ function Environment(props) {
|
|
|
420
437
|
return props.ground ? /* @__PURE__ */ jsx(EnvironmentGround, { ...props }) : props.map ? /* @__PURE__ */ jsx(EnvironmentMap, { ...props }) : props.children ? /* @__PURE__ */ jsx(EnvironmentPortal, { ...props }) : /* @__PURE__ */ jsx(EnvironmentCube, { ...props });
|
|
421
438
|
}
|
|
422
439
|
|
|
423
|
-
var __defProp$
|
|
424
|
-
var __defNormalProp$
|
|
425
|
-
var __publicField$
|
|
440
|
+
var __defProp$1 = Object.defineProperty;
|
|
441
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
442
|
+
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
426
443
|
const act = React["act"];
|
|
427
444
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React.useLayoutEffect : React.useEffect;
|
|
428
445
|
function useMutableCallback(fn) {
|
|
@@ -454,7 +471,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
454
471
|
return _a = class extends React.Component {
|
|
455
472
|
constructor() {
|
|
456
473
|
super(...arguments);
|
|
457
|
-
__publicField$
|
|
474
|
+
__publicField$1(this, "state", { error: false });
|
|
458
475
|
}
|
|
459
476
|
componentDidCatch(err) {
|
|
460
477
|
this.props.set(err);
|
|
@@ -462,7 +479,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
462
479
|
render() {
|
|
463
480
|
return this.state.error ? null : this.props.children;
|
|
464
481
|
}
|
|
465
|
-
}, __publicField$
|
|
482
|
+
}, __publicField$1(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
466
483
|
})();
|
|
467
484
|
|
|
468
485
|
const is = {
|
|
@@ -985,6 +1002,9 @@ function applyProps(object, props) {
|
|
|
985
1002
|
else target.set(value);
|
|
986
1003
|
} else {
|
|
987
1004
|
root[key] = value;
|
|
1005
|
+
if (key.endsWith("Node") && root.isMaterial) {
|
|
1006
|
+
root.needsUpdate = true;
|
|
1007
|
+
}
|
|
988
1008
|
if (rootState && rootState.renderer?.outputColorSpace === SRGBColorSpace && colorMaps.includes(key) && isTexture(value) && root[key]?.isTexture && // sRGB textures must be RGBA8 since r137 https://github.com/mrdoob/three.js/pull/23129
|
|
989
1009
|
root[key].format === RGBAFormat && root[key].type === UnsignedByteType) {
|
|
990
1010
|
root[key].colorSpace = rootState.textureColorSpace;
|
|
@@ -1018,38 +1038,60 @@ function applyProps(object, props) {
|
|
|
1018
1038
|
return object;
|
|
1019
1039
|
}
|
|
1020
1040
|
|
|
1041
|
+
const DEFAULT_POINTER_ID = 0;
|
|
1042
|
+
const XR_POINTER_ID_START = 1e3;
|
|
1043
|
+
function getPointerState(internal, pointerId) {
|
|
1044
|
+
let state = internal.pointerMap.get(pointerId);
|
|
1045
|
+
if (!state) {
|
|
1046
|
+
state = {
|
|
1047
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
1048
|
+
captured: /* @__PURE__ */ new Map(),
|
|
1049
|
+
initialClick: [0, 0],
|
|
1050
|
+
initialHits: []
|
|
1051
|
+
};
|
|
1052
|
+
internal.pointerMap.set(pointerId, state);
|
|
1053
|
+
}
|
|
1054
|
+
return state;
|
|
1055
|
+
}
|
|
1056
|
+
function getPointerId(event) {
|
|
1057
|
+
return "pointerId" in event ? event.pointerId : DEFAULT_POINTER_ID;
|
|
1058
|
+
}
|
|
1021
1059
|
function makeId(event) {
|
|
1022
1060
|
return (event.eventObject || event.object).uuid + "/" + event.index + event.instanceId;
|
|
1023
1061
|
}
|
|
1024
|
-
function releaseInternalPointerCapture(
|
|
1025
|
-
const
|
|
1062
|
+
function releaseInternalPointerCapture(internal, obj, pointerId) {
|
|
1063
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1064
|
+
if (!pointerState) return;
|
|
1065
|
+
const captureData = pointerState.captured.get(obj);
|
|
1026
1066
|
if (captureData) {
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
capturedMap.delete(pointerId);
|
|
1030
|
-
captureData.target.releasePointerCapture(pointerId);
|
|
1031
|
-
}
|
|
1067
|
+
pointerState.captured.delete(obj);
|
|
1068
|
+
captureData.target.releasePointerCapture(pointerId);
|
|
1032
1069
|
}
|
|
1033
1070
|
}
|
|
1034
1071
|
function removeInteractivity(store, object) {
|
|
1035
1072
|
const { internal } = store.getState();
|
|
1036
1073
|
internal.interaction = internal.interaction.filter((o) => o !== object);
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1074
|
+
for (const [pointerId, pointerState] of internal.pointerMap) {
|
|
1075
|
+
pointerState.initialHits = pointerState.initialHits.filter((o) => o !== object);
|
|
1076
|
+
pointerState.hovered.forEach((value, key) => {
|
|
1077
|
+
if (value.eventObject === object || value.object === object) {
|
|
1078
|
+
pointerState.hovered.delete(key);
|
|
1079
|
+
}
|
|
1080
|
+
});
|
|
1081
|
+
if (pointerState.captured.has(object)) {
|
|
1082
|
+
releaseInternalPointerCapture(internal, object, pointerId);
|
|
1041
1083
|
}
|
|
1042
|
-
}
|
|
1043
|
-
internal.capturedMap.forEach((captures, pointerId) => {
|
|
1044
|
-
releaseInternalPointerCapture(internal.capturedMap, object, captures, pointerId);
|
|
1045
|
-
});
|
|
1084
|
+
}
|
|
1046
1085
|
unregisterVisibility(store, object);
|
|
1047
1086
|
}
|
|
1048
1087
|
function createEvents(store) {
|
|
1049
|
-
function calculateDistance(event) {
|
|
1088
|
+
function calculateDistance(event, pointerId) {
|
|
1050
1089
|
const { internal } = store.getState();
|
|
1051
|
-
const
|
|
1052
|
-
|
|
1090
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1091
|
+
if (!pointerState) return 0;
|
|
1092
|
+
const [initialX, initialY] = pointerState.initialClick;
|
|
1093
|
+
const dx = event.offsetX - initialX;
|
|
1094
|
+
const dy = event.offsetY - initialY;
|
|
1053
1095
|
return Math.round(Math.sqrt(dx * dx + dy * dy));
|
|
1054
1096
|
}
|
|
1055
1097
|
function filterPointerEvents(objects) {
|
|
@@ -1085,6 +1127,15 @@ function createEvents(store) {
|
|
|
1085
1127
|
return state2.raycaster.camera ? state2.raycaster.intersectObject(obj, true) : [];
|
|
1086
1128
|
}
|
|
1087
1129
|
let hits = eventsObjects.flatMap(handleRaycast).sort((a, b) => {
|
|
1130
|
+
const aInteractivePriority = a.object.userData?.interactivePriority;
|
|
1131
|
+
const bInteractivePriority = b.object.userData?.interactivePriority;
|
|
1132
|
+
if (aInteractivePriority !== void 0 || bInteractivePriority !== void 0) {
|
|
1133
|
+
if (aInteractivePriority !== void 0 && bInteractivePriority === void 0) return -1;
|
|
1134
|
+
if (bInteractivePriority !== void 0 && aInteractivePriority === void 0) return 1;
|
|
1135
|
+
if (aInteractivePriority !== bInteractivePriority) {
|
|
1136
|
+
return (bInteractivePriority ?? 0) - (aInteractivePriority ?? 0);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1088
1139
|
const aState = getRootState(a.object);
|
|
1089
1140
|
const bState = getRootState(b.object);
|
|
1090
1141
|
const aPriority = aState?.events?.priority ?? 1;
|
|
@@ -1106,9 +1157,13 @@ function createEvents(store) {
|
|
|
1106
1157
|
eventObject = eventObject.parent;
|
|
1107
1158
|
}
|
|
1108
1159
|
}
|
|
1109
|
-
if ("pointerId" in event
|
|
1110
|
-
|
|
1111
|
-
|
|
1160
|
+
if ("pointerId" in event) {
|
|
1161
|
+
const pointerId = event.pointerId;
|
|
1162
|
+
const pointerState = state.internal.pointerMap.get(pointerId);
|
|
1163
|
+
if (pointerState?.captured.size) {
|
|
1164
|
+
for (const captureData of pointerState.captured.values()) {
|
|
1165
|
+
if (!duplicates.has(makeId(captureData.intersection))) intersections.push(captureData.intersection);
|
|
1166
|
+
}
|
|
1112
1167
|
}
|
|
1113
1168
|
}
|
|
1114
1169
|
return intersections;
|
|
@@ -1121,27 +1176,25 @@ function createEvents(store) {
|
|
|
1121
1176
|
if (state) {
|
|
1122
1177
|
const { raycaster, pointer, camera, internal } = state;
|
|
1123
1178
|
const unprojectedPoint = new Vector3(pointer.x, pointer.y, 0).unproject(camera);
|
|
1124
|
-
const hasPointerCapture = (id) =>
|
|
1179
|
+
const hasPointerCapture = (id) => {
|
|
1180
|
+
const pointerState = internal.pointerMap.get(id);
|
|
1181
|
+
return pointerState?.captured.has(hit.eventObject) ?? false;
|
|
1182
|
+
};
|
|
1125
1183
|
const setPointerCapture = (id) => {
|
|
1126
1184
|
const captureData = { intersection: hit, target: event.target };
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
} else {
|
|
1130
|
-
internal.capturedMap.set(id, /* @__PURE__ */ new Map([[hit.eventObject, captureData]]));
|
|
1131
|
-
}
|
|
1185
|
+
const pointerState = getPointerState(internal, id);
|
|
1186
|
+
pointerState.captured.set(hit.eventObject, captureData);
|
|
1132
1187
|
event.target.setPointerCapture(id);
|
|
1133
1188
|
};
|
|
1134
1189
|
const releasePointerCapture = (id) => {
|
|
1135
|
-
|
|
1136
|
-
if (captures) {
|
|
1137
|
-
releaseInternalPointerCapture(internal.capturedMap, hit.eventObject, captures, id);
|
|
1138
|
-
}
|
|
1190
|
+
releaseInternalPointerCapture(internal, hit.eventObject, id);
|
|
1139
1191
|
};
|
|
1140
1192
|
const extractEventProps = {};
|
|
1141
1193
|
for (const prop in event) {
|
|
1142
1194
|
const property = event[prop];
|
|
1143
1195
|
if (typeof property !== "function") extractEventProps[prop] = property;
|
|
1144
1196
|
}
|
|
1197
|
+
const eventPointerId = "pointerId" in event ? event.pointerId : void 0;
|
|
1145
1198
|
const raycastEvent = {
|
|
1146
1199
|
...hit,
|
|
1147
1200
|
...extractEventProps,
|
|
@@ -1152,18 +1205,19 @@ function createEvents(store) {
|
|
|
1152
1205
|
unprojectedPoint,
|
|
1153
1206
|
ray: raycaster.ray,
|
|
1154
1207
|
camera,
|
|
1208
|
+
pointerId: eventPointerId,
|
|
1155
1209
|
// Hijack stopPropagation, which just sets a flag
|
|
1156
1210
|
stopPropagation() {
|
|
1157
|
-
const
|
|
1211
|
+
const pointerState = eventPointerId !== void 0 ? internal.pointerMap.get(eventPointerId) : void 0;
|
|
1158
1212
|
if (
|
|
1159
1213
|
// ...if this pointer hasn't been captured
|
|
1160
|
-
!
|
|
1161
|
-
|
|
1214
|
+
!pointerState?.captured.size || // ... or if the hit object is capturing the pointer
|
|
1215
|
+
pointerState.captured.has(hit.eventObject)
|
|
1162
1216
|
) {
|
|
1163
1217
|
raycastEvent.stopped = localState.stopped = true;
|
|
1164
|
-
if (
|
|
1218
|
+
if (pointerState?.hovered.size && Array.from(pointerState.hovered.values()).find((i) => i.eventObject === hit.eventObject)) {
|
|
1165
1219
|
const higher = intersections.slice(0, intersections.indexOf(hit));
|
|
1166
|
-
cancelPointer([...higher, hit]);
|
|
1220
|
+
cancelPointer([...higher, hit], eventPointerId);
|
|
1167
1221
|
}
|
|
1168
1222
|
}
|
|
1169
1223
|
},
|
|
@@ -1179,15 +1233,18 @@ function createEvents(store) {
|
|
|
1179
1233
|
}
|
|
1180
1234
|
return intersections;
|
|
1181
1235
|
}
|
|
1182
|
-
function cancelPointer(intersections) {
|
|
1236
|
+
function cancelPointer(intersections, pointerId) {
|
|
1183
1237
|
const { internal } = store.getState();
|
|
1184
|
-
|
|
1238
|
+
const pid = pointerId ?? DEFAULT_POINTER_ID;
|
|
1239
|
+
const pointerState = internal.pointerMap.get(pid);
|
|
1240
|
+
if (!pointerState) return;
|
|
1241
|
+
for (const [hoveredId, hoveredObj] of pointerState.hovered) {
|
|
1185
1242
|
if (!intersections.length || !intersections.find(
|
|
1186
1243
|
(hit) => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId
|
|
1187
1244
|
)) {
|
|
1188
1245
|
const eventObject = hoveredObj.eventObject;
|
|
1189
1246
|
const instance = eventObject.__r3f;
|
|
1190
|
-
|
|
1247
|
+
pointerState.hovered.delete(hoveredId);
|
|
1191
1248
|
if (instance?.eventCount) {
|
|
1192
1249
|
const handlers = instance.handlers;
|
|
1193
1250
|
const data = { ...hoveredObj, intersections };
|
|
@@ -1216,41 +1273,118 @@ function createEvents(store) {
|
|
|
1216
1273
|
instance?.handlers.onDropMissed?.(event);
|
|
1217
1274
|
}
|
|
1218
1275
|
}
|
|
1276
|
+
function cleanupPointer(pointerId) {
|
|
1277
|
+
const { internal } = store.getState();
|
|
1278
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1279
|
+
if (pointerState) {
|
|
1280
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1281
|
+
const eventObject = hoveredObj.eventObject;
|
|
1282
|
+
const instance = eventObject.__r3f;
|
|
1283
|
+
if (instance?.eventCount) {
|
|
1284
|
+
const handlers = instance.handlers;
|
|
1285
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1286
|
+
handlers.onPointerOut?.(data);
|
|
1287
|
+
handlers.onPointerLeave?.(data);
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
internal.pointerMap.delete(pointerId);
|
|
1291
|
+
}
|
|
1292
|
+
internal.pointerDirty.delete(pointerId);
|
|
1293
|
+
}
|
|
1294
|
+
function processDeferredPointer(event, pointerId) {
|
|
1295
|
+
const state = store.getState();
|
|
1296
|
+
const { internal } = state;
|
|
1297
|
+
if (!state.events.enabled) return;
|
|
1298
|
+
const filter = filterPointerEvents;
|
|
1299
|
+
const hits = intersect(event, filter);
|
|
1300
|
+
cancelPointer(hits, pointerId);
|
|
1301
|
+
function onIntersect(data) {
|
|
1302
|
+
const eventObject = data.eventObject;
|
|
1303
|
+
const instance = eventObject.__r3f;
|
|
1304
|
+
if (!instance?.eventCount) return;
|
|
1305
|
+
const handlers = instance.handlers;
|
|
1306
|
+
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1307
|
+
const id = makeId(data);
|
|
1308
|
+
const pointerState = getPointerState(internal, pointerId);
|
|
1309
|
+
const hoveredItem = pointerState.hovered.get(id);
|
|
1310
|
+
if (!hoveredItem) {
|
|
1311
|
+
pointerState.hovered.set(id, data);
|
|
1312
|
+
handlers.onPointerOver?.(data);
|
|
1313
|
+
handlers.onPointerEnter?.(data);
|
|
1314
|
+
} else if (hoveredItem.stopped) {
|
|
1315
|
+
data.stopPropagation();
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
handlers.onPointerMove?.(data);
|
|
1319
|
+
}
|
|
1320
|
+
handleIntersects(hits, event, 0, onIntersect);
|
|
1321
|
+
}
|
|
1219
1322
|
function handlePointer(name) {
|
|
1220
1323
|
switch (name) {
|
|
1221
1324
|
case "onPointerLeave":
|
|
1222
|
-
case "onPointerCancel":
|
|
1223
1325
|
case "onDragLeave":
|
|
1224
1326
|
return () => cancelPointer([]);
|
|
1327
|
+
// Global cancel of these events
|
|
1328
|
+
case "onPointerCancel":
|
|
1329
|
+
return (event) => {
|
|
1330
|
+
const pointerId = getPointerId(event);
|
|
1331
|
+
cleanupPointer(pointerId);
|
|
1332
|
+
};
|
|
1225
1333
|
case "onLostPointerCapture":
|
|
1226
1334
|
return (event) => {
|
|
1227
1335
|
const { internal } = store.getState();
|
|
1228
|
-
|
|
1336
|
+
const pointerId = getPointerId(event);
|
|
1337
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1338
|
+
if (pointerState?.captured.size) {
|
|
1229
1339
|
requestAnimationFrame(() => {
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1340
|
+
const pointerState2 = internal.pointerMap.get(pointerId);
|
|
1341
|
+
if (pointerState2?.captured.size) {
|
|
1342
|
+
pointerState2.captured.clear();
|
|
1233
1343
|
}
|
|
1344
|
+
cancelPointer([], pointerId);
|
|
1234
1345
|
});
|
|
1235
1346
|
}
|
|
1236
1347
|
};
|
|
1237
1348
|
}
|
|
1238
1349
|
return function handleEvent(event) {
|
|
1239
1350
|
const state = store.getState();
|
|
1240
|
-
const { onPointerMissed, onDragOverMissed, onDropMissed, internal } = state;
|
|
1351
|
+
const { onPointerMissed, onDragOverMissed, onDropMissed, internal, events } = state;
|
|
1352
|
+
const pointerId = getPointerId(event);
|
|
1241
1353
|
internal.lastEvent.current = event;
|
|
1242
|
-
if (!
|
|
1354
|
+
if (!events.enabled) return;
|
|
1243
1355
|
const isPointerMove = name === "onPointerMove";
|
|
1244
1356
|
const isDragOver = name === "onDragOver";
|
|
1245
1357
|
const isDrop = name === "onDrop";
|
|
1246
1358
|
const isClickEvent = name === "onClick" || name === "onContextMenu" || name === "onDoubleClick";
|
|
1359
|
+
const isPointerDown = name === "onPointerDown";
|
|
1360
|
+
const isPointerUp = name === "onPointerUp";
|
|
1361
|
+
const isWheel = name === "onWheel";
|
|
1362
|
+
const canDeferRaycasts = events.frameTimedRaycasts && state.frameloop === "always";
|
|
1363
|
+
if (isPointerMove && canDeferRaycasts) {
|
|
1364
|
+
events.compute?.(event, state);
|
|
1365
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
if (isWheel && canDeferRaycasts && !events.alwaysFireOnScroll) {
|
|
1369
|
+
events.compute?.(event, state);
|
|
1370
|
+
internal.pointerDirty.set(pointerId, event);
|
|
1371
|
+
return;
|
|
1372
|
+
}
|
|
1373
|
+
if ((isClickEvent || isPointerDown || isPointerUp) && internal.pointerDirty.has(pointerId)) {
|
|
1374
|
+
const deferredEvent = internal.pointerDirty.get(pointerId);
|
|
1375
|
+
internal.pointerDirty.delete(pointerId);
|
|
1376
|
+
processDeferredPointer(deferredEvent, pointerId);
|
|
1377
|
+
}
|
|
1247
1378
|
const filter = isPointerMove || isDragOver || isDrop ? filterPointerEvents : void 0;
|
|
1248
1379
|
const hits = intersect(event, filter);
|
|
1249
|
-
const delta = isClickEvent ? calculateDistance(event) : 0;
|
|
1250
|
-
if (
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1380
|
+
const delta = isClickEvent ? calculateDistance(event, pointerId) : 0;
|
|
1381
|
+
if (isPointerDown) {
|
|
1382
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1383
|
+
pointerState2.initialClick = [event.offsetX, event.offsetY];
|
|
1384
|
+
pointerState2.initialHits = hits.map((hit) => hit.eventObject);
|
|
1385
|
+
}
|
|
1386
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1387
|
+
const initialHits = pointerState?.initialHits ?? [];
|
|
1254
1388
|
if (isClickEvent && !hits.length) {
|
|
1255
1389
|
if (delta <= 2) {
|
|
1256
1390
|
pointerMissed(event, internal.interaction);
|
|
@@ -1265,7 +1399,9 @@ function createEvents(store) {
|
|
|
1265
1399
|
dropMissed(event, internal.interaction);
|
|
1266
1400
|
if (onDropMissed) onDropMissed(event);
|
|
1267
1401
|
}
|
|
1268
|
-
if (isPointerMove || isDragOver)
|
|
1402
|
+
if (isPointerMove || isDragOver) {
|
|
1403
|
+
cancelPointer(hits, pointerId);
|
|
1404
|
+
}
|
|
1269
1405
|
function onIntersect(data) {
|
|
1270
1406
|
const eventObject = data.eventObject;
|
|
1271
1407
|
const instance = eventObject.__r3f;
|
|
@@ -1274,9 +1410,10 @@ function createEvents(store) {
|
|
|
1274
1410
|
if (isPointerMove) {
|
|
1275
1411
|
if (handlers.onPointerOver || handlers.onPointerEnter || handlers.onPointerOut || handlers.onPointerLeave) {
|
|
1276
1412
|
const id = makeId(data);
|
|
1277
|
-
const
|
|
1413
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1414
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1278
1415
|
if (!hoveredItem) {
|
|
1279
|
-
|
|
1416
|
+
pointerState2.hovered.set(id, data);
|
|
1280
1417
|
handlers.onPointerOver?.(data);
|
|
1281
1418
|
handlers.onPointerEnter?.(data);
|
|
1282
1419
|
} else if (hoveredItem.stopped) {
|
|
@@ -1286,9 +1423,10 @@ function createEvents(store) {
|
|
|
1286
1423
|
handlers.onPointerMove?.(data);
|
|
1287
1424
|
} else if (isDragOver) {
|
|
1288
1425
|
const id = makeId(data);
|
|
1289
|
-
const
|
|
1426
|
+
const pointerState2 = getPointerState(internal, pointerId);
|
|
1427
|
+
const hoveredItem = pointerState2.hovered.get(id);
|
|
1290
1428
|
if (!hoveredItem) {
|
|
1291
|
-
|
|
1429
|
+
pointerState2.hovered.set(id, data);
|
|
1292
1430
|
handlers.onDragOverEnter?.(data);
|
|
1293
1431
|
} else if (hoveredItem.stopped) {
|
|
1294
1432
|
data.stopPropagation();
|
|
@@ -1299,18 +1437,18 @@ function createEvents(store) {
|
|
|
1299
1437
|
} else {
|
|
1300
1438
|
const handler = handlers[name];
|
|
1301
1439
|
if (handler) {
|
|
1302
|
-
if (!isClickEvent ||
|
|
1440
|
+
if (!isClickEvent || initialHits.includes(eventObject)) {
|
|
1303
1441
|
pointerMissed(
|
|
1304
1442
|
event,
|
|
1305
|
-
internal.interaction.filter((object) => !
|
|
1443
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1306
1444
|
);
|
|
1307
1445
|
handler(data);
|
|
1308
1446
|
}
|
|
1309
1447
|
} else {
|
|
1310
|
-
if (isClickEvent &&
|
|
1448
|
+
if (isClickEvent && initialHits.includes(eventObject)) {
|
|
1311
1449
|
pointerMissed(
|
|
1312
1450
|
event,
|
|
1313
|
-
internal.interaction.filter((object) => !
|
|
1451
|
+
internal.interaction.filter((object) => !initialHits.includes(object))
|
|
1314
1452
|
);
|
|
1315
1453
|
}
|
|
1316
1454
|
}
|
|
@@ -1319,7 +1457,15 @@ function createEvents(store) {
|
|
|
1319
1457
|
handleIntersects(hits, event, delta, onIntersect);
|
|
1320
1458
|
};
|
|
1321
1459
|
}
|
|
1322
|
-
|
|
1460
|
+
function flushDeferredPointers() {
|
|
1461
|
+
const { internal, events } = store.getState();
|
|
1462
|
+
if (!events.frameTimedRaycasts) return;
|
|
1463
|
+
for (const [pointerId, event] of internal.pointerDirty) {
|
|
1464
|
+
processDeferredPointer(event, pointerId);
|
|
1465
|
+
}
|
|
1466
|
+
internal.pointerDirty.clear();
|
|
1467
|
+
}
|
|
1468
|
+
return { handlePointer, flushDeferredPointers, processDeferredPointer };
|
|
1323
1469
|
}
|
|
1324
1470
|
const DOM_EVENTS = {
|
|
1325
1471
|
onClick: ["click", false],
|
|
@@ -1338,10 +1484,15 @@ const DOM_EVENTS = {
|
|
|
1338
1484
|
onLostPointerCapture: ["lostpointercapture", true]
|
|
1339
1485
|
};
|
|
1340
1486
|
function createPointerEvents(store) {
|
|
1341
|
-
const { handlePointer } = createEvents(store);
|
|
1487
|
+
const { handlePointer, flushDeferredPointers, processDeferredPointer } = createEvents(store);
|
|
1488
|
+
let nextXRPointerId = XR_POINTER_ID_START;
|
|
1489
|
+
const xrPointers = /* @__PURE__ */ new Map();
|
|
1342
1490
|
return {
|
|
1343
1491
|
priority: 1,
|
|
1344
1492
|
enabled: true,
|
|
1493
|
+
frameTimedRaycasts: true,
|
|
1494
|
+
alwaysFireOnScroll: true,
|
|
1495
|
+
updateOnFrame: false,
|
|
1345
1496
|
compute(event, state) {
|
|
1346
1497
|
state.pointer.set(event.offsetX / state.size.width * 2 - 1, -(event.offsetY / state.size.height) * 2 + 1);
|
|
1347
1498
|
state.raycaster.setFromCamera(state.pointer, state.camera);
|
|
@@ -1351,11 +1502,33 @@ function createPointerEvents(store) {
|
|
|
1351
1502
|
(acc, key) => ({ ...acc, [key]: handlePointer(key) }),
|
|
1352
1503
|
{}
|
|
1353
1504
|
),
|
|
1354
|
-
update: () => {
|
|
1505
|
+
update: (pointerId) => {
|
|
1506
|
+
const { events, internal } = store.getState();
|
|
1507
|
+
if (!events.handlers) return;
|
|
1508
|
+
if (pointerId !== void 0) {
|
|
1509
|
+
const event = internal.pointerDirty.get(pointerId);
|
|
1510
|
+
if (event) {
|
|
1511
|
+
internal.pointerDirty.delete(pointerId);
|
|
1512
|
+
processDeferredPointer(event, pointerId);
|
|
1513
|
+
} else if (internal.lastEvent?.current) {
|
|
1514
|
+
processDeferredPointer(internal.lastEvent.current, pointerId);
|
|
1515
|
+
}
|
|
1516
|
+
} else {
|
|
1517
|
+
flushDeferredPointers();
|
|
1518
|
+
if (internal.lastEvent?.current) {
|
|
1519
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
},
|
|
1523
|
+
flush: () => {
|
|
1355
1524
|
const { events, internal } = store.getState();
|
|
1356
|
-
|
|
1525
|
+
flushDeferredPointers();
|
|
1526
|
+
if (events.updateOnFrame && internal.lastEvent?.current && events.handlers) {
|
|
1527
|
+
events.handlers.onPointerMove(internal.lastEvent.current);
|
|
1528
|
+
}
|
|
1357
1529
|
},
|
|
1358
1530
|
connect: (target) => {
|
|
1531
|
+
if (!target) return;
|
|
1359
1532
|
const { set, events } = store.getState();
|
|
1360
1533
|
events.disconnect?.();
|
|
1361
1534
|
set((state) => ({ events: { ...state.events, connected: target } }));
|
|
@@ -1379,6 +1552,32 @@ function createPointerEvents(store) {
|
|
|
1379
1552
|
}
|
|
1380
1553
|
set((state) => ({ events: { ...state.events, connected: void 0 } }));
|
|
1381
1554
|
}
|
|
1555
|
+
},
|
|
1556
|
+
registerPointer: (config) => {
|
|
1557
|
+
const pointerId = nextXRPointerId++;
|
|
1558
|
+
xrPointers.set(pointerId, config);
|
|
1559
|
+
const { internal } = store.getState();
|
|
1560
|
+
getPointerState(internal, pointerId);
|
|
1561
|
+
return pointerId;
|
|
1562
|
+
},
|
|
1563
|
+
unregisterPointer: (pointerId) => {
|
|
1564
|
+
xrPointers.delete(pointerId);
|
|
1565
|
+
const { internal } = store.getState();
|
|
1566
|
+
const pointerState = internal.pointerMap.get(pointerId);
|
|
1567
|
+
if (pointerState) {
|
|
1568
|
+
for (const [, hoveredObj] of pointerState.hovered) {
|
|
1569
|
+
const eventObject = hoveredObj.eventObject;
|
|
1570
|
+
const instance = eventObject.__r3f;
|
|
1571
|
+
if (instance?.eventCount) {
|
|
1572
|
+
const handlers = instance.handlers;
|
|
1573
|
+
const data = { ...hoveredObj, intersections: [] };
|
|
1574
|
+
handlers.onPointerOut?.(data);
|
|
1575
|
+
handlers.onPointerLeave?.(data);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
internal.pointerMap.delete(pointerId);
|
|
1579
|
+
}
|
|
1580
|
+
internal.pointerDirty.delete(pointerId);
|
|
1382
1581
|
}
|
|
1383
1582
|
};
|
|
1384
1583
|
}
|
|
@@ -1440,1036 +1639,12 @@ function notifyAlpha({ message, link }) {
|
|
|
1440
1639
|
}
|
|
1441
1640
|
}
|
|
1442
1641
|
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
/** Ordered list of phase nodes */
|
|
1450
|
-
__publicField$2(this, "phases", []);
|
|
1451
|
-
/** Quick lookup by name */
|
|
1452
|
-
__publicField$2(this, "phaseMap", /* @__PURE__ */ new Map());
|
|
1453
|
-
/** Cached ordered names (invalidated on changes) */
|
|
1454
|
-
__publicField$2(this, "orderedNamesCache", null);
|
|
1455
|
-
this.initializeDefaultPhases();
|
|
1456
|
-
}
|
|
1457
|
-
//* Initialization --------------------------------
|
|
1458
|
-
initializeDefaultPhases() {
|
|
1459
|
-
for (const name of DEFAULT_PHASES) {
|
|
1460
|
-
const node = { name, isAutoGenerated: false };
|
|
1461
|
-
this.phases.push(node);
|
|
1462
|
-
this.phaseMap.set(name, node);
|
|
1463
|
-
}
|
|
1464
|
-
this.invalidateCache();
|
|
1465
|
-
}
|
|
1466
|
-
//* Public API --------------------------------
|
|
1467
|
-
/**
|
|
1468
|
-
* Add a named phase to the graph
|
|
1469
|
-
* @param name - Phase name (must be unique)
|
|
1470
|
-
* @param options - Position options (before or after another phase)
|
|
1471
|
-
*/
|
|
1472
|
-
addPhase(name, options = {}) {
|
|
1473
|
-
if (this.phaseMap.has(name)) {
|
|
1474
|
-
console.warn(`[useFrame] Phase "${name}" already exists`);
|
|
1475
|
-
return;
|
|
1476
|
-
}
|
|
1477
|
-
const { before, after } = options;
|
|
1478
|
-
const node = { name, isAutoGenerated: false };
|
|
1479
|
-
let insertIndex = this.phases.length;
|
|
1480
|
-
const targetIndex = this.getPhaseIndex(before ?? after);
|
|
1481
|
-
if (targetIndex !== -1) {
|
|
1482
|
-
insertIndex = before ? targetIndex : targetIndex + 1;
|
|
1483
|
-
} else {
|
|
1484
|
-
const constraintType = before ? "before" : "after";
|
|
1485
|
-
console.warn(`[useFrame] Phase "${before ?? after}" not found for '${constraintType}' constraint`);
|
|
1486
|
-
}
|
|
1487
|
-
this.phases.splice(insertIndex, 0, node);
|
|
1488
|
-
this.phaseMap.set(name, node);
|
|
1489
|
-
this.invalidateCache();
|
|
1490
|
-
}
|
|
1491
|
-
/**
|
|
1492
|
-
* Get ordered list of phase names
|
|
1493
|
-
*/
|
|
1494
|
-
getOrderedPhases() {
|
|
1495
|
-
if (this.orderedNamesCache === null) this.orderedNamesCache = this.phases.map((p) => p.name);
|
|
1496
|
-
return this.orderedNamesCache;
|
|
1497
|
-
}
|
|
1498
|
-
/**
|
|
1499
|
-
* Check if a phase exists
|
|
1500
|
-
*/
|
|
1501
|
-
hasPhase(name) {
|
|
1502
|
-
return this.phaseMap.has(name);
|
|
1503
|
-
}
|
|
1504
|
-
/**
|
|
1505
|
-
* Get the index of a phase (-1 if not found)
|
|
1506
|
-
*/
|
|
1507
|
-
getPhaseIndex(name) {
|
|
1508
|
-
if (!name) return -1;
|
|
1509
|
-
return this.phases.findIndex((p) => p.name === name);
|
|
1510
|
-
}
|
|
1511
|
-
/**
|
|
1512
|
-
* Ensure a phase exists, creating an auto-generated one if needed.
|
|
1513
|
-
* Used for resolving before/after constraints.
|
|
1514
|
-
*
|
|
1515
|
-
* @param name - The phase name to ensure exists
|
|
1516
|
-
* @returns The phase name (may be auto-generated like 'before:render')
|
|
1517
|
-
*/
|
|
1518
|
-
ensurePhase(name) {
|
|
1519
|
-
if (this.phaseMap.has(name)) return name;
|
|
1520
|
-
const node = { name, isAutoGenerated: true };
|
|
1521
|
-
this.phases.push(node);
|
|
1522
|
-
this.phaseMap.set(name, node);
|
|
1523
|
-
this.invalidateCache();
|
|
1524
|
-
return name;
|
|
1525
|
-
}
|
|
1526
|
-
/**
|
|
1527
|
-
* Resolve where a job with before/after constraints should go.
|
|
1528
|
-
* Creates auto-generated phases if needed.
|
|
1529
|
-
*
|
|
1530
|
-
* @param before - Phase(s) to run before
|
|
1531
|
-
* @param after - Phase(s) to run after
|
|
1532
|
-
* @returns The resolved phase name
|
|
1533
|
-
*/
|
|
1534
|
-
resolveConstraintPhase(before, after) {
|
|
1535
|
-
const beforeArr = before ? Array.isArray(before) ? before : [before] : [];
|
|
1536
|
-
const afterArr = after ? Array.isArray(after) ? after : [after] : [];
|
|
1537
|
-
if (beforeArr.length > 0) {
|
|
1538
|
-
return this.ensureAutoPhase(beforeArr[0], "before", 0);
|
|
1539
|
-
}
|
|
1540
|
-
if (afterArr.length > 0) {
|
|
1541
|
-
return this.ensureAutoPhase(afterArr[0], "after", 1);
|
|
1542
|
-
}
|
|
1543
|
-
return "update";
|
|
1544
|
-
}
|
|
1545
|
-
/**
|
|
1546
|
-
* Ensure an auto-generated phase exists relative to a target phase.
|
|
1547
|
-
* Creates the phase if it doesn't exist, inserting it at the correct position.
|
|
1548
|
-
*
|
|
1549
|
-
* @param target - The target phase name to position relative to
|
|
1550
|
-
* @param prefix - Prefix for auto-generated phase name ('before' or 'after')
|
|
1551
|
-
* @param offset - Insertion offset (0 for before, 1 for after)
|
|
1552
|
-
* @returns The auto-generated phase name
|
|
1553
|
-
*/
|
|
1554
|
-
ensureAutoPhase(target, prefix, offset) {
|
|
1555
|
-
const autoName = `${prefix}:${target}`;
|
|
1556
|
-
if (this.phaseMap.has(autoName)) return autoName;
|
|
1557
|
-
const node = { name: autoName, isAutoGenerated: true };
|
|
1558
|
-
const targetIndex = this.getPhaseIndex(target);
|
|
1559
|
-
if (targetIndex !== -1) this.phases.splice(targetIndex + offset, 0, node);
|
|
1560
|
-
else this.phases.push(node);
|
|
1561
|
-
this.phaseMap.set(autoName, node);
|
|
1562
|
-
this.invalidateCache();
|
|
1563
|
-
return autoName;
|
|
1564
|
-
}
|
|
1565
|
-
// Internal --------------------------------
|
|
1566
|
-
invalidateCache() {
|
|
1567
|
-
this.orderedNamesCache = null;
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
1570
|
-
|
|
1571
|
-
function rebuildSortedJobs(jobs, phaseGraph) {
|
|
1572
|
-
const orderedPhases = phaseGraph.getOrderedPhases();
|
|
1573
|
-
const buckets = /* @__PURE__ */ new Map();
|
|
1574
|
-
for (const phase of orderedPhases) {
|
|
1575
|
-
buckets.set(phase, []);
|
|
1576
|
-
}
|
|
1577
|
-
for (const job of jobs.values()) {
|
|
1578
|
-
if (!job.enabled) continue;
|
|
1579
|
-
let bucket = buckets.get(job.phase);
|
|
1580
|
-
if (!bucket) {
|
|
1581
|
-
bucket = [];
|
|
1582
|
-
buckets.set(job.phase, bucket);
|
|
1583
|
-
}
|
|
1584
|
-
bucket.push(job);
|
|
1585
|
-
}
|
|
1586
|
-
const sortedBuckets = [];
|
|
1587
|
-
for (const phase of orderedPhases) {
|
|
1588
|
-
const bucket = buckets.get(phase);
|
|
1589
|
-
if (!bucket || bucket.length === 0) continue;
|
|
1590
|
-
bucket.sort((a, b) => {
|
|
1591
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1592
|
-
return a.index - b.index;
|
|
1593
|
-
});
|
|
1594
|
-
sortedBuckets.push(hasCrossJobConstraints(bucket) ? topologicalSort(bucket) : bucket);
|
|
1595
|
-
}
|
|
1596
|
-
for (const [phase, bucket] of buckets) {
|
|
1597
|
-
if (!orderedPhases.includes(phase) && bucket.length > 0) {
|
|
1598
|
-
bucket.sort((a, b) => {
|
|
1599
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1600
|
-
return a.index - b.index;
|
|
1601
|
-
});
|
|
1602
|
-
sortedBuckets.push(bucket);
|
|
1603
|
-
}
|
|
1604
|
-
}
|
|
1605
|
-
return sortedBuckets.flat();
|
|
1606
|
-
}
|
|
1607
|
-
function hasCrossJobConstraints(bucket) {
|
|
1608
|
-
const jobIds = new Set(bucket.map((j) => j.id));
|
|
1609
|
-
for (const job of bucket) {
|
|
1610
|
-
for (const ref of job.before) {
|
|
1611
|
-
if (jobIds.has(ref)) return true;
|
|
1612
|
-
}
|
|
1613
|
-
for (const ref of job.after) {
|
|
1614
|
-
if (jobIds.has(ref)) return true;
|
|
1615
|
-
}
|
|
1616
|
-
}
|
|
1617
|
-
return false;
|
|
1618
|
-
}
|
|
1619
|
-
function topologicalSort(jobs) {
|
|
1620
|
-
const n = jobs.length;
|
|
1621
|
-
if (n <= 1) return jobs;
|
|
1622
|
-
const jobMap = /* @__PURE__ */ new Map();
|
|
1623
|
-
const inDegree = /* @__PURE__ */ new Map();
|
|
1624
|
-
const adjacency = /* @__PURE__ */ new Map();
|
|
1625
|
-
for (const job of jobs) {
|
|
1626
|
-
jobMap.set(job.id, job);
|
|
1627
|
-
inDegree.set(job.id, 0);
|
|
1628
|
-
adjacency.set(job.id, []);
|
|
1629
|
-
}
|
|
1630
|
-
for (const job of jobs) {
|
|
1631
|
-
for (const ref of job.before) {
|
|
1632
|
-
if (jobMap.has(ref)) {
|
|
1633
|
-
adjacency.get(job.id).push(ref);
|
|
1634
|
-
inDegree.set(ref, inDegree.get(ref) + 1);
|
|
1635
|
-
}
|
|
1636
|
-
}
|
|
1637
|
-
for (const ref of job.after) {
|
|
1638
|
-
if (jobMap.has(ref)) {
|
|
1639
|
-
adjacency.get(ref).push(job.id);
|
|
1640
|
-
inDegree.set(job.id, inDegree.get(job.id) + 1);
|
|
1641
|
-
}
|
|
1642
|
-
}
|
|
1643
|
-
}
|
|
1644
|
-
const queue = [];
|
|
1645
|
-
for (const job of jobs) {
|
|
1646
|
-
if (inDegree.get(job.id) === 0) {
|
|
1647
|
-
queue.push(job);
|
|
1648
|
-
}
|
|
1649
|
-
}
|
|
1650
|
-
queue.sort((a, b) => {
|
|
1651
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1652
|
-
return a.index - b.index;
|
|
1653
|
-
});
|
|
1654
|
-
const result = [];
|
|
1655
|
-
while (queue.length > 0) {
|
|
1656
|
-
const job = queue.shift();
|
|
1657
|
-
result.push(job);
|
|
1658
|
-
const neighbors = adjacency.get(job.id) || [];
|
|
1659
|
-
for (const neighborId of neighbors) {
|
|
1660
|
-
const newDegree = inDegree.get(neighborId) - 1;
|
|
1661
|
-
inDegree.set(neighborId, newDegree);
|
|
1662
|
-
if (newDegree === 0) {
|
|
1663
|
-
const neighbor = jobMap.get(neighborId);
|
|
1664
|
-
insertSorted(queue, neighbor);
|
|
1665
|
-
}
|
|
1666
|
-
}
|
|
1667
|
-
}
|
|
1668
|
-
if (result.length !== n) {
|
|
1669
|
-
console.warn("[useFrame] Circular dependency detected in job constraints");
|
|
1670
|
-
const resultIds = new Set(result.map((j) => j.id));
|
|
1671
|
-
for (const job of jobs) {
|
|
1672
|
-
if (!resultIds.has(job.id)) result.push(job);
|
|
1673
|
-
}
|
|
1674
|
-
}
|
|
1675
|
-
return result;
|
|
1676
|
-
}
|
|
1677
|
-
function insertSorted(arr, job) {
|
|
1678
|
-
let i = 0;
|
|
1679
|
-
while (i < arr.length) {
|
|
1680
|
-
const cmp = arr[i];
|
|
1681
|
-
if (job.priority > cmp.priority || job.priority === cmp.priority && job.index < cmp.index) {
|
|
1682
|
-
break;
|
|
1683
|
-
}
|
|
1684
|
-
i++;
|
|
1685
|
-
}
|
|
1686
|
-
arr.splice(i, 0, job);
|
|
1687
|
-
}
|
|
1688
|
-
|
|
1689
|
-
function shouldRun(job, now) {
|
|
1690
|
-
if (!job.enabled) return false;
|
|
1691
|
-
if (!job.fps) return true;
|
|
1692
|
-
const minInterval = 1e3 / job.fps;
|
|
1693
|
-
const lastRun = job.lastRun ?? 0;
|
|
1694
|
-
const elapsed = now - lastRun;
|
|
1695
|
-
if (elapsed < minInterval) return false;
|
|
1696
|
-
if (job.drop) {
|
|
1697
|
-
job.lastRun = now;
|
|
1698
|
-
} else {
|
|
1699
|
-
const steps = Math.floor(elapsed / minInterval);
|
|
1700
|
-
job.lastRun = lastRun + steps * minInterval;
|
|
1701
|
-
if (job.lastRun < now - minInterval) {
|
|
1702
|
-
job.lastRun = now;
|
|
1703
|
-
}
|
|
1704
|
-
}
|
|
1705
|
-
return true;
|
|
1706
|
-
}
|
|
1707
|
-
function resetJobTiming(job) {
|
|
1708
|
-
job.lastRun = void 0;
|
|
1709
|
-
}
|
|
1710
|
-
|
|
1711
|
-
var __defProp$1 = Object.defineProperty;
|
|
1712
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1713
|
-
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1714
|
-
const hmrData = (() => {
|
|
1715
|
-
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return void 0;
|
|
1716
|
-
if (typeof import_meta_hot !== "undefined") return import_meta_hot;
|
|
1717
|
-
try {
|
|
1718
|
-
return (0, eval)("import.meta.hot");
|
|
1719
|
-
} catch {
|
|
1720
|
-
return void 0;
|
|
1721
|
-
}
|
|
1722
|
-
})();
|
|
1723
|
-
const _Scheduler = class _Scheduler {
|
|
1724
|
-
//* Constructor ================================
|
|
1725
|
-
constructor() {
|
|
1726
|
-
//* Critical State ================================
|
|
1727
|
-
__publicField$1(this, "roots", /* @__PURE__ */ new Map());
|
|
1728
|
-
__publicField$1(this, "phaseGraph");
|
|
1729
|
-
__publicField$1(this, "loopState", {
|
|
1730
|
-
running: false,
|
|
1731
|
-
rafHandle: null,
|
|
1732
|
-
lastTime: null,
|
|
1733
|
-
// null = uninitialized, 0+ = valid timestamp
|
|
1734
|
-
frameCount: 0,
|
|
1735
|
-
elapsedTime: 0,
|
|
1736
|
-
createdAt: performance.now()
|
|
1737
|
-
});
|
|
1738
|
-
__publicField$1(this, "stoppedTime", 0);
|
|
1739
|
-
//* Private State ================================
|
|
1740
|
-
__publicField$1(this, "nextRootIndex", 0);
|
|
1741
|
-
__publicField$1(this, "globalBeforeJobs", /* @__PURE__ */ new Map());
|
|
1742
|
-
__publicField$1(this, "globalAfterJobs", /* @__PURE__ */ new Map());
|
|
1743
|
-
__publicField$1(this, "nextGlobalIndex", 0);
|
|
1744
|
-
__publicField$1(this, "idleCallbacks", /* @__PURE__ */ new Set());
|
|
1745
|
-
__publicField$1(this, "nextJobIndex", 0);
|
|
1746
|
-
__publicField$1(this, "jobStateListeners", /* @__PURE__ */ new Map());
|
|
1747
|
-
__publicField$1(this, "pendingFrames", 0);
|
|
1748
|
-
__publicField$1(this, "_frameloop", "always");
|
|
1749
|
-
//* Independent Mode & Error Handling State ================================
|
|
1750
|
-
__publicField$1(this, "_independent", false);
|
|
1751
|
-
__publicField$1(this, "errorHandler", null);
|
|
1752
|
-
__publicField$1(this, "rootReadyCallbacks", /* @__PURE__ */ new Set());
|
|
1753
|
-
//* Core Loop Execution Methods ================================
|
|
1754
|
-
/**
|
|
1755
|
-
* Main RAF loop callback.
|
|
1756
|
-
* Executes frame, handles demand mode, and schedules next frame.
|
|
1757
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
1758
|
-
* @returns {void}
|
|
1759
|
-
* @private
|
|
1760
|
-
*/
|
|
1761
|
-
__publicField$1(this, "loop", (timestamp) => {
|
|
1762
|
-
if (!this.loopState.running) return;
|
|
1763
|
-
this.executeFrame(timestamp);
|
|
1764
|
-
if (this._frameloop === "demand") {
|
|
1765
|
-
this.pendingFrames = Math.max(0, this.pendingFrames - 1);
|
|
1766
|
-
if (this.pendingFrames === 0) {
|
|
1767
|
-
this.notifyIdle(timestamp);
|
|
1768
|
-
return this.stop();
|
|
1769
|
-
}
|
|
1770
|
-
}
|
|
1771
|
-
this.loopState.rafHandle = requestAnimationFrame(this.loop);
|
|
1772
|
-
});
|
|
1773
|
-
this.phaseGraph = new PhaseGraph();
|
|
1774
|
-
}
|
|
1775
|
-
static get instance() {
|
|
1776
|
-
return globalThis[_Scheduler.INSTANCE_KEY] ?? null;
|
|
1777
|
-
}
|
|
1778
|
-
static set instance(value) {
|
|
1779
|
-
globalThis[_Scheduler.INSTANCE_KEY] = value;
|
|
1780
|
-
}
|
|
1781
|
-
/**
|
|
1782
|
-
* Get the global scheduler instance (creates if doesn't exist).
|
|
1783
|
-
* Uses HMR data to preserve instance across hot reloads.
|
|
1784
|
-
* @returns {Scheduler} The singleton scheduler instance
|
|
1785
|
-
*/
|
|
1786
|
-
static get() {
|
|
1787
|
-
if (!_Scheduler.instance && hmrData?.data?.scheduler) {
|
|
1788
|
-
_Scheduler.instance = hmrData.data.scheduler;
|
|
1789
|
-
}
|
|
1790
|
-
if (!_Scheduler.instance) {
|
|
1791
|
-
_Scheduler.instance = new _Scheduler();
|
|
1792
|
-
if (hmrData?.data) {
|
|
1793
|
-
hmrData.data.scheduler = _Scheduler.instance;
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
return _Scheduler.instance;
|
|
1797
|
-
}
|
|
1798
|
-
/**
|
|
1799
|
-
* Reset the singleton instance. Stops the loop and clears all state.
|
|
1800
|
-
* Primarily used for testing to ensure clean state between tests.
|
|
1801
|
-
* @returns {void}
|
|
1802
|
-
*/
|
|
1803
|
-
static reset() {
|
|
1804
|
-
if (_Scheduler.instance) {
|
|
1805
|
-
_Scheduler.instance.stop();
|
|
1806
|
-
_Scheduler.instance = null;
|
|
1807
|
-
}
|
|
1808
|
-
if (hmrData?.data) {
|
|
1809
|
-
hmrData.data.scheduler = null;
|
|
1810
|
-
}
|
|
1811
|
-
}
|
|
1812
|
-
//* Getters & Setters ================================
|
|
1813
|
-
get phases() {
|
|
1814
|
-
return this.phaseGraph.getOrderedPhases();
|
|
1815
|
-
}
|
|
1816
|
-
get frameloop() {
|
|
1817
|
-
return this._frameloop;
|
|
1818
|
-
}
|
|
1819
|
-
set frameloop(mode) {
|
|
1820
|
-
if (this._frameloop === mode) return;
|
|
1821
|
-
const wasAlways = this._frameloop === "always";
|
|
1822
|
-
this._frameloop = mode;
|
|
1823
|
-
if (mode === "always" && !this.loopState.running && this.roots.size > 0) this.start();
|
|
1824
|
-
else if (mode !== "always" && wasAlways) this.stop();
|
|
1825
|
-
}
|
|
1826
|
-
get isRunning() {
|
|
1827
|
-
return this.loopState.running;
|
|
1828
|
-
}
|
|
1829
|
-
get isReady() {
|
|
1830
|
-
return this.roots.size > 0;
|
|
1831
|
-
}
|
|
1832
|
-
get independent() {
|
|
1833
|
-
return this._independent;
|
|
1834
|
-
}
|
|
1835
|
-
set independent(value) {
|
|
1836
|
-
this._independent = value;
|
|
1837
|
-
if (value) this.ensureDefaultRoot();
|
|
1838
|
-
}
|
|
1839
|
-
//* Root Management Methods ================================
|
|
1840
|
-
/**
|
|
1841
|
-
* Register a root (Canvas) with the scheduler.
|
|
1842
|
-
* The first root to register starts the RAF loop (if frameloop='always').
|
|
1843
|
-
* @param {string} id - Unique identifier for this root
|
|
1844
|
-
* @param {RootOptions} [options] - Optional configuration with getState and onError callbacks
|
|
1845
|
-
* @returns {() => void} Unsubscribe function to remove this root
|
|
1846
|
-
*/
|
|
1847
|
-
registerRoot(id, options = {}) {
|
|
1848
|
-
if (this.roots.has(id)) {
|
|
1849
|
-
console.warn(`[Scheduler] Root "${id}" already registered`);
|
|
1850
|
-
return () => this.unregisterRoot(id);
|
|
1851
|
-
}
|
|
1852
|
-
const entry = {
|
|
1853
|
-
id,
|
|
1854
|
-
getState: options.getState ?? (() => ({})),
|
|
1855
|
-
jobs: /* @__PURE__ */ new Map(),
|
|
1856
|
-
sortedJobs: [],
|
|
1857
|
-
needsRebuild: false
|
|
1858
|
-
};
|
|
1859
|
-
if (options.onError) {
|
|
1860
|
-
this.errorHandler = options.onError;
|
|
1861
|
-
}
|
|
1862
|
-
this.roots.set(id, entry);
|
|
1863
|
-
if (this.roots.size === 1) {
|
|
1864
|
-
this.notifyRootReady();
|
|
1865
|
-
if (this._frameloop === "always") this.start();
|
|
1866
|
-
}
|
|
1867
|
-
return () => this.unregisterRoot(id);
|
|
1868
|
-
}
|
|
1869
|
-
/**
|
|
1870
|
-
* Unregister a root from the scheduler.
|
|
1871
|
-
* Cleans up all job state listeners for this root's jobs.
|
|
1872
|
-
* The last root to unregister stops the RAF loop.
|
|
1873
|
-
* @param {string} id - The root ID to unregister
|
|
1874
|
-
* @returns {void}
|
|
1875
|
-
*/
|
|
1876
|
-
unregisterRoot(id) {
|
|
1877
|
-
const root = this.roots.get(id);
|
|
1878
|
-
if (!root) return;
|
|
1879
|
-
for (const jobId of root.jobs.keys()) {
|
|
1880
|
-
this.jobStateListeners.delete(jobId);
|
|
1881
|
-
}
|
|
1882
|
-
this.roots.delete(id);
|
|
1883
|
-
if (this.roots.size === 0) {
|
|
1884
|
-
this.stop();
|
|
1885
|
-
this.errorHandler = null;
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1888
|
-
/**
|
|
1889
|
-
* Subscribe to be notified when a root becomes available.
|
|
1890
|
-
* Fires immediately if a root already exists.
|
|
1891
|
-
* @param {() => void} callback - Function called when first root registers
|
|
1892
|
-
* @returns {() => void} Unsubscribe function
|
|
1893
|
-
*/
|
|
1894
|
-
onRootReady(callback) {
|
|
1895
|
-
if (this.roots.size > 0) {
|
|
1896
|
-
callback();
|
|
1897
|
-
return () => {
|
|
1898
|
-
};
|
|
1899
|
-
}
|
|
1900
|
-
this.rootReadyCallbacks.add(callback);
|
|
1901
|
-
return () => this.rootReadyCallbacks.delete(callback);
|
|
1902
|
-
}
|
|
1903
|
-
/**
|
|
1904
|
-
* Notify all registered root-ready callbacks.
|
|
1905
|
-
* Called when the first root registers.
|
|
1906
|
-
* @returns {void}
|
|
1907
|
-
* @private
|
|
1908
|
-
*/
|
|
1909
|
-
notifyRootReady() {
|
|
1910
|
-
for (const cb of this.rootReadyCallbacks) {
|
|
1911
|
-
try {
|
|
1912
|
-
cb();
|
|
1913
|
-
} catch (error) {
|
|
1914
|
-
console.error("[Scheduler] Error in root-ready callback:", error);
|
|
1915
|
-
}
|
|
1916
|
-
}
|
|
1917
|
-
this.rootReadyCallbacks.clear();
|
|
1918
|
-
}
|
|
1919
|
-
/**
|
|
1920
|
-
* Ensure a default root exists for independent mode.
|
|
1921
|
-
* Creates a minimal root with no state provider.
|
|
1922
|
-
* @returns {void}
|
|
1923
|
-
* @private
|
|
1924
|
-
*/
|
|
1925
|
-
ensureDefaultRoot() {
|
|
1926
|
-
if (!this.roots.has("__default__")) {
|
|
1927
|
-
this.registerRoot("__default__");
|
|
1928
|
-
}
|
|
1929
|
-
}
|
|
1930
|
-
/**
|
|
1931
|
-
* Trigger error handling for job errors.
|
|
1932
|
-
* Uses the bound error handler if available, otherwise logs to console.
|
|
1933
|
-
* @param {Error} error - The error to handle
|
|
1934
|
-
* @returns {void}
|
|
1935
|
-
*/
|
|
1936
|
-
triggerError(error) {
|
|
1937
|
-
if (this.errorHandler) this.errorHandler(error);
|
|
1938
|
-
else console.error("[Scheduler]", error);
|
|
1939
|
-
}
|
|
1940
|
-
//* Phase Management Methods ================================
|
|
1941
|
-
/**
|
|
1942
|
-
* Add a named phase to the scheduler's execution order.
|
|
1943
|
-
* Marks all roots for rebuild to incorporate the new phase.
|
|
1944
|
-
* @param {string} name - The phase name (e.g., 'physics', 'postprocess')
|
|
1945
|
-
* @param {AddPhaseOptions} [options] - Positioning options (before/after other phases)
|
|
1946
|
-
* @returns {void}
|
|
1947
|
-
* @example
|
|
1948
|
-
* scheduler.addPhase('physics', { before: 'update' });
|
|
1949
|
-
* scheduler.addPhase('postprocess', { after: 'render' });
|
|
1950
|
-
*/
|
|
1951
|
-
addPhase(name, options) {
|
|
1952
|
-
this.phaseGraph.addPhase(name, options);
|
|
1953
|
-
for (const root of this.roots.values()) {
|
|
1954
|
-
root.needsRebuild = true;
|
|
1955
|
-
}
|
|
1956
|
-
}
|
|
1957
|
-
/**
|
|
1958
|
-
* Check if a phase exists in the scheduler.
|
|
1959
|
-
* @param {string} name - The phase name to check
|
|
1960
|
-
* @returns {boolean} True if the phase exists
|
|
1961
|
-
*/
|
|
1962
|
-
hasPhase(name) {
|
|
1963
|
-
return this.phaseGraph.hasPhase(name);
|
|
1964
|
-
}
|
|
1965
|
-
//* Global Job Registration Methods (Deprecated APIs) ================================
|
|
1966
|
-
/**
|
|
1967
|
-
* Register a global job that runs once per frame (not per-root).
|
|
1968
|
-
* Used internally by deprecated addEffect/addAfterEffect APIs.
|
|
1969
|
-
* @param {'before' | 'after'} phase - When to run: 'before' all roots or 'after' all roots
|
|
1970
|
-
* @param {string} id - Unique identifier for this global job
|
|
1971
|
-
* @param {(timestamp: number) => void} callback - Function called each frame with RAF timestamp
|
|
1972
|
-
* @returns {() => void} Unsubscribe function to remove this global job
|
|
1973
|
-
* @deprecated Use useFrame with phases instead
|
|
1974
|
-
*/
|
|
1975
|
-
registerGlobal(phase, id, callback) {
|
|
1976
|
-
const job = { id, callback };
|
|
1977
|
-
if (phase === "before") {
|
|
1978
|
-
this.globalBeforeJobs.set(id, job);
|
|
1979
|
-
} else {
|
|
1980
|
-
this.globalAfterJobs.set(id, job);
|
|
1981
|
-
}
|
|
1982
|
-
return () => {
|
|
1983
|
-
if (phase === "before") this.globalBeforeJobs.delete(id);
|
|
1984
|
-
else this.globalAfterJobs.delete(id);
|
|
1985
|
-
};
|
|
1986
|
-
}
|
|
1987
|
-
//* Idle Callback Methods (Deprecated API) ================================
|
|
1988
|
-
/**
|
|
1989
|
-
* Register an idle callback that fires when the loop stops.
|
|
1990
|
-
* Used internally by deprecated addTail API.
|
|
1991
|
-
* @param {(timestamp: number) => void} callback - Function called when loop becomes idle
|
|
1992
|
-
* @returns {() => void} Unsubscribe function to remove this idle callback
|
|
1993
|
-
* @deprecated Use demand mode with invalidate() instead
|
|
1994
|
-
*/
|
|
1995
|
-
onIdle(callback) {
|
|
1996
|
-
this.idleCallbacks.add(callback);
|
|
1997
|
-
return () => this.idleCallbacks.delete(callback);
|
|
1998
|
-
}
|
|
1999
|
-
/**
|
|
2000
|
-
* Notify all registered idle callbacks.
|
|
2001
|
-
* Called when the loop stops in demand mode.
|
|
2002
|
-
* @param {number} timestamp - The RAF timestamp when idle occurred
|
|
2003
|
-
* @returns {void}
|
|
2004
|
-
* @private
|
|
2005
|
-
*/
|
|
2006
|
-
notifyIdle(timestamp) {
|
|
2007
|
-
for (const cb of this.idleCallbacks) {
|
|
2008
|
-
try {
|
|
2009
|
-
cb(timestamp);
|
|
2010
|
-
} catch (error) {
|
|
2011
|
-
console.error("[Scheduler] Error in idle callback:", error);
|
|
2012
|
-
}
|
|
2013
|
-
}
|
|
2014
|
-
}
|
|
2015
|
-
//* Job Registration & Management Methods ================================
|
|
2016
|
-
/**
|
|
2017
|
-
* Register a job (frame callback) with a specific root.
|
|
2018
|
-
* This is the core registration method used by useFrame internally.
|
|
2019
|
-
* @param {FrameNextCallback} callback - The function to call each frame
|
|
2020
|
-
* @param {JobOptions & { rootId?: string; system?: boolean }} [options] - Job configuration
|
|
2021
|
-
* @param {string} [options.rootId] - Target root ID (defaults to first registered root)
|
|
2022
|
-
* @param {string} [options.id] - Unique job ID (auto-generated if not provided)
|
|
2023
|
-
* @param {string} [options.phase] - Execution phase (defaults to 'update')
|
|
2024
|
-
* @param {number} [options.priority] - Priority within phase (higher = earlier, default 0)
|
|
2025
|
-
* @param {number} [options.fps] - FPS throttle limit
|
|
2026
|
-
* @param {boolean} [options.drop] - Drop frames when behind (default true)
|
|
2027
|
-
* @param {boolean} [options.enabled] - Whether job is active (default true)
|
|
2028
|
-
* @param {boolean} [options.system] - Internal flag for system jobs (not user-facing)
|
|
2029
|
-
* @returns {() => void} Unsubscribe function to remove this job
|
|
2030
|
-
*/
|
|
2031
|
-
register(callback, options = {}) {
|
|
2032
|
-
const rootId = options.rootId;
|
|
2033
|
-
const root = rootId ? this.roots.get(rootId) : this.roots.values().next().value;
|
|
2034
|
-
if (!root) {
|
|
2035
|
-
console.warn("[Scheduler] No root registered. Is this inside a Canvas?");
|
|
2036
|
-
return () => {
|
|
2037
|
-
};
|
|
2038
|
-
}
|
|
2039
|
-
const id = options.id ?? this.generateJobId();
|
|
2040
|
-
let phase = options.phase ?? "update";
|
|
2041
|
-
if (!options.phase && (options.before || options.after)) {
|
|
2042
|
-
phase = this.phaseGraph.resolveConstraintPhase(options.before, options.after);
|
|
2043
|
-
}
|
|
2044
|
-
const before = this.normalizeConstraints(options.before);
|
|
2045
|
-
const after = this.normalizeConstraints(options.after);
|
|
2046
|
-
const job = {
|
|
2047
|
-
id,
|
|
2048
|
-
callback,
|
|
2049
|
-
phase,
|
|
2050
|
-
before,
|
|
2051
|
-
after,
|
|
2052
|
-
priority: options.priority ?? 0,
|
|
2053
|
-
index: this.nextJobIndex++,
|
|
2054
|
-
fps: options.fps,
|
|
2055
|
-
drop: options.drop ?? true,
|
|
2056
|
-
enabled: options.enabled ?? true,
|
|
2057
|
-
system: options.system ?? false
|
|
2058
|
-
};
|
|
2059
|
-
if (root.jobs.has(id)) {
|
|
2060
|
-
console.warn(`[useFrame] Job with id "${id}" already exists, replacing`);
|
|
2061
|
-
}
|
|
2062
|
-
root.jobs.set(id, job);
|
|
2063
|
-
root.needsRebuild = true;
|
|
2064
|
-
return () => this.unregister(id, root.id);
|
|
2065
|
-
}
|
|
2066
|
-
/**
|
|
2067
|
-
* Unregister a job by its ID.
|
|
2068
|
-
* Searches all roots if rootId is not provided.
|
|
2069
|
-
* @param {string} id - The job ID to unregister
|
|
2070
|
-
* @param {string} [rootId] - Optional root ID to search (searches all if not provided)
|
|
2071
|
-
* @returns {void}
|
|
2072
|
-
*/
|
|
2073
|
-
unregister(id, rootId) {
|
|
2074
|
-
const root = rootId ? this.roots.get(rootId) : Array.from(this.roots.values()).find((r) => r.jobs.has(id));
|
|
2075
|
-
if (root?.jobs.delete(id)) {
|
|
2076
|
-
root.needsRebuild = true;
|
|
2077
|
-
this.jobStateListeners.delete(id);
|
|
2078
|
-
}
|
|
2079
|
-
}
|
|
2080
|
-
/**
|
|
2081
|
-
* Update a job's options dynamically.
|
|
2082
|
-
* Searches all roots to find the job by ID.
|
|
2083
|
-
* Phase/constraint changes trigger a rebuild of the sorted job list.
|
|
2084
|
-
* @param {string} id - The job ID to update
|
|
2085
|
-
* @param {Partial<JobOptions>} options - The options to update
|
|
2086
|
-
* @returns {void}
|
|
2087
|
-
*/
|
|
2088
|
-
updateJob(id, options) {
|
|
2089
|
-
let job;
|
|
2090
|
-
let root;
|
|
2091
|
-
for (const r of this.roots.values()) {
|
|
2092
|
-
job = r.jobs.get(id);
|
|
2093
|
-
if (job) {
|
|
2094
|
-
root = r;
|
|
2095
|
-
break;
|
|
2096
|
-
}
|
|
2097
|
-
}
|
|
2098
|
-
if (!job || !root) return;
|
|
2099
|
-
if (options.priority !== void 0) job.priority = options.priority;
|
|
2100
|
-
if (options.fps !== void 0) job.fps = options.fps;
|
|
2101
|
-
if (options.drop !== void 0) job.drop = options.drop;
|
|
2102
|
-
if (options.enabled !== void 0) {
|
|
2103
|
-
const wasEnabled = job.enabled;
|
|
2104
|
-
job.enabled = options.enabled;
|
|
2105
|
-
if (!wasEnabled && job.enabled) resetJobTiming(job);
|
|
2106
|
-
if (wasEnabled !== job.enabled) root.needsRebuild = true;
|
|
2107
|
-
}
|
|
2108
|
-
if (options.phase !== void 0 || options.before !== void 0 || options.after !== void 0) {
|
|
2109
|
-
if (options.phase) job.phase = options.phase;
|
|
2110
|
-
if (options.before !== void 0) job.before = this.normalizeConstraints(options.before);
|
|
2111
|
-
if (options.after !== void 0) job.after = this.normalizeConstraints(options.after);
|
|
2112
|
-
root.needsRebuild = true;
|
|
2113
|
-
}
|
|
2114
|
-
}
|
|
2115
|
-
//* Job State Management Methods ================================
|
|
2116
|
-
/**
|
|
2117
|
-
* Check if a job is currently paused (disabled).
|
|
2118
|
-
* @param {string} id - The job ID to check
|
|
2119
|
-
* @returns {boolean} True if the job exists and is paused
|
|
2120
|
-
*/
|
|
2121
|
-
isJobPaused(id) {
|
|
2122
|
-
for (const root of this.roots.values()) {
|
|
2123
|
-
const job = root.jobs.get(id);
|
|
2124
|
-
if (job) return !job.enabled;
|
|
2125
|
-
}
|
|
2126
|
-
return false;
|
|
2127
|
-
}
|
|
2128
|
-
/**
|
|
2129
|
-
* Subscribe to state changes for a specific job.
|
|
2130
|
-
* Listener is called when job is paused or resumed.
|
|
2131
|
-
* @param {string} id - The job ID to subscribe to
|
|
2132
|
-
* @param {() => void} listener - Callback invoked on state changes
|
|
2133
|
-
* @returns {() => void} Unsubscribe function
|
|
2134
|
-
*/
|
|
2135
|
-
subscribeJobState(id, listener) {
|
|
2136
|
-
if (!this.jobStateListeners.has(id)) {
|
|
2137
|
-
this.jobStateListeners.set(id, /* @__PURE__ */ new Set());
|
|
2138
|
-
}
|
|
2139
|
-
this.jobStateListeners.get(id).add(listener);
|
|
2140
|
-
return () => {
|
|
2141
|
-
this.jobStateListeners.get(id)?.delete(listener);
|
|
2142
|
-
if (this.jobStateListeners.get(id)?.size === 0) {
|
|
2143
|
-
this.jobStateListeners.delete(id);
|
|
2144
|
-
}
|
|
2145
|
-
};
|
|
2146
|
-
}
|
|
2147
|
-
/**
|
|
2148
|
-
* Notify all listeners that a job's state has changed.
|
|
2149
|
-
* @param {string} id - The job ID that changed
|
|
2150
|
-
* @returns {void}
|
|
2151
|
-
* @private
|
|
2152
|
-
*/
|
|
2153
|
-
notifyJobStateChange(id) {
|
|
2154
|
-
this.jobStateListeners.get(id)?.forEach((listener) => listener());
|
|
2155
|
-
}
|
|
2156
|
-
/**
|
|
2157
|
-
* Pause a job by ID (sets enabled=false).
|
|
2158
|
-
* Notifies any subscribed state listeners.
|
|
2159
|
-
* @param {string} id - The job ID to pause
|
|
2160
|
-
* @returns {void}
|
|
2161
|
-
*/
|
|
2162
|
-
pauseJob(id) {
|
|
2163
|
-
this.updateJob(id, { enabled: false });
|
|
2164
|
-
this.notifyJobStateChange(id);
|
|
2165
|
-
}
|
|
2166
|
-
/**
|
|
2167
|
-
* Resume a paused job by ID (sets enabled=true).
|
|
2168
|
-
* Resets job timing to prevent frame accumulation.
|
|
2169
|
-
* Notifies any subscribed state listeners.
|
|
2170
|
-
* @param {string} id - The job ID to resume
|
|
2171
|
-
* @returns {void}
|
|
2172
|
-
*/
|
|
2173
|
-
resumeJob(id) {
|
|
2174
|
-
this.updateJob(id, { enabled: true });
|
|
2175
|
-
this.notifyJobStateChange(id);
|
|
2176
|
-
}
|
|
2177
|
-
//* Frame Loop Control Methods ================================
|
|
2178
|
-
/**
|
|
2179
|
-
* Start the requestAnimationFrame loop.
|
|
2180
|
-
* Resets timing state (elapsedTime, frameCount) on start.
|
|
2181
|
-
* No-op if already running.
|
|
2182
|
-
* @returns {void}
|
|
2183
|
-
*/
|
|
2184
|
-
start() {
|
|
2185
|
-
if (this.loopState.running) return;
|
|
2186
|
-
const { elapsedTime, createdAt } = this.loopState;
|
|
2187
|
-
let adjustedCreated = 0;
|
|
2188
|
-
if (this.stoppedTime > 0) {
|
|
2189
|
-
adjustedCreated = createdAt - (performance.now() - this.stoppedTime);
|
|
2190
|
-
this.stoppedTime = 0;
|
|
2191
|
-
}
|
|
2192
|
-
Object.assign(this.loopState, {
|
|
2193
|
-
running: true,
|
|
2194
|
-
elapsedTime: elapsedTime ?? 0,
|
|
2195
|
-
lastTime: performance.now(),
|
|
2196
|
-
createdAt: adjustedCreated > 0 ? adjustedCreated : performance.now(),
|
|
2197
|
-
frameCount: 0,
|
|
2198
|
-
rafHandle: requestAnimationFrame(this.loop)
|
|
2199
|
-
});
|
|
2200
|
-
}
|
|
2201
|
-
/**
|
|
2202
|
-
* Stop the requestAnimationFrame loop.
|
|
2203
|
-
* Cancels any pending RAF callback.
|
|
2204
|
-
* No-op if not running.
|
|
2205
|
-
* @returns {void}
|
|
2206
|
-
*/
|
|
2207
|
-
stop() {
|
|
2208
|
-
if (!this.loopState.running) return;
|
|
2209
|
-
this.loopState.running = false;
|
|
2210
|
-
if (this.loopState.rafHandle !== null) {
|
|
2211
|
-
cancelAnimationFrame(this.loopState.rafHandle);
|
|
2212
|
-
this.loopState.rafHandle = null;
|
|
2213
|
-
}
|
|
2214
|
-
this.stoppedTime = performance.now();
|
|
2215
|
-
}
|
|
2216
|
-
/**
|
|
2217
|
-
* Request frames to be rendered in demand mode.
|
|
2218
|
-
* Accumulates pending frames (capped at 60) and starts the loop if not running.
|
|
2219
|
-
* No-op if frameloop is not 'demand'.
|
|
2220
|
-
* @param {number} [frames=1] - Number of frames to request
|
|
2221
|
-
* @param {boolean} [stackFrames=false] - Whether to add frames to existing pending count
|
|
2222
|
-
* - `false` (default): Sets pending frames to the specified value (replaces existing count)
|
|
2223
|
-
* - `true`: Adds frames to existing pending count (useful for accumulating invalidations)
|
|
2224
|
-
* @returns {void}
|
|
2225
|
-
* @example
|
|
2226
|
-
* // Request a single frame render
|
|
2227
|
-
* scheduler.invalidate();
|
|
2228
|
-
*
|
|
2229
|
-
* @example
|
|
2230
|
-
* // Request 5 frames (e.g., for animations)
|
|
2231
|
-
* scheduler.invalidate(5);
|
|
2232
|
-
*
|
|
2233
|
-
* @example
|
|
2234
|
-
* // Set pending frames to exactly 3 (don't stack with existing)
|
|
2235
|
-
* scheduler.invalidate(3, false);
|
|
2236
|
-
*
|
|
2237
|
-
* @example
|
|
2238
|
-
* // Add 2 more frames to existing pending count
|
|
2239
|
-
* scheduler.invalidate(2, true);
|
|
2240
|
-
*/
|
|
2241
|
-
invalidate(frames = 1, stackFrames = false) {
|
|
2242
|
-
if (this._frameloop !== "demand") return;
|
|
2243
|
-
const baseFrames = stackFrames ? this.pendingFrames : 0;
|
|
2244
|
-
this.pendingFrames = Math.min(60, baseFrames + frames);
|
|
2245
|
-
if (!this.loopState.running && this.pendingFrames > 0) this.start();
|
|
2246
|
-
}
|
|
2247
|
-
/**
|
|
2248
|
-
* Reset timing state for deterministic testing.
|
|
2249
|
-
* Preserves jobs and roots but resets lastTime, frameCount, elapsedTime, etc.
|
|
2250
|
-
* @returns {void}
|
|
2251
|
-
*/
|
|
2252
|
-
resetTiming() {
|
|
2253
|
-
this.loopState.lastTime = null;
|
|
2254
|
-
this.loopState.frameCount = 0;
|
|
2255
|
-
this.loopState.elapsedTime = 0;
|
|
2256
|
-
this.loopState.createdAt = performance.now();
|
|
2257
|
-
}
|
|
2258
|
-
//* Manual Stepping Methods ================================
|
|
2259
|
-
/**
|
|
2260
|
-
* Manually execute a single frame for all roots.
|
|
2261
|
-
* Useful for frameloop='never' mode or testing scenarios.
|
|
2262
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2263
|
-
* @returns {void}
|
|
2264
|
-
* @example
|
|
2265
|
-
* // Manual control mode
|
|
2266
|
-
* scheduler.frameloop = 'never';
|
|
2267
|
-
* scheduler.step(); // Execute one frame
|
|
2268
|
-
*/
|
|
2269
|
-
step(timestamp) {
|
|
2270
|
-
const now = timestamp ?? performance.now();
|
|
2271
|
-
this.executeFrame(now);
|
|
2272
|
-
}
|
|
2273
|
-
/**
|
|
2274
|
-
* Manually execute a single job by its ID.
|
|
2275
|
-
* Useful for testing individual job callbacks in isolation.
|
|
2276
|
-
* @param {string} id - The job ID to step
|
|
2277
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2278
|
-
* @returns {void}
|
|
2279
|
-
*/
|
|
2280
|
-
stepJob(id, timestamp) {
|
|
2281
|
-
let job;
|
|
2282
|
-
let root;
|
|
2283
|
-
for (const r of this.roots.values()) {
|
|
2284
|
-
job = r.jobs.get(id);
|
|
2285
|
-
if (job) {
|
|
2286
|
-
root = r;
|
|
2287
|
-
break;
|
|
2288
|
-
}
|
|
2289
|
-
}
|
|
2290
|
-
if (!job || !root) {
|
|
2291
|
-
console.warn(`[Scheduler] Job "${id}" not found`);
|
|
2292
|
-
return;
|
|
2293
|
-
}
|
|
2294
|
-
const now = timestamp ?? performance.now();
|
|
2295
|
-
const deltaMs = this.loopState.lastTime !== null ? now - this.loopState.lastTime : 0;
|
|
2296
|
-
const delta = deltaMs / 1e3;
|
|
2297
|
-
const elapsed = now - this.loopState.createdAt;
|
|
2298
|
-
const providedState = root.getState?.() ?? {};
|
|
2299
|
-
const frameState = {
|
|
2300
|
-
...providedState,
|
|
2301
|
-
time: now,
|
|
2302
|
-
delta,
|
|
2303
|
-
elapsed,
|
|
2304
|
-
frame: this.loopState.frameCount
|
|
2305
|
-
};
|
|
2306
|
-
try {
|
|
2307
|
-
job.callback(frameState, delta);
|
|
2308
|
-
} catch (error) {
|
|
2309
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2310
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2311
|
-
}
|
|
2312
|
-
}
|
|
2313
|
-
/**
|
|
2314
|
-
* Execute a single frame across all roots.
|
|
2315
|
-
* Order: globalBefore → each root's jobs → globalAfter
|
|
2316
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2317
|
-
* @returns {void}
|
|
2318
|
-
* @private
|
|
2319
|
-
*/
|
|
2320
|
-
executeFrame(timestamp) {
|
|
2321
|
-
const deltaMs = this.loopState.lastTime !== null ? timestamp - this.loopState.lastTime : 0;
|
|
2322
|
-
const delta = deltaMs / 1e3;
|
|
2323
|
-
this.loopState.lastTime = timestamp;
|
|
2324
|
-
this.loopState.frameCount++;
|
|
2325
|
-
this.loopState.elapsedTime += deltaMs;
|
|
2326
|
-
this.runGlobalJobs(this.globalBeforeJobs, timestamp);
|
|
2327
|
-
for (const root of this.roots.values()) {
|
|
2328
|
-
this.tickRoot(root, timestamp, delta);
|
|
2329
|
-
}
|
|
2330
|
-
this.runGlobalJobs(this.globalAfterJobs, timestamp);
|
|
2331
|
-
}
|
|
2332
|
-
/**
|
|
2333
|
-
* Run all global jobs from a job map.
|
|
2334
|
-
* Catches and logs errors without stopping execution.
|
|
2335
|
-
* @param {Map<string, GlobalJob>} jobs - The global jobs map to execute
|
|
2336
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2337
|
-
* @returns {void}
|
|
2338
|
-
* @private
|
|
2339
|
-
*/
|
|
2340
|
-
runGlobalJobs(jobs, timestamp) {
|
|
2341
|
-
for (const job of jobs.values()) {
|
|
2342
|
-
try {
|
|
2343
|
-
job.callback(timestamp);
|
|
2344
|
-
} catch (error) {
|
|
2345
|
-
console.error(`[Scheduler] Error in global job "${job.id}":`, error);
|
|
2346
|
-
}
|
|
2347
|
-
}
|
|
2348
|
-
}
|
|
2349
|
-
/**
|
|
2350
|
-
* Execute all jobs for a single root in sorted order.
|
|
2351
|
-
* Rebuilds sorted job list if needed, then dispatches each job.
|
|
2352
|
-
* Errors are caught and propagated via triggerError.
|
|
2353
|
-
* @param {RootEntry} root - The root entry to tick
|
|
2354
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2355
|
-
* @param {number} delta - Time since last frame in seconds
|
|
2356
|
-
* @returns {void}
|
|
2357
|
-
* @private
|
|
2358
|
-
*/
|
|
2359
|
-
tickRoot(root, timestamp, delta) {
|
|
2360
|
-
if (root.needsRebuild) {
|
|
2361
|
-
root.sortedJobs = rebuildSortedJobs(root.jobs, this.phaseGraph);
|
|
2362
|
-
root.needsRebuild = false;
|
|
2363
|
-
}
|
|
2364
|
-
const providedState = root.getState?.() ?? {};
|
|
2365
|
-
const frameState = {
|
|
2366
|
-
...providedState,
|
|
2367
|
-
time: timestamp,
|
|
2368
|
-
delta,
|
|
2369
|
-
elapsed: this.loopState.elapsedTime / 1e3,
|
|
2370
|
-
// Convert ms to seconds
|
|
2371
|
-
frame: this.loopState.frameCount
|
|
2372
|
-
};
|
|
2373
|
-
for (const job of root.sortedJobs) {
|
|
2374
|
-
if (!shouldRun(job, timestamp)) continue;
|
|
2375
|
-
try {
|
|
2376
|
-
job.callback(frameState, delta);
|
|
2377
|
-
} catch (error) {
|
|
2378
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2379
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2380
|
-
}
|
|
2381
|
-
}
|
|
2382
|
-
}
|
|
2383
|
-
//* Debug & Inspection Methods ================================
|
|
2384
|
-
/**
|
|
2385
|
-
* Get the total number of registered jobs across all roots.
|
|
2386
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2387
|
-
* @returns {number} Total job count
|
|
2388
|
-
*/
|
|
2389
|
-
getJobCount() {
|
|
2390
|
-
let count = 0;
|
|
2391
|
-
for (const root of this.roots.values()) {
|
|
2392
|
-
count += root.jobs.size;
|
|
2393
|
-
}
|
|
2394
|
-
return count + this.globalBeforeJobs.size + this.globalAfterJobs.size;
|
|
2395
|
-
}
|
|
2396
|
-
/**
|
|
2397
|
-
* Get all registered job IDs across all roots.
|
|
2398
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2399
|
-
* @returns {string[]} Array of all job IDs
|
|
2400
|
-
*/
|
|
2401
|
-
getJobIds() {
|
|
2402
|
-
const ids = [];
|
|
2403
|
-
for (const root of this.roots.values()) {
|
|
2404
|
-
ids.push(...root.jobs.keys());
|
|
2405
|
-
}
|
|
2406
|
-
ids.push(...this.globalBeforeJobs.keys());
|
|
2407
|
-
ids.push(...this.globalAfterJobs.keys());
|
|
2408
|
-
return ids;
|
|
2409
|
-
}
|
|
2410
|
-
/**
|
|
2411
|
-
* Get the number of registered roots (Canvas instances).
|
|
2412
|
-
* @returns {number} Number of registered roots
|
|
2413
|
-
*/
|
|
2414
|
-
getRootCount() {
|
|
2415
|
-
return this.roots.size;
|
|
2416
|
-
}
|
|
2417
|
-
/**
|
|
2418
|
-
* Check if any user (non-system) jobs are registered in a specific phase.
|
|
2419
|
-
* Used by the default render job to know if a user has taken over rendering.
|
|
2420
|
-
*
|
|
2421
|
-
* @param phase The phase to check
|
|
2422
|
-
* @param rootId Optional root ID to check (checks all roots if not provided)
|
|
2423
|
-
* @returns true if any user jobs exist in the phase
|
|
2424
|
-
*/
|
|
2425
|
-
hasUserJobsInPhase(phase, rootId) {
|
|
2426
|
-
const rootsToCheck = rootId ? [this.roots.get(rootId)].filter(Boolean) : Array.from(this.roots.values());
|
|
2427
|
-
return rootsToCheck.some((root) => {
|
|
2428
|
-
if (!root) return false;
|
|
2429
|
-
for (const job of root.jobs.values()) {
|
|
2430
|
-
if (job.phase === phase && !job.system && job.enabled) return true;
|
|
2431
|
-
}
|
|
2432
|
-
return false;
|
|
2433
|
-
});
|
|
2434
|
-
}
|
|
2435
|
-
//* Utility Methods ================================
|
|
2436
|
-
/**
|
|
2437
|
-
* Generate a unique root ID for automatic root registration.
|
|
2438
|
-
* @returns {string} A unique root ID in the format 'root_N'
|
|
2439
|
-
*/
|
|
2440
|
-
generateRootId() {
|
|
2441
|
-
return `root_${this.nextRootIndex++}`;
|
|
2442
|
-
}
|
|
2443
|
-
/**
|
|
2444
|
-
* Generate a unique job ID.
|
|
2445
|
-
* @returns {string} A unique job ID in the format 'job_N'
|
|
2446
|
-
* @private
|
|
2447
|
-
*/
|
|
2448
|
-
generateJobId() {
|
|
2449
|
-
return `job_${this.nextJobIndex}`;
|
|
2450
|
-
}
|
|
2451
|
-
/**
|
|
2452
|
-
* Normalize before/after constraints to a Set.
|
|
2453
|
-
* Handles undefined, single string, or array inputs.
|
|
2454
|
-
* @param {string | string[] | undefined} value - The constraint value(s)
|
|
2455
|
-
* @returns {Set<string>} Normalized Set of constraint strings
|
|
2456
|
-
* @private
|
|
2457
|
-
*/
|
|
2458
|
-
normalizeConstraints(value) {
|
|
2459
|
-
if (!value) return /* @__PURE__ */ new Set();
|
|
2460
|
-
if (Array.isArray(value)) return new Set(value);
|
|
2461
|
-
return /* @__PURE__ */ new Set([value]);
|
|
2462
|
-
}
|
|
2463
|
-
};
|
|
2464
|
-
//* Static State & Methods (Singleton Usage) ================================
|
|
2465
|
-
//* Cross-Bundle Singleton Key ==============================
|
|
2466
|
-
// Use Symbol.for() to ensure scheduler is shared across bundle boundaries
|
|
2467
|
-
// This prevents issues when mixing imports from @react-three/fiber and @react-three/fiber/webgpu
|
|
2468
|
-
__publicField$1(_Scheduler, "INSTANCE_KEY", Symbol.for("@react-three/fiber.scheduler"));
|
|
2469
|
-
let Scheduler = _Scheduler;
|
|
2470
|
-
const getScheduler = () => Scheduler.get();
|
|
2471
|
-
if (hmrData) {
|
|
2472
|
-
hmrData.accept?.();
|
|
1642
|
+
const INTERNAL_LIB_MARKERS = ["zustand", "setState", "Object.assign"];
|
|
1643
|
+
const INTERNAL_FIBER_MARKERS = ["packages/fiber/src", "packages/fiber/dist", "@react-three/fiber", "@react-three_fiber"];
|
|
1644
|
+
function isInternalRendererAccess(stack, callerDepth = 2) {
|
|
1645
|
+
if (!stack) return false;
|
|
1646
|
+
const caller = stack.split("\n")[callerDepth] ?? "";
|
|
1647
|
+
return INTERNAL_LIB_MARKERS.some((m) => caller.includes(m)) || INTERNAL_FIBER_MARKERS.some((m) => caller.includes(m));
|
|
2473
1648
|
}
|
|
2474
1649
|
|
|
2475
1650
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
@@ -2509,7 +1684,14 @@ const createStore = (invalidate, advance) => {
|
|
|
2509
1684
|
frustum: new Frustum(),
|
|
2510
1685
|
autoUpdateFrustum: true,
|
|
2511
1686
|
raycaster: null,
|
|
2512
|
-
events: {
|
|
1687
|
+
events: {
|
|
1688
|
+
priority: 1,
|
|
1689
|
+
enabled: true,
|
|
1690
|
+
connected: false,
|
|
1691
|
+
frameTimedRaycasts: true,
|
|
1692
|
+
alwaysFireOnScroll: true,
|
|
1693
|
+
updateOnFrame: false
|
|
1694
|
+
},
|
|
2513
1695
|
scene: null,
|
|
2514
1696
|
rootScene: null,
|
|
2515
1697
|
xr: null,
|
|
@@ -2600,11 +1782,14 @@ const createStore = (invalidate, advance) => {
|
|
|
2600
1782
|
},
|
|
2601
1783
|
setError: (error) => set(() => ({ error })),
|
|
2602
1784
|
error: null,
|
|
2603
|
-
//* TSL State (managed via hooks: useUniforms, useNodes, useTextures,
|
|
1785
|
+
//* TSL State (managed via hooks: useUniforms, useNodes, useBuffers, useGPUStorage, useTextures, useRenderPipeline) ==============================
|
|
2604
1786
|
uniforms: {},
|
|
2605
1787
|
nodes: {},
|
|
1788
|
+
buffers: {},
|
|
1789
|
+
gpuStorage: {},
|
|
2606
1790
|
textures: /* @__PURE__ */ new Map(),
|
|
2607
|
-
|
|
1791
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
1792
|
+
renderPipeline: null,
|
|
2608
1793
|
passes: {},
|
|
2609
1794
|
_hmrVersion: 0,
|
|
2610
1795
|
_sizeImperative: false,
|
|
@@ -2613,12 +1798,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2613
1798
|
internal: {
|
|
2614
1799
|
// Events
|
|
2615
1800
|
interaction: [],
|
|
2616
|
-
hovered: /* @__PURE__ */ new Map(),
|
|
2617
1801
|
subscribers: [],
|
|
1802
|
+
// Per-pointer state (new unified structure)
|
|
1803
|
+
pointerMap: /* @__PURE__ */ new Map(),
|
|
1804
|
+
pointerDirty: /* @__PURE__ */ new Map(),
|
|
1805
|
+
lastEvent: React.createRef(),
|
|
1806
|
+
// Deprecated but kept for backwards compatibility
|
|
1807
|
+
hovered: /* @__PURE__ */ new Map(),
|
|
2618
1808
|
initialClick: [0, 0],
|
|
2619
1809
|
initialHits: [],
|
|
2620
1810
|
capturedMap: /* @__PURE__ */ new Map(),
|
|
2621
|
-
lastEvent: React.createRef(),
|
|
2622
1811
|
// Visibility tracking (onFramed, onOccluded, onVisible)
|
|
2623
1812
|
visibilityRegistry: /* @__PURE__ */ new Map(),
|
|
2624
1813
|
// Occlusion system (WebGPU only)
|
|
@@ -2657,8 +1846,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2657
1846
|
const currentState = rootStore.getState();
|
|
2658
1847
|
if (!currentState.isLegacy && currentState.internal.actualRenderer) {
|
|
2659
1848
|
const stack = new Error().stack || "";
|
|
2660
|
-
|
|
2661
|
-
if (!isInternalAccess) {
|
|
1849
|
+
if (!isInternalRendererAccess(stack)) {
|
|
2662
1850
|
const cleanedStack = stack.split("\n").slice(2).join("\n") || "Stack trace unavailable";
|
|
2663
1851
|
notifyDepreciated({
|
|
2664
1852
|
heading: "Accessing state.gl in WebGPU mode",
|
|
@@ -2706,14 +1894,16 @@ const createStore = (invalidate, advance) => {
|
|
|
2706
1894
|
oldSize = size;
|
|
2707
1895
|
oldDpr = viewport.dpr;
|
|
2708
1896
|
updateCamera(camera, size);
|
|
2709
|
-
if (canvasTarget) {
|
|
1897
|
+
if (internal.isSecondary && canvasTarget) {
|
|
2710
1898
|
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
2711
|
-
|
|
2712
|
-
canvasTarget.setSize(size.width, size.height, updateStyle);
|
|
1899
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
2713
1900
|
} else {
|
|
2714
1901
|
if (viewport.dpr > 0) actualRenderer.setPixelRatio(viewport.dpr);
|
|
2715
|
-
|
|
2716
|
-
|
|
1902
|
+
actualRenderer.setSize(size.width, size.height, false);
|
|
1903
|
+
if (canvasTarget) {
|
|
1904
|
+
if (viewport.dpr > 0) canvasTarget.setPixelRatio(viewport.dpr);
|
|
1905
|
+
canvasTarget.setSize(size.width, size.height, false);
|
|
1906
|
+
}
|
|
2717
1907
|
}
|
|
2718
1908
|
}
|
|
2719
1909
|
if (camera !== oldCamera) {
|
|
@@ -2857,20 +2047,14 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
2857
2047
|
}
|
|
2858
2048
|
};
|
|
2859
2049
|
} else {
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
unregisterJob = registerOutside();
|
|
2869
|
-
});
|
|
2870
|
-
return () => {
|
|
2871
|
-
unsubReady();
|
|
2872
|
-
unregisterJob?.();
|
|
2873
|
-
};
|
|
2050
|
+
return scheduler.register(
|
|
2051
|
+
(state, delta) => {
|
|
2052
|
+
const frameState = state;
|
|
2053
|
+
if (!frameState.renderer) return;
|
|
2054
|
+
callbackRef.current?.(frameState, delta);
|
|
2055
|
+
},
|
|
2056
|
+
{ id, ...options }
|
|
2057
|
+
);
|
|
2874
2058
|
}
|
|
2875
2059
|
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
2876
2060
|
const isPaused = React.useSyncExternalStore(
|
|
@@ -2961,18 +2145,18 @@ function buildFromCache(input, textureCache) {
|
|
|
2961
2145
|
function useTexture(input, optionsOrOnLoad) {
|
|
2962
2146
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
2963
2147
|
const store = useStore();
|
|
2964
|
-
const textureCache = useThree((state) => state.textures);
|
|
2965
2148
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
2966
|
-
const { onLoad, cache =
|
|
2149
|
+
const { onLoad, cache = true } = options;
|
|
2967
2150
|
const onLoadRef = useRef(onLoad);
|
|
2968
2151
|
onLoadRef.current = onLoad;
|
|
2969
2152
|
const onLoadCalledForRef = useRef(null);
|
|
2970
2153
|
const urls = useMemo(() => getUrls(input), [input]);
|
|
2971
2154
|
const cachedResult = useMemo(() => {
|
|
2972
2155
|
if (!cache) return null;
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2156
|
+
const textures = store.getState().textures;
|
|
2157
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2158
|
+
return buildFromCache(input, textures);
|
|
2159
|
+
}, [cache, urls, input, store]);
|
|
2976
2160
|
const loadedTextures = useLoader(
|
|
2977
2161
|
TextureLoader,
|
|
2978
2162
|
IsObject(input) ? Object.values(input) : input
|
|
@@ -3016,8 +2200,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3016
2200
|
}, [input, loadedTextures, cachedResult]);
|
|
3017
2201
|
useEffect(() => {
|
|
3018
2202
|
if (!cache) return;
|
|
3019
|
-
if (cachedResult) return;
|
|
3020
|
-
const set = store.setState;
|
|
3021
2203
|
const urlTextureMap = [];
|
|
3022
2204
|
if (typeof input === "string") {
|
|
3023
2205
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -3031,18 +2213,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3031
2213
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
3032
2214
|
}
|
|
3033
2215
|
}
|
|
3034
|
-
|
|
3035
|
-
const
|
|
3036
|
-
let
|
|
2216
|
+
store.setState((state) => {
|
|
2217
|
+
const refs = new Map(state._textureRefs);
|
|
2218
|
+
let textures = state.textures;
|
|
2219
|
+
let added = false;
|
|
3037
2220
|
for (const [url, texture] of urlTextureMap) {
|
|
3038
|
-
if (!
|
|
3039
|
-
|
|
3040
|
-
|
|
2221
|
+
if (!textures.has(url)) {
|
|
2222
|
+
if (!added) {
|
|
2223
|
+
textures = new Map(textures);
|
|
2224
|
+
added = true;
|
|
2225
|
+
}
|
|
2226
|
+
textures.set(url, texture);
|
|
3041
2227
|
}
|
|
2228
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
3042
2229
|
}
|
|
3043
|
-
return
|
|
2230
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
3044
2231
|
});
|
|
3045
|
-
|
|
2232
|
+
return () => store.setState((state) => {
|
|
2233
|
+
const refs = new Map(state._textureRefs);
|
|
2234
|
+
for (const [url] of urlTextureMap) {
|
|
2235
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2236
|
+
if (next <= 0) refs.delete(url);
|
|
2237
|
+
else refs.set(url, next);
|
|
2238
|
+
}
|
|
2239
|
+
return { _textureRefs: refs };
|
|
2240
|
+
});
|
|
2241
|
+
}, [cache, input, mappedTextures, store]);
|
|
3046
2242
|
return mappedTextures;
|
|
3047
2243
|
}
|
|
3048
2244
|
useTexture.preload = (url) => useLoader.preload(TextureLoader, url);
|
|
@@ -3058,96 +2254,63 @@ const Texture = ({
|
|
|
3058
2254
|
return /* @__PURE__ */ jsx(Fragment, { children: children?.(ret) });
|
|
3059
2255
|
};
|
|
3060
2256
|
|
|
3061
|
-
function
|
|
3062
|
-
if (entry instanceof Texture$1) return entry;
|
|
3063
|
-
if (entry && typeof entry === "object" && "value" in entry && entry.value instanceof Texture$1) {
|
|
3064
|
-
return entry.value;
|
|
3065
|
-
}
|
|
3066
|
-
return null;
|
|
3067
|
-
}
|
|
3068
|
-
function useTextures() {
|
|
2257
|
+
function useTextures(selector) {
|
|
3069
2258
|
const store = useStore();
|
|
3070
|
-
|
|
3071
|
-
const
|
|
3072
|
-
const
|
|
3073
|
-
const
|
|
3074
|
-
set((state) => {
|
|
3075
|
-
const newMap = new Map(state.textures);
|
|
3076
|
-
newMap.set(key, value);
|
|
3077
|
-
return { textures: newMap };
|
|
3078
|
-
});
|
|
3079
|
-
};
|
|
3080
|
-
const addMultiple = (items) => {
|
|
3081
|
-
set((state) => {
|
|
3082
|
-
const newMap = new Map(state.textures);
|
|
3083
|
-
const entries = items instanceof Map ? items.entries() : Object.entries(items);
|
|
3084
|
-
for (const [key, value] of entries) {
|
|
3085
|
-
newMap.set(key, value);
|
|
3086
|
-
}
|
|
3087
|
-
return { textures: newMap };
|
|
3088
|
-
});
|
|
3089
|
-
};
|
|
3090
|
-
const remove = (key) => {
|
|
3091
|
-
set((state) => {
|
|
3092
|
-
const newMap = new Map(state.textures);
|
|
3093
|
-
newMap.delete(key);
|
|
3094
|
-
return { textures: newMap };
|
|
3095
|
-
});
|
|
3096
|
-
};
|
|
3097
|
-
const removeMultiple = (keys) => {
|
|
3098
|
-
set((state) => {
|
|
3099
|
-
const newMap = new Map(state.textures);
|
|
3100
|
-
for (const key of keys) newMap.delete(key);
|
|
3101
|
-
return { textures: newMap };
|
|
3102
|
-
});
|
|
3103
|
-
};
|
|
3104
|
-
const dispose = (key) => {
|
|
3105
|
-
const entry = getState().textures.get(key);
|
|
3106
|
-
if (entry) {
|
|
3107
|
-
const tex = getTextureValue(entry);
|
|
3108
|
-
tex?.dispose();
|
|
3109
|
-
}
|
|
3110
|
-
remove(key);
|
|
3111
|
-
};
|
|
3112
|
-
const disposeMultiple = (keys) => {
|
|
3113
|
-
const textures = getState().textures;
|
|
3114
|
-
for (const key of keys) {
|
|
3115
|
-
const entry = textures.get(key);
|
|
3116
|
-
if (entry) {
|
|
3117
|
-
const tex = getTextureValue(entry);
|
|
3118
|
-
tex?.dispose();
|
|
3119
|
-
}
|
|
3120
|
-
}
|
|
3121
|
-
removeMultiple(keys);
|
|
3122
|
-
};
|
|
3123
|
-
const disposeAll = () => {
|
|
3124
|
-
const textures = getState().textures;
|
|
3125
|
-
for (const entry of textures.values()) {
|
|
3126
|
-
const tex = getTextureValue(entry);
|
|
3127
|
-
tex?.dispose();
|
|
3128
|
-
}
|
|
3129
|
-
set({ textures: /* @__PURE__ */ new Map() });
|
|
3130
|
-
};
|
|
2259
|
+
const registry = useMemo(() => {
|
|
2260
|
+
const getState = store.getState;
|
|
2261
|
+
const setState = store.setState;
|
|
2262
|
+
const getOne = (key) => getState().textures.get(key);
|
|
3131
2263
|
return {
|
|
3132
|
-
|
|
3133
|
-
get textures() {
|
|
2264
|
+
get all() {
|
|
3134
2265
|
return getState().textures;
|
|
3135
2266
|
},
|
|
3136
|
-
|
|
3137
|
-
|
|
2267
|
+
get(input) {
|
|
2268
|
+
if (typeof input === "string") return getOne(input);
|
|
2269
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2270
|
+
const out = {};
|
|
2271
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2272
|
+
return out;
|
|
2273
|
+
},
|
|
3138
2274
|
has: (key) => getState().textures.has(key),
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
2275
|
+
add(keyOrRecord, texture) {
|
|
2276
|
+
setState((state) => {
|
|
2277
|
+
const textures = new Map(state.textures);
|
|
2278
|
+
if (typeof keyOrRecord === "string") {
|
|
2279
|
+
textures.set(keyOrRecord, texture);
|
|
2280
|
+
} else {
|
|
2281
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2282
|
+
}
|
|
2283
|
+
return { textures };
|
|
2284
|
+
});
|
|
2285
|
+
},
|
|
2286
|
+
dispose(key, options) {
|
|
2287
|
+
const state = getState();
|
|
2288
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2289
|
+
if (refs > 0 && !options?.force) {
|
|
2290
|
+
console.warn(
|
|
2291
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2292
|
+
);
|
|
2293
|
+
return false;
|
|
2294
|
+
}
|
|
2295
|
+
state.textures.get(key)?.dispose();
|
|
2296
|
+
setState((s) => {
|
|
2297
|
+
const textures = new Map(s.textures);
|
|
2298
|
+
textures.delete(key);
|
|
2299
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2300
|
+
nextRefs.delete(key);
|
|
2301
|
+
return { textures, _textureRefs: nextRefs };
|
|
2302
|
+
});
|
|
2303
|
+
return true;
|
|
2304
|
+
},
|
|
2305
|
+
disposeAll() {
|
|
2306
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2307
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2308
|
+
}
|
|
3149
2309
|
};
|
|
3150
2310
|
}, [store]);
|
|
2311
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2312
|
+
const selected = useThree(subscribe);
|
|
2313
|
+
return selector ? selected : registry;
|
|
3151
2314
|
}
|
|
3152
2315
|
|
|
3153
2316
|
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
@@ -14998,7 +14161,6 @@ function createRoot(canvas) {
|
|
|
14998
14161
|
events,
|
|
14999
14162
|
onCreated: onCreatedCallback,
|
|
15000
14163
|
shadows = false,
|
|
15001
|
-
textureColorSpace = SRGBColorSpace,
|
|
15002
14164
|
orthographic = false,
|
|
15003
14165
|
frameloop = "always",
|
|
15004
14166
|
dpr = [1, 2],
|
|
@@ -15013,6 +14175,7 @@ function createRoot(canvas) {
|
|
|
15013
14175
|
_sizeProps,
|
|
15014
14176
|
forceEven
|
|
15015
14177
|
} = props;
|
|
14178
|
+
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || SRGBColorSpace;
|
|
15016
14179
|
const state = store.getState();
|
|
15017
14180
|
const defaultGPUProps = {
|
|
15018
14181
|
canvas,
|
|
@@ -15050,6 +14213,12 @@ function createRoot(canvas) {
|
|
|
15050
14213
|
} else if (!state.internal.actualRenderer) {
|
|
15051
14214
|
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, WebGPURenderer);
|
|
15052
14215
|
if (!renderer.hasInitialized?.()) {
|
|
14216
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14217
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14218
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14219
|
+
canvas.width = size2.width * pixelRatio;
|
|
14220
|
+
canvas.height = size2.height * pixelRatio;
|
|
14221
|
+
}
|
|
15053
14222
|
await renderer.init();
|
|
15054
14223
|
}
|
|
15055
14224
|
const backend = renderer.backend;
|
|
@@ -15159,7 +14328,7 @@ function createRoot(canvas) {
|
|
|
15159
14328
|
lastConfiguredProps.performance = performance;
|
|
15160
14329
|
}
|
|
15161
14330
|
if (!state.xr) {
|
|
15162
|
-
const handleXRFrame = (timestamp,
|
|
14331
|
+
const handleXRFrame = (timestamp, _frame) => {
|
|
15163
14332
|
const state2 = store.getState();
|
|
15164
14333
|
if (state2.frameloop === "never") return;
|
|
15165
14334
|
advance(timestamp);
|
|
@@ -15195,15 +14364,22 @@ function createRoot(canvas) {
|
|
|
15195
14364
|
const oldType = renderer.shadowMap.type;
|
|
15196
14365
|
renderer.shadowMap.enabled = !!shadows;
|
|
15197
14366
|
if (is.boo(shadows)) {
|
|
15198
|
-
renderer.shadowMap.type =
|
|
14367
|
+
renderer.shadowMap.type = PCFShadowMap;
|
|
15199
14368
|
} else if (is.str(shadows)) {
|
|
14369
|
+
if (shadows === "soft") {
|
|
14370
|
+
notifyDepreciated({
|
|
14371
|
+
heading: 'shadows="soft" is deprecated',
|
|
14372
|
+
body: "Three has depreciated soft and improved basic PCFShadows, we converted for you.",
|
|
14373
|
+
link: "https://github.com/mrdoob/three.js/wiki/Migration-Guide?utm_source=chatgpt.com#181--182"
|
|
14374
|
+
});
|
|
14375
|
+
}
|
|
15200
14376
|
const types = {
|
|
15201
14377
|
basic: BasicShadowMap,
|
|
15202
14378
|
percentage: PCFShadowMap,
|
|
15203
|
-
soft:
|
|
14379
|
+
soft: PCFShadowMap,
|
|
15204
14380
|
variance: VSMShadowMap
|
|
15205
14381
|
};
|
|
15206
|
-
renderer.shadowMap.type = types[shadows] ??
|
|
14382
|
+
renderer.shadowMap.type = types[shadows] ?? PCFShadowMap;
|
|
15207
14383
|
} else if (is.obj(shadows)) {
|
|
15208
14384
|
Object.assign(renderer.shadowMap, shadows);
|
|
15209
14385
|
}
|
|
@@ -15219,13 +14395,24 @@ function createRoot(canvas) {
|
|
|
15219
14395
|
if (state.textureColorSpace !== textureColorSpace) state.set(() => ({ textureColorSpace }));
|
|
15220
14396
|
lastConfiguredProps.textureColorSpace = textureColorSpace;
|
|
15221
14397
|
}
|
|
14398
|
+
const r3fProps = ["textureColorSpace"];
|
|
14399
|
+
const constructorOnlyProps = ["samples", "antialias", "alpha", "canvas", "powerPreference"];
|
|
14400
|
+
const nonApplyProps = [...r3fProps, ...constructorOnlyProps];
|
|
15222
14401
|
if (glConfig && !is.fun(glConfig) && !isRenderer(glConfig) && !is.equ(glConfig, renderer, shallowLoose)) {
|
|
15223
|
-
|
|
14402
|
+
const glProps = {};
|
|
14403
|
+
for (const key in glConfig) {
|
|
14404
|
+
if (!nonApplyProps.includes(key)) glProps[key] = glConfig[key];
|
|
14405
|
+
}
|
|
14406
|
+
applyProps(renderer, glProps);
|
|
15224
14407
|
}
|
|
15225
14408
|
if (rendererConfig && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && state.renderer) {
|
|
15226
14409
|
const currentRenderer = state.renderer;
|
|
15227
14410
|
if (!is.equ(rendererConfig, currentRenderer, shallowLoose)) {
|
|
15228
|
-
|
|
14411
|
+
const rendererProps = {};
|
|
14412
|
+
for (const key in rendererConfig) {
|
|
14413
|
+
if (!nonApplyProps.includes(key)) rendererProps[key] = rendererConfig[key];
|
|
14414
|
+
}
|
|
14415
|
+
applyProps(currentRenderer, rendererProps);
|
|
15229
14416
|
}
|
|
15230
14417
|
}
|
|
15231
14418
|
const scheduler = getScheduler();
|
|
@@ -15251,6 +14438,18 @@ function createRoot(canvas) {
|
|
|
15251
14438
|
system: true
|
|
15252
14439
|
}
|
|
15253
14440
|
);
|
|
14441
|
+
const unregisterEventsFlush = scheduler.register(
|
|
14442
|
+
() => {
|
|
14443
|
+
const state2 = store.getState();
|
|
14444
|
+
state2.events.flush?.();
|
|
14445
|
+
},
|
|
14446
|
+
{
|
|
14447
|
+
id: `${newRootId}_events`,
|
|
14448
|
+
rootId: newRootId,
|
|
14449
|
+
phase: "input",
|
|
14450
|
+
system: true
|
|
14451
|
+
}
|
|
14452
|
+
);
|
|
15254
14453
|
const unregisterFrustum = scheduler.register(
|
|
15255
14454
|
() => {
|
|
15256
14455
|
const state2 = store.getState();
|
|
@@ -15261,7 +14460,7 @@ function createRoot(canvas) {
|
|
|
15261
14460
|
{
|
|
15262
14461
|
id: `${newRootId}_frustum`,
|
|
15263
14462
|
rootId: newRootId,
|
|
15264
|
-
|
|
14463
|
+
before: "render",
|
|
15265
14464
|
system: true
|
|
15266
14465
|
}
|
|
15267
14466
|
);
|
|
@@ -15273,7 +14472,7 @@ function createRoot(canvas) {
|
|
|
15273
14472
|
{
|
|
15274
14473
|
id: `${newRootId}_visibility`,
|
|
15275
14474
|
rootId: newRootId,
|
|
15276
|
-
|
|
14475
|
+
before: "render",
|
|
15277
14476
|
system: true,
|
|
15278
14477
|
after: `${newRootId}_frustum`
|
|
15279
14478
|
}
|
|
@@ -15285,7 +14484,7 @@ function createRoot(canvas) {
|
|
|
15285
14484
|
const userHandlesRender = scheduler.hasUserJobsInPhase("render", newRootId);
|
|
15286
14485
|
if (userHandlesRender || state2.internal.priority) return;
|
|
15287
14486
|
try {
|
|
15288
|
-
if (state2.
|
|
14487
|
+
if (state2.renderPipeline?.render) state2.renderPipeline.render();
|
|
15289
14488
|
else if (renderer2?.render) renderer2.render(state2.scene, state2.camera);
|
|
15290
14489
|
} catch (error) {
|
|
15291
14490
|
state2.setError(error instanceof Error ? error : new Error(String(error)));
|
|
@@ -15310,6 +14509,7 @@ function createRoot(canvas) {
|
|
|
15310
14509
|
unregisterRoot: () => {
|
|
15311
14510
|
unregisterRoot();
|
|
15312
14511
|
unregisterCanvasTarget();
|
|
14512
|
+
unregisterEventsFlush();
|
|
15313
14513
|
unregisterFrustum();
|
|
15314
14514
|
unregisterVisibility();
|
|
15315
14515
|
unregisterRender();
|
|
@@ -15475,9 +14675,13 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15475
14675
|
const store = createWithEqualityFn((set, get) => ({ ...rest, set, get }));
|
|
15476
14676
|
const onMutate = (prev) => store.setState((state2) => inject.current(prev, state2));
|
|
15477
14677
|
onMutate(previousRoot.getState());
|
|
15478
|
-
previousRoot.subscribe(onMutate);
|
|
15479
14678
|
return store;
|
|
15480
14679
|
}, [previousRoot, container]);
|
|
14680
|
+
useIsomorphicLayoutEffect(() => {
|
|
14681
|
+
const onMutate = (prev) => usePortalStore.setState((state2) => inject.current(prev, state2));
|
|
14682
|
+
const unsubscribe = previousRoot.subscribe(onMutate);
|
|
14683
|
+
return unsubscribe;
|
|
14684
|
+
}, [previousRoot, usePortalStore]);
|
|
15481
14685
|
return (
|
|
15482
14686
|
// @ts-ignore, reconciler types are not maintained
|
|
15483
14687
|
/* @__PURE__ */ jsx(Fragment, { children: reconciler.createPortal(
|
|
@@ -15491,6 +14695,59 @@ function flushSync(fn) {
|
|
|
15491
14695
|
return reconciler.flushSyncFromReconciler(fn);
|
|
15492
14696
|
}
|
|
15493
14697
|
|
|
14698
|
+
function parseBackground(background) {
|
|
14699
|
+
if (!background) return null;
|
|
14700
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14701
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14702
|
+
return {
|
|
14703
|
+
...rest,
|
|
14704
|
+
preset,
|
|
14705
|
+
files: envMap || files,
|
|
14706
|
+
backgroundFiles: backgroundMap,
|
|
14707
|
+
background: true
|
|
14708
|
+
};
|
|
14709
|
+
}
|
|
14710
|
+
if (typeof background === "number") {
|
|
14711
|
+
return { color: background, background: true };
|
|
14712
|
+
}
|
|
14713
|
+
if (typeof background === "string") {
|
|
14714
|
+
if (background in presetsObj) {
|
|
14715
|
+
return { preset: background, background: true };
|
|
14716
|
+
}
|
|
14717
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14718
|
+
return { files: background, background: true };
|
|
14719
|
+
}
|
|
14720
|
+
return { color: background, background: true };
|
|
14721
|
+
}
|
|
14722
|
+
if (background.isColor) {
|
|
14723
|
+
return { color: background, background: true };
|
|
14724
|
+
}
|
|
14725
|
+
return null;
|
|
14726
|
+
}
|
|
14727
|
+
|
|
14728
|
+
function parseRendererConfig(rendererProp) {
|
|
14729
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14730
|
+
if (isRendererConfig) {
|
|
14731
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14732
|
+
return {
|
|
14733
|
+
primaryCanvas: pc,
|
|
14734
|
+
scheduler: sc,
|
|
14735
|
+
renderer: Object.keys(rest).length > 0 ? rest : rendererProp
|
|
14736
|
+
};
|
|
14737
|
+
}
|
|
14738
|
+
return { primaryCanvas: void 0, scheduler: void 0, renderer: rendererProp };
|
|
14739
|
+
}
|
|
14740
|
+
|
|
14741
|
+
function clearHmrCaches(store) {
|
|
14742
|
+
store.setState((state) => ({
|
|
14743
|
+
nodes: {},
|
|
14744
|
+
uniforms: {},
|
|
14745
|
+
buffers: {},
|
|
14746
|
+
gpuStorage: {},
|
|
14747
|
+
_hmrVersion: state._hmrVersion + 1
|
|
14748
|
+
}));
|
|
14749
|
+
}
|
|
14750
|
+
|
|
15494
14751
|
function CanvasImpl({
|
|
15495
14752
|
ref,
|
|
15496
14753
|
children,
|
|
@@ -15511,6 +14768,8 @@ function CanvasImpl({
|
|
|
15511
14768
|
raycaster,
|
|
15512
14769
|
camera,
|
|
15513
14770
|
scene,
|
|
14771
|
+
autoUpdateFrustum,
|
|
14772
|
+
occlusion,
|
|
15514
14773
|
onPointerMissed,
|
|
15515
14774
|
onDragOverMissed,
|
|
15516
14775
|
onDropMissed,
|
|
@@ -15522,39 +14781,10 @@ function CanvasImpl({
|
|
|
15522
14781
|
forceEven,
|
|
15523
14782
|
...props
|
|
15524
14783
|
}) {
|
|
15525
|
-
const { primaryCanvas, scheduler,
|
|
15526
|
-
const renderer = Object.keys(rendererConfig).length > 0 ? rendererConfig : rendererProp;
|
|
14784
|
+
const { primaryCanvas, scheduler, renderer } = parseRendererConfig(rendererProp);
|
|
15527
14785
|
React.useMemo(() => extend(THREE), []);
|
|
15528
14786
|
const Bridge = useBridge();
|
|
15529
|
-
const backgroundProps = React.useMemo(() =>
|
|
15530
|
-
if (!background) return null;
|
|
15531
|
-
if (typeof background === "object" && !background.isColor) {
|
|
15532
|
-
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
15533
|
-
return {
|
|
15534
|
-
...rest,
|
|
15535
|
-
preset,
|
|
15536
|
-
files: envMap || files,
|
|
15537
|
-
backgroundFiles: backgroundMap,
|
|
15538
|
-
background: true
|
|
15539
|
-
};
|
|
15540
|
-
}
|
|
15541
|
-
if (typeof background === "number") {
|
|
15542
|
-
return { color: background, background: true };
|
|
15543
|
-
}
|
|
15544
|
-
if (typeof background === "string") {
|
|
15545
|
-
if (background in presetsObj) {
|
|
15546
|
-
return { preset: background, background: true };
|
|
15547
|
-
}
|
|
15548
|
-
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
15549
|
-
return { files: background, background: true };
|
|
15550
|
-
}
|
|
15551
|
-
return { color: background, background: true };
|
|
15552
|
-
}
|
|
15553
|
-
if (background.isColor) {
|
|
15554
|
-
return { color: background, background: true };
|
|
15555
|
-
}
|
|
15556
|
-
return null;
|
|
15557
|
-
}, [background]);
|
|
14787
|
+
const backgroundProps = React.useMemo(() => parseBackground(background), [background]);
|
|
15558
14788
|
const hasInitialSizeRef = React.useRef(false);
|
|
15559
14789
|
const measureConfig = React.useMemo(() => {
|
|
15560
14790
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15638,6 +14868,8 @@ function CanvasImpl({
|
|
|
15638
14868
|
performance,
|
|
15639
14869
|
raycaster,
|
|
15640
14870
|
camera,
|
|
14871
|
+
autoUpdateFrustum,
|
|
14872
|
+
occlusion,
|
|
15641
14873
|
size: effectiveSize,
|
|
15642
14874
|
// Store size props for reset functionality
|
|
15643
14875
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
@@ -15697,20 +14929,13 @@ function CanvasImpl({
|
|
|
15697
14929
|
const handleHMR = () => {
|
|
15698
14930
|
queueMicrotask(() => {
|
|
15699
14931
|
const rootEntry = _roots.get(canvas);
|
|
15700
|
-
if (rootEntry?.store)
|
|
15701
|
-
rootEntry.store.setState((state) => ({
|
|
15702
|
-
nodes: {},
|
|
15703
|
-
uniforms: {},
|
|
15704
|
-
_hmrVersion: state._hmrVersion + 1
|
|
15705
|
-
}));
|
|
15706
|
-
}
|
|
14932
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15707
14933
|
});
|
|
15708
14934
|
};
|
|
15709
14935
|
if (typeof import.meta !== "undefined" && import.meta.hot) {
|
|
15710
14936
|
const hot = import.meta.hot;
|
|
15711
14937
|
hot.on("vite:afterUpdate", handleHMR);
|
|
15712
|
-
return () => hot.
|
|
15713
|
-
});
|
|
14938
|
+
return () => hot.off?.("vite:afterUpdate", handleHMR);
|
|
15714
14939
|
}
|
|
15715
14940
|
if (typeof module !== "undefined" && module.hot) {
|
|
15716
14941
|
const hot = module.hot;
|
|
@@ -15733,7 +14958,16 @@ function CanvasImpl({
|
|
|
15733
14958
|
...style
|
|
15734
14959
|
},
|
|
15735
14960
|
...props,
|
|
15736
|
-
children: /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
14961
|
+
children: /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
14962
|
+
"canvas",
|
|
14963
|
+
{
|
|
14964
|
+
ref: canvasRef,
|
|
14965
|
+
id,
|
|
14966
|
+
className: "r3f-canvas",
|
|
14967
|
+
style: { display: "block", width: "100%", height: "100%" },
|
|
14968
|
+
children: fallback
|
|
14969
|
+
}
|
|
14970
|
+
) })
|
|
15737
14971
|
}
|
|
15738
14972
|
);
|
|
15739
14973
|
}
|
|
@@ -15809,6 +15043,8 @@ function createScopedStore(data) {
|
|
|
15809
15043
|
function createLazyCreatorState(state) {
|
|
15810
15044
|
let _uniforms = null;
|
|
15811
15045
|
let _nodes = null;
|
|
15046
|
+
let _buffers = null;
|
|
15047
|
+
let _gpuStorage = null;
|
|
15812
15048
|
return Object.create(state, {
|
|
15813
15049
|
uniforms: {
|
|
15814
15050
|
get() {
|
|
@@ -15819,10 +15055,34 @@ function createLazyCreatorState(state) {
|
|
|
15819
15055
|
get() {
|
|
15820
15056
|
return _nodes ?? (_nodes = createScopedStore(state.nodes));
|
|
15821
15057
|
}
|
|
15058
|
+
},
|
|
15059
|
+
buffers: {
|
|
15060
|
+
get() {
|
|
15061
|
+
return _buffers ?? (_buffers = createScopedStore(state.buffers));
|
|
15062
|
+
}
|
|
15063
|
+
},
|
|
15064
|
+
gpuStorage: {
|
|
15065
|
+
get() {
|
|
15066
|
+
return _gpuStorage ?? (_gpuStorage = createScopedStore(state.gpuStorage));
|
|
15067
|
+
}
|
|
15822
15068
|
}
|
|
15823
15069
|
});
|
|
15824
15070
|
}
|
|
15825
15071
|
|
|
15072
|
+
function resolvePrimary(local) {
|
|
15073
|
+
return local.getState().primaryStore ?? local;
|
|
15074
|
+
}
|
|
15075
|
+
function usePrimaryStore() {
|
|
15076
|
+
const local = useStore();
|
|
15077
|
+
const primary = local.getState().primaryStore;
|
|
15078
|
+
return useMemo(() => primary ?? local, [primary, local]);
|
|
15079
|
+
}
|
|
15080
|
+
function usePrimaryThree(selector = (state) => state, equalityFn) {
|
|
15081
|
+
const local = useStore();
|
|
15082
|
+
const primary = resolvePrimary(local);
|
|
15083
|
+
return primary(selector, equalityFn);
|
|
15084
|
+
}
|
|
15085
|
+
|
|
15826
15086
|
function addTexture(set, key, value) {
|
|
15827
15087
|
set((state) => {
|
|
15828
15088
|
const newMap = new Map(state.textures);
|
|
@@ -15901,6 +15161,10 @@ function vectorize(inObject) {
|
|
|
15901
15161
|
if (obj.isVector2 || obj.isVector3 || obj.isVector4) return inObject;
|
|
15902
15162
|
if (obj.isMatrix3 || obj.isMatrix4) return inObject;
|
|
15903
15163
|
if (obj.isColor || obj.isEuler || obj.isQuaternion || obj.isSpherical) return inObject;
|
|
15164
|
+
if ("r" in obj && "g" in obj && "b" in obj && typeof obj.r === "number" && typeof obj.g === "number" && typeof obj.b === "number") {
|
|
15165
|
+
const scale = obj.r > 1 || obj.g > 1 || obj.b > 1 ? 1 / 255 : 1;
|
|
15166
|
+
return new Color(obj.r * scale, obj.g * scale, obj.b * scale);
|
|
15167
|
+
}
|
|
15904
15168
|
if ("x" in obj && "y" in obj && typeof obj.x === "number" && typeof obj.y === "number") {
|
|
15905
15169
|
if ("w" in obj && typeof obj.w === "number" && "z" in obj && typeof obj.z === "number") {
|
|
15906
15170
|
return new Vector4(obj.x, obj.y, obj.z, obj.w);
|
|
@@ -15924,7 +15188,7 @@ function useCompareMemoize(value, deep) {
|
|
|
15924
15188
|
|
|
15925
15189
|
const isUniformNode$1 = (value) => value !== null && typeof value === "object" && "value" in value && "uuid" in value;
|
|
15926
15190
|
function useUniforms(creatorOrScope, scope) {
|
|
15927
|
-
const store =
|
|
15191
|
+
const store = usePrimaryStore();
|
|
15928
15192
|
const removeUniforms2 = useCallback(
|
|
15929
15193
|
(names, targetScope) => {
|
|
15930
15194
|
const nameArray = Array.isArray(names) ? names : [names];
|
|
@@ -15994,8 +15258,8 @@ function useUniforms(creatorOrScope, scope) {
|
|
|
15994
15258
|
}, [creatorOrScope, store]);
|
|
15995
15259
|
const memoizedInput = useCompareMemoize(inputForMemoization);
|
|
15996
15260
|
const isReader = memoizedInput === void 0 || typeof memoizedInput === "string";
|
|
15997
|
-
const storeUniforms =
|
|
15998
|
-
const hmrVersion =
|
|
15261
|
+
const storeUniforms = usePrimaryThree((s) => s.uniforms);
|
|
15262
|
+
const hmrVersion = usePrimaryThree((s) => s._hmrVersion);
|
|
15999
15263
|
const readerDep = isReader ? storeUniforms : null;
|
|
16000
15264
|
const creatorDep = isReader ? null : hmrVersion;
|
|
16001
15265
|
const uniforms = useMemo(() => {
|
|
@@ -16056,6 +15320,7 @@ function useUniforms(creatorOrScope, scope) {
|
|
|
16056
15320
|
return { ...uniforms, removeUniforms: removeUniforms2, clearUniforms, rebuildUniforms };
|
|
16057
15321
|
}
|
|
16058
15322
|
function rebuildAllUniforms(store, scope) {
|
|
15323
|
+
store = store.getState().primaryStore ?? store;
|
|
16059
15324
|
store.setState((state) => {
|
|
16060
15325
|
let newUniforms = {};
|
|
16061
15326
|
if (scope && scope !== "root") {
|
|
@@ -16184,7 +15449,7 @@ function useUniform(name, value) {
|
|
|
16184
15449
|
|
|
16185
15450
|
const isTSLNode = (value) => value !== null && typeof value === "object" && ("uuid" in value || "nodeType" in value);
|
|
16186
15451
|
function useNodes(creatorOrScope, scope) {
|
|
16187
|
-
const store =
|
|
15452
|
+
const store = usePrimaryStore();
|
|
16188
15453
|
const removeNodes2 = useCallback(
|
|
16189
15454
|
(names, targetScope) => {
|
|
16190
15455
|
const nameArray = Array.isArray(names) ? names : [names];
|
|
@@ -16241,8 +15506,8 @@ function useNodes(creatorOrScope, scope) {
|
|
|
16241
15506
|
[store]
|
|
16242
15507
|
);
|
|
16243
15508
|
const isReader = creatorOrScope === void 0 || typeof creatorOrScope === "string";
|
|
16244
|
-
const storeNodes =
|
|
16245
|
-
const hmrVersion =
|
|
15509
|
+
const storeNodes = usePrimaryThree((s) => s.nodes);
|
|
15510
|
+
const hmrVersion = usePrimaryThree((s) => s._hmrVersion);
|
|
16246
15511
|
const scopeDep = typeof creatorOrScope === "string" ? creatorOrScope : scope;
|
|
16247
15512
|
const readerDep = isReader ? storeNodes : null;
|
|
16248
15513
|
const creatorDep = isReader ? null : hmrVersion;
|
|
@@ -16301,6 +15566,7 @@ function useNodes(creatorOrScope, scope) {
|
|
|
16301
15566
|
return { ...nodes, removeNodes: removeNodes2, clearNodes, rebuildNodes };
|
|
16302
15567
|
}
|
|
16303
15568
|
function rebuildAllNodes(store, scope) {
|
|
15569
|
+
store = store.getState().primaryStore ?? store;
|
|
16304
15570
|
store.setState((state) => {
|
|
16305
15571
|
let newNodes = state.nodes;
|
|
16306
15572
|
if (scope && scope !== "root") {
|
|
@@ -16345,18 +15611,364 @@ function clearRootNodes(set) {
|
|
|
16345
15611
|
});
|
|
16346
15612
|
}
|
|
16347
15613
|
function useLocalNodes(creator) {
|
|
16348
|
-
const store =
|
|
16349
|
-
const uniforms =
|
|
16350
|
-
const nodes =
|
|
16351
|
-
const textures =
|
|
16352
|
-
const hmrVersion =
|
|
15614
|
+
const store = usePrimaryStore();
|
|
15615
|
+
const uniforms = usePrimaryThree((s) => s.uniforms);
|
|
15616
|
+
const nodes = usePrimaryThree((s) => s.nodes);
|
|
15617
|
+
const textures = usePrimaryThree((s) => s.textures);
|
|
15618
|
+
const hmrVersion = usePrimaryThree((s) => s._hmrVersion);
|
|
16353
15619
|
return useMemo(() => {
|
|
16354
15620
|
const wrappedState = createLazyCreatorState(store.getState());
|
|
16355
15621
|
return creator(wrappedState);
|
|
16356
15622
|
}, [store, creator, uniforms, nodes, textures, hmrVersion]);
|
|
16357
15623
|
}
|
|
16358
15624
|
|
|
16359
|
-
|
|
15625
|
+
const isBufferLike = (value) => {
|
|
15626
|
+
if (value === null || typeof value !== "object") return false;
|
|
15627
|
+
if (ArrayBuffer.isView(value)) return true;
|
|
15628
|
+
if ("isBufferAttribute" in value) return true;
|
|
15629
|
+
if ("uuid" in value || "nodeType" in value) return true;
|
|
15630
|
+
return false;
|
|
15631
|
+
};
|
|
15632
|
+
const disposeBuffer = (buffer) => {
|
|
15633
|
+
if (buffer === null || typeof buffer !== "object") return;
|
|
15634
|
+
if ("dispose" in buffer && typeof buffer.dispose === "function") {
|
|
15635
|
+
buffer.dispose();
|
|
15636
|
+
}
|
|
15637
|
+
};
|
|
15638
|
+
function useBuffers(creatorOrScope, scope) {
|
|
15639
|
+
const store = usePrimaryStore();
|
|
15640
|
+
const removeBuffers = useCallback(
|
|
15641
|
+
(names, targetScope) => {
|
|
15642
|
+
const nameArray = Array.isArray(names) ? names : [names];
|
|
15643
|
+
store.setState((state) => {
|
|
15644
|
+
if (targetScope) {
|
|
15645
|
+
const currentScope = { ...state.buffers[targetScope] };
|
|
15646
|
+
for (const name of nameArray) delete currentScope[name];
|
|
15647
|
+
return { buffers: { ...state.buffers, [targetScope]: currentScope } };
|
|
15648
|
+
}
|
|
15649
|
+
const buffers2 = { ...state.buffers };
|
|
15650
|
+
for (const name of nameArray) if (isBufferLike(buffers2[name])) delete buffers2[name];
|
|
15651
|
+
return { buffers: buffers2 };
|
|
15652
|
+
});
|
|
15653
|
+
},
|
|
15654
|
+
[store]
|
|
15655
|
+
);
|
|
15656
|
+
const clearBuffers = useCallback(
|
|
15657
|
+
(targetScope) => {
|
|
15658
|
+
store.setState((state) => {
|
|
15659
|
+
if (targetScope && targetScope !== "root") {
|
|
15660
|
+
const { [targetScope]: _, ...rest } = state.buffers;
|
|
15661
|
+
return { buffers: rest };
|
|
15662
|
+
}
|
|
15663
|
+
if (targetScope === "root") {
|
|
15664
|
+
const buffers2 = {};
|
|
15665
|
+
for (const [key, value] of Object.entries(state.buffers)) {
|
|
15666
|
+
if (!isBufferLike(value)) buffers2[key] = value;
|
|
15667
|
+
}
|
|
15668
|
+
return { buffers: buffers2 };
|
|
15669
|
+
}
|
|
15670
|
+
return { buffers: {} };
|
|
15671
|
+
});
|
|
15672
|
+
},
|
|
15673
|
+
[store]
|
|
15674
|
+
);
|
|
15675
|
+
const rebuildBuffers = useCallback(
|
|
15676
|
+
(targetScope) => {
|
|
15677
|
+
store.setState((state) => {
|
|
15678
|
+
let newBuffers = state.buffers;
|
|
15679
|
+
if (targetScope && targetScope !== "root") {
|
|
15680
|
+
const { [targetScope]: _, ...rest } = state.buffers;
|
|
15681
|
+
newBuffers = rest;
|
|
15682
|
+
} else if (targetScope === "root") {
|
|
15683
|
+
newBuffers = {};
|
|
15684
|
+
for (const [key, value] of Object.entries(state.buffers)) {
|
|
15685
|
+
if (!isBufferLike(value)) newBuffers[key] = value;
|
|
15686
|
+
}
|
|
15687
|
+
} else {
|
|
15688
|
+
newBuffers = {};
|
|
15689
|
+
}
|
|
15690
|
+
return { buffers: newBuffers, _hmrVersion: state._hmrVersion + 1 };
|
|
15691
|
+
});
|
|
15692
|
+
},
|
|
15693
|
+
[store]
|
|
15694
|
+
);
|
|
15695
|
+
const disposeBuffers = useCallback(
|
|
15696
|
+
(names, targetScope) => {
|
|
15697
|
+
const nameArray = Array.isArray(names) ? names : [names];
|
|
15698
|
+
const state = store.getState();
|
|
15699
|
+
for (const name of nameArray) {
|
|
15700
|
+
const buffer = targetScope ? state.buffers[targetScope]?.[name] : state.buffers[name];
|
|
15701
|
+
if (buffer && isBufferLike(buffer)) {
|
|
15702
|
+
disposeBuffer(buffer);
|
|
15703
|
+
}
|
|
15704
|
+
}
|
|
15705
|
+
removeBuffers(names, targetScope);
|
|
15706
|
+
},
|
|
15707
|
+
[store, removeBuffers]
|
|
15708
|
+
);
|
|
15709
|
+
const isReader = creatorOrScope === void 0 || typeof creatorOrScope === "string";
|
|
15710
|
+
const storeBuffers = usePrimaryThree((s) => s.buffers);
|
|
15711
|
+
const hmrVersion = usePrimaryThree((s) => s._hmrVersion);
|
|
15712
|
+
const scopeDep = typeof creatorOrScope === "string" ? creatorOrScope : scope;
|
|
15713
|
+
const readerDep = isReader ? storeBuffers : null;
|
|
15714
|
+
const creatorDep = isReader ? null : hmrVersion;
|
|
15715
|
+
const buffers = useMemo(() => {
|
|
15716
|
+
if (creatorOrScope === void 0) {
|
|
15717
|
+
return storeBuffers;
|
|
15718
|
+
}
|
|
15719
|
+
if (typeof creatorOrScope === "string") {
|
|
15720
|
+
const scopeData = storeBuffers[creatorOrScope];
|
|
15721
|
+
if (scopeData && !isBufferLike(scopeData)) return scopeData;
|
|
15722
|
+
return {};
|
|
15723
|
+
}
|
|
15724
|
+
const state = store.getState();
|
|
15725
|
+
const set = store.setState;
|
|
15726
|
+
const creator = creatorOrScope;
|
|
15727
|
+
const wrappedState = createLazyCreatorState(state);
|
|
15728
|
+
const created = creator(wrappedState);
|
|
15729
|
+
const result = {};
|
|
15730
|
+
let hasNewBuffers = false;
|
|
15731
|
+
if (scope) {
|
|
15732
|
+
const currentScope = state.buffers[scope] ?? {};
|
|
15733
|
+
for (const [name, buffer] of Object.entries(created)) {
|
|
15734
|
+
if (currentScope[name]) {
|
|
15735
|
+
result[name] = currentScope[name];
|
|
15736
|
+
} else {
|
|
15737
|
+
if ("setName" in buffer && typeof buffer.setName === "function") {
|
|
15738
|
+
buffer.setName(`${scope}.${name}`);
|
|
15739
|
+
}
|
|
15740
|
+
result[name] = buffer;
|
|
15741
|
+
hasNewBuffers = true;
|
|
15742
|
+
}
|
|
15743
|
+
}
|
|
15744
|
+
if (hasNewBuffers) {
|
|
15745
|
+
set((s) => ({
|
|
15746
|
+
buffers: {
|
|
15747
|
+
...s.buffers,
|
|
15748
|
+
[scope]: { ...s.buffers[scope], ...result }
|
|
15749
|
+
}
|
|
15750
|
+
}));
|
|
15751
|
+
}
|
|
15752
|
+
return result;
|
|
15753
|
+
}
|
|
15754
|
+
for (const [name, buffer] of Object.entries(created)) {
|
|
15755
|
+
const existing = state.buffers[name];
|
|
15756
|
+
if (existing && isBufferLike(existing)) {
|
|
15757
|
+
result[name] = existing;
|
|
15758
|
+
} else {
|
|
15759
|
+
if ("setName" in buffer && typeof buffer.setName === "function") {
|
|
15760
|
+
buffer.setName(name);
|
|
15761
|
+
}
|
|
15762
|
+
result[name] = buffer;
|
|
15763
|
+
hasNewBuffers = true;
|
|
15764
|
+
}
|
|
15765
|
+
}
|
|
15766
|
+
if (hasNewBuffers) {
|
|
15767
|
+
set((s) => ({ buffers: { ...s.buffers, ...result } }));
|
|
15768
|
+
}
|
|
15769
|
+
return result;
|
|
15770
|
+
}, [store, scopeDep, readerDep, creatorDep]);
|
|
15771
|
+
return { ...buffers, removeBuffers, clearBuffers, rebuildBuffers, disposeBuffers };
|
|
15772
|
+
}
|
|
15773
|
+
function rebuildAllBuffers(store, scope) {
|
|
15774
|
+
store = store.getState().primaryStore ?? store;
|
|
15775
|
+
store.setState((state) => {
|
|
15776
|
+
let newBuffers = state.buffers;
|
|
15777
|
+
if (scope && scope !== "root") {
|
|
15778
|
+
const { [scope]: _, ...rest } = state.buffers;
|
|
15779
|
+
newBuffers = rest;
|
|
15780
|
+
} else if (scope === "root") {
|
|
15781
|
+
newBuffers = {};
|
|
15782
|
+
for (const [key, value] of Object.entries(state.buffers)) {
|
|
15783
|
+
if (!isBufferLike(value)) newBuffers[key] = value;
|
|
15784
|
+
}
|
|
15785
|
+
} else {
|
|
15786
|
+
newBuffers = {};
|
|
15787
|
+
}
|
|
15788
|
+
return { buffers: newBuffers, _hmrVersion: state._hmrVersion + 1 };
|
|
15789
|
+
});
|
|
15790
|
+
}
|
|
15791
|
+
|
|
15792
|
+
const isStorageLike = (value) => {
|
|
15793
|
+
if (value === null || typeof value !== "object") return false;
|
|
15794
|
+
if ("isTexture" in value) return true;
|
|
15795
|
+
if ("isData3DTexture" in value) return true;
|
|
15796
|
+
if ("uuid" in value || "nodeType" in value) return true;
|
|
15797
|
+
return false;
|
|
15798
|
+
};
|
|
15799
|
+
const disposeStorage = (storage) => {
|
|
15800
|
+
if (storage === null || typeof storage !== "object") return;
|
|
15801
|
+
if ("dispose" in storage && typeof storage.dispose === "function") {
|
|
15802
|
+
storage.dispose();
|
|
15803
|
+
}
|
|
15804
|
+
};
|
|
15805
|
+
function useGPUStorage(creatorOrScope, scope) {
|
|
15806
|
+
const store = usePrimaryStore();
|
|
15807
|
+
const removeStorage = useCallback(
|
|
15808
|
+
(names, targetScope) => {
|
|
15809
|
+
const nameArray = Array.isArray(names) ? names : [names];
|
|
15810
|
+
store.setState((state) => {
|
|
15811
|
+
if (targetScope) {
|
|
15812
|
+
const currentScope = { ...state.gpuStorage[targetScope] };
|
|
15813
|
+
for (const name of nameArray) delete currentScope[name];
|
|
15814
|
+
return { gpuStorage: { ...state.gpuStorage, [targetScope]: currentScope } };
|
|
15815
|
+
}
|
|
15816
|
+
const gpuStorage2 = { ...state.gpuStorage };
|
|
15817
|
+
for (const name of nameArray) if (isStorageLike(gpuStorage2[name])) delete gpuStorage2[name];
|
|
15818
|
+
return { gpuStorage: gpuStorage2 };
|
|
15819
|
+
});
|
|
15820
|
+
},
|
|
15821
|
+
[store]
|
|
15822
|
+
);
|
|
15823
|
+
const clearStorage = useCallback(
|
|
15824
|
+
(targetScope) => {
|
|
15825
|
+
store.setState((state) => {
|
|
15826
|
+
if (targetScope && targetScope !== "root") {
|
|
15827
|
+
const { [targetScope]: _, ...rest } = state.gpuStorage;
|
|
15828
|
+
return { gpuStorage: rest };
|
|
15829
|
+
}
|
|
15830
|
+
if (targetScope === "root") {
|
|
15831
|
+
const gpuStorage2 = {};
|
|
15832
|
+
for (const [key, value] of Object.entries(state.gpuStorage)) {
|
|
15833
|
+
if (!isStorageLike(value)) gpuStorage2[key] = value;
|
|
15834
|
+
}
|
|
15835
|
+
return { gpuStorage: gpuStorage2 };
|
|
15836
|
+
}
|
|
15837
|
+
return { gpuStorage: {} };
|
|
15838
|
+
});
|
|
15839
|
+
},
|
|
15840
|
+
[store]
|
|
15841
|
+
);
|
|
15842
|
+
const rebuildStorage = useCallback(
|
|
15843
|
+
(targetScope) => {
|
|
15844
|
+
store.setState((state) => {
|
|
15845
|
+
let newStorage = state.gpuStorage;
|
|
15846
|
+
if (targetScope && targetScope !== "root") {
|
|
15847
|
+
const { [targetScope]: _, ...rest } = state.gpuStorage;
|
|
15848
|
+
newStorage = rest;
|
|
15849
|
+
} else if (targetScope === "root") {
|
|
15850
|
+
newStorage = {};
|
|
15851
|
+
for (const [key, value] of Object.entries(state.gpuStorage)) {
|
|
15852
|
+
if (!isStorageLike(value)) newStorage[key] = value;
|
|
15853
|
+
}
|
|
15854
|
+
} else {
|
|
15855
|
+
newStorage = {};
|
|
15856
|
+
}
|
|
15857
|
+
return { gpuStorage: newStorage, _hmrVersion: state._hmrVersion + 1 };
|
|
15858
|
+
});
|
|
15859
|
+
},
|
|
15860
|
+
[store]
|
|
15861
|
+
);
|
|
15862
|
+
const disposeStorageFn = useCallback(
|
|
15863
|
+
(names, targetScope) => {
|
|
15864
|
+
const nameArray = Array.isArray(names) ? names : [names];
|
|
15865
|
+
const state = store.getState();
|
|
15866
|
+
for (const name of nameArray) {
|
|
15867
|
+
const storage = targetScope ? state.gpuStorage[targetScope]?.[name] : state.gpuStorage[name];
|
|
15868
|
+
if (storage && isStorageLike(storage)) {
|
|
15869
|
+
disposeStorage(storage);
|
|
15870
|
+
}
|
|
15871
|
+
}
|
|
15872
|
+
removeStorage(names, targetScope);
|
|
15873
|
+
},
|
|
15874
|
+
[store, removeStorage]
|
|
15875
|
+
);
|
|
15876
|
+
const isReader = creatorOrScope === void 0 || typeof creatorOrScope === "string";
|
|
15877
|
+
const storeStorage = usePrimaryThree((s) => s.gpuStorage);
|
|
15878
|
+
const hmrVersion = usePrimaryThree((s) => s._hmrVersion);
|
|
15879
|
+
const scopeDep = typeof creatorOrScope === "string" ? creatorOrScope : scope;
|
|
15880
|
+
const readerDep = isReader ? storeStorage : null;
|
|
15881
|
+
const creatorDep = isReader ? null : hmrVersion;
|
|
15882
|
+
const gpuStorage = useMemo(() => {
|
|
15883
|
+
if (creatorOrScope === void 0) {
|
|
15884
|
+
return storeStorage;
|
|
15885
|
+
}
|
|
15886
|
+
if (typeof creatorOrScope === "string") {
|
|
15887
|
+
const scopeData = storeStorage[creatorOrScope];
|
|
15888
|
+
if (scopeData && !isStorageLike(scopeData)) return scopeData;
|
|
15889
|
+
return {};
|
|
15890
|
+
}
|
|
15891
|
+
const state = store.getState();
|
|
15892
|
+
const set = store.setState;
|
|
15893
|
+
const creator = creatorOrScope;
|
|
15894
|
+
const wrappedState = createLazyCreatorState(state);
|
|
15895
|
+
const created = creator(wrappedState);
|
|
15896
|
+
const result = {};
|
|
15897
|
+
let hasNewStorage = false;
|
|
15898
|
+
if (scope) {
|
|
15899
|
+
const currentScope = state.gpuStorage[scope] ?? {};
|
|
15900
|
+
for (const [name, storage] of Object.entries(created)) {
|
|
15901
|
+
if (currentScope[name]) {
|
|
15902
|
+
result[name] = currentScope[name];
|
|
15903
|
+
} else {
|
|
15904
|
+
if ("setName" in storage && typeof storage.setName === "function") {
|
|
15905
|
+
storage.setName(`${scope}.${name}`);
|
|
15906
|
+
}
|
|
15907
|
+
if ("name" in storage && typeof storage.name === "string") {
|
|
15908
|
+
storage.name = `${scope}.${name}`;
|
|
15909
|
+
}
|
|
15910
|
+
result[name] = storage;
|
|
15911
|
+
hasNewStorage = true;
|
|
15912
|
+
}
|
|
15913
|
+
}
|
|
15914
|
+
if (hasNewStorage) {
|
|
15915
|
+
set((s) => ({
|
|
15916
|
+
gpuStorage: {
|
|
15917
|
+
...s.gpuStorage,
|
|
15918
|
+
[scope]: { ...s.gpuStorage[scope], ...result }
|
|
15919
|
+
}
|
|
15920
|
+
}));
|
|
15921
|
+
}
|
|
15922
|
+
return result;
|
|
15923
|
+
}
|
|
15924
|
+
for (const [name, storage] of Object.entries(created)) {
|
|
15925
|
+
const existing = state.gpuStorage[name];
|
|
15926
|
+
if (existing && isStorageLike(existing)) {
|
|
15927
|
+
result[name] = existing;
|
|
15928
|
+
} else {
|
|
15929
|
+
if ("setName" in storage && typeof storage.setName === "function") {
|
|
15930
|
+
storage.setName(name);
|
|
15931
|
+
}
|
|
15932
|
+
if ("name" in storage && typeof storage.name === "string") {
|
|
15933
|
+
storage.name = name;
|
|
15934
|
+
}
|
|
15935
|
+
result[name] = storage;
|
|
15936
|
+
hasNewStorage = true;
|
|
15937
|
+
}
|
|
15938
|
+
}
|
|
15939
|
+
if (hasNewStorage) {
|
|
15940
|
+
set((s) => ({ gpuStorage: { ...s.gpuStorage, ...result } }));
|
|
15941
|
+
}
|
|
15942
|
+
return result;
|
|
15943
|
+
}, [store, scopeDep, readerDep, creatorDep]);
|
|
15944
|
+
return {
|
|
15945
|
+
...gpuStorage,
|
|
15946
|
+
removeStorage,
|
|
15947
|
+
clearStorage,
|
|
15948
|
+
rebuildStorage,
|
|
15949
|
+
disposeStorage: disposeStorageFn
|
|
15950
|
+
};
|
|
15951
|
+
}
|
|
15952
|
+
function rebuildAllStorage(store, scope) {
|
|
15953
|
+
store = store.getState().primaryStore ?? store;
|
|
15954
|
+
store.setState((state) => {
|
|
15955
|
+
let newStorage = state.gpuStorage;
|
|
15956
|
+
if (scope && scope !== "root") {
|
|
15957
|
+
const { [scope]: _, ...rest } = state.gpuStorage;
|
|
15958
|
+
newStorage = rest;
|
|
15959
|
+
} else if (scope === "root") {
|
|
15960
|
+
newStorage = {};
|
|
15961
|
+
for (const [key, value] of Object.entries(state.gpuStorage)) {
|
|
15962
|
+
if (!isStorageLike(value)) newStorage[key] = value;
|
|
15963
|
+
}
|
|
15964
|
+
} else {
|
|
15965
|
+
newStorage = {};
|
|
15966
|
+
}
|
|
15967
|
+
return { gpuStorage: newStorage, _hmrVersion: state._hmrVersion + 1 };
|
|
15968
|
+
});
|
|
15969
|
+
}
|
|
15970
|
+
|
|
15971
|
+
function useRenderPipeline(mainCB, setupCB) {
|
|
16360
15972
|
const store = useStore();
|
|
16361
15973
|
const { scene, camera, renderer, isLegacy } = useThree();
|
|
16362
15974
|
const callbacksRanRef = useRef(false);
|
|
@@ -16375,7 +15987,7 @@ function usePostProcessing(mainCB, setupCB) {
|
|
|
16375
15987
|
}, [store]);
|
|
16376
15988
|
const reset = useCallback(() => {
|
|
16377
15989
|
store.setState({
|
|
16378
|
-
|
|
15990
|
+
renderPipeline: null,
|
|
16379
15991
|
passes: {}
|
|
16380
15992
|
});
|
|
16381
15993
|
callbacksRanRef.current = false;
|
|
@@ -16388,13 +16000,13 @@ function usePostProcessing(mainCB, setupCB) {
|
|
|
16388
16000
|
}, []);
|
|
16389
16001
|
useLayoutEffect(() => {
|
|
16390
16002
|
if (isLegacy) {
|
|
16391
|
-
throw new Error("
|
|
16003
|
+
throw new Error("useRenderPipeline is only available with WebGPU renderer. Set renderer prop on Canvas.");
|
|
16392
16004
|
}
|
|
16393
16005
|
if (!renderer || !scene || !camera) return;
|
|
16394
16006
|
const state = store.getState();
|
|
16395
16007
|
const set = store.setState;
|
|
16396
16008
|
try {
|
|
16397
|
-
let pp = state.
|
|
16009
|
+
let pp = state.renderPipeline;
|
|
16398
16010
|
let currentPasses = { ...state.passes };
|
|
16399
16011
|
let justCreatedPP = false;
|
|
16400
16012
|
if (!pp) {
|
|
@@ -16411,7 +16023,7 @@ function usePostProcessing(mainCB, setupCB) {
|
|
|
16411
16023
|
}
|
|
16412
16024
|
currentPasses.scenePass = scenePass;
|
|
16413
16025
|
if (!pp.outputNode || justCreatedPP) pp.outputNode = scenePass;
|
|
16414
|
-
set({
|
|
16026
|
+
set({ renderPipeline: pp, passes: currentPasses });
|
|
16415
16027
|
const shouldRunCallbacks = justCreatedPP || !callbacksRanRef.current || !cacheValid;
|
|
16416
16028
|
if (shouldRunCallbacks) {
|
|
16417
16029
|
if (setupCBRef.current) {
|
|
@@ -16433,22 +16045,22 @@ function usePostProcessing(mainCB, setupCB) {
|
|
|
16433
16045
|
callbacksRanRef.current = true;
|
|
16434
16046
|
}
|
|
16435
16047
|
} catch (error) {
|
|
16436
|
-
console.error("[
|
|
16048
|
+
console.error("[useRenderPipeline] Setup error:", error);
|
|
16437
16049
|
}
|
|
16438
16050
|
}, [store, renderer, scene, camera, isLegacy, rebuildVersion]);
|
|
16439
16051
|
const passes = useThree((s) => s.passes);
|
|
16440
|
-
const
|
|
16052
|
+
const renderPipeline = useThree((s) => s.renderPipeline);
|
|
16441
16053
|
return {
|
|
16442
16054
|
passes,
|
|
16443
|
-
|
|
16055
|
+
renderPipeline,
|
|
16444
16056
|
clearPasses,
|
|
16445
16057
|
reset,
|
|
16446
16058
|
rebuild,
|
|
16447
|
-
// isReady indicates if
|
|
16448
|
-
isReady:
|
|
16059
|
+
// isReady indicates if RenderPipeline is configured and ready for rendering
|
|
16060
|
+
isReady: renderPipeline !== null
|
|
16449
16061
|
};
|
|
16450
16062
|
}
|
|
16451
16063
|
|
|
16452
16064
|
extend(THREE);
|
|
16453
16065
|
|
|
16454
|
-
export { Block, Canvas, Environment, EnvironmentCube, EnvironmentMap, EnvironmentPortal, ErrorBoundary, FROM_REF, IsObject, ONCE, Portal, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS,
|
|
16066
|
+
export { Block, Canvas, Environment, EnvironmentCube, EnvironmentMap, EnvironmentPortal, ErrorBoundary, FROM_REF, IsObject, ONCE, Portal, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS, Texture, _roots, act, addAfterEffect, addEffect, addTail, advance, applyProps, attach, buildGraph, calculateDpr, clearNodeScope, clearRootNodes, clearRootUniforms, clearScope, context, createEvents, createPointerEvents, createPortal, createRoot, createScopedStore, createStore, createTextureOperations, detach, diffProps, dispose, createPointerEvents as events, extend, findInitialRoot, flushSync, fromRef, getInstanceProps, getPrimary, getPrimaryIds, getRootState, getUuidPrefix, hasConstructor, hasPrimary, invalidate, invalidateInstance, is, isColorRepresentation, isCopyable, isFromRef, isObject3D, isOnce, isOrthographicCamera, isRef, isRenderer, isTexture, isVectorLike, once, prepare, presetsObj, rebuildAllBuffers, rebuildAllNodes, rebuildAllStorage, rebuildAllUniforms, reconciler, registerPrimary, removeInteractivity, removeNodes, removeUniforms, resolve, unmountComponentAtNode, unregisterPrimary, updateCamera, updateFrustum, useBridge, useBuffers, useEnvironment, useFrame, useGPUStorage, useGraph, useInstanceHandle, useIsomorphicLayoutEffect, useLoader, useLocalNodes, useMutableCallback, useNodes, useRenderPipeline, useRenderTarget, useStore, useTexture, useTextures, useThree, useUniform, useUniforms, waitForPrimary };
|