56 lines
1.7 KiB
Plaintext
56 lines
1.7 KiB
Plaintext
struct VSInput {
|
|
float2 position : POSITION;
|
|
float2 uv : TEXCOORD0;
|
|
float4 color : COLOR0;
|
|
};
|
|
|
|
struct PSInput {
|
|
float4 position : SV_Position;
|
|
float2 uv : TEXCOORD0;
|
|
float4 color : COLOR0;
|
|
};
|
|
|
|
struct Constants {
|
|
matrix transform;
|
|
};
|
|
ParameterBlock<Constants> param_buffer : register(b0);
|
|
|
|
PSInput vertex_main(VSInput input) {
|
|
PSInput output;
|
|
output.position = mul(float4(input.position, 0.0f, 1.0f), param_buffer.transform);
|
|
output.uv = input.uv;
|
|
output.color = input.color;
|
|
return output;
|
|
}
|
|
|
|
Texture2D sdf_texture : register(t0);
|
|
SamplerState sdf_sampler : register(s0);
|
|
|
|
struct FontParams {
|
|
float smoothing; // 平滑度
|
|
float thickness; // 字体粗细
|
|
float outline_width; // 描边宽度
|
|
float4 outline_color; // 描边颜色
|
|
}
|
|
ParameterBlock<FontParams> font_param : register(b1);
|
|
|
|
float4 pixel_main(PSInput input) : SV_Target {
|
|
// 采样SDF纹理
|
|
float distance = sdf_texture.Sample(sdf_sampler, input.uv).r;
|
|
|
|
// 计算主要形状
|
|
float alpha = smoothstep(font_param.thickness - font_param.smoothing,
|
|
font_param.thickness + font_param.smoothing,
|
|
distance);
|
|
|
|
// 计算轮廓
|
|
float outline_alpha = smoothstep(font_param.thickness - font_param.outline_width - font_param.smoothing,
|
|
font_param.thickness - font_param.outline_width + font_param.smoothing,
|
|
distance);
|
|
|
|
// 混合主要颜色和轮廓颜色
|
|
float4 main_color = input.color * alpha;
|
|
float4 outline = font_param.outline_color * (outline_alpha - alpha);
|
|
|
|
return main_color + outline;
|
|
} |