So, here’s another quick Unity3D tip. Incidentally, speaking of Unity, for anyone wondering what happened to the multi part endless runner posts I started, I decided to quit that since the folks at Unity released their own endless runner game which is much more fully featured. You can check it out here – it’s a great source.
Anyway, here’s a little image effect shader that can create a nice ripple or shockwave or etc type of effect:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/** * Copyright (c) 2017 Devon O. Wolfgang * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ Shader "PostEffects/Ripple" { Properties { _MainTex ("Texture", 2D) = "white" {} _CenterX ("Center X", float) = 300 _CenterY ("Center Y", float) = 250 _Amount ("Amount", float) = 25 _WaveSpeed("Wave Speed", range(.50, 50)) = 20 _WaveAmount("Wave Amount", range(0, 20)) = 10 } 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; }; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } sampler2D _MainTex; float _CenterX; float _CenterY; float _Amount; float _WaveSpeed; float _WaveAmount; fixed4 frag (v2f i) : SV_Target { fixed2 center = fixed2(_CenterX/_ScreenParams.x, _CenterY/_ScreenParams.y); fixed time = _Time.y * _WaveSpeed; fixed amt = _Amount/1000; fixed2 uv = center.xy-i.uv; uv.x *= _ScreenParams.x/_ScreenParams.y; fixed dist = sqrt(dot(uv,uv)); fixed ang = dist * _WaveAmount - time; uv = i.uv + normalize(uv) * sin(ang) * amt; return tex2D(_MainTex, uv); } ENDCG } } } |
Apply that shader to a material then add that material to this post processor script (which should be added as a component of the Main Camera).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/** * Copyright (c) 2017 Devon O. Wolfgang * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using UnityEngine; public class RipplePostProcessor : MonoBehaviour { public Material RippleMaterial; public float MaxAmount = 50f; [Range(0,1)] public float Friction = .9f; private float Amount = 0f; void Update() { if (Input.GetMouseButton(0)) { this.Amount = this.MaxAmount; Vector3 pos = Input.mousePosition; this.RippleMaterial.SetFloat("_CenterX", pos.x); this.RippleMaterial.SetFloat("_CenterY", pos.y); } this.RippleMaterial.SetFloat("_Amount", this.Amount); this.Amount *= this.Friction; } void OnRenderImage(RenderTexture src, RenderTexture dst) { Graphics.Blit(src, dst, this.RippleMaterial); } } |
You can play around with the public properties of the material and theĀ RipplePostProcessor component in the inspector, but here’s an example of what it can do – just click around the scene (again, using the fantasticĀ Blacksmith’s Forge package).
Hope it may help out.
Recent Comments