unity shader 波动圈

c#

////////////////////////////////////////////
// CameraFilterPack - by VETASOFT 2018 /////
////////////////////////////////////////////

using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[AddComponentMenu ("Camera Filter Pack/Distortion/ShockWave Manual")]
public class CameraFilterPack_Distortion_ShockWaveManual : MonoBehaviour {
#region Variables
public Shader SCShader;
private float TimeX = 1.0f;
 
private Material SCMaterial;
[Range(-1.5f, 1.5f)]
public float PosX = 0.5f;
[Range(-1.5f, 1.5f)]
public float PosY = 0.5f;
[Range(-0.1f, 2f)]
public float Value = 0.5f;
[Range(0f, 10f)]
public float Size = 1f;

#endregion
#region Properties
Material material
{
get
{
if(SCMaterial == null)
{
SCMaterial = new Material(SCShader);
SCMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return SCMaterial;
}
}
#endregion
void Start ()
{

SCShader = Shader.Find("CameraFilterPack/Distortion_ShockWaveManual");
if(!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
}

void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
{
if(SCShader != null)
{
TimeX+=Time.deltaTime;
if (TimeX>100)  TimeX=0;
material.SetFloat("_TimeX", TimeX);
material.SetFloat("_Value", PosX);
material.SetFloat("_Value2", PosY);
material.SetFloat("_Value3", Value);
material.SetFloat("_Value4", Size);
material.SetVector("_ScreenResolution",new Vector4(sourceTexture.width,sourceTexture.height,0.0f,0.0f));
Graphics.Blit(sourceTexture, destTexture, material);
}
else
{
Graphics.Blit(sourceTexture, destTexture);
}
}
void Update ()
{

#if UNITY_EDITOR
if (Application.isPlaying!=true)
{
SCShader = Shader.Find("CameraFilterPack/Distortion_ShockWaveManual");
}
#endif
}
void OnDisable ()
{
if(SCMaterial)
{
DestroyImmediate(SCMaterial);
}
}
}

 

shader

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

////////////////////////////////////////////
// CameraFilterPack - by VETASOFT 2018 /////
////////////////////////////////////////////

Shader "CameraFilterPack/Distortion_ShockWaveManual" 
{ 
    Properties 
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _TimeX ("Time", Range(0.0, 1.0)) = 1.0
        _ScreenResolution ("_ScreenResolution", Vector) = (0.,0.,0.,0.)
    }
    SubShader
    {
        Pass
        {
            Cull Off ZWrite Off ZTest Always
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
            #pragma target 3.0
            #pragma glsl
            #include "UnityCG.cginc"
            uniform sampler2D _MainTex;
            uniform float _TimeX;
            uniform float _Value;//x坐标
            uniform float _Value2;//y坐标
            uniform float _Value3;//圈的大小
            uniform float _Value4;//强度
            uniform float4 _ScreenResolution;
            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
            };
            struct v2f
            {
                float2 texcoord  : TEXCOORD0;
                float4 vertex   : SV_POSITION;
                float4 color    : COLOR;
            };
            v2f vert(appdata_t IN)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color; 
                return OUT;
            }
            half4 _MainTex_ST;
            float4 frag(v2f i) : COLOR
            {
                float2 uvst = UnityStereoScreenSpaceUVAdjust(i.texcoord, _MainTex_ST);
                float2 uv = uvst.xy;
                float Dist = distance(uv, float2(_Value, _Value2));
                float Diff = (Dist - _Value3);
                float v = (1.0 - pow(abs(Diff * 10.0), 0.8));
                
                float vt = saturate(v * 0.02 * _Value4);
                uv.x = uv.x - vt;
                uv.y = uv.y - vt;
                
                float4 Color = tex2D(_MainTex, uv);
                Color.rgb += float3(vt, vt, vt);
                return Color; 
            }
            ENDCG
        }
    }
}

 

posted @ 2019-10-08 17:42  三页菌  阅读(692)  评论(0编辑  收藏  举报