Advertisement
Guest User

Border.shader

a guest
Jul 28th, 2022
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2. // very simple edit to turn into scrolling texture setup by @Minionsart
  3. Shader "UI/Border"
  4. {
  5.     Properties
  6.     {
  7.         _MainTex ("Sprite Texture", 2D) = "white" {}
  8.         _Color ("Tint", Color) = (1,1,1,1)
  9.         _SpeedX("Speed X", Range(-4,4)) = 1.0
  10.         _SpeedY("Speed Y", Range(-4,4)) = 1.0
  11.         _Gradient("Color Map", 2D) = "white" {}
  12.  
  13.         _Fade("Fade Top", Range(0,1)) = 1.0
  14.         _Fade2("Fade Bottom", Range(0,1)) = 1.0
  15.         [Toggle(FadeEdges)] _FadeEdges("Fade Edges", Float) = 0
  16.  
  17.         _StencilComp ("Stencil Comparison", Float) = 8
  18.         _Stencil ("Stencil ID", Float) = 0
  19.         _StencilOp ("Stencil Operation", Float) = 0
  20.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
  21.         _StencilReadMask ("Stencil Read Mask", Float) = 255
  22.         _ColorMask ("Color Mask", Float) = 15
  23.  
  24.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  25.     }
  26.  
  27.     SubShader
  28.     {
  29.         Tags
  30.         {
  31.             "Queue"="Transparent"
  32.             "IgnoreProjector"="True"
  33.             "RenderType"="Transparent"
  34.             "PreviewType"="Plane"
  35.             "CanUseSpriteAtlas"="True"
  36.         }
  37.  
  38.         Stencil
  39.         {
  40.             Ref [_Stencil]
  41.             Comp [_StencilComp]
  42.             Pass [_StencilOp]
  43.             ReadMask [_StencilReadMask]
  44.             WriteMask [_StencilWriteMask]
  45.         }
  46.  
  47.         Cull Off
  48.         Lighting Off
  49.         ZWrite Off
  50.         ZTest [unity_GUIZTestMode]
  51.         Blend SrcAlpha OneMinusSrcAlpha
  52.         ColorMask [_ColorMask]
  53.  
  54.         Pass
  55.         {
  56.             Name "Default"
  57.             CGPROGRAM
  58.             #pragma vertex vert
  59.             #pragma fragment frag
  60.             #pragma target 2.0
  61.  
  62.             #include "UnityCG.cginc"
  63.             #include "UnityUI.cginc"
  64.  
  65.             #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
  66.             #pragma multi_compile_local _ UNITY_UI_ALPHACLIP
  67.             #pragma shader_feature FadeEdges
  68.  
  69.             struct appdata_t
  70.             {
  71.                 float4 vertex   : POSITION;
  72.                 float4 color    : COLOR;
  73.                 float2 texcoord : TEXCOORD0;
  74.                 UNITY_VERTEX_INPUT_INSTANCE_ID
  75.             };
  76.  
  77.             struct v2f
  78.             {
  79.                 float4 vertex   : SV_POSITION;
  80.                 fixed4 color    : COLOR;
  81.                 float2 texcoord  : TEXCOORD0;
  82.                 float2 texcoord2 : TEXCOORD2;
  83.                 float4 worldPosition : TEXCOORD1;
  84.                 UNITY_VERTEX_OUTPUT_STEREO
  85.             };
  86.  
  87.             sampler2D _MainTex;
  88.             fixed4 _Color;
  89.             fixed4 _TextureSampleAdd;
  90.             float4 _ClipRect;
  91.             float4 _MainTex_ST;
  92.             float _SpeedX, _SpeedY;
  93.             sampler2D _Gradient;
  94.             float _Fade, _Fade2;
  95.  
  96.             v2f vert(appdata_t v)
  97.             {
  98.                 v2f OUT;
  99.                 UNITY_SETUP_INSTANCE_ID(v);
  100.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  101.                 OUT.worldPosition = v.vertex;
  102.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  103.  
  104.                 OUT.texcoord = v.texcoord;
  105.                 OUT.texcoord2 = TRANSFORM_TEX(v.texcoord, _MainTex);
  106.                 OUT.color = v.color * _Color;
  107.                 return OUT;
  108.             }
  109.  
  110.             fixed4 frag(v2f IN) : SV_Target
  111.             {
  112.                 // create scrolling uvs
  113.                 float2 movingUV = float2(IN.texcoord2.x + (_Time.x * _SpeedX), IN.texcoord2.y + (_Time.y * _SpeedY));
  114.                 // main texture
  115.                 half4 mainTex = (tex2D(_MainTex, movingUV) + _TextureSampleAdd) * IN.color;
  116.  
  117.                 #ifdef UNITY_UI_CLIP_RECT
  118.                     mainTex.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  119.                 #endif
  120.                 // clipping
  121.                 #ifdef UNITY_UI_ALPHACLIP
  122.                     clip (mainTex.a - 0.001);
  123.                 #endif
  124.                 // use the main texture to colorize the whole thing
  125.                 float4 colorMap = tex2D(_Gradient, mainTex)  ;
  126.                 colorMap *= mainTex.a;
  127.                 // fade the edges
  128.                 #if FadeEdges
  129.                     float gradientfalloff =   smoothstep(0.99, _Fade, IN.texcoord.y) * smoothstep(0.99, _Fade2,1- IN.texcoord.y);
  130.                     colorMap *= gradientfalloff;
  131.                 #endif
  132.                 return colorMap;
  133.             }
  134.             ENDCG
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement