Files
nunuhara_xsystem4/shaders/effects/blur_crossfade.f.glsl
T
Nunuhara Cabbage 35a84ba708 Implement BLUR_CROSSFADE effect
Used in Shaman's Sanctuary.

This implementation gives a bit of a different result compared to
SACT2.dll, but it's reasonably close.
2021-07-08 20:07:43 -07:00

71 lines
2.4 KiB
GLSL

/* Copyright (C) 2019 Nunuhara Cabbage <nunuhara@haniwa.technology>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://gnu.org/licenses/>.
*/
uniform sampler2D tex; // the new scene
uniform sampler2D old; // the old scene
uniform vec2 resolution; // the screen resolution
uniform float progress; // effect progress (0..1)
in vec2 tex_coord;
out vec4 frag_color;
const float PI = 3.14159265;
const vec2 tex_offset = vec2(1.0, 1.0);
const float sigma = 50.0;
vec3 get_pixel(vec2 xy) {
return mix(texture(old, xy).rgb, texture(tex, xy).rgb, progress);
}
// incremental gaussian blur algorithm
void main() {
vec2 p = tex_coord * resolution;
float blur_size = (1.0 - abs(progress * 2.0 - 1.0)) * 100.0; // (0..0.5..1) -> (0..50..0)
float blur_half = blur_size / 2.0;
vec3 gauss;
gauss.x = 1.0 / (sqrt(2.0 * PI) * sigma);
gauss.y = exp(-0.5 / (sigma * sigma));
gauss.z = gauss.y * gauss.y;
vec3 avg = vec3(0.0);
float coefficient_sum = 0.0;
avg += get_pixel(p / resolution) * gauss.x;
coefficient_sum += gauss.x;
gauss.xy *= gauss.yz;
vec2 tex_offset = vec2(0.25, 0.75);
for (float i = 1.0; i <= blur_half; i += 2.0) {
avg += get_pixel((p - i * tex_offset) / resolution) * gauss.x;
avg += get_pixel((p + i * tex_offset) / resolution) * gauss.x;
coefficient_sum += 2.0 * gauss.x;
gauss.xy *= gauss.yz;
}
tex_offset = vec2(0.75, 0.25);
for (float i = 1.0; i <= blur_half; i += 2.0) {
avg += get_pixel((p - i * tex_offset) / resolution) * gauss.x;
avg += get_pixel((p + i * tex_offset) / resolution) * gauss.x;
coefficient_sum += 2.0 * gauss.x;
gauss.xy *= gauss.yz;
}
frag_color = vec4(avg / coefficient_sum, 1.0);
}