Godot着色器案例
案例列表
灰度图像
使用Luma公式
代码
gdshader
shader_type canvas_item;
void fragment() {
vec4 sampled_color = texture(screen_tex, SCREEN_UV);
// 灰度
float brightness = sampled_color.r * 0.299 + sampled_color.g * 0.587 + sampled_color.b * 0.144;
COLOR.rgb = vec3(brightness);
}
溶解
代码
gdshader
shader_type canvas_item;
uniform sampler2D noise;
uniform float step_width: hint_range(0.0, 1.0, 0.05) = 1.0;
void fragment() {
vec4 noise_color = texture(noise, UV);
vec4 current_color = texture(TEXTURE, UV);
COLOR.a = step(noise_color.r, step_width) * current_color.a;
//COLOR.a = step(noise_color.r, (cos(TIME) + 1.0) / 2.0) * current_color.a;
}
圆环
理论
代码
gdshader
shader_type canvas_item;
void vertex() {
}
void fragment() {
// l是当前uv距离圆心的距离
float l = length(UV - vec2(0.5));
// 如果只需要虚化的圆,则可以只计算一个值,并且a应大于b
// float s = smoothstep(0.5, 0.3, l);
// COLOR = vec4(s);
// 计算出距离圆心l位置的颜色,使用float存储
float s1 = smoothstep(0.2, 0.3, l);
float s2 = smoothstep(0.3, 0.4, l);
// 设置颜色,因为光环为白色,所以rgb颜色一致
// 如果需要设置alpha,则可以使用任意一个通道的值
COLOR = vec4(vec3(s1 - s2), vec3(s1 - s2).r);
}