我正在尝试在我的ARView上看到CollisionComponents。
我在调试选项中使用了.showPhysics
,但是由于屏幕上有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()
ballEntity.addChild(showCollisions)
ballEntity.generateCollisionShapes(recursive: true)
}
}