Hi, I am writing a shader where I apply a displacement map on a surface. I am using a plane for the surface. The problem is that I want to turn the normals so that they face away from the surface after I apply the displacement map. So I created fake vertexes so I can calculate the normals, but the problem is that they dont have uv's, and without uv's I can't figure out what height I need to place them because the displacement map desides that. How can I fake uv's for my fake vertexes so I can use the displacement map to give them height? This is my vertex function:
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
// create fake vertexes
float4 v1 = i.vertex + float4(0.05,0.0,0.0,0.0) ; // X
float4 v2 = i.vertex + float4(0.0,0.0,0.05,0.0) ; // Z
float fakeUvCoordX = i.texcoord.x + 0.05;
float fakeUvCoordY = i.texcoord.y + 0.05;
// assign the displacement map to uv coords
float4 disp = tex2Dlod(_Displacement, float4(i.texcoord.x + (_Time.x * _Speed), i.texcoord.y + (_Time.x * _Speed),0.0,0.0));
float4 disp2 = tex2Dlod(_Displacement, float4(fakeUvCoordX + (_Time.x * _Speed), i.texcoord.y + (_Time.x * _Speed),0.0,0.0));
float4 disp3 = tex2Dlod(_Displacement, float4(i.texcoord.x + (_Time.x * _Speed), fakeUvCoordY + (_Time.x * _Speed),0.0,0.0));
// offset the main vert
float4 newPos = i.vertex;
newPos.y += _Scale * disp.y;
// offset fake vertexes
v1 += _Scale * disp2.y;
v2 += _Scale * disp3.y;
// calculate the new normal direction
float3 newNor = cross(v2 - newPos, v1 - newPos);
VOUT.posWorld = mul(_Object2World, newPos);
VOUT.pos = mul(UNITY_MATRIX_MVP,newPos);
VOUT.tex = i.texcoord;
VOUT.normalWorld = normalize( mul(float4(newNor,0.0),_World2Object).xyz);
VOUT.tangentWorld = normalize( mul(_Object2World,i.tangent).xyz);
VOUT.binormalWorld = normalize( cross(VOUT.normalWorld, VOUT.tangentWorld) * i.tangent.w);
return VOUT;
}
Thanks in advance!
Replies
I like this vieuw:
But I hate this view:
Are there any tips you guys can give me? I think it has somthing to do with the reflection. Keep in mind I am not aiming to make a super realistic water shader, it just has to look good.
This is my code: