I'm using Sceneform with ARCore on Android and am unable to understand the concepts clearly with the documentation provided. I'm trying to modify the existing HelloSceneform App from github and trying to create a app, where as soon as it's started, the user sees a 3D object directly at his/her front. This is very similar to what I found https://github.com/google-ar/arcore-unity-sdk/issues/144, but I couldn't figure out how I can improve the existing code to get it.
setContentView(R.layout.activity_ux);
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
ModelRenderable.builder()
.setSource(this, R.raw.andy)
.build()
.thenAccept(modelRenderable -> {
andyRenderable=modelRenderable;
});
arFragment.setOnTapArPlaneListener(
(HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
TransformableNode andy = new TransformableNode(arFragment.getTransformationSystem());
andy.setParent(anchorNode);
andy.setRenderable(andyRenderable);
andy.select();
});
I just need to disable surface detection, get a Pose object, an anchor and set the object directly without any touch listeners on android, and all that in java code. When I try to create an anchor using a pose, it gives me a NotTrackingException.
session=new Session(this);
...
Pose pose = Pose.makeTranslation(-0.41058916f, -0.6668466f,
Anchor anchor = session.createAnchor(pose);
I hope someone can take their time to help.
@Override
public void onUpdate(FrameTime frameTime) {
Frame frame = playFragment.getArSceneView().getArFrame();
if (frame == null) {
return;
}
if (frame.getCamera().getTrackingState() != TrackingState.TRACKING) {
return;
}
for (Plane plane : frame.getUpdatedTrackables(Plane.class)) {
playFragment.getPlaneDiscoveryController().hide();
if (plane.getTrackingState() == TrackingState.TRACKING) {
for (HitResult hit : frame.hitTest(getScreenCenter().x, getScreenCenter().y)) {
Trackable trackable = hit.getTrackable();
if (trackable instanceof Plane && ((Plane) trackable).isPoseInPolygon(hit.getHitPose())) {
Anchor anchor = hit.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(playFragment.getArSceneView().getScene());
Pose pose = hit.getHitPose();
Node node = new Node();
node.setRenderable(modelRenderable);
node.setLocalPosition(new Vector3(pose.tx(), pose.compose(Pose.makeTranslation(0.0f, 0.05f, 0.0f)).ty(), pose.tz()));
node.setParent(anchorNode);
}
}
}
}
}
private Vector3 getScreenCenter() {
View vw = findViewById(android.R.id.content);
return new Vector3(vw.getWidth() / 2f, vw.getHeight() / 2f, 0f);
}