LCOV - code coverage report
Current view: top level - packages/viewport3d/src/load3d - HDRIManager.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 40.3 % 67 27
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import * as THREE from 'three'
       2              : import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader'
       3              : import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader'
       4              : 
       5              : import type { RendererViewState } from '#/three/sharedWebGLRenderer'
       6              : 
       7              : import type { EventManagerInterface } from './interfaces'
       8              : 
       9            0 : function getFilenameExtension(url: string): string | undefined {
      10            0 :   const queryString = url.split('?')[1]
      11            0 :   if (queryString) {
      12            0 :     const filename = new URLSearchParams(queryString).get('filename')
      13            0 :     if (filename) return filename.split('.').pop()?.toLowerCase()
      14              :   }
      15            0 :   return url.split('?')[0].split('.').pop()?.toLowerCase()
      16              : }
      17              : 
      18           24 : export class HDRIManager {
      19              :   private scene: THREE.Scene
      20              :   private viewState: RendererViewState
      21              :   private pmremGenerator: THREE.PMREMGenerator
      22              :   private eventManager: EventManagerInterface
      23              : 
      24              :   private hdriTexture: THREE.Texture | null = null
      25              :   private _loadedUrl: string | null = null
      26              :   private envMapTarget: THREE.WebGLRenderTarget | null = null
      27              : 
      28              :   private _isEnabled: boolean = false
      29              :   private _showAsBackground: boolean = false
      30              :   private _intensity: number = 1
      31              : 
      32              :   get isEnabled() {
      33            0 :     return this._isEnabled
      34              :   }
      35              : 
      36              :   get showAsBackground() {
      37            0 :     return this._showAsBackground
      38              :   }
      39              : 
      40              :   get intensity() {
      41            0 :     return this._intensity
      42              :   }
      43              : 
      44              :   get loadedUrl() {
      45            0 :     return this._loadedUrl
      46              :   }
      47              : 
      48              :   constructor(
      49              :     scene: THREE.Scene,
      50              :     renderer: THREE.WebGLRenderer,
      51              :     viewState: RendererViewState,
      52              :     eventManager: EventManagerInterface
      53              :   ) {
      54           27 :     this.scene = scene
      55           27 :     this.viewState = viewState
      56           27 :     this.pmremGenerator = new THREE.PMREMGenerator(renderer)
      57           27 :     this.pmremGenerator.compileEquirectangularShader()
      58           27 :     this.eventManager = eventManager
      59              :   }
      60              : 
      61              :   async loadHDRI(url: string): Promise<void> {
      62            0 :     const ext = getFilenameExtension(url)
      63              : 
      64            0 :     let newTexture: THREE.Texture
      65            0 :     if (ext === 'exr') {
      66            0 :       newTexture = await new Promise<THREE.Texture>((resolve, reject) => {
      67            0 :         new EXRLoader().load(url, resolve, undefined, reject)
      68              :       })
      69              :     } else {
      70            0 :       newTexture = await new Promise<THREE.Texture>((resolve, reject) => {
      71            0 :         new RGBELoader().load(url, resolve, undefined, reject)
      72              :       })
      73              :     }
      74              : 
      75            0 :     newTexture.mapping = THREE.EquirectangularReflectionMapping
      76            0 :     const newEnvMapTarget = this.pmremGenerator.fromEquirectangular(newTexture)
      77              : 
      78              :     // Dispose old resources only after the new one is ready
      79            0 :     this.hdriTexture?.dispose()
      80            0 :     this.envMapTarget?.dispose()
      81            0 :     this.hdriTexture = newTexture
      82            0 :     this.envMapTarget = newEnvMapTarget
      83            0 :     this._loadedUrl = url
      84              : 
      85            0 :     if (this._isEnabled) {
      86            0 :       this.applyToScene()
      87              :     }
      88              :   }
      89              : 
      90              :   setEnabled(enabled: boolean): void {
      91           25 :     this._isEnabled = enabled
      92           25 :     if (enabled) {
      93            0 :       if (this.envMapTarget) {
      94            0 :         this.applyToScene()
      95              :       }
      96              :     } else {
      97           25 :       this.removeFromScene()
      98              :     }
      99              :   }
     100              : 
     101              :   setShowAsBackground(show: boolean): void {
     102           25 :     this._showAsBackground = show
     103           25 :     if (this._isEnabled && this.envMapTarget) {
     104            0 :       this.applyToScene()
     105              :     }
     106              :   }
     107              : 
     108              :   setIntensity(intensity: number): void {
     109           25 :     this._intensity = intensity
     110           25 :     if (this._isEnabled) {
     111            0 :       this.scene.environmentIntensity = intensity
     112              :     }
     113              :   }
     114              : 
     115              :   private applyToScene(): void {
     116            0 :     const envMap = this.envMapTarget?.texture
     117            0 :     if (!envMap) return
     118            0 :     this.scene.environment = envMap
     119            0 :     this.scene.environmentIntensity = this._intensity
     120            0 :     this.scene.background = this._showAsBackground ? this.hdriTexture : null
     121            0 :     this.viewState.toneMapping = THREE.ACESFilmicToneMapping
     122            0 :     this.viewState.toneMappingExposure = 1.0
     123            0 :     this.eventManager.emitEvent('hdriChange', {
     124              :       enabled: this._isEnabled,
     125              :       showAsBackground: this._showAsBackground
     126              :     })
     127              :   }
     128              : 
     129              :   private removeFromScene(): void {
     130           26 :     this.scene.environment = null
     131           26 :     if (this.scene.background === this.hdriTexture) {
     132           26 :       this.scene.background = null
     133              :     }
     134           26 :     this.viewState.toneMapping = THREE.NoToneMapping
     135           26 :     this.viewState.toneMappingExposure = 1.0
     136           26 :     this.eventManager.emitEvent('hdriChange', {
     137              :       enabled: false,
     138              :       showAsBackground: this._showAsBackground
     139              :     })
     140              :   }
     141              : 
     142              :   private clearResources(): void {
     143            1 :     this.removeFromScene()
     144            1 :     this.hdriTexture?.dispose()
     145            1 :     this.envMapTarget?.dispose()
     146            1 :     this.hdriTexture = null
     147            1 :     this.envMapTarget = null
     148            1 :     this._loadedUrl = null
     149              :   }
     150              : 
     151              :   clear(): void {
     152            0 :     this.clearResources()
     153            0 :     this._isEnabled = false
     154              :   }
     155              : 
     156              :   dispose(): void {
     157            1 :     this.clearResources()
     158            1 :     this.pmremGenerator.dispose()
     159              :   }
     160              : }
        

Generated by: LCOV version 2.0-1