Advertisement
Guest User

UI Ubershader

a guest
Oct 11th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "_GoodCompany/GC_UI_UberShader"
  2. {
  3.     Properties
  4.     {
  5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6.  
  7.         [Header(Shader Features)]
  8.         [Toggle(MIPMAP_BIAS)]_a ("Custom Mipmap Bias", float) = 0
  9.         [Toggle(RECTMASK)]_c ("Rectmask clipping", float) = 0
  10.         [Toggle(SCROLLING)]_e ("Scrolling Texture", float) = 0
  11.         [Toggle(IMPROVED_ALPHA)]_f ("Harder Alpha", float) = 0
  12.         [Toggle(EXTERNAL_ALPHA)]_g ("Use external alpha texture", float) = 0
  13.         [Toggle(COLOR_TRANSITION)]_h ("Do Color Transition", float) = 0
  14.         [Toggle(TEXTURE_TRANSITION)]_i ("Do Texture Transition", float) = 0
  15.         [Toggle(PREMULTIPLIED)]_j ("Premultiplied alpha (blend mode One/OneMinusSrcAlpha)", float) = 0
  16.  
  17.         [Header(Settings)]
  18.         _Color("Tint", Color) = (1,1,1,1)
  19.         _Scrolling("Scrolling direction", Vector) = (1,1,0,0)
  20.         _MipmapBias("Mipmap bias", Range(-2, 1)) = 0
  21.         _ExternalAlpha("External alpha texture", 2D) = "white" {}
  22.         _TransitionReferenceTexture("Transition Reference Texture (greyscale)", 2D) = "white" {}
  23.         _TransitionColorTexture("Transition Color Texture", 2D) = "grey" {}
  24.         _TransitionColor("Transition Color", Color) = (0, 0, 0, 0)
  25.         _TransitionValue("Transition Value", Range(0, 1)) = 1
  26.  
  27.         [Space(20)]
  28.         [Header(BlendMode)]
  29.         [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("Source", Float) = 1
  30.         [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("Destination", Float) = 10
  31.  
  32.         //stuff the user probably doesn't understand anyways, we can bring it back if we end up needing it,
  33.         //but until then it's here and scripts can touch it as usual
  34.         [HideInInspector]_StencilComp("Stencil Comparison", Float) = 8
  35.         [HideInInspector]_Stencil("Stencil ID", Float) = 0
  36.         [HideInInspector]_StencilOp("Stencil Operation", Float) = 0
  37.         [HideInInspector]_StencilWriteMask("Stencil Write Mask", Float) = 255
  38.         [HideInInspector]_StencilReadMask("Stencil Read Mask", Float) = 255
  39.  
  40.         [HideInInspector]_ColorMask("Color Mask", Float) = 15
  41.     }
  42.  
  43.     SubShader
  44.     {
  45.         Tags
  46.         {
  47.             "Queue" = "Transparent"
  48.             "IgnoreProjector" = "True"
  49.             "RenderType" = "Transparent"
  50.             "PreviewType" = "Plane"
  51.             "CanUseSpriteAtlas" = "True"
  52.         }
  53.  
  54.         Stencil
  55.         {
  56.             Ref[_Stencil]
  57.             Comp[_StencilComp]
  58.             Pass[_StencilOp]
  59.             ReadMask[_StencilReadMask]
  60.             WriteMask[_StencilWriteMask]
  61.         }
  62.  
  63.         LOD 0
  64.  
  65.         Cull Off
  66.         Lighting Off
  67.         ZWrite Off
  68.         ZTest Always
  69.         Blend [_SrcBlend] [_DstBlend]
  70.         ColorMask[_ColorMask]
  71.  
  72.         Pass
  73.         {
  74.             CGPROGRAM
  75.             #pragma vertex vert
  76.             #pragma fragment frag
  77.  
  78.             #include "UnityCG.cginc"
  79.             #include "UnityUI.cginc"
  80.  
  81.             #pragma shader_feature MIPMAP_BIAS
  82.             #pragma shader_feature UNITY_UI_ALPHACLIP
  83.             #pragma shader_feature RECTMASK
  84.             #pragma shader_feature SCROLLING
  85.             #pragma shader_feature IMPROVED_ALPHA
  86.             #pragma shader_feature EXTERNAL_ALPHA
  87.             #pragma shader_feature COLOR_TRANSITION
  88.             #pragma shader_feature TEXTURE_TRANSITION
  89.             #pragma shader_feature PREMULTIPLIED
  90.  
  91.             struct appdata_t
  92.             {
  93.                 float4 vertex   : POSITION;
  94.                 float4 color    : COLOR;
  95.                 float2 texcoord : TEXCOORD0;
  96.             };
  97.  
  98.             struct v2f
  99.             {
  100.                 float4 vertex   : SV_POSITION;
  101.                 fixed4 color : COLOR;
  102.                 half2 texcoord  : TEXCOORD0;
  103.                 float4 worldPos : TEXCOORD2;
  104.             };
  105.  
  106.             fixed4 _Color;
  107.             sampler2D _MainTex;
  108.             bool _UseAlphaClip;
  109.  
  110. #ifdef SCROLLING
  111.             float2 _Scrolling;
  112. #endif
  113. #ifdef RECTMASK
  114.             float4 _ClipRect;
  115. #endif
  116. #ifdef MIPMAP_BIAS
  117.             float _MipmapBias;
  118. #endif
  119. #ifdef EXTERNAL_ALPHA
  120.             sampler2D _ExternalAlpha;
  121. #endif
  122. #ifdef COLOR_TRANSITION
  123.             sampler2D _TransitionReferenceTexture;
  124.             float4 _TransitionColor;
  125.             float _TransitionValue;
  126. #else
  127.     #ifdef TEXTURE_TRANSITION
  128.             sampler2D _TransitionReferenceTexture;
  129.             sampler2D _TransitionColorTexture;
  130.             float _TransitionValue;
  131.     #endif
  132. #endif
  133.  
  134.             v2f vert(appdata_t IN)
  135.             {
  136.                 v2f OUT;
  137.                 OUT.worldPos = IN.vertex;
  138.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
  139.                 OUT.texcoord = IN.texcoord;
  140.  
  141.                 #ifdef UNITY_HALF_TEXEL_OFFSET
  142.                     OUT.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1);
  143.                 #endif
  144.  
  145.                 OUT.color = IN.color * _Color;
  146.                 return OUT;
  147.             }
  148.  
  149.             float _CutOff;
  150.             bool _HardBlend = false;
  151.             bool _NoOuterClip = false;
  152.  
  153.             fixed4 frag(v2f IN) : SV_Target
  154.             {
  155.                 float2 scrollingCoordinates = IN.texcoord;
  156. #ifdef SCROLLING
  157.                 scrollingCoordinates += _Scrolling * _Time.y;
  158. #endif
  159. #ifdef MIPMAP_BIAS
  160.                 half4 color = tex2Dbias(_MainTex, float4(scrollingCoordinates, 0, _MipmapBias));
  161. #else
  162.                 half4 color = tex2D(_MainTex, scrollingCoordinates);
  163. #endif
  164.                 color *= IN.color;
  165. #ifdef PREMULTIPLIED
  166.                 color.rgb *= IN.color.a;
  167. #endif
  168. #ifdef COLOR_TRANSITION
  169.                 float transitionTex = tex2D(_TransitionReferenceTexture, IN.texcoord).r;
  170.                 float transitionTexChange = fwidth(transitionTex) * 0.5;
  171.                 float4 transitionColor = float4(_TransitionColor.rgb, min(color.a, _TransitionColor.a));
  172.     #ifdef PREMULTIPLIED
  173.                 transitionColor.rgb *= transitionColor.a;
  174.     #endif
  175.                 float transitionAmount = 1 - smoothstep(_TransitionValue - transitionTexChange, _TransitionValue + transitionTexChange, 1 - transitionTex);
  176.                 color = lerp(color, transitionColor, transitionAmount);
  177. #else
  178.     #ifdef TEXTURE_TRANSITION
  179.                 float transitionTex = tex2D(_TransitionReferenceTexture, IN.texcoord).r;
  180.                 float transitionTexChange = fwidth(transitionTex) * 0.5;
  181.         #ifdef MIPMAP_BIAS
  182.                 float4 transitionColor = tex2Dbias(_TransitionColorTexture, float4(scrollingCoordinates, 0, _MipmapBias)) * IN.color;
  183.         #else
  184.                 float4 transitionColor = tex2D(_TransitionColorTexture, scrollingCoordinates) * IN.color;
  185.         #endif
  186.                 float transitionAmount = 1 - smoothstep(_TransitionValue - transitionTexChange, _TransitionValue + transitionTexChange, 1 - transitionTex);
  187.                 color = lerp(color, transitionColor, transitionAmount);
  188.     #endif
  189. #endif
  190. #ifdef RECTMASK
  191.     #ifdef PREMULTIPLIED
  192.                 color.rgba *= UnityGet2DClipping(IN.worldPos.xy, _ClipRect);
  193.     #else
  194.                 color.a *= UnityGet2DClipping(IN.worldPos.xy, _ClipRect);
  195.     #endif
  196. #endif
  197. #ifdef EXTERNAL_ALPHA
  198.     #ifdef MIPMAP_BIAS
  199.                 float externalAlpha = tex2Dbias(_ExternalAlpha, float4(IN.texcoord, 0, _MipmapBias)).a;
  200.     #else
  201.                 float externalAlpha = tex2D(_ExternalAlpha, IN.texcoord).a;
  202.     #endif
  203.     #ifdef PREMULTIPLIED
  204.                 color.rgba *= externalAlpha;
  205.     #else
  206.                 color.a *= externalAlpha;
  207.     #endif
  208. #endif
  209. #ifdef UNITY_UI_ALPHACLIP //if the keyword is set we don't even ask the property, we just clip
  210.                 clip(color.a - 0.001);
  211. #else          
  212.                 if (_UseAlphaClip) 
  213.                     clip(color.a - 0.001);
  214. #endif
  215. #ifdef IMPROVED_ALPHA
  216.                 float alphaChange = fwidth(color.a) * 0.5;
  217.                 float newAlpha = smoothstep(0.5-alphaChange, 0.5+alphaChange, color.a);
  218.     #ifdef PREMULTIPLIED
  219.                 //max is here to prevent infinity
  220.                 float relativeAlpha = newAlpha / max(color.a, 0.000001);
  221.                 color.rgba *= relativeAlpha;
  222.     #else
  223.                 color.a = newAlpha;
  224.     #endif
  225. #endif
  226.                 return color;
  227.             }
  228.             ENDCG
  229.         }
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement