gpt4 book ai didi

ios - 在 SceneKit 上画一条线在设备上不起作用

转载 作者:行者123 更新时间:2023-11-28 21:19:16 24 4
gpt4 key购买 nike

遵循此解决方案:Custom SceneKit Geometry并转换为 Swift 3,代码变为:

func drawLine() {

var verts = [SCNVector3(x: 0,y: 0,z: 0),SCNVector3(x: 1,y: 0,z: 0),SCNVector3(x: 0,y: 1,z: 0)]

let src = SCNGeometrySource(vertices: &verts, count: 3)
let indexes: [CInt] = [0, 1, 2]

let dat = NSData(
bytes: indexes,
length: MemoryLayout<CInt>.size * indexes.count
)
let ele = SCNGeometryElement(
data: dat as Data,
primitiveType: .line,
primitiveCount: 2,
bytesPerIndex: MemoryLayout<CInt>.size
)
let geo = SCNGeometry(sources: [src], elements: [ele])

let nd = SCNNode(geometry: geo)

geo.materials.first?.lightingModel = .blinn
geo.materials.first?.diffuse.contents = UIColor.red
scene.rootNode.addChildNode(nd)

}

它在模拟器上工作:

red line on simulator

但我在设备上遇到错误:

/BuildRoot/Library/Caches/com.apple.xbs/Sources/Metal/Metal-85.83/ToolsLayers/Debug/MTLDebugRenderCommandEncoder.mm:130: failed assertion `indexBufferOffset(0) + (indexCount(4) * 4) must be <= [indexBuffer length](12).'

发生了什么事?

完整代码在这里:Source code

最佳答案

我正在回答我自己的问题,因为我找到了可以帮助他人的解决方案。

问题出在“索引”上,3 个索引不会绘制 2 个顶点。必须为每个要绘制的顶点设置 2 个索引。

这是最终的功能:

func drawLine(_ verts : [SCNVector3], color : UIColor) -> SCNNode? {

if verts.count < 2 { return nil }

let src = SCNGeometrySource(vertices: verts, count: verts.count )
var indexes: [CInt] = []

for i in 0...verts.count - 1 {
indexes.append(contentsOf: [CInt(i), CInt(i + 1)])
}

let dat = NSData(
bytes: indexes,
length: MemoryLayout<CInt>.size * indexes.count
)

let ele = SCNGeometryElement(
data: dat as Data,
primitiveType: .line,
primitiveCount: verts.count - 1,
bytesPerIndex: MemoryLayout<CInt>.size
)

let line = SCNGeometry(sources: [src], elements: [ele])

let node = SCNNode(geometry: line)

line.materials.first?.lightingModel = .blinn
line.materials.first?.diffuse.contents = color

return node
}

调用:

scene.rootNode.addChildNode(
drawLine(
[SCNVector3(x: -1,y: 0,z: 0),
SCNVector3(x: 1,y: 0.5,z: 1),
SCNVector3(x: 0,y: 1.5,z: 0)] , color: UIColor.red
)!
)

将绘制: enter image description here

关于ios - 在 SceneKit 上画一条线在设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40371370/

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