-
-
Save Nrjwolf/25412a5579dbc251165183d55a2edbdd to your computer and use it in GitHub Desktop.
Add WaveExplosionEffect to your camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' | |
Shader "Custom/WaveExplo" { | |
Properties { | |
_MainTex ("", 2D) = "white" {} | |
_CenterX ("CenterX", Range(-1,2)) = 0.5 | |
_CenterY ("CenterY", Range(-1,2)) = 0.5 | |
_Radius ("Radius", Range(-1,1)) = 0.2 | |
_WaveSize ("Wave Size", float) = 0.2 | |
_Amplitude ("Amplitude", Range(-10,10)) = 0.05 | |
[Toggle] _IsCorrectRatio("Correct ratio", Float) = 0 | |
} | |
SubShader { | |
ZTest Always Cull Off ZWrite Off Fog { Mode Off } //Rendering settings | |
Pass{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
//we include "UnityCG.cginc" to use the appdata_img struct | |
float _CenterX; | |
float _CenterY; | |
float _Radius; | |
float _Amplitude; | |
float _WaveSize; | |
float _IsCorrectRatio; | |
uniform float4 _MainTex_TexelSize; | |
struct v2f { | |
float4 pos : POSITION; | |
half2 uv : TEXCOORD0; | |
}; | |
//Our Vertex Shader | |
v2f vert (appdata_img v){ | |
v2f o; | |
o.pos = UnityObjectToClipPos (v.vertex); | |
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy); | |
return o; | |
} | |
sampler2D _MainTex; //Reference in Pass is necessary to let us use this variable in shaders | |
//Our Fragment Shader | |
fixed4 frag (v2f i) : COLOR{ | |
// correct ratio | |
float ratio = 1; | |
if(_IsCorrectRatio) ratio = _MainTex_TexelSize.x / _MainTex_TexelSize.y; | |
float2 diff = float2 (i.uv.x - _CenterX, (i.uv.y -_CenterY) * ratio); | |
float dist=sqrt(diff.x*diff.x+diff.y*diff.y); | |
float2 uv_displaced = float2(i.uv.x,i.uv.y); | |
if (dist>_Radius) { | |
if (dist<_Radius+_WaveSize) { | |
float angle=(dist-_Radius)*2*3.141592654/_WaveSize; | |
float cossin=(1-cos(angle))*0.5; | |
uv_displaced.x-=cossin*diff.x*_Amplitude/dist; | |
uv_displaced.y-=cossin*diff.y*_Amplitude/dist; | |
} | |
} | |
fixed4 orgCol = tex2D(_MainTex, uv_displaced); //Get the orginal rendered color | |
return orgCol; | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[ExecuteAlways] | |
public class WaveExplosionEffect : MonoBehaviour | |
{ | |
public Camera Camera; | |
public Vector2 Position = new Vector2(.5f, .5f); | |
public float Amplitude = .2f; | |
public float Radius; | |
public float WaveSize = .2f; | |
public bool IsCorrectRatio = false; | |
private Shader m_Shader; | |
private Material m_Material; | |
private const string k_CenterXKey = "_CenterX"; | |
private const string k_CenterYKey = "_CenterY"; | |
private const string k_RadiusKey = "_Radius"; | |
private const string k_AmplitudeKey = "_Amplitude"; | |
private const string k_WaveSizeKey = "_WaveSize"; | |
private const string k_IsCorrectRatioKey = "_IsCorrectRatio"; | |
public void Start() | |
{ | |
Camera = Camera.main; | |
m_Shader = Shader.Find("Custom/WaveExplo"); | |
m_Material = new Material(m_Shader); | |
if (!m_Shader && !m_Shader.isSupported) | |
{ | |
enabled = false; | |
} | |
} | |
public void SetPosition(Vector3 worldPosition) => Position = Camera.WorldToViewportPoint(worldPosition); | |
public void OnRenderImage(RenderTexture inTexture, RenderTexture outTexture) | |
{ | |
if (m_Shader != null) | |
{ | |
m_Material.SetFloat(k_CenterXKey, Position.x); | |
m_Material.SetFloat(k_CenterYKey, Position.y); | |
m_Material.SetFloat(k_RadiusKey, Radius); | |
m_Material.SetFloat(k_AmplitudeKey, Amplitude); | |
m_Material.SetFloat(k_WaveSizeKey, WaveSize); | |
m_Material.SetFloat(k_IsCorrectRatioKey, IsCorrectRatio.ToInt()); | |
Graphics.Blit(inTexture, outTexture, m_Material); | |
} | |
else | |
{ | |
Graphics.Blit(inTexture, outTexture); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample
