/* This code is part of an article, http://gpfault.net/posts/perlin-noise.txt.html */ /* Demonstrates generation of fractal 1D perlin noise */ /* Returns a gradient value for a given integer p. Gradients are 1 or -1 for 1D case. Gradients are read from a repeating texture containing random RGB values, with neares-neighbor filtering. */ float grad(float p) { const float texture_width = 256.0; float v = texture2D(iChannel0, vec2(p / texture_width, p)).r; return v > 0.5 ? 1.0 : -1.0; } /* S-shaped curve for 0 <= t <= 1 */ float fade(float t) { return t*t*t*(t*(t*6.0 - 15.0) + 10.0); } /* 1D noise */ float noise(float p) { float p0 = floor(p); float p1 = p0 + 1.0; float t = p - p0; float fade_t = fade(t); float g0 = grad(p0); float g1 = grad(p1); return (1.0-fade_t)*g0*(p - p0) + fade_t*g1*(p - p1); } float mountain2(float x) { float position = iGlobalTime * 12.5 + x; return 0.5*(noise(position * (1.0/300.0)) * 1.0 + noise(position * (1.0/150.0)) * 0.5 + noise(position * (1.0/75.0)) * 0.25 + noise(position * (1.0/37.5)) * 0.125); } float mountain1(float x) { float position = iGlobalTime * 50.0 + x; return noise(position * (1.0/300.0)) * 1.0 + noise(position * (1.0/150.0)) * 0.5 + noise(position * (1.0/75.0)) * 0.25 + noise(position * (1.0/37.5)) * 0.125; } void mainImage( out vec4 fragColor, in vec2 fragCoord ) { float n1 = mountain1(fragCoord.x); float n2 = mountain2(fragCoord.x); float ypos = fragCoord.y/iResolution.y; float y = 2.0 * (ypos) - 1.0; /* map fragCoord.y into [-1; 1] range */ vec3 color = mix(mix(vec3(1.0, 1.0, 0.0), vec3(1.0, 0.0, 0.0), ypos/1.1), vec3(0.0, 0.1, 0.2), ypos / 1.2); if(n2 > y) color = vec3(0.2, 0.2, 0.0); if(n1 > y) color = vec3(0.01); fragColor = vec4(color, 1.0); }