1
votes

I'm trying to overlay detected plane by some images

so I trying to use renderer(_:nodeFor:)

Inside that method some bloggers wrote code like this

    guard let anchor = anchor as? ARPlaneAnchor else { return nil }
    let anchorNode = SCNNode()
    let planeNode = SCNNode()

    let material = SCNMaterial()
    material.diffuse.contents = UIColor.gray.withAlphaComponent(0.6)

    let plane = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))
    plane.materials = [material]

    planeNode.geometry = plane
    planeNode.position = SCNVector3(anchor.center.x, 0, anchor.center.z)
    planeNode.eulerAngles = SCNVector3(-Float.pi / 2, 0, 0)

    anchorNode.addChildNode(planeNode)
    return anchorNode

why they add child node to Empty node?

why just doesn't rotate itself

Of course I read documentation about it

they said :

You can implement this method to provide a new SCNNode object (or instance of an SCNNode subclass) containing any attachments you plan to use as a visual representation of the anchor. Note that ARKit controls the node's visibility and its transform property, so you may find it useful to add child nodes or adjust the node's pivot property to maintain any changes to position or orientation that you make.

what is the logic behind the scene?

then how to rotate node itself using pivot?

1

1 Answers

0
votes

When renderer(_:nodeFor:) is called it is asking for a node to attach to the anchor and if the anchor's location in world space is updated it automatically updates the node as well. All you need to do is return the plane node that you want to put on the detected surface.

For example in renderer(_:nodeFor:) you could do this

guard let anchor = anchor as? ARPlaneAnchor else { return nil }

let imagePlane = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))
imagePlane.firstMaterial.diffuse.contents = UIImage(named: 'yourImage')

return SCNNode(geometry: imagePlane)