在ARkit中永久显示3D对象

时间:2019-01-26 15:46:57

标签: swift arkit

我正在尝试识别显示3D模型的对象, 当我将相机放在显示的3D模型的对象上时,它就可以工作 我的问题是3D模型仅显示5秒钟左右,然后消失了 那么如何永久显示我的3D模型?

这是我的代码部分:

class ViewController: UIViewController, UITextFieldDelegate 
{
  var proximityObserver: ProximityObserver! // Beacon declaration  
  @IBOutlet weak var sceneView: ARSCNView!
  //@IBOutlet var sceneView: ARSCNView!
  let fadeDuration: TimeInterval = 0.3
  let rotateDuration: TimeInterval = 3
  let waitDuration: TimeInterval = 0.5

  lazy var fadeAndSpinAction: SCNAction = {
    return .sequence([
        .fadeIn(duration: fadeDuration),
        .rotateBy(x: 0, y: 0, z: CGFloat.pi * 360 / 180, duration: rotateDuration),
        .wait(duration: waitDuration),
        .fadeOut(duration: fadeDuration)
        ])
}()

lazy var fadeAction: SCNAction = {
    return .sequence([
        .fadeOpacity(by: 0.8, duration: fadeDuration),
        .wait(duration: waitDuration),
        .fadeOut(duration: fadeDuration)
        ])
}()


lazy var bookNode: SCNNode = {
    guard let scene = SCNScene(named: "art.scnassets/book.scn"),
        let node = scene.rootNode.childNode(withName: "book", recursively: true) else { return SCNNode() }
    let scaleFactor  = 0.1
    node.scale = SCNVector3(scaleFactor, scaleFactor, scaleFactor)
    return node
}()


override func viewDidLoad() {
    super.viewDidLoad()


    let estimoteCloudCredentials = CloudCredentials(appID: "reem-badr-s-proximity-for--6o4", appToken: "8be2dff5dc16b9747b7fafe97ff53708")

    proximityObserver = ProximityObserver(credentials: estimoteCloudCredentials, onError: { error in
        print("ProximityObserver error: \(error)")
    })

    let zone = ProximityZone(tag: "reem-badr-s-proximity-for--6o4", range: ProximityRange.near)
    zone.onEnter = { contexts in
        print("enter")
        self.sceneView.delegate = self as ARSCNViewDelegate
        self.configureLighting()
    }
    zone.onExit = 
    { contexts in
        print("Exit")
    }
    proximityObserver.startObserving([zone])
}

func configureLighting() {
    sceneView.autoenablesDefaultLighting = true
    sceneView.automaticallyUpdatesLighting = true
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)        
    resetTrackingConfiguration()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    sceneView.session.pause()
}

@IBAction func resetButtonDidTouch(_ sender: UIBarButtonItem) {
    resetTrackingConfiguration()
}

  func resetTrackingConfiguration() 
  {
    guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return }
    let configuration = ARWorldTrackingConfiguration()
    configuration.detectionImages = referenceImages
    sceneView.session.run(configuration)
  }
}

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) 
{
  DispatchQueue.main.async {
        guard let imageAnchor = anchor as? ARImageAnchor,
            let imageName = imageAnchor.referenceImage.name else { return }

        let overlayNode = self.getNode(withImageName: imageName)
        overlayNode.opacity = 0
        overlayNode.position.y = 0.2
        overlayNode.runAction(self.fadeAndSpinAction)
        node.addChildNode(overlayNode)
    }

func getPlaneNode(withReferenceImage image: ARReferenceImage) -> SCNNode {
  let plane = SCNPlane(width: image.physicalSize.width,
                       height: image.physicalSize.height)
    let node = SCNNode(geometry: plane)
    return node
}

  func getNode(withImageName name: String) -> SCNNode 
  {
    var node = SCNNode()
    switch name 
    {
      case "Book":
       node = bookNode
      default:
        break
    }
    return node
  }
}

以此类推...

0 个答案:

没有答案