Advertisement
Guest User

AfterImageHLSL.shader

a guest
Nov 23rd, 2021
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "FX/After Image Effect HLSL"
  2. {
  3.     Properties
  4.     {
  5.         _Color("Extra Color", Color) = (1,1,1,1)       
  6.         _RimColor("Rim Color", Color) = (0,1,1,1)
  7.         _MainTex("Main Texture", 2D) = "black" {}
  8.         _RimPower("Rim Power", Range(1,50)) = 20
  9.         [PerRendererData]_Fade("Fade Amount", Range(0,1)) = 1
  10.         _Grow("Grow", Range(0,1)) = 0.05
  11.     }
  12.  
  13.     SubShader
  14.     {
  15.         Tags {  "Queue" = "Transparent""RenderType"="Transparent" "RenderPipeline"="UniversalRenderPipeline"}
  16.         Blend SrcAlpha One
  17.         Zwrite Off
  18.         Cull Back
  19.         Pass
  20.         {
  21.             Tags { "LightMode"="UniversalForward" }
  22.  
  23.             HLSLPROGRAM
  24.             #pragma vertex vert
  25.             #pragma fragment frag
  26.            
  27.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  28.  
  29.             struct Attributes
  30.             {
  31.                 float4 positionOS   : POSITION;
  32.                 float2 uv           : TEXCOORD0;
  33.                 float3 normal       : NORMAL;
  34.             };
  35.  
  36.             struct Varyings
  37.             {
  38.                 float2 uv           : TEXCOORD0;
  39.                 float4 positionHCS  : SV_POSITION;
  40.                 float3 wpos         : TEXCOORD1; // worldposition
  41.                 float3 normalDir    : TEXCOORD2; // normal direction for rimlighting
  42.             };
  43.  
  44.            
  45.            
  46.            
  47.            
  48.            
  49.             TEXTURE2D(_MainTex);
  50.             SAMPLER(sampler_MainTex);
  51.            
  52.             CBUFFER_START(UnityPerMaterial)
  53.             float4 _RimColor;
  54.             float _RimPower;
  55.             float4 _MainTex_ST;
  56.             float _Fade;
  57.             float4 _Color;
  58.             float _Grow;
  59.             CBUFFER_END
  60.            
  61.  
  62.             Varyings vert(Attributes IN)
  63.             {
  64.                 Varyings OUT;
  65.                 // grow based on normals and fade property
  66.                 IN.positionOS.xyz += IN.normal * saturate(1-  _Fade) * _Grow;
  67.                
  68.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
  69.                 OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
  70.  
  71.                
  72.                
  73.                 // world position and normal direction for fresnel
  74.                 OUT.wpos = mul(unity_ObjectToWorld, IN.positionOS).xyz;
  75.                 OUT.normalDir = normalize(mul(float4(IN.normal, 0.0), unity_WorldToObject).xyz);       
  76.                 return OUT;
  77.             }
  78.  
  79.             half4 frag(Varyings IN) : SV_Target
  80.             {
  81.                 float4 text = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
  82.                 // rim lighting
  83.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - IN.wpos.xyz);
  84.                 // fresnel based on view and normal
  85.                 half rim = 1.0 - saturate(dot(viewDirection, IN.normalDir));
  86.                 rim = pow(rim, _RimPower);                                 
  87.                
  88.                 // end result color    
  89.                 float4 col = (text * _Color) +(rim * _RimColor);           
  90.                 col.a *=   _Color.a;
  91.                 col.a *= (text.r + text.g + text.b) * 0.33f;
  92.                 col.a += rim;
  93.  
  94.                 // quick smoothstep to make the fade more interesting
  95.                 col.a = smoothstep( col.a ,col.a + 0.05 ,_Fade);                   
  96.                 col = saturate(col);
  97.  
  98.  
  99.                 return col;
  100.             }
  101.             ENDHLSL
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement