Shader屏幕扭曲效果

在研究某个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赋值到组件上。运行即可看到以下效果:

以上只是一个简单的扭曲效果,如果想要更复杂的效果,可以尝试加噪声贴图或者其他的噪声函数。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现图像扭曲过场效果,可以使用Unity3D中的Shader。具体实现步骤如下: 1. 创建一个新的Shader,并将其属性设置为Unlit/Texture。 2. 在Shader中添加一个名为“Distortion”的属性,类型为2D纹理,用于存储扭曲图像。 3. 在Shader中添加一个名为“DistortionStrength”的属性,类型为Range,用于控制扭曲强度。 4. 在Shader的片段着色器中,使用tex2D函数获取原始纹理的颜色,并使用uv坐标对扭曲图像进行采样。 5. 将扭曲图像的采样值与“DistortionStrength”属性相乘,并将结果添加到原始颜色中。 6. 最后,将新的颜色值输出到屏幕。 下面是一个简单的Shader示例: ``` Shader "Custom/DistortionTransition" { Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion", 2D) = "white" {} _DistortionStrength ("Distortion Strength", Range(0.0, 1.0)) = 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; }; sampler2D _MainTex; sampler2D _Distortion; float _DistortionStrength; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } fixed4 frag (v2f i) : SV_Target { // Get the original color fixed4 texColor = tex2D(_MainTex, i.uv); // Get the distortion value float4 distortion = tex2D(_Distortion, i.uv); // Calculate the distortion offset float2 offset = (distortion.rg * 2.0 - 1.0) * _DistortionStrength; // Apply the distortion to the UV coordinates float2 distortedUV = i.uv + offset; // Get the color from the distorted UV coordinates fixed4 distortedColor = tex2D(_MainTex, distortedUV); // Add the distortion to the original color return texColor + (distortedColor - texColor); } ENDCG } } FallBack "Diffuse" } ``` 在使用这个Shader时,可以将原始纹理作为“_MainTex”属性的值,将扭曲图像作为“_Distortion”属性的值,然后通过修改“_DistortionStrength”属性的值来控制扭曲强度。 希望这个示例能够帮助你实现所需的效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值