像素着色器的 'alpha' 值是多少?

what is the 'alpha' value of pixel shader?

你好,今天我正在使用 directx11 制作 2d 游戏 api。 而且它指出我需要使用透明效果。

所以我有一个绿色背景和一个脚印在中间。 并且只是在像素着色器中只设置返回颜色的 alpha 值,我取得了一些成功,但问题是它不适用于白色。

这是像素着色器代码

cbuffer CB_TRANSPARENCY : register(b0)
{
    float tp;
};

Texture2D footprint : register(t0);
SamplerState samplerState : register(s0);

struct PS_INPUT
{
    float4 pos : SV_POSITION;
    float2 tex : TEXCOORD;
};

float4 main(PS_INPUT input) : SV_Target
{
    float3 texColor = footprint.Sample(samplerState, input.tex).xyz;
    return float4(texColor, tp);
}

有什么我想念的吗? 或者我应该使用一些混合状态的东西? 任何帮助将不胜感激

[edit] 这里有一些东西要编辑。实际上 alpha 值在没有混合设置的情况下不会做任何事情。只需一个变量即可用于任何自定义计算。 在我的项目中,我使用 spritebathch,spritefont class 在屏幕上渲染字体, 所以我猜在 spritebatch class 中,引擎盖下可能有混合黑色的 blendingState,所以我没有设置我的 blendingState 就得到了这种效果。

是的,您需要使用适当的 alpha 处理模式创建混合状态,然后确保创建的混合状态在绘制之前附加到渲染管道的输出合并阶段:

D3D11_BLEND_DESC blendStateDesc{};
blendStateDesc.AlphaToCoverageEnable = FALSE;
blendStateDesc.IndependentBlendEnable = FALSE;        
blendStateDesc.RenderTarget[0].BlendEnable = TRUE;
blendStateDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendStateDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
blendStateDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

if(not SUCCEEDED(p_device->CreateBlendState(&blendStateDesc, &blendState)))
{
    std::abort();
}

p_device_context->OMSetBlendState(blendState, nullptr, 0xFFFFFFFF);
//draw calls...