- Home /
How do I alter vertex displacement based on UV coordinates?
I am fairly new to shader programming and I am trying to create a water shader that uses vertex displacement to simulate waves and bump mapping to simulate ripples. I have the ripples working, but I am having trouble implementing the waves.
Currently, I am using this code for displacement:
void disp (inout appdata v)
{
float d = tex2Dlod(_DispTex, float4(v.texcoord.xy,0,0)).r * _Displacement;
v.vertex.xyz += v.normal * d;
}
And this works fine for actually displacing the object, but the issue I am having is that I want to be able to change the coordinates of the displacement texture to allow the waves to move. I believe the issue here is using this to determine the UV coordinates:
float4(v.texcoord.xy,0,0)
But I am unsure of what I am supposed to replace this with.
My normal map code uses this line:
o.Normal = (UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap))
Which is combined with a script that changes the texture offset of the bump map to create a moving ripple effect. However, this technique does not seem to work with displacement maps and only results in error messages.
Any assistance would be greatly appreciated.
EDIT: I got the code working via a workaround. I created a separate Vector input and then used this vector in place of the uv coordinates so that the code looks like this:
float d = ((tex2Dlod(_DispTex, _Vector + float4(v.texcoord.xy,0,0)).r
You can see the shader in action HERE (Requires DX11)