Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

swift - Issue with adding node to scene view

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems that this issue entirely depends on a pivot point's position and a scale of your model.

Pivot point meets ARAnchor and helps you control a model's offset, orientation and scale of model on a floor, a table-top, or a wall.

node.scale = SCNVector3(x: 0.2, y: 0.2, z: 0.2)
node.pivot = SCNMatrix4MakeTranslation(0, -0.5, 0)

...or for positioning pivot use this approach:

node.simdPivot.columns.3.y = -0.5

So if you want your model to precisely stand on an invisible detected plane move a pivot point in SceneKit toward desired place or set its position in 3D authoring tool (like 3dsMax or Blender).

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...