1
votes

I'm trying to combine 2 rendertargets, color and normal, for diffuse lightning and to render the result on screen. The idea is to use a sprite with an effect containing only a pixelshader to combine the rendertargets from textures. XNA code:

GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
effect.CurrentTechnique = effect.Techniques["show_buffer"];
effect.Parameters["normalTex"].SetValue(normalRendertarget);
effect.Parameters["colorTex"].SetValue(colorRendertarget);
effect.Parameters["AmbientIntensity"].SetValue(ambientIntesity);
effect.Parameters["LightDirection"].SetValue(lightDirection);
effect.Parameters["DiffuseIntensity"].SetValue(diffuseIntensity);

spriteBatch.Begin(0, BlendState.Opaque, null, null, null,effect);
spriteBatch.Draw(normalRT, Vector2.Zero, Color.White);
spriteBatch.End();

For some reason the rendertarget used in spriteBatch.Draw() influences the result.

Pixel Shader:

void Tex_PixelShader(float2 texCoord : TEXCOORD0, out float4 color : COLOR0)
{
 float4 normal = tex2D(normalTexSampler, texCoord);
 //tranform normal back into [-1,1] range
 normal.rgb = (normal.rgb*2)-1; 
 float4 baseColor = tex2D(colorTexSampler, texCoord);
 float3 lightDirectionNorm = normalize(LightDirection);
 float diffuse = saturate(dot(-lightDirectionNorm,normal.rgb));

 //only works with normalRT in spriteBatch.Draw()
 //colorRT  in spriteBatch.Draw() gives colorRT but darker as result
 color = float4 (baseColor.rgb * (AmbientIntensity + diffuse*DiffuseIntensity), 1.0f);

 //only works with colorRT in spriteBatch.Draw()
 //normalRT in spriteBatch.Draw() gives normalRT as result
 //color = tex2D(colorTexSampler, texCoord); 

 //only works with NormalRT
 //colorRT in spriteBatch.Draw() gives colorRT as result
 //color=tex2D(normalTexSampler, texCoord);

 // works with any rendertarget in spriteBatch.Draw()
 //color = float4(0.0f,1.0f,0.0f,1.0f);
}

The alpha value in both rendertargets is always 1. Adding a vertex shader to the effect results in black. Drawing one rendertarget without any effect with spriteBatch.Draw() shows that the content of each rendertarget is fine. I can't make sense of this. Any ideas?

1
SpriteBatch will set Texture[0]. Are you setting Texture[1]?Andrew Russell

1 Answers

0
votes

Setting the textures with GraphicsDevice.Textures[1]=tex; instead of effect.Parameters["tex"]=tex; worked. Thanks Andrew.

Changed the xna code to:

GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
effect.CurrentTechnique = effect.Techniques["show_buffer"];
GraphicsDevice.Textures[1] = normalRT;   //changed
GraphicsDevice.Textures[2] = colorRT;    //changed
effect.Parameters["AmbientIntensity"].SetValue(ambientIntesity);
effect.Parameters["LightDirection"].SetValue(lightDirection);
effect.Parameters["DiffuseIntensity"].SetValue(diffuseIntensity);

spriteBatch.Begin(0, BlendState.Opaque, null, null, null,effect);
spriteBatch.Draw((Texture2D)colorRT, Vector2.Zero, Color.White);
spriteBatch.End();

And the shader code to:

sampler normalSampler: register(s1);   //added
sampler colorSampler: register(s2);    //added

void Tex_PixelShader(float2 texCoord : TEXCOORD0, out float4 color : COLOR0)
{
 float4 normal = tex2D(normalSampler, texCoord); //changed
 normal.rgb = (normal.rgb*2)-1;
 float4 baseColor = tex2D(colorSampler, texCoord);  //changed

 float3 lightDirectionNorm = normalize(LightDirection);
 float diffuse = saturate(dot(-lightDirectionNorm,normal.rgb));

 color = float4 (baseColor.rgb * (AmbientIntensity + diffuse*DiffuseIntensity), 1.0f);
}