簡體   English   中英

在SceneKit中背面剔除

[英]Back face culling in SceneKit

我目前正在嘗試在場景套件中設置一個旋轉球。 我創造了球,並為它應用了紋理。

ballMaterial.diffuse.contents = UIImage(named: ballTexture)
ballMaterial.doubleSided = true
ballGeometry.materials = [ballMaterial]

目前的ballTexture是一個半透明的紋理,因為我希望看到背面滾動。

然而,我得到一些奇怪的剔除,即使doubleSided屬性設置為true,也只顯示一半背面多邊形。

任何幫助將不勝感激,謝謝。

發生這種情況是因為透明度的影響取決於繪制順序。 SceneKit不知道在面向正面之前繪制球體的背面多邊形。 (實際上,如果沒有為每個幀重新組織GPU上的頂點緩沖區,它就無法真正做到這一點,這將對渲染性能產生巨大的影響。)

SCNSphere的頂點布局使其設置為地球上的緯度/長度網格:三角形沿着0°到360°的子午線順序渲染,因此取決於球體相對於攝像機的方向,球體遠端的面部將在較近的面部之前呈現。

要解決此問題,您需要直接或通過深度緩沖區強制渲染順序。 這是一種方法,使用內部表面的單獨材料來說明差異。

// add two balls, one a child of the other
let node = SCNNode(geometry: SCNSphere(radius: 1))
let node2 = SCNNode(geometry: SCNSphere(radius: 1))
scene.rootNode.addChildNode(node)
node.addChildNode(node2)

// cull back-facing polygons on the first ball
// so we only see the outside
let mat1 = node.geometry!.firstMaterial!
mat1.cullMode = .Back
mat1.transparent.contents = bwCheckers
// my "bwCheckers" uses black for transparent, white for opaque
mat1.transparencyMode = .RGBZero

// cull front-facing polygons on the second ball
// so we only see the inside
let mat2 = node2.geometry!.firstMaterial!
mat2.cullMode = .Front
mat2.diffuse.contents = rgCheckers

// sphere normals face outward, so to make the inside respond
// to lighting, we need to invert them
let shader = "_geometry.normal *= -1.0;"
mat2.shaderModifiers = [SCNShaderModifierEntryPointGeometry: shader]

雙面球

(最后的着色器修改器位不是必需的 - 它只是使內部材質得到漫反射着色。您也可以使用不涉及法線或光照的材質屬性,如emission ,具體取決於您想要的外觀。)

你也可以做到這一點使用與禁止雙面材料的單個節點writesToDepthBuffer ,但也可能導致與場景內容的其余部分不必要的相互作用-你可能還需要惹renderingOrder在這種情況下。

macOS 10.13和iOS 11添加了SCNTransparencyMode.dualLayer ,據我所知,甚至不需要將isDoubleSided設置為true(文檔根本不提供任何信息)。 所以一個對我有用的簡單解決方案是:

ballMaterial.diffuse.contents = UIImage(named: ballTexture)
ballMaterial.transparencyMode = .dualLayer  
ballGeometry.materials = [ballMaterial]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM