【问题标题】:Transparency on two rectangles in DirectX, one behind another - I see the background of window instead the second textureDirectX中两个矩形的透明度,一个接一个 - 我看到窗口的背景而不是第二个纹理
【发布时间】:2014-07-21 12:19:50
【问题描述】:

我有一个 DirectX 11 C++ 应用程序,它显示两个带有纹理和一些文本的矩形。 两种纹理均取自 TGA 资源(添加了 Alpha 通道)。

当我运行程序时,我得到了结果:

怎么了?仔细看看:

矩形的角是透明的(应该是透明的)。其余纹理设置为 30% 的不透明度(效果也很好)。

但是,当一个纹理(我们称之为texture1)在另一个(texture2)之上时:

texture1 的角是透明的。 但是在它们后面我看到的是窗口的背景,而不是纹理2。

换句话说,纹理的透明度与窗口的背景相互作用,而不是与它后面的纹理相互作用

我做错了什么?我的程序的哪个部分可以对此负责? 混合选项、渲染状态、着色器代码...?

在我的着色器中,我设置了:

technique10 RENDER{
    pass P0{
        SetVertexShader(CompileShader( vs_4_0, VS()));      
        SetPixelShader(CompileShader( ps_4_0, PS()));
        SetBlendState(SrcAlphaBlendingAdd, float4(0.0f, 0.0f, 0.0f, 0.0f), 
            0xFFFFFFFF);
    }    
}

附: 当然,当我将窗口的背景从蓝色更改为另一种颜色时,元素仍然具有透明度(角不是蓝色)。


编辑:

根据@ComicSansMS(+ 代表尼克,无论如何;p),我尝试更改渲染元素的顺序(我还稍微移动了较小的纹理,以检查错误是否仍然存在):

较小的纹理现在位于较大的纹理后面。但是角落的问题仍然存在(现在它出现在第二个纹理上)。 我几乎可以肯定我在渲染上面的矩形之前在后面绘制了矩形(我看到代码的行顺序)。

我的深度模具:

    //initialize the description of the stencil state
    ZeroMemory(depthStencilsDescs, sizeof(*depthStencilsDescs));

    //set up the description of the stencil state
    depthStencilsDescs->DepthEnable = true;
    depthStencilsDescs->DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    depthStencilsDescs->DepthFunc = D3D11_COMPARISON_LESS;

    depthStencilsDescs->StencilEnable = true;
    depthStencilsDescs->StencilReadMask = 0xFF;
    depthStencilsDescs->StencilWriteMask = 0xFF;

    //stencil operations if pixel is front-facing
    depthStencilsDescs->FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
    depthStencilsDescs->FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

    //stencil operations if pixel is back-facing
    depthStencilsDescs->BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
    depthStencilsDescs->BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthStencilsDescs->BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

    //create the depth stencil state
    result = device->CreateDepthStencilState(depthStencilsDescs, depthStencilState2D);

渲染函数:

...
//clear the back buffer
context->ClearRenderTargetView(myRenderTargetView, backgroundColor); //backgroundColor

//clear the depth buffer to 1.0 (max depth)
context->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
context->OMSetDepthStencilState(depthStencilState2D, 1);
context->VSSetShader(getVertexShader(), NULL, 0);
context->PSSetShader(getPixelShader(), NULL, 0);
for(...){
    rectangles[i]->render();
}

混合状态:

D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(D3D11_BLEND_DESC) );
blendDesc.AlphaToCoverageEnable = false;
blendDesc.IndependentBlendEnable = false;        
blendDesc.RenderTarget[0].BlendEnable = true;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;

blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;      
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;     
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL ;

ID3D11BlendState * blendState;

if (FAILED(device->CreateBlendState(&blendDesc, &blendState))){
}

context->OMSetBlendState(blendState,NULL,0xffffffff);

【问题讨论】:

    标签: c++ directx shader directx-11 pixel-shader


    【解决方案1】:

    您的抽奖顺序可能是错误的。

    混合不与其后面对象的像素交互,它与当前在帧缓冲区中的像素交互。

    所以如果你在前面绘制矩形之前在后面绘制矩形,它的混合操作将与帧缓冲区中的内容(即背景)交互。

    解决方案显然是根据对象在视图空间中的深度对对象进行排序并从后向前绘制,尽管有时说起来容易做起来难(尤其是在允许任意重叠时)。

    另一个问题似乎是您以相同的深度值绘制两个矩形。您的深度测试设置为D3D11_COMPARISON_LESS,因此一旦在一个像素上绘制了一个三角形,另一个三角形将无法通过该像素的深度测试并且根本不会被绘制。这解释了交换绘图顺序时得到的结果。

    请注意,如果您从后向前绘制对象,则根本不需要执行深度测试,因此在这种情况下您可能只想将其关闭。或者,您可以尝试通过赋予不同的 Z 值来沿深度轴排列对象,或者直接切换到D3D11_COMPARISON_LESS_EQUAL 进行深度测试。

    【讨论】:

    • 我不确定是否是渲染它们的顺序。我的意思是,当我更改顺序时,较小的矩形在较大的矩形后面(我看不到较小的矩形,它隐藏在较大的矩形后面)。也许有深度或一些缓冲区?
    • @PolGraphic 矩形的 Z 值是多少?如何配置深度测试?
    • 我已经更新了我的问题 - 我希望我给了你所有你想要的信息。我不太了解“矩形的 Z 值” - 我使用 2d 对象,我只将角的 2d 位置发送到着色器(着色器将 z 和 w 设置为 z=0,w=1)。将它们发送到着色器的顺序决定了哪个矩形将在前面(有效) - 但透明度不会。
    • 像魅力一样工作。谢谢你——你的耐心,以及准确和快速的回​​答。那个社区太棒了。附:正如你所建议的,我决定使用D3D11_COMPARISON_LESS_EQUAL
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 2015-07-28
    • 2010-10-20
    • 1970-01-01
    • 2020-09-12
    • 2014-06-06
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多