Can I get 3d models from web servers on Swift?

半腔热情 提交于 2019-12-04 16:04:12

you can load an scn file from a webserver with ip addresses like this (i used a fake ip below)

let myURL = NSURL(string: “http://110.151.153.202:80/scnfiles/myfile.scn”)

let scene = try! SCNScene(url: myURL! as URL, options:nil)

Edit:

Here’s a simple Swift PlayGrounds which pulls a test cube scn file from my github repo. You just tap anywhere and the cube loads.

import ARKit
import SceneKit
import PlaygroundSupport

class ViewController: NSObject {

      var sceneView: ARSCNView
      init(sceneView: ARSCNView) {
      self.sceneView = sceneView

      super.init()

      self.setupWorldTracking()
      self.sceneView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:))))

}

private func setupWorldTracking() {
    if ARWorldTrackingConfiguration.isSupported {
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = .horizontal
        configuration.isLightEstimationEnabled = true
        self.sceneView.session.run(configuration, options: [])
    }
}

@objc func handleTap(_ gesture: UITapGestureRecognizer) {
    let results = self.sceneView.hitTest(gesture.location(in: gesture.view), types: ARHitTestResult.ResultType.featurePoint)
    guard let result: ARHitTestResult = results.first else {
        return
    }

    // pulls cube.scn from github repo

    let myURL = NSURL(string: "https://raw.githubusercontent.com/wave-electron/scnFile/master/cube.scn")
    let scene = try! SCNScene(url: myURL! as URL, options: nil)
    let node = scene.rootNode.childNode(withName: "SketchUp", recursively: true)
    node?.scale = SCNVector3(0.01,0.01,0.01)

    let position = SCNVector3Make(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
    node?.position = position
    self.sceneView.scene.rootNode.addChildNode(node!)

 }
}

let sceneView = ARSCNView()

let viewController = ViewController(sceneView: sceneView)
sceneView.autoenablesDefaultLighting = true
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = viewController.sceneView 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!