-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(post-processing): refactor of existing effects (#64)
* chore: refactored depth of field * feat: made effects react to main camera changes * chore: refactored glitch effect * chore: added comments for util functions * chore: refactored bloom effect component * chore: removed prop that is not reactive from bloom demo * chore: refactored outline effect component * chore: removed obsolete todo comment --------- Co-authored-by: Tino Koch <tino.koch@xpoli.eu>
- Loading branch information
Showing
10 changed files
with
226 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Effect } from 'postprocessing' | ||
import { EffectPass } from 'postprocessing' | ||
import { inject, onUnmounted, shallowRef, watchEffect, defineExpose } from 'vue' | ||
import { useTresContext } from '@tresjs/core' | ||
import { effectComposerInjectionKey } from '../injectionKeys' | ||
|
||
export const useEffect = <T extends Effect>( newEffectFunction: () => T) => { | ||
const composer = inject(effectComposerInjectionKey) | ||
const pass = shallowRef<EffectPass | null>(null) | ||
const effect = shallowRef<T | null>(null) | ||
|
||
const { scene, camera } = useTresContext() | ||
|
||
watchEffect(() => { | ||
if (!camera.value || !effect?.value) return | ||
|
||
effect.value.mainCamera = camera.value | ||
}) | ||
|
||
let unwatch = () => {} // seperate declaration prevents error in HMR | ||
|
||
unwatch = watchEffect(() => { | ||
if (!camera.value || !composer?.value || !scene.value) return | ||
|
||
unwatch() | ||
if (effect.value) return | ||
|
||
effect.value = newEffectFunction() | ||
pass.value = new EffectPass(camera.value, effect.value) | ||
|
||
composer.value.addPass(pass.value) | ||
}) | ||
|
||
onUnmounted(() => { | ||
if (pass.value) composer?.value?.removePass(pass.value) | ||
effect.value?.dispose() | ||
pass.value?.dispose() | ||
}) | ||
|
||
return { | ||
pass, | ||
effect, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.