gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:18:03 24 4
gpt4 key购买 nike

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

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 组件,但你给它 double——CGPoint 的组件是 CGFloat 值,它被类型定义为 double 在 64 位系统上。不幸的是,SCNGeometrySource …textureCoordinates: 初始化程序坚持使用 CGPoints,因此您不能使用它;我找到的解决方法是创建一个 NSData 包装 SIMD float vectors 的数组, 然后使用更长的时间 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com