gpt4 book ai didi

ios - 在 SceneKit 节点周围添加边框

转载 作者:搜寻专家 更新时间:2023-10-30 21:55:09 26 4
gpt4 key购买 nike

我正在尝试使用点击手势突出显示 SceneKit 中的选定节点。不幸的是,我没能完成它。我能做的最好的事情就是在点击节点时更改 Material 。

let material = key.geometry!.firstMaterial!
material.emission.contents = UIColor.blackColor()

有人可以建议我可以只在对象周围添加边框或轮廓而不是更改整个节点的颜色吗?

最佳答案

基于@Karl Sigiscar 的回答和 SO 中的另一个回答,我想到了这个:

func createLineNode(fromPos origin: SCNVector3, toPos destination: SCNVector3, color: UIColor) -> SCNNode {
let line = lineFrom(vector: origin, toVector: destination)
let lineNode = SCNNode(geometry: line)
let planeMaterial = SCNMaterial()
planeMaterial.diffuse.contents = color
line.materials = [planeMaterial]

return lineNode
}

func lineFrom(vector vector1: SCNVector3, toVector vector2: SCNVector3) -> SCNGeometry {
let indices: [Int32] = [0, 1]

let source = SCNGeometrySource(vertices: [vector1, vector2])
let element = SCNGeometryElement(indices: indices, primitiveType: .line)

return SCNGeometry(sources: [source], elements: [element])
}


func highlightNode(_ node: SCNNode) {
let (min, max) = node.boundingBox
let zCoord = node.position.z
let topLeft = SCNVector3Make(min.x, max.y, zCoord)
let bottomLeft = SCNVector3Make(min.x, min.y, zCoord)
let topRight = SCNVector3Make(max.x, max.y, zCoord)
let bottomRight = SCNVector3Make(max.x, min.y, zCoord)


let bottomSide = createLineNode(fromPos: bottomLeft, toPos: bottomRight, color: .yellow)
let leftSide = createLineNode(fromPos: bottomLeft, toPos: topLeft, color: .yellow)
let rightSide = createLineNode(fromPos: bottomRight, toPos: topRight, color: .yellow)
let topSide = createLineNode(fromPos: topLeft, toPos: topRight, color: .yellow)

[bottomSide, leftSide, rightSide, topSide].forEach {
$0.name = kHighlightingNode // Whatever name you want so you can unhighlight later if needed
node.addChildNode($0)
}
}

func unhighlightNode(_ node: SCNNode) {
let highlightningNodes = node.childNodes { (child, stop) -> Bool in
child.name == kHighlightingNode
}
highlightningNodes.forEach {
$0.removeFromParentNode()
}
}

关于ios - 在 SceneKit 节点周围添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38637075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com