Search Unity

Add custom texture field to URP shader

Discussion in 'Shaders' started by Rs, Nov 25, 2020.

  1. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    Hello,
    I've done lots of shaders with the old method but approaching the URP I am a bit lost. In the following example I am trying to add an alpha mask to the properties (original code is from the URP/Unlit shader) but it doesn't show up in the inspector. It does show up when I select the shader, under Default Properties, but not when I select the material. The shader is compiled with no errors.
    What am I missing?
    Is it even possible in URP?

    Thanks

    ps: follow the _AlphaMask addition at lines 7, 73, 74, 102, 104 (which I thought was enough to have it show up and do its job).

    Code (CSharp):
    1. Shader "Custom/URP/Unlit/UnlitColorTextureAlphaMasked"
    2. {
    3.     Properties
    4.     {
    5.         _BaseMap("Texture", 2D) = "white" {}
    6.         _BaseColor("Color", Color) = (1, 1, 1, 1)
    7.         _AlphaMask("Alpha Mask (grey)", 2D) = "white" {}
    8.         _Cutoff("AlphaCutout", Range(0.0, 1.0)) = 0.5
    9.  
    10.         // BlendMode
    11.         [HideInInspector] _Surface("__surface", Float) = 0.0
    12.         [HideInInspector] _Blend("__blend", Float) = 0.0
    13.         [HideInInspector] _AlphaClip("__clip", Float) = 0.0
    14.         [HideInInspector] _SrcBlend("Src", Float) = 1.0
    15.         [HideInInspector] _DstBlend("Dst", Float) = 0.0
    16.         [HideInInspector] _ZWrite("ZWrite", Float) = 1.0
    17.         [HideInInspector] _Cull("__cull", Float) = 2.0
    18.  
    19.         // Editmode props
    20.         [HideInInspector] _QueueOffset("Queue offset", Float) = 0.0
    21.  
    22.         // ObsoleteProperties
    23.         [HideInInspector] _MainTex("BaseMap", 2D) = "white" {}
    24.         [HideInInspector] _Color("Base Color", Color) = (0.5, 0.5, 0.5, 1)
    25.         [HideInInspector] _SampleGI("SampleGI", float) = 0.0 // needed from bakedlit
    26.     }
    27.     SubShader
    28.     {
    29.         Tags { "RenderType" = "Transparent" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
    30.         LOD 100
    31.  
    32.         Blend [_SrcBlend][_DstBlend]
    33.         ZWrite [_ZWrite]
    34.         Cull [_Cull]
    35.  
    36.         Pass
    37.         {
    38.             Name "Unlit"
    39.             HLSLPROGRAM
    40.             // Required to compile gles 2.0 with standard srp library
    41.             #pragma prefer_hlslcc gles
    42.             #pragma exclude_renderers d3d11_9x
    43.  
    44.             #pragma vertex vert
    45.             #pragma fragment frag
    46.             #pragma shader_feature _ALPHATEST_ON
    47.             #pragma shader_feature _ALPHAPREMULTIPLY_ON
    48.  
    49.             // -------------------------------------
    50.             // Unity defined keywords
    51.             #pragma multi_compile_fog
    52.             #pragma multi_compile_instancing
    53.  
    54.             #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    55.  
    56.             struct Attributes
    57.             {
    58.                 float4 positionOS       : POSITION;
    59.                 float2 uv               : TEXCOORD0;
    60.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    61.             };
    62.  
    63.             struct Varyings
    64.             {
    65.                 float2 uv        : TEXCOORD0;
    66.                 float fogCoord  : TEXCOORD1;
    67.                 float4 vertex : SV_POSITION;
    68.  
    69.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    70.                 UNITY_VERTEX_OUTPUT_STEREO
    71.             };
    72.  
    73.             TEXTURE2D(_AlphaMask);
    74.             SAMPLER(sampler_AlphaMask);
    75.  
    76.             CBUFFER_START(UnityPerMaterial)
    77.                 //
    78.             CBUFFER_END
    79.             Varyings vert(Attributes input)
    80.             {
    81.                 Varyings output = (Varyings)0;
    82.  
    83.                 UNITY_SETUP_INSTANCE_ID(input);
    84.                 UNITY_TRANSFER_INSTANCE_ID(input, output);
    85.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    86.  
    87.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    88.                 output.vertex = vertexInput.positionCS;
    89.                 output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
    90.                 output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
    91.  
    92.                 return output;
    93.             }
    94.  
    95.             half4 frag(Varyings input) : SV_Target
    96.             {
    97.                 UNITY_SETUP_INSTANCE_ID(input);
    98.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    99.  
    100.                 half2 uv = input.uv;
    101.                 half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
    102.                 half4 alphaMaskColor = SAMPLE_TEXTURE2D(_AlphaMask, sampler_AlphaMask, uv);
    103.                 half3 color = texColor.rgb * _BaseColor.rgb;
    104.                 half alpha = texColor.a * _BaseColor.a * alphaMaskColor.a;
    105.                 AlphaDiscard(alpha, _Cutoff);
    106.  
    107. #ifdef _ALPHAPREMULTIPLY_ON
    108.                 color *= alpha;
    109. #endif
    110.  
    111.                 color = MixFog(color, input.fogCoord);
    112.  
    113.                 return half4(color, alpha);
    114.             }
    115.             ENDHLSL
    116.         }
    117.         Pass
    118.         {
    119.             Tags{"LightMode" = "DepthOnly"}
    120.  
    121.             ZWrite On
    122.             ColorMask 0
    123.  
    124.             HLSLPROGRAM
    125.             // Required to compile gles 2.0 with standard srp library
    126.             #pragma prefer_hlslcc gles
    127.             #pragma exclude_renderers d3d11_9x
    128.             #pragma target 2.0
    129.  
    130.             #pragma vertex DepthOnlyVertex
    131.             #pragma fragment DepthOnlyFragment
    132.  
    133.             // -------------------------------------
    134.             // Material Keywords
    135.             #pragma shader_feature _ALPHATEST_ON
    136.  
    137.             //--------------------------------------
    138.             // GPU Instancing
    139.             #pragma multi_compile_instancing
    140.  
    141.             #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    142.             #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
    143.             ENDHLSL
    144.         }
    145.  
    146.         // This pass it not used during regular rendering, only for lightmap baking.
    147.         Pass
    148.         {
    149.             Name "Meta"
    150.             Tags{"LightMode" = "Meta"}
    151.  
    152.             Cull Off
    153.  
    154.             HLSLPROGRAM
    155.             // Required to compile gles 2.0 with standard srp library
    156.             #pragma prefer_hlslcc gles
    157.             #pragma exclude_renderers d3d11_9x
    158.             #pragma vertex UniversalVertexMeta
    159.             #pragma fragment UniversalFragmentMetaUnlit
    160.  
    161.             #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    162.             #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitMetaPass.hlsl"
    163.  
    164.             ENDHLSL
    165.         }
    166.     }
    167.     FallBack "Hidden/Universal Render Pipeline/FallbackError"
    168.     CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.UnlitShader"
    169. }
    170.  
     
    Last edited: Nov 25, 2020
  2. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    Answering my own question. I totally missed that a CustomEditor was invoked in the last line. It was looking a bit too fancy before.