unity屏幕受伤特效

cs 复制代码
//使用用途:同于屏幕掉血的后处理特效
//请结合和脚本:BloodScreen 挂载至摄像机使用本特效
//本特效设计之初未考虑兼容移动设备,请注意

//使用说明:
//掉血获取此脚本,将showBlood设置为true,如果您需要更深的效果可将Alpha控制调节更高,控制上程序限制不小于0.5
//如果让血消失将showBlood设置为false即可

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[AddComponentMenu("Learn/BloodScreen")]
public class CameraFilterPack : MonoBehaviour
{
    public Shader SCShader;
    [Range(0, 1)]
    public float Alpha =0.5f;

    public bool showBlood = false;

    [Range(0, 1f)]
    public float LightReflect = 1f;

    private Material SCMaterial;
    public Texture2D Texture2;

    Material material
    {
        get
        {
            if (SCMaterial == null)
            {
                SCMaterial = new Material(SCShader);
                SCMaterial.hideFlags = HideFlags.HideAndDontSave;
            }
            return SCMaterial;
        }
    }


    // Start is called before the first frame update
    void Start()
    {
        Texture2 = Resources.Load("aab") as Texture2D;
        SCShader = Shader.Find("BloodScreen");
        if (!SystemInfo.supportsImageEffects)
        {
            enabled = false;
            return;
        }
    }

    void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
    {
        if (SCShader != null)
        {
            if (!showBlood)
            {
                Alpha = 0;
            }
            else
            {
                if (Alpha < 0.5f)
                {
                    Alpha = 0.5f;
                }
                
            }
            material.SetFloat("_Alpha", Alpha);
            material.SetTexture("_BloodTex", Texture2);
            Graphics.Blit(sourceTexture, destTexture, material);
        }
        else
        {
            Graphics.Blit(sourceTexture, destTexture);
        }

       
    }

    void Update()
    {
        #if UNITY_EDITOR
        if (Application.isPlaying != true)
        {
            SCShader = Shader.Find("BloodScreen");
            Texture2 = Resources.Load("aab") as Texture2D;
        }
        #endif
    }

    void OnDisable()
    {
        if (SCMaterial)
        {
            DestroyImmediate(SCMaterial);
        }

    }
}

shader代码:

bash 复制代码
//使用用途:同于屏幕掉血的后处理特效
//请结合和脚本:CameraFilterPack 挂载至摄像机使用本特效
//本特效设计之初未考虑兼容移动设备,请注意

Shader "BloodScreen"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_BloodTex ("Blood Texture", 2D) = "white" {}
		_Alpha("Alpha",Range(0.0,1.0))=1
	}
	SubShader
	{
		Pass
		{
			Cull Off ZWrite Off ZTest Always
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			

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

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

			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _BloodTex;
			float4 _BloodTex_ST;
			fixed _Alpha;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
				o.uv.zw = TRANSFORM_TEX(v.uv, _BloodTex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 base = tex2D(_MainTex, i.uv.xy);
				fixed4 blood=tex2D(_BloodTex,i.uv.zw);
				blood*=_Alpha;
				fixed4 col=base*blood+base*(1-_Alpha);
			
				return col;
			}
			ENDCG
		}
	}
}

贴图:(源自网络)ps自己画也行

相关推荐
程序猿阿伟1 天前
《C++游戏人工智能开发:开启智能游戏新纪元》
c++·人工智能·游戏
Artistation Game1 天前
九、怪物行为逻辑
游戏·unity·游戏引擎
百里香酚兰1 天前
【AI学习笔记】基于Unity+DeepSeek开发的一些BUG记录&解决方案
人工智能·学习·unity·大模型·deepseek
妙为1 天前
unreal engine5制作动作类游戏时,我们使用刀剑等武器攻击怪物或敌方单位时,发现攻击特效、伤害等没有触发
游戏·游戏引擎·虚幻·碰撞预设
网站领航1 天前
服装时尚与动漫游戏的跨界联动:创新运营与策划策略研究
游戏·流量运营·用户运营
龙智DevSecOps解决方案1 天前
Perforce演讲回顾(上):从UE项目Project Titan,看Helix Core在大型游戏开发中的版本控制与集成使用策略
游戏·ue5·源代码管理·perforce·helix core
dangoxiba1 天前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
新手unity自用笔记1 天前
项目-坦克大战学习-游戏结束
学习·游戏
DK七七1 天前
【PHP陪玩系统源码】游戏陪玩系统app,陪玩小程序优势
前端·vue.js·游戏·小程序·php·uniapp
先生沉默先2 天前
使用Materialize制作unity的贴图,Materialize的简单教程,Materialize学习日志
学习·unity·贴图