-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathTransformControls.tsx
162 lines (148 loc) · 5.48 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { ReactThreeFiber, useThree } from '@react-three/fiber'
import * as React from 'react'
import * as THREE from 'three'
import { TransformControls as TransformControlsImpl } from 'three-stdlib'
import { ForwardRefComponent } from '../helpers/ts-utils'
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?: 'translate' | 'rotate' | 'scale'
translationSnap?: number | null
rotationSnap?: number | null
scaleSnap?: number | null
space?: 'world' | 'local'
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: ForwardRefComponent<TransformControlsProps, TransformControlsImpl> =
/* @__PURE__ */ React.forwardRef<TransformControlsImpl, TransformControlsProps>(
(
{
children,
domElement,
onChange,
onMouseDown,
onMouseUp,
onObjectChange,
object,
makeDefault,
camera,
// Transform
enabled,
axis,
mode,
translationSnap,
rotationSnap,
scaleSnap,
space,
size,
showX,
showY,
showZ,
...props
},
ref
) => {
// @ts-expect-error new in @react-three/[email protected]
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>(null!)
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(() => void (onChangeRef.current = onChange), [onChange])
React.useLayoutEffect(() => void (onMouseDownRef.current = onMouseDown), [onMouseDown])
React.useLayoutEffect(() => void (onMouseUpRef.current = onMouseUp), [onMouseUp])
React.useLayoutEffect(() => void (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 (
<>
<primitive
ref={ref}
object={controls}
enabled={enabled}
axis={axis}
mode={mode}
translationSnap={translationSnap}
rotationSnap={rotationSnap}
scaleSnap={scaleSnap}
space={space}
size={size}
showX={showX}
showY={showY}
showZ={showZ}
/>
<group ref={group} {...props}>
{children}
</group>
</>
)
}
)