一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效

Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效

2022-03-11 12:56圖形小菜雞 C#

這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

旋轉(zhuǎn)扭曲特效是指在一個(gè)圓形區(qū)域內(nèi)扭曲所渲染的圖像,其他像素的旋轉(zhuǎn)程度隨著距離的變化而變化。具體可以通過修改shader來實(shí)現(xiàn)。

原始圖片

Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效

扭曲圖片

Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效

?
1
2
3
4
5
6
7
8
9
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
77
78
79
80
/*====================================================
 
        屏幕扭曲特效shader
 
======================================================*/
shader "hidden/twirleffects"
{
  properties
  {
    _maintex ("texture", 2d) = "white" {}
 
  }
  subshader
  {
    // no culling or depth
    cull off zwrite off ztest always
 
    pass
    {
      cgprogram
      #pragma vertex vert
      #pragma fragment frag
 
      #include "unitycg.cginc"
 
      uniform sampler2d _maintex;
      uniform float4  _maintex_texelsize;
      half4  _maintex_st;
 
      //旋轉(zhuǎn)扭曲的中心
      uniform float4 _centerradius;
      //將旋轉(zhuǎn)矩陣傳入
      uniform float4x4 _rotationmatrix;
 
      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);
        //將uv坐標(biāo)變換到center坐標(biāo)系中
        o.uv = v.uv - _centerradius.xy;
        return o;
      }
 
      fixed4 frag (v2f i) : sv_target
      {
 
        float2 offest = i.uv;
        //利用旋轉(zhuǎn)矩陣旋轉(zhuǎn)uv
        float2 distortedoffset = multiplyuv(_rotationmatrix,offest.xy);
 
        //計(jì)算uv點(diǎn)在旋轉(zhuǎn)圓中的位置
        float2 tmp = offest / _centerradius.zw;
        float t = min(1,length(tmp));
 
        //根據(jù)uv點(diǎn)在圓中的位置插值uv移動(dòng)的位置
        offest =lerp(distortedoffset,offest,t);
 
        //將uv坐標(biāo)返回原坐標(biāo)系中
        offest += _centerradius.xy;
 
        fixed4 col = tex2d(_maintex, unitystereoscreenspaceuvadjust(offest, _maintex_st));
 
        return col;
      }
      endcg
    }
  }
}

此旋轉(zhuǎn)特效主要就是對(duì)圖像的uv值進(jìn)行偏移,關(guān)鍵代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
float2 offest = i.uv;
//利用旋轉(zhuǎn)矩陣旋轉(zhuǎn)uv
 float2 distortedoffset = multiplyuv(_rotationmatrix,offest.xy);
 
 //計(jì)算uv點(diǎn)在旋轉(zhuǎn)圓中的位置
float2 tmp = offest / _centerradius.zw;
float t = min(1,length(tmp));
 
//根據(jù)uv點(diǎn)在圓中的位置插值uv移動(dòng)的位置
offest =lerp(distortedoffset,offest,t);
 
//將uv坐標(biāo)返回原坐標(biāo)系中
offest += _centerradius.xy;

根據(jù)uv點(diǎn)的位置,對(duì)圖像進(jìn)行扭曲。

下面是腳本的源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using system.collections;
using system.collections.generic;
using unityengine;
 
public class twirlscripts : monobehaviour {
 
  [executeineditmode]
 
  public vector2 radius = new vector2(0.3f, 0.3f);
 
  public vector2 center = new vector2(0.5f, 0.5f);
 
  [range(0.0f, 360.0f)]
  public float angle = 0.0f;
 
  public material material;
 
  private void onrenderimage(rendertexture source, rendertexture destination)
  {
 
    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);
 
  }
 
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/u014222099/article/details/54377229

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品久久久久久久久久久久久久 | 操骚0 | 免费观看欧美成人h | 国产在视频| 国产成人久视频免费 | 91精品国产高清久久久久久io | 日本高清不卡一区久久精品 | 色吊丝每日永久访问网站 | 青草青草久热精品视频在线网站 | 久久性综合亚洲精品电影网 | 微拍秒拍99福利精品小视频 | 强插美女 | www一区| 国产亚洲成归v人片在线观看 | 久久99精品国产免费观看 | 99热成人精品免费久久 | katsuniav在线播放 | 动漫美女强行被吸乳做羞羞事 | 日本乱中文字幕系列在线观看 | 国产专区一va亚洲v天堂 | 精品免费视在线视频观看 | 55夜色66夜亚州精品站 | 欧美亚洲韩国 | 美女扒开腿让男生桶爽漫画 | 91亚洲视频在线观看 | 双夫1v2| 亚洲男人的天堂在线 | 爆操| 欧美乱妇高清无乱码视频在线 | 精品无码久久久久久久动漫 | 美女和男人免费网站视频 | 99视频一区 | 国产成人青草视频 | porono日本人xxx | 99国产在线视频 | 日韩欧美高清一区 | 2020年精品国产午夜福利在线 | 免费视频精品一区二区三区 | 女人又色又爽又黄 | 女同久久另类99精品国产 | 欧美 亚洲 综合 卡通 另类 区 |