2021-04-20 18:41:04 +08:00
|
|
|
import { defineComponent, inject } from 'vue'
|
|
|
|
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js'
|
|
|
|
import { Pass } from 'three/examples/jsm/postprocessing/Pass'
|
2021-04-21 08:17:31 +08:00
|
|
|
import { RendererInterface } from '../core/Renderer'
|
2021-04-20 18:41:04 +08:00
|
|
|
|
|
|
|
interface EffectComposerSetupInterface {
|
2021-04-21 08:17:31 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
setup(): EffectComposerSetupInterface {
|
2021-04-21 08:17:31 +08:00
|
|
|
const renderer = inject('renderer') as RendererInterface
|
2021-04-20 18:41:04 +08:00
|
|
|
return {
|
2021-04-21 08:17:31 +08:00
|
|
|
renderer,
|
2021-04-20 18:41:04 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
provide() {
|
|
|
|
return {
|
|
|
|
composer: this,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
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-21 08:17:31 +08:00
|
|
|
this.renderer.three.composer = composer
|
2021-04-20 18:41:04 +08:00
|
|
|
|
2021-04-21 08:17:31 +08:00
|
|
|
// this.renderer.onInit(() => {
|
|
|
|
this.renderer.addListener('init', () => {
|
|
|
|
this.renderer.renderer.autoClear = false
|
2021-04-20 18:41:04 +08:00
|
|
|
this.resize()
|
2021-04-21 08:17:31 +08:00
|
|
|
// this.renderer.onResize(this.resize)
|
|
|
|
this.renderer.addListener('resize', this.resize)
|
2021-04-20 18:41:04 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
unmounted() {
|
2021-04-21 08:17:31 +08:00
|
|
|
// this.renderer.offResize(this.resize)
|
|
|
|
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-21 08:17:31 +08:00
|
|
|
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',
|
|
|
|
})
|