Line data Source code
1 : import type { Size2D } from '@comfyorg/shared-frontend-utils/geometry'
2 : import { SparkRenderer } from '@sparkjsdev/spark'
3 : import { delay } from 'es-toolkit'
4 : import * as THREE from 'three'
5 : import type { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
6 :
7 : import type { RendererView } from '#/three/RendererView'
8 : import { createHighPrecisionTarget } from '#/three/highPrecisionOutput'
9 :
10 : import { QuadWireframeOverlay } from './quadWireframe/QuadWireframeManager'
11 : import type {
12 : BackgroundRenderModeType,
13 : EventManagerInterface,
14 : SceneManagerInterface
15 : } from './interfaces'
16 :
17 24 : const SPLAT_SORT_TIMEOUT_MS = 5_000
18 24 : const SPLAT_SORT_POLL_MS = 16
19 :
20 24 : export class SceneManager implements SceneManagerInterface {
21 : scene!: THREE.Scene
22 : gridHelper: THREE.GridHelper
23 : private sparkRenderer: SparkRenderer
24 :
25 : private nextSparkDirtyPromise: Promise<void> | null = null
26 : private nextSparkDirtyResolve: (() => void) | null = null
27 :
28 : awaitNextSparkDirty(): Promise<void> {
29 0 : if (this.nextSparkDirtyPromise) return this.nextSparkDirtyPromise
30 0 : this.nextSparkDirtyPromise = new Promise<void>((resolve) => {
31 0 : this.nextSparkDirtyResolve = resolve
32 : })
33 0 : return this.nextSparkDirtyPromise
34 : }
35 :
36 : async whenSplatsSorted(camera: THREE.Camera): Promise<void> {
37 0 : const spark = this.sparkRenderer
38 0 : await spark.update({ scene: this.scene, camera })
39 0 : const deadline = performance.now() + SPLAT_SORT_TIMEOUT_MS
40 0 : while ((spark.sorting || spark.sortDirty) && performance.now() < deadline) {
41 0 : await delay(SPLAT_SORT_POLL_MS)
42 : }
43 : }
44 :
45 : hasSplats(): boolean {
46 1002 : return this.sparkRenderer.activeSplats > 0
47 : }
48 :
49 : backgroundScene!: THREE.Scene
50 : backgroundCamera: THREE.OrthographicCamera
51 : backgroundMesh: THREE.Mesh | null = null
52 : backgroundTexture: THREE.Texture | null = null
53 :
54 : backgroundRenderMode: 'tiled' | 'panorama' = 'tiled'
55 :
56 : backgroundColorMaterial: THREE.MeshBasicMaterial | null = null
57 : currentBackgroundType: 'color' | 'image' = 'color'
58 : currentBackgroundColor: string = '#282828'
59 :
60 : private eventManager: EventManagerInterface
61 : private renderer: THREE.WebGLRenderer
62 : private view: RendererView
63 :
64 : private getActiveCamera: () => THREE.Camera
65 :
66 : constructor(
67 : view: RendererView,
68 : getActiveCamera: () => THREE.Camera,
69 : _getControls: () => OrbitControls,
70 : eventManager: EventManagerInterface
71 : ) {
72 27 : this.view = view
73 27 : this.renderer = view.renderer
74 27 : this.eventManager = eventManager
75 27 : this.scene = new THREE.Scene()
76 :
77 27 : this.scene.name = 'MainScene'
78 :
79 27 : this.getActiveCamera = getActiveCamera
80 :
81 : // Spark 2.x requires a SparkRenderer in the scene tree to render SplatMesh
82 : // instances; without it splats are silent no-ops.
83 : //
84 : // onDirty fires twice per splat first-paint cycle: once from updateInternal
85 : // (data uploaded) and again from driveSort (sort completed; line 1105 in
86 : // SparkRenderer.ts). We expose it as a passive promise — awaiters get
87 : // notified, but the callback itself does NOT trigger a render. Wiring
88 : // forceRender directly into onDirty caused a per-frame render-setDirty
89 : // cascade that made splats visibly "balloon" during camera interaction.
90 27 : this.sparkRenderer = new SparkRenderer({
91 : renderer: view.renderer,
92 : onDirty: () => {
93 499 : const resolve = this.nextSparkDirtyResolve
94 499 : this.nextSparkDirtyResolve = null
95 499 : this.nextSparkDirtyPromise = null
96 499 : resolve?.()
97 : }
98 : })
99 27 : this.scene.add(this.sparkRenderer)
100 :
101 27 : this.gridHelper = new THREE.GridHelper(20, 20)
102 27 : this.gridHelper.position.set(0, 0, 0)
103 27 : this.scene.add(this.gridHelper)
104 :
105 27 : this.backgroundScene = new THREE.Scene()
106 27 : this.backgroundScene.name = 'BackgroundScene'
107 27 : this.backgroundCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, -1, 1)
108 :
109 27 : this.initBackgroundScene()
110 : }
111 :
112 : private initBackgroundScene(): void {
113 27 : const planeGeometry = new THREE.PlaneGeometry(2, 2)
114 :
115 27 : this.backgroundColorMaterial = new THREE.MeshBasicMaterial({
116 : color: new THREE.Color(this.currentBackgroundColor),
117 : transparent: false,
118 : depthWrite: false,
119 : depthTest: false,
120 : side: THREE.DoubleSide
121 : })
122 :
123 27 : this.backgroundMesh = new THREE.Mesh(
124 : planeGeometry,
125 : this.backgroundColorMaterial
126 : )
127 27 : this.backgroundMesh.position.set(0, 0, 0)
128 27 : this.backgroundScene.add(this.backgroundMesh)
129 :
130 27 : this.view.state.clearColor.set(0x000000)
131 27 : this.view.state.clearAlpha = 0
132 : }
133 :
134 : init(): void {}
135 :
136 : dispose(): void {
137 1 : if (this.backgroundTexture) {
138 0 : this.backgroundTexture.dispose()
139 : }
140 :
141 1 : if (this.backgroundColorMaterial) {
142 1 : this.backgroundColorMaterial.dispose()
143 : }
144 :
145 1 : if (this.backgroundMesh) {
146 1 : this.backgroundMesh.geometry.dispose()
147 1 : if (this.backgroundMesh.material instanceof THREE.Material) {
148 1 : this.backgroundMesh.material.dispose()
149 : }
150 : }
151 :
152 1 : if (this.scene.background) {
153 0 : this.scene.background = null
154 : }
155 :
156 1 : this.backgroundScene.clear()
157 :
158 1 : this.scene.clear()
159 : }
160 :
161 : toggleGrid(showGrid: boolean): void {
162 54 : this.gridHelper.visible = showGrid
163 :
164 54 : this.eventManager.emitEvent('showGridChange', showGrid)
165 : }
166 :
167 : setBackgroundColor(color: string): void {
168 59 : this.currentBackgroundColor = color
169 59 : this.currentBackgroundType = 'color'
170 :
171 59 : if (this.scene.background instanceof THREE.Texture) {
172 1 : this.scene.background = null
173 : }
174 :
175 59 : if (this.backgroundRenderMode === 'panorama') {
176 1 : this.backgroundRenderMode = 'tiled'
177 1 : this.eventManager.emitEvent('backgroundRenderModeChange', 'tiled')
178 : }
179 :
180 59 : if (!this.backgroundMesh || !this.backgroundColorMaterial) {
181 0 : this.initBackgroundScene()
182 : }
183 :
184 59 : this.backgroundColorMaterial!.color.set(color)
185 59 : this.backgroundColorMaterial!.map = null
186 59 : this.backgroundColorMaterial!.transparent = false
187 59 : this.backgroundColorMaterial!.needsUpdate = true
188 :
189 59 : if (this.backgroundMesh) {
190 59 : this.backgroundMesh.material = this.backgroundColorMaterial!
191 : }
192 :
193 59 : if (this.backgroundTexture) {
194 1 : this.backgroundTexture.dispose()
195 1 : this.backgroundTexture = null
196 : }
197 :
198 59 : this.eventManager.emitEvent('backgroundColorChange', color)
199 : }
200 :
201 : async setBackgroundImage(
202 : uploadPath: string,
203 : imageUrl: string
204 : ): Promise<void> {
205 6 : if (uploadPath === '') {
206 4 : this.setBackgroundColor(this.currentBackgroundColor)
207 :
208 4 : return
209 : }
210 :
211 2 : this.eventManager.emitEvent('backgroundImageLoadingStart', null)
212 :
213 4 : try {
214 2 : const textureLoader = new THREE.TextureLoader()
215 2 : const texture = await new Promise<THREE.Texture>((resolve, reject) => {
216 2 : textureLoader.load(imageUrl, resolve, undefined, reject)
217 : })
218 :
219 4 : if (this.backgroundTexture) {
220 1 : this.backgroundTexture.dispose()
221 : }
222 :
223 2 : texture.colorSpace = THREE.SRGBColorSpace
224 :
225 2 : this.backgroundTexture = texture
226 2 : this.currentBackgroundType = 'image'
227 :
228 4 : if (this.backgroundRenderMode === 'panorama') {
229 0 : texture.mapping = THREE.EquirectangularReflectionMapping
230 0 : this.scene.background = texture
231 : } else {
232 2 : if (!this.backgroundMesh) {
233 0 : this.initBackgroundScene()
234 : }
235 :
236 2 : const imageMaterial = new THREE.MeshBasicMaterial({
237 : map: texture,
238 : transparent: true,
239 : depthWrite: false,
240 : depthTest: false,
241 : side: THREE.DoubleSide
242 : })
243 :
244 2 : if (this.backgroundMesh) {
245 2 : if (
246 : this.backgroundMesh.material !== this.backgroundColorMaterial &&
247 : this.backgroundMesh.material instanceof THREE.Material
248 : ) {
249 1 : this.backgroundMesh.material.dispose()
250 : }
251 :
252 2 : this.backgroundMesh.material = imageMaterial
253 2 : this.backgroundMesh.position.set(0, 0, 0)
254 : }
255 :
256 2 : this.updateBackgroundSize(
257 : this.backgroundTexture,
258 : this.backgroundMesh,
259 : this.view.canvas.clientWidth,
260 : this.view.canvas.clientHeight
261 : )
262 : }
263 :
264 2 : this.eventManager.emitEvent('backgroundImageChange', uploadPath)
265 2 : this.eventManager.emitEvent('backgroundImageLoadingEnd', null)
266 : } catch (error) {
267 0 : this.eventManager.emitEvent('backgroundImageLoadingEnd', null)
268 0 : console.error('Error loading background image:', error)
269 0 : this.setBackgroundColor(this.currentBackgroundColor)
270 : }
271 : }
272 :
273 : removeBackgroundImage(): void {
274 0 : this.setBackgroundColor(this.currentBackgroundColor)
275 0 : this.eventManager.emitEvent('backgroundImageLoadingEnd', null)
276 : }
277 :
278 : setBackgroundRenderMode(mode: BackgroundRenderModeType): void {
279 27 : if (this.backgroundRenderMode === mode) return
280 :
281 1 : this.backgroundRenderMode = mode
282 :
283 27 : if (this.currentBackgroundType === 'image' && this.backgroundTexture) {
284 1 : try {
285 1 : if (mode === 'panorama') {
286 1 : this.backgroundTexture.mapping =
287 : THREE.EquirectangularReflectionMapping
288 1 : this.scene.background = this.backgroundTexture
289 : } else {
290 0 : this.scene.background = null
291 0 : if (
292 : this.backgroundMesh &&
293 : this.backgroundMesh.material instanceof THREE.MeshBasicMaterial
294 : ) {
295 0 : this.backgroundMesh.material.map = this.backgroundTexture
296 0 : this.backgroundMesh.material.needsUpdate = true
297 : }
298 : }
299 : } catch (error) {
300 0 : console.error('Error set background render mode:', error)
301 : }
302 : }
303 :
304 1 : this.eventManager.emitEvent('backgroundRenderModeChange', mode)
305 : }
306 :
307 : updateBackgroundSize(
308 : backgroundTexture: THREE.Texture | null,
309 : backgroundMesh: THREE.Mesh | null,
310 : targetWidth: number,
311 : targetHeight: number
312 : ): void {
313 4 : if (!backgroundTexture || !backgroundMesh) return
314 :
315 4 : const material = backgroundMesh.material as THREE.MeshBasicMaterial
316 :
317 4 : if (!material.map) return
318 :
319 4 : const image = backgroundTexture.image as Size2D
320 4 : const imageAspect = image.width / image.height
321 4 : const targetAspect = targetWidth / targetHeight
322 :
323 4 : if (imageAspect > targetAspect) {
324 0 : backgroundMesh.scale.set(imageAspect / targetAspect, 1, 1)
325 : } else {
326 4 : backgroundMesh.scale.set(1, targetAspect / imageAspect, 1)
327 : }
328 :
329 4 : material.needsUpdate = true
330 : }
331 :
332 : handleResize(width: number, height: number): void {
333 152 : if (
334 : this.backgroundTexture &&
335 : this.backgroundMesh &&
336 : this.currentBackgroundType === 'image'
337 : ) {
338 0 : this.updateBackgroundSize(
339 : this.backgroundTexture,
340 : this.backgroundMesh,
341 : width,
342 : height
343 : )
344 : }
345 : }
346 :
347 : renderBackground(): void {
348 1002 : if (
349 : (this.backgroundRenderMode === 'tiled' ||
350 : this.currentBackgroundType === 'color') &&
351 : this.backgroundMesh
352 : ) {
353 982 : const currentToneMapping = this.renderer.toneMapping
354 982 : const currentExposure = this.renderer.toneMappingExposure
355 :
356 982 : this.renderer.toneMapping = THREE.NoToneMapping
357 982 : this.renderer.render(this.backgroundScene, this.backgroundCamera)
358 :
359 982 : this.renderer.toneMapping = currentToneMapping
360 982 : this.renderer.toneMappingExposure = currentExposure
361 : }
362 : }
363 :
364 : getCurrentBackgroundInfo(): { type: 'color' | 'image'; value: string } {
365 923 : return {
366 : type: this.currentBackgroundType,
367 : value:
368 : this.currentBackgroundType === 'color'
369 : ? this.currentBackgroundColor
370 : : ''
371 : }
372 : }
373 :
374 : async captureScene(
375 : width: number,
376 : height: number
377 : ): Promise<{ scene: string; mask: string; normal: string }> {
378 3 : this.view.beginRender()
379 :
380 3 : const originalSize = new THREE.Vector2()
381 3 : this.renderer.getSize(originalSize)
382 3 : const originalPixelRatio = this.renderer.getPixelRatio()
383 3 : const originalClearColor = this.renderer.getClearColor(new THREE.Color())
384 3 : const originalClearAlpha = this.renderer.getClearAlpha()
385 3 : const originalOutputColorSpace = this.renderer.outputColorSpace
386 :
387 3 : const activeCamera = this.getActiveCamera()
388 3 : const savedCameraParams =
389 : activeCamera instanceof THREE.PerspectiveCamera
390 : ? { type: 'perspective' as const, aspect: activeCamera.aspect }
391 : : {
392 : type: 'orthographic' as const,
393 : left: (activeCamera as THREE.OrthographicCamera).left,
394 : right: (activeCamera as THREE.OrthographicCamera).right,
395 : top: (activeCamera as THREE.OrthographicCamera).top,
396 : bottom: (activeCamera as THREE.OrthographicCamera).bottom
397 : }
398 :
399 3 : const originalMaterials = new Map<
400 : THREE.Mesh,
401 : THREE.Material | THREE.Material[]
402 : >()
403 3 : const tempMaterials: THREE.MeshNormalMaterial[] = []
404 3 : const hiddenOverlays: THREE.Object3D[] = []
405 3 : const gridVisible = this.gridHelper.visible
406 3 : const captureTarget = this.hasSplats()
407 : ? createHighPrecisionTarget(width, height)
408 : : null
409 3 : const capturePass = (draw: () => void): string => {
410 9 : this.view.bindOutput(captureTarget, width, height)
411 9 : draw()
412 9 : this.view.resolveOutput()
413 9 : return this.renderer.domElement.toDataURL('image/png')
414 : }
415 :
416 3 : try {
417 : // Capture at exactly the requested pixel dimensions, independent of
418 : // the current zoom-driven pixel ratio.
419 3 : this.renderer.setPixelRatio(1)
420 3 : this.renderer.setSize(width, height)
421 :
422 3 : if (activeCamera instanceof THREE.PerspectiveCamera) {
423 3 : activeCamera.aspect = width / height
424 3 : activeCamera.updateProjectionMatrix()
425 : } else {
426 0 : const orthographicCamera = activeCamera as THREE.OrthographicCamera
427 :
428 0 : const frustumSize = 10
429 0 : const aspect = width / height
430 :
431 0 : orthographicCamera.left = (-frustumSize * aspect) / 2
432 0 : orthographicCamera.right = (frustumSize * aspect) / 2
433 0 : orthographicCamera.top = frustumSize / 2
434 0 : orthographicCamera.bottom = -frustumSize / 2
435 :
436 0 : orthographicCamera.updateProjectionMatrix()
437 : }
438 :
439 3 : if (
440 : this.backgroundTexture &&
441 : this.backgroundMesh &&
442 : this.currentBackgroundType === 'image'
443 : ) {
444 0 : this.updateBackgroundSize(
445 : this.backgroundTexture,
446 : this.backgroundMesh,
447 : width,
448 : height
449 : )
450 : }
451 :
452 3 : const sceneData = capturePass(() => {
453 3 : this.renderer.clear()
454 3 : this.renderBackground()
455 3 : this.renderer.render(this.scene, activeCamera)
456 : })
457 :
458 3 : this.renderer.setClearColor(0x000000, 0)
459 3 : const maskData = capturePass(() => {
460 3 : this.renderer.clear()
461 3 : this.renderer.render(this.scene, activeCamera)
462 : })
463 :
464 3 : this.scene.traverse((child) => {
465 31 : if (child instanceof THREE.Mesh) {
466 4 : originalMaterials.set(child, child.material)
467 :
468 4 : const tempMaterial = new THREE.MeshNormalMaterial({
469 : flatShading: false,
470 : side: THREE.DoubleSide,
471 : normalScale: new THREE.Vector2(1, 1)
472 : })
473 4 : tempMaterials.push(tempMaterial)
474 4 : child.material = tempMaterial
475 27 : } else if (child instanceof QuadWireframeOverlay && child.visible) {
476 0 : hiddenOverlays.push(child)
477 0 : child.visible = false
478 : }
479 : })
480 :
481 3 : this.gridHelper.visible = false
482 :
483 3 : this.renderer.setClearColor(0x000000, 1)
484 3 : const normalData = capturePass(() => {
485 3 : this.renderer.clear()
486 3 : this.renderer.render(this.scene, activeCamera)
487 : })
488 :
489 3 : this.renderer.setClearColor(0xffffff, 1)
490 3 : this.renderer.clear()
491 :
492 3 : return { scene: sceneData, mask: maskData, normal: normalData }
493 : } finally {
494 3 : this.view.bindOutput(null, width, height)
495 3 : captureTarget?.dispose()
496 3 : this.scene.traverse((child) => {
497 31 : if (child instanceof THREE.Mesh) {
498 4 : const originalMaterial = originalMaterials.get(child)
499 4 : if (originalMaterial) {
500 4 : child.material = originalMaterial
501 : }
502 : }
503 : })
504 3 : for (const mat of tempMaterials) {
505 4 : mat.dispose()
506 : }
507 3 : for (const overlay of hiddenOverlays) overlay.visible = true
508 3 : this.gridHelper.visible = gridVisible
509 3 : if (savedCameraParams.type === 'perspective') {
510 3 : const persp = activeCamera as THREE.PerspectiveCamera
511 3 : persp.aspect = savedCameraParams.aspect
512 3 : persp.updateProjectionMatrix()
513 : } else {
514 0 : const ortho = activeCamera as THREE.OrthographicCamera
515 0 : ortho.left = savedCameraParams.left
516 0 : ortho.right = savedCameraParams.right
517 0 : ortho.top = savedCameraParams.top
518 0 : ortho.bottom = savedCameraParams.bottom
519 0 : ortho.updateProjectionMatrix()
520 : }
521 3 : this.renderer.setClearColor(originalClearColor, originalClearAlpha)
522 3 : this.renderer.setPixelRatio(originalPixelRatio)
523 3 : this.renderer.setSize(originalSize.x, originalSize.y)
524 3 : this.renderer.outputColorSpace = originalOutputColorSpace
525 3 : this.handleResize(
526 : this.view.canvas.clientWidth,
527 : this.view.canvas.clientHeight
528 : )
529 : }
530 : }
531 :
532 : reset(): void {}
533 : }
|