( imageFile: File | undefined, imageName: string, enabled: boolean )
| 264 | * @returns The loaded texture or null |
| 265 | */ |
| 266 | export function useFrustumTexture( |
| 267 | imageFile: File | undefined, |
| 268 | imageName: string, |
| 269 | enabled: boolean |
| 270 | ): THREE.Texture | null { |
| 271 | const cacheVersion = useSyncExternalStore( |
| 272 | subscribeFrustumTextureCacheChanges, |
| 273 | getFrustumTextureCacheVersion, |
| 274 | getFrustumTextureCacheVersion |
| 275 | ); |
| 276 | void cacheVersion; |
| 277 | |
| 278 | useEffect(() => { |
| 279 | if (!enabled || !imageFile) return; |
| 280 | if (getCachedFrustumBitmap(bitmapCache, imageName)) return; |
| 281 | void loadFrustumBitmapFromFile(imageFile, imageName); |
| 282 | }, [enabled, imageFile, imageName]); |
| 283 | |
| 284 | const cachedBitmap = enabled ? getCachedFrustumBitmap(bitmapCache, imageName) : null; |
| 285 | const cacheToken = cachedBitmap ? `bitmap:${imageName}` : null; |
| 286 | const resourceRef = useRef<FrustumTextureResource | null>(null); |
| 287 | resourceRef.current ??= createFrustumTextureResource({ |
| 288 | getCachedTexture: getCachedFrustumTexture, |
| 289 | getOrLoadBitmap, |
| 290 | createTextureFromBitmap, |
| 291 | }); |
| 292 | const resource = resourceRef.current; |
| 293 | const snapshot = useSyncExternalStore( |
| 294 | resource.subscribe, |
| 295 | resource.getSnapshot, |
| 296 | resource.getSnapshot |
| 297 | ); |
| 298 | const cacheKey = getFrustumTextureCacheKey(cacheToken, imageName, enabled); |
| 299 | |
| 300 | useEffect(() => { |
| 301 | resource.sync({ |
| 302 | cachedUrl: cacheToken, |
| 303 | enabled, |
| 304 | imageName, |
| 305 | }); |
| 306 | }, [cacheToken, enabled, imageName, resource]); |
| 307 | |
| 308 | const texture = snapshot.cacheKey === cacheKey ? snapshot.texture : null; |
| 309 | |
| 310 | // Update lastUsed time when texture is accessed |
| 311 | useEffect(() => { |
| 312 | if (texture && imageName) { |
| 313 | touchActiveFrustumTexture(activeTextures, imageName); |
| 314 | } |
| 315 | }, [texture, imageName]); |
| 316 | |
| 317 | return texture; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Hook to get a high-resolution texture for the selected image. |
no test coverage detected