Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Converting custom shader from Standard Pipeline to URP problems

Discussion in 'Shaders' started by DressedToParty, Sep 15, 2021.

  1. DressedToParty

    DressedToParty

    Joined:
    Mar 7, 2019
    Posts:
    4
    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:
    Code (CSharp):
    1. Shader "Test/ObjectTest" {
    2.  
    3.     Properties{
    4.          _Color("Color", Color) = (1,1,1,1)
    5.          _MainTex("Albedo (RGB)", 2D) = "white" {}
    6.          _BumpMap("Bumpmap", 2D) = "bump" {}
    7.          _MetallicGlossMap("Metallic", 2D) = "white" {}
    8.          _Metallic("Metallic", Range(0,1)) = 0.0
    9.          _Glossiness("Smoothness", Range(0,1)) = 1.0
    10.     }
    11.  
    12.         SubShader{
    13.              Cull Off
    14.  
    15.              CGPROGRAM
    16.  
    17.              sampler2D _MainTex;
    18.              sampler2D _BumpMap;
    19.              sampler2D _MetallicGlossMap;
    20.              struct Input {
    21.                  float2 uv_MainTex;
    22.                  float2 uv_BumpMap;
    23.                  float3 worldPos;
    24.              };
    25.              half _Glossiness;
    26.              half _Metallic;
    27.              fixed4 _Color;
    28.  
    29.              #pragma surface surf Standard vertex:vert addshadow nolightmap
    30.              #pragma instancing_options procedural:setup
    31.  
    32.              float3 _TreePosition;
    33.  
    34.             #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    35.                  struct Tree
    36.                  {
    37.                      float3 position;
    38.                  };
    39.  
    40.                  StructuredBuffer<Tree> treeBuffer;
    41.               #endif
    42.  
    43.  
    44.               void vert(inout appdata_full v, out Input data)
    45.              {
    46.                  UNITY_INITIALIZE_OUTPUT(Input, data);
    47.  
    48.             #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
    49.                      v.vertex.xyz += _TreePosition;
    50.                  #endif
    51.              }
    52.  
    53.              void setup()
    54.              {
    55.                 #ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED            
    56.                      _TreePosition = treeBuffer[unity_InstanceID].position;
    57.                  #endif
    58.              }
    59.  
    60.               void surf(Input IN, inout SurfaceOutputStandard o) {
    61.                  fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    62.                  fixed4 m = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    63.                  o.Albedo = c.rgb;
    64.                  o.Alpha = c.a;
    65.                  o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    66.                  o.Metallic = m.r;
    67.                  o.Smoothness = _Glossiness * m.a;
    68.               }
    69.  
    70.               ENDCG
    71.          }
    72. }
    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)

    Code (CSharp):
    1. Shader "Test/HSLSTest" {
    2.  
    3.     Properties{
    4.          _Color("Color", Color) = (1,1,1,1)
    5.          _MainTex("Albedo (RGB)", 2D) = "white" {}
    6.          _BumpMap("Bumpmap", 2D) = "bump" {}
    7.     }
    8.  
    9.         SubShader{
    10.  
    11.              HLSLPROGRAM
    12.              #include "UnityCG.cginc"
    13.  
    14.              sampler2D _MainTex;
    15.              sampler2D _BumpMap;
    16.  
    17.              sampler2D _MetallicGlossMap;
    18.              struct Input {
    19.                  float2 uv_MainTex;
    20.                  float2 uv_BumpMap;
    21.                  float3 worldPos;
    22.              };
    23.  
    24.              float4 _Color;
    25.  
    26.              #pragma surface surf Standard vertex:vert addshadow nolightmap
    27.              #pragma instancing_options procedural:setup
    28.  
    29.              float3 _TreePosition;
    30.                  struct Tree
    31.                  {
    32.                      float3 position;
    33.                  };
    34.  
    35.             StructuredBuffer<Tree> treeBuffer;
    36.  
    37.               void vert(inout appdata_full v, out Input data)
    38.              {
    39.                  UNITY_INITIALIZE_OUTPUT(Input, data);
    40.  
    41.                      v.vertex.xyz += _TreePosition;
    42.              }
    43.  
    44.              void setup()
    45.              {        
    46.                      _TreePosition = treeBuffer[unity_InstanceID].position;
    47.              }
    48.  
    49.               void surf(Input IN, inout SurfaceOutputStandard o) {
    50.                  float4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    51.                  float4 m = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    52.                  o.Albedo = c.rgb;
    53.                  o.Alpha = c.a;
    54.                  o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    55.               }
    56.  
    57.               ENDHLSL
    58.          }
    59. }
    Any suggestions on how to do this in URP?

    Thanks
     
    Krishx007 likes this.
  2. Krishx007

    Krishx007

    Joined:
    Jul 15, 2014
    Posts:
    14