gpt4 book ai didi

android - 如何使用 ARCore 在平面上绘制线/折线

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:34:20 25 4
gpt4 key购买 nike

Java ARCore Hello AR示例中,我们可以通过点击屏幕将Android对象放置在平面上,我们如何使用这个HitResult的信息来画线在这些对象之间?

感谢您的帮助!

最佳答案

在您抓取 anchor 以放置对象的代码部分,您应该检查您是否已经有一个先前的 anchor 。如果你有一个以前的 anchor ,从以前的和当前的 anchor 中获取 worldPosition(作为 Vector3 对象),然后计算它们之间的差异,并创建一条长度相同的线,并将它附加到两个之间的中间点的场景点。最后,将 previousAnchor 设置为当前 Anchor。

这是我用来解决这个问题的一些代码:

// 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;

关于android - 如何使用 ARCore 在平面上绘制线/折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46176529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com