Ray-marching for Dummies!! Introduction! Godot 4!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you might have heard the term Ray tracing today we will be talking about its Big Brother Ray Maring Ray marching is a very popular and a powerful rendering technique often used to create 3D shaders hello everyone and welcome back to the channel my name is D JC gohill and today we will create a simple rayar Shader to render a 3D sphere in a box mesh for the tutorial I will be using GTO 4.2 and this video will be the basic introduction about re marching so if it sounds interesting to you stick around and we will dive into the topic right after the intro so the first thing is what is Ray marging imagine that we have a camera and it has a position in 3D space we will call it Ray origin then we will have a direction in which our camera is looking at we will call it Ray Direction in our scene if that Ray intersects with any surface we will get that point and color that pixel obviously we will do that for every pixel at which our camera is looking at now we cannot magically get the intersection point so in order to get that intersection point we will first ask a simple question that what is the closest distance from our Ray origin to the surface then we will get a point which is is the closest one to our Ray origin we will count the distance to that point from our Ray origin then we will move forward that much distance in our right direction now we will get this point again we will ask the question that what is the closest point from the new point to our surface then once more we will March that much distance in our right direction we will continue the process until the distance becomes so small that we count that as a hit this is the ray marching in a nutshell and it is called Ray marching because we are literally marching along the ray sometimes it is also called sphere tracing all right simple enough but now the question is how do we get the closest distance to our surface well to calculate that we will create something called sdfs which means sign distance functions now sdfs can be different for different surfac is here we want to calculate the closest point to our Sphere for that we will calculate the distance from our Ray origin to the sphere position then we will simply subtract the radius of our sphere to get the closest point then we will get a new point along our a direction then from the new Point rinse and repeat until we hit something okay that is all the theory for this video Let's jump into gdau here let's first create a cube so add node mesh instance 3D then select box mesh okay we have our Cube now let's create a Shader for our [Music] Cube I will call it Ray marching let's also create a material for our Cube assign our Shader to our material and apply our material to our cube in our Shader first I will set the render mode to unshaded I don't want it to interact with light and let me clean this up a bit in our fragment processor let's make our Cube black so let's create a vector 3 color and assign it to our albo now to write the ray marcher we need Ray origin and Ray Direction so vory Ray origin equals and let me hardcode it that I want my camera at 0 0 and minus 2 and for Ray direction we want to March forward for every pixel so let's use our uv. X UV doy and one and we want to normalize that hang on let's just first visualize our UV so color. RG equals UV and you can see that our UVS are kind of messed [Music] up that is because in GD the default box mesh is UV unwrap to show different texture for every faces so to fix our UV issue we need to multiply our UV with v 2 of three and two how I came up with these numbers well it's in the docks then we take the fraction part of the entire thing and I want the origin of our UV in the center of each phas so I will subtract .5 let's visualize is our calculated UV and boom we have nice accurate UVS now in the ray Direction Let's also use our calculated UVS and let me just visualize our R direction to see if we calculated that correctly so and Y we did it correctly all right now that we have origin and Ray Direction let's write our Ray marcher which will return a distance to the [Music] surface and it will take Ray origin and Ray Direction as input parameters here we first create a float variable to keep the track of distance we have marched from the origin let's call it distance from origin which will initially be zero now we need one more variable which holds the distance to the surface then we will set up our a marging loop for and i = z i less than some maximum number of steps because we don't want to Marge infinitely we have to set a limit and I ++ let's first Define the max step so hash Define Max steps 15 then here we want to calculate a point on our Ray Direction so back three point equals Ray origin plus Ray Direction into distance from origin this will be the purple point along our Ray direction that I showed you earlier then from this point we will ask the question what is the closest point to our sphere so distance to surface equals get distance to surface from our point and let me just Define get distance to surface first which will return the closest distance from our Point P so float I will just Define some distance d zero and return it for now we will get back here in a minute all right in our Ray marging Loop now that we have the closest distance we will add that to our distance from origin meaning we have marched forward on our Ray now let's say that we have marched far enough from our origin without hitting anything then we need to acknowledge that okay we have marged far enough without hitting anything and there is no point in marching forward so if distance from origin is greater than some max distance or our closest point is smaller than some threshold meaning it is so close to the surface that we count that as a hit so or distance to surface less than some surface distance then we want to break out from the loop and let me just Define Max dist and surface [Music] [Music] dist also you should tweak these fields to optimize our Shader okay now outside of a marging loop let's return our closest distance so return distance to surface now back to our get distance to surface function we want to get the closest point of a sphere from this point P so it will be just the length of our Point minus the radius of the sphere so let me Define the radius let's say 3 and actually I can directly return this all right now in our fragment processor let's get the distance to surface so flow T equals Ray March Ray origin and Ray Direction then we will check if our distance is smaller than surface distance means we hit something then let's just turn our pixel fully red so color. R = 1 else means we haven't hit [Music] anything if that's the case we don't want to draw the pixel so just discard and we have a circle on all six faces of our Cube and you might say that well that's a fancy and convoluted way to get the circle on the middle of each face but trust me this is a fully 3D sphere only reason it's looking like this is because we have hardcoded our Ray origin and Ray direction we actually want to feed the position and direction of our camera or the position from which we are currently looking at our Cube from the viewport luckily we can get the camera position with camera position Bol it Returns the camera position in w space and the redirection will be the vertex position minus camera position now it looks like we have messed something up but if we move the cube to the origin of our V and if we look at around then we can sometimes see our sphere and the reason is that our camera position is in wall space but our vertex position is in view space both of them need to be in the same coordinate space so let's just convert the vertex position in wall space we will get inverse view Matrix which will convert the vector from view space to wall space let's multiply it with our vertex and since our Matrix is 4x4 we need to convert our vertex to V four as well and for w component pass one now it will also return back four but we simply need the XY Z components and of course we want to normalize the entire thing so that our R Direction Vector will have the length of 1 unit and now we have the actual sphere but if we move our [Music] Cube our sphere stays at the origin of our VA the reason is our re origin and redirection are in World space now this could be useful if you want to create some sort of portal but I want to move our sphere along with our mesh so we we need to convert Ray origin and Ray Direction in local space or object space to convert camera position to local space we will take model Matrix which converts Vector from local to World space we want opposite of that so we will do the inverse of that then we will multiply it with our camera CA position camera position B this Vector 3 so we need to convert it to Vector 4 then we will take only X Y and Z component of the entire thing now here we have converted vertex position in World space and instead of adding one more operation there's a better way to do this in our vertex process proc we can get vertex position in local space so let's just pass that position in our fragment processor we can do that using varing let me declare a varing variable vory V in our vertex processor we will grab the vertex position in local space lastly in our fragment processor instead of all of this simply use our part variable and now we can move around our mesh and our sphere will move along with it pretty cool now instead of red let's assign our vertices XY components to see that it is actually a 3D sphere so color. RG equals word. XY [Music] now we can move the sphere by offsetting our point and get distance to surface function so here let's say p minus some [Music] position and we have moved the sphere a little [Music] bit we can draw another sphere as well let's say this is sphere a let's define some different radius as [Music] well move over sphere B in opposite direction now to get the minimum distance we will use Min [Music] function and we will return that and we have two spheres I can also make one sphere or bits around the second one let's put our sphere a at the center again so for sphere B let's put it away4 units away from Center in XZ direction we will multiply X component with s of time and Z component with cosine of time and we have sphere B orbiting around sphere a let me just reduce the radius of sere a a [Music] bit so you can see that Ray marging is a very powerful tool for 3D shaders we can do a lot with this but this is just an introduction I will give a more practical implementation in future video you can also find the Shader with various primitive shapes on my patreon page linked in the description below also I know a lot of you watch my channel for visual shaders and Shader graphs and you might wondering how to implement this in visual shaders well I am currently writing the notes for a marching in my add-on Shader lips so soon we can replicate this we have shers as well and that's pretty much the video If you find the video helpful hit the like button share it with your friends subscribe for more videos like this if you have any questions post them in the comments wish list Cosmic roads on Steam that's it from me and I will see you guys in the next [Music] one
Info
Channel: Digvijaysinh Gohil
Views: 713
Rating: undefined out of 5
Keywords: Tutorial, How to, Godot, shaders, Ray marching, Ray marcher, Rendering technique, 3D shaders, Introduction
Id: 68G3V5Yr8FY
Channel Id: undefined
Length: 20min 31sec (1231 seconds)
Published: Sun Mar 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.