对于代码党,或在某些情况下,更适合用代码。
var shader = ShaderMaterial.new()
var gradient = load("res://shader/gradient_texture_2d.tres")
shader.shader = load("res://shader/light.gdshader") # 加载shader
shader.set_shader_parameter("light_vector",gradient) # 设置参数
$logo.material = shader # 将shader加载到节点
shader内容,是一个划过光线的效果。与主题无关,参数是一个sampler2D(用于绑定2D纹理的采样器类型)类型。使用了GradientTexture2D左上暗,右下亮的渐变纹理,实现了扫光从左上到右下。
如果你需要从左向右(或相反),就使用左暗右亮。
shader_type canvas_item;
uniform sampler2D light_vector;
uniform float width = 0.08;
void fragment() {
vec4 color = texture(TEXTURE,UV);
if(color.a !=0.0){
float v = texture(light_vector,UV).r;
float diff = v - cos(TIME);
if(abs(diff)<width){
color = vec4(0.3,0.3,0.0,0.3) * (1.0 - abs(diff)/width) + color;
}
}
COLOR = color;
}
gradient_texture_2d.tres 文件是设置好(左上暗,右下亮)后保存的,下次就不用再设置了。
[gd_resource type="GradientTexture2D" load_steps=2 format=3 uid="uid://cxw7k8q3oie3q"]
[sub_resource type="Gradient" id="Gradient_heknt"]
[resource]
gradient = SubResource("Gradient_heknt")
fill_to = Vector2(1, 0.973118)