我不明白以下代码有什么问题:

class Terrain {

    private class func createGeometry () -> SCNGeometry {

        let sources = [
            SCNGeometrySource(vertices:[
                SCNVector3(x: -1.0, y: -1.0, z:  0.0),
                SCNVector3(x: -1.0, y:  1.0, z:  0.0),
                SCNVector3(x:  1.0, y:  1.0, z:  0.0),
                SCNVector3(x:  1.0, y: -1.0, z:  0.0)], count:4),
            SCNGeometrySource(normals:[
                SCNVector3(x:  0.0, y:  0.0, z: -1.0),
                SCNVector3(x:  0.0, y:  0.0, z: -1.0),
                SCNVector3(x:  0.0, y:  0.0, z: -1.0),
                SCNVector3(x:  0.0, y:  0.0, z: -1.0)], count:4),
            SCNGeometrySource(textureCoordinates:[
                CGPoint(x: 0.0, y: 0.0),
                CGPoint(x: 0.0, y: 1.0),
                CGPoint(x: 1.0, y: 1.0),
                CGPoint(x: 1.0, y: 0.0)], count:4)
        ]

        let elements = [
            SCNGeometryElement(indices: [0, 2, 3, 0, 1, 2], primitiveType: .Triangles)
        ]

        let geo = SCNGeometry(sources:sources, elements:elements)

        let mat = SCNMaterial()
        mat.diffuse.contents = UIColor.redColor()
        mat.doubleSided = true
        geo.materials = [mat, mat]


        return geo
    }

    class func createNode () -> SCNNode {

        let node = SCNNode(geometry: createGeometry())
        node.name = "Terrain"
        node.position = SCNVector3()
        return node
    }
}

我使用它如下:
   let terrain = Terrain.createNode()
   sceneView.scene?.rootNode.addChildNode(terrain)

但是得到:
2016-01-19 22:21:17.600 SceneKit: error, C3DRendererContextSetupResidentMeshSourceAtLocation - double not supported
2016-01-19 22:21:17.601 SceneKit: error, C3DSourceAccessorToVertexFormat - invalid vertex format
/BuildRoot/Library/Caches/com.apple.xbs/Sources/Metal/Metal-55.2.6.1/Framework/MTLVertexDescriptor.mm:761: failed assertion `Unused buffer at index 18.'

最佳答案

问题是,几何体期望的是float组件,但您给它的是doubles-cgpoint的组件是cgfloat值,在64位系统上,cgfloat值被typedef定义为double。不幸的是,scngemetrysource…textureCoordinates:初始值设定项坚持使用cgpoints,因此您不能使用cgpoints;我发现的解决方法是创建一个包含SIMD float vectors数组的nsdata,然后使用更长的data:semantic:etc:初始值设定项来使用数据。像这样的方法应该可以做到:

let coordinates = [float2(0, 0), float2(0, 1), float2(1, 1), float2(1, 0)]
let coordinateData = NSData(bytes:coordinates, length:4 * sizeof(float2))
let coordinateSource = SCNGeometrySource(data: coordinateData,
                                     semantic: SCNGeometrySourceSemanticTexcoord,
                                  vectorCount: 4,
                              floatComponents: true,
                          componentsPerVector: 2,
                            bytesPerComponent: sizeof(Float),
                                   dataOffset: 0,
                                   dataStride: sizeof(float2))

关于swift - SceneKit自定义几何体生成“double not supported”/“invalid vertex format”运行时错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34888231/

10-08 22:43