Monday, April 21, 2025
HomeProgrammingTaking part in with Mild and Refraction in Three.js: Warping 3D Textual...

Taking part in with Mild and Refraction in Three.js: Warping 3D Textual content Inside a Glass Torus


Hello! I’m Matt Park, a Inventive Developer primarily based in New York.

On this article, we’ll be exploring one of many best but coolest results you possibly can create in Three.js—the Transmission Materials.

To be exact, native Three.js doesn’t have a fabric known as TransmissionMaterial, however Drei does. Drei is a library full of helper elements designed to boost the event of React Three Fiber functions.

Though MeshTransmissionMaterial comes from Drei, it’s not a wholly new materials. Drei layers a bunch of shaders on prime of Three.js’ MeshPhysicalMaterial to create refraction results.

Setup

The unique concept behind the mannequin used on this article comes from a development within the Blender neighborhood the place round textual content is positioned inside a glass-like materials.

Upon seeing this Blender tutorial, I instantly knew it was the proper mannequin to experiment with MeshTransmissionMaterial, so I rapidly modeled it in Blender.

For these following together with this text, needless to say utilizing this particular mannequin isn’t vital in any respect. The basics of how Transmission Materials works stay the identical for all fashions, even for a easy boxGeometry 🙂

First Steps

When you load your mannequin into your Three.js scene with MeshTransmissionMaterial, you may be shocked to see a really dull-looking materials.

Then we return to the unique MeshPhysicalMaterial docs, solely to seek out the directions under.

It is because the important thing to transmission supplies lies in how mild is mirrored by means of the fabric. That’s the essence of any clear materials in actual life as effectively. Consider it this fashion: if a fabric have been clear with none reflections or mild bending throughout its floor, it could look as uninteresting because the torus above. The great thing about any clear materials comes from the best way mild bends and refracts by means of its floor. Including setting settings to our Three.js scene helps obtain this impact by simulating real-life lighting, offering sufficient mild for the fabric to replicate correctly.

Diving into Transmission Supplies

Utilizing MeshTransmissionMaterial is simple. Nevertheless, actually understanding its detailed properties takes time and requires a deeper grasp of how transmission supplies work. On this part, we’ll break down every discipline step-by-step and discover how they contribute to the fabric’s conduct.

The samples property controls the variety of refraction samples used to simulate mild transmission. Greater values lead to smoother, extra practical refraction, whereas decrease values can create rougher refractions, generally resulting in grainy artifacts. The default worth is 10, however you possibly can regulate it to seek out one of the best steadiness to your desired impact.

Be aware that greater sampling means better computational price, resulting in a damaging efficiency affect. This occurs as a result of rising the variety of shader traces considerably raises the computational workload required to approximate the refraction impact.

Decision

The decision discipline controls the render texture decision used for refraction. If utilizing a number of transmission supplies in your scene causes efficiency points, you may need to regulate the decision. Personally, I feel this is among the most underrated fields. Reducing the decision to round 128–256 can create a pixelated impact—with out even needing a customized shader.

Transmission

The transmission discipline controls how a lot mild passes by means of the fabric. It takes a float worth between 0.0 and 1.0, the place 0 represents a completely strong materials, and 1 makes it utterly clear. As you may anticipate, setting a price between 0 and 1 creates a semi-transparent impact, permitting some mild to cross by means of.

Roughness

As a few of you might need already seen, MeshTransmissionMaterial extends THREE.MeshPhysicalMaterial, which means we even have entry to all of the fields from MeshPhysicalMaterial. I wasn’t planning to cowl this discipline, since most of you’re most likely already acquainted with how roughness works in supplies. Nevertheless, I needed to focus on one attention-grabbing element—when roughness is about to 1.0, the fabric turns into utterly invisible.

This occurs as a result of when roughness is about to 1.0, the floor turns into so tough that it scatters mild utterly. Because of this, mild can not cross by means of, eliminating reflections whereas the fabric absorbs and diffuses all incoming mild.

Colour

The cool factor in regards to the colour discipline is that it acts as a further filter layer over your materials, affecting every part inside or behind the transmission materials. Discover how the unique letters inside my torus have been both orange or mild purple—setting the colour of MeshTransmissionMaterial creates the impact of wanting by means of tinted glass.

Diving Deeper into Transmission Materials

Now, let’s dive into extra superior ideas in TransmissionMaterial. Remember that these fields affect each other, and every distinctive mixture will produce a unique visible impact.

IOR

IOR stands for Index of Refraction. Merely put, it controls how a lot mild bends (refracts) when passing by means of a fabric. Consider it like adjusting the thickness of glass—though it’s a totally completely different idea. A fabric with a decrease IOR worth leads to much less refraction, whereas the next IOR worth will increase distortion and depth, just like glass or diamonds.

For reference, listed below are some frequent IOR values:

Air: 1.0003
Water: 1.333
Diamond: 2.42

These values give us an concept of how refraction behaves at completely different ranges.

One attention-grabbing attribute of IOR is that objects with excessive values seem warped. As IOR will increase, mild bends extra, creating sturdy distortions and a magnified, warped impact.

Beneath is an instance of how the fabric appears because the IOR worth will increase:

Thickness

Together with IOR, one other discipline that contributes to the warping impact is thickness. This discipline controls how deep mild travels by means of the fabric earlier than exiting. Remember that the fields mentioned on this part affect each other.

If IOR is about to 1.0, you’ll discover that altering the thickness has no impact on the fabric’s look. This occurs as a result of thickness solely issues when mild bends inside the fabric. If there’s no refraction taking place within the first place, thickness received’t have any visible affect.

Chromatic Aberration

The chromaticAberration discipline simulates colour dispersion, the place completely different wavelengths of sunshine bend at barely completely different angles. Consider how diamonds and prisms break up mild right into a delicate rainbow impact.

This Wikipedia instance offers a very good visible reference for chromatic aberration.

This discipline ranges from 0.0 to 1.0. Decrease values lead to little to no dispersion, just like real-life glass. As the worth approaches 1.0, colour splitting turns into extra pronounced, creating stronger chromatic aberration and exaggerated rainbow edges within the refraction.

Anisotropy

The anisotropy discipline, often known as directional blur, controls how mild is stretched—typically making a streaked glass or frosted glass impact. By default, anisotropy is about to 0, which means mild is mirrored evenly in all instructions.

An attention-grabbing side of this discipline is the way it interacts with roughness. Greater roughness values trigger anisotropic reflections to grow to be extra subtle and unfold out, altering the ultimate look of the fabric.

Discover how we will obtain a frosted glass impact by combining the anisotropy and roughness fields.

Distortion, Distortion Scale, and Temporal Distortion

These three properties—distortion, distortionScale, and temporalDistortion—management how mild waves bend and shift as they cross by means of the fabric. When used successfully, they will simulate results like wavy glass, warmth distortion, water, or perhaps a jelly-like look. Let’s break them down one after the other.

The distortion discipline is the first management for warping mild, making a wavy impact. Greater values enhance the quantity of bending and distortion.

distortionScale adjusts the scale and frequency of the distortion waves—greater values lead to bigger, extra stretched distortions.

Lastly, the temporalDistortion discipline animates the distortion impact. A well-balanced mixture of those values can create a shocking refraction impact, just like water, with out even needing to make use of useFrame or requestAnimationFrame!

Animations?

When you’ve adopted together with this text, you’ll begin to see that creating fancy animations simply by tweaking completely different fields isn’t troublesome in any respect. Whereas experimenting with completely different properties utilizing Leva, I discovered it extremely satisfying to look at how IOR adjustments in actual time—to truly see the “woosh” impact with our personal eyes.

To animate this impact, I arrange two completely different phases: the pause section and the oscillation section, the place the “wooshing” occurs. The IOR values I needed to make use of ranged from a minimal of 1.07 to a most of 1.5. Utilizing some primary math, I applied this inside my useFrame perform.

Through the use of GSAP to animate IOR over time, I achieved this end result:

After I posted this on X, I acquired quite a lot of inquiries about how I managed to place the textual content completely inside my torus. The key sauce? IOR 😉

Efficiency

Efficiency for MeshTransmissionMaterial is just not low-cost in any respect, so we must be aware of how we use it. When an object has transmission enabled, Three.js performs a separate render cross of the scene for every object utilizing this materials. That is essential to accurately compute the refracted background for each transmission object.

In contrast to easy opacity—the place transparency is dealt with in a single cross—transmission supplies require a further cross to find out the refraction impact. If a number of objects in a scene use transmission, Three.js should re-render your complete scene a number of occasions—as soon as for every object. This will rapidly grow to be a efficiency bottleneck, considerably rising the variety of draw calls.

Going Additional

Mastering MeshTransmissionMaterial can result in some actually fascinating outcomes, like this:

I’ve seen a number of Three.js scenes the place builders use complicated shaders to attain comparable results. Nevertheless, when you perceive the magic behind Transmission Materials, you’ll begin recognizing all of the artistic potentialities it gives! 🙂

And that’s it! Large because of Codrops for giving me this chance to contribute to the Three.js neighborhood. In case you have any questions, be happy to achieve out to me on X.

Bear in mind, all nice artwork comes from foolish experiments. Cheers to the Three.js neighborhood! 🎮🌟

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments