You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Session Anchor时通过Sceneform获取ARCore内部地板平面的方法咨询

获取ARCore内部地板平面(使用Session Anchor场景)

Hey there, I’ve been in this exact situation before—Sceneform doesn’t have a direct API to grab ARCore’s internal floor plane when you’re using Session Anchors instead of user-placed Trackable Anchors, but we can work around this by tapping into the underlying ARCore Session APIs that Sceneform wraps. Here’s how to do it step by step:

核心思路

ARCore会在后台自动检测并维护地板平面,哪怕你没使用用户选择的锚点。我们可以直接通过ARCore的Session实例获取这些平面,筛选出地板类型,再创建绑定到该平面姿态的Session Anchor。

具体实现步骤

  • Step 1: 开启平面检测功能
    首先要确保ARCore配置中开启了水平平面检测(地板属于水平平面),初始化Session时可以设置:

    val arSceneView = arFragment.arSceneView
    val session = arSceneView.session ?: return
    
    val config = session.config
    config.planeFindingMode = Config.PlaneFindingMode.HORIZONTAL
    session.configure(config)
    
  • Step 2: 监听帧更新来检测地板
    Sceneform的ArSceneView允许我们监听帧更新事件。在每一帧中,我们可以获取所有更新的平面,筛选出有效的地板平面:

    arSceneView.scene.addOnUpdateListener { _ ->
        val frame = arSceneView.arFrame ?: return@addOnUpdateListener
        // 获取当前帧中所有更新的平面
        val updatedPlanes = frame.getUpdatedTrackables(Plane::class.java)
    
        for (plane in updatedPlanes) {
            // 只筛选处于跟踪状态的地板平面
            if (plane.trackingState == TrackingState.TRACKING && plane.type == Plane.Type.FLOOR) {
                // 获取地板平面的中心姿态
                val floorCenterPose = plane.centerPose
                // 创建绑定到该地板姿态的Session Anchor
                val floorSessionAnchor = session.createAnchor(floorCenterPose)
                
                // 转换为Sceneform的AnchorNode,方便在场景中使用
                val floorAnchorNode = AnchorNode(floorSessionAnchor)
                floorAnchorNode.setParent(arSceneView.scene)
                
                // 现在你可以给这个节点添加3D内容,比如地板标记
                // val floorVisual = TransformableNode(arFragment.transformationSystem)
                // floorVisual.setParent(floorAnchorNode)
                // floorVisual.renderable = yourFloorRenderable
                
                // 可选:找到有效地板后停止监听
                // arSceneView.scene.removeOnUpdateListener(this)
                break
            }
        }
    }
    
  • Step 3: 处理平面合并(重要)
    ARCore经常会将相邻的平面合并成一个更大的平面,为了保持地板锚点的准确性,你可以监听平面合并事件,必要时更新Session Anchor:

    session.addTrackableListener(Plane::class.java) { plane, event ->
        if (event == Trackable.Event.MERGED && plane.type == Plane.Type.FLOOR && plane.trackingState == TrackingState.TRACKING) {
            // 将地板锚点更新到合并后平面的中心
            val newFloorPose = plane.centerPose
            val newSessionAnchor = session.createAnchor(newFloorPose)
            // 替换旧的AnchorNode为新的节点
            // ...
        }
    }
    

关键注意事项

  • 始终检查平面的trackingState——只使用处于TrackingState.TRACKING状态的平面,避免将内容放置在不稳定的位置。
  • Session Anchor可以跨会话持久化(如果你保存了它的云锚点ID),所以这种方法在后续恢复锚点时依然有效。
  • 如果你不需要持久化锚点,可以跳过创建Session Anchor,直接使用平面姿态放置临时内容。

内容的提问来源于stack exchange,提问作者MAML

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:48:53