Direct3D rendering 2D images with “multiply” blending mode and alpha

后端 未结 4 2025
暖寄归人
暖寄归人 2021-01-03 04:31

I\'m trying to replicate the Photoshop filter multiply with Direct3D. I\'ve been reading and googling about the different render states and I\'ve got the effect almost worki

4条回答
  •  猫巷女王i
    2021-01-03 05:30

    You can achieve this effect in one step by premultipling alpha in your pixel shader, or by using textures with pre-multiplied alpha.

    For example if you have 3 possible blend operations for a shader, and you want each one to take alpha into account.

    Blend = ( src.rgb * src.a ) + ( dest.rgb * (1-src.a) )
    Add = ( src.rgb * src.a ) + ( dest.rgb )
    Multiply = (src.rgb * dest.rgb * src.a) + (dest.rgb * (1-src.a) )
    

    You'll notice that Multiply is impossible with a single pass because there are two operations on the source color. But if you premultiply alpha in your shader you can extract the alpha component from the blending operation and it becomes possible to blend all three operations in the same shader.

    In your pixel shader you can pre-multiply alpha manually. Or use a tool like DirectXTex texconv to modify your textures.

    return float4(color.rgb*color.a, color.a);
    

    The operations become:

    Blend = ( src.rgb ) + ( dest.rgb * (1-src.a) )
    Add = ( src.rgb ) + ( dest.rgb )
    Multiply = ( src.rgb * dest.rgb ) + (dest.rgb * (1-src.a) )
    

提交回复
热议问题