1. 程式人生 > >Unity3D Shader圖像扭曲過場效果

Unity3D Shader圖像扭曲過場效果

mat 分享 col target lec ota dep 技術分享 腳本

技術分享

把腳本掛在攝像機上

using UnityEngine;
using System.Collections;


[RequireComponent(typeof(Camera))]
public class PostEffectTwist : MonoBehaviour {

	public Material ma;


	void OnRenderImage(RenderTexture src, RenderTexture dest)
	{
		Graphics.Blit (src, dest, ma);
	}
}

創建一個材質,再創建一個Shader

Shader "Hidden/NewImageEffectShader"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_Angle ("Rotation", Float) = 0
	}
	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 = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = v.uv;
				return o;
			}
			
			sampler2D _MainTex;
			float _Angle;

			fixed4 frag (v2f i) : SV_Target
			{
				float2 uv = float2(i.uv.x-0.5,i.uv.y-0.5);
				float f = distance(uv,float2(0,0));
				float s = sin(lerp(0,_Angle,f));
				float c = cos(lerp(0,_Angle,f));
				// -c s
				// s  c

				uv = float2(-uv.x*c+uv.y*s,uv.x*s+uv.y*c);
				uv = float2(uv.x+0.5,uv.y+0.5);
				fixed4 col = tex2D(_MainTex, uv);

				return col;
			}
			ENDCG
		}
	}
}

  

Unity3D Shader圖像扭曲過場效果