swift - 如何让 RealityKit 只显示 CollisionComponents?
问题描述
我试图在我的 ARView 上查看 CollisionComponents。
我将其.showPhysics
用作 debugOptions 的一部分,但由于屏幕上有 20 个对象,我让所有法线都变得疯狂,并且 CollisionComponents 的颜色不清楚(某种形式的奇怪粉红色)。
有谁知道如何只呈现 CollisionComponents 而没有任何额外数据作为.showPhysics
?
解决方案
您可以使用简单的 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)
}
}
请考虑,这是一个近似的可视化。这段代码只是向您展示了一种继续前进的方式。
推荐阅读
- node.js - Web socket 在其他机器上打不开
- html - 制作导航栏 两个不同的东西,中间没有背景
- c - 从 CCS 中的引脚读取模拟值
- c# - 从 List 返回较小的向量
- c# - 协助识别用户输入
- batch-file - 如何搜索匹配文件并分配给批处理文件中的变量?
- progressive-web-apps - 渐进式 Web 应用程序有什么方法可以访问本地文件系统?
- angular - Spring + Angular - URL 处理
- mysql - Sequelize 无法从 Node/express 应用程序同步到 docker 容器中的 MySQL (ECONNREFUSED)
- php - 在 PHP 中,将数组与 MySQL 表中的字段进行比较