Skip to content

Instantly share code, notes, and snippets.

@AlainBarrios
AlainBarrios / shape.frag
Created April 15, 2019 23:51
Function to create shapes with glsl
float shape(vec2 p, int N, float size){
p = p * 2. - 1.;
float a = atan(p.x, p.y) + PI;
float r = TWO_PI / float(N);
float l = length(p);
float d = cos(floor(.5 + a / r) * r - a) * l;
return step(size, d);
@AlainBarrios
AlainBarrios / rotate2d.frag
Last active April 17, 2019 15:36
rotation for uv space in 2d
vec2 rotate(vec2 v, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return m * v;
}
@AlainBarrios
AlainBarrios / rotate3d.frag
Created April 17, 2019 17:22
Rotation in 3d for uv space
mat4 rotationMatrix(vec3 axis, float angle) {
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
@AlainBarrios
AlainBarrios / distortion.frag
Created April 25, 2019 03:46
distortion function
vec2 distortion(vec2 p, float freq, float amp, float t, float map, float speed){
float phase = -1. * speed * freq;
float x = p.y * freq + t * phase;
float y = p.x * freq + t * .001 * phase;
float distX = cos(x+y) * amp;
float distY = sin(x-y) * amp;
vec2 distortion = vec2(distX * map, distY * map);
mat2 scale2D(vec2 _scale){
return mat2(_scale.x,0.0,
0.0,_scale.y);
}
float random (vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
float voronoi(vec2 uv){
float t = u_time;
vec2 i_st = floor(uv * 5.);
vec2 f_st = fract(uv * 5.);
float m_dist = 1.;
vec2 m_point;
for(int x = -1; x <= 1; x++){
vec2 centeredAspectRatio(vec2 uv, vec2 factor){
return ( uv - .5 ) * factor + .5;
}
vec2 centerUV(vec2 coord, vec2 coord2){
return (coord - .5 * coord2) / min(coord2.x, coord2.y);
}
mat3 gx = mat3(-1., 0., 1., -2., 0., 2., -1., 0., 1.);
mat3 gy = mat3(-1., -2., -1., 0., 0., 0., 1., 2., 1.);
float sobelEdge(sampler2D u_text,vec2 resolution, vec2 uv){
float h = 0.;
float v = 0.;
for(int y = 0; y < 3; y++){
for(int x = 0; x < 3; x++){
vec2 d = vec2(y, x) / resolution;
vec4 img = texture2D(u_text, uv + d);