Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

how do you make an UI additive shader in URP?

Discussion in 'Universal Render Pipeline' started by laurentlavigne, Oct 16, 2020.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,421
    in the game view
    upload_2020-10-16_14-37-37.png
    in the scene view it looks proper
    upload_2020-10-16_14-37-51.png
    see the huge difference?
    this is the shader, it used to work in built in, no idea why it shows up as normal blend instead of additive in URP view

    Code (CSharp):
    1. // ----------------------------------------------------------------------------
    2. // <copyright file="AdditiveUI.shader" company="Unity Technologies / Supyrb">
    3. //   Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    4. //   Copyright (c) 2017 Supyrb. All rights reserved.
    5. // </copyright>
    6. // <author>
    7. //   Johannes Deml
    8. //   send@johannesdeml.com
    9. // </author>
    10. // ----------------------------------------------------------------------------
    11. Shader "UI/Additive"
    12. {
    13.     Properties
    14.     {
    15.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    16.         _Color ("Tint", Color) = (1,1,1,1)
    17.    
    18.         _StencilComp ("Stencil Comparison", Float) = 8
    19.         _Stencil ("Stencil ID", Float) = 0
    20.         _StencilOp ("Stencil Operation", Float) = 0
    21.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    22.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    23.         _ColorMask ("Color Mask", Float) = 15
    24.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    25.     }
    26.     SubShader
    27.     {
    28.         Tags
    29.         {
    30.             "Queue"="Transparent"
    31.             "IgnoreProjector"="True"
    32.             "RenderType"="Transparent"
    33.             "PreviewType"="Plane"
    34.             "CanUseSpriteAtlas"="True"
    35.         }
    36.    
    37.         Stencil
    38.         {
    39.             Ref [_Stencil]
    40.             Comp [_StencilComp]
    41.             Pass [_StencilOp]
    42.             ReadMask [_StencilReadMask]
    43.             WriteMask [_StencilWriteMask]
    44.         }
    45.         Cull Off
    46.         Lighting Off
    47.         ZWrite Off
    48.         ZTest [unity_GUIZTestMode]
    49.         Blend SrcAlpha One
    50.         ColorMask [_ColorMask]
    51.         Pass
    52.         {
    53.             Name "Default"
    54.         CGPROGRAM
    55.             #pragma vertex vert
    56.             #pragma fragment frag
    57.             #pragma target 2.0
    58.             #include "UnityCG.cginc"
    59.             #include "UnityUI.cginc"
    60.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
    61.        
    62.             struct appdata_t
    63.             {
    64.                 float4 vertex   : POSITION;
    65.                 float4 color    : COLOR;
    66.                 float2 texcoord : TEXCOORD0;
    67.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    68.             };
    69.             struct v2f
    70.             {
    71.                 float4 vertex   : SV_POSITION;
    72.                 fixed4 color    : COLOR;
    73.                 float2 texcoord  : TEXCOORD0;
    74.                 float4 worldPosition : TEXCOORD1;
    75.                 UNITY_VERTEX_OUTPUT_STEREO
    76.             };
    77.        
    78.             fixed4 _Color;
    79.             fixed4 _TextureSampleAdd;
    80.             float4 _ClipRect;
    81.             v2f vert(appdata_t IN)
    82.             {
    83.                 v2f OUT;
    84.                 UNITY_SETUP_INSTANCE_ID(IN);
    85.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    86.                 OUT.worldPosition = IN.vertex;
    87.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
    88.                 OUT.texcoord = IN.texcoord;
    89.            
    90.                 OUT.color = IN.color * _Color;
    91.                 return OUT;
    92.             }
    93.             sampler2D _MainTex;
    94.             fixed4 frag(v2f IN) : SV_Target
    95.             {
    96.                 half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
    97.            
    98.                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    99.            
    100.                 #ifdef UNITY_UI_ALPHACLIP
    101.                 clip (color.a - 0.001);
    102.                 #endif
    103.                 return color;
    104.             }
    105.         ENDCG
    106.         }
    107.     }
    108. }
    109.