网格是有颜色的,虽然我没有给它任何颜色,但已经足够了

Mesh is colored, enough though I don't give it any color

我没有给顶点着色器任何颜色,但它用不同的颜色渲染网格。你可以在这里看到它

我不知道为什么它会变成那样的颜色,有什么想法吗?

这是我的着色器

cbuffer cbPerObject
{
    float4x4 gWorldViewProj; 
};

struct VertexIn
{
    float3 Pos  : POSITION;
    float4 Color : COLOR;
};

struct VertexOut
{
    float4 PosH  : SV_POSITION;
    float4 Color : COLOR;
};

VertexOut VS(VertexIn vin)
{
    VertexOut vout;

    // Transform to homogeneous clip space.
    vout.PosH = mul(float4(vin.Pos, 1.0f), gWorldViewProj);

    // Just pass vertex color into the pixel shader.
    vout.Color = vin.Color;

        return vout;
}

float4 PS(VertexOut pin) : SV_Target
{
    return pin.Color;
}

technique11 ColorTech
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
}

这是我初始化管道的部分:

    HRESULT result;

    GeometryGenerator::MeshData grid;
    GeometryGenerator geoGen;
    geoGen.CreateGrid(160.0f, 160.0f, 50, 50, grid);
    m_gridIndexCount = grid.indices.size();

    std::vector<Vertex> vertices(grid.vertices.size(),Vertex(XMFLOAT3(0,0,0),XMFLOAT4(0,0,0,0)));
    for (UINT i = 0; i < grid.vertices.size(); ++i)
    {
        XMFLOAT3 p = grid.vertices[i].Position;
        p.y = GetHeight(p.x, p.z);

        vertices[i].pos = p;
        /*
        if (p.y < -10.0f)
        {
            //sandy beach color
            vertices[i].color = XMFLOAT4(1.0f, 0.96f, 0.62f, 1.0f);
        }
        else if (p.y < 5.0f)
        {
            //light yellow-green color
            vertices[i].color = XMFLOAT4(0.48f, 0.77f, 0.46f, 1.0f);
        }
        else if (p.y < 12.0f)
        {
            //dark yellow-green color
            vertices[i].color = XMFLOAT4(0.1f, 0.48f, 0.19f, 1.0f);
        }
        else if (p.y < 20.f)
        {
            //dark brown color
            vertices[i].color = XMFLOAT4(0.45f, 0.39f, 0.34f, 1.0f);
        }
        else
        {
            //white snow color
            vertices[i].color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
        }*/
    }

    D3D11_BUFFER_DESC vertexDesc;
    ZeroMemory(&vertexDesc, sizeof(vertexDesc));
    vertexDesc.Usage = D3D11_USAGE_IMMUTABLE;
    vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexDesc.ByteWidth = sizeof(Vertex)* grid.vertices.size();
    D3D11_SUBRESOURCE_DATA resourceData;
    ZeroMemory(&resourceData, sizeof(resourceData));
    resourceData.pSysMem = &vertices[0];
    result = m_pd3dDevice->CreateBuffer(&vertexDesc, &resourceData, &m_pVertexBuffer);

    if (FAILED(result))
    {
        return false;
    }

    D3D11_BUFFER_DESC indexDesc;
    ZeroMemory(&indexDesc, sizeof(indexDesc));
    indexDesc.Usage = D3D11_USAGE_IMMUTABLE;
    indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    indexDesc.ByteWidth = sizeof(UINT)* m_gridIndexCount;

    D3D11_SUBRESOURCE_DATA indexData;
    ZeroMemory(&indexData, sizeof(indexData));
    indexData.pSysMem = &grid.indices[0];
    result = m_pd3dDevice->CreateBuffer(&indexDesc, &indexData, &m_pIndexBuffer);

    if (FAILED(result))
    {
        return false;
    }

    DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined _DEBUG || defined DEBUG
    shaderFlags = D3DCOMPILE_DEBUG;
#endif

    ID3D10Blob *compiledShader = 0;
    ID3D10Blob *compilationMsgs = 0;
    result = D3DX11CompileFromFile("SolidColor.fx", 0, 0, 0, "fx_5_0", shaderFlags,
        0, 0, &compiledShader, &compilationMsgs, 0);

    if (compilationMsgs != 0)
    {
        MessageBox(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0);
        compilationMsgs->Release();
        compilationMsgs = 0;
    }
    if (FAILED(result))
    {
        MessageBox(0, "error", 0, 0);
        return false;
    }

    result = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(),
        0, m_pd3dDevice, &m_pFx);
    compiledShader->Release();

    if (FAILED(result))
    {
        MessageBox(0, "error", 0, 0);
        return false;
    }

    m_pTechnique = m_pFx->GetTechniqueByName("ColorTech");
    m_pFxWorldViewProj = m_pFx->GetVariableByName("gWorldViewProj")->AsMatrix();


    D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }
    };

    UINT numLayoutElements = ARRAYSIZE(solidColorLayout);
    D3DX11_PASS_DESC passDesc;
    m_pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);

    result = m_pd3dDevice->CreateInputLayout(solidColorLayout, numLayoutElements, passDesc.pIAInputSignature,
        passDesc.IAInputSignatureSize, &m_pInputLayout);

    return true;

这是因为顶点着色器和像素着色器必须在 output/input 上匹配。在这里,SV_POSITION 语义在像素着色器中未使用并被剥离。因此,像素着色器在 "register 0" 中读取颜色,而顶点着色器在 "register 1" 中写入颜色,在 "register 0" 中写入位置。

要修复它,请像这样更改您的结构:

struct VertexOut
{
    float4 Color : COLOR;
    float4 PosH  : SV_POSITION;
};