Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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:
    3,494
    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:
    38
    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:
    38
  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:
    38
    Thanks for confirming! We now have a documentation ticket open to fix/improve the info.