question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating uniforms

See original GitHub issue

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.

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:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
julianscommented, Apr 1, 2019

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 the uniform prop changed.

This made me understand:

if you can’t set the entire uniforms structure in threejs, then you can’t do it here, the same rules apply

In the end, I went with useMemo in the component, as I’m loading a texture as well. Thank you so much for your help!

function Scene() {
  const material = useRef();

  let t = 0;
  useRender(
    () => (material.current.uniforms.u_time.value = t = (t + 0.01) % 1),
  );

  const [texture1991] = useMemo(() => {
    const loader = new TextureLoader();
    return [loader.load(texture1991Url)];
  }, [texture1991Url]);

  const uniforms = useMemo(() => {
    return {
      u_time: {type: 'f', value: 0},
      u_texture: {
        type: 't',
        value: texture1991,
      },
    };
  }, [texture1991]);

  return (
    <mesh>
      <planeBufferGeometry attach="geometry" args={[4, 4]} />
      <shaderMaterial
        attach="material"
        ref={material}
        uniforms={uniforms}
        vertexShader={vertexShader}
        fragmentShader={fragmentShader}
      />
    </mesh>
  );
}
0reactions
drcmdacommented, Apr 1, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found