can't use if statements in shader function - "Sampler parameter must come from a literal expression"

so i’m trying to get my shader to change it’s parameters based on the normal direction (like so walls and floors can have different properties, etc). it seems like all of the individual parts of this script work on their own, but when i try to control them with if statements the shader turns white and logs

Shader error in ‘Custom/STANDARD’: Sampler parameter must come from a literal expression. at line 56 (on d3d11)

here’s the shader code

Shader "Custom/STANDARD"
{
    Properties {
      _MainTexTOP ("Texture", 2D) = "white" {}
      _MainTexSIDE ("Texture", 2D) = "white" {}
      _BumpMapTOP ("Bumpmap", 2D) = "bump" {}
      _BumpMapSIDE ("Bumpmap", 2D) = "bump" {}
      _Bump ("Normal", float) = 1.0
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert
    
      struct Input {
        float3 worldNormal;
        float3 worldPos;
      };
    
      sampler2D _MainTexTOP;
      sampler2D _MainTexSIDE;
      sampler2D _BumpMapTOP;
      sampler2D _BumpMapSIDE;
      float _Bump;
    
      void surf (Input IN, inout SurfaceOutput o) {
    
        float2 UV = IN.worldPos.xy;
        sampler2D tex = _MainTexTOP;
        sampler2D bump = _BumpMapTOP;
      
      
        if(abs(IN.worldNormal.y)>0.5){
            UV = IN.worldPos.xz;
            tex = _MainTexTOP;
            bump = _BumpMapTOP;
        } else {
            if(abs(IN.worldPos.x)>0.5){
                UV = IN.worldPos.yz;
                tex = _MainTexSIDE;
                bump = _BumpMapSIDE;
            } else {
                if(abs(IN.worldNormal.z)>0.5){
                    UV = IN.worldPos.xy;
                    tex = _MainTexSIDE;
                    bump = _BumpMapSIDE;
                }
            }
        }
      
        //UV = IN.worldPos.xz;
        //tex = _MainTexTOP;
        //bump = _BumpMapTOP;

      
        o.Albedo = tex2D (tex, UV).rgb;
        o.Normal = UnpackNormal (tex2D (bump, UV));
      
      
      
      }
      ENDCG
    }
    Fallback "Diffuse"
  }

if i try manually setting the shader parameters to any of the configurations this function might return, it works. if i try running this whole function but then adding

        UV = IN.worldPos.xz;
        tex = _MainTexTOP;
        bump = _BumpMapTOP;

right before it actually sets the output it works. for some reason if i run this function /without/ resetting the parameters at the end, it says the parameters need to be literal expressions. what is it about setting the value of a parameter within an if nest that makes it no longer a literal statement? is this a scope problem??[/code]

1 Like

You can’t do things like this with samplers: “sampler2D tex = _MainTexTOP;”
You should just sample the textures in your if - else and assign to o.Albedo directly.

1 Like