-
Notifications
You must be signed in to change notification settings - Fork 620
/
TransformControls.tsx
139 lines (124 loc) · 4.95 KB
/
TransformControls.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
import { ReactThreeFiber, useThree } from '@react-three/fiber'
import omit from 'lodash.omit'
import pick from 'lodash.pick'
import * as React from 'react'
import * as THREE from 'three'
import { TransformControls as TransformControlsImpl } from 'three-stdlib'
type ControlsProto = {
enabled: boolean
}
export type TransformControlsProps = ReactThreeFiber.Object3DNode<TransformControlsImpl, typeof TransformControlsImpl> &
JSX.IntrinsicElements['group'] & {
object?: THREE.Object3D | React.MutableRefObject<THREE.Object3D>
enabled?: boolean
axis?: string | null
domElement?: HTMLElement
mode?: string
translationSnap?: number | null
rotationSnap?: number | null
scaleSnap?: number | null
space?: string
size?: number
showX?: boolean
showY?: boolean
showZ?: boolean
children?: React.ReactElement<THREE.Object3D>
camera?: THREE.Camera
onChange?: (e?: THREE.Event) => void
onMouseDown?: (e?: THREE.Event) => void
onMouseUp?: (e?: THREE.Event) => void
onObjectChange?: (e?: THREE.Event) => void
makeDefault?: boolean
}
export const TransformControls = React.forwardRef<TransformControlsImpl, TransformControlsProps>(
({ children, domElement, onChange, onMouseDown, onMouseUp, onObjectChange, object, makeDefault, ...props }, ref) => {
const transformOnlyPropNames = [
'enabled',
'axis',
'mode',
'translationSnap',
'rotationSnap',
'scaleSnap',
'space',
'size',
'showX',
'showY',
'showZ',
]
const { camera, ...rest } = props
const transformProps = pick(rest, transformOnlyPropNames)
const objectProps = omit(rest, transformOnlyPropNames)
// @ts-expect-error new in @react-three/fiber@7.0.5
const defaultControls = useThree((state) => state.controls) as ControlsProto
const gl = useThree((state) => state.gl)
const events = useThree((state) => state.events)
const defaultCamera = useThree((state) => state.camera)
const invalidate = useThree((state) => state.invalidate)
const get = useThree((state) => state.get)
const set = useThree((state) => state.set)
const explCamera = camera || defaultCamera
const explDomElement = (domElement || events.connected || gl.domElement) as HTMLElement
const controls = React.useMemo(
() => new TransformControlsImpl(explCamera, explDomElement),
[explCamera, explDomElement]
)
const group = React.useRef<THREE.Group>()
React.useLayoutEffect(() => {
if (object) {
controls.attach(object instanceof THREE.Object3D ? object : object.current)
} else if (group.current instanceof THREE.Object3D) {
controls.attach(group.current)
}
return () => void controls.detach()
}, [object, children, controls])
React.useEffect(() => {
if (defaultControls) {
const callback = (event) => (defaultControls.enabled = !event.value)
controls.addEventListener('dragging-changed', callback)
return () => controls.removeEventListener('dragging-changed', callback)
}
}, [controls, defaultControls])
const onChangeRef = React.useRef<(e?: THREE.Event) => void>()
const onMouseDownRef = React.useRef<(e?: THREE.Event) => void>()
const onMouseUpRef = React.useRef<(e?: THREE.Event) => void>()
const onObjectChangeRef = React.useRef<(e?: THREE.Event) => void>()
React.useLayoutEffect(() => (onChangeRef.current = onChange), [onChange])
React.useLayoutEffect(() => (onMouseDownRef.current = onMouseDown), [onMouseDown])
React.useLayoutEffect(() => (onMouseUpRef.current = onMouseUp), [onMouseUp])
React.useLayoutEffect(() => (onObjectChangeRef.current = onObjectChange), [onObjectChange])
React.useEffect(() => {
const onChange = (e: THREE.Event) => {
invalidate()
onChangeRef.current?.(e)
}
const onMouseDown = (e: THREE.Event) => onMouseDownRef.current?.(e)
const onMouseUp = (e: THREE.Event) => onMouseUpRef.current?.(e)
const onObjectChange = (e: THREE.Event) => onObjectChangeRef.current?.(e)
controls.addEventListener('change', onChange)
controls.addEventListener('mouseDown', onMouseDown)
controls.addEventListener('mouseUp', onMouseUp)
controls.addEventListener('objectChange', onObjectChange)
return () => {
controls.removeEventListener('change', onChange)
controls.removeEventListener('mouseDown', onMouseDown)
controls.removeEventListener('mouseUp', onMouseUp)
controls.removeEventListener('objectChange', onObjectChange)
}
}, [invalidate, controls])
React.useEffect(() => {
if (makeDefault) {
const old = get().controls
set({ controls })
return () => set({ controls: old })
}
}, [makeDefault, controls])
return controls ? (
<>
<primitive ref={ref} object={controls} {...transformProps} />
<group ref={group} {...objectProps}>
{children}
</group>
</>
) : null
}
)