Shader 特效实例 --- 屏幕后期特效

屏幕后期特效:

场景渲染完以后在添加一些特效。
1,引擎渲染后最终的结果是一张图片。

1, void OnRenderImage(RenderTexture sourceTexture, RenderTexture desTexture)
2, Graphics.Blit(sourceTexture, desTexture,mat);
sourceTexture拦截相机渲染出来的图片。
mat : 用哪个材质球重新渲染sourceTexture。
desTexture:更改以后的图片存在这里 重新交给引擎。

2,将图片传递给 shader 进行二次计算。

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/GaoSi"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}


		_Ambient ("Ambient", float) = 0.001
	}
	SubShader
	{
		// No culling or depth
		Cull Off ZWrite Off ZTest Always

		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   _Ambient;

			fixed4 frag (v2f i) : SV_Target
			{

			   float2  tmpUV =  i.uv ;


			 //  float    ambient = 0.001 ;

				fixed4 col = tex2D(_MainTex, tmpUV);
				//  just invert the colors
				// col = 1 - col;


				fixed4 col2 = tex2D(_MainTex, tmpUV+float2(-_Ambient,0));


				fixed4 col3 = tex2D(_MainTex, tmpUV+float2(0,-_Ambient));

			  	fixed4 col4 = tex2D(_MainTex, tmpUV+float2(_Ambient,0));

			   fixed4 col5 = tex2D(_MainTex, tmpUV+float2(0,_Ambient));



			   col   =  (col + col2 +col3+col4+col5) /5.0 ;


				return col;
			}
			ENDCG
		}
	}
}

生命周期
在这里插入图片描述
在这里插入图片描述

Blood 血液闪烁特效:

1, 红色光线从两边闪出
在这里插入图片描述
圆心(-0.5,0.5) (1.5, 0.5)

2,红色有中间到两边渐变变浓。
通过圆心的距离判别渐变的过程。
Length : 求向量的模长
Lerp: (1-t) A + t * B - - -
Lerp (A,B,T)
Clamp(x, a , b) : 表示将 X 限制在 a 和 B 之间。
Material 更改shader 属性:
graphicMat.SetFloat
graphicMat.SetTexture



// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
	Shader "Hidden/Blood"
	{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_BloodLength (" length",float) = 0.5
	}
	SubShader
	{
		// No culling or depth
		//Cull Off ZWrite Off ZTest Always
		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 _BloodLength;
			fixed4 frag (v2f i) : SV_Target
			{
			    float2  tmpUV =  i.uv ;
				float  tmpLength = 0 ;
				if(tmpUV.x < 0.5)
				{
				  	   tmpLength  =  length ( tmpUV - float2(-0.5 ,0.5)) ;
				}
				else
				{
				       tmpLength  =  length (float2(1.5 ,0.5)- tmpUV  ) ;
				}
				tmpLength  *= _BloodLength ;
				fixed4 col = tex2D(_MainTex, i.uv);
				col  = lerp(col,fixed4(1,0,0,1) , 1-clamp(tmpLength,0,1) ) ;
				return col;
			}
			ENDCG
		}
	}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Blood : MonoBehaviour {
	// Use this for initialization
	void Start () {		
	}

    float tmpCount;

	// Update is called once per frame
	void Update () {
        tmpCount += Time.deltaTime*5;
        rangValue =  Mathf.Sin(tmpCount) ;  //  -1   1
        rangValue = 1+(rangValue + 1) * 0.25f  ;   //  0 --0.5     //  1---1.	
	}

    public float rangValue;
    public Material graphicMat;
    void OnRenderImage(RenderTexture sourceTexture, RenderTexture desTexture)
    {
        graphicMat.SetFloat("_BloodLength", rangValue)   
        Graphics.Blit(sourceTexture, desTexture, graphicMat);
    }
}

屏幕特效–波动屏幕

波动方程:
y=Asin(ωx+φ)
φ:决定波形与X轴位置关系或横向移动距离(左加右减)
ω:决定周期(最小正周期T=2π/∣ω∣)
A:决定峰值(即纵向拉伸压缩的倍数)

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Hidden/Wave"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_Arange("Amplitute",float) = 1
		_Frenquncy("Frenquncy",float) = 0.5
		_Speed("Speed",float) = 0.5
	}

	SubShader
	{
		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;
			};


		   float   _Frenquncy ;

			float    _Arange ;

			float   _Speed ;


			v2f vert (appdata v)
			{
				  v2f o;
			      float  timer =  _Time.y *  _Speed ;
				  float  waver  = _Arange* sin(timer +  v.vertex.x *_Frenquncy );
				  v.vertex.y =  v.vertex.y + waver ;
				  o.vertex = UnityObjectToClipPos(v.vertex);
				  o.uv = v.uv;
				  return o;
			}
			
			sampler2D _MainTex;

	


			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				// just invert the colors
				//col = 1 - col;
				return col;
			}
			ENDCG
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值