111 lines
3.5 KiB
Plaintext
111 lines
3.5 KiB
Plaintext
// =============================================================================
|
||
// sprite_shader.slang - 使用全局变量的精灵着色器
|
||
// =============================================================================
|
||
|
||
#include "common_2d.slang"
|
||
|
||
// 自动生成标准顶点着色器
|
||
STANDARD_VERTEX_SHADER(vertexMain)
|
||
|
||
// 简单的像素着色器
|
||
STANDARD_PIXEL_SHADER(pixelMain, {
|
||
// 这里可以添加自定义代码
|
||
// color 变量已经包含了采样后的纹理颜色
|
||
})
|
||
|
||
// 带发光效果的像素着色器
|
||
DECLARE_PIXEL_GLOBALS
|
||
DECLARE_STANDARD_TEXTURES
|
||
|
||
[shader("fragment")]
|
||
float4 pixelMainGlow(VertexShaderOutput input) : SV_Target {
|
||
float4 color = g_mainTexture.Sample(g_linearSampler, input.texCoord);
|
||
color *= input.color;
|
||
|
||
// 发光效果
|
||
if (g_effect.glowSize > 0.0) {
|
||
float2 texelSize = g_scene.invScreenSize;
|
||
float4 glow = float4(0.0);
|
||
|
||
// 简单的发光采样
|
||
for (int y = -2; y <= 2; y++) {
|
||
for (int x = -2; x <= 2; x++) {
|
||
float2 offset = float2(x, y) * texelSize * g_effect.glowSize;
|
||
float4 sample = g_mainTexture.Sample(g_linearSampler, input.texCoord + offset);
|
||
float weight = 1.0 / (1.0 + length(float2(x, y)));
|
||
glow += sample * weight;
|
||
}
|
||
}
|
||
|
||
glow *= g_effect.glowColor;
|
||
color.rgb += glow.rgb * g_effect.glowColor.a;
|
||
}
|
||
|
||
// 应用标准材质参数
|
||
color.rgb = adjustBrightness(color.rgb, g_material.brightness);
|
||
color.rgb = adjustContrast(color.rgb, g_material.contrast);
|
||
color.rgb = adjustSaturation(color.rgb, g_material.saturation);
|
||
color.a *= g_material.opacity;
|
||
|
||
return color;
|
||
}
|
||
|
||
// 文字渲染着色器
|
||
DECLARE_PIXEL_GLOBALS
|
||
DECLARE_STANDARD_TEXTURES
|
||
|
||
[shader("fragment")]
|
||
float4 pixelMainText(VertexShaderOutput input) : SV_Target {
|
||
// 文字通常存储在alpha通道
|
||
float alpha = g_mainTexture.Sample(g_linearSampler, input.texCoord).a;
|
||
|
||
// SDF文字渲染
|
||
float distance = alpha;
|
||
float smoothing = fwidth(distance) * 0.75;
|
||
alpha = smootherstep(0.5 - smoothing, 0.5 + smoothing, distance);
|
||
|
||
// 应用颜色
|
||
float4 color = float4(input.color.rgb, input.color.a * alpha);
|
||
|
||
// 文字阴影
|
||
if (g_effect.shadowDistance > 0.0) {
|
||
float2 shadowOffset = float2(
|
||
cos(g_effect.shadowAngle),
|
||
sin(g_effect.shadowAngle)
|
||
) * g_effect.shadowDistance * g_scene.invScreenSize;
|
||
|
||
float shadowAlpha = g_mainTexture.Sample(g_linearSampler, input.texCoord - shadowOffset).a;
|
||
shadowAlpha = smootherstep(0.5 - smoothing * g_effect.shadowSoftness, 0.5 + smoothing * g_effect.shadowSoftness, shadowAlpha);
|
||
|
||
// 混合阴影
|
||
color.rgb = lerp(float3(0.0, 0.0, 0.0), color.rgb, alpha);
|
||
color.a = max(color.a, shadowAlpha * 0.5);
|
||
}
|
||
|
||
return color;
|
||
}
|
||
|
||
// 形状渲染着色器(使用SDF)
|
||
DECLARE_PIXEL_GLOBALS
|
||
|
||
[shader("fragment")]
|
||
float4 pixelMainShape(VertexShaderOutput input) : SV_Target {
|
||
float2 p = input.texCoord * 2.0 - 1.0; // 转换到[-1, 1]空间
|
||
|
||
// 圆角矩形SDF
|
||
float d = sdRoundedBox(p, float2(0.8, 0.6), 0.1);
|
||
|
||
// 抗锯齿边缘
|
||
float alpha = 1.0 - smoothstep(-0.01, 0.01, d);
|
||
|
||
// 渐变填充
|
||
float gradient = (p.y + 1.0) * 0.5;
|
||
float3 color = lerp(
|
||
g_effect.gradientColors[0].rgb,
|
||
g_effect.gradientColors[1].rgb,
|
||
gradient
|
||
);
|
||
|
||
return float4(color * input.color.rgb, alpha * input.color.a * g_material.opacity);
|
||
}
|