Ripple Shader
A website animation tutorial featuring a ripple shader effect using React Three Fiber, Next.js and React. Inspired by https://homunculus.jp/ and Yuri Artiukh.
Olivier Larose
February 17, 2024
/
Beginner
/
Short
A website tutorial on how to create a Glass looking Material with distortion by using the MeshTransmissionMaterial with Three.js, React, React Three Fiber and Next.js.
Live DemoSource codeInside Blender I create a 3D model of a Taurus, the shape that I want for this animation.
When the 3D scene is done, we can export it inside a .GLTF or .GLB file that we can then import inside a Three.js scene.
Let's start the project by creating a Next.js application. We can do that by running npx create-next-app@latest client
inside of a terminal.
We can delete everything in the page.js
, global.css
and page.module.css
and add our own HTML and CSS, to start with a nice blank application.
npm i three @react-three/fiber
.npm i @react-three/drei
.For the scene, I'm creating an external component that I'm lazy loading using the Dynamic import, which is a Next.js built in function.
page.js
1
import styles from './page.module.css'2
import dynamic from 'next/dynamic'3
4
const Scene = dynamic(() => import('@/components/Scene'), {5
ssr: false,6
})7
8
export default function Home() {9
return (10
<main className={styles.main}>11
<Scene />12
</main>13
)14
}
Note: Here I use the ssr: false
option to force the component to strictly be rendered client-side.
Here I'm creating a React Three Fiber Canvas and inside of it I'm adding an external Model Component where I'll render the 3D models.
I also add a basic light with an Environment to add lighting and colors to the overall scene.
Scene.jsx
1
'use client';2
import { Canvas } from '@react-three/fiber'3
import Model from './Model';4
import { Environment } from '@react-three/drei'5
6
export default function Index() {7
return (8
<Canvas style={{background: '#000000'}}>9
<Model />10
<directionalLight intensity={2} position={[0, 2, 3]}/>11
<Environment preset="city" />12
</Canvas>13
)14
}
For the model, I simply import the .GLB file and select the Torus mesh inside of it. Inside the node is a bunch of information about position, scale, etc.
Model.jsx
1
import React, { useRef } from 'react'2
import { useGLTF, Text } from "@react-three/drei";3
import { useFrame, useThree } from '@react-three/fiber'4
5
export default function Model() {6
const { nodes } = useGLTF("/medias/torrus.glb");7
const { viewport } = useThree()8
const torus = useRef(null);9
10
useFrame( () => {11
torus.current.rotation.x += 0.0212
})13
14
return (15
<group scale={viewport.width / 3.75} >16
<Text font={'/fonts/PPNeueMontreal-Bold.otf'} position={[0, 0, -1]} fontSize={0.5} color="white" anchorX="center" anchorY="middle">17
hello world!18
</Text>19
<mesh ref={torus} {...nodes.Torus002}>20
<meshBasicMaterial/>21
</mesh>22
</group>23
)24
}
useFramer
to continually rotate the mesh.For the transmission material, if you want transparency as well as distortion, you would have to do a custom shader. Thankfully, there is one made by the community inside the React Three Drei package called MeshTransmissionMaterial which gives us everything we need out of the box.
It's basically an extension of the Three.js MeshPhysicalMaterial, which already support transmission but adds on top of it multiple other properties like thickness, distortion and chromaticAberration. These properties really help give a realistic glass effect.
So what I'll do is apply this material to my Taurus:
Model.jsx
1
import { MeshTransmissionMaterial, ... } from "@react-three/drei";2
import { useControls } from 'leva';3
4
...5
6
export default function Model() {7
...8
const materialProps = useControls({9
thickness: { value: 0.2, min: 0, max: 3, step: 0.05 },10
roughness: { value: 0, min: 0, max: 1, step: 0.1 },11
transmission: {value: 1, min: 0, max: 1, step: 0.1},12
ior: { value: 1.2, min: 0, max: 3, step: 0.1 },13
chromaticAberration: { value: 0.02, min: 0, max: 1},14
backside: { value: true},15
})16
17
return (18
<group scale={viewport.width / 3} >19
...20
<mesh ref={torus} {...nodes.Torus002}>21
<MeshTransmissionMaterial {...materialProps}/>22
</mesh>23
</group>24
)25
}
That's it for this tutorial!
I'm just taken away by how powerful and easy to use this material is. I'm super grateful of being part of such a great community as well. Hope you learned something!
-Oli