Low poly

glint · Examples · Tutorials · Gallery

Rolling hills as flat-shaded triangles: a grid split along alternating diagonals, each triangle lit by the normal of its own three corners (heights from noise), coloured from sea to snow.

Low poly: 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 noise(vec2 p) {
    vec2 i = floor(p), f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
    float v = 0.0, a = 0.5;
    for (int i = 0; i < 5; i++) { v += a * noise(p); p = p * 2.03 + 17.0; a *= 0.5; }
    return v;
}

float height(vec2 v) { return noise(v * 0.25 + vec2(time * 0.15, 0.0)) * 1.3 + noise(v * 0.7) * 0.3; }

void main() {
    vec2 p = gl_FragCoord.xy / resolution.y * 12.0;
    vec2 cell = floor(p), f = fract(p);
    // Which triangle of the cell (the diagonal alternates like a checkerboard).
    bool flip = mod(cell.x + cell.y, 2.0) > 0.5;
    vec2 a, b, c;
    if (!flip) { a = vec2(0.0); c = vec2(1.0); b = f.x > f.y ? vec2(1.0, 0.0) : vec2(0.0, 1.0); }
    else { a = vec2(1.0, 0.0); c = vec2(0.0, 1.0); b = f.x + f.y < 1.0 ? vec2(0.0) : vec2(1.0); }
    vec3 A = vec3(a, height(cell + a)), B = vec3(b, height(cell + b)), C = vec3(c, height(cell + c));
    vec3 n = normalize(cross(B - A, C - A));
    if (n.z < 0.0) n = -n;
    float h = (A.z + B.z + C.z) / 3.0;
    vec3 base = h < 0.5 ? vec3(0.15, 0.4, 0.65) : h < 0.58 ? vec3(0.85, 0.8, 0.55) : h < 1.0 ? vec3(0.3, 0.6, 0.3) : h < 1.25 ? vec3(0.45, 0.4, 0.35) : vec3(0.95);
    float light = 0.35 + 0.65 * max(dot(n, normalize(vec3(-0.5, 0.6, 0.7))), 0.0);
    fragColor = vec4(base * light, 1.0);
}

More More GLSL examples

Spirograph · Field lines · Tesseract · Black hole · 10 PRINT · Invaders · Gears · Phyllotaxis