Files
agame/character/player/face/face_paint.gdshader
T
2026-10-02 15:42:34 +08:00

52 lines
2.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
shader_type spatial;
// 全身 shader:body_mesh 现在只有一个材质 mat_body,所以身体底色照常取它的 BaseColor,
// 脸部**不再单独换材质**,而是在同一个 shader 里,只在指定 UV 矩形内把表情和手绘叠上去。
// 这样脸和头部走的是同一套材质、同一套光照,不会再出现独立面片的色块边界。
//
// 贴图 alpha 只表示「墨线覆盖率」,绝不写进空间 ALPHA —— body_mesh 的面片必须保持不透明。
uniform sampler2D body_tex : source_color, filter_linear, repeat_disable,
hint_default_white;
uniform vec4 body_tint : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform float surface_roughness : hint_range(0.0, 1.0) = 0.65;
uniform float surface_metallic : hint_range(0.0, 1.0) = 0.0;
// 脸部在 body_mesh 的 UV 上占的矩形。**模型重导 / 改 UV 之后必须复核这里**:
// 它要整块落在头部那块 UV 孤岛里面,否则表情会画到别的部位上。
uniform vec2 face_uv_min = vec2(0.695, 0.682);
uniform vec2 face_uv_size = vec2(0.301, 0.314);
// 头部 UV 的 v 是「越往上越大」,而贴图第 0 行在上边,所以默认要把 v 翻过来,
// 否则表情上下颠倒。
uniform bool face_uv_flip_y = true;
// 表情底图:内置表情是保留原排布、已去底的透明图集;本地表情是整张透明 PNG。
uniform sampler2D base_tex : source_color, filter_linear, repeat_disable,
hint_default_transparent;
uniform vec2 base_uv_min = vec2(0.0, 0.0);
uniform vec2 base_uv_size = vec2(1.0, 1.0);
// 玩家笔画层(透明底)
uniform sampler2D paint_tex : source_color, filter_linear, repeat_disable,
hint_default_transparent;
void fragment() {
// 全身底色都来自 mat_body 的 BaseColor,表情只是盖在脸上
vec3 albedo = texture(body_tex, UV).rgb * body_tint.rgb;
vec2 face_uv = (UV - face_uv_min) / face_uv_size;
if (face_uv_flip_y) {
face_uv.y = 1.0 - face_uv.y;
}
// 用 step 而不是 if:采样留在统一控制流里,免得 mipmap 的导数在分支里未定义
float mask = step(0.0, face_uv.x) * step(face_uv.x, 1.0)
* step(0.0, face_uv.y) * step(face_uv.y, 1.0);
vec4 base = texture(base_tex, base_uv_min + face_uv * base_uv_size);
vec4 paint = texture(paint_tex, face_uv);
// 透明处保持身体底色 —— 所以脸和周围不可能出现底色差
albedo = mix(albedo, base.rgb, base.a * mask);
albedo = mix(albedo, paint.rgb, paint.a * mask);
ALBEDO = albedo;
ROUGHNESS = surface_roughness;
METALLIC = surface_metallic;
}