5_5_bool
コード 5.6, 5.7, 図 5.6:
5_5_bool.frag
#version 300 esprecision highp float;precision highp int;out vec4 fragColor;uniform float u_time;uniform vec2 u_resolution;int channel;const float PI = 3.1415926;const float TAU = 6.2831853;//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 vnoisefloat vnoise21(vec2 p){vec2 n = floor(p);float[4] v;for (int j = 0; j < 2; j ++){for (int i = 0; i < 2; i++){v[i+2*j] = hash21(n + vec2(i, j));}}vec2 f = fract(p);f = f * f * f * (10.0 - 15.0 * f + 6.0 * f * f);return mix(mix(v[0], v[1], f[0]), mix(v[2], v[3], f[0]), f[1]);}//end vnoise//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;}//end pnoisefloat fbm21(vec2 p, float g){float val = 0.0;float amp = 1.0;float freq = 1.0;for (int i = 0; i < 4; i++){val += amp * (vnoise21(freq * p) - 0.5);amp *= g;freq *= 2.01;}return 0.5 * val + 0.5;}float base21(vec2 p){return mod(u_time, 20.0) < 10.0 ?fbm21(p, 0.5) :pnoise21(p);}float warp21(vec2 p, float g){float val = 0.0;for (int i = 0; i < 4; i++){val = base21(p + g * vec2(cos(TAU * val), sin(TAU * val)));}return val;}void main(){vec2 pos = gl_FragCoord.xy/min(u_resolution.x, u_resolution.y);channel = int(2.0 * gl_FragCoord.x / u_resolution.x);pos = 10.0 * pos + u_time;vec2 f = vec2(warp21(pos, 1.0), warp21(pos + 10.0, 1.0));f -= 0.5;vec4 x;if (channel == 0){bvec2 b = bvec2(step(f, vec2(0)));x = vec4(b[0] && b[1],b[0] && !b[1],!b[0] && b[1],!(b[0] || b[1]));} else {x = vec4(max(f[0], f[1]),max(f[0], -f[1]),max(-f[0], f[1]),-min(f[0], f[1]));x = step(x, vec4(0.0));}vec3[4] col4 = vec3[](vec3(1, 0, 0),vec3(0, 1, 0),vec3(0, 0, 1),vec3(1, 1, 0));for (int i = 0; i < 4; i++){fragColor.rgb += x[i] * col4[i];}// matrix formulation// mat4x3 col = mat4x3(// vec3(1, 0, 0),// vec3(0, 1, 0),// vec3(0, 0, 1),// vec3(1, 1, 0)// );// fragColor.rgb = col * x;fragColor.a = 1.0;}