Last active
March 17, 2024 12:19
-
-
Save GrantMeStrength/62364f8a5d7ea26e2b97b37207459a10 to your computer and use it in GitHub Desktop.
Drawing a line between two points in SceneKit / iOS / Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func line(from p1: SCNVector3, to p2: SCNVector3) -> SCNNode? { | |
// Draw a line between two points and return it as a node | |
var indices = [Int32(0), Int32(1)] | |
let positions = [p1, p2] | |
let vertexSource = SCNGeometrySource(vertices: positions) | |
let indexData = Data(bytes: &indices, count:MemoryLayout<Int32>.size * indices.count) | |
let element = SCNGeometryElement(data: indexData, primitiveType: .line, primitiveCount: 1, bytesPerIndex: MemoryLayout<Int32>.size) | |
let line = SCNGeometry(sources: [vertexSource], elements: [element]) | |
let lineNode = SCNNode(geometry: line) | |
return lineNode | |
} | |
// Draws a (very thin) line between two points. | |
// Not sure how to set the color though - materials didn't seem to do much |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have developed a simple project that allows drawing any width and color line through multiple points. It also supports loops and mittered (sharp) joins. https://github.com/bialylis/ThickRedLine