0

I'm not sure if I'm not using the right SpriteBatch parameters or if my shader is not coded properly, but, to be simple, I'm trying to make a shader that sets the alpha of all pixels to 128. However, there seems to be only 2 possible 'alphas'. Either if I set the value to 0, nothing appears, and any other value will set the alpha to 255. There is absolutely no in-between and I can't put my finger on what's wrong. Here is the code around the draw call (I figured that, since I'm simply trying to set the alpha to 128 on everything, what I'm drawing is irrelevant)

m_GridFadeEffect.Parameters["FadeDistance"].SetValue(GridDrawRadius);
m_GridFadeEffect.Parameters["Center"].SetValue(Center);
m_GridFadeEffect.Parameters["LineColor"].SetValue(new Vector4(GridLinesColor.R, GridLinesColor.G, GridLinesColor.B, GridLinesColor.A));

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, m_GridFadeEffect);

 // Draw here

spriteBatch.End();

And here is the shader

uniform extern float FadeDistance;
uniform extern float2 Center;
uniform extern float4 LineColor;

float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR0
{
    float4 Return = LineColor;

    Return.a = 128; // I also tried Return.a = 0.5f;

    return Return;
}

technique
{
    pass P0
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 04 '15 at 00:01

3 Answers3

1

Well I suggest you to use the built-in feature to achieve it:

spriteBatch.Draw(myTexture, myLocation, Color.White * 0.5f); //set 50% transparency

If you want to use a pixel shader, it should be:

sampler textureSampler;

float4 ps_main( float2 tex2D : TEXCOORD0 ) : COLOR0 {  
   float color = tex2D( textureSampler, tex2D.xy ); //get color at xy coordinate
   color.a = 0.5f; //set 50% transparency

   return color; //output it
}

technique {
   pass Pass1 {
      PixelShader = compile ps_2_0 ps_main(); //XNA also supports ps_3_0
   }
}

Now pass the texture to the pixel shader:

myEffect.Parameters["textureSampler"] = myTexture;

apply it and then draw:

myEffect.CurrentTechnique.Passes[0].Apply(); //first and unique pass
//draw here
Omar
  • 16,329
  • 10
  • 48
  • 66
0

Pass BlendState.AlphaBlend to spritebatch.Draw() and return.a = 0.5 shoul works. Here is good HLSL tutorial. Part 10 can help you with your problem.

0

Firstly HLSL uses values between 0 and 1 for color values. To convert color values that range from 0-255, simply do:

valueBetweenZeroAndOne = desiredColor/255

Where desired color is the 0-255 value of the color.

The simplest way to do this would either use the default shader supplied by the spritebatch and pass in a color of white multiplied by a factor of 0-1.

The proper way of doing it in a shader would be:

sampler s0;
// You want to set this outside the shader program
float alpha = 1;

float4 pixelShader( float2 coords : TEXCOORD0 ) : COLOR0 {  
   float color = tex2D( s0, coords );    
   return (color.rgba * alpha);
}
Gibbo
  • 630
  • 1
  • 8
  • 22