Search Unity

Question URP Shader example doesn't work?

Discussion in 'Documentation' started by hangarter, Mar 5, 2023.

  1. hangarter

    hangarter

    Joined:
    Mar 14, 2016
    Posts:
    23
    Hi All,

    So I have been trying to follow the documentation from here: URP unlit basic shader | Universal RP | 12.1.10 (unity3d.com)

    The problem is that I do exactly what it tells me to do (create 3d obj, material, shader asset, than replace the code of shader.asset) BUT the shader renders fully transparent!
    Have anyone been through this?

    Thanks
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,059
    Share the shader you have
     
  3. hangarter

    hangarter

    Joined:
    Mar 14, 2016
    Posts:
    23
    It's the one below, literally a copy and paste from the documentation:

    Code (CSharp):
    1. // This shader fills the mesh shape with a color predefined in the code.
    2. Shader "Example/URPUnlitShaderBasic"
    3. {
    4.     // The properties block of the Unity shader. In this example this block is empty
    5.     // because the output color is predefined in the fragment shader code.
    6.     Properties
    7.     {
    8.         [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
    9.     }
    10.         // The SubShader block containing the Shader code.
    11.         SubShader
    12.     {
    13.         // SubShader Tags define when and under which conditions a SubShader block or
    14.         // a pass is executed.
    15.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
    16.  
    17.         Pass
    18.         {
    19.             // The HLSL code block. Unity SRP uses the HLSL language.
    20.             HLSLPROGRAM
    21.             // This line defines the name of the vertex shader.
    22.             #pragma vertex vert
    23.             // This line defines the name of the fragment shader.
    24.             #pragma fragment frag
    25.  
    26.             // The Core.hlsl file contains definitions of frequently used HLSL
    27.             // macros and functions, and also contains #include references to other
    28.             // HLSL files (for example, Common.hlsl, SpaceTransforms.hlsl, etc.).
    29.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    30.  
    31.             // The structure definition defines which variables it contains.
    32.             // This example uses the Attributes structure as an input structure in
    33.             // the vertex shader.
    34.             struct Attributes
    35.             {
    36.                 // The positionOS variable contains the vertex positions in object
    37.                 // space.
    38.                 float4 positionOS   : POSITION;
    39.             };
    40.  
    41.             struct Varyings
    42.             {
    43.                 // The positions in this struct must have the SV_POSITION semantic.
    44.                 float4 positionHCS  : SV_POSITION;
    45.             };
    46.  
    47.             // The vertex shader definition with properties defined in the Varyings
    48.             // structure. The type of the vert function must match the type (struct)
    49.             // that it returns.
    50.             Varyings vert(Attributes IN)
    51.             {
    52.                 // Declaring the output object (OUT) with the Varyings struct.
    53.                 Varyings OUT;
    54.                 // The TransformObjectToHClip function transforms vertex positions
    55.                 // from object space to homogenous clip space.
    56.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
    57.                 // Returning the output.
    58.                 return OUT;
    59.             }
    60.  
    61.             // The fragment shader definition.
    62.             half4 frag() : SV_Target
    63.             {
    64.                 // Defining the color variable and returning it.
    65.                 half4 customColor = half4(    1, 0, 0, 1);
    66.                 return customColor;
    67.             }
    68.             ENDHLSL
    69.         }
    70.     }
    71. }
     
  4. prodbld

    prodbld

    Joined:
    Mar 3, 2023
    Posts:
    1
    Have you found a solution yet? I also tried to follow the documentation and got the same result.
     
  5. berniegp

    berniegp

    Unity Technologies

    Joined:
    Sep 9, 2020
    Posts:
    42
    Which version of URP and Unity did you both use?
    Can you post a screenshot of what you got please?
     
  6. hangarter

    hangarter

    Joined:
    Mar 14, 2016
    Posts:
    23
    Apologies I haven't seen your answer before!
    The version of URP is 12.1.10.
    I created a material and assigned the shader to it, it's a capsule:
    upload_2023-3-26_13-49-47.png
     
  7. hangarter

    hangarter

    Joined:
    Mar 14, 2016
    Posts:
    23
    Hey I've just tried fiddling with the configuration and I can see that if "Rendering Path" in the URP-Asset is set to deferred, it renders the shader!
    If it's set to forward, it doesn't, @berniegp , would you know why?
    upload_2023-3-26_14-11-38.png

    upload_2023-3-26_14-12-5.png

    If that's a requirement, maybe that could go in the manual :D
     
    Last edited: Mar 26, 2023
  8. berniegp

    berniegp

    Unity Technologies

    Joined:
    Sep 9, 2020
    Posts:
    42
  9. Dropless

    Dropless

    Joined:
    May 1, 2022
    Posts:
    1
    I encounter the same problem here. By disabling "Depth Primming Mode", the shader works as expected.
     
  10. berniegp

    berniegp

    Unity Technologies

    Joined:
    Sep 9, 2020
    Posts:
    42
    Thanks for confirming! We now have a documentation ticket open to fix/improve the info.
     
  11. hystrix_

    hystrix_

    Joined:
    Sep 7, 2023
    Posts:
    2
    Hi! Sorry to message on an older thread but I've got the same original issue (shader renders completely transparent) and neither disabling Depth Priming Mode nor setting Rendering Path to Deferred fixes the issue for me.

    I'm using Unity Editor v2021.3.30f1 (LTS) with URP v12.1.12.
     
  12. hystrix_

    hystrix_

    Joined:
    Sep 7, 2023
    Posts:
    2
    I've found a solution (from this stackoverflow thread). The shader's Render Queue must be transparent (i.e. any value greater than 2500). In my shader I added
    "Queue"="AlphaTest+51"
    to the
    Tags
    block to get this working.

    Seems like a bug to me as I don't have a clue why this would be required to get the shader to render.

    Full shader code for reference:
    Code (CSharp):
    1. // This shader fills the mesh shape with a color predefined in the code.
    2. Shader "Example/URPUnlitShaderBasic"
    3. {
    4.     // The properties block of the Unity shader. In this example this block is empty
    5.     // because the output color is predefined in the fragment shader code.
    6.     Properties
    7.     { }
    8.  
    9.     // The SubShader block containing the Shader code.
    10.     SubShader
    11.     {
    12.         // SubShader Tags define when and under which conditions a SubShader block or
    13.         // a pass is executed.
    14.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue"="AlphaTest+51"}
    15.  
    16.         Pass
    17.         {
    18.             // The HLSL code block. Unity SRP uses the HLSL language.
    19.             HLSLPROGRAM
    20.             // This line defines the name of the vertex shader.
    21.             #pragma vertex vert
    22.             // This line defines the name of the fragment shader.
    23.             #pragma fragment frag
    24.  
    25.             // The Core.hlsl file contains definitions of frequently used HLSL
    26.             // macros and functions, and also contains #include references to other
    27.             // HLSL files (for example, Common.hlsl, SpaceTransforms.hlsl, etc.).
    28.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    29.  
    30.             // The structure definition defines which variables it contains.
    31.             // This example uses the Attributes structure as an input structure in
    32.             // the vertex shader.
    33.             struct Attributes
    34.             {
    35.                 // The positionOS variable contains the vertex positions in object
    36.                 // space.
    37.                 float4 positionOS   : POSITION;
    38.             };
    39.  
    40.             struct Varyings
    41.             {
    42.                 // The positions in this struct must have the SV_POSITION semantic.
    43.                 float4 positionHCS  : SV_POSITION;
    44.             };
    45.  
    46.             // The vertex shader definition with properties defined in the Varyings
    47.             // structure. The type of the vert function must match the type (struct)
    48.             // that it returns.
    49.             Varyings vert(Attributes IN)
    50.             {
    51.                 // Declaring the output object (OUT) with the Varyings struct.
    52.                 Varyings OUT;
    53.                 // The TransformObjectToHClip function transforms vertex positions
    54.                 // from object space to homogenous clip space.
    55.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
    56.                 // Returning the output.
    57.                 return OUT;
    58.             }
    59.  
    60.             // The fragment shader definition.
    61.             half4 frag() : SV_Target
    62.             {
    63.                 // Defining the color variable and returning it.
    64.                 half4 customColor = half4(0.5, 0, 0, 1);
    65.                 return customColor;
    66.             }
    67.             ENDHLSL
    68.         }
    69.     }
    70. }