Skip to content

Commit 83f88c1

Browse files
authored
fix: avoid recursive updates when resizing the window (#81)
* fix: avoid recursive updates when resizing the window * fix: added watchers back for scene and renderer
1 parent 5652038 commit 83f88c1

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ dist-ssr
2323
*.sln
2424
*.sw?
2525
docs/.vitepress/dist/
26-
docs/.vitepress/cache/
26+
docs/.vitepress/cache/
27+
stats.html

Diff for: playground-nuxt/nuxt.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default defineNuxtConfig({
1111
alias: {
1212
'@tresjs/post-processing': resolve(__dirname, '../src/'),
1313
},
14-
dedupe: ['three', '@tresjs/core'],
14+
dedupe: ['three', '@tresjs/core', '@vueuse/core'],
1515
},
1616
},
1717
})

Diff for: playground-nuxt/pages/index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const bloomParams = reactive({
3939
<TresGridHelper />
4040
<TresAmbientLight :intensity="1" />
4141
<Suspense>
42-
<EffectComposer :depth-buffer="true">
42+
<EffectComposer>
4343
<Bloom />
4444
</EffectComposer>
4545
</Suspense>

Diff for: src/core/EffectComposer.vue

+8-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { DepthDownsamplingPass, EffectComposer as EffectComposerImpl, NormalPass
66
77
import { isWebGL2Available } from 'three-stdlib'
88
import type { ShallowRef } from 'vue'
9-
import { computed, provide, shallowRef, watch, onUnmounted, watchEffect, onMounted } from 'vue'
9+
import { computed, provide, shallowRef, watch, onUnmounted } from 'vue'
1010
import { effectComposerInjectionKey } from './injectionKeys'
1111
1212
export interface EffectComposerProps {
@@ -56,11 +56,6 @@ const setNormalPass = () => {
5656
}
5757
}
5858
59-
watchEffect(() => {
60-
if (effectComposer.value && sizes.width.value && sizes.height.value)
61-
effectComposer.value.setSize(sizes.width.value, sizes.height.value)
62-
})
63-
6459
const effectComposerParams = computed(() => {
6560
const plainEffectComposer = new EffectComposerImpl()
6661
const params = {
@@ -88,14 +83,15 @@ const initEffectComposer = () => {
8883
if (!props.disableNormalPass) setNormalPass()
8984
}
9085
91-
let stop = () => { } // defining this prevents error in watcher
86+
watch([renderer, scene, camera, () => props.disableNormalPass], () => {
87+
if (!sizes.width.value || !sizes.height.value ) return
88+
initEffectComposer()
89+
})
9290
93-
stop = watch([sizes.height, sizes.width], () => {
91+
watch(() => [sizes.width.value, sizes.height.value], ([width, height]) => {
9492
// effect composer should only live once the canvas has a size > 0
95-
if (!sizes.height.value && !sizes.width.value) return
96-
97-
watchEffect(initEffectComposer)
98-
stop?.()
93+
if (!width && !height) return
94+
effectComposer.value ? effectComposer.value.setSize(width, height) : initEffectComposer()
9995
}, {
10096
immediate: true,
10197
})

Diff for: vite.config.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default defineConfig({
2020
alias: {
2121
'/@': resolve(__dirname, './src'),
2222
},
23-
dedupe: ['@tresjs/core'],
23+
dedupe: ['@tresjs/core', '@vueuse/core'],
2424
},
2525
plugins: [
2626
vue({
@@ -61,7 +61,7 @@ export default defineConfig({
6161
open: false,
6262
}),
6363
],
64-
external: ['three', 'vue', '@tresjs/core', 'postprocessing'],
64+
external: ['three', 'vue', '@tresjs/core', 'postprocessing', '@vueuse/core'],
6565
output: {
6666
exports: 'named',
6767
// Provide global variables to use in the UMD build
@@ -71,11 +71,12 @@ export default defineConfig({
7171
three: 'Three',
7272
vue: 'Vue',
7373
postprocessing: 'Postprocessing',
74+
'@vueuse/core': 'VueUseCore',
7475
},
7576
},
7677
},
7778
},
7879
optimizeDeps: {
79-
exclude: ['three', 'vue', '@tresjs/core', 'postprocessing'],
80+
exclude: ['three', 'vue', '@tresjs/core', 'postprocessing', '@vueuse/core'],
8081
},
8182
})

0 commit comments

Comments
 (0)