TH1/Unity/Assets/Shaders/Common/SpriteWhiteOverLay_Mat.shader
2025-07-23 00:30:51 +08:00

104 lines
3.8 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.

// Unity URP 2D Sprite White Overlay Shader
// Author: [你的名字/游戏研发专家]
Shader "TH1URPShaders/Sprite/WhiteOverlay"
{
// 在材质面板上显示的属性
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1) // 允许Sprite Renderer的颜色属性起作用
_OverlayStrength("White Overlay Strength", Range(0.0, 1.0)) = 0.5 // 白色蒙层强度
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"RenderType" = "Transparent"
"RenderPipeline" = "UniversalPipeline"
"IgnoreProjector" = "True"
}
Pass
{
// === 修正点 1: 渲染状态设置必须在HLSL代码块之前 ===
Blend SrcAlpha OneMinusSrcAlpha // 标准Alpha混合
Cull Off // 关闭背面剔除对Sprite很重要
ZWrite Off // 关闭深度写入(对透明物体很重要)
// HLSL代码块开始
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// 包含URP的核心库
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// 定义CBUFFER用于从Properties块接收数据
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST; // _MainTex的Tiling和Offset
half4 _Color; // Tint颜色
half _OverlayStrength; // 蒙层强度
CBUFFER_END
// 声明纹理
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
// 顶点着色器的输入结构
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
// 顶点着色器到片元着色器的输出结构
struct Varyings
{
float4 positionHCS : SV_POSITION;
float2 uv : TEXCOORD0;
half4 color : COLOR;
};
// 顶点着色器
Varyings vert(Attributes IN)
{
Varyings OUT;
// 将顶点位置从对象空间转换到齐次裁剪空间
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
// 转换UV坐标
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
// 将顶点颜色和材质的Tint颜色相乘
OUT.color = IN.color * _Color;
return OUT;
}
// 片元着色器 (核心逻辑在这里)
half4 frag(Varyings IN) : SV_Target
{
// 1. 从Sprite纹理采样原始颜色
half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
// 2. 应用Sprite Renderer的顶点色和材质的Tint
texColor *= IN.color;
// 3. 定义一个纯白色但alpha值使用原始纹理的alpha值
// 这样做可以保持Sprite原有的透明区域
half4 whiteOverlayColor = half4(1.0, 1.0, 1.0, texColor.a);
// 4. 使用lerp函数在原始颜色和白色之间进行插值
half4 finalColor = lerp(texColor, whiteOverlayColor, _OverlayStrength);
return finalColor;
}
// === 修正点 2: 必须使用 ENDHLSL 来结束HLSL代码块 ===
ENDHLSL
// HLSL代码块结束
}
}
}