@react-three/drei 9.121.5 → 9.122.0

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),t=require("three"),r=require("react"),n=require("@react-three/fiber"),o=require("maath");function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function c(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var s=u(e),a=c(t),i=c(r);const f=i.createContext(null);function l(){return i.useContext(f)}function p({points:e=50}){const{path:t}=l(),[r,n]=i.useState([]),[o]=i.useState((()=>new a.MeshBasicMaterial({color:"black"}))),[u]=i.useState((()=>new a.SphereGeometry(.025,16,16))),c=i.useRef([]);return i.useEffect((()=>{t.curves!==c.current&&(n(t.getPoints(e)),c.current=t.curves)})),i.createElement(i.Fragment,null,r.map(((e,t)=>i.createElement("mesh",{key:t,material:o,geometry:u,position:[e.x,e.y,e.z]}))))}const m=i.forwardRef((({children:e,curves:t=[],object:r,debug:u=!1,smooth:c=!1,focus:l,offset:m,eps:d=1e-5,damping:g=.1,focusDamping:v=.1,maxSpeed:b=1/0,...h},j)=>{const{camera:y}=n.useThree(),P=i.useRef(),[w]=i.useState((()=>new a.CurvePath)),x=i.useRef(null!=m?m:0),O=i.useMemo((()=>({focus:l,object:(null==r?void 0:r.current)instanceof a.Object3D?r:{current:y},path:w,current:x.current,offset:x.current,point:new a.Vector3,tangent:new a.Vector3,next:new a.Vector3})),[l,r]);i.useLayoutEffect((()=>{var e;w.curves=[];const r=t.length>0?t:null==(e=P.current)?void 0:e.__r3f.objects;for(var n=0;n<r.length;n++)w.add(r[n]);if(c){const e=w.getPoints("number"==typeof c?c:1),t=new a.CatmullRomCurve3(e);w.curves=[t]}w.updateArcLengths()})),i.useImperativeHandle(j,(()=>P.current),[]),i.useLayoutEffect((()=>{x.current=o.misc.repeat(x.current,1)}),[m]);let E=0;const[C]=i.useState((()=>new a.Vector3));return n.useFrame(((e,t)=>{if(E=O.offset,o.easing.damp(x,"current",void 0!==m?m:O.current,g,t,b,void 0,d),O.offset=o.misc.repeat(x.current,1),w.getCurveLengths().length>0){w.getPointAt(O.offset,O.point),w.getTangentAt(O.offset,O.tangent).normalize(),w.getPointAt(o.misc.repeat(x.current-(E-O.offset),1),O.next);const e=(null==r?void 0:r.current)instanceof a.Object3D?r.current:y;e.position.copy(O.point),l&&o.easing.dampLookAt(e,(e=>(null==e?void 0:e.current)instanceof a.Object3D)(l)?l.current.getWorldPosition(C):l,v,t,b,void 0,d)}})),i.createElement("group",s.default({ref:P},h),i.createElement(f.Provider,{value:O},e,u&&i.createElement(p,null)))}));exports.MotionPathControls=m,exports.useMotion=l;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),t=require("three"),r=require("react"),n=require("@react-three/fiber"),o=require("maath");function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function c(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var s=u(e),a=c(t),i=c(r);const l=i.createContext(null);function f(){const e=i.useContext(l);if(!e)throw new Error("useMotion hook must be used in a MotionPathControls component.");return e}function m({points:e=50,color:t="black"}){const{path:r}=f(),[n,o]=i.useState([]),u=i.useMemo((()=>new a.MeshBasicMaterial({color:t})),[t]),c=i.useMemo((()=>new a.SphereGeometry(.025,16,16)),[]),s=i.useRef([]);return i.useEffect((()=>{r.curves!==s.current&&(o(r.getPoints(e)),s.current=r.curves)})),n.map(((e,t)=>i.createElement("mesh",{key:t,material:u,geometry:c,position:[e.x,e.y,e.z]})))}const p=i.forwardRef((({children:e,curves:t=[],debug:r=!1,debugColor:u="black",object:c,focus:f,loop:p=!0,offset:d,smooth:g=!1,eps:v=1e-5,damping:b=.1,focusDamping:h=.1,maxSpeed:j=1/0,...y},M)=>{const{camera:w}=n.useThree(),P=i.useRef(null),O=i.useRef(null!=d?d:0),x=i.useMemo((()=>new a.CurvePath),[]),C=i.useMemo((()=>({focus:f,object:(null==c?void 0:c.current)instanceof a.Object3D?c:{current:w},path:x,current:O.current,offset:O.current,point:new a.Vector3,tangent:new a.Vector3,next:new a.Vector3})),[f,c]);i.useLayoutEffect((()=>{var e,r;x.curves=[];const n=t.length>0?t:null!==(e=null==(r=P.current)||null==(r=r.__r3f)?void 0:r.objects)&&void 0!==e?e:[];for(let e=0;e<n.length;e++)x.add(n[e]);if(g){const e=x.getPoints("number"==typeof g?g:1),t=new a.CatmullRomCurve3(e);x.curves=[t]}x.updateArcLengths()})),i.useImperativeHandle(M,(()=>Object.assign(P.current,{motion:C})),[C]),i.useLayoutEffect((()=>{O.current=o.misc.repeat(O.current,1)}),[d]);const E=i.useMemo((()=>new a.Vector3),[]);return n.useFrame(((e,t)=>{const r=C.offset;if(o.easing.damp(O,"current",void 0!==d?d:C.current,b,t,j,void 0,v),C.offset=p?o.misc.repeat(O.current,1):o.misc.clamp(O.current,0,1),x.getCurveLengths().length>0){x.getPointAt(C.offset,C.point),x.getTangentAt(C.offset,C.tangent).normalize(),x.getPointAt(o.misc.repeat(O.current-(r-C.offset),1),C.next);const e=(null==c?void 0:c.current)instanceof a.Object3D?c.current:w;e.position.copy(C.point),f&&o.easing.dampLookAt(e,(e=>(null==e?void 0:e.current)instanceof a.Object3D)(f)?f.current.getWorldPosition(E):f,h,t,j,void 0,v)}})),i.createElement("group",s.default({ref:P},y),i.createElement(l.Provider,{value:C},e,r&&i.createElement(m,{color:u})))}));exports.MotionPathControls=p,exports.useMotion=f;
@@ -3,8 +3,10 @@ import * as React from 'react';
3
3
  type MotionPathProps = JSX.IntrinsicElements['group'] & {
4
4
  curves?: THREE.Curve<THREE.Vector3>[];
5
5
  debug?: boolean;
6
+ debugColor?: THREE.ColorRepresentation;
6
7
  object?: React.MutableRefObject<THREE.Object3D>;
7
8
  focus?: [x: number, y: number, z: number] | React.MutableRefObject<THREE.Object3D>;
9
+ loop?: boolean;
8
10
  offset?: number;
9
11
  smooth?: boolean | number;
10
12
  eps?: number;
@@ -22,6 +24,9 @@ type MotionState = {
22
24
  tangent: THREE.Vector3;
23
25
  next: THREE.Vector3;
24
26
  };
27
+ export type MotionPathRef = THREE.Group & {
28
+ motion: MotionState;
29
+ };
25
30
  export declare function useMotion(): MotionState;
26
- export declare const MotionPathControls: React.ForwardRefExoticComponent<Omit<MotionPathProps, "ref"> & React.RefAttributes<THREE.Group>>;
31
+ export declare const MotionPathControls: React.ForwardRefExoticComponent<Omit<MotionPathProps, "ref"> & React.RefAttributes<MotionPathRef>>;
27
32
  export {};
@@ -5,21 +5,24 @@ import { useThree, useFrame } from '@react-three/fiber';
5
5
  import { misc, easing } from 'maath';
6
6
 
7
7
  const isObject3DRef = ref => (ref == null ? void 0 : ref.current) instanceof THREE.Object3D;
8
- const context = /* @__PURE__ */React.createContext(null);
8
+ const MotionContext = /* @__PURE__ */React.createContext(null);
9
9
  function useMotion() {
10
- return React.useContext(context);
10
+ const context = React.useContext(MotionContext);
11
+ if (!context) throw new Error('useMotion hook must be used in a MotionPathControls component.');
12
+ return context;
11
13
  }
12
14
  function Debug({
13
- points = 50
15
+ points = 50,
16
+ color = 'black'
14
17
  }) {
15
18
  const {
16
19
  path
17
20
  } = useMotion();
18
21
  const [dots, setDots] = React.useState([]);
19
- const [material] = React.useState(() => new THREE.MeshBasicMaterial({
20
- color: 'black'
21
- }));
22
- const [geometry] = React.useState(() => new THREE.SphereGeometry(0.025, 16, 16));
22
+ const material = React.useMemo(() => new THREE.MeshBasicMaterial({
23
+ color: color
24
+ }), [color]);
25
+ const geometry = React.useMemo(() => new THREE.SphereGeometry(0.025, 16, 16), []);
23
26
  const last = React.useRef([]);
24
27
  React.useEffect(() => {
25
28
  if (path.curves !== last.current) {
@@ -27,21 +30,23 @@ function Debug({
27
30
  last.current = path.curves;
28
31
  }
29
32
  });
30
- return /*#__PURE__*/React.createElement(React.Fragment, null, dots.map((item, index) => /*#__PURE__*/React.createElement("mesh", {
33
+ return dots.map((item, index) => /*#__PURE__*/React.createElement("mesh", {
31
34
  key: index,
32
35
  material: material,
33
36
  geometry: geometry,
34
37
  position: [item.x, item.y, item.z]
35
- })));
38
+ }));
36
39
  }
37
40
  const MotionPathControls = /* @__PURE__ */React.forwardRef(({
38
41
  children,
39
42
  curves = [],
40
- object,
41
43
  debug = false,
42
- smooth = false,
44
+ debugColor = 'black',
45
+ object,
43
46
  focus,
47
+ loop = true,
44
48
  offset = undefined,
49
+ smooth = false,
45
50
  eps = 0.00001,
46
51
  damping = 0.1,
47
52
  focusDamping = 0.1,
@@ -51,9 +56,9 @@ const MotionPathControls = /* @__PURE__ */React.forwardRef(({
51
56
  const {
52
57
  camera
53
58
  } = useThree();
54
- const ref = React.useRef();
55
- const [path] = React.useState(() => new THREE.CurvePath());
59
+ const ref = React.useRef(null);
56
60
  const pos = React.useRef(offset !== null && offset !== void 0 ? offset : 0);
61
+ const path = React.useMemo(() => new THREE.CurvePath(), []);
57
62
  const state = React.useMemo(() => ({
58
63
  focus,
59
64
  object: (object == null ? void 0 : object.current) instanceof THREE.Object3D ? object : {
@@ -67,12 +72,12 @@ const MotionPathControls = /* @__PURE__ */React.forwardRef(({
67
72
  next: new THREE.Vector3()
68
73
  }), [focus, object]);
69
74
  React.useLayoutEffect(() => {
70
- var _ref$current;
75
+ var _r3f$objects, _ref$current;
71
76
  path.curves = [];
72
- const _curves = curves.length > 0 ? curves : (_ref$current = ref.current) == null ? void 0 : _ref$current.__r3f.objects;
73
- for (var i = 0; i < _curves.length; i++) path.add(_curves[i]);
77
+ const _curves = curves.length > 0 ? curves : (_r3f$objects = (_ref$current = ref.current) == null || (_ref$current = _ref$current.__r3f) == null ? void 0 : _ref$current.objects) !== null && _r3f$objects !== void 0 ? _r3f$objects : [];
78
+ for (let i = 0; i < _curves.length; i++) path.add(_curves[i]);
74
79
 
75
- //Smoothen curve
80
+ // Smoothen curve
76
81
  if (smooth) {
77
82
  const points = path.getPoints(typeof smooth === 'number' ? smooth : 1);
78
83
  const catmull = new THREE.CatmullRomCurve3(points);
@@ -80,24 +85,24 @@ const MotionPathControls = /* @__PURE__ */React.forwardRef(({
80
85
  }
81
86
  path.updateArcLengths();
82
87
  });
83
- React.useImperativeHandle(fref, () => ref.current, []);
88
+ React.useImperativeHandle(fref, () => Object.assign(ref.current, {
89
+ motion: state
90
+ }), [state]);
84
91
  React.useLayoutEffect(() => {
85
92
  // When offset changes, normalise pos to avoid overshoot spinning
86
93
  pos.current = misc.repeat(pos.current, 1);
87
94
  }, [offset]);
88
- let last = 0;
89
- const [vec] = React.useState(() => new THREE.Vector3());
95
+ const vec = React.useMemo(() => new THREE.Vector3(), []);
90
96
  useFrame((_state, delta) => {
91
- last = state.offset;
97
+ const lastOffset = state.offset;
92
98
  easing.damp(pos, 'current', offset !== undefined ? offset : state.current, damping, delta, maxSpeed, undefined, eps);
93
- state.offset = misc.repeat(pos.current, 1);
99
+ state.offset = loop ? misc.repeat(pos.current, 1) : misc.clamp(pos.current, 0, 1);
94
100
  if (path.getCurveLengths().length > 0) {
95
101
  path.getPointAt(state.offset, state.point);
96
102
  path.getTangentAt(state.offset, state.tangent).normalize();
97
- path.getPointAt(misc.repeat(pos.current - (last - state.offset), 1), state.next);
103
+ path.getPointAt(misc.repeat(pos.current - (lastOffset - state.offset), 1), state.next);
98
104
  const target = (object == null ? void 0 : object.current) instanceof THREE.Object3D ? object.current : camera;
99
105
  target.position.copy(state.point);
100
- //@ts-ignore
101
106
  if (focus) {
102
107
  easing.dampLookAt(target, isObject3DRef(focus) ? focus.current.getWorldPosition(vec) : focus, focusDamping, delta, maxSpeed, undefined, eps);
103
108
  }
@@ -105,9 +110,11 @@ const MotionPathControls = /* @__PURE__ */React.forwardRef(({
105
110
  });
106
111
  return /*#__PURE__*/React.createElement("group", _extends({
107
112
  ref: ref
108
- }, props), /*#__PURE__*/React.createElement(context.Provider, {
113
+ }, props), /*#__PURE__*/React.createElement(MotionContext.Provider, {
109
114
  value: state
110
- }, children, debug && /*#__PURE__*/React.createElement(Debug, null)));
115
+ }, children, debug && /*#__PURE__*/React.createElement(Debug, {
116
+ color: debugColor
117
+ })));
111
118
  });
112
119
 
113
120
  export { MotionPathControls, useMotion };