首页 > 解决方案 > ARFrame 锚点中不存在 AREnvironmentProbeAnchor

问题描述

我无法弄清楚AREnvironmentProbeAnchor模式是如何工作的automatic。我假设 ARKit 为我创建了一系列带有纹理的锚点,但我看不到它们。

我添加到配置中:

session.configuration.environmentTexturing = AREnvironmentTexturingAutomatic;

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

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

有什么问题?

谢谢!

标签: objective-cswiftaugmented-realityscenekitarkit

解决方案


例如,您应该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

希望这可以帮助。


推荐阅读