I am having an issue with the way the model is being placed on the scene view.
The problem is that the model downloads successfully but for some reason it appears on top of the scene view.
here is a video of the problem:
https://www.youtube.com/watch?v=-pTPEZTF9zo
view did load:
override func viewDidLoad() {
super.viewDidLoad()
self.dowloadModel()
self.sceneView.debugOptions = [ARSCNDebugOptions.showWorldOrigin, ARSCNDebugOptions.showFeaturePoints]
self.configuration.planeDetection = .horizontal
self.sceneView.session.run(configuration)
self.registerGestureRecogniser()
}
Gesture recofniser:
func registerGestureRecogniser(){
let tapGestureRecogiser = UITapGestureRecognizer(target: self, action: #selector(tapped))
self.sceneView.addGestureRecognizer(tapGestureRecogiser)
}
Tapped function:
@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")
}
}
Download model from AWS:
private func dowloadModel(){
let url = URL(string: "https://ENTER URL HERE")!
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error{
print(error.localizedDescription)
return
}
if let data = data{
print(data)
let documentDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
if let documentDirectory = documentDirectories.first{
let fileURL = documentDirectory.appendingPathComponent("Food.scn")
let dataNS : NSData? = data as NSData
try! dataNS?.write(to: fileURL, options: .atomic)
print("Saved!")
}
}
}.resume()
}
Add Item 2 function:
func addItem2(hitTestResult : ARHitTestResult){
let documentDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
if let documentDirectory = documentDirectories.first{
let fileURL = documentDirectory.appendingPathComponent("Food.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 = UIImage(named: "Hamburger_BaseColor")
material.diffuse.wrapT = SCNWrapMode.repeat
material.diffuse.wrapS = SCNWrapMode.repeat
material.isDoubleSided = true
let transform = hitTestResult.worldTransform
let thirdColumn = transform.columns.3
node.position = SCNVector3(thirdColumn.x, thirdColumn.y, thirdColumn.z)
self.sceneView.scene.rootNode.addChildNode(node)
}
catch{
print(error)
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…