我正在尝试使用 Reality Kit 玩增强现实。
我想让我的程序执行以下操作之一, 可由用户 选择:
为了做到这一点,据我所知,我必须调整 3 件事:
ARWorldTracking配置
做类似的事情
func initSession() {
let config = ARWorldTrackingConfiguration()
config.planeDetection = .vertical
arView.session.delegate = self
arView.session.run(config)
}
在 Experience.rcproject 中创建场景一种用于我需要的 anchor 定类型。我创建了三个具有以下 anchor 类型的“场景”:
horizontal
、 vertical
和 image
。创建一个 ARCoachingOverlayView
指示用户使检测正常工作。
这些是问题:
ARWorldTrackingConfiguration
planeDetection
只有两个选项: horizontal
或 vertical
。Experience.rcproject
里面的场景,只能有 3 种: horizontal
、 vertical
或 image
。ARCoachingOverlayView.goal
的选项是: tracking
(如果没有适当的文档很难弄清楚)、 horizontalPlane
、 verticalPlane
和 anyPlane
。问题:
ARWorldTrackingConfiguration
和 ARCoachingOverlayView.goal
以使应用程序仅检测 horizontal
、仅 vertical
、horizontal and vertical
和 images
(如果它们没有所有这四个选项)?Experience.rcproject
中有 3 个场景,一个用于 horizontal
,一个用于 vertical
,另一个用于 image
检测。这是怎么做的?最佳答案
假设我们已经在 Reality Composer 中创建了三个场景,分别称为 BoxScene
用于水平平面检测(世界跟踪), StarScene
用于垂直平面检测(世界跟踪)和 PrismScene
用于图像检测(图像跟踪)。在每个场景中,我们为模型命名——这些名称会自动生成变量—— goldenBox
、 plasticStar
和 paintedPrism
。
要在 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/