Pixel-Composer/shaders/sh_camera/sh_camera.fsh

84 lines
2.4 KiB
Plaintext
Raw Normal View History

2024-10-24 12:40:14 +02:00
#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 --
2023-01-17 08:11:55 +01:00
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
2023-01-25 06:49:00 +01:00
uniform sampler2D backg;
2023-01-17 08:11:55 +01:00
uniform sampler2D scene;
uniform vec2 scnDimension;
uniform vec2 camDimension;
2023-07-15 20:01:29 +02:00
2024-04-03 09:40:37 +02:00
uniform vec2 position;
2023-01-17 08:11:55 +01:00
uniform float zoom;
2023-07-11 20:36:44 +02:00
uniform float bokehStrength;
const float GoldenAngle = 2.39996323;
const float Iterations = 400.0;
const float ContrastAmount = 150.0;
2024-04-03 09:40:37 +02:00
const vec3 ContrastFactor = vec3(9.0);
2023-07-11 20:36:44 +02:00
const float Smooth = 2.0;
2023-01-17 08:11:55 +01:00
2024-04-03 09:40:37 +02:00
vec4 bokeh(sampler2D tex, vec2 uv, float radius) { #region ref. sh_blur_bokeh
vec3 num, weight;
2023-07-11 20:36:44 +02:00
float alpha = 0.;
2024-04-03 09:40:37 +02:00
float rec = 1.0; // reciprocal
vec2 horizontalAngle = vec2(0.0, radius * 0.01 / sqrt(Iterations));
vec2 aspect = vec2(scnDimension.y / scnDimension.x, 1.0);
2023-07-11 20:36:44 +02:00
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;
2024-10-24 12:40:14 +02:00
vec4 sam = sampleTexture( tex, sampleUV );
2023-07-11 20:36:44 +02:00
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;
}
2024-04-03 09:40:37 +02:00
float _a = alpha / ((weight.r + weight.g + weight.b) / 3.);
return vec4(num / weight, pow(_a, 3.));
} #endregion
2023-07-11 20:36:44 +02:00
2024-04-03 09:40:37 +02:00
void main() { #region
2023-01-17 08:11:55 +01:00
vec2 pos = position + (v_vTexcoord - vec2(.5)) * (camDimension / scnDimension) * zoom;
2024-04-03 09:40:37 +02:00
vec4 _col0 = sampleTexture( backg, v_vTexcoord );
2023-07-11 20:36:44 +02:00
vec4 _col1 = bokeh( scene, pos, bokehStrength );
2023-01-25 06:49:00 +01:00
2023-07-11 20:36:44 +02:00
float al = _col1.a + _col0.a * (1. - _col1.a);
vec4 res = _col0 * _col0.a * (1. - _col1.a) + _col1 * _col1.a;
2024-04-03 09:40:37 +02:00
res /= al;
res.a = al;
2023-01-25 06:49:00 +01:00
gl_FragColor = res;
2024-04-03 09:40:37 +02:00
} #endregion