gpt4 book ai didi

java - 如何将 Renderable 放置到 HitTestResult getPoint() 位置

转载 作者:行者123 更新时间:2023-12-02 01:52:48 25 4
gpt4 key购买 nike

我想将一个可渲染的小球体放置到点击已放置模型的位置,并将其锚定到该位置。这可以实现吗?如果可以,如何实现?

private void tryPlaceModel(MotionEvent motionEvent, Frame frame)
{
if (isModelPlaced)
{
return;
}

if (motionEvent != null && frame.getCamera().getTrackingState() == TrackingState.TRACKING)
{
for (HitResult hit : frame.hitTest(motionEvent))
{
Trackable trackable = hit.getTrackable();

if (trackable instanceof Plane && ((Plane) trackable).isPoseInPolygon(hit.getHitPose()))
{
// Create the Anchor.
Anchor anchor = hit.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arSceneView.getScene());

TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
model.setParent(anchorNode);
model.setRenderable(andyRenderable);
model.select();

isModelPlaced = true;

model.setOnTapListener((hitTestResult, motionEvent1) ->
{
Frame frame1 = arSceneView.getArFrame();

if (motionEvent1 != null && frame1.getCamera().getTrackingState() == TrackingState.TRACKING)
{
com.google.ar.sceneform.rendering.Color color = new com.google.ar.sceneform.rendering.Color();
color.set(Color.RED);

MaterialFactory.makeOpaqueWithColor(this, color)
.thenAccept(
material ->
{
Pose pose = Pose.makeTranslation(hitTestResult.getPoint().x, hitTestResult.getPoint().y, hitTestResult.getPoint().z);

Anchor anchor1 = session.createAnchor(pose);

AnchorNode anchorNode1 = new AnchorNode(anchor1);
anchorNode1.setParent(hitTestResult.getNode());

Renderable sphere = ShapeFactory.makeSphere(0.05f,
/* WRONG LOCATION */anchorNode1.getLocalPosition(), material);

Node indicatorModel = new Node();
indicatorModel.setParent(anchorNode1);
indicatorModel.setRenderable(sphere);
}
);
}
});
}
}
}

这就是我现在所在的位置。除了我希望球体出现的位置之外,代码完全有效。我也尝试过使用 AnchorNode.getWorldPosition,但也是错误的。

球体出现在模型的背面。这是因为 getPoint() 方法返回光线击中平面的点(尽管字面意思是返回光线击中模型/节点的点),还是我只是做错了什么?

最佳答案

Node.OnTapListener当点击事件击中节点时调用,并且与平面监听器不同,平面监听器响应平面上的 HitTest ,而不是节点。

HitTestResult 具有命中的节点以及可渲染对象上命中点的世界坐标。有了这些,您可以创建一个节点,将父节点设置为节点命中,并将世界位置设置为 HitTest 结果点:

  public void addTapHandler(Node node) {
node.setOnTapListener((HitTestResult hitTestResult, MotionEvent motionEvent) -> {
// Hit test point will be where the ray from the screen point intersects the model.

// Create a sphere and attach it to the point.
Color color = new Color(.8f, 0, 0);

// Note: you can make one material and one sphere and reuse them.
MaterialFactory.makeOpaqueWithColor(this, color)
.thenAccept(material -> {
// The sphere is in local coordinate space, so make the center 0,0,0
Renderable sphere = ShapeFactory.makeSphere(0.05f, Vector3.zero(),
material);

Node indicatorModel = new Node();
indicatorModel.setParent(hitTestResult.getNode());
indicatorModel.setWorldPosition(hitTestResult.getPoint());
indicatorModel.setRenderable(sphere);
});
});
}

关于java - 如何将 Renderable 放置到 HitTestResult getPoint() 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52704938/

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