欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

旋轉(zhuǎn)扭曲特效是指在一個圓形區(qū)域內(nèi)扭曲所渲染的圖像,其他像素的旋轉(zhuǎn)程度隨著距離的變化而變化。具體可以通過修改Shader來實現(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);

        //計算uv點在旋轉(zhuǎn)圓中的位置
        float2 tmp = offest / _CenterRadius.zw;
        float t = min(1,length(tmp));

        //根據(jù)uv點在圓中的位置插值uv移動的位置
        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)特效主要就是對圖像的uv值進行偏移,關(guān)鍵代碼

 float2 offest = i.uv;
//利用旋轉(zhuǎn)矩陣旋轉(zhuǎn)uv
 float2 distortedOffset = MultiplyUV(_RotationMatrix,offest.xy);

 //計算uv點在旋轉(zhuǎn)圓中的位置
float2 tmp = offest / _CenterRadius.zw;
float t = min(1,length(tmp));

//根據(jù)uv點在圓中的位置插值uv移動的位置
offest =lerp(distortedOffset,offest,t);

//將uv坐標(biāo)返回原坐標(biāo)系中
offest += _CenterRadius.xy; 

根據(jù)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);

  }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Unity3D仿寫B(tài)utton面板事件綁定功能

欄    目:C#教程

下一篇:Unity3D網(wǎng)格功能生成球體網(wǎng)格模型

本文標(biāo)題:Unity實現(xiàn)旋轉(zhuǎn)扭曲圖像特效

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4893.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有