Animating uniforms in ShaderMaterial
See original GitHub issueHey, I’m quite new to all this, but I thought this might be a good occasion to get more familiar with THREE. I’m trying to replicate this demo here using react-three-fiber. https://tympanus.net/Development/DistortionHoverEffect/
From the demo source the material is built and animated as below with GSAP (dispFactor value animates from 0 to 1):
var mat = new THREE.ShaderMaterial({
uniforms: {
effectFactor: { type: "f", value: intensity },
dispFactor: { type: "f", value: 0.0 },
texture: { type: "t", value: texture1 },
texture2: { type: "t", value: texture2 },
disp: { type: "t", value: disp }
},
vertexShader: vertex,
fragmentShader: fragment,
transparent: true,
opacity: 1.0
});
// animation with GSAP
parent.addEventListener(evtIn, function(e) {
TweenMax.to(mat.uniforms.dispFactor, speedIn, {
value: 1,
ease: easing
});
});
I’ve tried to do the same with react-three-fiber and react-spring but nothing happens or nothing shows. Any idea what I’m doing wrong?
const [hovered, setHover] = useState(false)
const hover = useCallback(() => setHover(true), [])
const unhover = useCallback(() => setHover(false), [])
const { progress } = useSpring({ progress: hovered ? 1 : 0 })
return (
<anim.mesh {...props} onHover={hover} onUnhover={unhover}>
<planeBufferGeometry name="geometry" args={[3.8, 3.8]} />
<anim.shaderMaterial
name="material"
uniforms={{
effectFactor: { type: 'f', value: intensity },
dispFactor: { type: 'f', value: progress },
texture: { type: 't', value: texture1 },
texture2: { type: 't', value: texture2 },
disp: { type: 't', value: displacement }
}}
{...HoverShader}
transparent
opacity={1}
/>
</anim.mesh>
);
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
Animating uniforms in ShaderMaterial · Issue #23 - GitHub
Hey, I'm quite new to all this, but I thought this might be a good occasion to get more familiar with THREE. I'm...
Read more >Animating ThreeJS ShaderMaterial Uniforms - JSFiddle
The fiddle listings (Public, Private, Titled, etc) will now display latest versions instead of the ones saved as Base versions - this was...
Read more >How to Use Shaders as Materials in Three.js (with Uniforms)
I've recently learned how to use Three.js's ShaderMaterial to map shaders onto meshes. If you're not familiar with shaders, I recommend you ...
Read more >ShaderMaterial#uniforms – three.js docs
Custom uniforms must be defined in both the uniforms property of your ShaderMaterial , whereas any custom attributes must be defined via BufferAttribute ......
Read more >Synching two meshes, one with ShaderMaterial - Stack Overflow
I've got two meshes created from the same geometry and running the same animation. If I do absolutely nothing to the meshes they...
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

@marceloavf this is what worked for me in some code from many months ago, maybe you can try writing your code in a similar way and see if it works? Here it is:
There’s also a blog post I wrote around the same time (hopefully not too out of date) on this topic https://bearjam.dev/blog/react-three-fiber-shaders-setup
now it works: https://codesandbox.io/s/x37p4kk6p4
but that’s still weird. it should be able to pierce into the value, but for some reason it only works when i write the object around it, for which it needs to interpolate.
i’ll fix this tomorrow, then you will be able to just do: uniforms-xxxxx-value={animatedProp}