-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshaders.mjs
135 lines (118 loc) · 3.37 KB
/
shaders.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// this is just for code highlighting in VSCode
// via the glsl-literal extension
const glsl = x => x;
export const frag = glsl`
precision highp float;
uniform float time;
uniform float width;
uniform float height;
const float PI = 3.141592654;
const float DEG = PI / 180.0;
vec2 coords() {
float vmin = min(width, height);
return vec2((gl_FragCoord.x - width * .5) / vmin,
(gl_FragCoord.y - height * .5) / vmin);
}
vec2 rotate(vec2 p, float a) {
return vec2(p.x * cos(a) - p.y * sin(a),
p.x * sin(a) + p.y * cos(a));
}
vec2 repeat(in vec2 p, in vec2 c) {
return mod(p, c) - 0.5 * c;
}
// Distance functions by Inigo Quilez
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float circle(in vec2 p, in vec2 pos, float radius) {
return length(p - pos) - radius;
}
float box(in vec2 p, in vec2 pos, in vec2 b) {
vec2 d = abs(p - pos) - b;
return length(max(d, vec2(0))) + min(max(d.x, d.y), 0.0);
}
float triangle(in vec2 p, in float h) {
const float k = sqrt(3.0);
p.x = abs(p.x) - h;
p.y = p.y + h / k;
if( p.x + k*p.y > 0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0;
p.x -= clamp( p.x, -2.0, 0.0 );
return -length(p)*sign(p.y);
}
float hexagon(in vec2 p, in vec2 pos, in float r) {
const vec3 k = vec3(-0.866025404,0.5,0.577350269);
p = abs(p - pos);
p -= 2.0*min(dot(k.xy,p),0.0)*k.xy;
p -= vec2(clamp(p.x, -k.z*r, k.z*r), r);
return length(p) * sign(p.y);
}
float hexagram(in vec2 p, in vec2 pos, in float r) {
const vec4 k=vec4(-0.5,0.8660254038,0.5773502692,1.7320508076);
p = abs(p - pos);
p -= 2.0*min(dot(k.xy,p),0.0)*k.xy;
p -= 2.0*min(dot(k.yx,p),0.0)*k.yx;
p -= vec2(clamp(p.x,r*k.z,r*k.w),r);
return length(p)*sign(p.y);
}
float distanceField(vec2 p) {
float hexa = hexagon(p, vec2(0, 0), 1.5);
float star = hexagram(p, vec2(0, 0), .5);
float x = (1.0 + sin(time * 0.5)) * .5;
return mix(hexa, star, x);
}
// by @mattdesl
float hue2rgb(float f1, float f2, float hue) {
if (hue < 0.0)
hue += 1.0;
else if (hue > 1.0)
hue -= 1.0;
float res;
if ((6.0 * hue) < 1.0)
res = f1 + (f2 - f1) * 6.0 * hue;
else if ((2.0 * hue) < 1.0)
res = f2;
else if ((3.0 * hue) < 2.0)
res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
else
res = f1;
return res;
}
vec3 hsl2rgb(vec3 hsl) {
vec3 rgb;
if (hsl.y == 0.0) {
rgb = vec3(hsl.z); // Luminance
} else {
float f2;
if (hsl.z < 0.5)
f2 = hsl.z * (1.0 + hsl.y);
else
f2 = hsl.z + hsl.y - hsl.y * hsl.z;
float f1 = 2.0 * hsl.z - f2;
rgb.r = hue2rgb(f1, f2, hsl.x + (1.0/3.0));
rgb.g = hue2rgb(f1, f2, hsl.x);
rgb.b = hue2rgb(f1, f2, hsl.x - (1.0/3.0));
}
return rgb;
}
vec3 Stars(in vec2 p, in vec3 background) {
float sdf = distanceField(p);
float hue = floor(10.0 * (1.0 + 0.98 * cos(time * .125 + sdf))) / 10.0;
vec3 fill = hsl2rgb(vec3(hue, 1.0, .5));
//vec3 fill = vec3(1.0, clamp(cos(time * 2.0 - sdf * 20.0) * .5, 0.0, .25) * 4.0, 0.0);
return sdf < 0.0 ? fill : background;
}
void main () {
vec2 p0 = coords();
float zoom = 4.0;
vec2 p1 = rotate(p0 * zoom, 45.0 * DEG);
vec2 p2 = rotate(repeat(p1, vec2(1.75, 2.0)), 0.0 * DEG);
vec3 background = vec3(0, 0, 0);
vec3 col = Stars(p2, background);
gl_FragColor = vec4(col, 1.0);
}
`
export const vert = glsl`
precision mediump float;
attribute vec2 position;
void main () {
gl_Position = vec4(position, 0, 1.0);
}
`