locked
How do I do 3D transparency without painters algorithm?

    Question

  • In my test case, I am rendering two objects, both with textures that have transparency. (Think a circle with the rest of the texture rectangle having an alpha of 0.0).

    The transparency is working, but only if I draw them in a “Painters Algorithm” order.  If I draw the objects so that the closest draws last, the transparent portion of the rectangle overwrites the pixels below it with the background color.

    At first, there was no transparency, but I finally found a sample (DDSView), where I have blindly copied how they set up a D3D11_BLEND_DESC structure, and use it during “Render” by calling device->CreateBlendState.

    // In CreateDeviceResources for my object:
    
    void GeoShader::CreateBlendState(Microsoft::WRL::ComPtr<ID3D11Device1> device)
    {
    	D3D11_BLEND_DESC blendDesc = 
        {
            false,
            false,
            {
            true,
            D3D11_BLEND_SRC_ALPHA,
            D3D11_BLEND_INV_SRC_ALPHA,
            D3D11_BLEND_OP_ADD,
            D3D11_BLEND_ZERO,
            D3D11_BLEND_ZERO,
            D3D11_BLEND_OP_ADD,
            D3D11_COLOR_WRITE_ENABLE_ALL
            } 
        };
    
    	DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &BlendState));
    }
    
    // In "Render" for the same objects
    float bf [4] = {1.0f, 1.0f, 1.0f, 1.0f};
    context->OMSetBlendState(*geoBlueprint->Shader->BlendState.GetAddressOf(), bf, 0xffffffff );

    I have a very simple pixel shader, copied from another 3D sample, which is shown below.

    float4 main(PixelShaderInput input) : SV_TARGET
    {
        return SimpleTexture.Sample(SimpleSampler, input.tex) * 1.0f;
    }

    Question: What is the next step needed to allow me to ignore the draw order, and have the pixel shader blend what has already been drawn in back of it instead of overwriting it’s transparent area with the background color?

    In my search for an answer, I have come across answers like “disable Z writes”.  If that’s the answer, I can’t figure out where that would be done.  Is the solution in more code in the shader?  Is it that I just have the blend description set up wrong?   I’m not asking for someone to write my code for me, but I am going to need more than a 3 word answer that would make sense to a “real” shader writer.




    Jim Tomasko


    • Edited by James Tomasko Saturday, September 08, 2012 6:39 PM error in question corrected
    Saturday, September 08, 2012 6:38 PM

All replies