Converting custom shader from Standard Pipeline to URP problems

Hello, I am new to writing shaders, and having some problems.

I have a script that randomly places thousands of an instanced models all around my world.
It works as intended in the standard pipeline.
However when I bring it to URP it does not work at all, the objects all are placed in the world at 0,0,0

Here is the standard pipeline version:

Shader "Test/ObjectTest" {

    Properties{
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Albedo (RGB)", 2D) = "white" {}
         _BumpMap("Bumpmap", 2D) = "bump" {}
         _MetallicGlossMap("Metallic", 2D) = "white" {}
         _Metallic("Metallic", Range(0,1)) = 0.0
         _Glossiness("Smoothness", Range(0,1)) = 1.0
    }

        SubShader{
             Cull Off

             CGPROGRAM

             sampler2D _MainTex;
             sampler2D _BumpMap;
             sampler2D _MetallicGlossMap;
             struct Input {
                 float2 uv_MainTex;
                 float2 uv_BumpMap;
                 float3 worldPos;
             };
             half _Glossiness;
             half _Metallic;
             fixed4 _Color;

             #pragma surface surf Standard vertex:vert addshadow nolightmap
             #pragma instancing_options procedural:setup

             float3 _TreePosition;

            #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
                 struct Tree
                 {
                     float3 position;
                 };

                 StructuredBuffer<Tree> treeBuffer;
              #endif


              void vert(inout appdata_full v, out Input data)
             {
                 UNITY_INITIALIZE_OUTPUT(Input, data);

            #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
                     v.vertex.xyz += _TreePosition;
                 #endif
             }

             void setup()
             {
                #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED             
                     _TreePosition = treeBuffer[unity_InstanceID].position;
                 #endif
             }

              void surf(Input IN, inout SurfaceOutputStandard o) {
                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
                 fixed4 m = tex2D(_MetallicGlossMap, IN.uv_MainTex);
                 o.Albedo = c.rgb;
                 o.Alpha = c.a;
                 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
                 o.Metallic = m.r;
                 o.Smoothness = _Glossiness * m.a;
              }

              ENDCG
         }
}

If I put that into URP it does nothing. I belive it’s the #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED, that is not triggering, if I remove that if, I get an error because it does not know what a StructuredBuffer is

In URP I switch it out to a HLSL program, but keep getting an:
“Unexpected identifier “fixed4”. Expected one of: typedef const void inline uniform nointerpolation extern shared static volatile row_major column_major struct sampler or a user-defined type”

Here is my URP shader code (I don’t care about it displaying textures or anything right now, just want all the random positions I am feeding into the buffer for now to work)

Shader "Test/HSLSTest" {

    Properties{
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Albedo (RGB)", 2D) = "white" {}
         _BumpMap("Bumpmap", 2D) = "bump" {}
    }

        SubShader{

             HLSLPROGRAM
             #include "UnityCG.cginc"

             sampler2D _MainTex;
             sampler2D _BumpMap;

             sampler2D _MetallicGlossMap;
             struct Input {
                 float2 uv_MainTex;
                 float2 uv_BumpMap;
                 float3 worldPos;
             };

             float4 _Color;

             #pragma surface surf Standard vertex:vert addshadow nolightmap
             #pragma instancing_options procedural:setup

             float3 _TreePosition;
                 struct Tree
                 {
                     float3 position;
                 };

            StructuredBuffer<Tree> treeBuffer;

              void vert(inout appdata_full v, out Input data)
             {
                 UNITY_INITIALIZE_OUTPUT(Input, data);

                     v.vertex.xyz += _TreePosition;
             }

             void setup()
             {        
                     _TreePosition = treeBuffer[unity_InstanceID].position;
             }

              void surf(Input IN, inout SurfaceOutputStandard o) {
                 float4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
                 float4 m = tex2D(_MetallicGlossMap, IN.uv_MainTex);
                 o.Albedo = c.rgb;
                 o.Alpha = c.a;
                 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
              }

              ENDHLSL
         }
}

Any suggestions on how to do this in URP?

Thanks

1 Like

You need to migrate your shader from ShaderLab(Built-In Shader) to URP.
Please refer to given below free ebook.

https://developer.arm.com/documentation/102487/0100/Migrating-built-in-shaders-to-the-Universal-Render-Pipeline