View difference between Paste ID: 3f6cYUBr and aFYyjewx
SHOW: | | - or go back to the newest paste.
1
Shader "Custom/GlitchWireframeEffect"
2
{
3
  Properties
4
  {
5
    [Header(Vertex Displacement)]
6
    _Intensity ("Vertex Intensity", Range(0,1)) = 0.3
7
    _Width("Pulse Width", Range(0,1)) = 0.5
8
    _PulseFreq("Pulse Freq", Range(0,1)) = 0.2
9
    _Scale("Pulse Scale", Range(0,5)) = 2 
10
    _Speed("Speed", Range(-5,5)) = 0.3
11
    _Random("Random Offset", Range(0,5)) = 0.0
12
13
    [Header(Tinting)]
14
    _TintingBrightness("Tinting Brightness", Range(0,5)) = 1.0
15
    _Stretch("Stretch Tinting Gradient", Range(0,5)) = 2
16
    _Tinting("Albedo Tinting", Color) = (0,0,1,1)
17
    _Tinting2("Albedo Tinting 2", Color) = (0,0,0,1)
18
19
    [Header(Backfaces)]
20
    _Brightness ("Backface Brightness", Range(0,5)) = 4
21
    _BackfaceCol ("Backface Color", Color) = (0,0.5,1,1)
22
    _BackfaceCol2 ("Backface Color2", Color) = (0,1,1,1)
23
    _StretchBackface("Stretch BackFace Gradient", Range(0,5)) = 0.0
24
25
    [Header(Rim)]
26
    _RimBrightness("Rim Brightness", Range(0,20)) = 5
27
    _RimPower("RImPOwer", Range(0,20)) = 10
28
    _StretchRim("Stretch Rim Gradient", Range(0,5)) = 2
29
    _RimCol ("Rim Color", Color) = (1,0,1,1)
30
    _RimCol2 ("Rim Color 2", Color) = (1,1,1,1)
31
32
    [Header(Other)]
33
    _Clipping("Front Face Clipping", Range(0,1)) = 0.0
34
    _Distortion("Distortion", Range(0,1)) = 0.2
35
  }
36
  SubShader
37
  {
38
    Tags { "RenderType"="Opaque" "Queue"="Geometry+2"}
39
    LOD 200
40
    Cull Off
41
    GrabPass { "_GrabTex" }
42
    CGPROGRAM
43
    #include "UnityCG.cginc"
44
45
    
46
    // Physically based Standard lighting model, and enable shadows on all light types
47
    #pragma surface surf Simple vertex:vert addshadow 
48
49
    // Use shader model 3.0 target, to get nicer looking lighting
50
    #pragma target 4.0
51
    
52
53
    sampler2D _GrabTex;
54
    float4 _GrabTex_TexelSize;
55
    
56
57
    #pragma lighting Simple exclude_path:prepass
58
    inline half4 LightingSimple(SurfaceOutput s, half3 lightDir, half atten)
59
    {
60
      half4 c;
61
      c.rgb = s.Albedo * _LightColor0.rgb *atten; 
62
      c.a = s.Alpha;
63
      return c;
64
    }
65
66
    struct Input
67
    {
68
      float4 screenPos;
69
      float facing : VFACE;
70
      float3 viewDir;
71
      float3 worldPos;
72
      float glitchPosFront;
73
      float glitchPos;      
74
    };
75
76
    fixed4 _BackfaceCol2, _BackfaceCol;
77
    fixed4 _Tinting, _Tinting2;
78
    fixed4 _RimCol,_RimCol2;
79
    float _Intensity,_Width, _PulseFreq;
80
    float _Stretch, _StretchBackface, _StretchRim;
81
    float _Random,_Scale,_Speed;
82
    float _Brightness,_TintingBrightness,_RimBrightness;
83
    float _RimPower;
84
    sampler2D _CameraDepthTexture;
85
    float _Distortion;
86
    float _Clipping;
87
88
    float rand(float n)
89
    {
90
      return frac(sin(n) * 43758.5453123);
91
    }
92
93
94
95
    void vert (inout appdata_full v, out Input o)
96
    {
97
      UNITY_INITIALIZE_OUTPUT (Input, o);
98
      
99
      // vertex y position, with optional randomness
100
      float randomPos = (rand(v.texcoord.x) * _Random ) + (v.vertex.y * _Scale);
101
      float moveSpeed = _Time.y * _Speed;
102
103
      // repeat position on model, and move it up/down
104
      float glitchPos = frac((randomPos +moveSpeed ) * _PulseFreq)  ;// position on model
105
      
106
      // clamp the width with a smoothstep
107
      float glitchPosClamped =  smoothstep(glitchPos , glitchPos + 1, _Width );
108
109
      // only split on the camera viewing part
110
      float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
111
      float frontFacing =saturate(dot(normalize(v.normal), normalize(viewDir))) ;
112
      frontFacing = step(0.1, frontFacing);
113
114
      // move vertices outward based on the glitchy position
115
      v.vertex.xyz += (glitchPosClamped * normalize(v.normal)) * _Intensity * frontFacing;
116
117
      // send positions through to the fragment function
118
      o.glitchPos = glitchPos;
119
      o.glitchPosFront = glitchPosClamped* frontFacing;
120
    }
121
122
    // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
123
    // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
124
    // #pragma instancing_options assumeuniformscaling
125
    UNITY_INSTANCING_BUFFER_START(Props)
126
    // put more per-instance properties here
127
    UNITY_INSTANCING_BUFFER_END(Props)
128
129
    float3 GetFaceNormal(float3 position) {
130
      float3 dx = ddx(position);
131
      float3 dy = ddy(position);
132
      return normalize(cross(dy, dx));
133
    }
134
135
    void surf (Input IN, inout SurfaceOutput o)
136
    {
137
      // grabtex uv
138
      float2 uv = IN.screenPos.xy / IN.screenPos.w;
139
      
140
      // get an evened out facenormal, will make the distortions move more with the vertex movement
141
      float3 faceNormal = GetFaceNormal(IN.worldPos.xyz);
142
      
143
      // distorted uv 
144
      float2 distortedUV = lerp(uv, (faceNormal + uv) * 0.5,  _Distortion) ;
145
      // grabtex
146
      float4 grabPassTex = tex2D (_GrabTex, distortedUV );
147
148
      //base albedo is the grabtex
149
      o.Albedo =  grabPassTex.rgb ;
150
      
151
      // backfaces colors, lerped over glitchpos
152
      float3 backGlitchCol = lerp(_BackfaceCol2, _BackfaceCol, saturate(IN.glitchPos*  _StretchBackface));
153
154
      // tinted grabtex colors, lerped over glitchpos
155
      float3 albedoTinting = lerp( _Tinting, _Tinting2,saturate(IN.glitchPos* _Stretch));
156
      albedoTinting *=  grabPassTex.rgb;
157
      // add tinting
158
      o.Albedo += (albedoTinting * _TintingBrightness) ;
159
      
160
      // use vface to only add color to the front faces
161
      o.Albedo = (IN.facing>0) ? o.Albedo : 0;
162
      
163
      // create a rim
164
      float Rim = 1.0 - saturate(dot(normalize(faceNormal), normalize(IN.viewDir)));
165
      float softRim = pow(Rim, _RimPower)  ;
166
167
      
168
      // color rim based on glitchpos
169
      float3 rimCol = lerp(_RimCol, _RimCol2,  saturate(IN.glitchPos * _StretchRim) );
170
171
      // rimcol on frontfacing, backglithcol on backfacing
172
      o.Emission = ((IN.facing>0) ?  ( rimCol * _RimBrightness) * softRim: backGlitchCol * _Brightness ) ;
173
      
174
      // clip front to show into the mesh more
175
      clip( ((1-IN.glitchPosFront)- _Clipping));
176
      
177
      o.Alpha = 1;
178
    }
179
    ENDCG
180
  }
181
  FallBack "Diffuse"
182
}
183