LCOV - code coverage report
Current view: top level - packages/viewport3d/src/load3d - Load3d.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 53.4 % 283 151
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Vector3 } from '@comfyorg/shared-frontend-utils/geometry'
       2              : import * as THREE from 'three'
       3              : import { clone as cloneSkinned } from 'three/examples/jsm/utils/SkeletonUtils'
       4              : 
       5              : import type { AnimationManager } from './AnimationManager'
       6              : import type { GizmoManager } from './GizmoManager'
       7              : import type { HDRIManager } from './HDRIManager'
       8              : import type { LoaderManager } from './LoaderManager'
       9              : import { ModelExporter } from './ModelExporter'
      10              : import { DEFAULT_MODEL_CAPABILITIES } from './ModelAdapter'
      11              : import type { AdapterRef, ModelAdapterCapabilities } from './ModelAdapter'
      12              : import type { RecordingManager } from './RecordingManager'
      13              : import type { SceneModelManager } from './SceneModelManager'
      14              : import { Viewport3d } from './Viewport3d'
      15              : import type { Viewport3dDeps } from './Viewport3d'
      16              : import { adoptClonedModel } from './quadWireframe/adoptClonedModel'
      17              : import { computeCameraFromMatrices } from './cameraFromMatrices'
      18              : import { DIRECT_EXPORT_FORMATS } from './constants'
      19              : import type {
      20              :   CaptureResult,
      21              :   GizmoMode,
      22              :   Load3DOptions,
      23              :   Load3dHost,
      24              :   LoadModelOptions,
      25              :   LoadModelOutcome,
      26              :   MaterialMode,
      27              :   Model3DTransform,
      28              :   UpDirection
      29              : } from './interfaces'
      30              : import { computeLetterboxedViewport, isLoad3dActive } from './load3dViewport'
      31              : import { computeModelStats } from './modelStats'
      32              : import type { ModelStats } from './modelStats'
      33              : 
      34              : export type Load3dDeps = Viewport3dDeps & {
      35              :   hdriManager: HDRIManager
      36              :   loaderManager: LoaderManager
      37              :   modelManager: SceneModelManager
      38              :   recordingManager: RecordingManager
      39              :   animationManager: AnimationManager
      40              :   gizmoManager: GizmoManager
      41              :   adapterRef: AdapterRef
      42              :   resolveImageURL: Load3dHost['resolveImageURL']
      43              : }
      44              : 
      45            0 : function positionThumbnailCamera(
      46              :   camera: THREE.PerspectiveCamera,
      47              :   box: THREE.Box3
      48              : ) {
      49            0 :   const size = box.getSize(new THREE.Vector3())
      50            0 :   const center = box.getCenter(new THREE.Vector3())
      51              :   const maxDim = Math.max(size.x, size.y, size.z)
      52            0 :   const distance = maxDim * 1.5
      53              : 
      54            0 :   camera.position.set(
      55              :     center.x + distance * 0.7,
      56              :     center.y + distance * 0.5,
      57              :     center.z + distance * 0.7
      58              :   )
      59            0 :   camera.lookAt(center)
      60            0 :   camera.updateProjectionMatrix()
      61              : }
      62              : 
      63           24 : class Load3d extends Viewport3d {
      64              :   hdriManager: HDRIManager
      65              :   loaderManager: LoaderManager
      66              :   modelManager: SceneModelManager
      67              :   recordingManager: RecordingManager
      68              :   animationManager: AnimationManager
      69              :   gizmoManager: GizmoManager
      70              :   adapterRef: AdapterRef
      71              :   private readonly resolveImageURL: Load3dHost['resolveImageURL']
      72              :   private configurationCleanup?: () => void
      73              : 
      74              :   private loadingPromise: Promise<LoadModelOutcome> | null = null
      75              :   private _loadGeneration: number = 0
      76              :   private hasLoadedModel: boolean = false
      77              :   private thumbnailCaptureQueue: Promise<unknown> = Promise.resolve()
      78              : 
      79              :   constructor(
      80              :     container: HTMLElement,
      81              :     deps: Load3dDeps,
      82              :     options: Load3DOptions = {}
      83              :   ) {
      84           27 :     super(container, deps, options)
      85              : 
      86           27 :     this.hdriManager = deps.hdriManager
      87           27 :     this.loaderManager = deps.loaderManager
      88           27 :     this.modelManager = deps.modelManager
      89           27 :     this.recordingManager = deps.recordingManager
      90           27 :     this.animationManager = deps.animationManager
      91           27 :     this.gizmoManager = deps.gizmoManager
      92           27 :     this.adapterRef = deps.adapterRef
      93           27 :     this.resolveImageURL = deps.resolveImageURL
      94              : 
      95           27 :     this.loaderManager.init()
      96           27 :     this.animationManager.init()
      97           27 :     this.gizmoManager.setPointerNdcSource((clientX, clientY) =>
      98              :       this.clientPointToNdc(clientX, clientY)
      99              :     )
     100           27 :     this.gizmoManager.init()
     101              : 
     102           27 :     this.eventManager.addEventListener('modelReady', () => {
     103           10 :       if (this.adapterRef.current?.kind !== 'splat') return
     104            0 :       void this.repaintWhenSparkPaintable()
     105              :     })
     106              : 
     107           27 :     this.start()
     108              :   }
     109              : 
     110              :   private async repaintWhenSparkPaintable(): Promise<void> {
     111            0 :     const sortComplete = this.sceneManager.awaitNextSparkDirty()
     112            0 :     this.forceRender()
     113            0 :     await sortComplete
     114            0 :     this.forceRender()
     115              :   }
     116              : 
     117              :   getLoaderManager(): LoaderManager {
     118            0 :     return this.loaderManager
     119              :   }
     120              : 
     121              :   getModelManager(): SceneModelManager {
     122           24 :     return this.modelManager
     123              :   }
     124              : 
     125              :   getRecordingManager(): RecordingManager {
     126            0 :     return this.recordingManager
     127              :   }
     128              : 
     129              :   getGizmoManager(): GizmoManager {
     130           10 :     return this.gizmoManager
     131              :   }
     132              : 
     133              :   protected override tickPerFrame(delta: number): void {
     134          999 :     this.animationManager.update(delta)
     135          999 :     super.tickPerFrame(delta)
     136              :   }
     137              : 
     138              :   override isActive(): boolean {
     139          456 :     return isLoad3dActive({
     140              :       mouseOnNode: this.STATUS_MOUSE_ON_NODE,
     141              :       mouseOnScene: this.STATUS_MOUSE_ON_SCENE,
     142              :       mouseOnViewer: this.STATUS_MOUSE_ON_VIEWER,
     143              :       recording: this.isRecording(),
     144              :       initialRenderDone: this.INITIAL_RENDER_DONE,
     145              :       animationPlaying: this.animationManager.isAnimationPlaying
     146              :     })
     147              :   }
     148              : 
     149              :   async exportModel(format: string): Promise<void> {
     150            0 :     if (!this.modelManager.currentModel) {
     151            0 :       throw new Error('No model to export')
     152              :     }
     153              : 
     154            0 :     const exportMessage = `Exporting as ${format.toUpperCase()}...`
     155            0 :     this.eventManager.emitEvent('exportLoadingStart', exportMessage)
     156              : 
     157              :     const originalFileName = this.modelManager.originalFileName || 'model'
     158            0 :     const filename = `${originalFileName}.${format}`
     159            0 :     const originalURL = this.modelManager.originalURL
     160              : 
     161            0 :     if (DIRECT_EXPORT_FORMATS.has(format)) {
     162            0 :       try {
     163            0 :         if (this.getSourceFormat() !== format) {
     164            0 :           throw new Error(
     165              :             `Cannot export ${format} without converting from the loaded ${this.getSourceFormat() ?? 'unknown'} source`
     166              :           )
     167              :         }
     168            0 :         await ModelExporter.exportDirect(originalURL, filename, format)
     169              :       } catch (error) {
     170            0 :         console.error(`Error exporting model as ${format}:`, error)
     171            0 :         throw error
     172              :       } finally {
     173            0 :         this.eventManager.emitEvent('exportLoadingEnd', null)
     174              :       }
     175            0 :       return
     176              :     }
     177              : 
     178            0 :     const source = this.modelManager.currentModel
     179            0 :     const savedPos = source.position.clone()
     180            0 :     const savedRot = source.rotation.clone()
     181            0 :     const savedScale = source.scale.clone()
     182            0 :     source.position.set(0, 0, 0)
     183            0 :     source.rotation.set(0, 0, 0)
     184            0 :     source.scale.set(1, 1, 1)
     185            0 :     source.updateMatrixWorld(true)
     186              : 
     187            0 :     try {
     188            0 :       const original = this.modelManager.originalModel
     189            0 :       const clipsFromOriginal =
     190              :         original &&
     191              :         'animations' in original &&
     192              :         Array.isArray(original.animations)
     193              :           ? original.animations
     194              :           : []
     195            0 :       const clips = source.animations.length
     196              :         ? source.animations
     197              :         : clipsFromOriginal
     198            0 :       const model =
     199              :         format === 'fbx'
     200              :           ? Object.assign(cloneSkinned(source), { animations: clips })
     201              :           : source.clone()
     202            0 :       adoptClonedModel(model, source, this.modelManager.originalMaterials)
     203              : 
     204            0 :       await new Promise((resolve) => setTimeout(resolve, 10))
     205              : 
     206            0 :       switch (format) {
     207              :         case 'glb':
     208            0 :           await ModelExporter.exportGLB(model, filename, originalURL)
     209            0 :           break
     210              :         case 'obj':
     211            0 :           await ModelExporter.exportOBJ(model, filename, originalURL)
     212            0 :           break
     213              :         case 'stl':
     214            0 :           await ModelExporter.exportSTL(model, filename, originalURL)
     215            0 :           break
     216              :         case 'fbx':
     217            0 :           await ModelExporter.exportFBX(model, filename, originalURL)
     218            0 :           break
     219              :         default:
     220            0 :           throw new Error(`Unsupported export format: ${format}`)
     221              :       }
     222              : 
     223            0 :       await new Promise((resolve) => setTimeout(resolve, 10))
     224              :     } catch (error) {
     225            0 :       console.error(`Error exporting model as ${format}:`, error)
     226            0 :       throw error
     227              :     } finally {
     228            0 :       source.position.copy(savedPos)
     229            0 :       source.rotation.copy(savedRot)
     230            0 :       source.scale.copy(savedScale)
     231            0 :       source.updateMatrixWorld(true)
     232            0 :       this.eventManager.emitEvent('exportLoadingEnd', null)
     233              :     }
     234              :   }
     235              : 
     236              :   getSourceFormat(): string | null {
     237           21 :     const url = this.modelManager.originalURL
     238           21 :     if (!url) return null
     239           21 :     return ModelExporter.detectFormatFromURL(url)
     240              :   }
     241              : 
     242              :   protected override onActiveCameraChanged(): void {
     243           52 :     this.gizmoManager.updateCamera(this.cameraManager.activeCamera)
     244              :   }
     245              : 
     246              :   setFOV(fov: number): void {
     247           54 :     this.cameraManager.setFOV(fov)
     248           54 :     this.forceRender()
     249              :   }
     250              : 
     251              :   setBackgroundColor(color: string): void {
     252           55 :     this.sceneManager.setBackgroundColor(color)
     253           55 :     this.forceRender()
     254              :   }
     255              : 
     256              :   toggleGrid(showGrid: boolean): void {
     257           54 :     this.sceneManager.toggleGrid(showGrid)
     258           54 :     this.forceRender()
     259              :   }
     260              : 
     261              :   setLightIntensity(intensity: number): void {
     262           79 :     this.lightingManager.setLightIntensity(intensity)
     263           79 :     this.forceRender()
     264              :   }
     265              : 
     266              :   async setBackgroundImage(uploadPath: string): Promise<void> {
     267            6 :     await this.sceneManager.setBackgroundImage(
     268              :       uploadPath,
     269              :       uploadPath && this.resolveImageURL(uploadPath)
     270              :     )
     271              : 
     272            6 :     if (
     273              :       this.sceneManager.backgroundTexture &&
     274              :       this.sceneManager.backgroundMesh
     275              :     ) {
     276            2 :       const containerWidth = this.domElement.clientWidth
     277            2 :       const containerHeight = this.domElement.clientHeight
     278              : 
     279            2 :       if (this.shouldMaintainAspectRatio()) {
     280            2 :         const { width, height } = computeLetterboxedViewport(
     281              :           { width: containerWidth, height: containerHeight },
     282              :           this.targetAspectRatio
     283              :         )
     284              : 
     285            2 :         this.sceneManager.updateBackgroundSize(
     286              :           this.sceneManager.backgroundTexture,
     287              :           this.sceneManager.backgroundMesh,
     288              :           width,
     289              :           height
     290              :         )
     291              :       } else {
     292            0 :         this.sceneManager.updateBackgroundSize(
     293              :           this.sceneManager.backgroundTexture,
     294              :           this.sceneManager.backgroundMesh,
     295              :           containerWidth,
     296              :           containerHeight
     297              :         )
     298              :       }
     299              :     }
     300              : 
     301            6 :     this.forceRender()
     302              :   }
     303              : 
     304              :   removeBackgroundImage(): void {
     305            0 :     this.sceneManager.removeBackgroundImage()
     306            0 :     this.forceRender()
     307              :   }
     308              : 
     309              :   setBackgroundRenderMode(mode: 'tiled' | 'panorama'): void {
     310           27 :     this.sceneManager.setBackgroundRenderMode(mode)
     311           27 :     this.forceRender()
     312              :   }
     313              : 
     314              :   setCameraFromMatrices(
     315              :     extrinsics: readonly (readonly number[])[],
     316              :     intrinsics: readonly (readonly number[])[]
     317              :   ): void {
     318            0 :     const { position, target, fovYDegrees } = computeCameraFromMatrices(
     319              :       extrinsics,
     320              :       intrinsics
     321              :     )
     322            0 :     const current = this.cameraManager.getCameraState()
     323            0 :     this.setCameraState({
     324              :       position: new THREE.Vector3(position[0], position[1], position[2]),
     325              :       target: new THREE.Vector3(target[0], target[1], target[2]),
     326              :       zoom: current.zoom,
     327              :       cameraType: current.cameraType
     328              :     })
     329            0 :     this.setFOV(fovYDegrees)
     330              :   }
     331              : 
     332              :   getCurrentModel(): THREE.Object3D | null {
     333            0 :     return this.modelManager.currentModel
     334              :   }
     335              : 
     336              :   setMaterialMode(mode: MaterialMode): void {
     337           12 :     this.modelManager.setMaterialMode(mode)
     338           12 :     this.forceRender()
     339              :   }
     340              : 
     341              :   get currentLoadGeneration(): number {
     342            0 :     return this._loadGeneration
     343              :   }
     344              : 
     345              :   async loadModel(
     346              :     url: string,
     347              :     originalFileName?: string,
     348              :     options?: LoadModelOptions
     349              :   ): Promise<LoadModelOutcome> {
     350           19 :     this._loadGeneration += 1
     351           19 :     const loadGeneration = this._loadGeneration
     352              : 
     353           19 :     const previousLoad = this.loadingPromise
     354           19 :     const acceptedLoad = (async () => {
     355           19 :       try {
     356           19 :         await previousLoad
     357              :       } catch {
     358              :         // Serialization only: the rejection already reached the loadModel caller.
     359              :       }
     360              : 
     361           19 :       let outcome: LoadModelOutcome
     362           19 :       try {
     363           19 :         outcome = await this._loadModelInternal(url, originalFileName, options)
     364              :       } finally {
     365           19 :         if (loadGeneration !== this._loadGeneration) this.clearModelState()
     366              :       }
     367              : 
     368           19 :       return loadGeneration === this._loadGeneration ? outcome : 'cancelled'
     369              :     })()
     370              : 
     371              :     // Publish the tail before waiting so every accepted load is visible to
     372              :     // whenLoadIdle(), including loads queued behind the current one.
     373           19 :     this.loadingPromise = acceptedLoad
     374           19 :     try {
     375           19 :       return await acceptedLoad
     376              :     } finally {
     377           19 :       if (this.loadingPromise === acceptedLoad) this.loadingPromise = null
     378              :     }
     379              :   }
     380              : 
     381              :   async whenLoadIdle(): Promise<void> {
     382            3 :     let last: Promise<LoadModelOutcome> | null = null
     383            3 :     while (this.loadingPromise && this.loadingPromise !== last) {
     384            0 :       last = this.loadingPromise
     385            0 :       try {
     386            0 :         await last
     387              :       } catch {
     388              :         // Serialization only: the rejection already reached the loadModel caller.
     389              :       }
     390              :     }
     391              :   }
     392              : 
     393              :   private async _loadModelInternal(
     394              :     url: string,
     395              :     originalFileName?: string,
     396              :     options?: LoadModelOptions
     397              :   ): Promise<LoadModelOutcome> {
     398           19 :     const shouldRetainView = this.hasLoadedModel
     399           19 :     const savedCameraState = shouldRetainView
     400              :       ? this.cameraManager.getCameraState()
     401              :       : null
     402              : 
     403           19 :     if (!shouldRetainView) {
     404           18 :       this.cameraManager.reset()
     405           18 :       this.controlsManager.reset()
     406              :     }
     407           19 :     this.gizmoManager.detach()
     408           19 :     this.modelManager.clearModel()
     409           19 :     this.animationManager.dispose()
     410           19 :     this.hasLoadedModel = false
     411              : 
     412           19 :     const outcome = await this.loaderManager.loadModel(
     413              :       url,
     414              :       originalFileName,
     415              :       options
     416              :     )
     417              : 
     418           19 :     if (outcome !== 'loaded') {
     419            2 :       return outcome
     420              :     }
     421              : 
     422           17 :     if (this.modelManager.currentModel) {
     423           17 :       this.animationManager.setupModelAnimations(
     424              :         this.modelManager.currentModel,
     425              :         this.modelManager.originalModel
     426              :       )
     427           17 :       this.hasLoadedModel = true
     428              :     }
     429              : 
     430           17 :     if (savedCameraState) {
     431            1 :       if (
     432              :         savedCameraState.cameraType !==
     433              :         this.cameraManager.getCurrentCameraType()
     434              :       ) {
     435            0 :         this.toggleCamera(savedCameraState.cameraType)
     436              :       }
     437            1 :       this.cameraManager.setCameraState(savedCameraState)
     438              :     }
     439              : 
     440           17 :     this.handleResize()
     441              : 
     442           17 :     return outcome
     443              :   }
     444              : 
     445              :   isSplatModel(): boolean {
     446           40 :     return this.adapterRef.current?.kind === 'splat'
     447              :   }
     448              : 
     449              :   isPlyModel(): boolean {
     450           21 :     return this.adapterRef.current?.kind === 'pointCloud'
     451              :   }
     452              : 
     453              :   getCurrentModelCapabilities(): ModelAdapterCapabilities {
     454           28 :     return this.adapterRef.capabilities ?? DEFAULT_MODEL_CAPABILITIES
     455              :   }
     456              : 
     457              :   clearModel(): void {
     458            1 :     this._loadGeneration += 1
     459            1 :     this.clearModelState()
     460              :   }
     461              : 
     462              :   private clearModelState(): void {
     463            7 :     this.animationManager.dispose()
     464            7 :     this.gizmoManager.detach()
     465            7 :     this.modelManager.clearModel()
     466            7 :     this.adapterRef.current = null
     467            7 :     this.hasLoadedModel = false
     468            7 :     this.eventManager.emitEvent('modelCleared', null)
     469            7 :     this.forceRender()
     470              :   }
     471              : 
     472              :   setUpDirection(direction: UpDirection): void {
     473           12 :     this.modelManager.setUpDirection(direction)
     474           12 :     this.forceRender()
     475              :   }
     476              : 
     477              :   async loadHDRI(url: string): Promise<void> {
     478            0 :     await this.hdriManager.loadHDRI(url)
     479            0 :     this.forceRender()
     480              :   }
     481              : 
     482              :   setHDRIEnabled(enabled: boolean): void {
     483           25 :     this.hdriManager.setEnabled(enabled)
     484           25 :     this.lightingManager.setHDRIMode(enabled)
     485           25 :     this.forceRender()
     486              :   }
     487              : 
     488              :   setHDRIAsBackground(show: boolean): void {
     489           25 :     this.hdriManager.setShowAsBackground(show)
     490           25 :     this.forceRender()
     491              :   }
     492              : 
     493              :   setHDRIIntensity(intensity: number): void {
     494           25 :     this.hdriManager.setIntensity(intensity)
     495           25 :     this.forceRender()
     496              :   }
     497              : 
     498              :   clearHDRI(): void {
     499            0 :     this.hdriManager.clear()
     500            0 :     this.lightingManager.setHDRIMode(false)
     501            0 :     this.forceRender()
     502              :   }
     503              : 
     504              :   emitModelReady(): void {
     505           10 :     this.eventManager.emitEvent('modelReady', null)
     506              :   }
     507              : 
     508              :   captureScene(width: number, height: number): Promise<CaptureResult> {
     509            3 :     this.gizmoManager.removeFromScene()
     510              : 
     511            3 :     return this.sceneManager.captureScene(width, height).finally(() => {
     512            3 :       this.gizmoManager.ensureHelperInScene()
     513              :     })
     514              :   }
     515              : 
     516              :   public async startRecording(): Promise<void> {
     517            2 :     this.viewHelperManager.visibleViewHelper(false)
     518              : 
     519            2 :     await this.recordingManager.startRecording(
     520              :       this.targetWidth,
     521              :       this.targetHeight
     522              :     )
     523            2 :     this.renderLoop?.wake()
     524              :   }
     525              : 
     526              :   public stopRecording(): void {
     527            4 :     this.viewHelperManager.visibleViewHelper(true)
     528              : 
     529            4 :     this.recordingManager.stopRecording()
     530              : 
     531            4 :     this.eventManager.emitEvent('recordingStatusChange', false)
     532              :   }
     533              : 
     534              :   public isRecording(): boolean {
     535          456 :     return this.recordingManager.getIsRecording()
     536              :   }
     537              : 
     538              :   public getRecordingDuration(): number {
     539            5 :     return this.recordingManager.getRecordingDuration()
     540              :   }
     541              : 
     542              :   public getRecordingData(): string | null {
     543            3 :     return this.recordingManager.getRecordingData()
     544              :   }
     545              : 
     546              :   public exportRecording(filename?: string): void {
     547            0 :     this.recordingManager.exportRecording(filename)
     548              :   }
     549              : 
     550              :   public clearRecording(): void {
     551            1 :     this.recordingManager.clearRecording()
     552              :   }
     553              : 
     554              :   public setAnimationSpeed(speed: number): void {
     555            0 :     this.animationManager.setAnimationSpeed(speed)
     556              :   }
     557              : 
     558              :   public updateSelectedAnimation(index: number): void {
     559            0 :     this.animationManager.updateSelectedAnimation(index)
     560              :   }
     561              : 
     562              :   public toggleAnimation(play?: boolean): void {
     563            1 :     this.animationManager.toggleAnimation(play)
     564            1 :     this.renderLoop?.wake()
     565              :   }
     566              : 
     567              :   public hasAnimations(): boolean {
     568            4 :     return this.animationManager.animationClips.length > 0
     569              :   }
     570              : 
     571              :   public hasSkeleton(): boolean {
     572           19 :     return this.modelManager.hasSkeleton()
     573              :   }
     574              : 
     575              :   public async getModelStats(signal?: AbortSignal): Promise<ModelStats | null> {
     576           19 :     const model = this.modelManager.currentModel
     577           19 :     if (!model || this.isSplatModel()) return null
     578           17 :     return computeModelStats(model, signal)
     579              :   }
     580              : 
     581              :   public setShowSkeleton(show: boolean): void {
     582            2 :     this.modelManager.setShowSkeleton(show)
     583            2 :     this.forceRender()
     584              :   }
     585              : 
     586              :   public getShowSkeleton(): boolean {
     587            0 :     return this.modelManager.showSkeleton
     588              :   }
     589              : 
     590              :   public getAnimationTime(): number {
     591            0 :     return this.animationManager.getAnimationTime()
     592              :   }
     593              : 
     594              :   public getAnimationDuration(): number {
     595            0 :     return this.animationManager.getAnimationDuration()
     596              :   }
     597              : 
     598              :   public setAnimationTime(time: number): void {
     599            0 :     this.animationManager.setAnimationTime(time)
     600            0 :     this.forceRender()
     601              :   }
     602              : 
     603              :   public captureThumbnail(
     604              :     width: number = 256,
     605              :     height: number = 256
     606              :   ): Promise<string> {
     607            0 :     const capture = this.thumbnailCaptureQueue.then(() =>
     608              :       this.captureThumbnailNow(width, height)
     609              :     )
     610            0 :     this.thumbnailCaptureQueue = capture.catch(() => {})
     611            0 :     return capture
     612              :   }
     613              : 
     614              :   private async captureThumbnailNow(
     615              :     width: number,
     616              :     height: number
     617              :   ): Promise<string> {
     618            0 :     if (!this.modelManager.currentModel) {
     619            0 :       throw new Error('No model loaded for thumbnail capture')
     620              :     }
     621              : 
     622            0 :     const savedState = this.cameraManager.getCameraState()
     623            0 :     const savedCameraType = this.cameraManager.getCurrentCameraType()
     624            0 :     const savedGridVisible = this.sceneManager.gridHelper.visible
     625              : 
     626            0 :     try {
     627            0 :       this.sceneManager.gridHelper.visible = false
     628              : 
     629            0 :       if (savedCameraType !== 'perspective') {
     630            0 :         this.cameraManager.toggleCamera('perspective')
     631              :       }
     632              : 
     633            0 :       const box =
     634              :         this.modelManager.getCurrentBounds() ??
     635              :         new THREE.Box3().setFromObject(this.modelManager.currentModel)
     636              : 
     637            0 :       positionThumbnailCamera(this.cameraManager.perspectiveCamera, box)
     638              : 
     639            0 :       this.controlsManager.controls.target.copy(
     640              :         box.getCenter(new THREE.Vector3())
     641              :       )
     642            0 :       this.controlsManager.controls.update()
     643              : 
     644            0 :       if (this.isSplatModel()) {
     645            0 :         await this.sceneManager.whenSplatsSorted(
     646              :           this.cameraManager.perspectiveCamera
     647              :         )
     648              :       }
     649              : 
     650              :       const result = await this.captureScene(width, height)
     651            0 :       return result.scene
     652              :     } finally {
     653            0 :       this.sceneManager.gridHelper.visible = savedGridVisible
     654              : 
     655            0 :       if (savedCameraType !== 'perspective') {
     656            0 :         this.cameraManager.toggleCamera(savedCameraType)
     657              :       }
     658            0 :       this.cameraManager.setCameraState(savedState)
     659            0 :       this.controlsManager.controls.update()
     660              : 
     661            0 :       this.forceRender()
     662              :     }
     663              :   }
     664              : 
     665              :   public setGizmoEnabled(enabled: boolean): void {
     666            3 :     if (enabled && !this.getCurrentModelCapabilities().gizmoTransform) return
     667            3 :     this.gizmoManager.setEnabled(enabled)
     668            3 :     this.forceRender()
     669              :   }
     670              : 
     671              :   public setGizmoMode(mode: GizmoMode): void {
     672            3 :     if (!this.getCurrentModelCapabilities().gizmoTransform) return
     673            3 :     this.gizmoManager.setMode(mode)
     674            3 :     this.forceRender()
     675              :   }
     676              : 
     677              :   public resetGizmoTransform(): void {
     678            0 :     if (!this.getCurrentModelCapabilities().gizmoTransform) return
     679            0 :     this.gizmoManager.reset()
     680            0 :     this.forceRender()
     681              :   }
     682              : 
     683              :   public applyGizmoTransform(
     684              :     position: Vector3,
     685              :     rotation: Vector3,
     686              :     scale?: Vector3
     687              :   ): void {
     688            2 :     if (!this.getCurrentModelCapabilities().gizmoTransform) return
     689            2 :     this.gizmoManager.applyTransform(position, rotation, scale)
     690            2 :     this.forceRender()
     691              :   }
     692              : 
     693              :   public applyModelTransform(transform: Model3DTransform): void {
     694            0 :     if (!this.getCurrentModelCapabilities().gizmoTransform) return
     695            0 :     this.gizmoManager.applyModelTransform(transform)
     696            0 :     this.forceRender()
     697              :   }
     698              : 
     699              :   public getGizmoTransform(): {
     700              :     position: Vector3
     701              :     rotation: Vector3
     702              :     scale: Vector3
     703              :   } {
     704            2 :     return this.gizmoManager.getTransform()
     705              :   }
     706              : 
     707              :   public getModelInfo(): Model3DTransform | null {
     708           22 :     return this.gizmoManager.getModelInfo()
     709              :   }
     710              : 
     711              :   public fitToViewer(): void {
     712            0 :     this.modelManager.fitToViewer()
     713            0 :     this.forceRender()
     714              :   }
     715              : 
     716              :   public centerCameraOnModel(): void {
     717            0 :     const bounds = this.modelManager.getCurrentBounds()
     718            0 :     if (!bounds || bounds.isEmpty()) return
     719              : 
     720            0 :     const center = bounds.getCenter(new THREE.Vector3())
     721            0 :     const camera = this.cameraManager.activeCamera
     722            0 :     const controls = this.controlsManager.controls
     723            0 :     const offset = center.clone().sub(camera.position)
     724              : 
     725            0 :     camera.position.add(offset)
     726            0 :     controls.target.add(offset)
     727            0 :     camera.updateMatrixWorld(true)
     728            0 :     controls.update()
     729            0 :     this.forceRender()
     730              :   }
     731              : 
     732              :   setConfigurationCleanup(cleanup: () => void): void {
     733           25 :     this.clearConfigurationCleanup()
     734           25 :     this.configurationCleanup = cleanup
     735              :   }
     736              : 
     737              :   private clearConfigurationCleanup(): void {
     738           26 :     this.configurationCleanup?.()
     739           26 :     this.configurationCleanup = undefined
     740              :   }
     741              : 
     742              :   protected override disposeManagers(): void {
     743            1 :     this.clearConfigurationCleanup()
     744            1 :     super.disposeManagers()
     745            1 :     this.hdriManager.dispose()
     746            1 :     this.loaderManager.dispose()
     747            1 :     this.modelManager.dispose()
     748            1 :     this.adapterRef.current = null
     749            1 :     this.recordingManager.dispose()
     750            1 :     this.animationManager.dispose()
     751            1 :     this.gizmoManager.dispose()
     752              :   }
     753              : }
     754              : 
     755              : export default Load3d
        

Generated by: LCOV version 2.0-1