-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmoothOrbitControls.tsx
50 lines (44 loc) · 1.4 KB
/
SmoothOrbitControls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { OrbitControlsProps } from '@react-three/drei/core/OrbitControls';
import { OrbitControls, TrackballControls } from '@react-three/drei';
import { useThree, useFrame } from '@react-three/fiber';
import React, { useRef } from 'react';
interface SmoothOrbitControlsProps extends OrbitControlsProps {
zoomSpeed?: number;
zoomDamping?: number
}
export default function SmoothOrbitControls(
{
zoomSpeed = .07,
zoomDamping = .03,
...props
}: SmoothOrbitControlsProps
) {
const orbitControlsRef = useRef<any>();
const trackballControlsRef = useRef<any>();
const { camera, gl } = useThree();
useFrame(() => {
if (trackballControlsRef.current && !props.zoomToCursor) {
const t = orbitControlsRef.current.target;
trackballControlsRef.current.target.set(t.x, t.y, t.z)
}
});
return (
<>
<OrbitControls
ref={orbitControlsRef}
args={[camera, gl.domElement]}
{...props}
enableZoom={false}
/>
<TrackballControls
ref={trackballControlsRef}
args={[camera, gl.domElement]}
dynamicDampingFactor={zoomDamping}
zoomSpeed={zoomSpeed}
noPan
noRotate
cursorZoom={props.zoomToCursor}
/>
</>
);
};