国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity實現旋轉扭曲圖像特效

2020-01-24 00:09:10
字體:
來源:轉載
供稿:網友

旋轉扭曲特效是指在一個圓形區域內扭曲所渲染的圖像,其他像素的旋轉程度隨著距離的變化而變化。具體可以通過修改Shader來實現。

原始圖片

扭曲圖片

/*====================================================        屏幕扭曲特效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;      //旋轉扭曲的中心      uniform float4 _CenterRadius;      //將旋轉矩陣傳入      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坐標變換到center坐標系中        o.uv = v.uv - _CenterRadius.xy;        return o;      }      fixed4 frag (v2f i) : SV_Target      {        float2 offest = i.uv;        //利用旋轉矩陣旋轉uv        float2 distortedOffset = MultiplyUV(_RotationMatrix,offest.xy);        //計算uv點在旋轉圓中的位置        float2 tmp = offest / _CenterRadius.zw;        float t = min(1,length(tmp));        //根據uv點在圓中的位置插值uv移動的位置        offest =lerp(distortedOffset,offest,t);        //將uv坐標返回原坐標系中        offest += _CenterRadius.xy;         fixed4 col = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(offest, _MainTex_ST));        return col;      }      ENDCG    }  }}

此旋轉特效主要就是對圖像的uv值進行偏移,關鍵代碼

 float2 offest = i.uv;//利用旋轉矩陣旋轉uv float2 distortedOffset = MultiplyUV(_RotationMatrix,offest.xy); //計算uv點在旋轉圓中的位置float2 tmp = offest / _CenterRadius.zw;float t = min(1,length(tmp));//根據uv點在圓中的位置插值uv移動的位置offest =lerp(distortedOffset,offest,t);//將uv坐標返回原坐標系中offest += _CenterRadius.xy; 

根據uv點的位置,對圖像進行扭曲。

下面是腳本的源碼

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);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 许昌县| 大竹县| 平武县| 乡宁县| 运城市| 闵行区| 广河县| 临猗县| 伊春市| 长兴县| 盖州市| 信丰县| 南丰县| 长治市| 嘉祥县| 辽源市| 郁南县| 濮阳市| 云霄县| 镶黄旗| 双牌县| 许昌市| 潞西市| 青龙| 吉林省| 达孜县| 横山县| 河津市| 筠连县| 友谊县| 新化县| 五华县| 罗甸县| 高邑县| 海城市| 绥芬河市| 高邮市| 洛阳市| 新疆| 阜康市| 双辽市|