ios - RealityKit 中的第一人称体验
问题描述
我对 Swift 还很陌生,刚开始使用 RealityKit 和 ARKit。我正在做一个个人项目,我想以第一人称将 3D 对象粘在相机上。类似于 AR 愤怒的小鸟或任何 FPS 游戏。我在 SceneKit 或 SpriteKit 中看到了一些示例,我确信这只是对锚定实体如何工作的误解。
我的主要问题是:
- 我将如何以第一人称将我在 Reality Composer 中创建的现实对象粘贴到相机上?我想创建一个现实场景,在这种情况下是一门手臂大炮,然后点击它进行射击。
下面是我的 ViewController 的代码
extension ViewController: ARSessionDelegate
{
func session(_ session: ARSession, didUpdate frame: ARFrame)
{
guard let arCamera = session.currentFrame?.camera else { return }
// Probably where I update the location of my reality experience
}
}
class ViewController: UIViewController
{
@IBOutlet var arView: ARView!
override func viewDidLoad()
{
super.viewDidLoad()
arView.session.delegate = self
// Load the "ArmCannon" scene from the "Experience" Reality File
let armCannonAnim = try! Experience.loadArmcannon()
// Create Anchor to anchor arm cannon to
let anchor = AnchorEntity(.camera)
anchor.transform = arView.cameraTransform
// Add the anchor to the scene
arView.scene.addAnchor(anchor)
// Setup tap gesture on arm cannon
let tapGesture = UITapGestureRecognizer(target: self, action:#selector(onTap))
arView.addGestureRecognizer(tapGesture)
// Add the the cannon animation to arView
arView.scene.anchors.append(armCannonAnim)
}
@IBAction func onTap(_ sender: UITapGestureRecognizer)
{
let tapLocation = sender.location(in: arView)
// Get the entity at the location we've tapped, if one exists
if let cannonFiring = arView.entity(at: tapLocation)
{
print(cannonFiring.name)
print("firing Cannon")
}
}
}
解决方案
代替:
arView.scene.anchors.append(armCannonAnim)
放:
anchor.addChild(armCannonAnim)
您需要将此 armCannonAnim 作为相机的子对象,并且该anchor
对象是相机变换处的锚点。这相当于在 SceneKit 中向 cameraNode 添加一个子节点。
推荐阅读
- amazon-web-services - 标记s3上spark读取的对象
- android - 参与者失去连接后,Twilio Video Android SDK 断开连接
- javascript - 将带有 ajax 的数组发送到我在 django 中的 python 函数
- string - 在Matlab中将单元格数组的部分乘以因子
- php - 每当我在 $_POST 上使用 php 变量作为索引时,即使索引存在,isset() 也不会返回 true
- linux - 如何在 Linux 中运行 Visual Studio 代码
- java - 如何在 Android 中使用 CheckBox 设置字符串
- google-cloud-platform - Stackdriver Trace 与 Google Cloud Run
- python - 熊猫:如何结合 nunique 和 sum
- db2 - 无法连接到 db2 中的远程数据库