mick1996
mick1996

Reputation: 606

Track percentage download of .SCN file

I am new to ARKIT and I was just wondering is there a way to track the percentage progress of the download of the .SCN file. I have tried using URL session but I couldn't get it to work.

Although my code is working well, if my code is completely off please show me how to make it better!

 private func dowloadModel(){
        let url = URL(string: "URL for .SCN")!

        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)

                    let url1 = URL(string: "URL for .jpg overlay")
                    let data1 = try? Data(contentsOf: url1!) //make sure your image in this url does exist
                    self.imagesOne = UIImage(data: data1!)

                    print("Saved!")
                }
            }

        }.resume()
    }

This is how I render the node then:

 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 = imagesOne
                material.diffuse.wrapT = SCNWrapMode.repeat
                material.diffuse.wrapS = SCNWrapMode.repeat
                material.isDoubleSided = true

                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)

                //rotation added
                let action = SCNAction.rotateBy(x: 0, y: CGFloat(2 * Double.pi), z: 0, duration: 15)
                let repAction = SCNAction.repeatForever(action)
                node.runAction(repAction, forKey: "myrotate")

                self.sceneView.scene.rootNode.addChildNode(node)
            }
            catch{
                print(error)
            }

        }

    }

Upvotes: 0

Views: 126

Answers (1)

the.alch3m1st
the.alch3m1st

Reputation: 56

From: https://developer.apple.com/documentation/foundation/urlsession/1411554-datatask

URLSession.shared.dataTask(with:)

returns a URLSessionTask object wihich has a .progress property

https://developer.apple.com/documentation/foundation/urlsessiontask/2908821-progress

Upvotes: 1

Related Questions