I tried simplest example to use .polygon type:
let vertices: [SCNVector3] = [
SCNVector3(-1, 0, 0),
SCNVector3( 0, 1, 0),
SCNVector3( 1, 0, 0)
]
let indices: [Int32] = [3, 0, 1, 2]
let indexData = NSData(bytes: indices,
length: MemoryLayout<Int32>.size * indices.count)
as Data
let element = SCNGeometryElement(data: indexData,
primitiveType: .polygon,
primitiveCount: 1,
bytesPerIndex: MemoryLayout<Int32>.size)
Now when I try simple vertex source as
let vertexSource = SCNGeometrySource(vertices: vertices)
All works well. However when I use NSData it does not work:
let data = NSData(bytes: vertices,
length: MemoryLayout<SCNVector3>.size * vertices.count)
as Data
let vertexSource = SCNGeometrySource(data: data,
semantic: .vertex,
vectorCount: vertices.count,
usesFloatComponents: true,
componentsPerVector: 3,
bytesPerComponent: MemoryLayout<CGFloat>.size,
dataOffset: 0,
dataStride: MemoryLayout<SCNVector3>.stride)
I need to use NSData approach later on but I want to understand what is wrong in this simplest example. Any help is appreciated.
Just to complete it:
let geometry = SCNGeometry(sources: [vertexSource], elements: [element])
let node = SCNNode(geometry: geometry)
// to ignore orientation problem
node.geometry?.firstMaterial?.isDoubleSided = true
I solved this problem by porting app to iOS. There I realised that I need typealias for CGFloat vs. Float so I made SCNFloat.
Then when I replaced it everywhere including the parameter of SCNGeometrySource it seems to be solved.
bytesPerComponent: MemoryLayout<SCNFloat>.size,