Pixel-Composer/shaders/sh_bevel/sh_bevel.fsh

75 lines
1.7 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform vec2 dimension;
uniform vec2 scale;
uniform vec2 shift;
uniform float height;
#define TAU 6.28318
float bright(in vec4 col) {
return dot(col.rgb, vec3(0.2126, 0.7152, 0.0722)) * col.a;
}
2023-01-17 08:11:55 +01:00
2022-01-13 05:24:03 +01:00
void main() {
vec2 pixelStep = 1. / dimension;
float tauDiv = TAU / 32.;
vec4 col = texture2D(gm_BaseTexture, v_vTexcoord);
2023-01-17 08:11:55 +01:00
vec4 col1;
2022-01-13 05:24:03 +01:00
gl_FragColor = col;
bool done = false;
2023-01-17 08:11:55 +01:00
float b0 = bright(col);
2022-01-13 05:24:03 +01:00
float shift_angle = atan(shift.y, shift.x);
float shift_distance = length(shift);
2023-01-17 08:11:55 +01:00
float slope_distance = height * b0;
float max_distance = height;
if(b0 == 0.) return;
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
float b1 = b0;
float ang, added_distance, _b1;
vec2 shf, pxs;
for(float i = 1.; i < 16.; i++) {
if(i >= height) break;
for(float j = 0.; j < 32.; j++) {
ang = j * tauDiv;
added_distance = 1. + cos(abs(shift_angle - ang)) * shift_distance;
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
shf = vec2( cos(ang), sin(ang)) * (i * added_distance) / scale;
pxs = v_vTexcoord + shf * pixelStep;
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
if(pxs.x < 0. || pxs.y < 0. || pxs.x > 1. || pxs.y > 1.)
_b1 = 0.;
else {
col1 = texture2D( gm_BaseTexture, pxs );
_b1 = bright(col1);
}
if(_b1 < b1) {
slope_distance = min(slope_distance, i);
max_distance = min(max_distance, (b0 - _b1) * height);
b1 = min(b1, _b1);
i = height;
break;
2022-01-13 05:24:03 +01:00
}
}
2023-01-17 08:11:55 +01:00
}
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
if(max_distance == 0.)
gl_FragColor = vec4(vec3(b0), col.a);
else {
float sl = clamp(mix(b1, b0, slope_distance / max_distance), 0., 1.);
gl_FragColor = vec4(vec3(sl), col.a);
//gl_FragColor = vec4(slope_distance / height, max_distance / height, sl, col.a);
//gl_FragColor = vec4(b0, b1, sl, col.a);
2022-01-13 05:24:03 +01:00
}
}