最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - How can I make custom depth testing logic in Metal? - Stack Overflow

programmeradmin1浏览0评论

I am implementing a basic partial transparency system. While this is not correct, order independent transparency, this does work well enough (is more-or-less correct) when dealing with objects that are either fully opaque or fully of about 50% transparency.

let depth = MTLDepthStencilDescriptor()
depth.depthCompareFunction = .less
readDepth = device.makeDepthStencilState(descriptor: depth)
depth.isDepthWriteEnabled = true
writeDepth = device.makeDepthStencilState(descriptor: depth)
func draw(in view: MTKView) {

    // Boilerplate that defines an MTLRenderCommandEncoder encoder

    encoder.setDepthStencilState(writeDepth)

    // Render all opaque objects

    encoder.setDepthStencilState(readDepth)

    // Render all partially transparent objects
}

The idea is to make opaque objects occlude other objects, but make transparent objects not occlude each other.

However, this approach falls short if I have a single texture that contains both opaque and transparent portions. If I render them with the opaque objects, they will incorrectly occlude objects behind the transparent portions, and if I render them with the transparent objects, the opaque portions will incorrectly fail to occlude each other if rendered in the wrong order.

Is there a way to set a custom function for the depth testing system besides the predefined functions? So that I can implement my own comparison and depth writing logic (which in my case would be, only write to the depth buffer if the pixel has a transparency/alpha of 0)?

发布评论

评论列表(0)

  1. 暂无评论