본글은 ARKit 동영상 강의를 기반으로 작성한 글입니다. 출처는 아래에서 확인할 수 있습니다.
오역이나 잘못된 정보에 대한 수정은 언제든지 환영합니다 :)
지난 글에서는 평면을 인식하고 오버레이를 적용함
그러나 해당 코드만 적용하면 같은 평면의 다른 부분을 발견하더라도 크기를 확장하지 않고 원래대로 유지되기 때문에 이를 개선하기 위한 작업이 필요.
planes
에 발견한 평면 추가
같은 평면을 확장하기 위해서는 우선 발견한 모든 평면을 저장하고 있어야함. 때문에 renderer(_:didAdd:for:)
안에서 해당 코드 추가
self.planes.append(plane)
2. renderer(_:didUpdate:for:)
을 이용해 같은 anchor를 가진 노드를 업데이트 시켜줌
renderer(_:didUpdate:for:)
은 노드의 속성이 대응하는 anchor 와 일치하기 위해 업데이트 되었다고 알려주는 함수. 이 함수를 통해 전달된 anchor 의 identifier 가 planes
에 담긴 노드 중의 anchor identifier와 같다면 해당 노드를 return하고 받은 anchor 를 통해 update 시켜줌
let plane = self.planes.filter { plane inreturn plane.anchor.identifier == anchor.identifier}.firstif plane == nil {return}plane?.update(anchor: anchor as! ARPlaneAnchor)
cf)renderer(_:didUpdate:for:) — Tells the delegate that a SceneKit node’s properties have been updated to match the current state of its corresponding anchor.
renderer(_:didAdd:for:) — Tells the delegate that a SceneKit node corresponding to a new AR anchor has been added to the scene.
ARAnchor — A real-world position and orientation that can be used for placing objects in an AR scene.
3. update(anchor:)
메소드 작성
받아온 anchor
을 통해 geometry
의 width
와 height
그리고 자신(노드)의 position
을 설정해줌.
self.planeGeometry.width = CGFloat(anchor.extent.x)self.planeGeometry.height = CGFloat(anchor.extent.z)self.position = SCNVector3Make(anchor.center.x, 0, anchor.center.z)