How do I use the non-convenience SCNGeometrySource initializer correctly?

Viewed 55

being new to Swift and SceneKit (not new to programming) and trying to get my first triangle displayed (in preparation of rendering a more complex procedurally generated geometry) I just stumbled over the following issue:

When I initialise the SCNGeometrySource as shown in the code below the triangle is in the wrong position. When using the shorter, commented out version to create vertices it works as expected. My understanding from the documentation and a couple of examples I found online was that these two should be equivalent.

Which embarrassing detail am I missing? Thanks!

      assert( MemoryLayout< SCNVector3 >.size == 24 )
      assert( MemoryLayout< SCNVector3 >.stride == 24 )
      let varray = [ SCNVector3( -3.0, -3.0, 0.0 ),
                     SCNVector3( 3.0, 3.0, 0.0 ),
                     SCNVector3( -3.0, 3.0, 0.0 ) ]
      let vdata = Data( bytes: varray, count: varray.count * 24 )
      print( "size: \(vdata.count), data: \(hexDump( vdata ) )" )  // size: 72, data: 00000000000008c000000000000008c0000000000000000000000000000008400000000000000840000000000000000000000000000008c000000000000008400000000000000000
      let vertices = SCNGeometrySource(data: vdata,
                                       semantic: .vertex,
                                       vectorCount: varray.count,
                                       usesFloatComponents: true,
                                       componentsPerVector: 3,
                                       bytesPerComponent: 8,
                                       dataOffset: 0,
                                       dataStride: 24 )
      // let vertices = SCNGeometrySource(vertices: varray)  // This works, the line above not

Edit: It works correctly when the input data consists of (vectors of) 32-bit floats (and all the parameters are adjusted accordingly).

0 Answers
Related