123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- Shader "Custom/GammaSpaceAlphaBlend"
- {
- Properties
- {
- _MainTex ("Base Texture", 2D) = "white" {}
- _AlphaTex ("Alpha Texture", 2D) = "white" {} // 透明度纹理
- _Color ("Color", Color) = (1,1,1,1) // 输入的颜色
- }
- SubShader
- {
- Tags { "RenderType"="Opaque" }
- Pass
- {
- // 使用 alpha blend 模式
- Blend SrcAlpha OneMinusSrcAlpha
- ZWrite On
- Cull Front
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- struct appdata_t
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- };
- struct v2f
- {
- float4 pos : SV_POSITION;
- float2 uv : TEXCOORD0;
- };
- // Shader 需要的属性
- sampler2D _MainTex;
- sampler2D _AlphaTex;
- float4 _Color; // 混合时使用的颜色
- // Gamma -> Linear 转换
- float GammaToLinear(float c)
- {
- return pow(c, 2.2); // 伽马到线性空间的转换
- }
- // Linear -> Gamma 转换
- float GammaToGammaSpace(float c)
- {
- return pow(c, 1.0 / 2.2); // 线性到伽马空间的转换
- }
- // 顶点着色器
- v2f vert(appdata_t v)
- {
- v2f o;
- o.pos = UnityObjectToClipPos(v.vertex);
- o.uv = v.uv;
- return o;
- }
- // 片段着色器
- float4 frag(v2f i) : SV_Target
- {
- // 获取基础纹理颜色和透明度纹理
- float3 baseColor = tex2D(_MainTex, i.uv).rgb;
- float alpha = tex2D(_AlphaTex, i.uv).r;
- // 伽马 -> 线性 转换
- float3 linearBaseColor = GammaToLinear(baseColor);
- float linearAlpha = GammaToLinear(alpha);
- // 混合颜色和透明度(线性空间)
- float3 blendedColor = lerp(linearBaseColor, _Color.rgb, linearAlpha);
- // 线性 -> 伽马 转换
- blendedColor = GammaToGammaSpace(blendedColor);
- // 返回最终的颜色(带有透明度)
- return float4(blendedColor, 1.0);
- }
- ENDCG
- }
- }
- FallBack "Diffuse"
- }
|