SceneKit自定义几何体生成“double not supported”/“invalid vertex format”运行时错误

时间:2016-01-19 22:33:00

标签: swift scenekit

我不明白以下代码的错误:

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.'

1 个答案:

答案 0 :(得分:1)

问题是几何体需要float组件,但是你给它double s-CGPoint的组件是CGFloat值,它们在64-上被double类型定义比特系统。不幸的是,SCNGeometrySource …textureCoordinates:初始化程序坚持使用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))
相关问题