ARMeshAnchor – SceneKit SCNView Renderer EXC_BAD_ACCESS

Viewed 233

Im converting the ARMeshAnchor data to mesh using SCNGeometrySource which it works fine but sometimes 3/10 I will get a bad_access from SceneKit renderer.

[![enter image description here][1]][1]

1 Answers

It occurs because ARMeshAnchors constantly update their data as ARKit refines its understanding of the real world. All ARMeshAnchors are dynamic anchors. However their mesh's subsequent changes are not intended to reflect in real time.

If you want to duplicate your ARMeshAnchors collection use the following code:

var anchorsArray = [ARMeshAnchor]()
    
let frame = arView.session.currentFrame
    
let meshAnchors = frame?.anchors.compactMap { $0 as? ARMeshAnchor }
    
anchorsArray += meshAnchors!

Every ARMeshAnchor is connected with primitive geometry data that's stored in MTLBuffer:

anchorsArray[0].geometry.faces.buffer
anchorsArray[0].geometry.vertices.buffer
anchorsArray[0].geometry.normals.buffer
anchorsArray[0].geometry.classification?.buffer
Related