I try to set entity with custom material to ARMeshAnchor using RealityKit, like this examplehttps://www.dropbox.com/s/b0u9mbsxqaobnrf/Screen%20Shot%202021-10-29%20at%205.25.57%20PM.png?dl=0
I found mini solution how to make it via sceneKit Geometry, but can't something how to make it via RealityKit. Tell me is it possible to do it? If this is possible, how? I tried to do this, but it looks like I'm doing something wrong.
I'm also interested in understanding how to stretch mesh entities along the boundaries of the space that captures the camera, since it seems to be done through .generatePlane(width: 1, height: 1)
complete
private lazy var arView = ARView().do {
$0.frame = view.bounds
}
override func viewDidLoad() {
super.viewDidLoad()
MetalLibLoader.initializeMetal()
setupSubviews()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
arView.session.delegate = self
configureWorldTracking()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
arView.session.pause()
}
private func setupSubviews() {
view.addSubview(arView)
}
private func configureWorldTracking() {
let configuration = ARWorldTrackingConfiguration()
let personSegmentation: ARWorldTrackingConfiguration.FrameSemantics = .personSegmentationWithDepth
if ARWorldTrackingConfiguration.supportsFrameSemantics(personSegmentation) {
configuration.frameSemantics.insert(personSegmentation)
}
let sceneReconstruction: ARWorldTrackingConfiguration.SceneReconstruction = .mesh
if ARWorldTrackingConfiguration.supportsSceneReconstruction(sceneReconstruction) {
configuration.sceneReconstruction = sceneReconstruction
}
configuration.planeDetection.insert(.horizontal)
arView.renderOptions.insert(.disableMotionBlur)
arView.session.run(configuration)
}
private func updateAnchors(anchors: [ARAnchor]) {
for anchor in anchors.compactMap({ $0 as? ARMeshAnchor }) {
let anchorEntity = AnchorEntity(anchor: anchor)
anchorEntity.addChild(plasmaEntity())
arView.scene.addAnchor(anchorEntity)
}
}
private func plasmaEntity() -> ModelEntity {
let customMaterial: CustomMaterial
let surfaceShader = CustomMaterial.SurfaceShader(named: "plasma", in: MetalLibLoader.library)
do {
try customMaterial = CustomMaterial(surfaceShader: surfaceShader, lightingModel: .lit)
} catch {
fatalError(error.localizedDescription)
}
return ModelEntity(mesh: .generatePlane(width: 1, height: 1), materials: [customMaterial])
}
}
extension ARViewController: ARSessionDelegate {
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
updateAnchors(anchors: anchors)
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
updateAnchors(anchors: anchors)
}
}