Pixel-Composer/shaders/sh_bevel/sh_bevel.fsh

108 lines
2.4 KiB
Text
Raw Normal View History

2022-01-13 05:24:03 +01:00
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
2023-02-23 07:02:19 +01:00
#define TAU 6.283185307179586
2022-01-13 05:24:03 +01:00
uniform vec2 dimension;
uniform vec2 scale;
uniform vec2 shift;
2023-01-25 06:49:00 +01:00
uniform int slope;
2023-02-28 09:43:01 +01:00
uniform int sampleMode;
2022-01-13 05:24:03 +01:00
2023-12-23 09:39:55 +01:00
uniform vec2 height;
uniform int heightUseSurf;
uniform sampler2D heightSurf;
float bright(in vec4 col) { return (col.r + col.g + col.b) / 3. * col.a; }
2023-01-17 08:11:55 +01:00
2023-12-23 09:39:55 +01:00
vec4 sampleTexture(vec2 pos) { #region
2023-02-28 09:43:01 +01:00
if(pos.x >= 0. && pos.y >= 0. && pos.x <= 1. && pos.y <= 1.)
return texture2D(gm_BaseTexture, pos);
if(sampleMode == 0)
return vec4(0.);
if(sampleMode == 1)
return texture2D(gm_BaseTexture, clamp(pos, 0., 1.));
if(sampleMode == 2)
return texture2D(gm_BaseTexture, fract(pos));
return vec4(0.);
2023-12-23 09:39:55 +01:00
} #endregion
2023-02-28 09:43:01 +01:00
2022-01-13 05:24:03 +01:00
void main() {
2023-12-23 09:39:55 +01:00
float hei = height.x;
float heiMax = max(height.x, height.y);
if(heightUseSurf == 1) {
vec4 _vMap = texture2D( heightSurf, v_vTexcoord );
hei = mix(height.x, height.y, (_vMap.r + _vMap.g + _vMap.b) / 3.);
}
2022-01-13 05:24:03 +01:00
vec2 pixelStep = 1. / dimension;
2023-02-23 07:02:19 +01:00
2022-01-13 05:24:03 +01:00
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-12-23 09:39:55 +01:00
vec2 shiftPx = -shift / dimension;
float b0 = bright(col);
2023-03-28 06:58:28 +02:00
float shift_angle = atan(shiftPx.y, shiftPx.x);
float shift_distance = length(shiftPx);
2023-12-23 09:39:55 +01:00
float slope_distance = hei * b0;
float max_distance = hei;
2023-01-17 08:11:55 +01:00
if(b0 == 0.) return;
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
float b1 = b0;
2023-02-23 07:02:19 +01:00
float added_distance, _b1;
2023-01-17 08:11:55 +01:00
vec2 shf, pxs;
2023-12-23 09:39:55 +01:00
for(float i = 1.; i < heiMax; i++) {
if(i >= hei) break;
2023-02-23 07:02:19 +01:00
float base = 1.;
float top = 0.;
for(float j = 0.; j <= 64.; j++) {
float ang = top / base * TAU;
top += 2.;
if(top >= base) {
top = 1.;
base *= 2.;
}
2023-01-17 08:11:55 +01:00
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-02-28 09:43:01 +01:00
col1 = sampleTexture( pxs );
_b1 = bright(col1);
2023-01-17 08:11:55 +01:00
if(_b1 < b1) {
slope_distance = min(slope_distance, i);
2023-12-23 09:39:55 +01:00
max_distance = min(max_distance, (b0 - _b1) * hei);
2023-01-17 08:11:55 +01:00
b1 = min(b1, _b1);
2023-12-23 09:39:55 +01:00
i = hei;
2023-01-17 08:11:55 +01:00
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 {
2023-01-25 06:49:00 +01:00
float mx = slope_distance / max_distance;
if(slope == 1) mx = pow(mx, 3.) + 3. * mx * mx * (1. - mx);
else if(slope == 2) mx = sqrt(1. - pow(mx - 1., 2.));
mx = clamp(mx, 0., 1.);
float prg = mix(b1, b0, mx);
gl_FragColor = vec4(vec3(prg), col.a);
2022-01-13 05:24:03 +01:00
}
}