Showing posts with label graphics math. Show all posts
Showing posts with label graphics math. Show all posts

Sunday, March 1, 2020

Ray Marching


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.




Monday, February 24, 2020

Representation of basis vector in matrix form.

When you read math books related with game programming, you should know representation of basis vector is depends on representation of vector form.

if the book uses row vector then vector multiplication with matrix is going to be
vM form which is above form in the below image.



if the book uses column vector then vector multiplication with matrix is going to be Mv form which is below form in the above image.

Representation of basis vector i, j, k are also depends on the order of vector representation like the image I shown.




Wednesday, November 27, 2019

Applying a transform into Normal vector.


Applying a transform into normal vector
When we apply a transform into normal vector, we can't use N * M. Because we don't want scale effect like below.


We represent basic transform like below. 




We want to apply R(rotation) only not S(scale). Our expected transform needs to be like below.



If we take inverse of rotation matrix and then take transpose, result matrix will be same as original rotation matrix. so we can represent above formula like below.


As we know, scale transform is a diagonal matrix. If we take a transpose of it, result will be same as original scale transform.


Our expected matrix can be like this.

In the transpose matrix, there is a property.


matrix can be represented like below.


In the inverse matrix, there is a property.


matrix can be represented like below.


R1 * S * R2 is a transform matrix M. As a result, we can have a matrix down below.


The transform matrix what we want is Mwant and it is a inverse of original matrix M and take transpose.

Task in UnrealEngine

 https://www.youtube.com/watch?v=1lBadANnJaw