Created
June 27, 2017 16:37
-
-
Save ada-lovecraft/52a3d7d71fc7a6a8ab472e47de9629d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Ada Lovecraft | |
// Title: Polygonal Exclusion | |
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
#define PI 3.14159265358979323846 | |
uniform vec2 u_resolution; | |
uniform vec2 u_mouse; | |
uniform float u_time; | |
vec4 geobounds = vec4( | |
-102.36966133117676, | |
31.916106047833807, | |
-102.36045598983765, | |
31.924010483480597 | |
); | |
vec2 verts[10]; | |
const int vertsLength = 10; | |
vec2 squareFrame(vec2 screenSize) { | |
vec2 position = 2.0 * (gl_FragCoord.xy / screenSize.xy) - 1.0; | |
position.x *= screenSize.x / screenSize.y; | |
return position; | |
} | |
vec2 squareFrame(vec2 screenSize, vec2 coord) { | |
vec2 position = 2.0 * (coord.xy / screenSize.xy) - 1.0; | |
position.x *= screenSize.x / screenSize.y; | |
return position; | |
} | |
vec2 map(vec2 value, vec2 inMin, vec2 inMax, vec2 outMin, vec2 outMax) { | |
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin); | |
} | |
float map(float value, float inMin, float inMax, float outMin, float outMax) { | |
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin); | |
} | |
float circle(in vec2 _st, in vec2 center, in float _radius) { | |
vec2 dist = _st - center; | |
return 1. - smoothstep( | |
_radius-(_radius*0.01), | |
_radius+(_radius*0.01), | |
dot(dist,dist)*8.0); | |
} | |
float box(vec2 _st, vec2 _size, float _smoothEdges){ | |
_size = vec2(0.5)-_size*0.5; | |
vec2 aa = vec2(_smoothEdges*0.5); | |
vec2 uv = smoothstep(_size,_size+aa,_st); | |
uv *= smoothstep(_size,_size+aa,vec2(1.0)-_st); | |
return uv.x*uv.y; | |
} | |
highp float random(vec2 co) | |
{ | |
highp float a = 12.9898; | |
highp float b = 78.233; | |
highp float c = 43758.5453; | |
highp float dt= dot(co.xy ,vec2(a,b)); | |
highp float sn= mod(dt,3.14); | |
return fract(sin(sn) * c); | |
} | |
void defineVerts() { | |
verts[0] = vec2(-102.36251592636108, 31.924010483480597 ); | |
verts[1] = vec2(-102.36884593963623,31.922735620452148); | |
verts[2] = vec2(-102.36863136291504,31.921879631073775); | |
verts[3] = vec2(-102.36888885498047,31.921278612494262); | |
verts[4] = vec2(-102.36936092376709,31.920641164254544); | |
verts[5] = vec2(-102.36966133117676,31.919858007511934); | |
verts[6] = vec2(-102.36953258514404,31.918929138544694); | |
verts[7] = vec2(-102.36871719360352, 31.916106047833807); | |
verts[8] = vec2(-102.36045598983765,31.917945620002385); | |
verts[9] = vec2(-102.36251592636108,31.924010483480597); | |
} | |
void mapVerts() { | |
vec2 rez = squareFrame(u_resolution.xy, u_resolution.xy); | |
for(int i = 0; i < vertsLength; i++) { | |
verts[i] = map(verts[i], geobounds.xy, geobounds.zw, vec2(-0.5), vec2(0.5)); | |
} | |
} | |
float dotproduct(in vec2 a,in vec2 b, in vec2 c) { | |
float bax = a.x - b.x; | |
float bay = a.y - b.y; | |
float bcx = c.x - b.x; | |
float bcy = c.y - b.y; | |
return bax * bcx + bay * bcy; | |
} | |
float crossproductlength(in vec2 a, in vec2 b, in vec2 c) { | |
float bax = a.x - b.x; | |
float bay = a.y - b.y; | |
float bcx = c.x - b.x; | |
float bcy = c.y - b.y; | |
return bax * bcy - bay * bcx; | |
} | |
float angle(in vec2 a, in vec2 b, in vec2 c) { | |
float dp = dotproduct(a,b,c); | |
float cpl = crossproductlength(a,b,c); | |
return atan(cpl, dp); | |
} | |
float pointInPolygon(vec2 _st) { | |
float totalAngle = angle(verts[vertsLength - 1], _st, verts[0]); | |
for(int i = 0; i < vertsLength - 1; i++) { | |
totalAngle += angle(verts[i], _st, verts[i+1]); | |
} | |
return sign(floor(abs(totalAngle))); | |
} | |
vec2 rotate2d(vec2 _st, float _angle){ | |
_st -= 0.5; | |
_st = mat2(cos(_angle),-sin(_angle), | |
sin(_angle),cos(_angle)) * _st; | |
_st += 0.5; | |
return _st; | |
} | |
vec2 tile(in vec2 _st, in float _zoom){ | |
_st *= _zoom; | |
return fract(_st); | |
} | |
void main() { | |
defineVerts(); | |
mapVerts(); | |
vec2 st = squareFrame(u_resolution.xy, gl_FragCoord.xy); | |
vec3 color = vec3(0.8,0.1,0.4); | |
vec2 tiled = tile(st, 4. + (sin(u_time* 0.01)+1.) + random(st)); | |
vec2 rotated = rotate2d(tiled, PI * u_time * 0.1); | |
float b = box(rotated, vec2(0.8), 0.1); | |
color += vec3(b); | |
color *= vec3(0., .5, .5); | |
//color += vec3(1. - pct, 0., (1.-pct)*0.5); | |
float pct = pointInPolygon(st); | |
gl_FragColor = vec4(mix(color,vec3(0.838,0.850,0.841),pct), 1.); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment