Pixel-Composer/shaders/sh_camera/sh_camera.fsh
2024-10-24 17:40:14 +07:00

83 lines
2.4 KiB
GLSL

#pragma use(sampler_simple)
#region -- sampler_simple -- [1729740692.1417658]
uniform int sampleMode;
vec4 sampleTexture( sampler2D texture, vec2 pos) {
if(pos.x >= 0. && pos.y >= 0. && pos.x <= 1. && pos.y <= 1.)
return texture2D(texture, pos);
if(sampleMode <= 1) return vec4(0.);
else if(sampleMode == 2) return texture2D(texture, clamp(pos, 0., 1.));
else if(sampleMode == 3) return texture2D(texture, fract(pos));
else if(sampleMode == 4) return vec4(vec3(0.), 1.);
return vec4(0.);
}
#endregion -- sampler_simple --
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform sampler2D backg;
uniform sampler2D scene;
uniform vec2 scnDimension;
uniform vec2 camDimension;
uniform vec2 position;
uniform float zoom;
uniform float bokehStrength;
const float GoldenAngle = 2.39996323;
const float Iterations = 400.0;
const float ContrastAmount = 150.0;
const vec3 ContrastFactor = vec3(9.0);
const float Smooth = 2.0;
vec4 bokeh(sampler2D tex, vec2 uv, float radius) { #region ref. sh_blur_bokeh
vec3 num, weight;
float alpha = 0.;
float rec = 1.0; // reciprocal
vec2 horizontalAngle = vec2(0.0, radius * 0.01 / sqrt(Iterations));
vec2 aspect = vec2(scnDimension.y / scnDimension.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;
vec2 offset = (rec - 1.0) * horizontalAngle;
vec2 sampleUV = uv + aspect * offset;
vec4 sam = sampleTexture( tex, sampleUV );
vec3 col = sam.rgb * sam.a;
// increase contrast and smooth
vec3 bokeh = Smooth + pow(col, ContrastFactor) * ContrastAmount;
num += col * bokeh;
alpha += sam.a * (bokeh.r + bokeh.g + bokeh.b) / 3.;
weight += bokeh;
}
float _a = alpha / ((weight.r + weight.g + weight.b) / 3.);
return vec4(num / weight, pow(_a, 3.));
} #endregion
void main() { #region
vec2 pos = position + (v_vTexcoord - vec2(.5)) * (camDimension / scnDimension) * zoom;
vec4 _col0 = sampleTexture( backg, v_vTexcoord );
vec4 _col1 = bokeh( scene, pos, bokehStrength );
float al = _col1.a + _col0.a * (1. - _col1.a);
vec4 res = _col0 * _col0.a * (1. - _col1.a) + _col1 * _col1.a;
res /= al;
res.a = al;
gl_FragColor = res;
} #endregion