mirror of
https://github.com/troisjs/trois.git
synced 2024-11-24 04:12:02 +08:00
ts => js for postprocessing
This commit is contained in:
parent
3715262cb3
commit
4900a69c30
@ -1,14 +1,13 @@
|
|||||||
import { defineComponent, inject, onMounted, onUnmounted, PropType } from 'vue'
|
import { defineComponent, inject, onMounted, onUnmounted } from 'vue'
|
||||||
import { LoadingManager } from 'three'
|
import { LoadingManager } from 'three'
|
||||||
// @ts-ignore
|
|
||||||
import * as PP from 'postprocessing'
|
import * as PP from 'postprocessing'
|
||||||
import { EffectPassInjectionKey, EffectPassInterface } from './EffectPass'
|
import { EffectPassInjectionKey } from './EffectPass'
|
||||||
|
|
||||||
type EffectTypes = 'bloom' | 'dof' | 'godrays' | 'smaa'
|
// type EffectTypes = 'bloom' | 'dof' | 'godrays' | 'smaa'
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
props: {
|
props: {
|
||||||
type: { type: String as PropType<EffectTypes>, required: true },
|
type: { type: String, required: true },
|
||||||
options: { type: Object, default: () => ({}) },
|
options: { type: Object, default: () => ({}) },
|
||||||
onReady: Function,
|
onReady: Function,
|
||||||
},
|
},
|
||||||
@ -19,23 +18,23 @@ export default defineComponent({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let effect: undefined | PP.Effect // not useful
|
let effect
|
||||||
const effectIndex = effectPass.getEffectIndex()
|
const effectIndex = effectPass.getEffectIndex()
|
||||||
|
|
||||||
const initEffect = (params: any = undefined) => {
|
const initEffect = (params) => {
|
||||||
effect = createEffect(effectPass, props.type, props.options, params)
|
effect = createEffect(effectPass, props.type, props.options, params)
|
||||||
if (!effect) {
|
if (!effect) {
|
||||||
console.error('Invalid effect type')
|
console.error('Invalid effect type')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
props.onReady?.(effect)
|
if (props.onReady) props.onReady(effect)
|
||||||
effectPass.addEffect(effect, effectIndex)
|
effectPass.addEffect(effect, effectIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
if (props.type === 'smaa') {
|
if (props.type === 'smaa') {
|
||||||
const smaaImageLoader = new PP.SMAAImageLoader(new LoadingManager())
|
const smaaImageLoader = new PP.SMAAImageLoader(new LoadingManager())
|
||||||
smaaImageLoader.load(([search, area]: [HTMLImageElement, HTMLImageElement]) => {
|
smaaImageLoader.load(([search, area]) => {
|
||||||
initEffect({ smaaSearch: search, smaaArea: area })
|
initEffect({ smaaSearch: search, smaaArea: area })
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -53,12 +52,7 @@ export default defineComponent({
|
|||||||
render() { return [] },
|
render() { return [] },
|
||||||
})
|
})
|
||||||
|
|
||||||
function createEffect(
|
function createEffect(effectPass, type, options, assets) {
|
||||||
effectPass: EffectPassInterface,
|
|
||||||
type: string,
|
|
||||||
options: Record<string, any>,
|
|
||||||
assets: any = undefined
|
|
||||||
): PP.Effect {
|
|
||||||
let effect
|
let effect
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'bloom' :
|
case 'bloom' :
|
||||||
@ -77,14 +71,13 @@ function createEffect(
|
|||||||
return effect
|
return effect
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSmaaEffect(options: Record<string, any>, assets: any): PP.Pass {
|
function createSmaaEffect(options, assets) {
|
||||||
const { smaaSearch, smaaArea } = assets
|
const { smaaSearch, smaaArea } = assets
|
||||||
// TODO : options
|
|
||||||
const params = [options.preset ?? PP.SMAAPreset.HIGH, options.edgeDetectionMode ?? PP.EdgeDetectionMode.COLOR]
|
const params = [options.preset ?? PP.SMAAPreset.HIGH, options.edgeDetectionMode ?? PP.EdgeDetectionMode.COLOR]
|
||||||
return new PP.SMAAEffect(smaaSearch, smaaArea, ...params)
|
return new PP.SMAAEffect(smaaSearch, smaaArea, ...params)
|
||||||
}
|
}
|
||||||
|
|
||||||
function createGodraysEffect(effectPass: EffectPassInterface, options: Record<string, any>): PP.Pass {
|
function createGodraysEffect(effectPass, options) {
|
||||||
const opts = { ...options }
|
const opts = { ...options }
|
||||||
const { lightSource } = options
|
const { lightSource } = options
|
||||||
if (typeof lightSource !== 'string') {
|
if (typeof lightSource !== 'string') {
|
||||||
@ -93,7 +86,7 @@ function createGodraysEffect(effectPass: EffectPassInterface, options: Record<st
|
|||||||
}
|
}
|
||||||
delete opts.lightSource
|
delete opts.lightSource
|
||||||
|
|
||||||
const lightSourceComp = effectPass.renderer.$root?.$refs[lightSource] as any
|
const lightSourceComp = effectPass.renderer.$root.$refs[lightSource]
|
||||||
if (!lightSourceComp) {
|
if (!lightSourceComp) {
|
||||||
console.error('Invalid lightSource ref')
|
console.error('Invalid lightSource ref')
|
||||||
return
|
return
|
@ -1,20 +1,11 @@
|
|||||||
import { defineComponent, inject, InjectionKey, onUnmounted, provide } from 'vue'
|
import { inject, onUnmounted, provide } from 'vue'
|
||||||
import { Clock } from 'three'
|
import { Clock } from 'three'
|
||||||
// @ts-ignore
|
import { EffectComposer } from 'postprocessing'
|
||||||
import * as PP from 'postprocessing'
|
|
||||||
import { RendererInjectionKey } from '../../../../build/trois.module.js'
|
import { RendererInjectionKey } from '../../../../build/trois.module.js'
|
||||||
import { RendererPublicInterface } from '../../../../build/trois'
|
|
||||||
// import { RendererInjectionKey, RendererPublicInterface } from '../../../export'
|
|
||||||
|
|
||||||
export interface EffectComposerInterface {
|
export const ComposerInjectionKey = Symbol('Composer')
|
||||||
renderer: RendererPublicInterface
|
|
||||||
composer: PP.EffectComposer
|
|
||||||
getPassIndex: {(): number}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ComposerInjectionKey: InjectionKey<EffectComposerInterface> = Symbol('Composer')
|
export default {
|
||||||
|
|
||||||
export default defineComponent({
|
|
||||||
setup() {
|
setup() {
|
||||||
const renderer = inject(RendererInjectionKey)
|
const renderer = inject(RendererInjectionKey)
|
||||||
if (!renderer) {
|
if (!renderer) {
|
||||||
@ -22,7 +13,7 @@ export default defineComponent({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const composer = new PP.EffectComposer(renderer.renderer)
|
const composer = new EffectComposer(renderer.renderer)
|
||||||
const clock = new Clock()
|
const clock = new Clock()
|
||||||
const render = () => { composer.render(clock.getDelta()) }
|
const render = () => { composer.render(clock.getDelta()) }
|
||||||
const setSize = () => { composer.setSize(renderer.size.width, renderer.size.height) }
|
const setSize = () => { composer.setSize(renderer.size.width, renderer.size.height) }
|
||||||
@ -47,4 +38,4 @@ export default defineComponent({
|
|||||||
render() {
|
render() {
|
||||||
return this.$slots.default ? this.$slots.default() : []
|
return this.$slots.default ? this.$slots.default() : []
|
||||||
},
|
},
|
||||||
})
|
}
|
@ -1,23 +1,10 @@
|
|||||||
import { defineComponent, inject, InjectionKey, onUnmounted, provide } from 'vue'
|
import { inject, onUnmounted, provide } from 'vue'
|
||||||
// @ts-ignore
|
import { EffectPass } from 'postprocessing'
|
||||||
import * as PP from 'postprocessing'
|
import { ComposerInjectionKey } from './EffectComposer.js'
|
||||||
import { ComposerInjectionKey, EffectComposerInterface } from './EffectComposer'
|
|
||||||
import { RendererPublicInterface } from '../../../../build/trois'
|
|
||||||
// import { RendererPublicInterface } from '../../../export'
|
|
||||||
|
|
||||||
export interface EffectPassInterface {
|
export const EffectPassInjectionKey = Symbol('EffectPass')
|
||||||
composer: EffectComposerInterface
|
|
||||||
renderer: RendererPublicInterface
|
|
||||||
effectPass: PP.EffectPass
|
|
||||||
effects: Array<PP.Effect>
|
|
||||||
getEffectIndex: {(): number}
|
|
||||||
addEffect: {(effect: PP.Effect, index: number): number}
|
|
||||||
removeEffect: {(effect: PP.Effect): number}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const EffectPassInjectionKey: InjectionKey<EffectPassInterface> = Symbol('Composer')
|
export default {
|
||||||
|
|
||||||
export default defineComponent({
|
|
||||||
props: {
|
props: {
|
||||||
// needsSwap: { type: Boolean, default: false },
|
// needsSwap: { type: Boolean, default: false },
|
||||||
renderToScreen: { type: Boolean, default: false },
|
renderToScreen: { type: Boolean, default: false },
|
||||||
@ -30,9 +17,9 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const passIndex = composer.getPassIndex()
|
const passIndex = composer.getPassIndex()
|
||||||
let effectPass: PP.EffectPass
|
let effectPass
|
||||||
|
|
||||||
const effects: Array<PP.Effect> = []
|
const effects = []
|
||||||
let effectIndex = 0
|
let effectIndex = 0
|
||||||
const getEffectIndex = () => { return effectIndex++ }
|
const getEffectIndex = () => { return effectIndex++ }
|
||||||
|
|
||||||
@ -42,16 +29,16 @@ export default defineComponent({
|
|||||||
composer.composer.removePass(effectPass)
|
composer.composer.removePass(effectPass)
|
||||||
effectPass.dispose()
|
effectPass.dispose()
|
||||||
}
|
}
|
||||||
effectPass = new PP.EffectPass(composer.renderer.camera, ...effects)
|
effectPass = new EffectPass(composer.renderer.camera, ...effects)
|
||||||
composer.composer.addPass(effectPass, passIndex)
|
composer.composer.addPass(effectPass, passIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
const addEffect = (effect: PP.Effect, index: number) => {
|
const addEffect = (effect, index) => {
|
||||||
effects.splice(index, 1, effect)
|
effects.splice(index, 1, effect)
|
||||||
refreshEffectPass()
|
refreshEffectPass()
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeEffect = (effect: PP.Effect) => {
|
const removeEffect = (effect) => {
|
||||||
const index = effects.indexOf(effect)
|
const index = effects.indexOf(effect)
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
effects.splice(index, 1)
|
effects.splice(index, 1)
|
||||||
@ -76,4 +63,4 @@ export default defineComponent({
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
render() { return this.$slots.default ? this.$slots.default() : [] },
|
render() { return this.$slots.default ? this.$slots.default() : [] },
|
||||||
})
|
}
|
@ -1,15 +1,12 @@
|
|||||||
import { defineComponent, inject, onUnmounted, PropType } from 'vue'
|
import { inject, onUnmounted } from 'vue'
|
||||||
// @ts-ignore
|
import { BlurPass, RenderPass } from 'postprocessing'
|
||||||
import * as PP from 'postprocessing'
|
|
||||||
import { ComposerInjectionKey } from './EffectComposer'
|
import { ComposerInjectionKey } from './EffectComposer'
|
||||||
import { RendererPublicInterface } from '../../../../build/trois'
|
|
||||||
// import { RendererPublicInterface } from '../../../export'
|
|
||||||
|
|
||||||
type PassTypes = 'render' | 'blur'
|
// type PassTypes = 'render' | 'blur'
|
||||||
|
|
||||||
export default defineComponent({
|
export default {
|
||||||
props: {
|
props: {
|
||||||
type: { type: String as PropType<PassTypes>, required: true },
|
type: { type: String, required: true },
|
||||||
options: { type: Object, default: () => ({}) },
|
options: { type: Object, default: () => ({}) },
|
||||||
// needsSwap: { type: Boolean, default: false },
|
// needsSwap: { type: Boolean, default: false },
|
||||||
renderToScreen: { type: Boolean, default: false },
|
renderToScreen: { type: Boolean, default: false },
|
||||||
@ -22,17 +19,17 @@ export default defineComponent({
|
|||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
let pass: undefined | PP.Pass
|
let pass
|
||||||
const passIndex = composer.getPassIndex()
|
const passIndex = composer.getPassIndex()
|
||||||
|
|
||||||
const initPass = (params: any = undefined) => {
|
const initPass = () => {
|
||||||
pass = createPass(composer.renderer, props.type, props.options)
|
pass = createPass(composer.renderer, props.type, props.options)
|
||||||
if (!pass) {
|
if (!pass) {
|
||||||
console.error('Invalid pass type')
|
console.error('Invalid pass type')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pass.renderToScreen = props.renderToScreen
|
pass.renderToScreen = props.renderToScreen
|
||||||
props.onReady?.(pass)
|
if (props.onReady) props.onReady(pass)
|
||||||
composer.composer.addPass(pass, passIndex)
|
composer.composer.addPass(pass, passIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,20 +43,16 @@ export default defineComponent({
|
|||||||
initPass()
|
initPass()
|
||||||
},
|
},
|
||||||
render() { return [] },
|
render() { return [] },
|
||||||
})
|
}
|
||||||
|
|
||||||
function createPass(
|
function createPass(renderer, type, options) {
|
||||||
renderer: RendererPublicInterface,
|
|
||||||
type: string,
|
|
||||||
options: Record<string, any>
|
|
||||||
): PP.Pass {
|
|
||||||
let pass
|
let pass
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'render' :
|
case 'render' :
|
||||||
pass = new PP.RenderPass(renderer.scene, renderer.camera)
|
pass = new RenderPass(renderer.scene, renderer.camera)
|
||||||
break
|
break
|
||||||
case 'blur' :
|
case 'blur' :
|
||||||
pass = new PP.BlurPass(options)
|
pass = new BlurPass(options)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return pass
|
return pass
|
Loading…
Reference in New Issue
Block a user