1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 12:22:03 +08:00
trois/src/effects/EffectComposer.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-04-25 03:45:57 +08:00
import { defineComponent, inject, InjectionKey } from 'vue'
2021-04-20 18:41:04 +08:00
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js'
import { Pass } from 'three/examples/jsm/postprocessing/Pass'
2021-04-25 03:45:57 +08:00
import { RendererInjectionKey, RendererInterface } from '../core/Renderer'
2021-04-20 18:41:04 +08:00
interface EffectComposerSetupInterface {
2021-04-25 03:45:57 +08:00
renderer?: RendererInterface
2021-04-20 18:41:04 +08:00
composer?: EffectComposer
}
export interface EffectComposerInterface extends EffectComposerSetupInterface {
addPass(pass: Pass): void
removePass(pass: Pass): void
}
2021-04-25 03:45:57 +08:00
export const ComposerInjectionKey: InjectionKey<EffectComposerInterface> = Symbol('Composer')
2021-04-20 18:41:04 +08:00
export default defineComponent({
setup(): EffectComposerSetupInterface {
2021-04-25 03:45:57 +08:00
const renderer = inject(RendererInjectionKey)
return { renderer }
2021-04-20 18:41:04 +08:00
},
provide() {
return {
2021-04-25 03:45:57 +08:00
[ComposerInjectionKey as symbol]: this,
2021-04-20 18:41:04 +08:00
}
},
created() {
2021-04-25 03:45:57 +08:00
if (!this.renderer) {
console.error('Renderer not found')
return
}
const renderer = this.renderer
2021-04-21 08:17:31 +08:00
const composer = new EffectComposer(this.renderer.renderer)
2021-04-20 18:41:04 +08:00
this.composer = composer
2021-04-22 03:05:02 +08:00
this.renderer.composer = composer
2021-04-20 18:41:04 +08:00
2021-04-21 08:17:31 +08:00
// this.renderer.onInit(() => {
2021-04-25 03:45:57 +08:00
renderer.addListener('init', () => {
renderer.renderer.autoClear = false
2021-04-20 18:41:04 +08:00
this.resize()
2021-04-25 03:45:57 +08:00
renderer.addListener('resize', this.resize)
2021-04-20 18:41:04 +08:00
})
},
unmounted() {
2021-04-25 03:45:57 +08:00
this.renderer?.removeListener('resize', this.resize)
2021-04-20 18:41:04 +08:00
},
methods: {
addPass(pass: Pass) {
this.composer?.addPass(pass)
},
removePass(pass: Pass) {
this.composer?.removePass(pass)
},
resize() {
2021-04-25 03:45:57 +08:00
if (this.composer && this.renderer) {
this.composer.setSize(this.renderer.size.width, this.renderer.size.height)
}
2021-04-20 18:41:04 +08:00
},
},
render() {
return this.$slots.default ? this.$slots.default() : []
},
__hmrId: 'EffectComposer',
})