# 【Shader练习-恶魔猎手】

曾经经常玩的游戏《炉石传说》里的恶魔猎手职业卡“怒火”动态效果的模拟实现。

全屏显示

包括了以下练习内容:

  1. 月刃的绿色火焰。
  2. 飞速冲刺风。

使用到的纹理资源:

Markdown 图片

# 雾气

Markdown 图片

/** mask 是扣除角色部分 */
float4 windColor2(float mask,float2 uv)
{
    float2 uv1 = float2(uv.x + _Time.y * 0.025,uv.y);
    float4 noise2Col = tex2D(_Noise2Tex, uv1);
    return noise2Col*0.25 * mask;
}

# 冲刺风

Markdown 图片

/** 极坐标 */
float2 polarUV(float2 uv)
{
    float PI = 3.14159265438;
    float2 uv2 = (uv * PI * 2 - PI).rg;
    float uv_y = atan2(uv2.g,uv2.r) * 0.1591596 + 0.5;
    float uv_x = 1-distance(uv,float2(0.5,0.5));
    return float2(uv_x,uv_y);
}

/** 
* 使用了极坐标
* mask 是扣除角色部分
*/
float4 windColor(float mask,float2 uv0)
{
    float uvAddTime = frac(_Time.y*0.25);
    float2 uv = float2(uv0.x,uv0.y);
    float2 puv = polarUV(uv);
    puv.x = puv.x* 1.5 - uvAddTime;
    puv.y = puv.y * 5;

    // 风效
    fixed4 windCol = tex2D(_WindTex, puv);
    float4 windMaskCol = mask * pow(windCol.r,2) * 0.35;
    return windMaskCol;
}

# 蛋刀光效

Markdown 图片

Markdown 图片

float4 frontKnifeColor(float mask,float2 uv)
{
    float speed = 1;
    // 噪声图1
    float2 uv1 = float2(uv.x - _Time.y * 0.1,uv.y);
    uv1.y = uv1.y - uv.x;

    float4 knifeColor = float4(0.4156, 0.980, 0.0,1);
    float4 noise2Col = tex2D(_Noise2Tex, uv);
    float4 noise1Col = tex2D(_Noise1Tex, uv1);
    noise1Col =  noise1Col * noise2Col * knifeColor;
    noise1Col = noise1Col*2 + pow(noise1Col,3)+ pow(noise1Col,4);

    return  noise1Col * mask;
}

float4 backKnifeColor(float mask,float2 uv)
{
    float speed = 1;
    // 噪声图1
    float2 uv1 = float2(uv.x + _Time.y * 0.1,uv.y);
    uv1.y = uv1.y + uv.x;
    float4 knifeColor = float4(0.4156, 0.980, 0.0,1);
    float4 noise2Col = tex2D(_Noise2Tex, uv);
    float4 noise1Col = tex2D(_Noise1Tex, uv1);
    noise1Col =  noise1Col * noise2Col * knifeColor;
    noise1Col = noise1Col*2 + pow(noise1Col,3)+ pow(noise1Col,4);
    return noise1Col * mask;
}

# 整合

v2f vert (appdata v)
{
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = v.uv;
    return o;
}

fixed4 frag (v2f i) : SV_Target
{ 
    // 遮罩
    float4 maskCol = tex2D(_MaskTex, i.uv);
    float4 windColor22 = windColor2(maskCol.a,i.uv);

    // 背景色
    fixed4 mainCol = tex2D(_MainTex, i.uv);

    // 背刀光
    float4 backKnifeColor1 = backKnifeColor(maskCol.g,i.uv);
    float4 frontKnifeColor1 = frontKnifeColor(maskCol.r,i.uv);

    float windColor1 = windColor(maskCol.a,i.uv);
    return  mainCol + backKnifeColor1 + frontKnifeColor1 + windColor1 + windColor22;
}

度盘:https://pan.baidu.com/s/1ZQ_SHghIlxVk-h3IJd9-3Q?pwd=66a1 (opens new window)