Skip to content

Commit

Permalink
fix: avoid recursive updates when resizing the window (#81)
Browse files Browse the repository at this point in the history
* fix: avoid  recursive updates when resizing the window

* fix: added watchers back for scene and renderer
  • Loading branch information
alvarosabu authored Nov 14, 2023
1 parent 5652038 commit 83f88c1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ dist-ssr
*.sln
*.sw?
docs/.vitepress/dist/
docs/.vitepress/cache/
docs/.vitepress/cache/
stats.html
2 changes: 1 addition & 1 deletion playground-nuxt/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineNuxtConfig({
alias: {
'@tresjs/post-processing': resolve(__dirname, '../src/'),
},
dedupe: ['three', '@tresjs/core'],
dedupe: ['three', '@tresjs/core', '@vueuse/core'],
},
},
})
2 changes: 1 addition & 1 deletion playground-nuxt/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const bloomParams = reactive({
<TresGridHelper />
<TresAmbientLight :intensity="1" />
<Suspense>
<EffectComposer :depth-buffer="true">
<EffectComposer>
<Bloom />
</EffectComposer>
</Suspense>
Expand Down
20 changes: 8 additions & 12 deletions src/core/EffectComposer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DepthDownsamplingPass, EffectComposer as EffectComposerImpl, NormalPass
import { isWebGL2Available } from 'three-stdlib'
import type { ShallowRef } from 'vue'
import { computed, provide, shallowRef, watch, onUnmounted, watchEffect, onMounted } from 'vue'
import { computed, provide, shallowRef, watch, onUnmounted } from 'vue'
import { effectComposerInjectionKey } from './injectionKeys'
export interface EffectComposerProps {
Expand Down Expand Up @@ -56,11 +56,6 @@ const setNormalPass = () => {
}
}
watchEffect(() => {
if (effectComposer.value && sizes.width.value && sizes.height.value)
effectComposer.value.setSize(sizes.width.value, sizes.height.value)
})
const effectComposerParams = computed(() => {
const plainEffectComposer = new EffectComposerImpl()
const params = {
Expand Down Expand Up @@ -88,14 +83,15 @@ const initEffectComposer = () => {
if (!props.disableNormalPass) setNormalPass()
}
let stop = () => { } // defining this prevents error in watcher
watch([renderer, scene, camera, () => props.disableNormalPass], () => {
if (!sizes.width.value || !sizes.height.value ) return
initEffectComposer()
})
stop = watch([sizes.height, sizes.width], () => {
watch(() => [sizes.width.value, sizes.height.value], ([width, height]) => {
// effect composer should only live once the canvas has a size > 0
if (!sizes.height.value && !sizes.width.value) return
watchEffect(initEffectComposer)
stop?.()
if (!width && !height) return
effectComposer.value ? effectComposer.value.setSize(width, height) : initEffectComposer()
}, {
immediate: true,
})
Expand Down
7 changes: 4 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
alias: {
'/@': resolve(__dirname, './src'),
},
dedupe: ['@tresjs/core'],
dedupe: ['@tresjs/core', '@vueuse/core'],
},
plugins: [
vue({
Expand Down Expand Up @@ -61,7 +61,7 @@ export default defineConfig({
open: false,
}),
],
external: ['three', 'vue', '@tresjs/core', 'postprocessing'],
external: ['three', 'vue', '@tresjs/core', 'postprocessing', '@vueuse/core'],
output: {
exports: 'named',
// Provide global variables to use in the UMD build
Expand All @@ -71,11 +71,12 @@ export default defineConfig({
three: 'Three',
vue: 'Vue',
postprocessing: 'Postprocessing',
'@vueuse/core': 'VueUseCore',
},
},
},
},
optimizeDeps: {
exclude: ['three', 'vue', '@tresjs/core', 'postprocessing'],
exclude: ['three', 'vue', '@tresjs/core', 'postprocessing', '@vueuse/core'],
},
})

0 comments on commit 83f88c1

Please sign in to comment.