Advertisement
Guest User

AfterImage.shader

a guest
Nov 16th, 2021
436
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "FX/After Image Effect" {
  2.     Properties{
  3.         _TintColor("Tint Color", Color) = (0,0.5,1,1)
  4.         _RimColor("Rim Color", Color) = (0,1,1,1)
  5.         _MainTex("Main Texture", 2D) = "black" {}
  6.         _RimPower("Rim Power", Range(1,50)) = 20
  7.         _Fade("Fade Amount", Range(0,1)) = 1
  8.         _Grow("Grow", Range(0,1)) = 0.05
  9.     }
  10.    
  11.     Category{
  12.         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "PreviewType" = "Sphere" }
  13.         Blend SrcAlpha One
  14.         Zwrite Off
  15.         Cull Back
  16.         SubShader{
  17.             Pass{
  18.                
  19.                 CGPROGRAM
  20.                 #pragma vertex vert
  21.                 #pragma fragment frag
  22.                 #pragma multi_compile_instancing
  23.                 #pragma target 3.0                         
  24.                 #include "UnityCG.cginc"
  25.                
  26.                 sampler2D _MainTex;
  27.                 fixed4 _TintColor;
  28.                 fixed4 _RimColor;
  29.                
  30.                
  31.                 struct appdata_t {
  32.                     float4 vertex : POSITION;              
  33.                     float2 texcoord : TEXCOORD0;
  34.                     float3 normal : NORMAL; // vertex normal
  35.                     UNITY_VERTEX_INPUT_INSTANCE_ID
  36.                 };
  37.                
  38.                 struct v2f {
  39.                     float4 vertex : SV_POSITION;
  40.                     float2 texcoord : TEXCOORD0;
  41.                     float3 wpos : TEXCOORD1; // worldposition
  42.                     float3 normalDir : TEXCOORD2; // normal direction for rimlighting
  43.                     UNITY_VERTEX_INPUT_INSTANCE_ID
  44.                    
  45.                 };
  46.                
  47.                 float4 _MainTex_ST;
  48.                 float _RimPower;
  49.                 float _Grow;
  50.  
  51.                 UNITY_INSTANCING_BUFFER_START(Props)
  52.                 UNITY_DEFINE_INSTANCED_PROP(float, _Fade)
  53.                 UNITY_INSTANCING_BUFFER_END(Props)
  54.  
  55.                 v2f vert(appdata_t v)
  56.                 {
  57.                     v2f o;
  58.                    
  59.                     UNITY_SETUP_INSTANCE_ID(v);
  60.                     UNITY_TRANSFER_INSTANCE_ID(v, o);
  61.                    
  62.                     // grow based on normals and fade property
  63.                     v.vertex.xyz += v.normal * saturate(1- UNITY_ACCESS_INSTANCED_PROP(Props, _Fade)) * _Grow;
  64.                     o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
  65.                     o.vertex = UnityObjectToClipPos(v.vertex);
  66.                    
  67.                     // world position and normal direction for fresnel
  68.                     o.wpos = mul(unity_ObjectToWorld, v.vertex).xyz;
  69.                     o.normalDir = normalize(mul(float4(v.normal, 0.0), unity_WorldToObject).xyz);                  
  70.                     return o;
  71.                 }
  72.                
  73.                 fixed4 frag(v2f i) : SV_Target
  74.                 {
  75.                     UNITY_SETUP_INSTANCE_ID(i);
  76.                     float4 text = tex2D(_MainTex, i.texcoord);// texture
  77.                    
  78.                     // rim lighting
  79.                     float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.wpos.xyz);
  80.                     // fresnel based on view and normal
  81.                     half rim = 1.0 - saturate(dot(viewDirection, i.normalDir));
  82.                     rim = pow(rim, _RimPower);                                 
  83.                    
  84.                     // end result color    
  85.                     fixed4 col = (text * _TintColor) +(rim * _RimColor);           
  86.                     col.a *= _TintColor.a;
  87.                     col.a *= (text.r + text.g + text.b) * 0.33f;
  88.                     col.a += rim;
  89.  
  90.                     // quick smoothstep to make the fade more interesting
  91.                     col.a = smoothstep( col.a ,col.a + 0.05 ,UNITY_ACCESS_INSTANCED_PROP(Props, _Fade));                   
  92.                     col = saturate(col);
  93.                     return col;
  94.                 }
  95.                 ENDCG
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement