0

I'm trying to blend a source texture with some snowflakes in Metal, but I can't find blending options that work for my use case.

This is the closest I've gotten to it.

The blending options I'm currently using are:

descriptor.sourceRGBBlendFactor        = MTLBlendFactorSourceAlpha;
descriptor.sourceAlphaBlendFactor      = MTLBlendFactorOne;
descriptor.destinationRGBBlendFactor   = MTLBlendFactorOne;
descriptor.destinationAlphaBlendFactor = MTLBlendFactorOne;

I've tried these.

I expected the blue portion of the snowflakes to blend with the blue background, not with the underlying one.

Can anyone explain to me what I'm getting wrong, what the correct formula should be and how we arrive to it?

vimuth
  • 5,064
  • 33
  • 79
  • 116

1 Answers1

0

You code is missing something. I think you need an add blend operation for both rgbBlendOperation and alphaBlendOperation. If you just want to add a snowflake on top, I think the factors you have should be enough.

If you take a look at https://developer.apple.com/documentation/metal/mtlblendoperation/add, it lists some formulas

RGB = Source.rgb * SBF + Dest.rgb * DBF
A = Source.a * SBF + Dest.a * DBF

where SBF is source blend factor, and DBF is destination blend factor.

So, basically, to expand in your case,

RGB = Source.rgb * Source.a + Dest.rgb
A = Source.a * SBF + Dest.a

Seems like it should do the trick.

JustSomeGuy
  • 3,677
  • 1
  • 23
  • 31