Search code examples
androidaugmented-realityarcore

How to completely fit an AR ViewRenderable on target image?


I want an AR ViewRenderable to be placed over the real world target picture and completely fit over its boundaries. Although ViewSizer is used to change the size of AR objects, but it does it globally(sets dpPerMeter for every situation). However, I want to scale it wrt target's size,

I think something can be done by setLocalScale methods along with getExtentX and getExtentZ, but not sure how to set the parameters for my cause.

Have a look at the current code snippet :

    setAnchor(image.createAnchor(image.getCenterPose()));
    Node cornerNode = new Node();
    cornerNode.setParent(this);
    cornerNode.setLocalRotation(new Quaternion(new Vector3(1,0,0), -90));
    cornerNode.setLocalPosition(new Vector3(0.0f, 0.0f, 0f));
    cornerNode.setRenderable(targetImage.getNow(null));

Solution

  • By default, 250dp equals to 1 meters, but you can change it by ViewRenderable.builder().setSizer(DpToMetersViewSizer(you_size))

    you have to calculate the scale by the image size and AR core estimated size, so add these code like

    // in this example, the image is 100 cm x 66 cm
    val imageWidth = 1f //  = 1m
    val imageHeight = 0.66 // = 66 cm
    
    val scaledWidth = imageWidth / image.extentX
    val scaledHeight = imageHeight / image.extentZ
    
    // scale the Node
    node.localScale = Vector3(scaledWidth, scaledHeight, scaledWidth)
    
    // also, my view_wall.xml is 250dp x 166dp and the VerticalAlignment is center, like
    val wall = ViewRenderable.builder().setView(this, R.layout.view_wall)
                .setVerticalAlignment(ViewRenderable.VerticalAlignment.CENTER)
                .build()
    

    It works for me, help it works for you.

    by the way this is my practice project https://github.com/swarmnyc/arcore-augmented-image-swarm