I'm the one that coded the water. I think one of the problems is that the normal map is in tangent space but we dont have code for tangent space normals. Also, our game uses Y = up. I'm not sure how that affects the water shader. I had to change the sampled value order to .rbg just to get it to not look completely messed up. The specular reflections also seem wrong to me, they only occur near the middle of the water.
What were you suggesting to enable anisotropic filtering on? The reflections/refractions or the normal map?
The code is mostly the same as from
this tutorial
float4x4 World;
float4x4 View;
float4x4 ReflectionView;
float4x4 Projection;
float3 CamPos;
float FarPlane;
float4 WaterColor;
float WaveLength;
float WaveHeight;
float Time;
float WindForce;
float3 WindDirection;
float3 LightDirection;
Texture2D ReflectionMap;
Texture2D RefractionMap;
Texture2D DepthMap;
Texture2D BumpMap1;
Texture2D BumpMap2;
//specular input variables
float SpecPerturb = 1;
float SpecPower = 354;
SamplerState RTSampler
{
Filter = Min_Mag_Mip_Linear;
AddressU = mirror;
AddressV = mirror;
};
SamplerState MapSampler
{
Filter = Min_Mag_Mip_Linear;
AddressU = Wrap;
AddressV = Wrap;
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : SV_POSITION;
float4 ReflectionMapSamplingPos : TEXCOORD0;
float2 BumpMapSamplingPos : TEXCOORD1;
float2 BumpMapSamplingPos2 : TEXCOORD2;
float4 RefractionMapSamplingPos : TEXCOORD3;
float4 Position3D : TEXCOORD4;
};
struct PixelShaderOutput
{
float4 Color : SV_TARGET0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
float4x4 worldViewProjection = mul(World, mul(View, Projection));
float4x4 reflectionWorldViewProjection = mul(World, mul(ReflectionView, Projection));
float3 windDir = normalize(WindDirection);
float3 windDir2 = normalize(mul(WindDirection, float3x3(0.707, 0, -.707, 0, 1, 0, .707, 0, .707))); //45 degree rotation matrix
float3 perpDir = cross(windDir, float3(0, 1, 0));
float ydot = dot(input.TexCoords, windDir.xz);
float xdot = dot(input.TexCoords, perpDir.xz);
float2 moveVector = float2(xdot, ydot);
float2 moveVector2 = float2(dot(input.TexCoords, cross(windDir2, float3(0, 1, 0)).xz), dot(input.TexCoords, windDir2.xz));
moveVector.y += Time * WindForce;
moveVector2.y += Time * 2 * WindForce;
output.Position = mul(input.Position, worldViewProjection);
output.Position.z = log(0.001 * output.Position.z + 1) / log(0.001 * FarPlane + 1) * output.Position.w;
output.ReflectionMapSamplingPos = mul(input.Position, reflectionWorldViewProjection);
output.RefractionMapSamplingPos = output.Position;
output.Position3D = mul(input.Position, World);
output.BumpMapSamplingPos = moveVector / WaveLength;
output.BumpMapSamplingPos2 = moveVector2 / (WaveLength / 2);
//output.Depth.x = output.Position.z;
//output.Depth.y = output.Position.w;
return output;
}
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
PixelShaderOutput output = (PixelShaderOutput)0;
float2 ProjectedTexCoords;
float2 ProjectedRefrTexCoords;
float3 BumpColor1 = BumpMap1.Sample(MapSampler, input.BumpMapSamplingPos).rgb;
float3 BumpColor2 = BumpMap1.Sample(MapSampler, input.BumpMapSamplingPos2).rgb;
//float3 BumpColor = float3(BumpColor1.xy + BumpColor2.xy, BumpColor1.z * BumpColor2.z);
float3 BumpColor = normalize(2 * (BumpColor1 + BumpColor2) - 2);
ProjectedTexCoords.x = input.ReflectionMapSamplingPos.x / input.ReflectionMapSamplingPos.w / 2.0f + 0.5f;
ProjectedTexCoords.y = -input.ReflectionMapSamplingPos.y / input.ReflectionMapSamplingPos.w / 2.0f + 0.5f;
ProjectedRefrTexCoords.x = input.RefractionMapSamplingPos.x / input.RefractionMapSamplingPos.w / 2.0f + 0.5f;
ProjectedRefrTexCoords.y = -input.RefractionMapSamplingPos.y / input.RefractionMapSamplingPos.w / 2.0f + 0.5f;
float2 perturbation = WaveHeight * (BumpColor);
float2 perturbatedTexCoords = ProjectedTexCoords + perturbation;
float2 perturbatedRefrTexCoords = ProjectedRefrTexCoords + perturbation;
float4 ReflectiveColor = ReflectionMap.Sample(RTSampler, perturbatedTexCoords);
float4 RefractiveColor = RefractionMap.Sample(RTSampler, perturbatedRefrTexCoords);
//output.Color = RefractiveColor;
//return output;
//clip(input.Depth.x / input.Depth.y - Depth);
float3 eyeVector = normalize(CamPos - input.Position3D);
float3 normalVector = (BumpColor.rbg);
float fresnelTerm = 0.02f+0.97f*pow((1-dot(eyeVector, normalVector)),5);
//float fresnelTerm = dot(eyeVector, normalVector);
//fresnelTerm = 1 - fresnelTerm * 1.3f;
float3 reflectionVector = -reflect(LightDirection, normalVector);
//float specular = dot(normalize(reflectionVector), normalize(eyeVector));
//specular = pow(specular, 256);
float4 specColor;
float3 lightSourceDir = normalize(-LightDirection);
//float3 lightSourceDir = normalize(float3(0.8f, 0.1f, 0.0f));
float3 halfvec = normalize(eyeVector + lightSourceDir + float3(perturbation.xy * SpecPerturb, 0));
float3 temp = 0;
temp.x = pow(dot(halfvec, normalVector), SpecPower);
specColor = float4(1, 1, 1, 1);
specColor *= temp.x;
specColor = float4(specColor.xyz * specColor.w, 0);
fresnelTerm = clamp(fresnelTerm, 0, 1);
//float4 combinedColor = lerp(ReflectiveColor, RefractiveColor, fresnelTerm);
float4 combinedColor = RefractiveColor * (1 - fresnelTerm) + ReflectiveColor * fresnelTerm;
output.Color = lerp(combinedColor, float4(WaterColor.rgb, 1), WaterColor.w) + specColor;
//output.Color = WaterColor;
return output;
//return float4(1,1,1,0.0);
}
technique11 Water
{
pass Pass1
{
SetVertexShader(CompileShader(vs_4_0, VertexShaderFunction()));
SetPixelShader(CompileShader(ps_4_0, PixelShaderFunction()));
}
}
Edited by Telanor, 30 June 2012 - 01:17 AM.