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

服務(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91麻豆制片厂 | 亚洲国产成人久久综合区 | 青青网| 男人天堂官方网站 | 日韩高清在线免费观看 | 成人国产精品 | 色天天综合色天天碰 | 韩剧消失的眼角膜免费完整版 | 蘑菇香蕉茄子绿巨人丝瓜草莓 | 午夜精品一区 | 思思99热久久精品在2019线 | 国产伦精一区二区三区视频 | 免费一级毛片完整版在线看 | 亚洲精品国产成人中文 | 亚洲精品专区 | 亚洲成年人免费网站 | 国产第一福利影院 | 女教师系列三上悠亚在线观看 | 久久亚洲免费视频 | ai换脸杨颖啪啪免费网站 | 500第一精品 | 千金奴隶在线 | 国产精品一区二区三区免费 | 99热er| 秋霞午夜伦午夜高清福利片 | 男人天堂新 | 四虎影视免费观看免费观看 | 亚洲一区二区成人 | 9热在线精品视频观看 | 91视频a | 77成人影院 | 亚洲黄色天堂 | 欧洲vodafone精品性 | 国产一区二区免费福利片 | 女女性恋爱视频入口 | 亚洲香蕉伊在人在线观婷婷 | 国产欧美日韩专区毛茸茸 | 欧美成人在线影院 | 欧美洲大黑香蕉在线视频 | 国产欧美在线播放 | 四虎在线观看 |