AlphaFunc

Is AlphaFunc supported?

Can’t seem to get it to work I just get

Leaves.fx (112,3) unexpected token ‘A’ found. Expected CloseBracket

This is in the pixel shader right ?

float4 color = input.Color;

float AlphaElement = color.a; // lower case shader is super picky.

// modify color
color = float4(1.0f, color.a, color.a, AlphaElement);

Hi Will,

No it’s in the technique definition

Typically I used to set all sorts of flags here
zbuffer, alphablending, zwrite, things like that.

AlphaFunc was a really useful one, but looks like it may have been killed in XNA 4 in the quest for the useless pre-multiplies alpha

Oh i see what your saying now.
Ya you can change the alpha behavior from game1 thru the graphics device or on the shader. I forget how to do it from the shader exactly but their is a command as well there, that i think overrides the graphics device command passed in not sure which has priority i think its the shader anyways.

In game1 you can just set the blend state on the device before you draw.

        #region static device stuff

        public static SamplerState SS_PointBorder = new SamplerState() { Filter = TextureFilter.Point, AddressU = TextureAddressMode.Border, AddressV = TextureAddressMode.Border };
        public static SamplerState SS_PointClamp = new SamplerState() { Filter = TextureFilter.Point, AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp };
        public static SamplerState SS_PointWrap = new SamplerState() { Filter = TextureFilter.Point, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap };
        public static SamplerState SS_PointMirror = new SamplerState() { Filter = TextureFilter.Point, AddressU = TextureAddressMode.Mirror, AddressV = TextureAddressMode.Mirror };

        public static RasterizerState RS_scissors_on = new RasterizerState() { ScissorTestEnable = true };
        public static RasterizerState RS_scissors_off = new RasterizerState() { ScissorTestEnable = false };

        public static BlendState BS_Additive = BlendState.Additive;
        public static BlendState BS_Alpha = BlendState.AlphaBlend;
        public static BlendState BS_NonPremultiplied = BlendState.NonPremultiplied;
        public static BlendState BS_Opaque = BlendState.Opaque;

        public static void BlendingState(BlendState bs)
        {
            device.BlendState = bs;
        }

device there is just a static reference to the GraphicsDevice

you can also pass that into one of the parameters for spritebatch.Begins( …,… , blendstate,…, ect …);

Make sure you make a state outside of the method if it doesn’t exist already though, because creating a new state on the fly creates a crap ton of garbage that gets collected.

Ya pre-multiplied alpha to me is worthless to me its like one of those pre-optimizations that ties your hands. Then you end up frowning on it later, when you realize you have to undo it all to do this or that anyways. So that it ends being instead a de-optimization. A lot of other people had the same opinion when they did that.

Edit:

Not sure this is right but i found it on stack overflow i have it all in a shader where i was doing the states but id be digging forever to find it.

technique SpriteDrawing
{
pass P0
{
AlphaBlendEnable = FALSE; // or maybe lower case
VertexShader = compile vs_2_0 SpriteVertexShader();
PixelShader = compile ps_2_0 SpritePixelShader();
}
};