Fractal Ray Marching in Blender (Geometry Nodes)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to today's tutorial where we will be Ray marching fractals in Geometry nodes there will be a link to the blend file in the description so check that out if you'd like and let's get into it open blender select the cube press contr I to select everything else X to delete create a new window set it as geometry nodes and create a new node network but hold on what even is Ray marching Ray marching is a technique for rendering fractals and other distance-based functions by marching along a series of rays that radiate from the position of a virtual camera through a set of points representing each pixel in the output image through a series of steps we move each point along its corresponding Ray Direction until it reaches the quote unquote surface of the distance function we are rendering this surface is defined as each point in space where the distance function returns zero we can improve this process by using the current distance of each point to the surface of the function to drive how far along its Ray Direction the point moves each step finding this value just involves feeding the current Point position into the distance function and this way we never overshoot the closest surface point in any given Ray Direction and can Converge on the final image faster traditionally Ram arching is done in fragment shaders operating directly on pixels but that won't stop us from doing it in blender using geometry instead of pixels regardless you should definitely check out Shader toy.com which is an incredible Learning Resource for Ray marching shaders and computer Graphics in general I know Shader programming can be intimidating at first but over time you will get the hang of it and it will open up an entire world of possibilities now without further Ado let's for real get into it first we'll create the geometry that will represent the pixels in our output image we'll do this with a grid mesh primitive node and then pull in a value node to drive the size of the grid uh in both the X and Y directions because we want it to be a square and then also an integer input node to drive the resolution of our grid which will correspond to the number of pixels in our output image I've got that set to zero for now but we'll change that in just a second next we'll lay down two transform geometry nodes and one object info node we're going to create a camera object that we're going to use with the object info node so the second transform geometry node essentially moves the grid object so that it has the same translation and rotation of the camera so that it always moves with the camera and the first transform geometry node will just translate the grid object in the Z Direction before it is moved to copy the transformations of the camera and that's just so it's offset from the camera position just a little bit to actually see the grid and the Transformations that we've just applied we're going to have to set the resolution to something higher than zero so we'll do that and here we'll offset the grid geometry a little bit from the actual origin of the camera by changing the Z translation component of the first transform geometry node here let's just set up the camera to move with the view and now you can kind of see how the grid moves with the camera as you transform it around the scene next let's hop into the output settings and set the resolution to 1024x 1024 and then use the Z offset of the grid from the camera to fit it nicely within the camera frame here we play with the resolution just to show what that looks like and you can also use the size of the grid to fit it in the frame that or the offset are both perfectly valid you can do a lot of different things a lot of different ways the next thing that we're going to want to do is store the ray direction as an attribute for each of the individual points so use that store Nam attribute node set to vector and point and then what we're going to do is we're going to get the difference Vector between the position of the current point and the position of the camera um and then we're going to normalize that Vector so that the length of that Vector is one and we can scale it to whatever we want later um and then we're just going to feed that into the stor named attribute node so again it's the position of the current Point minus the position position of the camera object you normalize that store that in the ray Direction attribute for Ray marching will put down a simulation Loop and perform one Ray marching step inside this is done by using the set position node to offset the position of each grid Point by its Ray Direction attributes scaled according to the distance function the distance function itself can be any function that takes a 3D Vector as an input and returns a single value also known as a scaler as an output one of the simplest distance functions describes a sphere this works by taking the distance of the position from a point in space because we want the function to return zero on the surface of the sphere rather than the center of it we'll also subtract a bit from the output of the distance node if we position the camera so that it points at the sphere location and press play we can see how the grid projects onto the surface of the sphere we can make this distance function slightly more interesting by wrapping the position input between 0.5 0.5 0.5 and 0.5 0.5 0.5 so that the sphere repeats in Space the fractal distance function we'll use today has a couple more steps like most fractal functions it requires an iterative Loop so let's put down a repeat Zone similar to before we'll lay down a wrap node to create a repeating or folding effect this time we'll wrap between 1-11 and 111 this fractal works by iteratively applying a repetition and a spherical inversion these next couple of nodes perform the inversion we'll get the squared magnitude of the wrapped position Vector through taking its dot product with itself then we'll divide the radius of the inversion sphere which we are setting to be 1.333 by the squared magnitude then we'll scale both the wrapped position vector and a scale parameter by the output it can be difficult to get a good intuition about what what's going on here but the gist is that we are starting with a bunch of points in space and iteratively applying these repetitions and spherical inversion transforms to each point a certain number of times after these iterations we check to see where the points end up in relation to some reference point or reference function in this case the value of the Z position for each point this Z position value acts as the distance function output and if it's close to zero then the original point in space is close to the surface of the fractal I learned about this fractal algorithm in a post on Shader toy by Inigo quillas you can find a link in the description so check that out to learn more here we separate the position Vector after the transformation iterations take the absolute value of the Z position multiply this by 0.25 and then divide by the iterated scale parameter these operations tune the points in space that correspond to the fra surface we can use the output of this distance function just like the simpler one we made before and voila a ray arched fractal one last tip for navigation is to replace the simulation loop with a repeat Zone and set the resolution pretty low so you can interactively move the camera around and find an interesting angle before cranking up the resolution for the final render also we can use a compare node to only perform Ray marching on grid points whose distance to the surface of the fractal is greater than some small number in this case 0.00001 this prevents artifacts in the mesh that can occur if you leave the simulation running for a large number of frames and that just about covers it I hope you enjoyed this tutorial I hope you learned something and above all I hope you had fun until next time bye
Info
Channel: Seanterelle
Views: 14,055
Rating: undefined out of 5
Keywords:
Id: eIZ97sP6xAg
Channel Id: undefined
Length: 8min 24sec (504 seconds)
Published: Sun Nov 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.