gpt4 book ai didi

ios - 设置 SCNNode 几何属性

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

我有一个 SCNNode,我正在使用此方法将 .dae 文件中的几何图形应用到它:

func addNode(gridPosition: SCNVector3, row: Int, gamePlaneHeight: Float, sceneArray: [ObjectInfo]) -> SCNNode {

// Create a material using the model_texture.tga image
let carMaterial = SCNMaterial()
carMaterial.diffuse.contents = UIImage(named: "assets.scnassets/Textures/model_texture.tga")
carMaterial.locksAmbientWithDiffuse = false

// Create a clone of the Car node of the carScene - you need a clone because you need to add many cars
var carNode: SCNNode!
let randomNumb = AppDelegate().randRange(0, upper: sceneArray.count - 1)
let selectedObject = sceneArray[randomNumb]
carNode = selectedObject.objectscene.rootNode.childNodeWithName(selectedObject.objectname, recursively: false)!.clone() as SCNNode

carNode.scale = SCNVector3(0.5, 0.5, AppDelegate().randomBetweenNumbers(0.8, secondNum: 2.2))
carNode.name = selectedObject.objectname
carNode.position = gridPosition
carNode.position.y = carNode.position.y + gamePlaneHeight

// Set the material
carNode.geometry?.firstMaterial = carMaterial

return carNode
}

该方法大部分时间有效,但偶尔会在线上崩溃

carNode.geometry?.firstMaterial = carMaterial

错误

fatal error: unexpectedly found nil while unwrapping an Optional value
warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

我不明白为什么,因为大多数时候它都有效。

最佳答案

carNode 是从 selectedObject.objectname 构建的。如果场景中不存在 selectedObject.objectname,则 childNodeWithName 将返回 nil。因为 carNode 是一个隐式展开的可选项,Swift 将尝试展开 nil 并崩溃。

您应该使 carNode 可选(var carNode: SCNNode?)或添加测试,例如 if carNode == nil { ... } 并尝试调试您的输入 (selectedObject) 是什么以及它为什么不正确。

关于ios - 设置 SCNNode 几何属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36073353/

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