我是 Android AR 的新手,我想实现以下行为。我正在使用场景形式。
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.15.0'
我有一个 3D 模型(简单的长方体)。这是创建长方体的方法。
private ModelRenderable 3dModel;
private void createModel(AnchorNode anchorNode) {
TransformableNode cartoonPersonNodeThree = new TransformableNode(arFragment.getTransformationSystem());
cartoonPersonNodeThree.getScaleController()
.setEnabled(false); // Disable scaling
cartoonPersonNodeThree.getTranslationController()
.setEnabled(false); // Disable moving
cartoonPersonNodeThree.setParent(anchorNode);
cartoonPersonNodeThree.setRenderable(3dModel);
cartoonPersonNodeThree.select();
}
当检测到 Surface 时,我需要将此模型显示在用户面前,并且它应该随着相机的移动而移动。
下面的代码做到了。3D模型显示在中央和相机前方1m处。
arFragment.getArSceneView()
.getScene()
.addOnUpdateListener(frameTime -> {
Frame frame = arFragment.getArSceneView()
.getArFrame();
if (frame == null) {
return;
}
if (frame.getCamera()
.getTrackingState() != TrackingState.TRACKING) {
return;
}
if (isModelAlreadyInPlace) {
return;
}
if (isModelLocked) {
return;
}
removeAnchorNode(anchorNode); // Remove previous 3D model
// Place the anchor 1m in front of the camera.
Session session = arFragment.getArSceneView()
.getSession();
Anchor newMarkAnchor = session.createAnchor(frame.getCamera()
.getPose()
.compose(Pose.makeTranslation(0, 0, -1f)) //This will place the anchor 1M in front of the camera
.extractTranslation());
AnchorNode addedAnchorNode = new AnchorNode(newMarkAnchor);
addedAnchorNode.setRenderable(cartoonPersonThree);
addedAnchorNode.setParent(arFragment.getArSceneView()
.getScene());
anchorNode = addedAnchorNode;
});
这里的问题是 3D 模型在空中,如果我将相机移到天花板上,3D 模型就会在那里。
有没有办法将 3D 模型与地板对齐?