BLENDING (混合)
D3D11可以通过
HRESULT ID3D11Device::CreateBlendState(
const D3D11_BLEND_DESC *pBlendStateDesc,
ID3D11BlendState **ppBlendState);
函数来开启混合模式,将正在光栅化的像素Scr和后台缓存中的像素Dst带入一个混合方程式中进行混合,而不是单纯的替换。
混合方程: C(Src)*F(Src) 操作数 C(Dst) *F(Dst)
操作数有以下几个枚举
typedef enum D3D11_BLEND_OP
{
D3D11_BLEND_OP_ADD = 1,
D3D11_BLEND_OP_SUBTRACT = 2,
D3D11_BLEND_OP_REV_SUBTRACT = 3,
D3D11_BLEND_OP_MIN = 4,
D3D11_BLEND_OP_MAX = 5,
} D3D11_BLEND_OP;
F为混合因子,有以下几个枚举
D3D11_BLEND_ZERO: F = (0,0,0) and F = 0
D3D11_BLEND_ONE: F = (1,1,1) and F = 1
D3D11_BLEND_SRC_COLOR: F = (rs, gs, bs)
D3D11_BLEND_INV_SRC_COLOR: F = (1 − rs, 1 − gs, 1 − bs)
D3D11_BLEND_SRC_ALPHA: F = (as, as, as) and F = as
D3D11_BLEND_INV_SRC_ALPHA: F = (1 − as, 1 − as, 1 − as) and F = 1 − as
D3D11_BLEND_DEST_ALPHA: F = (ad, ad, ad) and F = ad
D3D11_BLEND_INV_DEST_ALPHA: F = (1 − ad, 1 − ad, 1 − ad) and F = 1 − ad
D3D11_BLEND_DEST_COLOR: F = (rd, gd, bd)
D3D11_BLEND_INV_DEST_COLOR: F = (1 − rd, 1 − gd, 1 − bd)
D3D11_BLEND_SRC_ALPHA_SAT:
创建一个混合模式;
//透明融合
D3D11_BLEND_DESC transparentDesc = { 0 };
transparentDesc.AlphaToCoverageEnable = false;
transparentDesc.IndependentBlendEnable = false;
transparentDesc.RenderTarget[0].BlendEnable = true;
transparentDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
transparentDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
transparentDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
transparentDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
transparentDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
transparentDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
transparentDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
HR(device->CreateBlendState(&transparentDesc, &TransparentBS));
在绘制时执行
D3d->GetContext()->OMSetBlendState(RenderStates::TransparentBS, blendFactor, 0xffffffff);
效果图