Skip to content

Commit 7c5bfe0

Browse files
fix: god rays async light source (#194)
* fix: god rays async light source * fix: direct testing * fix: lint * fix: direct testing * fix: code review * fix: add reactive computef * fix: code review and change deprecated
1 parent 3e38d9b commit 7c5bfe0

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

playground/src/pages/postprocessing/god-rays.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TresLeches, useControls } from '@tresjs/leches'
55
import { NoToneMapping } from 'three'
66
import { BlendFunction, KernelSize, Resolution } from 'postprocessing'
77
import { EffectComposerPmndrs, GodRaysPmndrs } from '@tresjs/post-processing'
8+
import { shallowRef } from 'vue'
89
910
import '@tresjs/leches/styles'
1011
@@ -14,8 +15,7 @@ const gl = {
1415
multisampling: 8,
1516
}
1617
17-
const boxMeshRef = ref(null)
18-
const sphereMeshRef = ref(null)
18+
const sphereMeshRef = shallowRef(null)
1919
2020
const { blur, kernelSize, resolutionX, resolutionY, resolutionScale, opacity, blendFunction, density, decay, weight, exposure, samples, clampMax } = useControls({
2121
blendFunction: {
@@ -69,7 +69,7 @@ const { blur, kernelSize, resolutionX, resolutionY, resolutionScale, opacity, bl
6969
<TresMeshBasicMaterial color="#FFDDAA" />
7070
</TresMesh>
7171

72-
<TresMesh ref="boxMeshRef" :position="[0, .5, 0]">
72+
<TresMesh :position="[0, .5, 0]">
7373
<TresBoxGeometry :args="[2, 2, 2]" />
7474
<TresMeshBasicMaterial color="white" />
7575
</TresMesh>

src/core/pmndrs/GodRaysPmndrs.vue

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { GodRaysEffect } from 'postprocessing'
44
import { makePropWatchers } from '../../util/prop'
55
import { useEffectPmndrs } from './composables/useEffectPmndrs'
66
import { useTresContext } from '@tresjs/core'
7-
import { toRaw, watch } from 'vue'
8-
import type { Mesh, Points } from 'three'
7+
import { computed, toRaw, watch } from 'vue'
8+
import type { Points } from 'three'
9+
import { Mesh, MeshBasicMaterial, SphereGeometry } from 'three'
910
1011
export interface GodRaysPmndrsProps {
1112
/**
@@ -16,7 +17,7 @@ export interface GodRaysPmndrsProps {
1617
/**
1718
* The light source. Must not write depth and has to be flagged as transparent.
1819
*/
19-
lightSource?: Mesh | Points
20+
lightSource?: Mesh | Points | null
2021
2122
/**
2223
* The opacity of the God Rays.
@@ -83,8 +84,15 @@ const props = defineProps<GodRaysPmndrsProps>()
8384
8485
const { camera } = useTresContext()
8586
87+
const resolvedLightSource = computed(() =>
88+
props.lightSource ?? new Mesh(
89+
new SphereGeometry(0.00001),
90+
new MeshBasicMaterial({ visible: false }),
91+
),
92+
)
93+
8694
const { pass, effect } = useEffectPmndrs(
87-
() => new GodRaysEffect(camera.value, toRaw(props.lightSource), props),
95+
() => new GodRaysEffect(camera.value, resolvedLightSource.value, props),
8896
props,
8997
)
9098
@@ -102,21 +110,34 @@ makePropWatchers(
102110
[() => props.resolutionScale, 'resolution.scale'],
103111
[() => props.resolutionX, 'resolution.width'],
104112
[() => props.resolutionY, 'resolution.height'],
105-
[() => props.kernelSize, 'kernelSize'],
106-
[() => props.blur, 'blur'],
113+
[() => props.kernelSize, 'blurPass.kernelSize'],
114+
[() => props.blur, 'blurPass.enabled'],
107115
],
108116
effect,
109117
() => new GodRaysEffect(),
110118
)
111119
120+
watch(
121+
[() => props.lightSource, effect],
122+
() => {
123+
if (effect.value) {
124+
effect.value.lightSource = toRaw(resolvedLightSource.value)
125+
}
126+
},
127+
{ immediate: true },
128+
)
129+
112130
watch(
113131
[() => props.opacity],
114132
() => {
115133
if (props.opacity !== undefined) {
116134
effect.value?.blendMode.setOpacity(props.opacity)
117135
}
118136
else {
119-
const plainEffect = new GodRaysEffect(camera.value, toRaw(props.lightSource))
137+
const plainEffect = new GodRaysEffect(
138+
camera.value,
139+
toRaw(resolvedLightSource.value),
140+
)
120141
effect.value?.blendMode.setOpacity(plainEffect.blendMode.getOpacity())
121142
plainEffect.dispose()
122143
}

0 commit comments

Comments
 (0)