ARFrame定位标记中不存在AREnvironmentProbeAnchor

时间:2019-04-05 17:16:18

标签: ios objective-c swift augmented-reality arkit

当模式为AREnvironmentProbeAnchor时,我不知道automatic的工作方式。我假设ARKit为我创建了具有纹理的一系列锚点,但是我看不到它们。

我已添加到配置:

session.configuration.environmentTexturing = AREnvironmentTexturingAutomatic;

现在我正在循环遍历框架中的所有锚点并寻找AREnvironmentProbeAnchor

for (ARAnchor* anchor in frame.anchors) {
    if ([anchor isKindOfClass:[AREnvironmentProbeAnchor class]]) {
        NSLog(@"found");
    }
}

怎么了?

谢谢!

1 个答案:

答案 0 :(得分:1)

例如,您应该在实例方法if-statement中使用renderer(_:didAdd:for:)。任何ARAnchor必须属于SCNNode。尝试下面的代码以了解其工作原理(抱歉,它在Swift中不在Objective-C中):

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.delegate = self
        let scene = SCNScene(named: "art.scnassets/yourScene.scn")!

        let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.05))
        sphereNode.position = SCNVector3(x: 0, y: -0.5, z: -1)
        let reflectiveMaterial = SCNMaterial()
        reflectiveMaterial.lightingModel = .physicallyBased
        reflectiveMaterial.metalness.contents = 1.0
        reflectiveMaterial.roughness.contents = 0
        sphereNode.geometry?.firstMaterial = reflectiveMaterial

        let moveLeft = SCNAction.moveBy(x: 0.25, y: 0, z: 0.25, duration: 2)
        moveLeft.timingMode = .easeInEaseOut;
        let moveRight = SCNAction.moveBy(x: -0.25, y: 0, z: -0.25, duration: 2)
        moveRight.timingMode = .easeInEaseOut;
        let moveSequence = SCNAction.sequence([moveLeft, moveRight])
        let moveLoop = SCNAction.repeatForever(moveSequence)
        sphereNode.runAction(moveLoop)

        sceneView.scene = scene
        sceneView.scene.rootNode.addChildNode(sphereNode)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let configuration = ARWorldTrackingConfiguration()
        configuration.environmentTexturing = .automatic
        sceneView.session.run(configuration)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        sceneView.session.pause()
    }
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard anchor is AREnvironmentProbeAnchor else {
            print("Environment anchor is NOT FOUND")
            return
        }
        print("It's", anchor.isKind(of: AREnvironmentProbeAnchor.self))
    }
}
  

结果:

// It's true

希望这会有所帮助。