Line data Source code
1 : import type { Point2D, Vector3 } from '@comfyorg/shared-frontend-utils/geometry'
2 : import * as THREE from 'three'
3 : import { TransformControls } from 'three/examples/jsm/controls/TransformControls'
4 :
5 : import type { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
6 :
7 : import type { GizmoMode, Model3DTransform } from './interfaces'
8 : import type { PointerNdcSource } from './load3dViewport'
9 :
10 24 : const OFF_SCREEN_POINTER_NDC = { x: 10, y: 10 }
11 :
12 24 : export class GizmoManager {
13 : private transformControls: TransformControls | null = null
14 : private targetObject: THREE.Object3D | null = null
15 : private initialPosition: THREE.Vector3 = new THREE.Vector3()
16 : private initialRotation: THREE.Euler = new THREE.Euler()
17 : private initialScale: THREE.Vector3 = new THREE.Vector3(1, 1, 1)
18 : private enabled: boolean = false
19 : private activeCamera: THREE.Camera
20 : private mode: GizmoMode = 'translate'
21 : private scene: THREE.Scene
22 : private interactionElement: HTMLElement
23 : private orbitControls: OrbitControls
24 : private onTransformChange?: () => void
25 : private getPointerNdc?: PointerNdcSource
26 :
27 : constructor(
28 : scene: THREE.Scene,
29 : interactionElement: HTMLElement,
30 : orbitControls: OrbitControls,
31 : getActiveCamera: () => THREE.Camera,
32 : onTransformChange?: () => void
33 : ) {
34 27 : this.scene = scene
35 27 : this.interactionElement = interactionElement
36 27 : this.orbitControls = orbitControls
37 27 : this.activeCamera = getActiveCamera()
38 27 : this.onTransformChange = onTransformChange
39 : }
40 :
41 : init(): void {
42 27 : this.transformControls = new TransformControls(
43 : this.activeCamera,
44 : this.interactionElement
45 : )
46 :
47 27 : this.transformControls.addEventListener('dragging-changed', (event) => {
48 0 : this.orbitControls.enabled = !event.value
49 0 : if (!event.value && this.onTransformChange) {
50 0 : this.onTransformChange()
51 : }
52 : })
53 :
54 27 : this.installPointerNdcOverride()
55 :
56 27 : const helper = this.transformControls.getHelper()
57 27 : helper.name = 'GizmoTransformControls'
58 27 : helper.renderOrder = 999
59 27 : this.scene.add(helper)
60 : }
61 :
62 : setPointerNdcSource(getPointerNdc: PointerNdcSource): void {
63 27 : this.getPointerNdc = getPointerNdc
64 : }
65 :
66 : private installPointerNdcOverride(): void {
67 27 : if (!this.transformControls) return
68 27 : const transformControls = this.transformControls
69 27 : const controls = transformControls as unknown as {
70 : _getPointer?: (event: PointerEvent) => Point2D & {
71 : button: number
72 : }
73 : }
74 27 : const original = controls._getPointer
75 27 : if (typeof original !== 'function') {
76 0 : console.warn(
77 : 'TransformControls no longer exposes _getPointer; letterbox-aware gizmo pointer mapping is disabled.'
78 : )
79 0 : return
80 : }
81 27 : controls._getPointer = (event: PointerEvent) => {
82 0 : if (!this.getPointerNdc) return original.call(transformControls, event)
83 0 : const ndc = this.getPointerNdc(event.clientX, event.clientY)
84 0 : if (!ndc || (!ndc.inside && !transformControls.dragging)) {
85 0 : return { ...OFF_SCREEN_POINTER_NDC, button: event.button }
86 : }
87 0 : return { x: ndc.x, y: ndc.y, button: event.button }
88 : }
89 : }
90 :
91 : setupForModel(model: THREE.Object3D): void {
92 19 : if (!this.transformControls) return
93 :
94 19 : this.ensureHelperInScene()
95 :
96 19 : this.transformControls.detach()
97 19 : this.transformControls.enabled = false
98 :
99 19 : this.targetObject = model
100 19 : this.initialPosition.copy(model.position)
101 19 : this.initialRotation.copy(model.rotation)
102 19 : this.initialScale.copy(model.scale)
103 :
104 19 : if (this.enabled) {
105 0 : this.transformControls.attach(model)
106 0 : this.transformControls.setMode(this.mode)
107 0 : this.transformControls.enabled = true
108 : }
109 : }
110 :
111 : detach(): void {
112 28 : this.enabled = false
113 28 : if (this.transformControls) {
114 28 : this.transformControls.detach()
115 28 : this.transformControls.enabled = false
116 : }
117 28 : this.targetObject = null
118 : }
119 :
120 : setEnabled(enabled: boolean): void {
121 3 : this.enabled = enabled
122 :
123 3 : if (!this.transformControls) return
124 :
125 3 : this.ensureHelperInScene()
126 :
127 3 : if (enabled && this.targetObject) {
128 0 : this.transformControls.attach(this.targetObject)
129 0 : this.transformControls.setMode(this.mode)
130 0 : this.transformControls.enabled = true
131 : } else {
132 3 : this.transformControls.detach()
133 3 : this.transformControls.enabled = false
134 : }
135 : }
136 :
137 : ensureHelperInScene(): void {
138 25 : if (!this.transformControls) return
139 25 : const helper = this.transformControls.getHelper()
140 25 : if (!helper.parent) {
141 3 : this.scene.add(helper)
142 : }
143 : }
144 :
145 : removeFromScene(): void {
146 3 : if (!this.transformControls) return
147 3 : const helper = this.transformControls.getHelper()
148 3 : if (helper.parent) {
149 3 : helper.parent.remove(helper)
150 : }
151 : }
152 :
153 : isEnabled(): boolean {
154 4 : return this.enabled
155 : }
156 :
157 : updateCamera(camera: THREE.Camera): void {
158 52 : this.activeCamera = camera
159 52 : if (this.transformControls) {
160 52 : this.transformControls.camera = camera
161 : }
162 : }
163 :
164 : setMode(mode: GizmoMode): void {
165 3 : this.mode = mode
166 :
167 3 : if (this.transformControls) {
168 3 : this.transformControls.setMode(mode)
169 : }
170 : }
171 :
172 : getMode(): GizmoMode {
173 0 : return this.mode
174 : }
175 :
176 : reset(): void {
177 0 : if (!this.targetObject) return
178 :
179 0 : this.targetObject.position.copy(this.initialPosition)
180 0 : this.targetObject.rotation.copy(this.initialRotation)
181 0 : this.targetObject.scale.copy(this.initialScale)
182 0 : this.onTransformChange?.()
183 : }
184 :
185 : applyTransform(position: Vector3, rotation: Vector3, scale?: Vector3): void {
186 2 : if (!this.targetObject) return
187 2 : this.targetObject.position.set(position.x, position.y, position.z)
188 2 : this.targetObject.rotation.set(rotation.x, rotation.y, rotation.z)
189 2 : if (scale) {
190 2 : this.targetObject.scale.set(scale.x, scale.y, scale.z)
191 : }
192 : }
193 :
194 : applyModelTransform(transform: Model3DTransform): void {
195 0 : if (!this.targetObject) return
196 0 : this.targetObject.position.set(
197 : transform.position.x,
198 : transform.position.y,
199 : transform.position.z
200 : )
201 0 : this.targetObject.quaternion.set(
202 : transform.quaternion.x,
203 : transform.quaternion.y,
204 : transform.quaternion.z,
205 : transform.quaternion.w
206 : )
207 0 : this.targetObject.scale.set(
208 : transform.scale.x,
209 : transform.scale.y,
210 : transform.scale.z
211 : )
212 0 : this.onTransformChange?.()
213 : }
214 :
215 : getInitialTransform(): {
216 : position: Vector3
217 : rotation: Vector3
218 : scale: Vector3
219 : } {
220 2 : return {
221 : position: {
222 : x: this.initialPosition.x,
223 : y: this.initialPosition.y,
224 : z: this.initialPosition.z
225 : },
226 : rotation: {
227 : x: this.initialRotation.x,
228 : y: this.initialRotation.y,
229 : z: this.initialRotation.z
230 : },
231 : scale: {
232 : x: this.initialScale.x,
233 : y: this.initialScale.y,
234 : z: this.initialScale.z
235 : }
236 : }
237 : }
238 :
239 : getTransform(): {
240 : position: Vector3
241 : rotation: Vector3
242 : scale: Vector3
243 : } {
244 2 : if (!this.targetObject) {
245 0 : return {
246 : position: { x: 0, y: 0, z: 0 },
247 : rotation: { x: 0, y: 0, z: 0 },
248 : scale: { x: 1, y: 1, z: 1 }
249 : }
250 : }
251 :
252 2 : return {
253 : position: {
254 : x: this.targetObject.position.x,
255 : y: this.targetObject.position.y,
256 : z: this.targetObject.position.z
257 : },
258 : rotation: {
259 : x: this.targetObject.rotation.x,
260 : y: this.targetObject.rotation.y,
261 : z: this.targetObject.rotation.z
262 : },
263 : scale: {
264 : x: this.targetObject.scale.x,
265 : y: this.targetObject.scale.y,
266 : z: this.targetObject.scale.z
267 : }
268 : }
269 : }
270 :
271 : getModelInfo(): Model3DTransform | null {
272 22 : const object = this.targetObject
273 22 : if (!object) return null
274 :
275 18 : return {
276 : position: {
277 : x: object.position.x,
278 : y: object.position.y,
279 : z: object.position.z
280 : },
281 : quaternion: {
282 : x: object.quaternion.x,
283 : y: object.quaternion.y,
284 : z: object.quaternion.z,
285 : w: object.quaternion.w
286 : },
287 : scale: {
288 : x: object.scale.x,
289 : y: object.scale.y,
290 : z: object.scale.z
291 : }
292 : }
293 : }
294 :
295 : dispose(): void {
296 1 : if (this.transformControls) {
297 1 : const helper = this.transformControls.getHelper()
298 1 : this.scene.remove(helper)
299 1 : this.transformControls.detach()
300 1 : this.transformControls.dispose()
301 1 : this.transformControls = null
302 : }
303 :
304 1 : this.targetObject = null
305 : }
306 : }
|