SCNBox and its vertices don't match

I fully admit that I'm a SceneKit noob but I cannot seem to figure this one out with internet searches. In the following code, I generate a SCNBox and then extract the vertices of the triangular mesh. Well, the mesh doesn't match the box at all. If fact, it's always the default SCNBox with dimensions from -0.5 to 0.5 in x, y, and z.


        let rect = SCNBox(width: 40.0, height: 40.0, length: 5.0, chamferRadius: 0.0)
        let srcs = rect.sources(for: .vertex)
        guard let src = srcs.first else { exit(1)}

        let bperC = src.bytesPerComponent
        let stride = src.dataStride / bperC
        let offset = src.dataOffset / bperC
        let vectorCount = src.vectorCount
        
        let vertices = src.data.withUnsafeBytes { (buffer : UnsafePointer<Float>) -> [float3] in
                        var result = Array<float3>()
                        for i in 0...vectorCount - 1 {
                            let start = i * stride + offset
                            let x = buffer[start]
                            let y = buffer[start + 1]
                            let z = buffer[start + 2]
                            result.append(float3(x, y, z))
                        }
                        return result
                    }

        print(vertices)



The output of this is:

[float3(-0.5, -0.5, 0.5), float3(-0.5, 0.5, 0.5), float3(0.5, -0.5, 0.5), float3(0.5, 0.5, 0.5), 
     float3(0.5, -0.5, 0.49999997), float3(0.5, 0.5, 0.49999997), float3(0.49999994, -0.5, -0.5), 
     float3(0.49999994, 0.5, -0.5), float3(0.49999994, -0.5, -0.5000001), float3(0.49999994, 0.5, -0.5000001), 
     float3(-0.5, -0.5, -0.5), float3(-0.5, 0.5, -0.5), float3(-0.5, -0.5, -0.5), float3(-0.5, 0.5, -0.5), 
     float3(-0.5, -0.5, 0.5), float3(-0.5, 0.5, 0.5), float3(-0.5, 0.5, 0.49999997), float3(-0.5, 0.49999994, -0.5),
     float3(0.5, 0.5, 0.49999997), float3(0.5, 0.49999994, -0.5), float3(-0.5, -0.49999997, -0.5), 
     float3(-0.5, -0.5, 0.49999994), float3(0.5, -0.49999997, -0.5), float3(0.5, -0.5, 0.49999994)]


Even though my box is supposed to have width and height of 40.0 and a length of 5.0. What am I missing? I have looked for some scale factors but from what I can tell those exist when you make a node from a geometry but not in the geometry itself.


Also, if I make a node from this rect and add it to the scene, it display with the proper dimensions (40 x 40 x 5).


Thanks for any assistance. I need to extract vertices for perfoming some metal ray-tracing calculations and this one has stumped me.


-Matt

Replies

To make the situation even more complicated. I run this code in a Playground and it give me the output I expect.


[float3(-20.0, -20.0, 2.5), float3(-20.0, 20.0, 2.5), float3(20.0, -20.0, 2.5), 
     float3(20.0, 20.0, 2.5), float3(20.0, -20.0, 2.5), float3(20.0, 20.0, 2.5), 
     float3(20.0, -20.0, -2.500002), float3(20.0, 20.0, -2.500002), float3(20.0, -20.0, -2.5000033), 
     float3(20.0, 20.0, -2.5000033), float3(-20.0, -20.0, -2.5), float3(-20.0, 20.0, -2.5), 
     float3(-20.0, -20.0, -2.5), float3(-20.0, 20.0, -2.5), float3(-20.0, -20.0, 2.5000005),
     float3(-20.0, 20.0, 2.5000005), float3(-20.0, 20.0, 2.5), float3(-20.0, 20.0, -2.500002), 
     float3(20.0, 20.0, 2.5), float3(20.0, 20.0, -2.500002), float3(-20.0, -20.0, -2.5), 
     float3(-20.0, -20.0, 2.499998), float3(20.0, -20.0, -2.5), float3(20.0, -20.0, 2.499998)


The code doesn't work when I run it is in my view controller's viewDidLoad() method?? It gives the previous output of +-0.5 in all dimensions.

Hello,


If you are able to wait until the renderer:updateAtTime: method has been called at least once before you query the geometry source, you should get the correct results.


It is worth filing a bug report for this issue anyway at https://developer.apple.com/bug-reporting/