-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
TransformControls.svelte
152 lines (130 loc) · 4.13 KB
/
TransformControls.svelte
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
<script lang="ts">
import {
forwardEventHandlers,
HierarchicalObject,
T,
useThrelte,
watch,
type Props
} from '@threlte/core'
import { Group } from 'three'
import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js'
import { derived, writable } from 'svelte/store'
import { useControlsContext } from '../useControlsContext'
import type {
TransformControlsEvents,
TransformControlsProps,
TransformControlsSlots
} from './TransformControls.svelte'
type $$Props = TransformControlsProps
type $$Events = TransformControlsEvents
type $$Slots = TransformControlsSlots
export let autoPauseOrbitControls: $$Props['autoPauseOrbitControls'] = true
export let autoPauseTrackballControls: $$Props['autoPauseTrackballControls'] = true
export let object: $$Props['object'] = undefined
const { camera, renderer, invalidate, scene } = useThrelte()
const { orbitControls, trackballControls } = useControlsContext()
const isDragging = writable(false)
const useAutoPauseOrbitControls = writable(autoPauseOrbitControls ?? true)
$: useAutoPauseOrbitControls.set(autoPauseOrbitControls ?? true)
const useAutoPauseTrackballControls = writable(autoPauseTrackballControls ?? true)
$: useAutoPauseTrackballControls.set(autoPauseTrackballControls ?? true)
const onDraggingChanged = (e: { value: boolean }) => {
isDragging.set(e.value)
}
watch(
[orbitControls, isDragging, useAutoPauseOrbitControls],
([orbitControls, isDragging, useAutoPauseOrbitControls]) => {
// if there are no orbitcontrols or we're not even
// dragging, or the orbitcontrols are disabled, return
if (!orbitControls || (!orbitControls.enabled && isDragging)) return
orbitControls.enabled = !(isDragging && useAutoPauseOrbitControls)
return () => {
// we know they were enabled before, so we can
// safely re-enable them
orbitControls.enabled = true
}
}
)
watch(
[trackballControls, isDragging, useAutoPauseTrackballControls],
([trackballControls, isDragging, useAutoPausetrackballControls]) => {
// if there are no trackballcontrols or we're not even
// dragging, or the trackballcontrols are disabled, return
if (!trackballControls || (!trackballControls.enabled && isDragging)) return
trackballControls.enabled = !(isDragging && useAutoPausetrackballControls)
return () => {
// we know they were enabled before, so we can
// safely re-enable them
trackballControls.enabled = true
}
}
)
export const group = new Group()
const controlsStore = derived(camera, (camera) => {
return new TransformControls(camera, renderer.domElement)
})
export let controls = $controlsStore
$: controls = $controlsStore
const attachTo = writable(object ?? group)
watch([controlsStore, attachTo], ([controls, attachTo]) => {
controls.attach(attachTo)
return () => {
controls.detach()
}
})
// This component is receiving the props for the controls as well as the props
// for the group, so we need to split them up
const transformOnlyPropNames = [
'enabled',
'axis',
'mode',
'translationSnap',
'rotationSnap',
'scaleSnap',
'space',
'size',
'showX',
'showY',
'showZ',
'visible'
]
let transformProps: Props<TransformControls> = {}
let objectProps: Props<Group> = {}
$: {
transformProps = {}
objectProps = {}
for (let [key, value] of Object.entries($$restProps)) {
if (transformOnlyPropNames.includes(key)) {
transformProps[key] = value
} else {
objectProps[key] = value
}
}
}
const component = forwardEventHandlers()
</script>
<!-- TransformControls need to be added to the scene -->
<HierarchicalObject
onChildMount={(child) => {
scene.add(child)
}}
onChildDestroy={(child) => {
scene.remove(child)
}}
>
<T
is={$controlsStore}
on:dragging-changed={onDraggingChanged}
on:change={invalidate}
{...transformProps}
bind:this={$component}
/>
</HierarchicalObject>
<T
is={group}
let:ref
{...objectProps}
>
<slot {ref} />
</T>