我在使用平移手势识别器旋转模型时遇到了问题,有点迷茫。
每次我尝试添加手势时,它都无法找到节点并旋转它!
下面是我下载覆盖图像(漫反射内容)和.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.comwatch?v=J1SA3AZumeU
请告知
我在这里找到了一个很好的答案。
https:/stackoverflow.coma4947562610459255。
@objc func rotateNode(_ gesture: UIRotationGestureRecognizer){
//Store The Rotation Of The CurrentNode
var currentAngleY: Float = 0.0
//1. Get The Current Rotation From The Gesture
let rotation = Float(gesture.rotation)
//2. If The Gesture State Has Changed Set The Nodes EulerAngles.y
if gesture.state == .changed{
isRotating = true
node.eulerAngles.y = currentAngleY + rotation
}
//3. If The Gesture Has Ended Store The Last Angle Of The Cube
if(gesture.state == .ended) {
currentAngleY = node.eulerAngles.y
isRotating = false
}
}