Updating uniforms
See original GitHub issueHello!
I’m trying to update a uniform value used in a fragment shader, but the change doesn’t seem to register with the material.
Do I need to do anything other than updating the uniform, like calling needsUpdate or something like that?
(The value of time is changing, I checked that 😉)
Here’s an abbreviated version of what I’m doing:
const fragmentShader = `
precision highp float;
uniform float u_time;
void main() {
gl_FragColor = vec4(u_time, u_time, u_time, 1.0);
}
`;
…
const uniforms = {
u_time: {type: 'f', value: time},
};
…
<mesh rotation-y={time}>
<planeBufferGeometry name="geometry" args={[4, 4]} />
<shaderMaterial
name="material"
uniforms={uniforms}
vertexShader={vertexShader}
fragmentShader={fragmentShader}
</mesh>
Here’s a Code Sandbox demonstrating the problem: https://codesandbox.io/embed/rw980y5jnq
I looked at the documentation and the issues, but couldn’t find a solution.
All examples with updating shaders seem to use react-spring and <animated.shaderMaterial> – does that do anything special?
I also tried using a ref on the mesh and useRender, like so, which didn’t work, either:
useRender(() => {
if (
mesh.current &&
mesh.current.material &&
mesh.current.material.uniforms
) {
mesh.current.material.uniforms.u_time.value = time;
}
});
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Medical Scrubs And Accessories | Update Uniforms | United ...
Click on the New Arrivals tab to see what's new and in stock! Update Uniforms. 2740 Franklin Road. Roanoke, VA 24014. (540) 982-8438....
Read more >How to Update Your Uniform Dress Policy
Updating your current uniform dress policy can be an exciting and challenging time. Here is how to create visible uniformity by updating your...
Read more >Updating uniforms · Issue #53 · pmndrs/react-three-fiber - GitHub
Hello! I'm trying to update a uniform value used in a fragment shader, but the change doesn't seem to register with the material....
Read more >UPDATE UNIFORMS - 2740 Franklin Rd SW, Roanoke, VA
Update Uniforms is located at 2740 Franklin Rd SW, Roanoke, VA. Is this your business? Respond to reviews and customer messages. Claiming is...
Read more >How do you update a uniform In Three.js? - Stack Overflow
You can update uniform in Shader Material itself. OR · you can use mesh.material to access the ShaderMaterial and then update the uniform....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks so much for the explanation! I didn’t know that I had to keep the same uniform between renders and only change the value of
u_time. I guess I was sort of expecting the shaderMaterial component to update when theuniformprop changed.This made me understand:
In the end, I went with
useMemoin the component, as I’m loading a texture as well. Thank you so much for your help!tbh i don’t understand how three-js treats uniforms. it is a little odd. i know i’ve been struggling with it before. they seem to cache it on first run, once a uniform is set it looks to me like it can’t be exchanged again, only properties can be mutated.