MrRobot9
MrRobot9

Reputation: 2684

ARCore Android Studio: Place a 3D model on the 2D coordinate of the screen

I have 2D screen coordinates. That's where I want to place the 3D model irrespective of the depth. Shouldn't necessarily is a plane. It could on-air or anywhere. How do I go about it?

How do I get the Anchor position from 2D coordinate. I'm lost here, since there is no documentation. All the tutorials I have seen, place the 3D model on tap on the horizontal plane.

Upvotes: 1

Views: 1400

Answers (1)

Mick
Mick

Reputation: 25491

If you are still using Sceneform then the code below will place an object in front of the camera as whatever depth you want - you can play around with the 'pos' to move it if center is not useful for you, and to change the depth.

//Add an Anchor and a renderable in front of the camera       
Session session = arFragment.getArSceneView().getSession();
float[] pos = { 0, 0, -1 };
float[] rotation = { 0, 0, 0, 1 };
Anchor anchor =  session.createAnchor(new Pose(pos, rotation));
anchorNode = new AnchorNode(anchor);
anchorNode.setRenderable(andyRenderable);
anchorNode.setParent(arFragment.getArSceneView().getScene());

Note that Sceneform is 'open sourced and archived', so you may not want to use it now - see note here: https://developers.google.com/sceneform/develop.

If you are not using Scenefrom you could experiment with ARCore 'instant placement' - it is designed to allow an object be placed before the planes etc are deleted so not exactly what you are looking for, but you may find it can meet your needs. More detail and examples here: https://developers.google.com/ar/develop/java/instant-placement/developer-guide

Upvotes: 1

Related Questions