Shader旋转穿越效果实现

游戏开发中,往往会用到一些屏幕特效。下图展现的是一种“旋屏”效果,它会旋转屏幕图像,且距离中心点越远的点旋转角度越大。这种效果特别适合营造“梦幻”感,比如,在RPG游戏中,经过一段“旋屏”特效,主角穿越到了10年前。

 1、编写Shader

下面的着色器代码使用了顶点/片元着色器处理旋屏特效的功能。这里定义3个属性,其中_MainTex代表屏幕贴图,_Rot 代表基准的旋转角度。核心代码在片元着色器frag中实现。

如下图所示,屏幕图像被归一到[0,1]的空间中,中心点为(0.5,0.5)。假设某个点的uv坐标为(x,y),经过一系列处理,它的坐标变为(x1,y1),而(x1,y1)便是实现旋转效果后的uv坐标。




由“float distance = sqrt((i.uv.x - 0.5)*(i.uv.x - 0.5) +(i.uv.y - 0.5)*(i.uv.y - 0.5));”可以计算点到屏幕中心的距离distance。由于距离越远旋转角度越大,使用“_Rot *=distance”将角度增量基准与距离联系起来,即可获取需要旋转的角度:angle = _Rot*distance + A。

由反正切公式可得∠A = atan((y - 0.5)/(x - 0.5)),由于atan的取值为[-π/2,π/2],还需根据y值确定∠A所在的象限,故而∠A = step(x,0.5)*PI+ atan((y - 0.5)/(x - 0.5)) 。计算∠A 后,便可由angle = _Rot*distance + A计算总的旋转角度。

前面已经计算了点到屏幕中心的距离distance,故而:
x1 = 0.5 + distance *cos(angle)
y1 = 0.5 + distance *sin(angle)

Shader代码如下所示:

Shader "Custom/ScreenRot" 
{
	Properties
	{
		_MainTex ("Main Tex", 2D) = "white" {}
		_Rot ("Rotation", float) = 0
	}
 
	SubShader
	{
		Tags {"Queue" = "Geometry"}
 
		Pass
		{
			Tags {"LightMode" = "ForwardBase"}
			ZWrite Off
 
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#define PI 3.14159265358979 
 
			sampler2D _MainTex;
			float _Rot;
 
			struct a2v
			{
				float4 vertex : POSITION;
				float3 texcoord : TEXCOORD0;
			};
 
			struct v2f
			{
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
			};
 
			v2f vert(a2v v)
			{
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = v.texcoord;
				return o;
			}
 
			fixed4 frag(v2f i) : SV_Target
			{
				float distance = sqrt((i.uv.x - 0.5) * (i.uv.x - 0.5) + (i.uv.y - 0.5) * (i.uv.y - 0.5));
				_Rot *= distance;
 
				float angle = step(i.uv.x, 0.5) * PI + atan((i.uv.y - 0.5) / (i.uv.x - 0.5)) + _Rot;
				i.uv.x = 0.5 + distance * cos(angle);
				i.uv.y = 0.5 + distance * sin(angle);
 
				fixed4 c = tex2D(_MainTex, i.uv);
				return c;
			}
 
			ENDCG
		}
	}
	  FallBack "Specular"
}

C#脚本编写如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Oscillator : MonoBehaviour {
    private float timeCounter = 0;
    private float speed = 5;
    float width = 4;
    float height = 7;
    public LineRenderer line;

    public Material mtl;
    float rot;
    // Use this for initialization
    void Start () {
        //line.positionCount = 3;
        //line.SetPosition(0, new Vector3(1, 1, 1));
        //line.SetPosition(1, new Vector3(3, 3, 3));
        //line.SetPosition(2, new Vector3(6, 6, 6));
    }
	
	// Update is called once per frame
	void Update () {
        //timeCounter += Time.deltaTime * speed;
        //float x = Mathf.Cos(timeCounter) * width;
        //float y = Mathf.Sin(timeCounter) * height;
        //float z = 10;

        //transform.position = new Vector3(x, y, z);

        rot += 0.1f;
	}

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (rot == 0)
            return;

        mtl.SetFloat("_Rot", rot);
        Graphics.Blit(source, destination, mtl);
    }
}

 最后再次附上原文链接:http://www.manew.com/thread-98320-1-1.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值