Quote: "float Clip=any((Out.pos.z>NearClipPlane)*(Out.pos.z<FarClipPlane));
Out.pos *=Clip;"
Won't that cause horrible distortions on models that stretch far into the scene? You're essentially snapping the vertices beyond the FarClipPlane to 0,0,0, or am I misinterpreting that?
I think it would be better to do the clipping in the fragment shader, and use the command
clip(). Something like this (no guarantee this will work, it's off the top of my head):
// -------------------------------------------------------------------
// Projection matrices
// -------------------------------------------------------------------
float4x4 matWorldViewProjection : WORLDVIEWPROJECTION;
// -------------------------------------------------------------------
// Parameters
// -------------------------------------------------------------------
// our clipping plane
float4 clippingPlane;
// -------------------------------------------------------------------
// Textures
// -------------------------------------------------------------------
texture diffuseTexture <string ResourceName = "";>;
sampler diffuseSampler = sampler_state
{
texture = <diffuseTexture>;
};
// -------------------------------------------------------------------
// Structs
// -------------------------------------------------------------------
struct VertexShaderInput
{
float4 Position : POSITION;
float2 texCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 texCoord : TEXCOORD0;
float4 clipping : TEXCOORD1;
};
// -------------------------------------------------------------------
// Vertex Shader
// -------------------------------------------------------------------
VertexShaderOutput VertexShaderFunction( VertexShaderInput input )
{
// our return value
VertexShaderOutput output;
// output current vertex
output.Position = mul(input.Position, matWorldViewProjection);
// output UV coordinates
output.texCoord = input.texCoord;
// if vertex position is beyond clipping plane, this will be negative
output.clipping = 0;
output.clipping.x = dot( output.Position, clippingPlane ) ;
// return output
return output;
}
// -------------------------------------------------------------------
// Fragment Shader
// -------------------------------------------------------------------
float4 FragmentShaderFunction(VertexShaderOutput input) : COLOR0
{
// our return value
float4 colour;
// clip pixel
clip(input.clipping.x);
// sample diffuse texture
colour = tex2D( diffuseSampler, input.texCoord );
// output final colour
return colour;
}
// -------------------------------------------------------------------
// Techniques
// -------------------------------------------------------------------
technique Technique1
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 FragmentShaderFunction();
}
}
http://msdn.microsoft.com/en-us/library/windows/desktop/bb204826
TheComet