
Raymarching Material 101: Mastering An Advanced three.js Technique
Abstract
Ever found yourself on ShaderToy, awestruck by the complex visuals created with shaders and wondered how to incorporate such shaders into your web-based three.js projects? You’re about to embark on a journey that explores the powerful technique of raymarching, a cornerstone of many stunning 3D web experiences. This guide will navigate you through integrating both ShaderToy shaders and your own creations into the three.js framework, enriching your projects with unparalleled visual depth.
Disclaimer: While I navigate the realms of three.js and raymarching with keen interest, I consider myself at the beginning of this journey, mastering enough to integrate raymarching into three.js effectively. For those looking to dive deeper into raymarching’s theoretical underpinnings, I recommend resources such as TheArtOfCode’s YouTube Channel or Michael Walczyk’s blog . Our focus here is squarely on the practical integration of these techniques within the vibrant ecosystem of three.js.
I welcome your feedback, questions, and insights. If you’ve encountered any uncertainties, spotted typos, or simply wish to share your thoughts, reaching out on Twitter is greatly appreciated. Your interaction fuels this shared exploration of 3D web development and creative coding.
Thank you for joining this explorative venture into enhancing three.js projects with the dynamic capabilities of raymarching and shaders. ✨
Process introduction 📐
Reminder: What is raymarching in first place?
It’s a technique that involves view rays extrapolated from the camera, along which we are going to march forward, to see if there is intersections with specific surfaces. These specific surfaces are defined by SDF (a.k.a. Signed distance functions). A ray is composed of two properties: An origin (the starting point) and a direction. The raymarching algorithm will take in input these two components.
If all of this is really new to you, you may have trouble to follow along, because I don’t go in depth about the raymarching theory. Go back to the abstract and follow the links I put for you !
How to get the rays properties?
Let’s put aside how we get the rays for a moment, and remember about the rendering pipeline.
First, our vertex shader will run on each vertex of our primitive. Then, down the rendering pipeline, we get a lot of fragments. On each fragment, the fragment shader will be runned, with in inputs some interpolated values coming from the vertex shader.

In each fragment, we will run the raymarcher. So basically, in each fragment, we will get a different ray, that we will follow along. Then, we will test intersections inside the raymarching loop, and determine the pixel color that we need to render in this direction.
The plan to make our raymarcher work in the context of three.js is to calculate each ray in the vertex shader from the real three.js camera. Then, pass down to the fragment shader through varying the ray origin and the ray direction.
**Reminder: The fragment shader will interpolate the values passed in varying. For the ray origin, it is the camera position for everyone, so it will be a constant value. For the ray direction, it will be interpolated from the calculation done in the vertex shader. Each fragment will have a specific ray direction. It is exactly what we need to make the raymarcher work in our context ! **
A little drawing could help no ?

Each arrow you see on the picture is a ray. It is basically what will happen in the vertex shader. Each arrow will be the result of the vertex shader calculation. Then each arrow will be interpolated before feeding the fragment shader.
The formulas for the ray is very simple:
The ray origin is the camera world position or the vertex local position depending of the raymarching effect you want. Taking the local vertex position will ensure that you have the same effect even if you move the sphere around in the scene. Taking the camera world position will make the effect inside different depending on the position of the object in the world space.
There is no right or wrong, just matter of the effect you want to be performed in the sphere.
The ray direction is given by rayDirection = normalize(vertexWorldPosition - cameraWorldPosition). Just make sure that you have every coordinates in the same space.
We normalize the vector, to have a unit length.
Implementation ⚙️
Beginning of the implementation
First, we need to create a shader material. We will pass the vertex and fragment shader as strings.
Don’t forget to update the uTime uniform in the render loop.
First we calculate the world position of the vertex by multiplying the model matrix by the vertex coordinates.
Then we prepare the varyings that will be interpolated before the fragment shader.
vDirection is the ray direction from the camera to the vertex world position, as discussed in the previous section.
vPosition is the ray origin. It is the position of the vertex in local space, so the effect stays local to the sphere rather than depending on the object or camera position in world space.
Basically, it is what we are trying to build.

Raymarching algorithm
We will use TheArtOfCode raymarcher for it. Like I said, if you are not familiar with the code of the raymarching algorithm, check out his videos. Here, a part of the fragment shader.
Basically, we calculate a distance in a direction from an origin point. We march forward until we hit something in our abstract space or pass the maximum distance. We save this distance d.
We can then calculate the position we marched to with rayOrigin + rayDirection * d (we marched a distance d along the ray direction, starting from rayOrigin).
We then calculate the light at the current position, which gives us the color.
We do this for every ray generated by every fragment shader running. From there, we can determine every fragment color, which determine every pixel color for the mesh.
What to take off from this blog post ?
As we wrap up our exploration of raymarching and its application in three.js, I hope you find yourself equipped with new insights and inspired to push the boundaries of 3D web development further. This journey into the intricate world of three.js and raymarching algorithms is just the beginning of what you can achieve in the realm of digital creativity.
You can go on ShaderToy to experiment on raymarching algorithm. I did the experimentation with a cool sea shader I found here:
All you need to do is seek for the raymarching algorithm. Replace the main function by our main, with the calculation of the ray origin and ray direction relative to the three.js camera. And you can get this type of cool effect on your three.js sphere !
This is the initial shader that we will modify to make it work in three.js.
The naming convention are different, we don’t need the iMouse, the camera ray is impacted by our OrbitControls.
Go to the next step to get the full fragment shader that can be used in three.js.
Be sure to make these changes to bind the convention of three.js to the shadertoy shader.
- Replace ShaderToy’s
iTimewithuTime. - Remove the
iMousedependency becauseOrbitControlsmoves the camera. - Replace
iResolutionwithuResolution. - Rename
mainImagetomain. - Write the final color to
gl_FragColor.
I have implemented it in a react three fiber project, you can see the result below:
The shader would be the same in three.js if you are a vanilla user or if you use any other type of three.js framework on top. Once you understand the translation steps, you can adapt this technique to any 3D context that you are working on.
Mastering Raymarching with Three.js
The journey through ShaderToy and the integration of raymarching algorithms into your three.js projects opens up a world of possibilities. The convergence of these technologies enables you to create more dynamic, interactive scenes and objects, enriching the user experience on the web. Embracing these techniques not only boosts your skillset but also places you at the forefront of innovative web development.
The path to mastery is paved with trial, error, and continuous learning. As you experiment and refine your approach, remember that every challenge is an opportunity to grow. The field of 3D web development is ever-evolving, and your contributions—no matter how small they might seem—are valuable steps forward in this exciting journey.
Looking Forward
I encourage you to view this post as a stepping stone towards achieving mastery in 3D web development with three.js. The principles of raymarching and their application within the three.js framework have the potential to transform your projects into immersive experiences that captivate and engage users.
Keep exploring, experimenting, and challenging yourself. The more you learn and apply, the more proficient you’ll become. And as you progress, share your discoveries and creations with the community. Together, we can push the boundaries of what’s possible in 3D web development. If you liked this article, you can also check out my other articles on the depth buffer and the indexed geometry in three.js and react three fiber.
Thank you for spending this time with me. I look forward to seeing where your creativity and technical prowess will take you next in the world of three.js and beyond.