在 SharpDX 应用程序中获得意想不到的颜色

Getting unexpected colors in SharpDX app

我正在通过 .NET 的 SharpDX 包装器深入研究 directx,但我得到了一些意想不到的结果。

这是我的预期结果:

这是我得到的结果:

这是我的着色器:

struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

VOut vsMain(float4 position : POSITION, float4 color : COLOR)
{
    VOut output;

    output.position = position;
    output.color = color;

    return output;
}

float4 psMain(VOut pInput) : SV_TARGET
{
    return pInput.color;
}

连同输入布局:

private D3D11.InputElement[] inputElements = new D3D11.InputElement[]
{
    new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, D3D11.InputClassification.PerVertexData, 0),
    new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
};

我正在通过 vertexBuffer 传递以下一组顶点

mesh = new NonIndexMesh() {
    vertices = new List<Vertex>() {
        new Vertex(new Vector3(-0.5f, 0.5f, 0.0f), Color.Red),
        new Vertex(new Vector3(0.5f, 0.5f, 0.0f), Color.Red),
        new Vertex(new Vector3(0.0f, -0.5f, 0.0f), Color.Red)
    }
}

我的顶点类型如下所示:

public struct Vertex {

    public Vector3 position;
    public Color color;

    public Vertex(Vector3 pos, Color col) {
        position = pos;
        color = col;
    }
}

根据打印到控制台的内容,即使在运行时,传递的数据也是正确的,而且每个渲染顶点的位置似乎都是正确的。

我在这里遗漏了什么导致我渲染的三角形出现这些奇怪的颜色?

您使用的颜色 class 将颜色表示为字节。因此 RGBA 的值范围为 0 - 255。尝试使用 class Color4 将颜色表示为 0.0 - 1.0 范围内的浮点数。这确实是着色器所期望的。