-
Star
(110)
You must be signed in to star a gist -
Fork
(17)
You must be signed in to fork a gist
-
-
Save KeyMaster-/363d3d5c35b956dfacdd to your computer and use it in GitHub Desktop.
| //Copyright (c) 2014 Tilman Schmidt (@KeyMaster_) | |
| //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 "Sprites/Glitch" | |
| { | |
| Properties | |
| { | |
| [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} | |
| _Color ("Tint", Color) = (1,1,1,1) | |
| [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 | |
| _DispIntensity ("Displacement Glitch Intensity", Float) = 0.09 | |
| _DispFrequency ("Displacement Glitch Frequency", Float) = 0.022 | |
| _ColorIntensity("Color Glitch Intensity", Float) = 0.07 | |
| _ColorFrequency("Color Glitch Frequency", Float) = 0.02 | |
| [MaterialToggle] _DispGlitchOn ("Displacement Glitch On", Float) = 1 | |
| [MaterialToggle] _ColorGlitchOn ("Color Glitch On", Float) = 1 | |
| } | |
| SubShader | |
| { | |
| Tags | |
| { | |
| "Queue"="Transparent" | |
| "IgnoreProjector"="True" | |
| "RenderType"="Transparent" | |
| "PreviewType"="Plane" | |
| "CanUseSpriteAtlas"="True" | |
| } | |
| Cull Off | |
| Lighting Off | |
| ZWrite Off | |
| Fog { Mode Off } | |
| Blend One OneMinusSrcAlpha | |
| Pass | |
| { | |
| CGPROGRAM | |
| // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members pos) | |
| #pragma exclude_renderers xbox360 | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| #pragma target 3.0 | |
| #pragma multi_compile DUMMY PIXELSNAP_ON | |
| #include "UnityCG.cginc" | |
| struct appdata_t | |
| { | |
| float4 vertex : POSITION; | |
| float4 color : COLOR; | |
| float2 texcoord : TEXCOORD0; | |
| }; | |
| struct v2f | |
| { | |
| float4 vertex : SV_POSITION; | |
| fixed4 color : COLOR; | |
| half2 texcoord : TEXCOORD0; | |
| }; | |
| fixed4 _Color; | |
| v2f vert(appdata_t IN) | |
| { | |
| v2f OUT; | |
| OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); | |
| OUT.texcoord = IN.texcoord; | |
| OUT.color = IN.color * _Color; | |
| #ifdef PIXELSNAP_ON | |
| OUT.vertex = UnityPixelSnap (OUT.vertex); | |
| #endif | |
| return OUT; | |
| } | |
| sampler2D _MainTex; | |
| float rand(float x, float y){ | |
| return frac(sin(x*12.9898 + y*78.233)*43758.5453); | |
| } | |
| float _DispIntensity; | |
| float _DispFrequency; | |
| float _ColorIntensity; | |
| float _ColorFrequency; | |
| float _DispGlitchOn; | |
| float _ColorGlitchOn; | |
| fixed4 frag(v2f IN) : SV_Target | |
| { | |
| float timeYIndependent = float(_Time.y + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]); | |
| float timeYIndependent2 = float(_Time.x + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]); | |
| float dispGlitchRandom = rand(timeYIndependent, -timeYIndependent); | |
| float colorGlitchRandom = rand(timeYIndependent, timeYIndependent); | |
| float rShiftRandom = (rand(-timeYIndependent, timeYIndependent) - 0.5) * _ColorIntensity; | |
| float gShiftRandom = (rand(-timeYIndependent, -timeYIndependent) - 0.5) * _ColorIntensity; | |
| float bShiftRandom = (rand(timeYIndependent, -timeYIndependent) - 0.5) * _ColorIntensity; | |
| float shiftLineOffset = float((rand(timeYIndependent2, -timeYIndependent2) - 0.5) / 50); | |
| if(dispGlitchRandom < _DispFrequency && _DispGlitchOn == 1){ | |
| IN.texcoord.x += (rand(floor(IN.texcoord.y / (0.2 + shiftLineOffset)) - _Time.x, floor(IN.texcoord.y / (0.2 + shiftLineOffset)) + _Time.x) - 0.5) * _DispIntensity; | |
| } | |
| fixed4 normalC = tex2D(_MainTex, IN.texcoord); | |
| fixed4 rShifted = tex2D(_MainTex, float2(IN.texcoord.x + rShiftRandom, IN.texcoord.y + rShiftRandom)); | |
| fixed4 gShifted = tex2D(_MainTex, float2(IN.texcoord.x + gShiftRandom, IN.texcoord.y + gShiftRandom)); | |
| fixed4 bShifted = tex2D(_MainTex, float2(IN.texcoord.x + bShiftRandom, IN.texcoord.y + bShiftRandom)); | |
| fixed4 c = fixed4(0.0,0.0,0.0,0.0); | |
| if(colorGlitchRandom < _ColorFrequency && _ColorGlitchOn== 1){ | |
| c.r = rShifted.r; | |
| c.g = gShifted.g; | |
| c.b = bShifted.b; | |
| c.a = (rShifted.a + gShifted.a + bShifted.a) / 3; | |
| } | |
| else{ | |
| c = normalC; | |
| } | |
| c.rgb *= IN.color; | |
| c.rgb *= c.a; | |
| return c; | |
| } | |
| ENDCG | |
| } | |
| } | |
| SubShader | |
| { | |
| Tags | |
| { | |
| "Queue"="Transparent" | |
| "IgnoreProjector"="True" | |
| "RenderType"="Transparent" | |
| "PreviewType"="Plane" | |
| "CanUseSpriteAtlas"="True" | |
| } | |
| Cull Off | |
| Lighting Off | |
| ZWrite Off | |
| Fog { Mode Off } | |
| Blend One OneMinusSrcAlpha | |
| Pass | |
| { | |
| CGPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| #pragma multi_compile DUMMY PIXELSNAP_ON | |
| #include "UnityCG.cginc" | |
| struct appdata_t | |
| { | |
| float4 vertex : POSITION; | |
| float4 color : COLOR; | |
| float2 texcoord : TEXCOORD0; | |
| }; | |
| struct v2f | |
| { | |
| float4 vertex : SV_POSITION; | |
| fixed4 color : COLOR; | |
| half2 texcoord : TEXCOORD0; | |
| }; | |
| fixed4 _Color; | |
| v2f vert(appdata_t IN) | |
| { | |
| v2f OUT; | |
| OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); | |
| OUT.texcoord = IN.texcoord; | |
| OUT.color = IN.color * _Color; | |
| #ifdef PIXELSNAP_ON | |
| OUT.vertex = UnityPixelSnap (OUT.vertex); | |
| #endif | |
| return OUT; | |
| } | |
| sampler2D _MainTex; | |
| float rand(float x, float y){ | |
| return frac(sin(x*12.9898 + y*78.233)*43758.5453); | |
| } | |
| float _DispIntensity; | |
| float _DispFrequency; | |
| fixed4 frag(v2f IN) : SV_Target | |
| { | |
| float timeYIndependent = float(_Time.y + UNITY_MATRIX_MV[0][3] + UNITY_MATRIX_MV[1][3]); | |
| float timeRandom = rand(timeYIndependent, -timeYIndependent); | |
| if(timeRandom < _DispFrequency){ | |
| IN.texcoord.x += (rand(floor(IN.texcoord.y / 0.2) - _Time.x, floor(IN.texcoord.y / 0.2) + _Time.x) - 0.5) * _DispIntensity; | |
| } | |
| fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color; | |
| c.rgb *= c.a; | |
| return c; | |
| } | |
| ENDCG | |
| } | |
| } | |
| } |
Hey! This is a really nice shader 😃 , however, I keep getting errors that say "Arithmetic instruction limit of 64 exceeded" since there are 73 instructions. Shader 3.0 is enabled in your script but for some reason it keeps kicking me to this error when compiling. Everything works fine - I just get this error on compile. Thoughts?
Very nice! im wondering though if there is a way to add diffuse? im making a game where the sprite art is affected by point lights and such, giving it some light and dark areas
Nice work! I'm having a bit of an issue with the shader, though. When I apply these shaders to a UI object, and then push that object beyond the bounds of a UI Mask, it does not hide as it normally does when it does not have a material attached. Is there any good fix for that?
Brilliant! thank you for this!
I'm getting this warning. I don't know enough to edit the shader myself. Could you modify it?
"Shader warning in 'Sprites/Glitch': Use of UNITY_MATRIX_MV is detected. To transform a vertex into view space, consider using UnityObjectToViewPos for better performance."
Hi, since I don't use Unity myself anymore, I haven't kept this shader up to date, and I currently don't have the time to fix this.
However, after some brief googling, it seems like any use of UNITY_MATRIX_MVP should now use UnityObjectToClipPos and any use of UNITY_MATRIX_MV should now use UnityObjectToViewPos. Line 224 uses UNITY_MATRIX_MVP,
and I think OUT.vertex = UnityObjectToClipPos(IN.vertex.xyz); should be an equivalent line that can replace it.
As for the uses of UNITY_MATRIX_MV, (112 and 113, 249), they use the matrix to get the sprite's position in view space.
I think at both of those places, you could insert float3 offset = UnityObjectToViewPos(float3(0.0, 0.0, 0.0)); before the uses of the matrix, and then replace any instance of UNITY_MATRIX_MV[0][3] with offset.x and any instance of UNITY_MATRIX_MV[1][3] with offset.y.
See Unity Shaders builtin functions page for more info on those functions.
As I said I have no way of testing this right now, but I hope this will be of some use.
Thank you, the warning went away, and it seems to be working. I didn't have in depth knowledge to adapt it, so thank you for the help. Other glitch effects effect whole screen, this one effects only the sprites so that makes it interesting to me. Thank you again.
It works perfectly! Anyone knows how to extend this to support grayscale?
Glitch shader for sprites in Unity3D
Demo: http://imgur.com/fkGLhlw
Lets sprites glitch occasionally by either displacing strips of the sprite horizontally or displacing the color channels.
Features
Settings
0 == Never, 1 == Always. Low value around 0.005 is recommended, depending on the glitch interval
The value represents the "percentage" of the sprite width, e.g. 0.5 means half the sprite width in displacement at most
0 == Never, 1 == Always. Low value around 0.005 recommended
Again, a percentage of the sprite width and height, e.g. 0.5 means half the sprites width at most and half the sprites height at most