SceneKit – Custom geometry does not show up

后端 未结 2 976
傲寒
傲寒 2020-12-19 04:17

I should see 2 yellow triangles, but I see nothing.

class Terrain {

    private class func createGeometry () -> SCNGeometry {

        let sources = [
           


        
2条回答
  •  时光取名叫无心
    2020-12-19 05:11

    Note: see Ash's answer, which is a much better approach for modern Swift than this one.

    Your index array has the wrong size element. It's being inferred as [Int]. You need [CInt].

    I broke out your elements setup into:

        let indices = [0, 2, 3, 0, 1, 2] // [Int]
        print(sizeof(Int))               // 8
        print(sizeof(CInt))              // 4
        let elements = [
            SCNGeometryElement(indices: indices, primitiveType: .Triangles)
        ]
    

    To get the indices to be packed like the expected C array, declare the type explicitly:

        let indices: [CInt] = [0, 2, 3, 0, 1, 2]
    

    Custom SceneKit Geometry in Swift on iOS not working but equivalent Objective C code does goes into more detail, but it's written against Swift 1, so you'll have to do some translation.

    SCNGeometryElement(indices:, primitiveType:) doesn't appear to be documented anywhere, although it does appear in the headers.

提交回复
热议问题