问:如何添加多个对象的AR检测

时间:2018-12-05 11:29:56

标签: swift xcode arkit

我在我的应用程序中添加了一个ar函数来检测我们的产品。一个对象正在工作,但是我想添加多个arobject文件。我扫描了一些对象并将其添加到我的资源组中。 我创建了一个product.sks并添加了标签+背景。 enter image description here 我的第一个问题:如何将标签固定到一个物体上?我目前有两个对象,所以我需要2个不同的标签。我该如何区分正确的标签到对象?

我的第二个问题:目前,我的HU标签是固定的,并且在移动iPhone时不会移动。我试图改变一些位置等,但它总是固定的。 我该如何移动?

先谢谢了。我希望我的担忧足够详细

import UIKit
import SceneKit
import ARKit

class ARViewController: UIViewController, ARSCNViewDelegate {


@IBOutlet var sceneView: ARSCNView!

override func viewDidLoad() {
    super.viewDidLoad()


    sceneView.delegate = self


    sceneView.showsStatistics = true


    let scene = SCNScene(named: "art.scnassets/scene.scn")!


    sceneView.scene = scene
}

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


    let configuration = ARWorldTrackingConfiguration()

    configuration.detectionObjects = ARReferenceObject.referenceObjects(inGroupNamed: "Module", bundle: Bundle.main)!


    sceneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)


    sceneView.session.pause()
}


 func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
 let node = SCNNode()

 return node
 }


func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
    let node = SCNNode()

    if let objectAnchor = anchor as? ARObjectAnchor {
        let plane = SCNPlane(width: CGFloat(objectAnchor.referenceObject.extent.x * 0.8), height: CGFloat(objectAnchor.referenceObject.extent.y * 0.5))
        plane.cornerRadius = plane.width * 0.125

        let displayScene = SKScene(fileNamed: "product")

        plane.firstMaterial?.diffuse.contents = displayScene
        plane.firstMaterial?.isDoubleSided = true
        plane.firstMaterial?.diffuse.contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0)

        let planeNode = SCNNode(geometry: plane)
        planeNode.position = SCNVector3Make(objectAnchor.referenceObject.center.x, objectAnchor.referenceObject.center.y + 0.12, objectAnchor.referenceObject.center.z)

        node.addChildNode(planeNode)

    }
    return node
}

func session(_ session: ARSession, didFailWithError error: Error) {
    // Present an error message to the user

}

func sessionWasInterrupted(_ session: ARSession) {
    // Inform the user that the session has been interrupted, for example, by presenting an overlay

}

func sessionInterruptionEnded(_ session: ARSession) {
    // Reset tracking and/or remove existing anchors if consistent tracking is required

}

}

1 个答案:

答案 0 :(得分:0)

  1. 使用anchor.referenceObject.name吗?
  2. 如何移动?如果您希望它始终面向手机,请应用广告牌约束。