【问题标题】:How to make RealityKit to show only CollisionComponents?如何让 RealityKit 只显示 CollisionComponents?
【发布时间】:2020-08-05 20:58:13
【问题描述】:

我正在尝试在我的 ARView 上查看 CollisionComponents。

我使用.showPhysics 作为 debugOptions 的一部分,但由于屏幕上有 20 个对象,我让所有法线都变得疯狂,并且 CollisionComponents 的颜色不清楚(某种奇怪的粉红色)。

有谁知道如何只显示 CollisionComponents 而没有任何额外数据作为.showPhysics 的一部分?

【问题讨论】:

  • P.S:我是 RealityKit 新手,所以请温柔:)

标签: swift augmented-reality arkit realitykit


【解决方案1】:

您可以使用简单的 Swift 扩展来扩展 RealityKit 的 ARView 的标准功能:

import RealityKit
import ARKit

fileprivate extension ARView.DebugOptions {

    func showCollisions() -> ModelEntity {

        print("Code for visualizing collision objects goes here...")

        let vc = ViewController()

        let box = MeshResource.generateBox(size: 0.04)    
        let color = UIColor(white: 1.0, alpha: 0.15)    
        let colliderMaterial = UnlitMaterial(color: color)

        vc.visualCollider = ModelEntity(mesh: box,
                                   materials: [colliderMaterial])    
        return vc.visualCollider
    }
}

...然后在您点击屏幕时在 ViewController 中调用此方法:

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    let anchor = AnchorEntity()
    var ballEntity = ModelEntity()
    var visualCollider = ModelEntity()
    var sphere: MeshResource?

    @IBAction func onTap(_ sender: UITapGestureRecognizer) {

        sphere = MeshResource.generateSphere(radius: 0.02)

        let material = SimpleMaterial(color: .systemPink,
                                 isMetallic: false)

        ballEntity = ModelEntity(mesh: sphere!,
                            materials: [material])

        let point: CGPoint = sender.location(in: arView)

        guard let query = arView.makeRaycastQuery(from: point,
                                              allowing: .estimatedPlane,
                                             alignment: .any)
        else { return }

        let result = arView.session.raycast(query)

        guard let raycastResult = result.first
        else { return }

        let anchor = AnchorEntity(raycastResult: raycastResult)
        anchor.addChild(ballEntity)
        arView.scene.anchors.append(anchor)

        let showCollisions = arView.debugOptions.showCollisions()  // here it is
        ballEntity.addChild(showCollisions)

        ballEntity.generateCollisionShapes(recursive: true)
    }
}

请考虑一下,这是一个近似的可视化。这段代码只是向您展示了一种继续前进的方式。

【讨论】:

  • 谢谢安迪!您将如何实际可视化这些碰撞组件?只需在相同坐标上显示一个框?
  • 但是 CollisionComponent 的 ShapeResource 不是 MeshResource,我似乎找不到任何从 ShapeResource 创建 MeshResource 的方法。那你会怎么做呢?
  • @YanivH,我已经更新了我的答案。而且我还没有使用过ShapeResource 类。
  • 谢谢!!最后一个问题:有没有办法让纹理看起来与 .showPhysics 纹理完全一样,这样更容易看到 3D 可视化?
  • 是的,您可以创建 UV 映射纹理(边缘上的白色条纹)并将其指定为该立方体上的预乘 RGB*A png 纹理。 stackoverflow.com/questions/59084240/…
猜你喜欢
  • 2019-06-14
  • 2023-04-01
  • 2014-02-10
  • 2015-07-06
  • 2022-01-21
  • 2019-11-21
  • 2021-08-10
相关资源
最近更新 更多