This project is read-only.

Per vertex alpha with material shader?

Nov 12, 2008 at 4:12 PM
First up many thanks for the great API - Ive been looking for something like this to use as a basis for some hobby projects in XNA and its just the ticket!

One thing which I cant seem to figure out though is if it is possible to set the alpha value of each vertex using the material shader? I am using the XNA VertexPositionColor struct for the vertices and setting the Alpha value of the colour but it doesnt seem to have any effect. If I set the the Alpha member of the shader itself then everything works as I would expect but this alpha value is applied to all the vertices whereas i would like to be able to apply different alpha values for each vert. Is this just not supported by the material shader?
Nov 14, 2008 at 1:03 AM
Edited Nov 14, 2008 at 3:16 AM

Hello!

I've had a look into this. While I did find a bug (MaterialShader.Alpha defaults to zero, when it should be one) I was able to get per-vertex alpha working correctly.

The only subtle point you may be missing is when using vertex colours, MaterialShader.UseVertexColour must be set to true. Not all geometry has vertex colour data, and this can't be tested at runtime (well, it could, but it'd be tricky).

The following class produces correct vertex alpha results:

It should make a full screen quad, that is solid red on the left, and transparent red on the right, when using a Camera2D. (sorry about the bad formatting)


public class FullScreenRedQuad : IDraw
{

IVertices quad;
MaterialShader ms;

public FullScreenRedQuad()
{
quad = new Vertices<VertexPositionColor>(
new VertexPositionColor(new Vector3(0, 0, 0),new Color(255, 0, 0, 255)),
new VertexPositionColor(new Vector3(0, 1, 0),new Color(255, 0, 0, 255)),
new VertexPositionColor(new Vector3(1, 0, 0),new Color(255, 0, 0, 0)),
new VertexPositionColor(new Vector3(1, 1, 0),new Color(255, 0, 0, 0))
);

ms = new MaterialShader();
ms.UseVertexColour = true;
ms.Alpha = 1; // bug!
}

public void Draw(DrawState state)
{
state.PushRenderState();
state.RenderState.AlphaBlend = AlphaBlendState.Alpha;

ms.Bind(state);
quad.Draw(state, null, PrimitiveType.TriangleStrip);

state.PopRenderState();
}

public bool CullTest(ICuller culler)
{
return true;
}
}