0

我正在使用 C#。我需要使用 DirectX 绘制点,它们应该是半透明的。我声明要transformColored的顶点并将颜色值设置为:

 vertices[i].Color = Color.FromArgb(20,0,0,255).ToArgb();

这应该是相当透明的蓝色,但我得到的是不透明的蓝色。无论我为 alpha 值使用什么值,它总是变得完全不透明。任何想法为什么?我希望转换后的颜色字段支持 alpha 值。

提前致谢。

绘图代码为:

device.Clear(ClearFlags.Target, System.Drawing.Color.White, 1.0f, 0);

device.RenderState.AlphaBlendEnable = true;

device.RenderState.AlphaSourceBlend = Blend.SourceAlpha;

device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha;

device.RenderState.BlendOperation = BlendOperation.Add;

CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[N];

for (int i = 0; i < N; i++)
{
   vertices[i].Position = new Vector4(2.5f + (g_embed[i, 0] - minx) * (width - 5.0f) / maxx, 2.5f + (g_embed[i, 1] - miny) * (height - 5.0f) / maxy, 0f, 1f);//g_embed, minx, width,maxx, miny,height, maxy are all predifined

   vertices[i].Color = Color.FromArgb(20, 0, 0, 255).ToArgb();
}

 device.BeginScene();

 device.VertexFormat = CustomVertex.TransformedColored.Format;

 device.DrawUserPrimitives(PrimitiveType.PointList, N, vertices);

 device.EndScene();

 device.Present();

 this.Invalidate();
4

1 回答 1

0

您的渲染状态用于预乘 alpha。使用真正的 alpha 通道需要以下设置(Blendenumerationdoc):

graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
graphics.GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add;

它按照公式创建,其中 a 是颜色的 alpha:a * SourceColor + (1-a) * DestColor

于 2013-07-27T17:10:04.723 回答