标签:
Matrix4x4 rotationMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, 0, angle),Vector3.one);
然后通过material的以下函数向shader里传参数
material.SetMatrix("name",value) 对应Shader里的 float4x4
material.SetVector("name",value) 对应Shader里的 float4
float2 MultiplyUV (float4x4 mat, float2 inUV) {
float4 temp = float4 (inUV.x, inUV.y, 0, 0);
temp = mul (mat, temp);
return temp.xy;
}
步骤:

脚本
(1) 通过Angle得到旋转矩阵
Matrix4x4 rotationMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0 , 0 , angle),Vector3.one);
(2) 将矩阵和 旋转中点 及 半径传入shader.
Shader
(1) 将坐标系从左下角的坐标系 转到 以旋转中点为 原点的坐标系
vert里 o.uv = v.texcoord - _CenterRadius.xy;

(2) 将旋转坐标系 里的uv乘以 旋转矩阵。
(3) 将uv除以 半径, 再取长度。长度超过1则为 旋转区域外,小于则在区域内
using UnityEngine;
using System.Collections;
public class TwirlEffectMe : MonoBehaviour {
public Shader shader;
private Material mat;
public Vector2 radius = new Vector2(0.3F,0.3F);
public float angle = 50;
public Vector2 center = new Vector2 (0.5F, 0.5F);
void Start()
{
mat = new Material(shader);
}
void OnRenderImage (RenderTexture source, RenderTexture destination) {
RenderDistortion (mat, source, destination, angle, center, radius);
}
public static void RenderDistortion(Material material, RenderTexture source, RenderTexturedestination, float angle, Vector2 center, Vector2 radius)
{
Matrix4x4 rotationMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0 , 0 , angle), Vector3.one);
material.SetMatrix("_RotationMatrix", rotationMatrix);
material.SetVector("_CenterRadius", new Vector4(center.x, center.y, radius.x, radius.y));
Graphics.Blit(source, destination, material);
}
}
Shader "Custom/TwirlEffectMe" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _MainTex_TexelSize;
uniform float4 _CenterRadius;
uniform float4x4 _RotationMatrix;
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
} ;
v2f vert( appdata_img v )
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord - _CenterRadius.xy;
return o;
}
float4 frag (v2f i) : COLOR
{
float2 offset = i.uv;
float2 distortedOffset = MultiplyUV (_RotationMatrix, offset.xy);
float2 tmp = offset / _CenterRadius.zw;
float2 finalUV;
float len = length(tmp);
// out of twirl
if( len >1)
{
finalUV = offset;
}
else
{
finalUV = distortedOffset;
}
// back to normal uv coordinate
finalUV += _CenterRadius.xy;
return tex2D(_MainTex, finalUV);
}
ENDCG
}
}
Fallback off
}

整体代码如下:
Shader "Custom/TwirlEffectMe" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _MainTex_TexelSize;
uniform float4 _CenterRadius;
uniform float4x4 _RotationMatrix;
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
} ;
v2f vert( appdata_img v )
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord - _CenterRadius.xy;
return o;
}
float4 frag (v2f i) : COLOR
{
float2 offset = i.uv;
float2 distortedOffset = MultiplyUV (_RotationMatrix, offset.xy);
float2 tmp = offset / _CenterRadius.zw;
float2 finalUV;
float len = length(tmp);
// out of twirl
if( len >1)
{
finalUV = offset;
}
else
{
finalUV = lerp(distortedOffset, offset, len );
}
// back to normal uv coordinate
finalUV += _CenterRadius.xy;
return tex2D(_MainTex, finalUV);
}
ENDCG
}
}
Fallback off
}

Unity3D教程宝典之Shader篇:第二十六讲ImageEffects_Twirl
标签:
原文地址:http://www.cnblogs.com/zdlbbg/p/4329559.html