Line data Source code
1 : import type {
2 : Size2D,
3 : Bounds as ViewportRect
4 : } from '@comfyorg/shared-frontend-utils/geometry'
5 : import * as THREE from 'three'
6 :
7 : import type { RendererView } from '#/three/RendererView'
8 :
9 : import type { CameraManager } from './CameraManager'
10 : import type { ControlsManager } from './ControlsManager'
11 : import type { EventManager } from './EventManager'
12 : import type { LightingManager } from './LightingManager'
13 : import type { SceneManager } from './SceneManager'
14 : import type { ViewHelperManager } from './ViewHelperManager'
15 : import type {
16 : CameraState,
17 : EventCallback,
18 : Load3DOptions,
19 : SceneOverlay
20 : } from './interfaces'
21 : import { attachContextMenuGuard } from './load3dContextMenuGuard'
22 : import type { RenderLoopHandle } from './load3dRenderLoop'
23 : import { startRenderLoop } from './load3dRenderLoop'
24 : import type { LetterboxedViewport, LetterboxNdc } from './load3dViewport'
25 : import {
26 : clientPointToLetterboxNdc,
27 : computeLetterboxBars,
28 : computeLetterboxedViewport,
29 : isLoad3dActive,
30 : normalize
31 : } from './load3dViewport'
32 :
33 24 : const LETTERBOX_CLEAR_COLOR = 0x0a0a0a
34 24 : const LETTERBOX_DIM_OPACITY = 0.5
35 :
36 : type LetterboxDimmer = {
37 : scene: THREE.Scene
38 : camera: THREE.OrthographicCamera
39 : geometry: THREE.PlaneGeometry
40 : material: THREE.MeshBasicMaterial
41 : }
42 :
43 919 : function supportsViewOffset(
44 : camera: THREE.Camera
45 : ): camera is THREE.PerspectiveCamera | THREE.OrthographicCamera {
46 919 : return (
47 : camera instanceof THREE.PerspectiveCamera ||
48 : camera instanceof THREE.OrthographicCamera
49 : )
50 : }
51 :
52 23 : function createLetterboxDimmer(): LetterboxDimmer {
53 23 : const geometry = new THREE.PlaneGeometry(2, 2)
54 23 : const material = new THREE.MeshBasicMaterial({
55 : color: 0x000000,
56 : transparent: true,
57 : opacity: LETTERBOX_DIM_OPACITY,
58 : depthTest: false,
59 : depthWrite: false
60 : })
61 23 : const scene = new THREE.Scene()
62 23 : scene.add(new THREE.Mesh(geometry, material))
63 : const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, -1, 1)
64 23 : return { scene, camera, geometry, material }
65 : }
66 :
67 24 : const VIEW_HELPER_SIZE = 128
68 :
69 : export type Viewport3dDeps = {
70 : view: RendererView
71 : eventManager: EventManager
72 : sceneManager: SceneManager
73 : cameraManager: CameraManager
74 : controlsManager: ControlsManager
75 : lightingManager: LightingManager
76 : viewHelperManager: ViewHelperManager
77 : }
78 :
79 24 : export class Viewport3d {
80 : protected readonly view: RendererView
81 : protected timer: THREE.Timer
82 : protected renderLoop: RenderLoopHandle | null = null
83 : private onContextMenuCallback?: (event: MouseEvent) => void
84 : private getDimensionsCallback?: () => Size2D | null
85 :
86 : eventManager: EventManager
87 : sceneManager: SceneManager
88 : cameraManager: CameraManager
89 : controlsManager: ControlsManager
90 : lightingManager: LightingManager
91 : viewHelperManager: ViewHelperManager
92 :
93 : STATUS_MOUSE_ON_NODE: boolean
94 : STATUS_MOUSE_ON_SCENE: boolean
95 : STATUS_MOUSE_ON_VIEWER: boolean
96 : INITIAL_RENDER_DONE: boolean = false
97 :
98 : targetWidth: number = 0
99 : targetHeight: number = 0
100 : targetAspectRatio: number = 1
101 : isViewerMode: boolean = false
102 :
103 : private disposeContextMenuGuard: (() => void) | null = null
104 : private getZoomScaleCallback: (() => number) | undefined
105 : private externalActiveCamera: THREE.Camera | null = null
106 : private overlay: SceneOverlay | null = null
107 : private initialRenderTimer: ReturnType<typeof setTimeout> | null = null
108 : private viewPixelScale = 1
109 : private letterboxDimmer: LetterboxDimmer | null = null
110 :
111 : constructor(
112 : container: HTMLElement,
113 : deps: Viewport3dDeps,
114 : options: Load3DOptions = {}
115 : ) {
116 27 : this.view = deps.view
117 27 : this.timer = new THREE.Timer()
118 27 : this.isViewerMode = options.isViewerMode || false
119 27 : this.onContextMenuCallback = options.onContextMenu
120 27 : this.getDimensionsCallback = options.getDimensions
121 27 : this.getZoomScaleCallback = options.getZoomScale
122 :
123 27 : if (options.width !== undefined && options.height !== undefined) {
124 23 : this.applyTargetSize(options.width, options.height)
125 : }
126 :
127 27 : this.eventManager = deps.eventManager
128 27 : this.sceneManager = deps.sceneManager
129 27 : this.cameraManager = deps.cameraManager
130 27 : this.controlsManager = deps.controlsManager
131 27 : this.lightingManager = deps.lightingManager
132 27 : this.viewHelperManager = deps.viewHelperManager
133 :
134 27 : this.sceneManager.init()
135 27 : this.cameraManager.init()
136 27 : this.controlsManager.init()
137 27 : this.lightingManager.init()
138 :
139 27 : this.viewHelperManager.createViewHelper(container)
140 27 : this.viewHelperManager.init()
141 :
142 27 : this.STATUS_MOUSE_ON_NODE = false
143 27 : this.STATUS_MOUSE_ON_SCENE = false
144 27 : this.STATUS_MOUSE_ON_VIEWER = false
145 :
146 27 : this.initContextMenu()
147 27 : this.view.observeResize(container, () => this.handleResize())
148 : }
149 :
150 : get renderer(): THREE.WebGLRenderer {
151 5834 : return this.view.renderer
152 : }
153 :
154 : get rendererView(): RendererView {
155 0 : return this.view
156 : }
157 :
158 : get domElement(): HTMLCanvasElement {
159 4 : return this.view.canvas
160 : }
161 :
162 : start(): void {
163 27 : if (this.hasStarted) return
164 27 : this.hasStarted = true
165 27 : this.handleResize()
166 27 : this.startAnimation()
167 27 : this.initialRenderTimer = setTimeout(() => {
168 27 : this.initialRenderTimer = null
169 27 : this.forceRender()
170 : }, 100)
171 : }
172 :
173 : private hasStarted: boolean = false
174 :
175 : private applyTargetSize(width: number, height: number): void {
176 1094 : if (!Number.isFinite(width) || !Number.isFinite(height)) return
177 1094 : if (width <= 0 || height <= 0) return
178 1094 : this.targetWidth = width
179 1094 : this.targetHeight = height
180 1094 : this.targetAspectRatio = width / height
181 : }
182 :
183 : private initContextMenu(): void {
184 27 : this.disposeContextMenuGuard = attachContextMenuGuard(
185 : this.view.canvas,
186 : (event) => this.onContextMenuCallback?.(event),
187 : { isDisabled: () => this.isViewerMode }
188 : )
189 : }
190 :
191 : getEventManager(): EventManager {
192 0 : return this.eventManager
193 : }
194 : getSceneManager(): SceneManager {
195 8 : return this.sceneManager
196 : }
197 : getCameraManager(): CameraManager {
198 2 : return this.cameraManager
199 : }
200 : getControlsManager(): ControlsManager {
201 0 : return this.controlsManager
202 : }
203 : getLightingManager(): LightingManager {
204 2 : return this.lightingManager
205 : }
206 : getViewHelperManager(): ViewHelperManager {
207 0 : return this.viewHelperManager
208 : }
209 :
210 : getTargetSize(): Size2D {
211 0 : return {
212 : width: this.targetWidth,
213 : height: this.targetHeight
214 : }
215 : }
216 :
217 : protected shouldMaintainAspectRatio(): boolean {
218 1150 : return this.isViewerMode || (this.targetWidth > 0 && this.targetHeight > 0)
219 : }
220 :
221 : forceRender(): void {
222 573 : const delta = this.timer.update().getDelta()
223 573 : this.tickPerFrame(delta)
224 573 : this.renderView()
225 573 : this.INITIAL_RENDER_DONE = true
226 : }
227 :
228 : private renderView(): void {
229 999 : this.renderer.state.reset()
230 999 : this.view.beginRender(this.sceneManager.hasSplats())
231 999 : this.runPreRenderCallbacks()
232 999 : this.renderMainScene()
233 999 : this.runPostRenderCallbacks()
234 :
235 999 : this.view.setScissorTest(false)
236 999 : this.viewHelperManager.render(
237 : this.view,
238 : VIEW_HELPER_SIZE * this.viewPixelScale
239 : )
240 :
241 999 : this.view.blit()
242 : }
243 :
244 : private readonly preRenderCallbacks: Array<() => void> = []
245 : private readonly postRenderCallbacks: Array<() => void> = []
246 :
247 : addPreRenderCallback(cb: () => void): () => void {
248 0 : const registered = () => cb()
249 0 : this.preRenderCallbacks.push(registered)
250 0 : return () => {
251 0 : const i = this.preRenderCallbacks.indexOf(registered)
252 0 : if (i >= 0) this.preRenderCallbacks.splice(i, 1)
253 : }
254 : }
255 :
256 : addPostRenderCallback(cb: () => void): () => void {
257 0 : const registered = () => cb()
258 0 : this.postRenderCallbacks.push(registered)
259 0 : return () => {
260 0 : const i = this.postRenderCallbacks.indexOf(registered)
261 0 : if (i >= 0) this.postRenderCallbacks.splice(i, 1)
262 : }
263 : }
264 :
265 : private runPreRenderCallbacks(): void {
266 999 : for (const cb of Array.from(this.preRenderCallbacks)) cb()
267 : }
268 :
269 : private runPostRenderCallbacks(): void {
270 999 : for (const cb of Array.from(this.postRenderCallbacks)) cb()
271 : }
272 :
273 : protected tickPerFrame(delta: number): void {
274 999 : this.overlay?.update?.(delta)
275 999 : this.viewHelperManager.update(delta)
276 999 : this.controlsManager.update()
277 : }
278 :
279 : getRenderCamera(): THREE.Camera {
280 999 : return this.externalActiveCamera ?? this.cameraManager.activeCamera
281 : }
282 :
283 : setExternalActiveCamera(camera: THREE.Camera | null): void {
284 0 : if (this.externalActiveCamera === camera) return
285 0 : this.externalActiveCamera = camera
286 0 : if (camera) {
287 0 : this.controlsManager.detach()
288 0 : this.viewHelperManager.visibleViewHelper(false)
289 : } else {
290 0 : this.controlsManager.attach()
291 0 : this.viewHelperManager.visibleViewHelper(true)
292 : }
293 0 : this.overlay?.onActiveCameraChange?.(this.getRenderCamera())
294 0 : this.forceRender()
295 : }
296 :
297 : setOverlay(overlay: SceneOverlay): void {
298 0 : if (this.overlay === overlay) return
299 0 : if (this.overlay) {
300 0 : this.overlay.detach()
301 0 : this.overlay.dispose()
302 : }
303 0 : this.overlay = overlay
304 0 : overlay.attach(this.sceneManager.scene)
305 0 : overlay.onActiveCameraChange?.(this.getRenderCamera())
306 0 : this.forceRender()
307 : }
308 :
309 : removeOverlay(): void {
310 0 : if (!this.overlay) return
311 0 : this.overlay.detach()
312 0 : this.overlay.dispose()
313 0 : this.overlay = null
314 0 : this.forceRender()
315 : }
316 :
317 : getOverlay(): SceneOverlay | null {
318 0 : return this.overlay
319 : }
320 :
321 : renderMainScene(): void {
322 999 : const viewWidth = this.view.width
323 999 : const viewHeight = this.view.height
324 :
325 999 : if (this.getDimensionsCallback) {
326 919 : const dims = this.getDimensionsCallback()
327 919 : if (dims) {
328 919 : this.applyTargetSize(dims.width, dims.height)
329 : }
330 : }
331 :
332 999 : this.view.setViewport(0, 0, viewWidth, viewHeight)
333 999 : this.view.setScissor(0, 0, viewWidth, viewHeight)
334 999 : this.view.setScissorTest(true)
335 :
336 999 : if (!this.shouldMaintainAspectRatio()) {
337 80 : this.renderer.setClearColor(
338 : this.view.state.clearColor,
339 : this.view.state.clearAlpha
340 : )
341 80 : this.renderer.clear()
342 80 : this.sceneManager.renderBackground()
343 80 : this.renderer.render(this.sceneManager.scene, this.getRenderCamera())
344 80 : return
345 : }
346 :
347 919 : const container = { width: viewWidth, height: viewHeight }
348 919 : const viewport = computeLetterboxedViewport(
349 : container,
350 : this.targetAspectRatio
351 : )
352 :
353 919 : this.renderer.setClearColor(LETTERBOX_CLEAR_COLOR)
354 919 : this.renderer.clear()
355 :
356 919 : this.cameraManager.updateAspectRatio(viewport.width / viewport.height)
357 :
358 919 : const camera = this.getRenderCamera()
359 :
360 963 : if (!supportsViewOffset(camera)) {
361 0 : this.view.setViewport(
362 : viewport.offsetX,
363 : viewport.offsetY,
364 : viewport.width,
365 : viewport.height
366 : )
367 0 : this.view.setScissor(
368 : viewport.offsetX,
369 : viewport.offsetY,
370 : viewport.width,
371 : viewport.height
372 : )
373 0 : this.sceneManager.renderBackground()
374 0 : this.renderer.render(this.sceneManager.scene, camera)
375 0 : return
376 : }
377 :
378 919 : camera.setViewOffset(
379 : viewport.width,
380 : viewport.height,
381 : -viewport.offsetX,
382 : -viewport.offsetY,
383 : viewWidth,
384 : viewHeight
385 : )
386 :
387 919 : this.renderLetterboxedBackground(viewport)
388 919 : this.renderer.render(this.sceneManager.scene, camera)
389 919 : camera.clearViewOffset()
390 :
391 919 : this.dimLetterboxBars(computeLetterboxBars(container, viewport))
392 : }
393 :
394 : private renderLetterboxedBackground(viewport: LetterboxedViewport): void {
395 919 : if (this.sceneManager.getCurrentBackgroundInfo().type !== 'image') {
396 876 : this.sceneManager.renderBackground()
397 876 : return
398 : }
399 :
400 43 : this.view.setViewport(
401 : viewport.offsetX,
402 : viewport.offsetY,
403 : viewport.width,
404 : viewport.height
405 : )
406 43 : this.view.setScissor(
407 : viewport.offsetX,
408 : viewport.offsetY,
409 : viewport.width,
410 : viewport.height
411 : )
412 43 : this.sceneManager.renderBackground()
413 43 : this.view.setViewport(0, 0, this.view.width, this.view.height)
414 43 : this.view.setScissor(0, 0, this.view.width, this.view.height)
415 : }
416 :
417 : private dimLetterboxBars(bars: ViewportRect[]): void {
418 919 : if (bars.length === 0) return
419 :
420 919 : const dimmer = (this.letterboxDimmer ??= createLetterboxDimmer())
421 919 : for (const bar of bars) {
422 1838 : this.view.setViewport(bar.x, bar.y, bar.width, bar.height)
423 1838 : this.view.setScissor(bar.x, bar.y, bar.width, bar.height)
424 1838 : this.renderer.render(dimmer.scene, dimmer.camera)
425 : }
426 : }
427 :
428 : clientPointToNdc(clientX: number, clientY: number): LetterboxNdc | null {
429 0 : const rect = this.domElement.getBoundingClientRect()
430 0 : if (rect.width <= 0 || rect.height <= 0) return null
431 0 : return clientPointToLetterboxNdc(
432 : normalize(clientX, rect.left, rect.right),
433 : normalize(clientY, rect.top, rect.bottom),
434 : { width: rect.width, height: rect.height },
435 : this.shouldMaintainAspectRatio() ? this.targetAspectRatio : null
436 : )
437 : }
438 :
439 : protected startAnimation(): void {
440 27 : this.renderLoop = startRenderLoop({
441 : tick: () => {
442 426 : const delta = this.timer.update().getDelta()
443 426 : this.tickPerFrame(delta)
444 426 : this.renderView()
445 : },
446 : isActive: () => this.isActive()
447 : })
448 : }
449 :
450 : updateStatusMouseOnNode(onNode: boolean): void {
451 0 : this.STATUS_MOUSE_ON_NODE = onNode
452 0 : this.renderLoop?.wake()
453 : }
454 :
455 : updateStatusMouseOnScene(onScene: boolean): void {
456 29 : this.STATUS_MOUSE_ON_SCENE = onScene
457 29 : this.renderLoop?.wake()
458 : }
459 :
460 : updateStatusMouseOnViewer(onViewer: boolean): void {
461 2 : this.STATUS_MOUSE_ON_VIEWER = onViewer
462 2 : this.renderLoop?.wake()
463 : }
464 :
465 : isActive(): boolean {
466 0 : return isLoad3dActive({
467 : mouseOnNode: this.STATUS_MOUSE_ON_NODE,
468 : mouseOnScene: this.STATUS_MOUSE_ON_SCENE,
469 : mouseOnViewer: this.STATUS_MOUSE_ON_VIEWER,
470 : recording: false,
471 : initialRenderDone: this.INITIAL_RENDER_DONE,
472 : animationPlaying: false
473 : })
474 : }
475 :
476 : toggleCamera(cameraType?: 'perspective' | 'orthographic'): void {
477 52 : this.cameraManager.toggleCamera(cameraType)
478 52 : this.controlsManager.updateCamera(this.cameraManager.activeCamera)
479 52 : this.onActiveCameraChanged()
480 52 : this.viewHelperManager.recreateViewHelper()
481 52 : if (!this.externalActiveCamera) {
482 52 : this.overlay?.onActiveCameraChange?.(this.cameraManager.activeCamera)
483 : }
484 52 : this.handleResize()
485 : }
486 :
487 : protected onActiveCameraChanged(): void {}
488 :
489 : getCurrentCameraType(): 'perspective' | 'orthographic' {
490 2 : return this.cameraManager.getCurrentCameraType()
491 : }
492 :
493 : setCameraState(state: CameraState): void {
494 6 : this.cameraManager.setCameraState(state)
495 6 : this.forceRender()
496 : }
497 :
498 : getCameraState(): CameraState {
499 7 : return this.cameraManager.getCameraState()
500 : }
501 :
502 : setUseCustomUp(use: boolean): void {
503 0 : this.cameraManager.setUseCustomUp(use)
504 0 : this.forceRender()
505 : }
506 :
507 : setTargetSize(width: number, height: number): void {
508 21 : this.applyTargetSize(width, height)
509 21 : this.handleResize()
510 : }
511 :
512 : addEventListener<T>(event: string, callback: EventCallback<T>): void {
513 604 : this.eventManager.addEventListener(event, callback)
514 : }
515 :
516 : removeEventListener<T>(event: string, callback: EventCallback<T>): void {
517 0 : this.eventManager.removeEventListener(event, callback)
518 : }
519 :
520 : refreshViewport(): void {
521 0 : this.handleResize()
522 : }
523 :
524 : handleResize(): void {
525 149 : const parentElement = this.view.canvas.parentElement
526 :
527 149 : if (!parentElement) {
528 0 : console.warn('Parent element not found')
529 0 : return
530 : }
531 :
532 149 : const containerWidth = parentElement.clientWidth
533 149 : const containerHeight = parentElement.clientHeight
534 :
535 149 : const zoomScale = this.getZoomScaleCallback?.() ?? 1
536 149 : this.viewPixelScale = Math.min(zoomScale, 3)
537 :
538 149 : if (this.getDimensionsCallback) {
539 131 : const dims = this.getDimensionsCallback()
540 131 : if (dims) {
541 131 : this.applyTargetSize(dims.width, dims.height)
542 : }
543 : }
544 :
545 149 : this.view.setSize(
546 : containerWidth * this.viewPixelScale,
547 : containerHeight * this.viewPixelScale
548 : )
549 :
550 149 : if (this.shouldMaintainAspectRatio()) {
551 131 : const { width, height } = computeLetterboxedViewport(
552 : { width: containerWidth, height: containerHeight },
553 : this.targetAspectRatio
554 : )
555 :
556 131 : this.cameraManager.handleResize(width, height)
557 131 : this.sceneManager.handleResize(width, height)
558 : } else {
559 18 : this.cameraManager.handleResize(containerWidth, containerHeight)
560 18 : this.sceneManager.handleResize(containerWidth, containerHeight)
561 : }
562 :
563 149 : this.forceRender()
564 : }
565 :
566 : remove(): void {
567 1 : if (this.initialRenderTimer) {
568 0 : clearTimeout(this.initialRenderTimer)
569 0 : this.initialRenderTimer = null
570 : }
571 :
572 1 : this.disposeContextMenuGuard?.()
573 1 : this.disposeContextMenuGuard = null
574 :
575 1 : this.renderLoop?.stop()
576 1 : this.renderLoop = null
577 :
578 1 : this.disposeManagers()
579 :
580 1 : this.view.dispose()
581 : }
582 :
583 : protected disposeManagers(): void {
584 1 : if (this.overlay) {
585 0 : this.overlay.detach()
586 0 : this.overlay.dispose()
587 0 : this.overlay = null
588 : }
589 1 : if (this.letterboxDimmer) {
590 1 : this.letterboxDimmer.geometry.dispose()
591 1 : this.letterboxDimmer.material.dispose()
592 1 : this.letterboxDimmer = null
593 : }
594 1 : this.sceneManager.dispose()
595 1 : this.cameraManager.dispose()
596 1 : this.controlsManager.dispose()
597 1 : this.lightingManager.dispose()
598 1 : this.viewHelperManager.dispose()
599 : }
600 : }
|