Problem about displacement in surface shaders

Hi all,

I’ m trying to implement a surface shader which uses a displacement map for modifying particular parts of an armor mesh. Below you can see the code:

  Shader "Custom/ExtrusionWithBump" {
    Properties {
      _MainTex ("Texture", 2D) = "white" {}
      _BumpMap ("Bumpmap", 2D) = "bump" {}
      _DispTex ("Disp", 2D) = "gray" {}
      _Amount ("Extrusion Amount", Range(-1,1)) = 0.5
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert vertex:vert

      struct Input {
          float2 uv_MainTex;
          float2 uv_BumpMap;
      };
      
      sampler2D _DispTex;
      float _Amount;
      
      void vert (inout appdata_full v) {
          float d = tex2D(_DispTex, float4(v.texcoord.xy,0,0)).r * _Amount;
      	v.vertex.xyz += v.normal * d;
      }
      
      sampler2D _MainTex;
      sampler2D _BumpMap;
      void surf (Input IN, inout SurfaceOutput o) {
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
          o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
      }
      ENDCG
    } 
    Fallback "Diffuse"
  }

Unity doesn’ t compile this code and i couldn’ t manage to find a solution. Can you please tell me where the problem is.

thanks

When Unity fails to compile things, it usually produces at least one error message.

1 Like

These are the errors

Error 1:
Shader error in ‘Custom/ExtrusionWithBump’: Program ‘vert_surf’, ambiguous overloaded function reference “tex2D(sampler2D, float4)” at line 22

Keywords: DIRECTIONAL, LIGHTMAP_OFF, DIRLIGHTMAP_OFF, SHADOWS_OFF

Error 2:
Shader error in ‘Custom/ExtrusionWithBump’: Program ‘vert_surf’, maximum vs_4_0_level_9_3 sampler register index (0) exceeded - note that the target doesn’t support texture sampling intrinsics (compiling for d3d11_9x) at line 23

Error3:
Shader error in ‘Custom/ExtrusionWithBump’: Program ‘vert_surf’, cannot map expression to vs_4_0 instruction set (compiling for d3d11) at line 61

So which version of tex2D() did you mean? There aren’t any which take a float4.

I’ ve changed the line 22 like this:

float d = tex2D(_DispTex, v.texcoord.xy).r * _Amount;

now the compiler says
Program ‘vert_surf’, function “tex2D” not supported in this profile line 22

http://forum.unity3d.com/threads/63231-How-to-use-vertex-texture-fetch

You need to change tex2D to tex2Dlod (keep float4 for uv as in the original) and add
#pragma glsl
#pragma target 3.0

That’s because texture fetch from vertex shader is supported from Shader Model 3 and up and because the default compilation target for opengl is ARB shaders which don’t support it either.
This of course rises the hardware requirements for the shader.

thank you both,

@cician it worked perfectly.

I got a problem with VTF as well but it’s not a problem with the compiling.
The shader works fine in the editor but when I build the game it will only show the default pink material. The shader code is just copied from the examples and of course I tried the glsl version with the renderer forced into OpenGL mode, but to no avail.
There is just this warning, which I can’t figure out: Shader warning in ‘Custom/VTF’: Program ‘vert_surf’, maximum vs_4_0_level_9_3 sampler register index (0) exceeded - note that the target doesn’t support texture sampling intrinsics (compiling for d3d11_9x) at line 21

Edit: Found the solution to the problem. Nothing wrong with the shader itself but the GameObject using the shader was instantiated programmatically by me and had no references to the scene. So it couldn’t be loaded. The solution: Copy the shader to the Resources folder. Guess you never learn out :stuck_out_tongue: