I am not create the planes underneath each object. Instead, I just create planes based on real world plane.
Here is the plane detection and creation code:
private let planeTracking = PlaneDetectionProvider(alignments: [.horizontal])
@MainActor
func processPlaneAnchor(_ anchorUpdate: AnchorUpdate<PlaneAnchor>) {
let anchor = anchorUpdate.anchor
if anchor.classification == .window { return }
switch anchorUpdate.event {
case .added:
print("PlaneAnchor added")
if let entity = planeMap[anchor.id] {
let planeEntity = entity.findEntity(named: "plane") as! ModelEntity
planeEntity.model!.mesh = MeshResource.generatePlane(width: anchor.geometry.extent.width, height: anchor.geometry.extent.height)
planeEntity.transform = Transform(matrix: anchor.geometry.extent.anchorFromExtentTransform)
} else {
let entity = Entity()
// Add a new entity to represent this plane.
var material = PhysicallyBasedMaterial()
material.baseColor.tint = UIColor.white.withAlphaComponent(0.0)
material.roughness = PhysicallyBasedMaterial.Roughness(floatLiteral: 1.0)
material.metallic = PhysicallyBasedMaterial.Metallic(floatLiteral: 0.0)
material.blending = .transparent(opacity: 0.0)
let planeEntity = ModelEntity(mesh: .generatePlane(width: anchor.geometry.extent.width, height: anchor.geometry.extent.height), materials: [material])
planeEntity.components.set(OpacityComponent(opacity: 0.0))
planeEntity.name = "plane"
planeEntity.transform = Transform(matrix: anchor.geometry.extent.anchorFromExtentTransform)
entity.addChild(planeEntity)
planeMap[anchor.id] = entity
rootEntity.addChild(entity)
}
planeMap[anchor.id]?.transform = Transform(matrix: anchor.originFromAnchorTransform)
case .updated:
print("PlaneAnchor updated")
case .removed:
print("PlaneAnchor removed")
planeMap[anchor.id]?.removeFromParent()
planeMap.removeValue(forKey: anchor.id)
}
}
And there is some strange event:
The outlines just sometimes appear, not always, even for the same object.
The outlines appears underneach the objects like its shadow, maybe it is not the boarder of the plane.
Topic:
Spatial Computing
SubTopic:
ARKit
Tags: