屏幕后期特效——Blood(角色死亡闪血)

本文介绍了一种名为Blood的闪烁特效实现方法,通过Shader代码在Unity中创建出血液从两边向中间渐变淡的视觉效果。特效利用了向量的模长来计算渐变距离,并通过C#脚本实时调整Shader参数,实现动态变化的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Blood 血液闪烁特效
1、红色光线从两边闪出

在这里插入图片描述
2、红色从两边倒中间渐变淡(通过圆心的距离判别渐变的距离)

Shader 代码:

Shader "Hidden/Blood"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Blood("Length",float)=1
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always
        Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

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

            sampler2D _MainTex;
            float _Blood;
            fixed4 frag (v2f i) : SV_Target
            {
                float2 tmpUV=i.uv;
                float tempLength=0;
                if(tmpUV.x<0.5)
                {
                    tempLength=length(tmpUV-float2(-0.5,0.5));//求向量的模长
                }
                else
                {
                    tempLength=length(float2(1.5,0.5)-tmpUV);
                }
                tempLength *=_Blood;
                fixed4 col = tex2D(_MainTex, i.uv);
                col=lerp(fixed4(1,0,0,1),col,clamp(tempLength,0,1));//讲tempLength限制在0——1之间
                // just invert the colors
                //col.rgb = 1 - col.rgb;
                return col;
            }
            ENDCG
        }
    }
}

C#代码:

public class Blood : MonoBehaviour
{

    public Material graphicMat;
    public float rangValue=0.5f;
    public float tempCount=0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        tempCount+=Time.deltaTime*10;
        rangValue=Mathf.Cos(tempCount);
        rangValue=1+(rangValue+1)*0.25f-0.25f;    //范围0.75~1.25 
    }

    private void OnRenderImage(RenderTexture src, RenderTexture dest) {
        
         graphicMat.SetFloat("_Blood",rangValue); //设置Shader参数
         Graphics.Blit(src,dest,graphicMat);
    }
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值