低于0.5f的Alpha值不可见

问题描述

我目前正在开发一种新的机制,以可视化激光束撞击飞船的护罩。完成了cpu方面的开发,并且Vertex Shader正常运行,但是在创建Pixel Shader时遇到了一个问题:任何低于0.5的透明度值都是不可见的。

以下像素着色器非常简单:如果像素在命中半径内,则显示为半透明的蓝色像素,否则将不显示任何内容

float4 PS(PS_IN input) : SV_Target
{
    if (input.impactdistances.x > 0.0f && input.impactdistances.x < 2.0f)
    {
        return float4(0.0f,0.0f,1.0f,0.5f);
    }

    return float4(1.0f,0.0f);
}

结果是这样的(请参见黄色箭头上方的蓝色区域)。

enter image description here

现在,如果我换行

    return float4(0.0f,0.5f);

    return float4(0.0f,0.4f);

然后,影响区域是完全看不见的,我想不出任何导致这种行为的因素。你有什么主意吗?

如果有帮助,这是我用于整个游戏的混合状态的设置。

var blendStateDescription = new SharpDX.Direct3D11.BlendStateDescription();
blendStateDescription.rendertarget[0].IsBlendEnabled = true;
blendStateDescription.rendertarget[0].rendertargetWriteMask = SharpDX.Direct3D11.ColorWriteMaskFlags.All;

blendStateDescription.rendertarget[0].sourceBlend = SharpDX.Direct3D11.BlendOption.sourceAlpha;
blendStateDescription.rendertarget[0].BlendOperation = SharpDX.Direct3D11.BlendOperation.Add;
blendStateDescription.rendertarget[0].DestinationBlend = SharpDX.Direct3D11.BlendOption.InverseSourceAlpha;

blendStateDescription.rendertarget[0].sourceAlphaBlend = SharpDX.Direct3D11.BlendOption.Zero;
blendStateDescription.rendertarget[0].AlphaBlendOperation = SharpDX.Direct3D11.BlendOperation.Add;
blendStateDescription.rendertarget[0].DestinationAlphaBlend = SharpDX.Direct3D11.BlendOption.One;

blendStateDescription.AlphaToCoverageEnable = true;

_blendState = new SharpDX.Direct3D11.BlendState(_device,blendStateDescription);

_deviceContext.OutputMerger.SetBlendState(_blendState);

解决方法

blendStateDescription.AlphaToCoverageEnable = true;

肯定会在您的情况下造成问题,将其设置为false将会应用正确的Alpha混合。