Isometric city

glint · Examples · Tutorials · Gallery

Blocks rising and falling in a wave, drawn in isometric projection: raymarching with an orthographic camera (every ray parallel), and three flat shades for the three visible faces.

Isometric city: 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;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

float height(vec2 id) {
    return 0.3 + 1.2 * hash(id) * (0.5 + 0.5 * sin(time * 1.5 - length(id) * 0.5));
}
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 map(vec3 p) {
    vec2 id = floor(p.xz + 0.5);
    vec3 q = p;
    q.xz -= id;
    float h = height(id);
    return min(sdBox(q - vec3(0.0, h * 0.5, 0.0), vec3(0.4, h * 0.5, 0.4)), p.y);
}

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

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    // Orthographic: the ray starts at the pixel on a plane and always points the same way.
    vec3 fwd = normalize(vec3(-1.0, -0.82, -1.0));
    vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), fwd)), up = cross(fwd, right);
    vec3 ro = vec3(10.0, 8.2, 10.0) + (uv.x * right + uv.y * up) * 9.0;
    float t = 0.0;
    for (int i = 0; i < 120; i++) {
        float d = map(ro + fwd * t);
        if (d < 0.001 || t > 40.0) break;
        t += d * 0.6;
    }
    vec3 p = ro + fwd * t;
    vec3 n = normal(p);
    vec2 id = floor(p.xz + 0.5);
    vec3 base = p.y < 0.01 ? vec3(0.12, 0.13, 0.16) : pal(0.55 + height(id) * 0.25);
    // Top, left and right faces each get one flat shade: the isometric look.
    float shade = n.y > 0.5 ? 1.0 : (abs(n.x) > 0.5 ? 0.7 : 0.45);
    fragColor = vec4(base * shade, 1.0);
}

More More GLSL examples

CRT television · Lightning · Candle · Planet · Gyroscope · Moiré · Toon shading · Mandelbox