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自己画也行

相关推荐
weixin_409383125 小时前
godot创建两种敌人僵尸 一种吐舌头 一种在角色脚下生成圆形伤害圈 两种僵尸均继承enemy脚本 理解继承
游戏引擎·godot
mxwin11 小时前
Unity Shader 跨平台兼容性:处理纹理坐标翻转与精度差异
unity·游戏引擎
王家视频教程图书馆12 小时前
godot 下载地址
游戏引擎·godot
世人万千丶15 小时前
开源鸿蒙跨平台Flutter开发:成语接龙游戏应用
学习·flutter·游戏·华为·开源·harmonyos·鸿蒙
派葛穆15 小时前
汇川PLC-Unity3d与汇川easy521plc进行Modbustcp通讯
unity·c#
small-pudding16 小时前
Unity URP + Compute Shader 路径追踪器实战:从可用到可优化
unity·游戏引擎
weixin_4239950016 小时前
unity 物体转向鼠标点击方向2d和3d
unity·计算机外设·游戏引擎
兔子零102416 小时前
Claude Code 都把宠物养进终端了,我做了一个真正能长期玩的中文宠物游戏
前端·游戏·游戏开发
mxwin16 小时前
Unity URP 下 Shader 变体 (Variants):multi_compile 与 shader_feature的关键字管理及变体爆炸防控策略
unity·游戏引擎
howlet217 小时前
AI生成cocos-creator打砖块游戏-跑通第1关(CodeBuddy)
人工智能·游戏·cocos2d