Stand inside (VR)

glint · Examples · Tutorials · Gallery

A courtyard of glowing arches round a still pool, with a camera that circles it. In a VR headset (or a phone in a VR viewer), Show › View in VR puts you in the middle of it: the shader's mainVR is handed each eye's ray, as on Shadertoy. Everything is in render(ro, rd), which both cameras call.

Stand inside (VR): 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;

in vec2 v_texcoord;
out vec4 fragColor;

// Eight arches in a ring 6 m out, a pool in the middle, the sky above.
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); }

float arch(vec3 p) {
    float outer = sdBox(p - vec3(0.0, 1.8, 0.0), vec3(1.1, 1.8, 0.25));
    float hole = max(length(p.xy - vec2(0.0, 2.1)) - 0.75, -p.y + 2.1);
    hole = min(hole, sdBox(p - vec3(0.0, 1.0, 0.0), vec3(0.75, 1.15, 0.4)));
    return max(outer, -hole);
}

// x = distance, y = material (0 floor, 1 arch).
vec2 map(vec3 p) {
    float a = atan(p.x, p.z), seg = 6.2831853 / 8.0;
    float k = floor(a / seg + 0.5) * seg;
    vec3 q = vec3(cos(k) * p.x - sin(k) * p.z, p.y, sin(k) * p.x + cos(k) * p.z);
    float ar = arch(q - vec3(0.0, 0.0, 6.0));
    float fl = p.y;
    return ar < fl ? vec2(ar, 1.0) : vec2(fl, 0.0);
}

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));
}

vec3 sky(vec3 rd) {
    vec3 c = mix(vec3(0.85, 0.45, 0.35), vec3(0.08, 0.1, 0.3), pow(max(rd.y, 0.0), 0.45));
    vec3 sun = normalize(vec3(-0.4, 0.15, -1.0));
    return c + vec3(1.0, 0.7, 0.4) * pow(max(dot(rd, sun), 0.0), 64.0);
}

vec3 render(vec3 ro, vec3 rd) {
    float t = 0.0;
    vec2 h = vec2(1.0);
    for (int i = 0; i < 96; i++) {
        h = map(ro + rd * t);
        if (h.x < 0.001 || t > 60.0) break;
        t += h.x;
    }
    if (t > 60.0) return sky(rd);
    vec3 p = ro + rd * t, n = normalAt(p);
    vec3 sun = normalize(vec3(-0.4, 0.15, -1.0));
    // The pool: a mirror in a ring of stone.
    float r = length(p.xz);
    if (h.y < 0.5 && r < 3.0) {
        vec3 rr = reflect(rd, vec3(0.0, 1.0, 0.0));
        vec3 rp = p + vec3(0.0, 0.001, 0.0);
        float s = 0.05;
        vec2 hh = vec2(1.0);
        for (int i = 0; i < 64; i++) {
            hh = map(rp + rr * s);
            if (hh.x < 0.002 || s > 40.0) break;
            s += hh.x;
        }
        vec3 refl = s > 40.0 ? sky(rr) : vec3(0.9, 0.75, 0.6) * 0.5;
        return mix(vec3(0.05, 0.12, 0.15), refl, 0.8) + 0.03 * sin(r * 12.0 - time * 1.5);
    }
    vec3 base = h.y > 0.5 ? vec3(0.85, 0.72, 0.58) : mix(vec3(0.3, 0.27, 0.25), vec3(0.6, 0.55, 0.48), mod(floor(p.x) + floor(p.z), 2.0));
    float diff = max(dot(n, sun), 0.0);
    // The arches glow inside, slowly, one after another.
    float glow = h.y > 0.5 ? 0.5 + 0.5 * sin(atan(p.x, p.z) * 1.0 + time) : 0.0;
    // Shadows from the low sun.
    float sh = 1.0, s2 = 0.05;
    for (int i = 0; i < 32; i++) {
        float d = map(p + n * 0.01 + sun * s2).x;
        sh = min(sh, 10.0 * d / s2);
        s2 += clamp(d, 0.05, 0.5);
        if (sh < 0.01 || s2 > 20.0) break;
    }
    vec3 col = base * (vec3(1.2, 0.85, 0.6) * diff * clamp(sh, 0.0, 1.0) + vec3(0.12, 0.14, 0.24)) + vec3(1.0, 0.5, 0.2) * glow * 0.35 * smoothstep(3.2, 1.0, p.y) * step(0.5, h.y);
    return mix(col, sky(rd), 1.0 - exp(-t * 0.015));
}

// In VR: each pixel's ray from the eye (on Shadertoy too). The headset's floor is y = 0.
void mainVR(out vec4 fragColor, in vec2 fragCoord, in vec3 ro, in vec3 rd) {
    fragColor = vec4(render(ro, rd), 1.0);
}

// On a screen: circle the pool (drag to look around).
void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float a = time * 0.1 + (mouse.x - 0.5) * 3.0;
    vec3 ro = vec3(sin(a) * 3.5, 1.6, cos(a) * 3.5);
    vec3 fw = normalize(vec3(0.0, 1.6 + (mouse.y - 0.5) * 2.0, 0.0) - ro + vec3(sin(a + 1.2), 0.0, cos(a + 1.2)) * 2.0);
    vec3 rt = normalize(cross(vec3(0, 1, 0), fw)), up = cross(fw, rt);
    vec3 rd = normalize(fw * 1.2 + rt * uv.x + up * uv.y);
    fragColor = vec4(render(ro, rd), 1.0);
}

More Games, sound & cube maps examples

Keyboard maze · Little synth · Cube map reflections · Textured room · Video wall · HDRI reflections