Toon shading
glint · Examples · Tutorials · Gallery
Raymarched blobs drawn like a cartoon: light in three flat steps instead of a smooth gradient, a rim highlight, and ink outlines where the ray only just missed or grazes the surface.

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))); }
vec3 cameraRay(vec3 ro, vec3 target, vec2 uv, float zoom) {
vec3 f = normalize(target - ro);
vec3 r = normalize(cross(vec3(0.0, 1.0, 0.0), f));
vec3 u = cross(f, r);
return normalize(uv.x * r + uv.y * u + zoom * f);
}
float smin(float a, float b, float k) { float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0); return mix(b, a, h) - k * h * (1.0 - h); }
float map(vec3 p) {
float d = length(p) - 0.6;
for (int i = 0; i < 4; i++) {
float fi = float(i);
vec3 c = 0.75 * vec3(sin(time * 0.9 + fi * 1.7), cos(time * 0.7 + fi * 2.3), sin(time * 0.5 + fi));
d = smin(d, length(p - c) - 0.3, 0.35);
}
return d;
}
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;
vec3 ro = vec3(0.0, 0.0, -3.5);
vec3 rd = cameraRay(ro, vec3(0.0), uv, 1.5);
float t = 0.0, closest = 1e9;
for (int i = 0; i < 90; i++) {
float d = map(ro + rd * t);
closest = min(closest, d);
if (d < 0.001 || t > 10.0) break;
t += d;
}
// Background: halftone dots.
vec2 g = fract(gl_FragCoord.xy / 10.0) - 0.5;
vec3 col = mix(vec3(1.0, 0.95, 0.8), vec3(0.95, 0.8, 0.6), smoothstep(0.25, 0.2, length(g) - uv.y * 0.3));
if (t < 10.0) {
vec3 p = ro + rd * t;
vec3 n = normal(p);
float diff = max(dot(n, normalize(vec3(0.6, 0.7, -0.5))), 0.0);
float bands = diff > 0.6 ? 1.0 : diff > 0.25 ? 0.65 : 0.35; // three flat steps
col = vec3(0.3, 0.75, 0.95) * bands;
col += smoothstep(0.55, 0.6, 1.0 - max(dot(n, -rd), 0.0)) * 0.3; // rim
col = mix(col, vec3(0.05), smoothstep(0.3, 0.2, max(dot(n, -rd), 0.0))); // outline where it grazes
} else if (closest < 0.02) col = vec3(0.05); // outline where it just missed
fragColor = vec4(col, 1.0);
}More More GLSL examples
Planet · Isometric city · Gyroscope · Moiré · Mandelbox · Hyperbolic tiling · Wave interference · Lorenz attractor