在SceneKit节点周围添加边框

时间:2016-07-28 12:57:18

标签: ios swift scenekit scnnode

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

ssh -i myrsa.key centos@server

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

3 个答案:

答案 0 :(得分: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()
    }
}

答案 1 :(得分:2)

SCNNode符合SCNBoundingVolume协议。

此协议定义getBoundingBoxMin:max:方法。

使用此选项可获取附加到节点的几何体的边界框的最小和最大坐标。

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

答案 2 :(得分:-1)

如果节点是原始形状,则可以将UIImage设置为diffuse。图像将被拉伸以覆盖您的节点。如果您使用的图像上带有边框,则将转换为在节点上创建边框。

planeGeometry.firstMaterial?.diffuse.contents = UIImage(named: "blueSquareOutline.png")

Example border image