shader_type spatial;
render_mode cull_back, depth_draw_opaque, diffuse_burley, specular_schlick_ggx;
/// 主纹理(模型原本的颜色)
uniform sampler2D albedo_tex : source_color;
/// 噪声贴图(黑白云纹、噪点图都行)
uniform sampler2D noise_tex : source_color;
/// 溶解进度:0 = 完整,1 = 完全消失
uniform float dissolve_amount : hint_range(0.0, 1.0) = 0.0;
/// 溶解边缘宽度
uniform float edge_width : hint_range(0.0, 0.5) = 0.0;
/// 边缘颜色
uniform vec3 edge_color : source_color = vec3(1.0, 0.6, 0.1);
/// 边缘发光强度
uniform float edge_emission_strength : hint_range(0.0, 10.0) = 10.0;
void fragment() {
vec2 uv = UV;
// 读取贴图
vec4 base_col = texture(albedo_tex, uv);
float noise_v = texture(noise_tex, uv).r;
float threshold = dissolve_amount;
// 溶解掉的部分:直接丢弃像素(挖洞,不是变透明)
if (noise_v < threshold - edge_width) {
discard;
}
ALBEDO = base_col.rgb;
// ---------- 溶解边缘 ----------
float edge_mask = smoothstep(threshold - edge_width, threshold, noise_v)
* (1.0 - smoothstep(threshold, threshold + edge_width, noise_v));
if (edge_mask > 0.0) {
ALBEDO = mix(ALBEDO, edge_color, edge_mask);
EMISSION = edge_color * edge_emission_strength * edge_mask;
}
}实现物体渐渐溶解的着色器,需要一张噪声图才可以使用。演示如图: