Textured room

glint · Examples · Tutorials · Gallery

glint's stock textures in a raymarched room: wood on the floor, rock tiles on the walls, a marble ball and a rusty metal one (mapped from all three sides), the colonnade cube map through the window, and drifting dust from a 3D noise volume. Each is just a uniform with its name: uniform sampler2D wood;

Textured room: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform sampler2D wood;
uniform sampler2D rockTiles;
uniform sampler2D marble;
uniform sampler2D rustyMetal;
uniform samplerCube colonnade;
uniform sampler3D noise3D;

in vec2 v_texcoord;
out vec4 fragColor;

// Room 8 wide, 4 high, 10 deep; a window in the back wall; two balls on the floor.
const vec3 ROOM = vec3(4.0, 4.0, 5.0);
const vec3 BALL1 = vec3(-1.1, 0.8, 0.5);
const vec3 BALL2 = vec3(1.3, 0.6, -0.8);

float sdBox(vec3 p, vec3 b) { vec3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); }

// x = distance, y = material (0 room, 1 marble, 2 rusty).
vec2 map(vec3 p) {
    vec3 q = p - vec3(0.0, ROOM.y * 0.5, 0.0);
    float room = -sdBox(q, ROOM * vec3(1.0, 0.5, 1.0));
    // The window: a hole through the back wall.
    room = max(room, -sdBox(p - vec3(0.6, 2.3, -ROOM.z), vec3(1.4, 0.9, 0.5)));
    vec2 r = vec2(room, 0.0);
    float b1 = length(p - BALL1) - 0.8, b2 = length(p - BALL2) - 0.6;
    if (b1 < r.x) r = vec2(b1, 1.0);
    if (b2 < r.x) r = vec2(b2, 2.0);
    return r;
}

vec3 normalAt(vec3 p) {
    vec2 e = vec2(0.002, 0.0);
    return normalize(vec3(map(p + e.xyy).x - map(p - e.xyy).x, map(p + e.yxy).x - map(p - e.yxy).x, map(p + e.yyx).x - map(p - e.yyx).x));
}

// A texture from all three sides, blended by the normal (no seams on a ball).
vec3 triplanar(sampler2D t, vec3 p, vec3 n, float s) {
    vec3 w = pow(abs(n), vec3(4.0));
    w /= w.x + w.y + w.z;
    return texture(t, p.yz * s).rgb * w.x + texture(t, p.xz * s).rgb * w.y + texture(t, p.xy * s).rgb * w.z;
}

vec3 albedo(vec3 p, vec3 n, float m) {
    if (m > 1.5) return triplanar(rustyMetal, p, n, 0.6);
    if (m > 0.5) return triplanar(marble, p, n, 0.4);
    if (n.y > 0.5) return texture(wood, p.xz * 0.18).rgb;                 // floor
    if (n.y < -0.5) return vec3(0.75, 0.72, 0.68);                       // ceiling
    return texture(rockTiles, (abs(n.x) > 0.5 ? p.zy : p.xy) * 0.22).rgb; // walls
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float a = (mouse.x - 0.5) * 1.2 + sin(time * 0.15) * 0.25;
    vec3 ro = vec3(sin(a) * 3.0, 1.7 + (mouse.y - 0.5), 4.2);
    vec3 fw = normalize(vec3(0.0, 1.2, -1.0) - ro), rt = normalize(cross(vec3(0, 1, 0), fw)), up = cross(fw, rt);
    vec3 rd = normalize(fw * 1.3 + rt * uv.x + up * uv.y);
    float t = 0.0;
    vec2 h = vec2(1.0);
    for (int i = 0; i < 128; i++) {
        h = map(ro + rd * t);
        if (h.x < 0.001 || t > 30.0) break;
        t += h.x;
    }
    vec3 lightPos = vec3(0.6, 2.3, -ROOM.z - 1.0);
    vec3 col;
    if (t > 30.0 || ro.z + rd.z * t < -ROOM.z - 0.2) {
        // Out through the window.
        col = texture(colonnade, rd).rgb;
    } else {
        vec3 p = ro + rd * t, n = normalAt(p);
        vec3 l = normalize(lightPos - p);
        float diff = max(dot(n, l), 0.0);
        // Soft shadow towards the window.
        float sh = 1.0, s = 0.05;
        for (int i = 0; i < 32; i++) {
            float d = map(p + l * s).x;
            sh = min(sh, 8.0 * d / s);
            s += clamp(d, 0.05, 0.4);
            if (sh < 0.01 || s > length(lightPos - p)) break;
        }
        vec3 base = albedo(p, n, h.y);
        col = base * (vec3(1.2, 1.1, 0.95) * diff * clamp(sh, 0.0, 1.0) + vec3(0.18, 0.2, 0.25));
        if (h.y > 1.5) col += texture(colonnade, reflect(rd, n)).rgb * 0.15;
        if (h.y > 0.5 && h.y < 1.5) col += texture(colonnade, reflect(rd, n)).rgb * 0.35 * pow(1.0 - max(dot(n, -rd), 0.0), 3.0);
    }
    // Dust in the light: a few samples of the 3D noise along the ray, brighter near the window's beam.
    float dust = 0.0;
    for (int i = 0; i < 12; i++) {
        vec3 q = ro + rd * (float(i) + 0.5) * min(t, 12.0) / 12.0;
        float beam = exp(-pow(length((q - lightPos).xy - vec2(0.0, 0.0)) * 0.6, 2.0));
        dust += texture(noise3D, q * 0.25 + vec3(0.0, time * 0.02, time * 0.01)).x * beam;
    }
    col += vec3(1.0, 0.9, 0.7) * dust * 0.03;
    col = col / (1.0 + col);
    fragColor = vec4(pow(col, vec3(0.85)), 1.0);
}

More Games, sound & cube maps examples

Keyboard maze · Little synth · Cube map reflections · Stand inside (VR) · Video wall · HDRI reflections