简体   繁体   中英

DirectX Blending State issue

I've got an issue i failed to solve, so i seek your help. I'm developing a DirectX App, in which my scene contains a page with text on it and i'm trying to add higlighting logic.

So i want to be able to highlight a text and also to change this highlighting's color \\ opacity.

In order to do so, i'm trying to use ID3D11BlendState. Please consider a little code snippet on how i create it:

        HRESULT hr = S_OK;
        D3D11_BLEND_DESC blendDesc;
        D3D11_RENDER_TARGET_BLEND_DESC rtBlend;
        Microsoft::WRL::ComPtr<ID3D11BlendState> state;
        auto d3dDev = this->dxDevMt.GetD3DDevice();

        rtBlend.BlendEnable = TRUE;
        rtBlend.SrcBlend = D3D11_BLEND_DEST_COLOR;
        rtBlend.DestBlend = D3D11_BLEND_ZERO;
        rtBlend.BlendOp = D3D11_BLEND_OP_ADD;
        rtBlend.SrcBlendAlpha = D3D11_BLEND_ONE;
        rtBlend.DestBlendAlpha = D3D11_BLEND_ZERO;
        rtBlend.BlendOpAlpha = D3D11_BLEND_OP_ADD;
        rtBlend.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

        blendDesc.AlphaToCoverageEnable = FALSE;
        blendDesc.IndependentBlendEnable = FALSE;

        for (auto &i : blendDesc.RenderTarget) {
            i = rtBlend;
        }

        hr = d3dDev->CreateBlendState(&blendDesc, state.GetAddressOf());

The problem is, - it is not working in a way i want it to work. As long as alpha equals 1 it looks just fine:

只要alpha等于1,看起来就很好: But if i try to change it somewhat, - any color that was before changes into black: 但我尝试对其进行一些更改,-更改为黑色之前的任何颜色 I don't really understand why is this happening and i ask for your guidance!. Thank you in advance!

Ok, i got it working. In case anyone would ever struggle with this kind of issue, try doing this:

Firstly, pass your highlight color into pixel shader. Then you want do treat this color's alpha value as a lerp value between white color (page background) and your value.

so your shader would look presumably something like:

cbuffer Buf0 : register(b0){
    float4 Color;
}

struct PsInput{
    float4 pos : SV_POSITION;
};

float4 main(PsInput input) : SV_TARGET{
    float4 color2 = lerp(float4(1,1,1,1), Color, Color.a);
    return color2;
}

and after that you want to set your blendstate to multiply colors:

rtBlend.BlendEnable = TRUE;
rtBlend.SrcBlend = D3D11_BLEND_DEST_COLOR;
rtBlend.DestBlend = D3D11_BLEND_ZERO;
rtBlend.BlendOp = D3D11_BLEND_OP_ADD;
rtBlend.SrcBlendAlpha = D3D11_BLEND_ZERO;// D3D11_BLEND_ONE;
rtBlend.DestBlendAlpha = D3D11_BLEND_ONE;// D3D11_BLEND_ZERO;
rtBlend.BlendOpAlpha = D3D11_BLEND_OP_ADD;
rtBlend.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

blendDesc.AlphaToCoverageEnable = FALSE;
blendDesc.IndependentBlendEnable = FALSE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM