Pixel-Composer/shaders/sh_blur_bokeh/sh_blur_bokeh.fsh

61 lines
1.6 KiB
Plaintext
Raw Normal View History

2023-01-25 06:49:00 +01:00
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform vec2 dimension;
2023-12-23 09:39:55 +01:00
uniform vec2 strength;
uniform int strengthUseSurf;
uniform sampler2D strengthSurf;
2023-01-25 06:49:00 +01:00
const float GoldenAngle = 2.39996323;
2023-12-23 09:39:55 +01:00
const float Iterations = 400.0;
2023-01-25 06:49:00 +01:00
const float ContrastAmount = 150.0;
2023-12-23 09:39:55 +01:00
const vec3 ContrastFactor = vec3(9.0);
2023-01-25 06:49:00 +01:00
const float Smooth = 2.0;
2023-07-11 20:36:44 +02:00
vec4 bokeh(sampler2D tex, vec2 uv, float radius) {
2023-01-25 06:49:00 +01:00
vec3 num, weight;
2023-07-11 20:36:44 +02:00
float alpha = 0.;
2023-01-25 06:49:00 +01:00
float rec = 1.0; // reciprocal
vec2 horizontalAngle = vec2(0.0, radius * 0.01 / sqrt(Iterations));
vec2 aspect = vec2(dimension.y / dimension.x, 1.0);
mat2 Rotation = mat2(
cos(GoldenAngle), sin(GoldenAngle),
-sin(GoldenAngle), cos(GoldenAngle)
);
for (float i; i < Iterations; i++) {
rec += 1.0 / rec;
horizontalAngle = horizontalAngle * Rotation;
2023-07-11 20:36:44 +02:00
vec2 offset = (rec - 1.0) * horizontalAngle;
2023-01-25 06:49:00 +01:00
vec2 sampleUV = uv + aspect * offset;
2023-07-11 20:36:44 +02:00
vec4 sam = texture2D(tex, sampleUV);
vec3 col = sam.rgb * sam.a;
2023-01-25 06:49:00 +01:00
// increase contrast and smooth
vec3 bokeh = Smooth + pow(col, ContrastFactor) * ContrastAmount;
2023-07-11 20:36:44 +02:00
num += col * bokeh;
alpha += sam.a * (bokeh.r + bokeh.g + bokeh.b) / 3.;
weight += bokeh;
2023-01-25 06:49:00 +01:00
}
2023-07-11 20:36:44 +02:00
return vec4(num / weight, alpha / ((weight.r + weight.g + weight.b) / 3.));
2023-01-25 06:49:00 +01:00
}
void main() {
2023-12-23 09:39:55 +01:00
float str = strength.x;
if(strengthUseSurf == 1) {
vec4 _vMap = texture2D( strengthSurf, v_vTexcoord );
str = mix(strength.x, strength.y, (_vMap.r + _vMap.g + _vMap.b) / 3.);
}
gl_FragColor = bokeh(gm_BaseTexture, v_vTexcoord, str);
2023-01-25 06:49:00 +01:00
}