我想使用iOS SceneKit加载对象。
以及如何卸载已加载的对象并重新加载另一个对象?
我通过参考代码below成功加载了对象。
func sceneSetup() {
if let filePath = Bundle.main.path(forResource: "Smiley", ofType: "scn") {
let referenceURL = URL(fileURLWithPath: filePath)
self.contentNode = SCNReferenceNode(url: referenceURL)
self.contentNode?.load()
self.head.morpher?.unifiesNormals = true // ensures the normals are not morphed but are recomputed after morphing the vertex instead. Otherwise the node has a low poly look.
self.scene.rootNode.addChildNode(self.contentNode!)
}
self.faceView.autoenablesDefaultLighting = true
// set the scene to the view
self.faceView.scene = self.scene
// allows the user to manipulate the camera
self.faceView.allowsCameraControl = false
// configure the view
self.faceView.backgroundColor = .clear
}
但是我不知道如何加载和切换多个对象。
我将testScene.scn添加到项目中,并添加了以下代码,但是仅加载了第一个指定的对象。
var charaSelect = "Smiley"
//tapEvent(ViewDidLoad)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(FaceGeoViewController.tapped(_:)))
tapGesture.delegate = self
self.view.addGestureRecognizer(tapGesture)
//tap
@objc func tapped(_ sender: UITapGestureRecognizer)
{
self.charaSelect = "testScene"
}
func sceneSetup() {
if let filePath = Bundle.main.path(forResource: self.charaSelect, ofType: "scn") {
let referenceURL = URL(fileURLWithPath: filePath)
self.contentNode = SCNReferenceNode(url: referenceURL)
self.contentNode?.load()
self.head.morpher?.unifiesNormals = true // ensures the normals are not morphed but are recomputed after morphing the vertex instead. Otherwise the node has a low poly look.
self.scene.rootNode.addChildNode(self.contentNode!)
}
self.faceView.autoenablesDefaultLighting = true
// set the scene to the view
self.faceView.scene = self.scene
// allows the user to manipulate the camera
self.faceView.allowsCameraControl = false
// configure the view
self.faceView.backgroundColor = .clear
}
我该怎么办?
[我将在这里解释这个概念,但是如果您可能需要将这些东西看做一个完整的项目,欢迎参考我从Apple Education,2019年的一本书code中学到的“App Development with Swift”,特别是《指南》在第3A章末尾的项目。
下面您可以看到示例屏幕截图。在应用程序中,您可以通过触摸SceneView上的空白位置或触摸与其他对象(平面)碰撞来添加元素。此外,还有一种用于删除对象的逻辑
因此,基本上,能够从场景中删除节点的一种方法是使用特殊数组ViewController
在var placedNodes = [SCNNode]()
中跟踪它们。这样,您可以从所有节点清除视图(例如,通过创建Button Action“ Clear”)
您可能会从Apple开发人员那里获得的另一个不错的补充功能不是使用敲击手势识别器,而是通过覆盖touchesBegan / touchesMoved,这可以为您提供更多关于触摸手势的灵活性,特别是,您可以通过调用touch.location(in: sceneView)
在SceneView中获取其位置。
因此,touchesBegan/touchesMoved
允许您找到用户点击的位置。这可用于在SceneView上添加/删除对象
希望这会有所帮助!