Skip to content

Commit f916be7

Browse files
committed
distance fractal shader
1 parent e3ddee3 commit f916be7

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

src/logic/pool/Shader.tsx

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ vec2 pos = ((gl_FragCoord.xy + position / escale) / resol.xy + vec2(-0.5)) * esc
5858
vec2 zi = pos;
5959
for (int i = 0; i < MAX_ITS; i++) {
6060
if (i == max_its) {break;}
61-
zi = zi - cmul(cdiv(cpow(zi, n) - vec2(100., 0.), float(n) * cpow(zi, n-1)), point_a);
61+
zi = zi - cmul(cdiv(cpow(zi, n) - vec2(100., 0.), float(n) * cpow(zi, n-1)), point_a) + point_a;
6262
// zi = zi - cmul(cdiv(sin(zi) - vec2(1., 0.), cos(zi)), point_a);
6363
}
6464
@@ -90,5 +90,99 @@ gl_FragColor = vec4(color / 255., 1);
9090
// }
9191
// if (colors[0].x == 0.) {gl_FragColor = vec4(0.2, 0.4, 0.6, 1);}
9292
93+
}
94+
`;
95+
96+
export const Alter = `
97+
98+
precision highp float;
99+
100+
uniform vec2 resol;
101+
uniform int n;
102+
uniform float scale;
103+
uniform float roots[12];
104+
uniform vec2 position;
105+
uniform vec3 colors[12];
106+
uniform int max_its;
107+
uniform vec2 point_a;
108+
109+
const int MAX_ITS = 300;
110+
111+
float sigm(float x) {
112+
return 1. / (1. + exp(-abs(x / 10.)+2.));
113+
}
114+
115+
vec2 cmul(vec2 x, vec2 y) {
116+
return vec2(x.x * y.x - x.y * y.y, x.x * y.y + x.y * y.x);
117+
}
118+
119+
vec2 cdiv(vec2 x, vec2 y) {
120+
float d = (y.x * y.x + y.y * y.y);
121+
return vec2((x.x * y.x + x.y * y.y) / d, (y.x * x.y - x.x * y.y) / d);
122+
123+
}
124+
125+
vec2 cpow(vec2 x, int p) {
126+
vec2 res = vec2(1, 0);
127+
for (int i = 0; i <= MAX_ITS; i++) {
128+
if (i == p) {break;}
129+
res = cmul(res, x);
130+
}
131+
return res;
132+
}
133+
134+
vec2 transform(float phi) {
135+
return vec2(cos(phi), sin(phi));
136+
}
137+
138+
float cdist(vec2 x, vec2 y) {
139+
return length((x-y));
140+
}
141+
142+
void main( void ) {
143+
144+
float escale = exp(scale - 1.);
145+
vec2 pos = ((gl_FragCoord.xy + position / escale) / resol.xy + vec2(-0.5)) * escale;
146+
147+
148+
vec2 zi = pos;
149+
for (int i = 0; i < MAX_ITS; i++) {
150+
if (i == max_its) {break;}
151+
zi = zi - cmul(cdiv(cpow(zi, n) - vec2(100., 0.), float(n) * cpow(zi, n-1)), point_a);
152+
// zi = zi - cmul(cdiv(sin(zi) - vec2(1., 0.), cos(zi)), point_a);
153+
}
154+
155+
int mi = 0;
156+
float md = cdist(transform(roots[0]), zi);
157+
for (int i = 0; i < MAX_ITS; i++) {
158+
if (i == n) { break; }
159+
160+
vec2 point = transform(roots[i]);
161+
162+
float dst = cdist(point, zi);
163+
164+
if (dst < md) {
165+
md = dst;
166+
mi = i;
167+
}
168+
}
169+
vec3 color = vec3(1.0);
170+
for (int i = 0; i < 12; i++) {
171+
if (i == mi) {
172+
color = colors[i];
173+
break;
174+
}
175+
}
176+
float dst = cdist(zi, pos + md );
177+
// float dst = md;
178+
color = vec3(sigm(dst));
179+
180+
// if (pos.x < 0.1 && pos.x > -0.1 && pos.y < 0.1 && pos.y > -0.1) {
181+
// gl_FragColor = vec4(vec3(1.,0.,0.), 1);
182+
// } else {
183+
gl_FragColor = vec4(color, 1.);
184+
// }
185+
if (colors[0].x == 0.) {gl_FragColor = vec4(0.2, 0.4, 0.6, 1);}
186+
93187
}
94188
`;

0 commit comments

Comments
 (0)