0

I am trying to place a virtual 2D image on the screen aside of a tracked (reference) image. To do that I used the code below once the reference image is tracked, I create a plane which resembles the image on the screen and then I evaluate position for the image node with simple basic math. Too simple apparently because the result is not very good.

I believe there should be a better way but I am new to ARKit and I haven't found it yet, could use some help. Thanks.

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

    let node = SCNNode()

    if let imageAnchor = anchor as? ARImageAnchor {

        if let name = imageAnchor.name {
            print("Image name: \(name)")
        }

        // Create a plane to visualize the initial position of the detected image.
        let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width,
                             height: imageAnchor.referenceImage.physicalSize.height)
        let planeNode = SCNNode(geometry: plane)
        planeNode.opacity = 0.25

        planeNode.eulerAngles.x = -.pi / 2

        let imagePlane = SCNPlane(width: 0.1, height: 0.1)
        imagePlane.firstMaterial?.diffuse.contents = UIImage(named: "image")
        let imageNode = SCNNode(geometry: imagePlane)

        imageNode.eulerAngles.x = -.pi / 2

        let x = Float(-1 * (plane.width/2 + imagePlane.width/2))
        let y = Float(-1 * (plane.height/2 + imagePlane.height/2))
        imageNode.position = SCNVector3Make(Float(x), Float(y), 0)

        node.addChildNode(imageNode)
    }

    return node
}

Here is the unsatisfactory result:

enter image description here

EDIT: What looks wrong here? The image should stay right next to the tracked image (the big one) on the left, touching the left side of the tracked image. And the problem is also the way to achieve that, I accomplished this bad result with approximate math calculation, I suppose there is a way to use the transform of the tracked image plane to dispose another image on the side.

Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32
  • What is the result, can we see a screenshot? – EmilioPelaez Nov 29 '18 at 17:31
  • Added screenshot to the question – Fabrizio Prosperi Nov 30 '18 at 08:35
  • What looks wrong there? The size of the image? Just set the image as the material of the first plane, I don't get why you would need 2 – jlsiewert Nov 30 '18 at 11:46
  • @jlsiewert: the problem is not the size of the image, the image should stay right next to the tracked image (the big one) on the left, touching the left side of the tracked image. And the problem is also the way to achieve that, I accomplished this bad result with approximate math calculation, I suppose there is a way to use the transform of the tracked image plane to dispose another image on the side. – Fabrizio Prosperi Nov 30 '18 at 14:54

0 Answers0