这篇文章主要为大家详细介绍了Unity shader实现自由放大缩小效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Unity shader实现自由放大缩小效果的具体代码,供大家参考,具体内容如下
代码:
以下实现的shader代码:
Shader "Hidden/Wave"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_WaveWidth("Wave Width",float) = 0.5
_CenterX("CenterX",float)=0.5
_CenterY("CenterY",float)=0.5
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float _WaveWidth;
float _CenterX;
float _CenterY;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
float2 center=float2(_CenterX,_CenterY);
float2 distance= center - i.uv;
float x=center.x+ center.x*(-distance.x/center.x) *(1-_WaveWidth);
float y=center.y+ center.y*(-distance.y/center.y) *(1-_WaveWidth);
float2 uv = float2(x,y);
return tex2D(_MainTex, uv);
}
ENDCG
}
}
}
主要的内容还是在frag中。
下面是挂在摄像机上的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaveCreame : MonoBehaviour {
public Shader waveShader = null;
[Range(0.0f,1f)]
public float waveWidth = 0.3f;
private Material m_WaveMaterial = null;
private float m_CenterX = 0.5f;
private float m_CtenterY = 0.5f;
// Use this for initialization
void Start () {
m_WaveMaterial = new Material(waveShader);
}
// Update is called once per frame
void Update () {
Vector3 pos = Input.mousePosition;
m_CenterX = pos.x / Screen.width;
m_CtenterY = pos.y / Screen.height;
if (Input.GetMouseButton(0)) {
waveWidth += Time.deltaTime * 0.5f;
}
if (Input.GetMouseButton(1))
{
waveWidth -= Time.deltaTime * 0.5f;
}
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (waveShader == null || m_WaveMaterial == null) return;
m_WaveMaterial.SetFloat("_WaveWidth", waveWidth);
m_WaveMaterial.SetFloat("_CenterX", m_CenterX);
m_WaveMaterial.SetFloat("_CenterY", m_CtenterY);
Graphics.Blit(source, destination, m_WaveMaterial);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Unity shader实现自由放大缩小效果
基础教程推荐
猜你喜欢
- C#类和结构详解 2023-05-30
- 一个读写csv文件的C#类 2022-11-06
- unity实现动态排行榜 2023-04-27
- C# 调用WebService的方法 2023-03-09
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C#控制台实现飞行棋小游戏 2023-04-22
- ZooKeeper的安装及部署教程 2023-01-22
- C# windows语音识别与朗读实例 2023-04-27
- C# List实现行转列的通用方案 2022-11-02
- winform把Office转成PDF文件 2023-06-14