我想在检测到飞机后立即在ARcore的随机部分上设置3d模型。不确定使用哪种方法进行设置。
addOnUpdateListener(this::onUpdate);
private void onUpdate(FrameTime frameTime) {
if (numOfModels > 0) return;
modelLoader1 = new ModelLoader(weakReference);
Frame frame = arFragment.getArSceneView().getArFrame();
Collection<Plane> planes = frame.getUpdatedTrackables(Plane.class);
for (Plane plane : planes) {
if (plane.getTrackingState() == TrackingState.TRACKING) {
addObject(Uri.parse("andy_dance.sfb"));
break;
}
}
}
private void addObject(Uri model) {
Frame frame = arFragment.getArSceneView().getArFrame();
android.graphics.Point pt = getScreenCenter();
List<HitResult> hits;
if (frame != null) {
hits = frame.hitTest(pt.x, pt.y);
for (HitResult hit : hits) {
Trackable trackable = hit.getTrackable();
if (trackable instanceof Plane &&
((Plane) trackable).isPoseInPolygon(hit.getHitPose())) {
modelLoader1.loadModel(hit.createAnchor(), model);
break;
}
}
}
}
public void loadModel(Anchor anchor, Uri uri) {
if (owner.get() == null) {
Log.d(TAG, "Activity is null. Cannot load model.");
return;
}
ModelRenderable.builder()
.setSource(owner.get(), uri)
.build()
.handle((renderable, throwable) -> {
MainActivity activity = owner.get();
if (activity == null) {
return null;
} else if (throwable != null) {
activity.onException(throwable);
} else {
activity.addNodeToScene(anchor, renderable);
}
return null;
});
return;
}
public void addNodeToScene(Anchor anchor, ModelRenderable renderable) {
numOfModels++;
AnchorNode anchorNode = new AnchorNode(anchor);
TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());
node.setRenderable(renderable);
node.setParent(anchorNode);
node.setLocalPosition(new Vector3(0f, 0f, 0f));
modelLoader1.setNumofLivesModel0(2);
arFragment.getArSceneView().getScene().addChild(anchorNode);
setNodeListener(node, anchorNode, modelLoader1);
到目前为止,我可以用一个模型填充屏幕,但是在屏幕的中心,我们想暂时将模型设置在角落。
答案 0 :(得分:0)
在活动开始时,我实例化了一个新的Vector3对象,因此以后可以将其用于3d模型定位:
vector = new Vector3();
通过实现ARCore识别为手机位置的世界锚点来解决这个问题:
AnchorNode anchorNode = new AnchorNode();
anchorNode.setWorldPosition(new Vector3(0, 0, 0));
通过设置AnchorNode,我们可以知道世界上的实际位置,从而将其放置在世界上。
vector.set(randomCoordinates(true), randomCoordinates(false), -.7f);
AnchorNode anchorNode = new AnchorNode();
TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());
node.setRenderable(ModelRenderable);
node.setParent(anchorNode);
node.setWorldPosition(vector);
vector.set()接受的参数是3个浮点数,它们是x,y,z坐标。为了使3D模型显示在相机的前面,建议x坐标在-.300到.300之间,y坐标在-.500到.500之间,而z坐标在在-.7到1之间。