【发布时间】:2026-01-29 18:15:02
【问题描述】:
我在使用平移手势识别器旋转我的模型时遇到了问题,有点迷茫。
每次我尝试添加手势时都无法找到节点并旋转它!
在这里我下载覆盖图像(漫反射内容)和 .SCN 文件
private func dowloadModel(){
let configuration = URLSessionConfiguration.default
let operationQueue = OperationQueue()
let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)
do {
if restaurauntName == ""{
throw MyError.FoundNil("Something hasnt loaded")
}
//overlay image
let url1 = URL(string: "https://URL_Where_PNG_File_Is_Stored/Burger.png")
let data1 = try? Data(contentsOf: url1!) //make sure your image in this url does exist
self.imagesOne = UIImage(data: data1!)
}
catch {
print("Unexpected error: \(error).")
self.showAlert()
}
//.SCN file location
let url = URL(string: "https://URL_Where_SCN_File_Is_Stored/Burger.scn")!
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
以下是我将模型添加到场景中的方法:
func addItem(hitTestResult : ARHitTestResult){
let documentDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
if let documentDirectory = documentDirectories.first{
let fileURL = documentDirectory.appendingPathComponent("Burger.scn")
do{
let scene = try SCNScene(url: fileURL, options: nil)
let node = scene.rootNode.childNode(withName: "Burger", recursively: true)!
let material = SCNMaterial()
material.diffuse.contents = imagesOne
material.diffuse.wrapT = SCNWrapMode.repeat
material.diffuse.wrapS = SCNWrapMode.repeat
material.isDoubleSided = true
node.geometry?.materials = [material]
let transform = hitTestResult.worldTransform
let thirdColumn = transform.columns.3
node.scale = SCNVector3(x: 0.008, y: 0.008, z: 0.008)
node.pivot = SCNMatrix4MakeTranslation(0, -0.5, 0)
node.position = SCNVector3(thirdColumn.x, thirdColumn.y, thirdColumn.z)
self.sceneView.scene.rootNode.addChildNode(node)
}
catch{
print(error)
}
}
}
这是成功添加模型到场景中的添加模型手势识别器:
@objc func tapped(sender:UITapGestureRecognizer){
let sceneView = sender.view as! ARSCNView
let tapLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
if !hitTest.isEmpty{
print("touched a horizontal surface")
self.addItem2(hitTestResult: hitTest.first!)
}
else{
print("no match")
}
}
总结一下我的问题,我可以成功地将模型添加到场景中,但是在使用平移手势识别器旋转模型时遇到问题。我该怎么做?
我希望能够像这样旋转它:https://www.youtube.com/watch?v=J1SA3AZumeU
请指教
【问题讨论】:
标签: ios swift augmented-reality scenekit arkit