UE4 Raymarcher

Simple Raymarcher running in a post process material. Just two spheres that move and scale arbitrarily with a subtraction function.

A foundation for further work!

Two intersecting spheres

For reference, here’s the Graph:

And the custom nodes…

Raymarch

// March
float march = raymarch(origin, direction, MaxSteps, SurfDist, MaxDist);
float3 normal = GetNormal(origin + direction * march);
float NdotL = dot(normal,L);
float3 hal = normalize( L-direction);
float3 ref = reflect( direction, normal );

float3 col = float3(1,1,1) * NdotL;

return float4(col,march);

RaymarchFn (contains the actual loop)

return 1;
}

float3 GetNormal(float3 p)
{
    float dist = GetDist(p);
    float2 e = float2(0.01, .0);
    float3 normal = dist - float3(
        GetDist(p-e.xyy),
        GetDist(p-e.yxy),
        GetDist(p-e.yyx)
        );
        
    return normalize(normal);
}
float raymarch(float3 origin, float3 dir, int MAX_STEPS, float SURFACE_DISTANCE, float MAX_DISTANCE )
{
    float distOrigin = 0.0;
    for ( int i = 0; i < MAX_STEPS; i++)
    {
        float3 p = origin + distOrigin * dir;
        float distSurface = GetDist(p);
        distOrigin += distSurface;
        if(distSurface < SURFACE_DISTANCE || distOrigin > MAX_DISTANCE)
        {
            break;
        }
    }
    return distOrigin;

And the two Distance Fns:

return 1;
}

float GetDist(float3 p)
{
    return max(-GetDist1(p) , length(p.xyz - float3(
	// X
	( (((sin(View.GameTime)+1.0) / 2.0) + 1)* 20 ),
	// Y
	( (((cos(View.GameTime)+1.0) / 2.0) + 1)* 60 ),
	// Z
	300
	// 
	)) -
	// Radius
	( (((sin(View.GameTime)+1.0) / 2.0) + 1)* 100 ));
return 1;
}

float GetDist1(float3 p)
{
    return length(p.xyz - float3(
	// X
	( (((cos(View.GameTime * 10)+1.0) / 2.0) + 1)* 10 ),
	// Y
	( (((sin(View.GameTime * 3.33)+1.0) / 2.0) + 1)* 10 ),
	// Z
	( (((sin(View.GameTime * .5)+1.0) / 2.0) + 20)* 10 ) + 100
	// 
	)) -
	// Radius
	( (((cos(View.GameTime / 2)+1.0) / 2.0) + 1)* 50 );

Currently sphere lives at 0,0,0.

Leave a Reply

Your email address will not be published. Required fields are marked *