Я хочу разместить небольшую сферу для визуализации в том месте, где была нажата уже размещенная модель, а также привязать ее к этому месту. Можно ли этого достичь, и если да, то как?
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 вызывается, когда событие касания попадает в узел и отличается от прослушивателя плоскости, который отвечает на проверку попадания в плоскости, а не в узле.
HitTestResult имеет узел, который попал в точку, и мировые координаты точки на рендеринге. С их помощью вы можете создать узел, установить родительский элемент для попадания в узел, а мировую позицию - для точки результата тестирования:
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);
});
});
}
@Clayton, не могли бы вы помочь мне с этой проблемой, пожалуйста, stackoverflow.com/questions/63900439/…
Отличный ответ, спасибо!
Как раз то, что мне было нужно. В конце концов, это было просто. Спасибо!