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