-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomDragControls.tsx
185 lines (160 loc) · 6.24 KB
/
CustomDragControls.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as React from 'react'
import * as THREE from 'three'
import { useThree } from '@react-three/fiber'
import { useGesture, DragConfig } from '@use-gesture/react'
import { ForwardRefComponent } from '@react-three/drei/helpers/ts-utils'
const initialModelPosition = new THREE.Vector3()
const mousePosition2D = new THREE.Vector2()
const mousePosition3D = new THREE.Vector3()
const dragOffset = new THREE.Vector3()
const dragPlaneNormal = new THREE.Vector3()
const dragPlane = new THREE.Plane()
type ControlsProto = {
enabled: boolean
}
export type CustomDragControlsProps = {
/** If autoTransform is true, automatically apply the local transform on drag, true */
autoTransform?: boolean
/** The matrix to control */
matrix?: THREE.Matrix4
/** Lock the drag to a specific axis */
axisLock?: 'x' | 'y' | 'z'
/** Limits */
dragLimits?: [[number, number] | undefined, [number, number] | undefined, [number, number] | undefined]
/** Hover event */
onHover?: (hovering: boolean) => void
/** Drag start event */
onDragStart?: (origin: THREE.Vector3) => void
/** Drag event */
onDrag?: (
localMatrix: THREE.Matrix4,
deltaLocalMatrix: THREE.Matrix4,
worldMatrix: THREE.Matrix4,
deltaWorldMatrix: THREE.Matrix4
) => void /** Drag end event */
onDragEnd?: () => void
children: React.ReactNode
dragConfig?: DragConfig
preventOverlap?: boolean /** If preventOverlap is true, other overlapping CustomDragControls will not be dragged*/
}
export const CustomDragControls: ForwardRefComponent<CustomDragControlsProps, THREE.Group> = React.forwardRef<
THREE.Group,
CustomDragControlsProps
>(
(
{
autoTransform = true,
matrix,
axisLock,
dragLimits,
onHover,
onDragStart,
onDrag,
onDragEnd,
children,
dragConfig,
...props
},
fRef
) => {
const defaultControls = useThree((state) => (state as any).controls) as ControlsProto | undefined
const { camera, size, raycaster, invalidate } = useThree()
const ref = React.useRef<THREE.Group>(null!)
const bind = useGesture(
{
onHover: ({ hovering }) => onHover && onHover(hovering ?? false),
onDragStart: ({ event }) => {
if (defaultControls) defaultControls.enabled = false
const { point } = event as any
ref.current.matrix.decompose(initialModelPosition, new THREE.Quaternion(), new THREE.Vector3())
mousePosition3D.copy(point)
dragOffset.copy(mousePosition3D).sub(initialModelPosition)
onDragStart && onDragStart(initialModelPosition)
invalidate()
},
onDrag: ({ xy: [dragX, dragY], intentional, event }) => {
if (!intentional) return
if(props.preventOverlap === true){
event.stopPropagation();
}
const normalizedMouseX = ((dragX - size.left) / size.width) * 2 - 1
const normalizedMouseY = -((dragY - size.top) / size.height) * 2 + 1
mousePosition2D.set(normalizedMouseX, normalizedMouseY)
raycaster.setFromCamera(mousePosition2D, camera)
if (!axisLock) {
camera.getWorldDirection(dragPlaneNormal).negate()
} else {
switch (axisLock) {
case 'x':
dragPlaneNormal.set(1, 0, 0)
break
case 'y':
dragPlaneNormal.set(0, 1, 0)
break
case 'z':
dragPlaneNormal.set(0, 0, 1)
break
}
}
dragPlane.setFromNormalAndCoplanarPoint(dragPlaneNormal, mousePosition3D)
raycaster.ray.intersectPlane(dragPlane, mousePosition3D)
const previousLocalMatrix = ref.current.matrix.clone()
const previousWorldMatrix = ref.current.matrixWorld.clone()
const intendedNewPosition = new THREE.Vector3(
mousePosition3D.x - dragOffset.x,
mousePosition3D.y - dragOffset.y,
mousePosition3D.z - dragOffset.z
)
if (dragLimits) {
intendedNewPosition.x = dragLimits[0]
? Math.max(Math.min(intendedNewPosition.x, dragLimits[0][1]), dragLimits[0][0])
: intendedNewPosition.x
intendedNewPosition.y = dragLimits[1]
? Math.max(Math.min(intendedNewPosition.y, dragLimits[1][1]), dragLimits[1][0])
: intendedNewPosition.y
intendedNewPosition.z = dragLimits[2]
? Math.max(Math.min(intendedNewPosition.z, dragLimits[2][1]), dragLimits[2][0])
: intendedNewPosition.z
}
if (autoTransform) {
ref.current.matrix.setPosition(intendedNewPosition)
const deltaLocalMatrix = ref.current.matrix.clone().multiply(previousLocalMatrix.invert())
const deltaWorldMatrix = ref.current.matrix.clone().multiply(previousWorldMatrix.invert())
onDrag && onDrag(ref.current.matrix, deltaLocalMatrix, ref.current.matrixWorld, deltaWorldMatrix)
} else {
const tempMatrix = new THREE.Matrix4().copy(ref.current.matrix)
tempMatrix.setPosition(intendedNewPosition)
const deltaLocalMatrix = tempMatrix.clone().multiply(previousLocalMatrix.invert())
const deltaWorldMatrix = tempMatrix.clone().multiply(previousWorldMatrix.invert())
onDrag && onDrag(tempMatrix, deltaLocalMatrix, ref.current.matrixWorld, deltaWorldMatrix)
}
invalidate()
},
onDragEnd: () => {
if (defaultControls) defaultControls.enabled = true
onDragEnd && onDragEnd()
invalidate()
},
},
{
drag: {
filterTaps: true,
threshold: 1,
...(typeof dragConfig === 'object' ? dragConfig : {}),
},
}
)
React.useImperativeHandle(fRef, () => ref.current, [])
React.useLayoutEffect(() => {
if (!matrix) return
// If the matrix is a real matrix4 it means that the user wants to control the gizmo
// In that case it should just be set, as a bare prop update would merely copy it
ref.current.matrix = matrix
}, [matrix])
return (
<group ref={ref} {...(bind() as any)} matrix={matrix} matrixAutoUpdate={false} {...props}>
{children}
</group>
)
}
)