Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效
旋轉(zhuǎn)扭曲特效是指在一個(gè)圓形區(qū)域內(nèi)扭曲所渲染的圖像,其他像素的旋轉(zhuǎn)程度隨著距離的變化而變化。具體可以通過(guò)修改Shader來(lái)實(shí)現(xiàn)。
原始圖片
扭曲圖片
/*====================================================
        屏幕扭曲特效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)鍵代碼
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)行扭曲。
下面是腳本的源碼
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);
  }
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Unity3D仿寫(xiě)B(tài)utton面板事件綁定功能
欄 目:C#教程
下一篇:Unity3D網(wǎng)格功能生成球體網(wǎng)格模型
本文標(biāo)題:Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4893.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
 - 01-10C#實(shí)現(xiàn)多線程下載文件的方法
 - 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
 - 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
 - 01-10C#自定義簽名章實(shí)現(xiàn)方法
 - 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
 - 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 01-10C#中split用法實(shí)例總結(jié)
 - 04-02jquery與jsp,用jquery
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-10delphi制作wav文件的方法
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 


