firtoz / react-three-renderer

Render into a three.js canvas using React.
https://toxicfork.github.com/react-three-renderer-example/
MIT License
1.49k stars 155 forks source link

ShaderMaterial uniforms not updating? #212

Closed johnmarinelli closed 6 years ago

johnmarinelli commented 6 years ago

Hi,

I'm trying to add an elapsedTime uniform to my fragment shader that tracks time since the program started.

Here's the relevant portion of the fragment shader:

uniform float elapsedTime;                                                         

void main() {       
  float e = elapsedTime / 100.0;                                                               
  gl_FragColor = vec4(e, e, e, 1.0);                 
}    

So far, I've tried updating the uniform in jsx:

uniforms={{                                                                
  elapsedTime: {value: this.state.elapsedTime}                                          
}} 

and also trying to use a ref (this.handles.skyMesh is a ref to the mesh that holds the material in question)

elapsedTime = this.clock.getElapsedTime();

if (null !== this.handles.skyMesh) {                                      
  this.handles.skyMesh.material.uniforms.elapsedTime.value = elapsedTime;
  this.handles.skyMesh.material.uniforms.elapsedTime.needsUpdate = true;  
  this.handles.skyMesh.material.needsUpdate = true;                                                                                                                          
}   

neither of these approaches work. Do you have any tips/ideas?

toxicFork commented 6 years ago

I'd expect the last one to work...

I'm guessing the color may be bound to max of 1 for the values though, try something like:

gl_FragColor = vec4(sin(e), cos(e), 1.0, 1.0);
toxicFork commented 6 years ago

Also, #152 is relevant :)

johnmarinelli commented 6 years ago

Nice, I've implemented one of the solutions from that thread. Thanks!