我想将一个可渲染的小球体放置到已经放置的模型被点击的位置,并将其锚定到该位置。这可以实现吗?如果可以,如何实现?
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() 方法返回射线击中平面的点(尽管字面上写它返回射线击中模型/节点的点),还是我做错了什么?