在SceneKit节点周围添加边框

7 ios scenekit swift scnnode

我试图用点击手势突出显示SceneKit中的选定节点.不幸的是,我无法完成它.我能做的最好的事情是在点击节点时更改材料.

let material = key.geometry!.firstMaterial!
material.emission.contents = UIColor.blackColor()
Run Code Online (Sandbox Code Playgroud)

有人可以提出一种方法,我可以只是在对象周围添加边框或轮廓而不是改变整个节点的颜色?

lea*_*rco 6

根据@Karl Sigiscar的答案以及另一个答案,我在这里得到了这个:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)


Kar*_*car 3

SCNNode 遵循 SCNBoundingVolume 协议。

该协议定义了getBoundingBoxMin:max:方法。

使用它来获取附加到节点的几何体的边界框的最小和最大坐标。

然后使用SceneKit基元类型SCNGeometryPrimitiveTypeLine来绘制边界框的线条。检查 SCNGeometryElement。

  • 您能否发布一些有关如何执行此操作的示例代码?即使我也在寻找相同的解决方案 (2认同)