Home Unreal Engine

Ray marching - Chapter 1 - The very basics and all about the clipping

grand marshal polycounter
Offline / Send Message
Obscura grand marshal polycounter
Ray marching has become a very popular technique these days among real time artists and developers, due to the crazily increasing hardware speed. It was widely used in the past too, but mostly in offline applications because of its computation requirements. But luckily now we can have a glimple in our real time renderers too. There are already some examples for in game, or nearly real time applications. Its a versatile technique for rendering volumetrics, and procedural solid surfaces, fractals, demoscenes, and other stuff. Advanced applications of the technique  can lead to very convincing results of the previously mentioned topics. Needless to mention that I'm very far from perfecting the technique, but I'm researching it for quite a while now thanks to my job and my interest. I've been also making numerous posts regarding the topic recently, showcasing what I've learned so far. I also got some messages and posts asking for learning resources, so I decided to make a series of tutorials about this topic. This is the first one, discussing the very basics. Its important to mention that the entire tutorial series requires at least basic understanding of hlsl or glsl, intermediate understanding of mathematics, and basic understanding of Unreal's  shading pipeline. The entire tutorial series will be written in Unreal specific hlsl syntax, but you can translate it to other shading languages too if you've met the requirements. In the later episodes we will be discussing various shading techniques too, but first lets start at the very basics. I hope that the first chapter isn't too "dumb" but I wanted to target those people too, who are just starting and has very little undstanding of the requirements, and wants to be motivated to learn, so a bit later they can do it themselves. 

For inspiration, I'm going to leave some references here about my stuff, and after that, some pro stuff.

My learning  experimentations, with little to no explanation but some example images and videos:

https://polycount.com/discussion/157279/the-wonders-of-technical-art-unreal-engine#first  - My Unreal related sketchbook. Most of the ray march stuff is on the last page.

https://www.youtube.com/user/KristofLovas/videos?view_as=subscriber  - My yourube with some example videos.

https://polycount.com/discussion/207928/the-basics-of-creating-pseudo-volume-textures-in-substance-designer-tutorial#latest  - About generating input data for volumetric rendering.


Pro references:

http://www.iquilezles.org/www/index.htm  - My main resource when it comes to ray marching, also the most informative resource available on the internet.

https://www.shadertoy.com/  - A great site for  shader eyecandy, and great resource for ray marching.

http://killzone.dl.playstation.net/killzone/horizonzerodawn/presentations/Siggraph15_Schneider_Real-Time_Volumetric_Cloudscapes_of_Horizon_Zero_Dawn.pdf  - An actual example of full implementation in a AAA game.

https://shaderbits.com/blog/creating-volumetric-ray-marcher  - Intermediate and advanced introduction to the technique by Ryan brucks.

There are other examples too, you can probably find them yourself too if you are curious enough.

Alright, lets get started.

Like I said this first part will be simplified to a very basic level to easily understand the concept, and also to key up newbies.
First of all we need to understand how 3d data can be treated in a way that is optimal for simple implementation. In this explanation we mainly focus on 2 techniques. Pre generated pseudo 3d textures and procedural shapes. So lets visualize a 3d coordinate system projected on the surface of a cube:

Here, we can see the 3 axes, and the value ranges. Lets visualize each component individually:
x:

y:

z:


...


Now lets take a look at how we can utilize this to map 3d data on it :


This last image may not be so obvious to understand at first, but here is a link from Ryan again to learn more:
https://shaderbits.com/blog/authoring-pseudo-volume-textures

^This should do it. As you can see, this is an example of mapping pseudo 3d textures using our 3d coordinates.

Ok, lets get to the interesting part now. So how do we get a volumetric render out of this, and what does ray "marching" actually covers. In short, its a kind of ray tracing that uses an iterative method to slowly accumulate the result.

Here is an example image of mapping some 3d data inside our 3d coordinates:


For simplicity, we will still use a box mesh to achieve 3d coordinates easily. You can see that there is a sphere inside our mesh. Now imagine that the box has the previously mentioned 0-1 range xyz coordinates. Also remember that we use a "3d" texture so it has some resolution. So imagine a grid dividing the cube into pixels, or to better express, voxels. Its a volume texture with 3d resolution. You can tell that this data wouldn't be visible by default, as its fully inside the box, but its there. This is where ray marching comes in, and also the part of the title where it says "clipping":


If we plug our 3d coordinates into some 3d noise, we can see that the 3d noise only appears on the surface of the cube. Only those points will be visualized which are clipping the surface. If we would put some offset on the coordinates before feeding it into the noise, we could move the noise, and get a "slice" of it.

Lets put this into an actual ray marching example:


Ray marching is actually very simple. Take a look at image above....


No words needed. "Clipping" is also explained in the last 2 images. I like to call it like that, because in regular cases, you will basically pull the data trough the surface of the mesh to accumulate your render within a single frame. The marching direction will depend on what do you want to do, but a basic opacity marcher will do this towards the camera. 

Maybe its a bit disappointing, but this first part ends here, and we won't do any actual ray marching, because I think even this needs some digesting. The link to the much shorter second chapter will be on the end of this thread. If you have any questions, don't understand something, you found an error in the writeup, or something along these lines, please let me know. But I most likely wont reply to "I have a normalmap error" level of messages, please note that.

Chapter 2:
https://polycount.com/discussion/208922/ray-marching-chapter-2-the-first-not-fancy-ray-marcher#latest
Sign In or Register to comment.