Adding a border around a SceneKit node

I am trying to highlight a highlighted node in SceneKit using tap gestures. Unfortunately, I could not do this. The best I could do was change the stuff when clicking node.

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

Can someone suggest a way that I can just add a border or outline around an object instead of changing the color of the entire node?

+5
source share
2 answers

SCNNode conforms to the SCNBoundingVolume protocol.

This protocol defines the getBoundingBoxMin: max: method.

Use this to get the minimum and maximum coordinates of the bounding box of the geometry attached to the node.

Then use the primitive SceneKit SCNGeometryPrimitiveTypeLine type to draw the bounding box lines. Check the SCNGeometryElement element.

+2
source

Based on @Karl Sigiscar's answer and another answer here in SO, I came up with the following:

 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
source

All Articles