Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效
旋轉(zhuǎn)扭曲特效是指在一個(gè)圓形區(qū)域內(nèi)扭曲所渲染的圖像,其他像素的旋轉(zhuǎn)程度隨著距離的變化而變化。具體可以通過修改Shader來實(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í)有所幫助,也希望大家多多支持腳本之家。
- Unity 從UI中拖拽對(duì)象放置并拖動(dòng)效果 附demo
- Unity UGUI實(shí)現(xiàn)簡(jiǎn)單拖拽圖片功能
- Unity工具類ScrollView實(shí)現(xiàn)拖拽滑動(dòng)翻頁
- Unity UI拖拽模型選擇功能
- Unity3D實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)效果
- Unity向量按照某一點(diǎn)進(jìn)行旋轉(zhuǎn)
- Unity實(shí)現(xiàn)物體沿自身的任意軸向旋轉(zhuǎn)
- Unity實(shí)現(xiàn)人物旋轉(zhuǎn)和移動(dòng)效果
- Unity實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量
- Unity UI實(shí)現(xiàn)拖拽旋轉(zhuǎn)
相關(guān)文章
C# Socket 發(fā)送&接收&返回 簡(jiǎn)單應(yīng)用實(shí)例
下面小編就為大家分享一篇C# Socket 發(fā)送&接收&返回 簡(jiǎn)單應(yīng)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11C#的path.GetFullPath 獲取上級(jí)目錄實(shí)現(xiàn)方法
這篇文章主要介紹了C#的path.GetFullPath 獲取上級(jí)目錄實(shí)現(xiàn)方法,包含了具體的C#實(shí)現(xiàn)方法以及ASP.net與ASP等的方法對(duì)比,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10Unity實(shí)現(xiàn)鼠標(biāo)或者手指點(diǎn)擊模型播放動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)鼠標(biāo)或者手指點(diǎn)擊模型播放動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01

C#在DataTable中根據(jù)條件刪除某一行的實(shí)現(xiàn)方法

Unity UGUI的ToggleGroup選項(xiàng)組件介紹使用