Published: April 3, 2022
28
245
1.7k

✨Making realtime volumetric effects with raymarching A thread 🧵 #blender #b3d #Blender3D

So what exatly is raymarching, and why should I care? Essentially, raymarching is a mind-bogglingly simple method for rendering 3D surfaces based off of a greyscale texture. (1/?)

To get a better idea of what's going on here, lets look at an example in 2D. Every "step", the point checks how far it is from the nearest surface, and moves in a given direction based off that distance. The more steps, the closer the point gets to the actual surface. (2/?)

The most complex part of this system is figuring out how close the point is to the nearest surface, so lets start there. In order to do this, we need what's called an SDF, or Signed Distance Function. (3/?)

This may sound complex, but a nice example of a basic SDF is our good ol'friend the Voronoi texture. For every pixel, it displays how far it is from the nearest of many randomly positioned points. (4/?)

Image in tweet by Nugget 🏳️‍🌈

With this in mind, lets set up our SDF nodegroup. All it needs is a position input and distance output. For now, I'll use a noise texture with some distortion. (5/?)

Image in tweet by Nugget 🏳️‍🌈
Image in tweet by Nugget 🏳️‍🌈

Before we go further, there's a small mistake that we need to resolve. Returning to the voronoi example, if we display pixels greater than .5, scaling up the texture causes the circles to change size too. (6/?) incorrect correct

Luckily this can be easily resolved by dividing by the scale after the noise texture like so: (7/?)

Image in tweet by Nugget 🏳️‍🌈

With our SDF node complete, we can move onto our main function. For every step, it needs to: 1. Detect the distance to the closest surface 2. Move by that distance away from the camera (8/?)

Before we figure out how to do this, we need 2 variables: Ray origin: the current position of the point we are moving around. Ray direction: the direction that each point moves in every step. These can both be achieved with the geometry node like so: (9/?)

Image in tweet by Nugget 🏳️‍🌈

Next up, we can plug our ray origin into the SDF node to see how far away from the surface we are. After that, we scale our ray direction by that distance and add it to the old ray origin to create the new ray origin. We've just marched forewards! (10/?)

Image in tweet by Nugget 🏳️‍🌈

Believe it or not, we've just made the entire raymarching function! We can cram it all into a nodegroup like so: (11/?)

Image in tweet by Nugget 🏳️‍🌈
Image in tweet by Nugget 🏳️‍🌈

Now for the fun part. Since our inputs and outputs are identical, we can just repeat the nodegroup over and over to march the ray forewards! I've found around 8 marches works well for what we need. (12/?)

Image in tweet by Nugget 🏳️‍🌈

If we pan around the scene while previewing the final ray origin, its clear we're getting closer to the desired effect. (13/?)

It's a bit hard to visualize, though, so lets change that. If we get the distance from the starting ray origin to the final ray origin and plug it into the function 1/(10x+1), we get a much nicer gradient. (14/?)

Image in tweet by Nugget 🏳️‍🌈

After a bit of fiddling with the SDF and the function we just made, I've found that this setup seems to achieve the nicest results: (15/?)

Image in tweet by Nugget 🏳️‍🌈

You may have noticed that there are some pretty stars in the original render. These can be achieved pretty easily by adding a Voronoi texture, dividing it by its scale, and combining it with the noise texture using a minimum operation like so: (16/?)

Image in tweet by Nugget 🏳️‍🌈

As for the coloring, just slap a colorramp on the end and play with it to your liking. You can achieve a ton of variation with different palettes! (17/?)

The great thing about this method is that it works perfectly on any object, which means... ✨Magic Suzanne✨

Image in tweet by Nugget 🏳️‍🌈

Well, that's about it for the thread! Raymarching is a super interesting topic to get into, and I just barely scratched the surface. As usual, the project file is available for free on my Gumroad: https://nuggetblends.gumroad.c...

Quick correction: Raymarching is a bit of a generalization, as it refers to any rendering method that involves incrementally moving a ray forwards. The method on display here is actually called Sphere Tracing!

@HotdogNugget This IS pretty fun! You can do interior mapping like this too, right?

@Sad_Lux Technically, though for that you can analytically calculate the surface without a bunch of steps.

@HotdogNugget very clear explanation!

@HotdogNugget Absolutely loving this! AND even more so, I noticed the grandiose glitter on the fabric underneath O,O did you by any chance ever.. showcase how you achieved such glitter on fabric? ♥ ♥

@HotdogNugget My experiments with shader on sphere was kinda the same…

Image in tweet by Nugget 🏳️‍🌈
Image in tweet by Nugget 🏳️‍🌈
Image in tweet by Nugget 🏳️‍🌈

@HotdogNugget Awesome thanks 👌

@HotdogNugget Great stuff man!!! Yo, are you sure you’re 15 😊

@HotdogNugget This is so beautiful 🥰🥰 Thank you for the clear explanation!

@HotdogNugget great thread and graphics explaining ✨

@HotdogNugget This is the way.

@HotdogNugget Pretty. Slow animated volumetric would be also fitting. 😺

@HotdogNugget Holy moly this is stunning! Thank you for sharing.

@HotdogNugget this is so cool. thanks for taking the time to break it down!

@HotdogNugget when i was a kid i had a crystal ball toy that was squishy and stuff swirlled in it if u turned it on and it was these colors 🥹i love this

@HotdogNugget Love to see a crystal ball on the timeline 🔮

@HotdogNugget Looks cool

Share this thread

Read on Twitter

View original thread

Navigate thread

1/37