Hi everybody,
I’m affraid this is more a R3F issue but I’m not sure of the feasibility of this.
So, I’ve got a shaderMaterial that I populate with those :
export const vertexShader = `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`
export const fragmentShader = `
uniform vec3 color1;
uniform vec3 color2;
varying vec2 vUv;
void main() {
gl_FragColor = vec4(mix(color1, color2, vUv.y), 1.0);
}
Now I’ve got to set the uniforms like this (example) :
let uniforms = {
color1: {
value: new THREE.Color("red")
},
color2: {
value: new THREE.Color("blue")
}
}
And this works as a charm
Now I want my shader colors to fade when pressing a button for animation purpose.
If my material was a meshBasicMaterial, I would have done that :
const { color1anim } = useSpring({
color1anim: !custom ? "red" : "yellow",
config: config.smooth,
delay: 300
})
return (
<meshBasicMaterial color={color1anim}/>
)
And then it works well…
But now i’d like to do the same with my shaderMaterial… which args are an array containing this object, and uniforms will work as passed in the comments :
<animated.shaderMaterial
/* uniforms={{
color1: {
value: new THREE.Color("#1b00e6")
},
color2: {
value: new THREE.Color("#ff08da")
}
}} */
args={[{
uniforms:{
color1: {
value: new THREE.Color("blue")
},
color2: {
value: new THREE.Color("red")
}
},
vertexShader: vertexShader,
fragmentShader: fragmentShader,
}]}
/>
But when I use my spring color values instead, like the next example, they’ll always be undefined.
const { color1anim } = useSpring({
color1anim: !custom ? "red" : "yellow",
config: config.smooth,
delay: 300
})
const { color2anim } = useSpring({
color2anim: !custom ? "green" : "blue",
config: config.smooth,
delay: 300
})
<animated.shaderMaterial
/* uniforms={{
color1: {
value: new THREE.Color("#1b00e6")
},
color2: {
value: new THREE.Color("#ff08da")
}
}} */
args={[{
uniforms:{
color1: {
value: new THREE.Color(color1anim)
},
color2: {
value: new THREE.Color(color2anim)
}
},
vertexShader: vertexShader,
fragmentShader: fragmentShader,
}]}
/>
)
I think there’s a problem coming from the way props are passed through the component, but does someone has a clue about updating corectly my Spring changing colors in this type of props (again, no problem if passed in a simple prop like rotation-x, color, scale) ?
Thanks a lot !