How to correctly sample a texture with tex2Dlod

I’ve recently been playing around with shaders, and I’ve been interested in creating water. I built a simple shader to practice displacing vertices by adding to their object space y coordinate based on a sample from a displacement texture.

Here’s the difficulty: Although I’m using the x and z world coordinates of each vertex to sample the displacement texture, each vertex is displaced by the same amount! I’m using the red value of the sample colour. I’m sure I’m missing something, but experimentation and scouring forums for existing questions have gotten me nowhere. Any help would be very welcome!


Here’s my code:

Shader "Custom/Test"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows vertex:vert
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        void vert(inout appdata_full v)
        {
            // Get world position
            float4 worldPos = mul(unity_ObjectToWorld, v.vertex);

            // Sample texture based on world position
            float4 textureSample = tex2Dlod(_MainTex, float4(worldPos.xz, 0.0, 0.0));

            // Use texture sample to modify height
            v.vertex.y += textureSample.x;
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            o.Albedo = 1.0;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Here’s the displacement texture I’m using, which is assigned to _MainTex:
196924-displacement-map.png

Here’s an image of the result:

I figured it out! The second argument of tex2Dlod (line 29 above) is a float4, where the two first floats represent coordinates for getting a colour from the texture. I hadn’t realized that those coordinates need to be normalized between 0 and 1. My values were always above 1, so my constant output was just the result of it getting clamped!