Skip to content

Commit d20ddad

Browse files
fix: with the v5 the reflector component stop working.
Problem: the way the color prop was being set Solution: set the color prop manually using watch
1 parent 9bc368d commit d20ddad

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

playground/vue/src/pages/abstractions/ReflectorMeshDemo.vue

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
} from '@tresjs/cientos'
88
import { TresCanvas } from '@tresjs/core'
99
import { BasicShadowMap, NoToneMapping, SRGBColorSpace } from 'three'
10-
import { shallowRef, watch } from 'vue'
10+
import { TresLeches, useControls } from '@tresjs/leches'
11+
import '@tresjs/leches/styles'
1112
1213
const gl = {
1314
clearColor: '#111',
@@ -18,22 +19,19 @@ const gl = {
1819
toneMapping: NoToneMapping,
1920
}
2021
21-
const reflectorRef = shallowRef()
22-
23-
watch(reflectorRef, (value) => {
24-
// eslint-disable-next-line no-console
25-
console.log(value)
26-
})
27-
2822
const options = {
29-
color: '#f7f7f7',
3023
clipBias: 0,
3124
textureWidth: 1024,
3225
textureHeight: 1024,
3326
}
27+
28+
const { color } = useControls({
29+
color: { value: '#f7f7f7', type: 'color', label: 'Color' },
30+
})
3431
</script>
3532

3633
<template>
34+
<TresLeches />
3735
<TresCanvas
3836
v-bind="gl"
3937
>
@@ -51,10 +49,9 @@ const options = {
5149
/>
5250
</TresMesh>
5351
<Reflector
54-
ref="reflectorRef"
5552
:rotation="[-Math.PI * 0.5, 0, 0]"
5653
:position="[0, -2, 0]"
57-
:color="options.color"
54+
:color="color"
5855
:clip-bias="options.clipBias"
5956
:texture-width="options.textureWidth"
6057
:texture-height="options.textureHeight"

src/core/abstractions/Reflector.vue

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { useTres } from '@tresjs/core'
33
import { Reflector } from 'three-stdlib'
44
import { shallowRef, toRefs, watch } from 'vue'
55
import type { TresColor } from '@tresjs/core'
6+
import type { ShaderMaterial } from 'three'
7+
8+
const props = withDefaults(defineProps<ReflectorProps>(), {
9+
color: '#333',
10+
textureWidth: 512,
11+
textureHeight: 512,
12+
clipBias: 0,
13+
multisample: 4,
14+
// @ts-expect-error: `ReflectorShader` is not present in imported type but is present here:
15+
// https://github.com/mrdoob/three.js/blob/dev/examples/jsm/objects/Reflector.js#L32
16+
shader: Reflector.ReflectorShader,
17+
})
618
719
export interface ReflectorProps {
820
/**
@@ -61,21 +73,17 @@ export interface ReflectorProps {
6173
shader?: object
6274
}
6375
64-
const props = withDefaults(defineProps<ReflectorProps>(), {
65-
color: '#333',
66-
textureWidth: 512,
67-
textureHeight: 512,
68-
clipBias: 0,
69-
multisample: 4,
70-
// @ts-expect-error: `ReflectorShader` is not present in imported type but is present here:
71-
// https://github.com/mrdoob/three.js/blob/dev/examples/jsm/objects/Reflector.js#L32
72-
shader: Reflector.ReflectorShader,
73-
})
74-
7576
const { extend, invalidate } = useTres()
7677
7778
const reflectorRef = shallowRef<Reflector>()
7879
80+
watch(reflectorRef, (value) => {
81+
if (value) {
82+
const material = value.material as ShaderMaterial
83+
material.uniforms.color.value.set(props.color as string)
84+
}
85+
})
86+
7987
extend({ Reflector })
8088
8189
const { color, textureWidth, textureHeight, clipBias, multisample, shader }
@@ -85,6 +93,11 @@ watch(props, () => {
8593
invalidate()
8694
})
8795
96+
watch(color, (_color) => {
97+
const material = reflectorRef.value?.material as ShaderMaterial
98+
material?.uniforms.color.value.set(_color as string)
99+
})
100+
88101
defineExpose({
89102
instance: reflectorRef,
90103
})
@@ -94,7 +107,6 @@ defineExpose({
94107
<TresReflector
95108
ref="reflectorRef"
96109
:args="[undefined, { textureWidth, textureHeight, clipBias, multisample, shader }]"
97-
:material-uniforms-color-value="color"
98110
>
99111
<slot>
100112
<TresPlaneGeometry :args="[5, 5]" />

0 commit comments

Comments
 (0)