簡體   English   中英

如何為簡單的頂點對象添加透明度?

[英]How I can add transparency to simple vertex objects?

如何為 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);

頂點 class 聲明:

[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();

這些 RenderState 標志起到了神奇的作用:

_device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
_device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
_device.SetRenderState(RenderState.AlphaBlendEnable, true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM