Render a 3D model (hair) with semi-transparent texture in SceneKit?

微笑、不失礼 提交于 2019-12-10 00:19:40

问题


I'm trying to render a 3D model in SceneKit but it looks incorrect. For example this model (it's an SCN file with texture and you can reproduce it in your Xcode):

In Xcode Scene Editor it is rendered like this:

Transparency -> Mode -> Dual Layer
Double Sided = true

If I turn off the "Write depth" option, it will look like this: But there are also some issues because it I see only "the lowest layer" of haircut.

I think this should be possible. How to do it right?


回答1:


The reason that in your 3D model some strands of hair popped out when viewed from different angles is quite usual for SceneKit: your model has semi-transparent material that SceneKit can't render properly due to some inner engine rendering techniques (time 49:35) applied to depth buffer.

In order to deal with this problem there are two solutions:

Solution 1:

Your 3D model must have a completely opaque texture (without semi-transparent parts at all). In that case use .dualLayer property.

let scene = SCNScene(named: "art.scnassets/Hair.scn")!
let hair = scene.rootNode.childNode(withName: "MDL_OBJ", recursively: true)!

hair.geometry?.firstMaterial?.transparencyMode = SCNTransparencyMode.dualLayer

Solution 2:

Strands of hair mustn't be a mono-geometry but must be a compound geometry (consisted of several geometry layers unified in one group).

hair.geometry?.firstMaterial?.colorBufferWriteMask = SCNColorMask.all
hair.geometry?.firstMaterial?.readsFromDepthBuffer = false
hair.geometry?.firstMaterial?.writesToDepthBuffer = false
hair.geometry?.firstMaterial?.blendMode = SCNBlendMode.alpha


来源:https://stackoverflow.com/questions/57936887/render-a-3d-model-hair-with-semi-transparent-texture-in-scenekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!