Search Unity

Texture array surface shader problem

Discussion in 'Shaders' started by Hybrid46, Jul 29, 2020.

  1. Hybrid46

    Hybrid46

    Joined:
    Mar 21, 2014
    Posts:
    5
    Hi!

    I'm trying to write a simple surface shader which use a texture array and choose the index by the vertex color red value. The following code doesn't work. I already read some forums about surface shaders, checked the documentetion multiple times and can't figure out whats wrong. I already made a custom texturearray shader which works good(no lightning) but i want a surface shader for auto generated lightning.

    Shader "Custom/NewTerrainSurfaceShader"
    {
    Properties
    {
    _MainTex("Albedo (RGB)", 2DArray) = "white" {}
    _SliceRange("Slices", Range(1,16)) = 5
    }
    SubShader
    {
    Tags { "RenderType" = "Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Standard fullforwardshadows
    #pragma target 3.5
    #pragma require 2darray

    UNITY_DECLARE_TEX2DARRAY(_MainTex);
    float _SliceRange;

    struct Input
    {
    float2 uv_MainTex;
    float4 color : COLOR;
    };

    void surf(Input IN, inout SurfaceOutputStandard o)
    {
    fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(IN.uv_MainTex.xy, IN.color.r * _SliceRange));
    o.Albedo = c.rgb;
    //o.Metallic = IN.color.g;
    //o.Smoothness = IN.color.b;
    //o.Alpha = IN.color.a;
    }
    ENDCG
    }
    FallBack "Diffuse"
    }

    Thanks for the help!
     
    Last edited: Jul 29, 2020
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    What do you mean by "doesn't work"? There's nothing obviously wrong with the above shader code. Is it only ever showing the first layer, or is there a shader error and it's showing pink?
     
  3. Hybrid46

    Hybrid46

    Joined:
    Mar 21, 2014
    Posts:
    5
    That's the problem. No error, seems to be nothing wrong. But everything purple! I deleted shader cache to reset everything, but when i try to use IN.color for albedo its not working anymore. Strange. It's like the shader stucked in a wrong state and no refresh. I'm using Unity 2019.4.5.
     
  4. Hybrid46

    Hybrid46

    Joined:
    Mar 21, 2014
    Posts:
    5
    Finally I figured it out: It's not working with Universal Rendering Pipeline! It's working without any RP.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Yep. That was going to be my response. Surface Shaders only work with the built in rendering paths. For the URPs you'll want to use Shader Graph.
     
    Hybrid46 likes this.