0

Swift 5.5, iOS 15

I have box I build using SCNBox like this using SceneKit.

let colors = [UIColor(red: 1, green: 0, blue: 0, alpha: 0.4), // front
                  UIColor(red: 0, green: 1, blue: 0, alpha: 0.4), // right
                  UIColor(red: 0, green: 0, blue: 1, alpha: 0.4), // back
                  UIColor(red: 1, green: 1, blue: 0, alpha: 0.4), // left
                  UIColor(red: 1, green: 140/255, blue: 0, alpha: 0.4), // top
                  UIColor(red: 1, green: 1, blue: 1, alpha: 0.4)] // bottom

let sideMaterials = colors.map { color -> SCNMaterial in
            let material = SCNMaterial()
            material.diffuse.contents = color
            material.locksAmbientWithDiffuse = true
            return material
        }
cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.2)
cubeGeometry.materials = sideMaterials

And I am trying to figure out a way to see which face I hit when I click on it, so I using a hitTest function.

That produces an array of hits that I can look at. I tried looking at hit.geometryIndex, but that doesn't give me what I want? I want to be able to identify which of the six sides I tapped on.

I tried this answer that looked very promising, but it sadly it isn't reliable.

Identify face of a cube hit on touches began in Swift - SceneKit

hit.faceindex looks more promising to me, but how can I find out what the index values have been set too? The box is made up of triangles? Can I assume I got 12 face indexes given I got two per side?

How can I list the face index numbers?

user3069232
  • 7,756
  • 7
  • 41
  • 74

0 Answers0