When application launched first a vertical surface is detected on one wall than camera focus to the second wall, in the second wall another surface is detected. The first wall is now no more visible to the ARCamera but this code is providing me the anchors of the first wall. but I need anchors of the second wall which is right now Visible/focused in camera.
if let anchor = sceneView.session.currentFrame?.anchors.first {
let node = sceneView.node(for: anchor)
addNode(position: SCNVector3Zero, anchorNode: node)
} else {
debugPrint("anchor node is nil")
}
In order to get the node that is currently is in point of view you can do something like this:
If you would like to get all focused nodes, collect them in an array satisfying specified condition
Good luck!
The clue to the answer is in the beginning line of your
if let
statement.Lets break this down:
When you say
let anchor = sceneView.session.currentFrame?.anchors.first
, you are referencing an optional array ofARAnchor
, which naturally can have more than one element.Since your are always calling
first
e.g. index [0], you will always get the 1stARAnchor
which was added to the array.Since you now have 2 anchors, you would naturally need the last (latest) element. As such you can try this as a starter:
Update: Since another poster has interpreted the question differently, in that they believe the question is how can I detect if an ARPlaneAnchor is in view? Let's approach it another way.
First we need to take into consideration that the ARCamera has a Frostrum in which our content is shown:
As such, we would then need to determine whether an ARPlaneAnchor was
inViewOfFrostrum
.First we will create 2 variables:
The 1st to store the
ARPlaneAnchor
and its associatedSCNNode
, and the 2nd in order to provide a unique ID for each plane.In the
ARSCNViewDelegate
we can visualise an ARPlaneAnchor and then store it's information e.g:Now we have stored the detected planes, we then of course need to determine if any of these are in view of the ARCamera e.g:
Finally we then need to access this function which we could do in the following
delegate
method for examplerenderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval)
:Hopefully both of these will point in the right direction...