Skip to content

Commit

Permalink
feat: on-demand Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Jun 17, 2024
1 parent 74ee39f commit adc0ea8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/core/staging/useEnvironment/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ const fbo = ref(null as null | WebGLCubeRenderTarget)
let cubeCamera = null as null | CubeCamera
const envSence = ref<EnvSence | null>(null)
onUnmounted(() => {
envSence.value?.destructor()
fbo.value?.dispose()
})
const { onBeforeRender } = useLoop()
let count = 1
onBeforeRender(() => {
Expand Down Expand Up @@ -83,6 +80,11 @@ watch(useSlots().default, (value) => {
setTextureEnvAndBG()
}, { immediate: true, deep: true })
texture.value = useEnvironmentTexture
onUnmounted(() => {
envSence.value?.destructor()
fbo.value?.dispose()
})
</script>

<template>
Expand Down
4 changes: 4 additions & 0 deletions src/core/staging/useEnvironment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { RGBELoader } from 'three-stdlib'
import type { Ref } from 'vue'
import { computed, ref, toRefs, unref, watch } from 'vue'
import { useOnDemandInvalidation } from '../../../composables/useOnDemandInvalidation'
import type { EnvironmentOptions } from './const'
import { environmentPresets } from './const'

Expand Down Expand Up @@ -45,6 +46,8 @@ export async function useEnvironment(
background,
} = toRefs(options)

const { invalidateOnDemand } = useOnDemandInvalidation(options)

const texture: Ref<Texture | CubeTexture | undefined> = ref()
const isCubeMap = computed(() => Array.isArray((files as Ref<string[]>).value))
const loader = computed(() => isCubeMap.value ? CubeTextureLoader : RGBELoader)
Expand Down Expand Up @@ -122,6 +125,7 @@ export async function useEnvironment(
texture.value = result.value
texture.value.mapping = EquirectangularReflectionMapping
}
invalidateOnDemand()
}
else if (value && !(value in environmentPresets)) {
throw new Error(`Preset must be one of: ${Object.keys(environmentPresets).join(', ')}`)
Expand Down

0 comments on commit adc0ea8

Please sign in to comment.