Custom SCNGeometrySource does not produce correct geometry

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
Answered by cap7ain in 703652022

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,

just ignore this comment of mine for now

I have exactly the same problem. using SCNGeometrySource(vertices: vertices) works using SCNGeometrySource(data: data,....fails Any suggestion welcome...

I checked it more deeply and it seems that it also affect SCNGeometrySource for .color semantic

Accepted Answer

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,

Custom SCNGeometrySource does not produce correct geometry
 
 
Q