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 Shader with Texture Array not working with Hybrid Renderer v2

Discussion in 'DOTS Graphics' started by soundeos, Apr 6, 2022.

  1. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    My custom shader stopped working after upgrading to hybrid renderer v2. Here is the shader;
    Code (CSharp):
    1. Shader "Custom/Terrain" {
    2.     Properties
    3.     {
    4.         _textureArray ("TextureArray", 2DArray) = "" {}
    5.     }
    6.     SubShader
    7.     {
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 200
    10.  
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #pragma multi_compile_instancing
    17.             #pragma require 2darray
    18.            
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float3 uv : TEXCOORD2;
    25.                 float4 color: COLOR;
    26.  
    27.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float4 color : COLOR;
    33.                 float3 terrain : TEXCOORD2;
    34.                 float4 pos : SV_POSITION;
    35.                 float3 worldPos : TEXCOORD1;
    36.  
    37.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    38.                 UNITY_VERTEX_OUTPUT_STEREO
    39.             };
    40.  
    41.             UNITY_DECLARE_TEX2DARRAY(_textureArray);
    42.             float4 _textureArray_ST;
    43.  
    44.             v2f vert (appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.pos = UnityObjectToClipPos(v.vertex);
    48.  
    49.                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    50.                 o.worldPos.xyz = worldPos;
    51.  
    52.                 o.terrain = v.uv.xyz;
    53.                 o.color = v.color;
    54.  
    55.                 UNITY_SETUP_INSTANCE_ID(v);
    56.                 UNITY_TRANSFER_INSTANCE_ID(v, o);
    57.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    58.  
    59.                 return o;
    60.             }
    61.  
    62.             float4 GetTerrainColor (v2f IN, int index) {
    63.                 float3 uvw = float3(IN.worldPos.xy * 0.2, IN.terrain[index]);
    64.                 float4 c = UNITY_SAMPLE_TEX2DARRAY(_textureArray, uvw);
    65.                 return c * IN.color[index];
    66.             }
    67.  
    68.             fixed4 frag (v2f i) : SV_Target
    69.             {
    70.                 UNITY_SETUP_INSTANCE_ID(i);
    71.  
    72.                 fixed4 c =
    73.                     GetTerrainColor(i, 0) +
    74.                     GetTerrainColor(i, 1) +
    75.                     GetTerrainColor(i, 2);
    76.  
    77.                 return c;
    78.             }
    79.  
    80.             ENDCG
    81.         }
    82.     }
    83. }
    and here is the error code;

    How can I make this v2 compatible? I'm just a beginner with shaders.

    Thanks in advance.
     
  2. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    158
    At a minimum you need the following:
    • You need to be using SRP (URP, HDRP or a custom SRP)
    • Your shader needs to have
      #pragma target 4.5
    • Your shader needs to have
      #pragma multi_compile _ DOTS_INSTANCING_ON

    You can look at the
    CustomDotsInstancingShader.shader
    in the
    SimpleDotsInstancingShader
    test scene in the HybridURPSamples test project as a working example (https://github.com/Unity-Technologi...ncingShader/CustomDotsInstancingShader.shader). Alternatively, you could look at URP/Unlit or URP/Lit but they are more complicated.
     
  3. soundeos

    soundeos

    Joined:
    Mar 4, 2014
    Posts:
    21
    I've added the pragma directives but still the same error.

    Code (CSharp):
    1.             #pragma vertex vert
    2.             #pragma fragment frag
    3.             #pragma multi_compile_instancing
    4.             #pragma require 2darray
    5.  
    6.             #pragma target 4.5
    7.             #pragma multi_compile _ DOTS_INSTANCING_ON
    I looked into the sample shader but I'm not sure how I suppose to use the UNITY_DOTS_INSTANCED_PROP function.
     
unityunity