Godot - Shader 滤镜

看粒子中的Shader


  • vignette / 镜头模糊
    通过亮暗模糊屏幕周围区域


textureLod 修改原图像的模糊量

shader_type canvas_item;
uniform sampler2D vignette;
void fragment() {
    vec3 vignette_color = texture(vignette, UV).rgb;
    COLOR.rgb = textureLod(SCREEN_TEXTURE, SCREEN_UV, (1.0 - vignette_color.r) * 4.0).rgb;
    COLOR.rgb *= texture(vignette, UV).rgb;
}
  • blur / 全屏模糊
shader_type canvas_item;
uniform float amount : hint_range(0.0, 5.0);
void fragment() {
    COLOR.rgb = textureLod(SCREEN_TEXTURE, SCREEN_UV, amount).rgb;
}
  • pixelize / 像素化
shader_type canvas_item;
uniform float size_x = 0.008;
uniform float size_y = 0.008;
void fragment() {
    vec2 uv = SCREEN_UV;
    uv -= mod(uv, vec2(size_x, size_y));
    COLOR.rgb = textureLod(SCREEN_TEXTURE, uv, 0.0).rgb;
}
  • whirl / 扭曲
shader_type canvas_item;
uniform float rotation = 3.0;
void fragment() {
    vec2 uv = SCREEN_UV;
    vec2 rel = uv - vec2(0.5, 0.5);
    float angle = length(rel) * rotation;
    mat2 rot = mat2(vec2(cos(angle), -sin(angle)), vec2(sin(angle), cos(angle)));
    rel = rot * rel;
    uv = clamp(rel + vec2(0.5,0.5), vec2(0.0, 0.0), vec2(1.0, 1.0));
    COLOR.rgb = textureLod(SCREEN_TEXTURE, uv, 0.0).rgb;
}

shader 相关操作请参考: //www.greatytc.com/p/aca68b21b049

  • sepia / 棕色

将某色彩按照图像的明度dot(c, vec3(0.33333, 0.33333, 0.33333)) 来勾勒图像

shader_type canvas_item;

uniform vec4 base : hint_color;

void fragment() {
    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
    
    //float v = max(c.r, max(c.g, c.b));
    float v = dot(c, vec3(0.33333, 0.33333, 0.33333));
    v = sqrt(v);
    //v *= v;
    COLOR.rgb = base.rgb * v;

}

  • negative / 反色/底片

反色最好理解,rgb颠倒数值

shader_type canvas_item;

void fragment() {

    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
    c = vec3(1.0) - c;
    COLOR.rgb = c;
}
  • contrasted / 对比?

将一半以上颜色反转 0~1->0~1.5-取模>0~1,让更亮的区域产生变化

shader_type canvas_item;
void fragment() {
    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
    c = mod(c + vec3(0.5), vec3(1.0));
    COLOR.rgb = c;
}
  • normalized / 标量

标量:让色彩向量的length=1

shader_type canvas_item;
void fragment() {
    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
    COLOR.rgb = normalize(c);
}
  • BCS / 亮度,对比度,饱和度

设置亮度,对比度,饱和度。dot(vec3(1.0), c.rgb) * 0.33333的写法是最简单的

shader_type canvas_item;
uniform float brightness = 0.8;
uniform float contrast = 1.5;
uniform float saturation = 1.8;
void fragment() {
    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
    c.rgb = mix(vec3(0.0), c.rgb, brightness);
    c.rgb = mix(vec3(0.5), c.rgb, contrast);
    c.rgb = mix(vec3(dot(vec3(1.0), c.rgb) * 0.33333), c.rgb, saturation);
    COLOR.rgb = c;
}
  • mirage / 扭曲

根据时间扭曲,整个图像

shader_type canvas_item;
uniform float frequency = 60;
uniform float depth = 0.005;
void fragment() {
    vec2 uv = SCREEN_UV;
    uv.x += sin(uv.y * frequency + TIME) * depth;
    uv.x = clamp(uv.x, 0.0, 1.0);
    vec3 c = textureLod(SCREEN_TEXTURE, uv, 0.0).rgb;
    COLOR.rgb = c;
}

  • old film / 老电影

混合了vignette,sepia。使用ofs算出伪随机,从grain中获得随机噪点。混合使用flashing根据时间周期的调整整个画面的敏感。

shader_type canvas_item;

uniform vec4 base : hint_color;
uniform sampler2D grain;
uniform float grain_strength = 0.3;
uniform sampler2D vignette;
uniform float fps = 12.0;
uniform float stretch = 0.5;
uniform float flashing = 0.01;

float make_grain(float time, vec2 uv) {
    vec2 ofs = vec2(sin(41.0 * time * sin(time * 123.0)), sin(27.0 * time * sin(time * 312.0)));
    return texture(grain, (uv + mod(ofs, vec2(1.0, 1.0))) * stretch).r;
}

void fragment() {
    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
    //float v = max(c.r, max(c.g, c.b));
    float v = dot(c, vec3(0.33333, 0.33333, 0.33333));
    v = sqrt(v);
    //v *= v;
    float f = 1.0 / fps;
    float g = make_grain(TIME - mod(TIME, f), UV);
    g = max(g, make_grain(TIME - mod(TIME, f) + f, UV) * 0.5);
    g = max(g, make_grain(TIME - mod(TIME, f) + f * 2.0, UV) * 0.25);
    COLOR.rgb = base.rgb * v - vec3(g) * grain_strength;
    COLOR.rgb *= texture(vignette, UV).r;
    float ft = TIME * 0.002;
    COLOR.rgb += vec3(sin(75.0 * ft * sin(ft * 123.0))) * flashing;
}

ps:这些滤镜之间是不能叠加的,想要叠加,需要在一个shader里写出来。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容