Unity实现旋转扭曲图像特效
作者:图形小菜鸡 时间:2023-12-16 08:35:17
旋转扭曲特效是指在一个圆形区域内扭曲所渲染的图像,其他像素的旋转程度随着距离的变化而变化。具体可以通过修改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);
}
}
来源:https://blog.csdn.net/u014222099/article/details/54377229
标签:unity,旋转,扭曲图像
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SprinBoot如何集成参数校验Validator及参数校验的高阶技巧
2023-10-23 16:24:31
![](https://img.aspxhome.com/file/2023/3/84333_0s.png)
Spring MVC数据绑定方式
2023-03-18 03:06:55
java网络编程基础知识介绍
2023-01-10 20:37:44
![](https://img.aspxhome.com/file/2023/7/77987_0s.jpg)
Spring Aop 如何获取参数名参数值
2022-09-08 17:00:41
详解Spring关于@Resource注入为null解决办法
2023-02-13 18:14:13
![](https://img.aspxhome.com/file/2023/7/82017_0s.png)
android播放器实现歌词显示功能
2021-10-27 13:44:37
深度理解Java访问修饰符
2021-10-26 17:16:08
![](https://img.aspxhome.com/file/2023/8/76598_0s.png)
Java SpringBoot在RequestBody中高效的使用枚举参数原理案例详解
2022-03-28 01:47:18
![](https://img.aspxhome.com/file/2023/4/71684_0s.jpg)
Java Spring AOP源码解析之事务实现原理
2023-09-10 02:39:46
![](https://img.aspxhome.com/file/2023/5/66945_0s.png)
Android开发之对话框案例详解(五种对话框)
2021-08-07 11:37:47
Spring中ApplicationContextAware的使用方法详解
2023-12-25 07:01:33
Unity实现截图功能
2022-02-18 11:36:18
Spring集成Redis详解代码示例
2023-05-04 00:58:25
![](https://img.aspxhome.com/file/2023/1/94341_0s.jpg)
Java使用JDBC连接Oracle_MSSQL实例代码
2023-04-19 19:34:46
Java基础高级综合练习题扑克牌的创建
2023-09-08 06:56:19
![](https://img.aspxhome.com/file/2023/9/97979_0s.png)
Java调取创蓝253短信验证码的实现代码
2021-11-05 00:48:10
在Android中使用WebSocket实现消息通信的方法详解
2022-06-10 06:26:18
![](https://img.aspxhome.com/file/2023/4/98664_0s.png)
C#泛型和反射实例解析
2023-10-04 03:49:02
Spring Boot 实现https ssl免密登录(X.509 pki登录)
2023-07-28 18:46:11
![](https://img.aspxhome.com/file/2023/9/132159_0s.png)
Android开发中使用sqlite实现新闻收藏和取消收藏的功能
2023-01-27 18:15:13
![](https://img.aspxhome.com/file/2023/1/129011_0s.jpg)