ARCore – 如何在没有特征点的墙面上放置/创建对象?
创始人
2024-09-12 09:02:22
0

要在没有特征点的墙面上放置/创建对象,可以使用平面检测功能和ARCore的Anchor来解决问题。以下是一个示例代码,演示如何在墙面上放置一个3D对象:

import com.google.ar.core.Anchor;
import com.google.ar.core.AugmentedImage;
import com.google.ar.core.AugmentedImageDatabase;
import com.google.ar.core.Config;
import com.google.ar.core.Frame;
import com.google.ar.core.Plane;
import com.google.ar.core.Session;
import com.google.ar.core.TrackingState;
import com.google.ar.sceneform.AnchorNode;
import com.google.ar.sceneform.ArSceneView;
import com.google.ar.sceneform.FrameTime;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.TransformableNode;

public class CustomArFragment extends ArFragment {

    private boolean wallDetected = false;
    private AnchorNode anchorNode;

    @Override
    protected Config getSessionConfiguration(Session session) {
        Config config = super.getSessionConfiguration(session);
        config.setFocusMode(Config.FocusMode.AUTO); // 自动对焦
        config.setPlaneFindingMode(Config.PlaneFindingMode.HORIZONTAL_AND_VERTICAL); // 启用水平和垂直平面检测
        return config;
    }

    @Override
    public void onUpdate(FrameTime frameTime) {
        super.onUpdate(frameTime);

        if (wallDetected) {
            return;
        }

        Frame frame = getArSceneView().getArFrame();
        if (frame == null) {
            return;
        }

        if (frame.getUpdatedPlanes().isEmpty()) {
            return;
        }

        for (Plane plane : frame.getUpdatedPlanes()) {
            if (plane.getTrackingState() == TrackingState.TRACKING && plane.getType() == Plane.Type.VERTICAL) {
                // 检测到垂直墙面
                if (!wallDetected) {
                    anchorNode = new AnchorNode();
                    anchorNode.setParent(getArSceneView().getScene());

                    Anchor anchor = plane.createAnchor(plane.getCenterPose());
                    anchorNode.setAnchor(anchor);

                    // 创建并放置你的3D对象
                    TransformableNode objectNode = new TransformableNode(getTransformationSystem());
                    objectNode.setParent(anchorNode);
                    objectNode.setRenderable(your3DObjectRenderable);
                    objectNode.select();

                    wallDetected = true;
                }
                break;
            }
        }
    }
}

要使用此代码,您需要将其放在一个自定义的ArFragment类中,然后在ArActivity中使用此自定义ArFragment

此代码在onUpdate方法中检测是否有更新的平面,并在检测到垂直墙面时创建并放置3D对象。在此之前,它还启用了自动对焦和水平/垂直平面检测。请确保将your3DObjectRenderable替换为您自己的3D对象渲染资源。

希望这可以帮助您在没有特征点的墙面上放置/创建对象!

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...