在研究某个shader效果时发现如下代码 “float2x2(cosval, -sinval, sinval, cosval)”,比较好奇其功能。于是便搜索到如下文章。
原文地址:http://www.redblack.cn/?p=115(原文地址地址已经打不开了)
游戏中,很多地方可以用到屏幕扭曲效果。比如传送时、进入梦境时等等。这篇文件就提供一种简单的屏幕扭曲效果。
以下是扭曲效果的shader文件:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
Shader
"Unlit/screenTwist"
{
Properties
{
_MainTex (
"Texture"
, 2D) =
"white"
{}
_Twist(
"Twist"
,
float
) = 1
}
SubShader
{
Tags {
"RenderType"
=
"Opaque"
}
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct
appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct
v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainTex_TexelSize;
float
_Twist;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return
o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed2 tuv = i.uv;
//这里将当前纹理坐标平移至中心点,表示按中心旋转,
//如果有需要,可以算出3d坐标在屏幕上的位置,然后根据屏幕位置做平移
fixed2 uv = fixed2(tuv.x - 0.5, tuv.y - 0.5);
//通过距离计算出当前点的旋转弧度PI/180=0.1745
float
angle = _Twist * 0.1745 / (length(uv) + 0.1);
float
sinval, cosval;
sincos(angle, sinval, cosval);
//构建旋转矩阵
float2x2 mat = float2x2(cosval, -sinval, sinval, cosval);
//旋转完成后,平移至原位置
uv = mul(mat, uv) + 0.5;
// sample the texture
fixed4 col = tex2D(_MainTex, uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return
col;
}
ENDCG
}
}
}
|
并新建一个cs文件调用上面的shader来修改相机纹理:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
using
UnityEngine;
using
System.Collections;
public
class
Twist : MonoBehaviour {
public
Shader shader;
public
float
twistAngle = 0;
private
Material mat;
void
Start () {
mat =
new
Material(shader);
}
void
OnRenderImage(RenderTexture source, RenderTexture destination)
{
mat.SetFloat(
"_Twist"
, twistAngle);
Graphics.Blit(source, destination, mat);
}
}
|
将以上组件绑定至摄像机上,并把shader赋值到组件上。运行即可看到以下效果:
以上只是一个简单的扭曲效果,如果想要更复杂的效果,可以尝试加噪声贴图或者其他的噪声函数。