xxxxxxxxxx
82
precision mediump float;
uniform vec2 mapPos;
uniform vec2 res;
in vec2 pos;
out vec4 colour;
// Noise algo from:
// https://www.shadertoy.com/view/4dS3Wd
float hash(vec2 p) {vec3 p3 = fract(vec3(p.xyx) * 0.13); p3 += dot(p3, p3.yzx + 3.333); return fract((p3.x + p3.y) * p3.z); }
float noise(vec2 x) {
vec2 i = floor(x);
vec2 f = fract(x);
// Four corners in 2D of a tile
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
// Simple 2D lerp using smoothstep envelope between the values.
// return vec3(mix(mix(a, b, smoothstep(0.0, 1.0, f.x)),
// mix(c, d, smoothstep(0.0, 1.0, f.x)),
// smoothstep(0.0, 1.0, f.y)));
// Same code, with the clamps in smoothstep and common subexpressions
// optimized away.
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
}
float fbm(vec2 x) {
float v = 0.0;
float a = 0.5;
vec2 shift = vec2(100);
// Rotate to reduce axial bias
mat2 rot = mat2(cos(0.5), sin(0.5), -sin(0.5), cos(0.50));
for (int i = 0; i < NUM_NOISE_OCTAVES; ++i) {
v += a * noise(x);
x = rot * x * 2.0 + shift;
a *= 0.5;
}
return v;
}
vec4 packHeight(float value) {
int rgba = floatBitsToInt(value);
float a = float((rgba & 0xff000000) >> 24) / 255.0;
float b = float((rgba & 0x00ff0000) >> 16) / 255.0;
float g = float((rgba & 0x0000ff00) >> 8) / 255.0;
float r = float(rgba & 0x000000ff) / 255.0;
a = max(1./255., a);
return vec4(r, g, b, a);
}
float getHeight(vec2 p) {
p += mapPos;
float n = 1.1 * fbm(p * 5.);
n = pow(n, 2.);
float d = length(pos - (0.5)) * 2.;
return clamp(n * (1.3 - d), 0., 1.);
}
void main() {
float d = length(pos - 0.5);
float h = (1. - step(0.3, d)) * step(0.1, d) * 0.5 + (1. - d) * 0.5;
// float h = getHeight(pos * res);
colour = packHeight(h);
}