swift - 这个 RealityKit 的东西是构思不好还是我遗漏了什么?

标签 swift augmented-reality arkit realitykit reality-composer

我正在尝试使用 Reality Kit 玩增强现实。
我想让我的程序执行以下操作之一, 可由用户 选择:

  • 仅检测水平表面。
  • 仅检测垂直表面。
  • 检测水平和垂直表面。
  • 检测图像,就像我打印目标一样,附加到现实世界中的对象,应用程序会检测到它。

  • 为了做到这一点,据我所知,我必须调整 3 件事:
    ARWorldTracking配置
    做类似的事情
    func initSession() {
    
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = .vertical
      
        arView.session.delegate = self
        arView.session.run(config)
    }
    
    在 Experience.rcproject 中创建场景
    一种用于我需要的 anchor 定类型。我创建了三个具有以下 anchor 类型的“场景”: horizontalverticalimage
    创建一个 ARCoachingOverlayView
    指示用户使检测正常工作。
    这些是问题:
  • ARWorldTrackingConfiguration planeDetection 只有两个选项: horizontalvertical
  • Experience.rcproject 里面的场景,只能有 3 种: horizontalverticalimage
  • ARCoachingOverlayView.goal 的选项是: tracking (如果没有适当的文档很难弄清楚)、 horizontalPlaneverticalPlaneanyPlane

  • 问题:
  • 如何配置 ARWorldTrackingConfigurationARCoachingOverlayView.goal 以使应用程序仅检测 horizontal、仅 verticalhorizontal and verticalimages(如果它们没有所有这四个选项)?
  • 我在 Experience.rcproject 中有 3 个场景,一个用于 horizontal ,一个用于 vertical ,另一个用于 image 检测。这是怎么做的?
  • 最佳答案

    假设我们已经在 Reality Composer 中创建了三个场景,分别称为 BoxScene 用于水平平面检测(世界跟踪), StarScene 用于垂直平面检测(世界跟踪)和 PrismScene 用于图像检测(图像跟踪)。在每个场景中,我们为模型命名——这些名称会自动生成变量—— goldenBoxplasticStarpaintedPrism
    要在 RealityKit 中从 World Tracking 配置切换到 Image Tracking 配置,我们必须使用在按钮的 @IBActions 中编写的明确的 AnchorEntity 初始值设定项 – .image.plane
    查看以下代码以了解如何执行您想要的操作。

    import RealityKit
    import UIKit
    
    class ViewController: UIViewController {
    
        @IBOutlet var arView: ARView!
        
        let cubeScene = try! Experience.loadBoxScene()
        let starScene = try! Experience.loadStarScene()
        let prismScene = try! Experience.loadPrismScene()
    
    
    
        // IMAGE TRACKING
        @IBAction func image(_ button: UIButton) {
            
            arView.scene.anchors.removeAll()
            
            let anchor = AnchorEntity(.image(group: "AR Resources", 
                                              name: "image"))
            
            let prism = prismScene.paintedPrism!
            anchor.addChild(prism)
            arView.scene.anchors.append(anchor)
        }
    
        
        // WORLD TRACKING
        @IBAction func verticalAndHorizontal(_ button: UIButton) {
            
            arView.scene.anchors.removeAll()
            
            let trackingAnchor = AnchorEntity(.plane([.vertical, .horizontal],
                                      classification: .any,
                                       minimumBounds: [0.1, 0.1]))
             
            let cube = cubeScene.goldenBox!
            let star = starScene.plasticStar!
            
            if trackingAnchor.anchor?.anchoring.target == .some(.plane([.vertical, 
                                                                        .horizontal], 
                                                        classification: .any, 
                                                         minimumBounds: [0.1, 0.1])) {
                
                let anchor1 = AnchorEntity(.plane(.horizontal,
                                  classification: .any,
                                   minimumBounds: [0.1, 0.1]))
                
                anchor1.addChild(cube)
                arView.scene.anchors.append(anchor1)
            }
            
            if trackingAnchor.anchor?.anchoring.target == .some(.plane([.vertical, 
                                                                        .horizontal], 
                                                        classification: .any, 
                                                         minimumBounds: [0.1, 0.1])) {
                
                let anchor2 = AnchorEntity(.plane(.vertical,
                                  classification: .any,
                                   minimumBounds: [0.1, 0.1]))
    
    
                anchor2.addChild(star)
                arView.scene.anchors.append(anchor2)
            }           
        }
    }
    
    P. S.
    目前我没有电脑,我是在 iPhone 上写的。所以不知道这段代码有没有错误...

    关于swift - 这个 RealityKit 的东西是构思不好还是我遗漏了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63266116/

    相关文章:

    来自 ARKit 深度缓冲区的 SceneKit 点云

    ios - swift : ARKit Save ARPlaneAnchor for next session

    ios - 缩小 UINavigationBar 大标题文本的大小

    ios - 在 xcode 模拟器中构建时禁用 RealityKit/ARKit

    swift - 在射击游戏中自动射击(Swift 4 - SpriteKit)

    ios - 是否可以使用 ARCore 在 iOS 上实现面部增强功能?

    ios - 为 iOS 增强现实应用程序开发自定义过滤器

    ios - 是否可以创建一个带有原始顶点而不是混合形状的 ARFaceGeometry 实例,就像 ARSession 在使用 ARFaceTrackingConfiguration 运行时所做的那样?

    iOS Swift - 没有 Firebase 等模块

    swift - Swift 访问 URL 路径时删除部分字符串