Directx 什么是';α';像素着色器的值?
嗨,有一天我正在使用DirectX11API制作2d游戏。 现在我需要使用透明效果 所以我有一个绿色的背景,中间有一个脚印。 在像素着色器中,除了返回颜色的alpha值之外,我没有设置任何东西,我取得了一些成功,但问题是它不适用于白色 这是像素着色器代码Directx 什么是';α';像素着色器的值?,directx,shader,pixel,alpha,blending,Directx,Shader,Pixel,Alpha,Blending,嗨,有一天我正在使用DirectX11API制作2d游戏。 现在我需要使用透明效果 所以我有一个绿色的背景,中间有一个脚印。 在像素着色器中,除了返回颜色的alpha值之外,我没有设置任何东西,我取得了一些成功,但问题是它不适用于白色 这是像素着色器代码 cbuffer CB_TRANSPARENCY : register(b0) { float tp; }; Texture2D footprint : register(t0); SamplerState samplerState :
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);
}
有什么我错过的吗?
或者我应该用一些混合状态的东西?
任何帮助都将不胜感激
[编辑]这里有一些需要编辑的内容。实际上,alpha值在没有混合设置的情况下不起任何作用。只有一个变量可用于任何自定义计算。
在我的项目中,我使用SpriteBatch,spritefont类在屏幕上呈现字体,
所以我猜在spritebatch类中,引擎盖下可能有混合黑色的混合状态,因此我在不设置混合状态的情况下获得了这种效果。是的,需要使用适当的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...
是的,您需要使用适当的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...
请注意,上面的代码使用的是“纯alpha”,这是alpha混合的一种方法。使用预乘alpha是另一种选择,对于SrcBlend/SrcBlendAlpha状态,使用
D3D11_blend_ONE
的混合具有优势。请注意,上面的代码使用的是“纯alpha”,这是alpha混合的一种方法。使用预乘alpha是另一种选择,对于SrcBlend/SrcBlendAlpha状态,使用D3D11_blend_ONE
的混合具有优势。看见