Ray Marching
2020-02-24
Kiyoung
Moon
Basics
Ray Marching is a kind of ray tracing
algorithm. I couldn’t see any case where it is used without SDF(Signed Distance
Function).
SDF(Signed Distance Function) is a distance
function which means we represent shapes with the function instead of vertices
data. For instance, we have sphere, which has radius ‘r’.
If we define ‘e’ is a position of camera, we
can define sphere like below.
If the function f(x,y,z) is positive then camera
‘e’ is outside of the sphere.
If the function f(x,y,z) is 0 then camera ‘e’
is on the surface of the sphere.
If the function f(x,y,z) is negative then
camera ‘e’ is inside of the sphere.
We can use this value in the Ray Marching
algorithm.
Ray Marching
Like I said, Ray Marching works with SDF. It
will not test (collision test) with the mesh data which is vertices.
Every shapes in the Ray Marching uses SDF
shapes. You can see some of primitive SDF shapes down below.
There are many primitive SDF shapes that
people already discovered. You can visit iq’s website. (https://www.iquilezles.org/index.html)
What
is ray marching?
First, we need a Ray. We shoot the Ray into
the screen and then checks whether there are any shapes that is collide or not.
Instead of using traditional collision detection logic, we can use SDF. Assume
we have E which is the position of the Ray.
The start position of Ray is E. and we can
check whether it is collided with sphere or not. If not then we can go forward.
How much? Can we go 0.001 more? 0.01 More?
Of course, we can go 0.00000001, which is small
step. It is working. Do we have any problem here? Yes. It is slow!
Now we can use SDF and can save our time. As
you can see, there are three spheres and we can get the distances using this
equation.
As use can see, red line is the shortest
distance.
‘r’ is the radius of sphere 3(see the
number inside of sphere). If we take ‘D – r’, this is the safe distance that Ray
Marching algorithm can use for their step. What is the means of safe distance?
Like I shown that we could take small step
which is 0.0001 for Ray but it is too slow and it is useless to check whether
collision happen or not. If we take safe distance then we can move Ray quickly.
After we use safe distance for the Ray’s
step then next Ray position is going to be Blue point on the Ray’s direction
like below image.
Ray Marching algorithm keep moving forward
until it reaches sphere or end of maximum ray distance. If Ray reaches to the
end of maximum ray distance then it means there is no collision happen so pixel
color will be black.
If we found collision then we can use this
color of sphere.
float4
CalculateScene(float3 eye)
{
float globalDst =
maxDst;
float3 colour = float3(0,1,0);
// iterate all the shapes to check the distance.
for (int i = 0; i
< numShapes; ++i)
{
Shape
shape = shapes[i];
float distance =
GetShapeDistance(shape, eye);
// closer
if (distance
< globalDst)
{
colour
= shape.colour;
globalDst
= distance;
}
}
return float4(colour,
globalDst); // w is the distance
}
|
while (rayDst <
maxDst) {
marchSteps++;
float4 sceneInfo =
CalculateScene(ray.origin);
float dst =
sceneInfo.w;
if (dst <=
epsilon) {
Result[id.xy]
= float4(sceneInfo.xyz, 1);
break;
}
ray.origin
+= ray.direction * dst;
rayDst +=
dst;
}
|
This is the result image.
No comments:
Post a Comment