quaternionboy
quaternionboy

Reputation: 361

Rendering opaque object inside semi-transparent one in Metal

I’m trying to render an opaque object inside a semi-transparent one.

My approach is:

Depth compare function set to less

descriptor.depthCompareFunction = .less

  1. First a draw call with the opaque object, with not blending enabled.

  2. A second draw call with the semi-transparent object, with blending enabled.

if !opaque {
    pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
    pipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
    pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
    pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
}

But I get unexpected result: example

all the code here: https://github.com/quaternionboy/Metal-Playground

Upvotes: 1

Views: 85

Answers (1)

quaternionboy
quaternionboy

Reputation: 361

I found, with the help of Caroline Begbie (raywenderlich), the solution: sort front to back the vertices...

https://forums.raywenderlich.com/t/rendering-opaque-object-inside-transparent-one/121942/2

Upvotes: 2

Related Questions