Vertex Alpha


Alpha data can be supplied in the vertex data. To enable vertex alpha, set the device's render state RenderStateManager.DiffuseMaterialSource to ColorSource.Color1 so that the Microsoft Direct3D runtime takes the diffuse value from the diffuse color rather than the material.

Alpha data can be added to a color component of a vertex. The following code creates two copies of a sphere mesh, adds a color component to the vertex definition, and specifies the color for each vertex. This code can be added to the GraphicsClass.InitializeDeviceObjects method generated by the DirectX 9.0 Visual C# Wizard to demonstrate the functionality.

          [C#]
          
// Create the sphere geometry. rearSphere = Mesh.Sphere(device,1f,10,10); // Copy the sphere, adding a diffuse color to the vertices. sphere = rearSphere.Clone(MeshFlags.Managed,VertexFormats.PositionNormal | VertexFormats.Diffuse,device); // Add the same color componant to the original sphere. rearSphere = rearSphere.Clone(MeshFlags.Managed,VertexFormats.PositionNormal | VertexFormats.Diffuse,device); // Lock the vertex buffer of the rearSphere to obtain the vertex information. CustomVertex.PositionNormalColored[] Source = (CustomVertex.PositionNormalColored[])rearSphere.LockVertexBuffer(typeof(CustomVertex.PositionNormalColored), 0,rearSphere.NumberVertices); // Loop through the vertices, adding a red transparent color to each. for(int x=0; x < (Source.GetLength(0)); x++) { Source[x].Color = Color.FromArgb(0, 255, 0, 255).ToArgb(); } rearSphere.UnlockVertexBuffer(); // Add a color component with alpha to the second sphere. CustomVertex.PositionNormalColored[] Destination = (CustomVertex.PositionNormalColored[])sphere.LockVertexBuffer(typeof(CustomVertex.PositionNormalColored), 0,rearSphere.NumberVertices); // Instead of adding the same alpha value to each of the vertices, // this code increments the value, creating a gradient effect across the sphere. int num = 256/Destination.GetLength(0); for(int x=0; x < (Destination.GetLength(0)); x++) { Destination[x].Color = Color.FromArgb(150, 255, 255, num*x).ToArgb(); } sphere.UnlockVertexBuffer();

The color alpha value that has been added into the vertices now can be used to draw the spheres with transparency. The following code sets up a rendering Device and draws sphere in front of rearSphere to demonstrate the transparency. This code can be added to the GraphicsClass.Render method generated by the DirectX 9.0 Visual C# Wizard to demonstrate the functionality. Place the code after the Device.Transform.Projection matrix is set.

          [C#]
          
// A StateBlock is used to reset the device to its former state after we draw transparency. StateBlock sb = new StateBlock(device, StateBlockType.All); sb.Capture(); // Use the diffuse vertex color as the DiffuseMaterialSource for color lighting calculations // (as opposed to a material). device.RenderState.DiffuseMaterialSource = ColorSource.Color1; // Set up the device to render the alpha information. device.RenderState.AlphaBlendEnable = true; device.RenderState.SourceBlend = Blend.SourceColor; device.RenderState.DestinationBlend = Blend.InvSourceAlpha; // Translate the rearSphere so that it is drawn behind the sphere. Matrix m2 = m; m.Translate(0,0,2); device.Transform.World = m; rearSphere.DrawSubset(0); // Rotate the sphere so we can see the gradient a little better. device.Transform.World = m2; Vector3 rot = new Vector3(90,0,0); m2.RotateAxis(rot,-45); device.Transform.World = m2; sphere.DrawSubset(0); // Restore the device to its original settings. sb.Apply();

This code results in two transparent spheres, one slightly in front of the other.


Send comments about this topic to Microsoft. © Microsoft Corporation. All rights reserved.

Feedback? Please provide us with your comments on this topic.
For more help, visit the DirectX Developer Center