Skip to content

Commit 68f47f6

Browse files
committed
fix: avoid recursive updates when resizing the window
1 parent 5652038 commit 68f47f6

File tree

5 files changed

+14
-21
lines changed

5 files changed

+14
-21
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
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

playground-nuxt/nuxt.config.ts

Lines changed: 1 addition & 1 deletion
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
})

playground-nuxt/pages/index.vue

Lines changed: 1 addition & 1 deletion
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>

src/core/EffectComposer.vue

Lines changed: 6 additions & 15 deletions
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, watchEffect, onMounted, unref } 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 = {
@@ -82,20 +77,16 @@ const effectComposerParams = computed(() => {
8277
const initEffectComposer = () => {
8378
if (!renderer.value && !scene.value && !camera.value) return
8479
85-
effectComposer.value = new EffectComposerImpl(renderer.value, effectComposerParams.value)
86-
effectComposer.value.addPass(new RenderPass(scene.value, camera.value))
80+
effectComposer.value = new EffectComposerImpl(unref(renderer), effectComposerParams.value)
81+
effectComposer.value.addPass(new RenderPass(unref(scene), unref(camera)))
8782
8883
if (!props.disableNormalPass) setNormalPass()
8984
}
9085
91-
let stop = () => { } // defining this prevents error in watcher
92-
93-
stop = watch([sizes.height, sizes.width], () => {
86+
watch(() => [sizes.height.value, sizes.width.value], ([width, height]) => {
9487
// 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?.()
88+
if (!width && !height) return
89+
effectComposer.value ? effectComposer.value.setSize(sizes.width.value, sizes.height.value) : initEffectComposer()
9990
}, {
10091
immediate: true,
10192
})

vite.config.ts

Lines changed: 4 additions & 3 deletions
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)