Advertisement
Sinuousity

lineargrad shader

Feb 7th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "LinearGrad"
  2. {
  3.     Properties
  4.     {
  5.         _TopColor ("Top Color", Color) = (1, 1, 1, 1)
  6.         _BottomColor ("Bottom Color", Color) = (1, 1, 1, 1)
  7.         _RampTex ("Ramp Texture", 2D) = "white" {}
  8.  
  9.         _MaskRad("Mask Radius", Range(0.0,1.0)) = 0.5
  10.         _MaskSoft("Mask Softness", Range(0.0,1.0)) = 0.1
  11.         _Border("Border Thickness",Range(0.0,1.0)) = 0.1
  12.         _BorderColor("Border Color", Color) = (0, 0, 0, 1)
  13.     }
  14.  
  15.     SubShader
  16.     {
  17.         //this forces the preview in the material inspector to show as a flat quad instead of the default shapes
  18.         Tags{ "PreviewType" = "Plane" }
  19.         Blend SrcAlpha OneMinusSrcAlpha
  20.         Fog { Mode Off }
  21.         Lighting Off
  22.         ZWrite On//switched to on to work better with transparency, depends on usage
  23.         Cull Off
  24.  
  25.         Pass
  26.         {
  27.             CGPROGRAM
  28.             #pragma vertex vert
  29.             #pragma fragment frag
  30.  
  31.             struct vertexIn
  32.             {
  33.                 float4 pos : POSITION;
  34.                 float2 uv : TEXCOORD0;
  35.             };
  36.  
  37.             struct v2f
  38.             {
  39.                 float4 pos : SV_POSITION;
  40.                 float2 uv : TEXCOORD0;
  41.             };
  42.  
  43.             v2f vert(vertexIn input)
  44.             {
  45.                 v2f output;
  46.                 output.pos = UnityObjectToClipPos(input.pos);
  47.                 output.uv = input.uv;
  48.                 return output;
  49.             }
  50.  
  51.             float4 _TopColor, _BottomColor;
  52.             sampler2D _RampTex;
  53.  
  54.             float4 _BorderColor;
  55.             float _Border;
  56.             float _MaskSoft;
  57.             float _MaskRad;
  58.  
  59.             /*
  60.             Distance functions return the implicit distance from a given point and the surface of a shape
  61.             Many distance functions give exact values, meaning negative values represent the interior of the shape
  62.  
  63.             Excellent 2D distance functions reference by the legendary Inigo Quilez
  64.             http://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
  65.             */
  66.  
  67.             //the distance function for a circle centered around point 'p' with radius 'r'
  68.             float dCircle(float2 p, float r) { return length(p) - r; }
  69.  
  70.             float4 frag(v2f input) : COLOR
  71.             {
  72.                 float4 result = 0.0;
  73.  
  74.                 //this value is used with the 'smoothstep' function to produce smooth yet distinct edges from the fields returned by distance functions
  75.                 //you can actually calculate what this value needs to be for consistent softness across different resolutions using screen-space derivatives, but here we use predefined constants
  76.                 float s = lerp(0.005, 0.1, _MaskSoft);
  77.  
  78.                 //The border is just a slightly bigger circle behind the mask circle
  79.                 float borderRadius = lerp(0.1, 0.495, _MaskRad);
  80.                 //You can easily achieve relative changes in radius for a distance field by modifying the distance result received from the distance function
  81.                 float maskRadius = borderRadius - _Border * 0.1;
  82.  
  83.                 //these are the distance fields returned per-pixel to define the shapes we need
  84.                 float dist_mask = dCircle(input.uv - 0.5, maskRadius);
  85.                 float dist_border = dCircle(input.uv - 0.5 , borderRadius);
  86.  
  87.                 //uncomment the following line to see a simple representation of the distance field
  88.                 //return abs(dist_mask);
  89.                 //uncomment the following line to see an exaggerated representation of the distance field
  90.                 //return abs(sin(dist_mask*6.0*6.28318));
  91.  
  92.                 //once we smoothstep a distance field, we are left with a [ 0 , 1 ] range result that looks like the shape we wanted
  93.                 float mask = smoothstep(s, -s, dist_mask);
  94.                 float border = smoothstep( s , -s , dist_border);
  95.  
  96.                 //use the larger border circle to define visible area
  97.                 result.a = border;
  98.                 //'discard'ing this fragment allows us to use 'ZWrite On' with our Alpha Blending
  99.                 //this works better with sharper edges
  100.                 if (result.a < 0.0001) discard;
  101.  
  102.                 //the actual color gradient
  103.                 float4 grad = lerp(_BottomColor, _TopColor, input.uv.y);
  104.  
  105.                 //here we overwrite the color based on our masks, in a specific order from back to front
  106.                 result.rgb = lerp(result.rgb, _BorderColor.rgb, border);
  107.                 result.rgb = lerp(result.rgb, grad, mask);
  108.  
  109.                 return result;
  110.             }
  111.             ENDCG
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement