Raymarch – Terrain

At each step we calculate if the heightmap altitude > current ray cordinate. If its true, then we display the pixel with uvw coordinate (the box uv with y component).

Setup:
MeshInstance -> CuveMesh -> Shadermaterial

Set box mesh size, ex. uniform vec3 box_size = vec3(1.0, 0.025, 1.0);
WARNING: Subdivide cube, ex: 8

Shader code
shader_type spatial;
render_mode unshaded, cull_front;
uniform vec3 box_size = vec3(1.0, 0.025, 1.0);
uniform sampler2D heightmap;
uniform float step_size = 0.005;
uniform int max_step = 2048;
varying vec3 v_ray_dir;
varying vec3 cam_world;

void vertex() {
    vec3 world_vert = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
    cam_world = (inverse(VIEW_MATRIX) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
    v_ray_dir = normalize(world_vert - cam_world);
}

float box_intersect(vec3 ro, vec3 rd, vec3 box_min, vec3 box_max) {
    vec3 tmin = (box_min - ro) / rd;
    vec3 tmax = (box_max - ro) / rd;
    vec3 t1 = min(tmin, tmax);
    vec3 t2 = max(tmin, tmax);
    float t_near = max(max(t1.x, t1.y), t1.z);
    float t_far = min(min(t2.x, t2.y), t2.z);
    if (t_near > t_far || t_far < 0.0) return -1.0;
    return max(t_near, 0.0);
}

void fragment() {
    vec3 center_world = (MODEL_MATRIX * vec4(vec3(0.0), 1.0)).xyz;
    vec3 box_min_world = center_world - box_size * 0.5;
    vec3 box_max_world = center_world + box_size * 0.5;
    vec3 ro = cam_world;
    vec3 rd = v_ray_dir;
    float t_entry = box_intersect(ro, rd, box_min_world, box_max_world);
    float t_exit = box_intersect(ro, rd, box_min_world, box_max_world);
    if (t_entry < 0.0) discard;
    // Calculate actual distance through the box for this ray
    vec3 tmin2 = (box_min_world - ro) / rd;
    vec3 tmax2 = (box_max_world - ro) / rd;
    vec3 t1b = min(tmin2, tmax2);
    vec3 t2b = max(tmin2, tmax2);
    float t_far = min(min(t2b.x, t2b.y), t2b.z);
    float total_dist = t_far - t_entry;
    vec3 pos = ro + rd * t_entry;
    // Use world-space step but cap iterations by actual traversal distance
    int required_steps = int(total_dist / step_size) + 2;
    int limit = min(required_steps, max_step);
    bool hit = false;
    for (int i = 0; i < limit; i++) {
        pos += rd * step_size;
        if (pos.y > box_max_world.y) break;
        vec2 uv = (pos.xz - box_min_world.xz) / box_size.xz;
        uv = clamp(uv, 0.0, 1.0);
        float h = texture(heightmap, uv).r;
        float terrain_y = box_min_world.y + h * box_size.y;
        if (pos.y <= terrain_y) {
            vec3 uvw = (pos - box_min_world) / box_size;
            ALBEDO = uvw;
            hit = true;
            break;
        }
    }
    if (!hit) discard;
}
Live Preview
Tags
Data/Generation, raymarch, terrain, volumetric
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from spaghetti

Related shaders

Comments 0

No comments yet.