【问题标题】:Only do alpha blending on certain output colors仅对某些输出颜色进行 Alpha 混合
【发布时间】:2018-11-30 22:36:10
【问题描述】:

在 MonoGame 中,我有一个具有两种输出颜色的像素着色器。

第一个是绘制到标准 4 通道 32 位颜色渲染目标的常规颜色。

第二个是绘制到 1 通道 16 位 HalfSingle 渲染目标的 z-index。

我想对第一种颜色进行 alpha 混合,而不是在 z-index 上。

有没有办法在不为底层纹理声明采样器并手动进行 alpha 混合的情况下做到这一点?

matrix WorldViewProjection;

Texture2D SpriteTexture;

sampler2D SpriteTextureSampler = sampler_state
{
    Texture = <SpriteTexture>;
};

struct VertexShaderOutput
{
    float4 Position : SV_POSITION;
    float4 Color : COLOR0;
    float2 TextureCoordinates : TEXCOORD0;
    float2 depth : TEXCOORD1;
};

struct PS_OUTPUT
{
    float4 color : COLOR0;
    float4 depth : COLOR1;
};

VertexShaderOutput SpriteVertexShader(float4 position : POSITION0, float2 float4 color : COLOR0, float2 texCoord : TEXCOORD0)
{
    VertexShaderOutput output = (VertexShaderOutput)0;
    output.Position = mul(position, WorldViewProjection);
    output.Color = color;
    output.TextureCoordinates = texCoord;
    output.depth.x = position.z;
    return output;
}

PS_OUTPUT SpritePixelShader(VertexShaderOutput input) : COLOR
{
    PS_OUTPUT output = (PS_OUTPUT)0;
    float4 color = tex2D(SpriteTextureSampler, input.TextureCoordinates) * input.Color;
    output.color = color;
    output.depth.x = input.depth.x;
    return output;
}

technique SpriteDrawing
{
    pass P0
    {
        VertexShader = compile vs_2_0 SpriteVertexShader();
        PixelShader = compile ps_2_0 SpritePixelShader();
    }
};

【问题讨论】:

  • 您能否澄清一下“我想在第一种颜色上进行 alpha 混合,而不是在 z-index 上。”我了解第一种颜色,但是,我不明白接下来的内容。你打算混合什么?

标签: xna shader monogame hlsl alphablending


【解决方案1】:

您可以为每个渲染目标设置独立的混合状态:

BlendState myBlendState = new BlendState();
myBlendState.IndependentBlendEnable = true;

//0 is default and is opaque, modify the blend operation
myBlendState[0].ColorDestinationBlend = Blend.DestinationAlpha;
myBlendState[0].ColorSourceBlend = Blend.InverseDestinationAlpha;

//1 is opaque, no changes needed
myBlendState[1] = noBlend ;

然后您可以使用以下方法附加此状态:

//if you want to restore
var oldBlendState = device.BlendState;

device.BlendState = myBlendState;

//do your draw here
//Restore old blend state if needed
device.BlendState = oldBlendState;

【讨论】:

  • 很遗憾,MonoGame 不支持此功能。
【解决方案2】:

您可以将不想混合的颜色的 alpha 设置为 1。这里我用于深度的渲染目标没有 alpha 通道,但这仍然有效。

output.depth.a = 1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多