Desert dunes
glint · Examples · Tutorials · Gallery
Flying low over sand dunes at sunset: a height field of sharp-crested ridges bent by noise, marched in steps, lit by a low sun with cool shadows and haze.

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 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 height(vec2 p) {
// 1 - |sin| has sharp crests and round troughs, like a dune; noise bends the ridges.
float n = noise(p * 0.12) * 4.0 + noise(p * 0.4);
float big = pow(1.0 - abs(sin(p.x * 0.25 + p.y * 0.1 + n)), 2.0) * 1.6;
float small = pow(1.0 - abs(sin(p.x * 0.6 - p.y * 0.3 + n * 1.5)), 2.0) * 0.4;
float ripples = sin(p.x * 6.0 + p.y * 2.0 + n * 8.0) * 0.015;
return big + small + ripples;
}
vec3 normal(vec3 p) {
vec2 e = vec2(0.01, 0.0);
return normalize(vec3(height(p.xz - e.xy) - height(p.xz + e.xy), 2.0 * e.x, height(p.xz - e.yx) - height(p.xz + e.yx)));
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
const vec3 SUN = normalize(vec3(-0.8, 0.18, 0.6));
vec3 ro = vec3(time * 0.4, 0.0, time * 2.0);
ro.y = height(ro.xz) + 1.3;
vec3 rd = cameraRay(ro, ro + vec3(0.2, -0.25, 1.0), uv, 1.4);
vec3 sky = mix(vec3(1.0, 0.6, 0.35), vec3(0.25, 0.35, 0.6), smoothstep(-0.05, 0.4, rd.y));
sky += vec3(1.0, 0.7, 0.4) * pow(max(dot(rd, SUN), 0.0), 300.0) * 6.0 + vec3(1.0, 0.5, 0.2) * pow(max(dot(rd, SUN), 0.0), 8.0) * 0.4;
float t = 0.1;
bool hit = false;
for (int i = 0; i < 150; i++) {
vec3 p = ro + rd * t;
float d = p.y - height(p.xz);
if (d < 0.002 * t) { hit = true; break; }
if (t > 60.0) break;
t += d * 0.45;
}
vec3 col = sky;
if (hit) {
vec3 p = ro + rd * t;
vec3 n = normal(p);
float diff = max(dot(n, SUN), 0.0);
vec3 sand = vec3(0.95, 0.6, 0.35);
col = sand * diff * vec3(1.3, 1.0, 0.8) + sand * vec3(0.2, 0.25, 0.45) * (0.5 + 0.5 * n.y);
col = mix(col, sky * 0.9, 1.0 - exp(-t * 0.035));
}
fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}More More GLSL examples
Chrome balls · Stained glass · DNA helix · Clock · Apollonian · Digital rain · Bokeh · ASCII