9_5_displacement
Code 9.5, Figure 9.9:​
9_5_displacement.frag
#version 300 esprecision mediump float;precision highp int;out vec4 fragColor;uniform float u_time;uniform vec2 u_resolution;//begin rotvec2 rot2(vec2 p, float t){return vec2(cos(t) * p.x -sin(t) * p.y, sin(t) * p.x + cos(t) * p.y);}vec3 rotX(vec3 p, float t){return vec3(p.x, rot2(p.yz, t));}vec3 rotY(vec3 p, float t){return vec3(p.y, rot2(p.zx, t)).zxy;}vec3 rotZ(vec3 p, float t){return vec3(rot2(p.xy, t), p.z);}vec3 euler(vec3 p, vec3 t){return rotZ(rotY(rotX(p, t.x), t.y), t.z);}//end rot//start hashuvec3 k = uvec3(0x456789abu, 0x6789ab45u, 0x89ab4567u);uvec3 u = uvec3(1, 2, 3);const uint UINT_MAX = 0xffffffffu;uint uhash11(uint n){n ^= (n << u.x);n ^= (n >> u.x);n *= k.x;n ^= (n << u.x);return n * k.x;}uvec2 uhash22(uvec2 n){n ^= (n.yx << u.xy);n ^= (n.yx >> u.xy);n *= k.xy;n ^= (n.yx << u.xy);return n * k.xy;}uvec3 uhash33(uvec3 n){n ^= (n.yzx << u);n ^= (n.yzx >> u);n *= k;n ^= (n.yzx << u);return n * k;}float hash11(float p){uint n = floatBitsToUint(p);return float(uhash11(n)) / float(UINT_MAX);}float hash21(vec2 p){uvec2 n = floatBitsToUint(p);return float(uhash22(n).x) / float(UINT_MAX);}float hash31(vec3 p){uvec3 n = floatBitsToUint(p);return float(uhash33(n).x) / float(UINT_MAX);}vec2 hash22(vec2 p){uvec2 n = floatBitsToUint(p);return vec2(uhash22(n)) / vec2(UINT_MAX);}vec3 hash33(vec3 p){uvec3 n = floatBitsToUint(p);return vec3(uhash33(n)) / vec3(UINT_MAX);}//end hash//start pnoisefloat gtable2(vec2 lattice, vec2 p){uvec2 n = floatBitsToUint(lattice);uint ind = uhash22(n).x >> 29;float u = 0.92387953 * (ind < 4u ? p.x : p.y); //0.92387953 = cos(pi/8)float v = 0.38268343 * (ind < 4u ? p.y : p.x); //0.38268343 = sin(pi/8)return ((ind & 1u) == 0u ? u : -u) + ((ind & 2u) == 0u? v : -v);}float pnoise21(vec2 p){vec2 n = floor(p);vec2 f = fract(p);float[4] v;for (int j = 0; j < 2; j ++){for (int i = 0; i < 2; i++){v[i+2*j] = gtable2(n + vec2(i, j), f - vec2(i, j));}}f = f * f * f * (10.0 - 15.0 * f + 6.0 * f * f);return 0.5 * mix(mix(v[0], v[1], f[0]), mix(v[2], v[3], f[0]), f[1]) + 0.5;}float gtable3(vec3 lattice, vec3 p){uvec3 n = floatBitsToUint(lattice);uint ind = uhash33(n).x >> 28;float u = ind < 8u ? p.x : p.y;float v = ind < 4u ? p.y : ind == 12u || ind == 14u ? p.x : p.z;return ((ind & 1u) == 0u? u: -u) + ((ind & 2u) == 0u? v : -v);}float pnoise31(vec3 p){vec3 n = floor(p);vec3 f = fract(p);float[8] v;for (int k = 0; k < 2; k++ ){for (int j = 0; j < 2; j++ ){for (int i = 0; i < 2; i++){v[i+2*j+4*k] = gtable3(n + vec3(i, j, k), f - vec3(i, j, k)) * 0.70710678;}}}f = f * f * f * (10.0 - 15.0 * f + 6.0 * f * f);float[2] w;for (int i = 0; i < 2; i++){w[i] = mix(mix(v[4*i], v[4*i+1], f[0]), mix(v[4*i+2], v[4*i+3], f[0]), f[1]);}return 0.5 * mix(w[0], w[1], f[2]) + 0.5;}//end pnoisefloat sphereSDF(vec3 p){return length(p) - 1.0;}float sceneSDF(vec3 p){return sphereSDF(p)+ 0.1 * pnoise31(10.0 * p);}vec3 gradSDF(vec3 p){float eps = 0.001;return normalize(vec3(sceneSDF(p + vec3(eps, 0.0, 0.0)) - sceneSDF(p - vec3(eps, 0.0, 0.0)),sceneSDF(p + vec3(0.0, eps, 0.0)) - sceneSDF(p - vec3(0.0, eps, 0.0)),sceneSDF(p + vec3(0.0, 0.0, eps)) - sceneSDF(p - vec3(0.0, 0.0, eps))));}void main(){vec2 p = (gl_FragCoord.xy * 2.0 - u_resolution) / min(u_resolution.x, u_resolution.y);float t = u_time * 0.3;vec3 cPos = rotY(vec3(0.0, 0.0, 2.0), t);vec3 cDir = rotY(vec3(0.0, 0.0, - 1.0), t);vec3 cUp = rotY(vec3(0.0, 1.0, 0.0), t);vec3 cSide = cross(cDir, cUp);float targetDepth = 1.0;vec3 lDir = rotY(vec3(1.0), t);vec3 ray = cSide * p.x + cUp * p.y + cDir * targetDepth;vec3 rPos = ray + cPos;ray = normalize(ray);bool hit = false;for(int i = 0; i < 50; i ++ ){if (sceneSDF(rPos) > 0.001){rPos += sceneSDF(rPos) * ray;} else {float amb = 0.1;float diff = 0.9 * max(dot(normalize(lDir), gradSDF(rPos)), 0.0);vec3 col = vec3(1.0);fragColor.rgb = col * (diff + amb);break;}}fragColor.a = 1.0;}