@react-three/fiber 8.0.17 → 8.0.20

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.
@@ -164,7 +164,7 @@ function dispose(obj) {
164
164
  function prepare(object, state) {
165
165
  const instance = object;
166
166
 
167
- if (state != null && state.primitive || !instance.__r3f) {
167
+ if (!instance.__r3f) {
168
168
  instance.__r3f = {
169
169
  type: '',
170
170
  root: null,
@@ -173,7 +173,7 @@ function prepare(object, state) {
173
173
  eventCount: 0,
174
174
  handlers: {},
175
175
  objects: [],
176
- parent: null,
176
+ parents: [],
177
177
  ...state
178
178
  };
179
179
  }
@@ -287,10 +287,10 @@ function diffProps(instance, {
287
287
  } // This function applies a set of changes to the instance
288
288
 
289
289
  function applyProps$1(instance, data) {
290
- var _instance$__r3f3, _root$getState;
290
+ var _instance$__r3f3, _root$getState, _localState$parents;
291
291
 
292
292
  // Filter equals, events and reserved props
293
- const localState = (_instance$__r3f3 = instance == null ? void 0 : instance.__r3f) != null ? _instance$__r3f3 : {};
293
+ const localState = (_instance$__r3f3 = instance.__r3f) != null ? _instance$__r3f3 : {};
294
294
  const root = localState.root;
295
295
  const rootState = (_root$getState = root == null ? void 0 : root.getState == null ? void 0 : root.getState()) != null ? _root$getState : {};
296
296
  const {
@@ -362,7 +362,7 @@ function applyProps$1(instance, data) {
362
362
  // Auto-convert sRGB colors
363
363
  // https://github.com/pmndrs/react-three-fiber/issues/344
364
364
 
365
- const supportsColorManagement = THREE__namespace.ColorManagement;
365
+ const supportsColorManagement = ('ColorManagement' in THREE__namespace);
366
366
  if (!supportsColorManagement && !rootState.linear && isColor) targetProp.convertSRGBToLinear();
367
367
  } // Else, just overwrite the value
368
368
 
@@ -378,7 +378,7 @@ function applyProps$1(instance, data) {
378
378
  invalidateInstance(instance);
379
379
  });
380
380
 
381
- if (localState.parent && rootState.internal && instance.raycast && prevHandlers !== localState.eventCount) {
381
+ if ((_localState$parents = localState.parents) != null && _localState$parents.length && rootState.internal && instance.raycast && prevHandlers !== localState.eventCount) {
382
382
  // Pre-emptively remove the instance from the interaction manager
383
383
  const index = rootState.internal.interaction.indexOf(instance);
384
384
  if (index > -1) rootState.internal.interaction.splice(index, 1); // Add the instance to the interaction manager only when it has handlers
@@ -418,6 +418,15 @@ function updateCamera(camera, size) {
418
418
  camera.updateMatrixWorld();
419
419
  }
420
420
  }
421
+ /**
422
+ * Safely sets a deeply-nested value on an object.
423
+ */
424
+
425
+ function setDeep(obj, value, keys) {
426
+ const key = keys.pop();
427
+ const target = keys.reduce((acc, key) => acc[key], obj);
428
+ return target[key] = value;
429
+ }
421
430
 
422
431
  function makeId(event) {
423
432
  return (event.eventObject || event.object).uuid + '/' + event.index + event.instanceId;
@@ -471,7 +480,6 @@ function releaseInternalPointerCapture(capturedMap, obj, captures, pointerId) {
471
480
 
472
481
  function removeInteractivity(store, object) {
473
482
  const {
474
- events,
475
483
  internal
476
484
  } = store.getState(); // Removes every trace of an object from the data store
477
485
 
@@ -698,14 +706,14 @@ function createEvents(store) {
698
706
  return intersections;
699
707
  }
700
708
 
701
- function cancelPointer(hits) {
709
+ function cancelPointer(intersections) {
702
710
  const {
703
711
  internal
704
712
  } = store.getState();
705
713
  Array.from(internal.hovered.values()).forEach(hoveredObj => {
706
714
  // When no objects were hit or the the hovered object wasn't found underneath the cursor
707
715
  // we call onPointerOut and delete the object from the hovered-elements map
708
- if (!hits.length || !hits.find(hit => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId)) {
716
+ if (!intersections.length || !intersections.find(hit => hit.object === hoveredObj.object && hit.index === hoveredObj.index && hit.instanceId === hoveredObj.instanceId)) {
709
717
  const eventObject = hoveredObj.eventObject;
710
718
  const instance = eventObject.__r3f;
711
719
  const handlers = instance == null ? void 0 : instance.handlers;
@@ -714,7 +722,7 @@ function createEvents(store) {
714
722
  if (instance != null && instance.eventCount) {
715
723
  // Clear out intersects, they are outdated by now
716
724
  const data = { ...hoveredObj,
717
- intersections: hits || []
725
+ intersections
718
726
  };
719
727
  handlers.onPointerOut == null ? void 0 : handlers.onPointerOut(data);
720
728
  handlers.onPointerLeave == null ? void 0 : handlers.onPointerLeave(data);
@@ -884,7 +892,7 @@ function createRenderer(roots, getEventPriority) {
884
892
  type,
885
893
  root,
886
894
  attach,
887
- // TODO: Figure out what this is for
895
+ // Save args in case we need to reconstruct later for HMR
888
896
  memoizedProps: {
889
897
  args
890
898
  }
@@ -905,10 +913,9 @@ function createRenderer(roots, getEventPriority) {
905
913
  if (child) {
906
914
  var _child$__r3f, _parentInstance$__r3f;
907
915
 
908
- // The attach attribute implies that the object attaches itself on the parent
909
- if ((_child$__r3f = child.__r3f) != null && _child$__r3f.attach) {
910
- attach(parentInstance, child, child.__r3f.attach);
911
- } else if (child.isObject3D && parentInstance.isObject3D) {
916
+ // The attach attribute implies that the object attaches itself on the parent.
917
+ // That is handled at commit to avoid duplication during Suspense
918
+ if (!((_child$__r3f = child.__r3f) != null && _child$__r3f.attach) && child.isObject3D && parentInstance.isObject3D) {
912
919
  // add in the usual parent-child way
913
920
  parentInstance.add(child);
914
921
  added = true;
@@ -918,7 +925,9 @@ function createRenderer(roots, getEventPriority) {
918
925
 
919
926
  if (!added) (_parentInstance$__r3f = parentInstance.__r3f) == null ? void 0 : _parentInstance$__r3f.objects.push(child);
920
927
  if (!child.__r3f) prepare(child, {});
921
- child.__r3f.parent = parentInstance;
928
+
929
+ child.__r3f.parents.push(parentInstance);
930
+
922
931
  updateInstance(child);
923
932
  invalidateInstance(child);
924
933
  }
@@ -930,9 +939,7 @@ function createRenderer(roots, getEventPriority) {
930
939
  if (child) {
931
940
  var _child$__r3f2, _parentInstance$__r3f2;
932
941
 
933
- if ((_child$__r3f2 = child.__r3f) != null && _child$__r3f2.attach) {
934
- attach(parentInstance, child, child.__r3f.attach);
935
- } else if (child.isObject3D && parentInstance.isObject3D) {
942
+ if (!((_child$__r3f2 = child.__r3f) != null && _child$__r3f2.attach) && child.isObject3D && parentInstance.isObject3D) {
936
943
  child.parent = parentInstance;
937
944
  child.dispatchEvent({
938
945
  type: 'added'
@@ -945,7 +952,9 @@ function createRenderer(roots, getEventPriority) {
945
952
 
946
953
  if (!added) (_parentInstance$__r3f2 = parentInstance.__r3f) == null ? void 0 : _parentInstance$__r3f2.objects.push(child);
947
954
  if (!child.__r3f) prepare(child, {});
948
- child.__r3f.parent = parentInstance;
955
+
956
+ child.__r3f.parents.push(parentInstance);
957
+
949
958
  updateInstance(child);
950
959
  invalidateInstance(child);
951
960
  }
@@ -960,7 +969,7 @@ function createRenderer(roots, getEventPriority) {
960
969
  var _parentInstance$__r3f3, _child$__r3f3, _child$__r3f5;
961
970
 
962
971
  // Clear the parent reference
963
- if (child.__r3f) child.__r3f.parent = null; // Remove child from the parents objects
972
+ if (child.__r3f) child.__r3f.parents = child.__r3f.parents.filter(parent => parent !== parentInstance); // Remove child from the parents objects
964
973
 
965
974
  if ((_parentInstance$__r3f3 = parentInstance.__r3f) != null && _parentInstance$__r3f3.objects) parentInstance.__r3f.objects = parentInstance.__r3f.objects.filter(x => x !== child); // Remove attachment
966
975
 
@@ -974,7 +983,7 @@ function createRenderer(roots, getEventPriority) {
974
983
  if ((_child$__r3f4 = child.__r3f) != null && _child$__r3f4.root) {
975
984
  removeInteractivity(child.__r3f.root, child);
976
985
  }
977
- } // Allow objects to bail out of recursive dispose alltogether by passing dispose={null}
986
+ } // Allow objects to bail out of recursive dispose altogether by passing dispose={null}
978
987
  // Never dispose of primitives because their state may be kept outside of React!
979
988
  // In order for an object to be able to dispose it has to have
980
989
  // - a dispose method,
@@ -1021,11 +1030,11 @@ function createRenderer(roots, getEventPriority) {
1021
1030
  }
1022
1031
 
1023
1032
  function switchInstance(instance, type, newProps, fiber) {
1024
- var _instance$__r3f, _instance$__r3f2;
1033
+ var _instance$__r3f, _newInstance$__r3f;
1025
1034
 
1026
- const parent = (_instance$__r3f = instance.__r3f) == null ? void 0 : _instance$__r3f.parent;
1027
- if (!parent) return;
1028
- const newInstance = createInstance(type, newProps, (_instance$__r3f2 = instance.__r3f) == null ? void 0 : _instance$__r3f2.root); // https://github.com/pmndrs/react-three-fiber/issues/1348
1035
+ const parents = (_instance$__r3f = instance.__r3f) == null ? void 0 : _instance$__r3f.parents;
1036
+ if (!(parents != null && parents.length)) return;
1037
+ const newInstance = createInstance(type, newProps, instance.__r3f.root); // https://github.com/pmndrs/react-three-fiber/issues/1348
1029
1038
  // When args change the instance has to be re-constructed, which then
1030
1039
  // forces r3f to re-parent the children and non-scene objects
1031
1040
  // This can not include primitives, which should not have declarative children
@@ -1038,13 +1047,24 @@ function createRenderer(roots, getEventPriority) {
1038
1047
  instance.__r3f.objects.forEach(child => appendChild(newInstance, child));
1039
1048
 
1040
1049
  instance.__r3f.objects = [];
1041
- removeChild(parent, instance);
1042
- appendChild(parent, newInstance); // Re-bind event handlers
1050
+
1051
+ for (const parent of parents) {
1052
+ removeChild(parent, instance);
1053
+ appendChild(parent, newInstance);
1054
+ } // Re-bind event handlers
1055
+
1043
1056
 
1044
1057
  if (newInstance.raycast && newInstance.__r3f.eventCount) {
1045
1058
  const rootState = newInstance.__r3f.root.getState();
1046
1059
 
1047
1060
  rootState.internal.interaction.push(newInstance);
1061
+ } // The attach attribute implies that the object attaches itself on the parent
1062
+
1063
+
1064
+ if ((_newInstance$__r3f = newInstance.__r3f) != null && _newInstance$__r3f.attach) {
1065
+ for (const parent of parents) {
1066
+ attach(parent, newInstance, newInstance.__r3f.attach);
1067
+ }
1048
1068
  } // This evil hack switches the react-internal fiber node
1049
1069
  [fiber, fiber.alternate].forEach(fiber => {
1050
1070
  if (fiber !== null) {
@@ -1080,12 +1100,12 @@ function createRenderer(roots, getEventPriority) {
1080
1100
  getChildHostContext: parentHostContext => parentHostContext,
1081
1101
 
1082
1102
  finalizeInitialChildren(instance) {
1083
- var _instance$__r3f3;
1103
+ var _instance$__r3f2;
1084
1104
 
1085
- const localState = (_instance$__r3f3 = instance == null ? void 0 : instance.__r3f) != null ? _instance$__r3f3 : {}; // https://github.com/facebook/react/issues/20271
1105
+ const localState = (_instance$__r3f2 = instance == null ? void 0 : instance.__r3f) != null ? _instance$__r3f2 : {}; // https://github.com/facebook/react/issues/20271
1086
1106
  // Returning true will trigger commitMount
1087
1107
 
1088
- return !!localState.handlers;
1108
+ return !!localState.handlers || !!localState.attach;
1089
1109
  },
1090
1110
 
1091
1111
  prepareUpdate(instance, type, oldProps, newProps) {
@@ -1123,14 +1143,21 @@ function createRenderer(roots, getEventPriority) {
1123
1143
  },
1124
1144
 
1125
1145
  commitMount(instance, type, props, int) {
1126
- var _instance$__r3f4;
1146
+ var _instance$__r3f3;
1127
1147
 
1128
1148
  // https://github.com/facebook/react/issues/20271
1129
1149
  // This will make sure events are only added once to the central container
1130
- const localState = (_instance$__r3f4 = instance == null ? void 0 : instance.__r3f) != null ? _instance$__r3f4 : {};
1150
+ const localState = (_instance$__r3f3 = instance.__r3f) != null ? _instance$__r3f3 : {};
1131
1151
 
1132
1152
  if (instance.raycast && localState.handlers && localState.eventCount) {
1133
1153
  instance.__r3f.root.getState().internal.interaction.push(instance);
1154
+ } // The attach attribute implies that the object attaches itself on the parent
1155
+
1156
+
1157
+ if (localState.attach) {
1158
+ for (const parent of localState.parents) {
1159
+ attach(parent, instance, localState.attach);
1160
+ }
1134
1161
  }
1135
1162
  },
1136
1163
 
@@ -1144,27 +1171,11 @@ function createRenderer(roots, getEventPriority) {
1144
1171
  detachDeletedInstance: () => {},
1145
1172
 
1146
1173
  hideInstance(instance) {
1147
- var _instance$__r3f5;
1148
-
1149
- // Deatch while the instance is hidden
1150
- const {
1151
- attach: type,
1152
- parent
1153
- } = (_instance$__r3f5 = instance == null ? void 0 : instance.__r3f) != null ? _instance$__r3f5 : {};
1154
- if (type && parent) detach(parent, instance, type);
1155
1174
  if (instance.isObject3D) instance.visible = false;
1156
1175
  invalidateInstance(instance);
1157
1176
  },
1158
1177
 
1159
1178
  unhideInstance(instance, props) {
1160
- var _instance$__r3f6;
1161
-
1162
- // Re-attach when the instance is unhidden
1163
- const {
1164
- attach: type,
1165
- parent
1166
- } = (_instance$__r3f6 = instance == null ? void 0 : instance.__r3f) != null ? _instance$__r3f6 : {};
1167
- if (type && parent) attach(parent, instance, type);
1168
1179
  if (instance.isObject3D && props.visible == null || props.visible) instance.visible = true;
1169
1180
  invalidateInstance(instance);
1170
1181
  },
@@ -1351,35 +1362,29 @@ const createStore = (invalidate, advance) => {
1351
1362
  initialHits: [],
1352
1363
  capturedMap: new Map(),
1353
1364
  subscribe: (ref, priority, store) => {
1354
- set(({
1355
- internal
1356
- }) => ({
1357
- internal: { ...internal,
1358
- // If this subscription was given a priority, it takes rendering into its own hands
1359
- // For that reason we switch off automatic rendering and increase the manual flag
1360
- // As long as this flag is positive there can be no internal rendering at all
1361
- // because there could be multiple render subscriptions
1362
- priority: internal.priority + (priority > 0 ? 1 : 0),
1363
- // Register subscriber and sort layers from lowest to highest, meaning,
1364
- // highest priority renders last (on top of the other frames)
1365
- subscribers: [...internal.subscribers, {
1366
- ref,
1367
- priority,
1368
- store
1369
- }].sort((a, b) => a.priority - b.priority)
1370
- }
1371
- }));
1365
+ const internal = get().internal; // If this subscription was given a priority, it takes rendering into its own hands
1366
+ // For that reason we switch off automatic rendering and increase the manual flag
1367
+ // As long as this flag is positive there can be no internal rendering at all
1368
+ // because there could be multiple render subscriptions
1369
+
1370
+ internal.priority = internal.priority + (priority > 0 ? 1 : 0);
1371
+ internal.subscribers.push({
1372
+ ref,
1373
+ priority,
1374
+ store
1375
+ }); // Register subscriber and sort layers from lowest to highest, meaning,
1376
+ // highest priority renders last (on top of the other frames)
1377
+
1378
+ internal.subscribers = internal.subscribers.sort((a, b) => a.priority - b.priority);
1372
1379
  return () => {
1373
- set(({
1374
- internal
1375
- }) => ({
1376
- internal: { ...internal,
1377
- // Decrease manual flag if this subscription had a priority
1378
- priority: internal.priority - (priority > 0 ? 1 : 0),
1379
- // Remove subscriber from list
1380
- subscribers: internal.subscribers.filter(s => s.ref !== ref)
1381
- }
1382
- }));
1380
+ const internal = get().internal;
1381
+
1382
+ if (internal != null && internal.subscribers) {
1383
+ // Decrease manual flag if this subscription had a priority
1384
+ internal.priority = internal.priority - (priority > 0 ? 1 : 0); // Remove subscriber from list
1385
+
1386
+ internal.subscribers = internal.subscribers.filter(s => s.ref !== ref);
1387
+ }
1383
1388
  };
1384
1389
  }
1385
1390
  }
@@ -1788,11 +1793,12 @@ function createRoot(canvas) {
1788
1793
  if (!isBoolean) Object.assign(gl.shadowMap, shadows);else gl.shadowMap.type = THREE__namespace.PCFSoftShadowMap;
1789
1794
  if (old !== gl.shadowMap.enabled) gl.shadowMap.needsUpdate = true;
1790
1795
  }
1791
- } // Set color management
1796
+ } // Safely set color management if available.
1797
+ // Avoid accessing THREE.ColorManagement to play nice with older versions
1792
1798
 
1793
1799
 
1794
- if (THREE__namespace.ColorManagement) {
1795
- THREE__namespace.ColorManagement.legacyMode = legacy;
1800
+ if ('ColorManagement' in THREE__namespace) {
1801
+ setDeep(THREE__namespace, legacy, ['ColorManagement', 'legacyMode']);
1796
1802
  }
1797
1803
 
1798
1804
  const outputEncoding = linear ? THREE__namespace.LinearEncoding : THREE__namespace.sRGBEncoding;
@@ -2042,7 +2048,7 @@ function Portal({
2042
2048
  reconciler.injectIntoDevTools({
2043
2049
  bundleType: 0 ,
2044
2050
  rendererPackageName: '@react-three/fiber',
2045
- version: '18.0.0'
2051
+ version: React__namespace.version
2046
2052
  });
2047
2053
  const act = React__namespace.unstable_act;
2048
2054