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.
  2. Dismiss Notice

Help with Converting to URP

Discussion in 'Universal Render Pipeline' started by maxostrach, Feb 7, 2021.

  1. maxostrach

    maxostrach

    Joined:
    Apr 8, 2020
    Posts:
    9
    Hello, I am working on a card game and I stumbled upon this really cool free asset:

    https://assetstore.unity.com/packages/vfx/shaders/3d-game-card-146177

    Unfortunately, this does not work in URP where I am building this project. I lack the shader skills to wrap my head around this, so I was wondering if there was anyone who could help me to convert this to be compatible in URP.

    It's a free asset so I'm assuming it is okay to post the code:
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Stencils/StencilMask" {
    4.     Properties {
    5.         [IntRange] _StencilMask("Stencil Mask", Range(0, 255)) = 0
    6.     }
    7.     SubShader {
    8.         Tags {
    9.             "RenderType" = "Opaque"
    10.             "Queue" = "Geometry-100"
    11.         }
    12.         ColorMask 0
    13.         ZWrite off
    14.         Stencil {
    15.             Ref[_StencilMask]
    16.             Comp always
    17.             Pass replace
    18.         }
    19.  
    20.         Pass {
    21.             CGPROGRAM
    22.             #include "UnityCG.cginc"
    23.  
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.  
    27.             struct appdata {
    28.                 float4 vertex : POSITION;
    29.             };
    30.  
    31.             struct v2f {
    32.                 float4 position : SV_POSITION;
    33.             };
    34.  
    35.             v2f vert(appdata v) {
    36.                 v2f o;
    37.                 o.position = UnityObjectToClipPos(v.vertex);
    38.                 return o;
    39.             }
    40.  
    41.             half4 frag(v2f i) : COLOR {
    42.                 return half4(1,1,0,1);
    43.             }
    44.             ENDCG
    45.         }
    46.     }
    47. }

    and then:

    Code (CSharp):
    1. Shader "Stencils/StencilObject" {
    2.     Properties {
    3.         [IntRange] _StencilMask("Stencil Mask", Range(0, 255)) = 0
    4.  
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.         Stencil {
    14.             Ref[_StencilMask]
    15.             Comp equal
    16.             Pass keep
    17.             Fail keep
    18.         }
    19.  
    20.         CGPROGRAM
    21.  
    22.         // Physically based Standard lighting model, and enable shadows on all light types
    23.         #pragma surface surf Standard fullforwardshadows
    24.  
    25.         // Use shader model 2.0 target, to get nicer looking lighting
    26.         #pragma target 2.0
    27.  
    28.         sampler2D _MainTex;
    29.  
    30.         struct Input {
    31.             float2 uv_MainTex;
    32.         };
    33.  
    34.         half _Glossiness;
    35.         half _Metallic;
    36.         fixed4 _Color;
    37.  
    38.         void surf (Input IN, inout SurfaceOutputStandard o) {
    39.  
    40.             // Albedo comes from a texture tinted by color
    41.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    42.             o.Albedo = c.rgb;
    43.  
    44.             // Metallic and smoothness come from slider variables
    45.             o.Metallic = _Metallic;
    46.             o.Smoothness = _Glossiness;
    47.             o.Alpha = c.a;
    48.         }
    49.         ENDCG
    50.     }
    51.     FallBack "Diffuse"
    52. }
    53.  
    Any help would be much appreciated. Thanks.
     
  2. Jonas-Mortensen

    Jonas-Mortensen

    Unity Technologies

    Joined:
    Jan 3, 2020
    Posts:
    98
    Hi!
    The reason this is not working in URP is that Surface Shaders (in this case Stencils/StencilObject) are not supported. Unfortunately, shader graph doesn't support stencils so you will have to do a workaround until either stencils in shader graph or surface shaders are supported in URP.

    The workaround:
    Create a Shader/Universal Render Pipeline/Lit Shader Graph with the exposed properties you need. This will replace the StencilObject shader from the asset. An example could be:
    StencilObjects_Graph.png

    Locate the shader graph asset in the project tab and click the "Copy Shader" button. The reason for this is that we want to modify the coded shader that shader graph generated. Create a new unlit shader (call it StencilObject) and paste the copied shader graph shader. This is a huge and complex shader but we only need to modify a tiny bit so don't worry :)

    Rename the shader on line 1 to whatever suits you (e.g. Shader "Stencils/StencilObject" )

    Copy the stencil mask property (line 3) from Asset version of the StencilObject shader and paste it into the properties section of the shader graph shader.

    Copy the stencil section (line 14-17) from the Asset version and paste it under the Render State lines of the shader graph shader to make it look like this:
    RenderState.png

    Now you should be able create the effect from the asset. Just set up the stencil masks and materials.

    Hope this helps! :)
     
    Nemean, maxostrach and tree_arb like this.
  3. maxostrach

    maxostrach

    Joined:
    Apr 8, 2020
    Posts:
    9
    Thank you so much! You explained everything so well I'm now less afraid of shaders. Cheers!