# 【Shader练习-崇高牺牲】

曾经经常玩的游戏《炉石传说》里的圣骑士奥秘卡“崇高牺牲”动态效果的模拟实现。

Markdown 图片

全屏显示

包括了以下练习内容:

  1. UV扰动
  2. 闪烁
  3. 遮罩

Markdown 图片

Shader "Examples/GoldCard"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _NoiseMask ("NoiseMask", 2D) = "white" {}
        _Noise ("Noise", 2D) = "white" {}
        _Flash ("Flash", 2D) = "white" {}
    }

    SubShader
    {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0

            uniform sampler2D _MainTex;
            uniform sampler2D _NoiseMask;
            uniform sampler2D _Noise;
            uniform sampler2D _Flash;

            struct VertexInput {
                float4 vertex : POSITION;
                float4 uv : TEXCOORD0;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.pos = UnityObjectToClipPos( v.vertex );
                o.uv = v.uv;
                o.uv2 = v.uv - float2(frac(_Time.y * 0.5),frac(_Time.y * 0.1));
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                // 采样噪声的遮罩
                float3 var_NoiseMask = tex2D(_NoiseMask,i.uv);
                // 采用噪声
                float3 var_NoiseTex = tex2D(_Noise,i.uv2);
                // 采样闪烁的光的高亮部分
                float3 var_Flash = tex2D(_Flash,i.uv) * 1.1-var_NoiseMask;

                // 进行扰动uv和采用出贴图
                float3 noiseResult = var_NoiseTex * var_NoiseMask * 0.1;
                float2 wrapUV = i.uv - noiseResult.rg;
                float3 var_MainTex = tex2D(_MainTex,wrapUV);
                
                float flshVal = cos(_Time.z * 2) * 0.5 + 0.5;
                float3 finalRGB = var_MainTex + pow(var_Flash,3) * frac(flshVal);
                return fixed4(finalRGB,1);
            }
            ENDCG
        }
    }
}

度盘:https://pan.baidu.com/s/12KARfRtqtxyP8uFcRikqlg?pwd=66a1 (opens new window)