I am rendering a simple flat plane + texture to use as the floor for a game. I'm rendering it using a custom effect I've written.
The problem I have is that the ground in the distance gets a bit distorted and shakey when the camera moves forwards and backwards.
If I render the same model + texture using the BasicEffect instead of a custom shader it appears smooth and works correctly.
Here's the XNA code setting up the custom effect:
Matrix worldMatrix = Matrix.CreateScale(0.2f);
_floorEffect.CurrentTechnique = _floorEffect.Techniques["Textured"];
_floorEffect.Parameters["World"].SetValue(worldMatrix);
_floorEffect.Parameters["ViewProjection"].SetValue(camera.view * camera.projection);
_floorEffect.Parameters["diffuseTexture"].SetValue(_diffuseTexture);
foreach (EffectPass pass in _floorEffect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount);
}
Here's the shader code:
float4x4 World;
float4x4 ViewProjection;
Texture diffuseTexture;
sampler TextureSampler = sampler_state { texture = <diffuseTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = wrap; AddressV = wrap;};
struct TexturedVertexToPixel
{
float4 position : POSITION0;
float2 textureCoords : TEXCOORD0;
};
TexturedVertexToPixel TextureVertextShader(float4 inPos : POSITION, float2 inTextureCoords : TEXCOORD0)
{
TexturedVertexToPixel output;
float4x4 preWorldViewProjection = mul (World, ViewProjection);
output.position = mul(inPos, preWorldViewProjection);
output.textureCoords = inTextureCoords;
return output;
}
float4 TexturePixelShader(TexturedVertexToPixel input) : COLOR0
{
return tex2D(TextureSampler, input.textureCoords);
}
technique Textured
{
pass Pass1
{
VertexShader = compile vs_2_0 TextureVertextShader();
PixelShader = compile ps_2_0 TexturePixelShader();
}
}
Videos to demonstrate what I mean:
Using the BasicEffect http://www.youtube.com/watch?v=matmrIZH-pU
Using the Custom Shader http://www.youtube.com/watch?v=Fu9P4D-UCTc (see it clearest in 1080p full screen)
Notice the difference when looking at the ground in the distance.