Skip to content

Instantly share code, notes, and snippets.

@behreajj
Last active August 9, 2018 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behreajj/bd3da3195d614d28c0f524cfc257ea16 to your computer and use it in GitHub Desktop.
Save behreajj/bd3da3195d614d28c0f524cfc257ea16 to your computer and use it in GitHub Desktop.
Warp Fragment Shader
precision mediump float;
precision mediump int;
uniform float colorquant;
uniform float noiseamp;
uniform float time;
uniform vec2 center;
uniform vec2 dimensions;
uniform vec2 tiling;
uniform vec4 aclr;
uniform vec4 bclr;
// For use in Perlin noise.
const vec2 tr = vec2(1.0, 0.0);
const vec2 bl = vec2(0.0, 1.0);
const vec2 br = vec2(1.0, 1.0);
// To track rotation of rectangles.
const mat3 transform = mat3(
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0);
varying vec4 vertTexCoord;
/* minkowski, quantize, quantizeclr and tile functions, as before. */
float random(in vec2 st) {
return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123);
}
// Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd .
float noise(vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
// Find four corresponding corners.
float a = random(i);
float b = random(i + tr);
float c = random(i + bl);
float d = random(i + br);
// Smooth step.
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;
}
mat3 rotate(mat3 m, float angle) {
float c = cos(angle);
float s = sin(angle);
// To change CW/CCW direction, swap
// +/- signs for s.
return mat3(
c * m[0][0] + s * m[1][0],
c * m[0][1] + s * m[1][1],
c * m[0][2] + s * m[1][2],
c * m[1][0] - s * m[0][0],
c * m[1][1] - s * m[0][1],
c * m[1][2] - s * m[0][2],
m[2][0], m[2][1], m[2][2]);
}
// Subtract center from point to return its pivot to (0, 0).
// Convert the difference to a vec3 to allow multiplication
// with a mat3. Return to a vec2 by sampling xy, then add center.
float rect(mat3 transform, vec2 center, vec2 point, vec2 extents) {
vec2 pt = center + (transform *
vec3(point - center, 0.0)).xy;
vec2 topleft = center - extents;
vec2 bottomright = center + extents;
// When boolean false == 0.0 and true == 1.0,
// then a & b is min(a, b) and a | b is max(a, b).
vec2 st = min(step(topleft, pt),
step(pt, bottomright));
return min(st.x, st.y);
}
void main() {
vec2 coord01 = gl_FragCoord.xy / dimensions;
coord01.y = 1.0 - coord01.y;
vec2 pt = tile(coord01 + time, tiling);
// Create rotating rectangle.
mat3 rot = rotate(transform, time * fract(pt).x);
float shape = rect(rot, center, pt, vec2(0.2, 0.2));
float shapetime = shape + time;
// Calculate distance.
float cososc = 0.5 + 0.5 * cos(time);
float mnkfac = mix(0.5, 3.0, cososc);
float mnk = minkowski(pt, center, mnkfac);
// Calculate noise and amplify.
vec2 amplified = noiseamp * coord01;
float fac = noise(mnk + amplified + shapetime);
// Mix and quantize color.
vec4 color = mix(aclr, bclr, fac);
vec4 qcolor = quantizeclr(color, colorquant);
gl_FragColor = qcolor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment