Surface Shader 'vert' function texture lookups

Hello!

I’m trying to write a surface shader that uses a noise texture for vertex displacement. However, I am getting a compile error when trying to do a texture lookup in the ‘vert’ function.

Here is the code for the shader (obviously not finished, don’t point and laugh! :wink: )

  Shader "Test/Noise" {
    Properties {
      _MainTex ("Texture", 2D) = "white" {}
	  _NoiseTex("Noise", 2D) = "white" {}
	  _TimeScale ("Time Scaling", Range(0,10)) = 1.0
	  _Displacement("Displacement", Range(0,5)) = 1.0
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM

	  #pragma target 3.0
          #pragma surface surf Lambert vertex:vert
	  	  
	  float _Sharpness;
	  float _Displacement;
	  float _TimeScale;
	  sampler2D _NoiseTex;
	  
	  void vert(inout appdata_full v)
	  {
		float2 uvHack = float2(frac(v.vertex.x + v.vertex.y + v.vertex.z),
				                frac(v.vertex.x * v.vertex.y * v.vertex.z));
		
		float3 noise = tex2D(_NoiseTex,uvHack).rgb;
		
		v.vertex.xyz = v.vertex.xyz - (v.normal.xyz * (noise.x * _Displacement));
	  }
	  
          sampler2D _MainTex;
	  
          struct Input {
              float2 uv_MainTex;
          };
 
          void surf (Input IN, inout SurfaceOutput o) {
		  
              o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
          }

      ENDCG
    }
    Fallback "Diffuse"
  }

Is it even possible to do this with a surface shader? I’ve never even tried this, and some googling informed me that it is possible (dependant on hardware) if I write out the vertex and fragment shaders separately, but I would like the functionality of using the surface shader to handle lighting.

I’m new to unity/ShaderLab, but I have some experience with CgFX, so I have a fairly good grasp of what is going on, I’m just not sure if what I want to do is possible or not.

Thanks, tex2Dlod() worked! This thread has more detail on the matter:

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

I didn’t notice it before I posted this thread :confused:

Thanks for the help!

1 Like