Advertisement
Guest User

TrailShader

a guest
Feb 20th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Trail"
  2. {
  3.     Properties
  4.     {
  5.         _Color("Main Color", Color) = (1,1,1,1)
  6.         _Width("Width", float) = 0.2
  7.     }
  8.  
  9.     SubShader
  10.     {
  11.         Tags
  12.         {
  13.             "Queue" = "Transparent"
  14.             "IgnoreProjector" = "True"
  15.             "RenderType" = "Opaque"
  16.         }
  17.  
  18.         Pass
  19.         {
  20.             Cull Back
  21.             Lighting Off
  22.             ZWrite Off
  23.             Blend OneMinusDstColor OneMinusSrcColor
  24.  
  25.             CGPROGRAM
  26.  
  27.             #pragma vertex vert
  28.             #pragma fragment frag
  29.  
  30.             #include "MyriadCG.cginc"
  31.  
  32.             uniform fixed4 _Color;
  33.             uniform float2 _Trail[600];
  34.             uniform int _Samples;
  35.             uniform float _Width;
  36.             uniform float2 _Ref;
  37.  
  38.             struct v2f
  39.             {
  40.                 float4 vertex : SV_POSITION;
  41.             };
  42.  
  43.             inline float unlerp(float min, float max, float value)
  44.             {
  45.                 return clamp((value - min) / (max - min), 0, 1);
  46.             }
  47.  
  48.             v2f vert(uint index : SV_VertexID)
  49.             {
  50.                 int sign = ((index % 2) * 2) - 1;
  51.                 int current = clamp(floor((float)index * 0.5), 0, _Samples);
  52.                 int prev = current + 1;
  53.                
  54.                 float2 prevPosition = mul(unity_WorldToObject, _Trail[current] - _Ref);
  55.                 float2 currentPosition= mul(unity_WorldToObject, _Trail[prev] - _Ref);
  56.  
  57.                 float2 direction = normalize(currentPosition - prevPosition);
  58.                 float2 perpendicular = float2(-direction.y, direction.x) * sign;
  59.  
  60.                 v2f output;
  61.                 output.vertex = UnityObjectToClipPos(float4(currentPosition + (perpendicular * 0.2 * 0.5 * unlerp(0, _Samples, current)), 0, 0));
  62.                 return output;
  63.             }
  64.  
  65.             fixed4 frag(void) : COLOR
  66.             {
  67.                 return _Color;
  68.             }
  69.  
  70.             ENDCG
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement