在Java ARCore Hello AR
示例中,我们可以通过点击屏幕上放置的Android对象在飞机上,我们如何使用这个HitResult
单曲信息,这些对象之间画线?
感谢您的帮助!
在这里你抓锚放置目标代码段,你应该检查,如果你已经有一个以前的锚。如果你有一个以前的锚,抓住从以往和当前锚worldPosition(作为的Vector3对象),然后计算它们之间的区别,并创建一个行的长度,并在两者之间的中间点连接到现场点。最后,设置previousAnchor到当前。
下面是一些代码,我用来解决这个问题:
// Create the Anchor.
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
// Code to insert object probably happens here
if (lastAnchorNode != null) {
Vector3 point1, point2;
point1 = lastAnchorNode.getWorldPosition();
point2 = anchorNode.getWorldPosition();
Node line = new Node();
/* First, find the vector extending between the two points and define a look rotation in terms of this
Vector. */
final Vector3 difference = Vector3.subtract(point1, point2);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
final Renderable[] lineRenderable = new Renderable[1];
/* Then, create a rectangular prism, using ShapeFactory.makeCube() and use the difference vector
to extend to the necessary length. */
MaterialFactory.makeOpaqueWithColor(this, color)
.thenAccept(
material -> {
lineRenderable[0] = ShapeFactory.makeCube(new Vector3(.01f, .01f, difference.length()),
Vector3.zero(), material);
});
/* Last, set the world rotation of the node to the rotation calculated earlier and set the world position to
the midpoint between the given points . */
line.setParent(anchorNode);
line.setRenderable(lineRenderable[0]);
line.setWorldPosition(Vector3.add(point1, point2).scaled(.5f));
line.setWorldRotation(rotationFromAToB);
}
lastAnchorNode = anchorNode;
Anchor
将是对你有帮助。多亏了它,你可以跟踪挖掘位置和使用点之间的坐标。我做过类似的东西来算,我已经挖两点之间的距离
从@Arthulia的解决方案并没有为我工作=(
但我已经做了一点点不同。有用:
在我的onCreate:
ArFragment arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.vr_fragment);
arFragment.setOnTapArPlaneListener(
(HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
addLineBetweenHits(hitResult, plane, motionEvent);
});
然后:
private void addLineBetweenHits(HitResult hitResult, Plane plane, MotionEvent motionEvent) {
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
if (lastAnchorNode != null) {
anchorNode.setParent(arFragment.getArSceneView().getScene());
Vector3 point1, point2;
point1 = lastAnchorNode.getWorldPosition();
point2 = anchorNode.getWorldPosition();
/*
First, find the vector extending between the two points and define a look rotation
in terms of this Vector.
*/
final Vector3 difference = Vector3.subtract(point1, point2);
final Vector3 directionFromTopToBottom = difference.normalized();
final Quaternion rotationFromAToB =
Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
MaterialFactory.makeOpaqueWithColor(getApplicationContext(), color)
.thenAccept(
material -> {
/* Then, create a rectangular prism, using ShapeFactory.makeCube() and use the difference vector
to extend to the necessary length. */
ModelRenderable model = ShapeFactory.makeCube(
new Vector3(.01f, .01f, difference.length()),
Vector3.zero(), material);
/* Last, set the world rotation of the node to the rotation calculated earlier and set the world position to
the midpoint between the given points . */
Node node = new Node();
node.setParent(anchorNode);
node.setRenderable(model);
node.setWorldPosition(Vector3.add(point1, point2).scaled(.5f));
node.setWorldRotation(rotationFromAToB);
}
);
lastAnchorNode = anchorNode;
}