1启用带有已加载的具有Alpha的PNG纹理的Blendstat

1启用带有已加载的具有Alpha的PNG纹理的Blendstat

本文介绍了Windows 8 Store App DirectX 11.1启用带有已加载的具有Alpha的PNG纹理的Blendstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将具有透明度的PNG加载到纹理中:

I am loading a PNG with transparency to a texture with the following code:

ComPtr<IWICStream> stream;
ComPtr<IWICBitmapDecoder> bitmapDecoder;
ComPtr<IWICBitmapFrameDecode> bitmapFrame;
ComPtr<IWICFormatConverter> formatConverter;
unsigned int width, height
D3D11_SUBRESOURCE_DATA resourceData;

ZeroMemory(&resourceData, sizeof(resourceData));

DX::ThrowIfFailed( m_wicFactory->CreateStream(&stream) );
DX::ThrowIfFailed( stream->InitializeFromMemory( rawFileBytes->Data, rawFileBytes->Length) );
DX::ThrowIfFailed( m_wicFactory->CreateDecoderFromStream( stream.Get(), nullptr, WICDecodeMetadataCacheOnDemand, &bitmapDecoder ) );
DX::ThrowIfFailed( bitmapDecoder->GetFrame(0, &bitmapFrame) );
DX::ThrowIfFailed( m_wicFactory->CreateFormatConverter(&formatConverter) );
DX::ThrowIfFailed( formatConverter->Initialize( bitmapFrame.Get(), GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 1.0f /* some docs set this to 0.0f */, WICBitmapPaletteTypeCustom ) );
DX::ThrowIfFailed( bitmapFrame->GetSize(&width, &height) );

std::unique_ptr<byte[]> bitmapPixels(new byte[width * height * 4]);
DX::ThrowIfFailed( formatConverter->CopyPixels( nullptr, width * 4, width * height * 4, bitmapPixels.get() ) );

resourceData.pSysMem = bitmapPixels.get();
resourceData.SysMemPitch = width * 4;
resourceData.SysMemSlicePitch = 0;

CD3D11_TEXTURE2D_DESC textureDesc( DXGI_FORMAT_B8G8R8A8_UNORM, width, height, 1, 1 );
DX::ThrowIfFailed( m_d3dDevice->CreateTexture2D( &textureDesc, &resourceData, &texture2D ) );

if ( textureView != nullptr ) {
  CD3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc( texture2D.Get(), D3D11_SRV_DIMENSION_TEXTURE2D );
  DX::ThrowIfFailed( m_d3dDevice->CreateShaderResourceView( texture2D.Get(), &shaderResourceViewDesc, &shaderResourceView ) );
}

并在另一段代码中设置我的混合状态:

And to set up my blend state in a different section of code:

Microsoft::WRL::ComPtr<ID3D11BlendState1> blendState;

D3D11_BLEND_DESC1 desc;
ZeroMemory( &desc, sizeof( desc ) );
desc.IndependentBlendEnable = FALSE;
desc.AlphaToCoverageEnable = FALSE;
desc.RenderTarget[0].BlendEnable = TRUE;
desc.RenderTarget[0].LogicOpEnable = FALSE;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND::D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND::D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP::D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND::D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND::D3D11_BLEND_ONE;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP::D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

direct3d.device->CreateBlendState1( &desc, blendState.GetAddressOf() );
direct3d.context->OMSetBlendState( blendState.Get(), NULL, 0xffffffff );

通过所有这些设置,我仍然得到应该有Alpha的黑色背景。

With all this setup, I still get black backgrounds where there should be alpha.

说明:我有一组48个正方形的面板,从z 0.0f到48.0f彼此重叠,但是我只能看到前48.0f纹理。而不是透明区域是透明的,它们被渲染为黑色。

Clarification: I have a set of 48 square panels that are being overlayed on each other from z 0.0f to 48.0f, but all I can see is the very front 48.0f texture. Rather than the transparent zones being transparent, they are being rendered as black.

编辑:这是我的像素着色器:

Here is my pixel shader:

Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);

struct sPSInput
{
    float4 pos : SV_POSITION;
    float3 norm : NORMAL;
    float2 tex : TEXCOORD0;
};

float4 SimplePixelShader(sPSInput input) : SV_TARGET
{
  float4 textured = Texture.Sample(Sampler, input.tex);
  return textured;
}


推荐答案

    desc.RenderTarget[0].SrcBlend = D3D11_BLEND::D3D11_BLEND_ONE;

应该是这个

   desc.RenderTarget[0].SrcBlend = D3D11_BLEND::D3D11_BLEND_ALPHA;

AlphaToCoverageEnable也应根据您的需要设置为假

also AlphaToCoverageEnable should be set false for your needs

此外,您必须禁用Z缓冲区。启用的Z缓冲区可防止绘制前面对象后面的所有对象。

Furthermore you have to disable the Z buffer. The enabled Z buffer prevents all objects behind the object in front to be drawn.

这篇关于Windows 8 Store App DirectX 11.1启用带有已加载的具有Alpha的PNG纹理的Blendstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:58