如何为简单的顶点对象添加透明度?
Posted
技术标签:
【中文标题】如何为简单的顶点对象添加透明度?【英文标题】:How I can add transparency to simple vertex objects? 【发布时间】:2021-02-24 11:32:50 【问题描述】:如何在 Direct3D 中为简单的顶点对象添加透明度?
我使用了这里的信息但没有结果: https://docs.microsoft.com/ru-ru/windows/win32/direct3d9/vertex-alpha
我这样初始化设备:
PresentParameters presentParams = new PresentParameters();
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.DeviceWindowHandle = this.Handle;
presentParams.BackBufferFormat = Format.A8R8G8B8;
presentParams.PresentFlags = PresentFlags.LockableBackBuffer;
presentParams.BackBufferWidth = this.ClientSize.Width;
presentParams.BackBufferHeight = this.ClientSize.Height;
presentParams.Windowed = true;
var device = new Device(_direct3d, 0, DeviceType.Hardware, this.Handle, _createFlags, presentParams);device.SetRenderState(RenderState.Lighting, false);
// ..tried different variations of flags here with no result..
device.SetRenderState(RenderState.DiffuseMaterialSource, ColorSource.Color1);
var surface = device.GetBackBuffer(0, 0);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
顶点类声明:
[StructLayout(LayoutKind.Sequential)]
public struct ColoredVertex : IEquatable<ColoredVertex>
public Vector3 Position get; set;
public int Color get; set;
public static VertexElement[] Format
get
return new VertexElement[]
new VertexElement(0,0,DeclarationType.Float3,DeclarationMethod.Default,DeclarationUsage.PositionTransformed,0),
new VertexElement(0,12,DeclarationType.Color,DeclarationMethod.Default,DeclarationUsage.Color,0),
VertexElement.VertexDeclarationEnd
;
public ColoredVertex(Vector3 position, int color)
: this()
Position = position;
Color = color;
然后我画图元。 颜色包含第二个三角形的 alpha 为 50,所以我希望三角形是透明的.. 但没有
_device.BeginScene();
var colorN1 = Color.FromArgb(255, 100, 100, 100);
var triangleN1 = new ColoredVertex[]
new ColoredVertex(new Vector3(10f, 10f, 0.0f), colorN1.ToArgb()),
new ColoredVertex(new Vector3(1000.0f, 10.0f, 0.0f), colorN1.ToArgb()),
new ColoredVertex(new Vector3(1000f, 800f, 0.0f), colorN1.ToArgb()),
;
using (var decl = new VertexDeclaration(_device, ColoredVertex.Format))
_device.VertexFormat = VertexFormat.Diffuse;
_device.VertexDeclaration = decl;
_device.DrawUserPrimitives<ColoredVertex>(PrimitiveType.TriangleList, 1, triangleN1);
var colorN2 = Color.FromArgb(50, 100, 0, 0);
var triangleN2 = new ColoredVertex[]
new ColoredVertex(new Vector3(100f, 100f, 1.0f), colorN2.ToArgb()),
new ColoredVertex(new Vector3(800.0f, 100.0f, 1.0f), colorN2.ToArgb()),
new ColoredVertex(new Vector3(700f, 900f, 1.0f), colorN2.ToArgb()),
;
using (var decl = new VertexDeclaration(_device, ColoredVertex.Format))
_device.VertexFormat = VertexFormat.Diffuse;
_device.VertexDeclaration = decl;
_device.DrawUserPrimitives<ColoredVertex>(PrimitiveType.TriangleList, 1, triangleN2);
_device.EndScene();
_device.Present();
【问题讨论】:
您使用的是旧版托管 DX 1.1 程序集吗?如果是这样,你应该转移到别的地方。见this blog post。 我使用 SlimDX。没有那么传统) 【参考方案1】:这些 RenderState 标志发挥了作用:
_device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
_device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
_device.SetRenderState(RenderState.AlphaBlendEnable, true);
【讨论】:
以上是关于如何为简单的顶点对象添加透明度?的主要内容,如果未能解决你的问题,请参考以下文章