I'm reading riemers tutorial on how to make a water effect and trying to translate it from xna 3 to xna 4. So far I figured out I have to create a shader in order to clip the planes for my refraction map. I'm new to hlsl and how to implement custom shaders into my program, so I am wondering what I am doing wrong here.
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 ClipPlane0;
void vs(inout float4 position : POSITION0, out float4 clipDistances : TEXCOORD0)
{
clipDistances.y = 0;
clipDistances.z = 0;
clipDistances.w = 0;
position = mul(mul(mul(position, World), View), Projection);
clipDistances.x = dot(position, ClipPlane0);
}
float4 ps(float4 clipDistances : TEXCOORD0) : COLOR0
{
clip(clipDistances);
return float4(0, 0, 0, 0); // TODO: whatever other shading logic you want
}
technique
{
pass
{
VertexShader = compile vs_2_0 vs();
PixelShader = compile ps_2_0 ps();
}
}
This is the fx file that I found on an xna forum just for reference.
Major edit:
I was able to get the program running successfully but when I viewed the saved screenshot of the refraction map, nothing was clipped.
public void DrawRefractionMap(Effect clipEffect, Camera camera, GraphicsDevice device)
{
Plane refractionPlane = CreatePlane(waterHeight + 1.5f, new Vector3(0, -1, 0), camera, false);
clipEffect.Parameters["ClipPlane0"].SetValue(new Vector4(refractionPlane.Normal, refractionPlane.D));
device.SetRenderTarget(refractionRenderTarget);
device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1, 0);
foreach (EffectPass pass in clipEffect.CurrentTechnique.Passes)
{
pass.Apply();
DrawTerrain(clipEffect, camera, device);
}
device.SetRenderTarget(null);
refractionTexture = (Texture2D)refractionRenderTarget;
FileStream stream = File.OpenWrite("Screenshot33.png");
refractionTexture.SaveAsJpeg(stream, refractionTexture.Width, refractionTexture.Height);
}