I am learning shaders, and I figured I would try to use them to create sort of a predator effect. I got to the point where I rendered my level to a rendertarget and passed that to the shader file, and attempted to get the color from it. This, by my understanding, should render the player completely invisible from the code I have. I have included relevant portions of my code.
Shader Code:
float4x4 World;
float4x4 View;
float4x4 Projection;
sampler s0;
texture land;
sampler land_sample = sampler_state{ Texture = land; };
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
float4 color = tex2D(s0, coords);
float4 land_color = tex2D(land_sample, coords);
if (color.a)
return land_color;
return color;
}
Main Draw Method:
protected override void Draw(GameTime gameTime)
{
//First draw the level to the land rendertarget
GraphicsDevice.SetRenderTarget(land);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, null, camera.Transform);
levelOne.Draw(spriteBatch, gameTime);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin();
//Draw land
spriteBatch.Draw(land, new Vector2(0, 0), Color.White);
spriteBatch.End();
//Draw the player and npcs normally
spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, null, camera.Transform);
npcs.ForEach(n => n.Draw(spriteBatch, gameTime));
effect.Parameters["land"].SetValue(land);
effect.CurrentTechnique.Passes[0].Apply();
player.Draw(spriteBatch, gameTime);
spriteBatch.End();
base.Draw(gameTime);
}
Level Draw Code:
public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
{
//Will cull tiles from level based on camera position later
for (int i = 0; i < tiles.Length; i++)
{
for (int j = 0; j < tiles[i].Length; j++)
{
tiles[i][j].Draw(spriteBatch, gameTime);
}
}
}
Based on the tutorial I was following, this should replace the color in the player's sprite with that of the render target, but it does not. Furthermore, the 'level' drawn in the player's sprite is scaled down a bit.
My question is why this isn't working? Is there something I'm missing in the shader code? Also, as a beginner shader learner please try to keep your responses rather simple, or point me to a reference. Thanks! :)