I develop an android application with ArCore library. I need make two points, draw a line between they and set a text labels near each line.
The text labels need to be rotated face-to-camera and have positions along the line.
1) Add a line between 2 points:
AnchorNode previousPoint = /* point 0 */
AnchorNode currentPoint = /* point 1 */
Vector3 previousPosition = previousPoint.getWorldPosition();
Vector3 currentPosition = currentPoint.getWorldPosition();
Quaternion rotation = currentPoint.getWorldRotation();
float[] f1 = new float[]{currentPosition.x, currentPosition.y, currentPosition.z};
float[] f2 = new float[]{rotation.x, rotation.y, rotation.z, rotation.w};
Vector3 difference = Vector3.subtract(previousPosition, currentPosition);
Vector3 directionFromTopToBottom = difference.normalized();
Quaternion rotationFromAToB = Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
ModelRenderable model = ShapeFactory.makeCube(new Vector3(.0025f, .0025f, difference.length()), Vector3.zero(), /* material */ );
Anchor anchor = getSession().createAnchor(new Pose(f1, f2));
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(getScene());
Vector3 lineCenter = Vector3.add(previousPosition, currentPosition).scaled(.5f);
Node lineNode = new Node();
lineNode.setParent(anchorNode);
lineNode.setRenderable(model);
lineNode.setWorldPosition(lineCenter);
lineNode.setWorldRotation(rotationFromAToB);
- Add a text label
Quaternion cameraRotation = getCamera().getWorldRotation();
float distance = Vector3.subtract(cameraPosition, lineCenter).length();
String result = String.format(Locale.ENGLISH, "%.3f", difference.length()) + ".m";
ViewRenderable textRenderable = mArModelCreator.getTextView();
((TextView)textRenderable.getView()
.findViewById(R.id.textLabel)).setText(result);
Node textNode = new Node();
textNode.setParent(lineNode);
textNode.setRenderable(textRenderable);
textNode.setWorldScale(new Vector3(1f, 1f, 1f).scaled(distance));
textNode.setWorldRotation(Quaternion.multiply(rotationFromAToB, cameraRotation));
But I do not have correct result.
My current labels:
What I need:
Please help me.