5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Metal] 半透明の処理(Blending)

Last updated at Posted at 2015-05-08

Metalでブレンディングをする方法をメモ。

MTLRenderPipelineDescriptorのプロパティにあるMTLRenderPipelineColorAttachmentDescriptorの値を適切に設定することで、様々なブレンディングができます。

ちなみに値についてはこちら(Translucency and Transparency in Metal)を参考にしました。

MTLRenderPipelineColorAttachmentDescriptor *colorAttachment = pipelineStateDescriptor.colorAttachments[0];
colorAttachment.blendingEnabled             = YES;
colorAttachment.rgbBlendOperation           = MTLBlendOperationAdd;
colorAttachment.alphaBlendOperation         = MTLBlendOperationAdd;
colorAttachment.sourceRGBBlendFactor        = MTLBlendFactorSourceAlpha;
colorAttachment.sourceAlphaBlendFactor      = MTLBlendFactorSourceAlpha;
colorAttachment.destinationRGBBlendFactor   = MTLBlendFactorOneMinusSourceAlpha;
colorAttachment.destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;

一般的なアルファブレンディングはOneMinusSourceAlphaを利用します。

式は以下のようになります。

C_f = \alpha_s \times C_s + (1 - \alpha_s) \times C_d

$._s$となっている部分はsourcesです。
$._d$となっている部分はdestinationd、つまりブレンド対象の色、ということになります。
CはColorのCです)

式を見てもらうと分かるように、計算の要が$\alpha_s$となっているのが分かると思います。今まさにレンダリングしようとしている色のアルファ値が、最終的な色に影響を与えているわけです。
式でもOneMinusAlpha($1 - \alpha_s$)となっていますね。

そしてそれらを適切に表現した設定が上記のもの、ということになります。

ちなみにこちらの記事(Cocos2d-x で BlendFunc)が色々なブレンディングについて書かれていました。
(内容はCocos2d-xのものですが、基本的な考え方はどれも同じです)

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?