How to display / determine the index of a vertex in ARSCNFaceGeometry?

1k Views Asked by At

I want to get the index of a specific vertex in the mesh of the ARSCNFaceGeometry.

I tried to add a SCNText to the vertex that displays the vertex index:

// Create face node
let faceNode = SCNNode(geometry: geometry) // geometry is the ARSCNFaceGeometry

// Get vertices of face node
let vertices = ...

// For each vertex
vertices.enumerated().forEach({ (index, vertex) in
    
    // Create text geometry
    let textGeometry = SCNText(string: text, extrusionDepth: 1)
    textGeometry.firstMaterial?.diffuse.contents = UIColor.black
    
    // Create text node
    let textNode = SCNNode(geometry: textGeometry)
    textNode.scale = SCNVector3(0.0002, 0.0002, 0.0002)
    textNode.position = SCNVector3(vertex.x, vertex.y, vertex.z)
    
    // Add text node to face mask node
    faceNode.addChildNode(textNode)
})

But the result in Xcode Scene Editor is that some text nodes are positioned correctly, others are scattered all over the place:

enter image description here

Why are the text nodes positioned like that?

Is there another way to get the vertex indices?

Update:

I tried to add SCNBox to every vertex:

let box = SCNBox(width: 0.005, height: 0.005, length: 0.005, chamferRadius: 0)
box.firstMaterial?.multiply.contents = UIColor.blue
let boxNode = SCNNode(geometry: box)
boxNode = vertex
faceNode.addChildNode(boxNode)

The result shows that even the box positions are wrong. So how can I use the vertex to position each box centered in the mesh point?

enter image description here

1

There are 1 best solutions below

0
On

The way I retrieved the vertices from the face mesh was wrong. I use this answer now to extract the vertices and it works.