# 6DOF（SLAM）

## **介绍**

SLAM-6DoF （**Simultaneous Localization and Mapping - 6 Degrees of Freedom**） 是一种空间定位与建图技术，可实现设备在三维空间内的精准定位与姿态感知。

在 X3Pro 设备中，借助 VGA 相机模式来实现它。这种模式下，功耗更低，能有效延长设备使用时间；性能也更稳定，可精准捕捉环境信息，让定位与建图更准确，为设备导航等应用提供可靠支持。

![](/files/55d37899ffdec50ec9fdf575ef954b034ede759a)

## **API介绍**

[头控模式请选择SLAM模式](/rayneo-devdoc/x-xi-lie/unity-kai-fa/kuai-su-kai-shi/dao-ru-ardk-ji-pei-zhi.md#tou-kong-mo-shi-xuan-ze)

XR Plugin 预制体参考 SlamDemoCtrl 脚本

<table data-header-hidden><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>调用方法</td><td>使用说明</td><td>示例代码</td></tr><tr><td>开启Slam 6dofAlgorithm.EnableSlamHeadTracker()</td><td><strong>参数与返回值细节：函数无参数和返回</strong><br><strong>异常场景说明：</strong>无<br><strong>调用时机建议：在进入Slam 6dof 前 及时 调用</strong></td><td><pre class="language-c#"><code class="lang-c#">Algorithm.EnableSlamHeadTracker();
</code></pre><p><br></p></td></tr><tr><td>获取当前Slam的状态Algorithm.GetSlamStatus()<br></td><td><strong>参数与返回值细节：</strong>调用无入参，返回 SlamState 枚举值，可根据值判断 SLAM 运行阶段<strong>异常场景说明：使能Slam 6dof</strong>。<strong>调用时机建议：在使能Slam 6dof，可以根据需要实时监听状态，然后进行对应处理</strong>。<br></td><td><pre class="language-c#"><code class="lang-c#">Algorithm.GetSlamStatus()

public enum SlamState
{
//正在初始化
FFVINS\_INITIALIZING = 0,
//初始化成功
FFVINS\_TRACKING\_SUCCESS = 1,
//失败
FFVINS\_TRACKING\_FAIL = 2,
}

/// \<summary>
/// 获取 Slam 状态
/// \</summary>
private void JudgeSlamStateUpdate() {
Algorithm.SlamState slamState = Algorithm.GetSlamStatus();
Debug.Log(TAG + "JudgeSlamState() slamState = " + slamState);
if (mSlamState != slamState && slamState == Algorithm.SlamState.FFVINS\_TRACKING\_FAIL) {
Debug.Log(TAG + "JudgeSlamState() FFVINS\_TRACKING\_FAIL");
mSlamState = slamState;

```
    OnSlamFailed();

} else if (mSlamState != slamState &#x26;&#x26; slamState == Algorithm.SlamState.FFVINS_TRACKING_SUCCESS) {
    Debug.Log(TAG + "JudgeSlamState() FFVINS_TRACKING_FAIL");
    mSlamState = slamState;

    OnSlamSuccess();
}
```

} </code></pre></td></tr><tr><td>姿态更新HeadTrackedPoseDriver.OnPostUpdate</td><td><strong>参数与返回值细节：回调函数 Pose，Pose.rotation 可以获取旋转, Pose.position 获取位置</strong><br><strong>异常场景说明：</strong>无<br><strong>调用时机建议：需要时候获取</strong></td><td><pre class="language-c#"><code class="lang-c#">HeadTrackedPoseDriver.OnPostUpdate += OnPostUpdate;
HeadTrackedPoseDriver.OnPostUpdate -= OnPostUpdate;

// 事件回调示例
private void OnPostUpdate(Pose pose)
{
Debug.Log($"\[SlamDemoCtrl] OnPostUpdate() position = {pose.position}, rotation = {pose.rotation}");
} </code></pre></td></tr><tr><td>关闭 Slam 6dofAlgorithm.DisableSlamHeadTracker();</td><td><strong>参数与返回值细节：函数无参数和返回</strong><br><strong>异常场景说明：</strong>无<br><strong>调用时机建议：在退出入Slam 6dof 前 及时 调用</strong></td><td><pre class="language-c#"><code class="lang-c#">Algorithm.DisableSlamHeadTracker(); </code></pre></td></tr></tbody></table>

## **构建场景**

参见 [导入ARDK及配置](/rayneo-devdoc/x-xi-lie/unity-kai-fa/kuai-su-kai-shi/dao-ru-ardk-ji-pei-zhi.md)，搭建好基础环境，并且导入 Sample 功能能力 Demo 。

![](/files/52d1b1b64afbb1d694d7cd5493286f43b850ba53)

接下来我们参见 Sample 中 Slam ,进行实现 该案例

### **1. 新建场景**

新建Unity3D 工程，取名 SLAM-6DoF

![](/files/0527b0daafa8bc56a0a2a223f8f77a1a31ff67d2)

### **2. 替换 MainCamera 为 XR Plugin**

打开场景，删除默认的MainCamera

![](/files/0e363e1c381e61e6338a5f5f44b6a30c24de07e7)

在 packages 目录下找到 RayNeo OPenXR ARDK - SDK - Runtime - Resources - Prefab 文件夹下 的 XR Plugin 预制体

![](/files/7553240d7436cbddce3ff948d4b9044fa642dedc)

把 XR Plugin 预制体 拖到场景中

![](/files/d7aaaa610ca26e9aec5c9fd929f88492fa2ef7a1)

### **3. 在场景中添加资源**

鼠标右键，在场景中添加一个 Cube

![](/files/b9032bf31faeb7bba1da68916bcc4577031aaba6)

对场景中 Cube 进行一些参数设置，并添加一个 材质

![](/files/7a1b9620ed5d13abf161dccd4c0731f285c08f8c)

![](/files/51f628cca5158465fcb8e33d343012a9ceb3958f)

### **4. 添加双击 TP 操控板退出应用功能**

在 Scripts 文件夹（没有自行创建即可）下，创建一个 QuitApp 脚本

![](/files/99e3c4d5349e897ae2b0bb2c36f3a8d43c65dab9)

双击脚本，打开编辑器，编写双击 TP 退出代码

```c#
using RayNeo;
using UnityEngine;

public class QuitApp : MonoBehaviour
{
    /// <summary>
    /// 退出应用
    /// </summary>
    public void ToQuitApp()
    {
        Application.Quit();
    }

    void Start()
    {
        // 添加双击事件
        SimpleTouchForLite.Instance.OnDoubleTap.AddListener(ToQuitApp);
    }

    private void OnDestroy()
    {
        if (SimpleTouchForLite.SingletonExist)
        {
            // 移除双击事件
            SimpleTouchForLite.Instance.OnDoubleTap.RemoveListener(ToQuitApp);
        }

    }
}
```

把 QuitApp 挂载到 SlamDemoCtrl 物体上

![](/files/cb97314ef0250ca1534432b036ed61b05a82ccd1)

## **编译运行场景**

### **1. 编译打包 APK**

把 Slam-6DoF 场景 添加到 Scene In Build 中，点击 Build 进行 打包 APK

![](/files/35503f1f88e488824dcd188d6d730ca15dc32df6)

### **2. 连接设备**

X3pro 眼镜通过设备线，连接电脑，（其中，电脑 ADB 环境参见手机已经配置好环境），使用 adb devices ，查看连接的设备

![](/files/b97e2a1715f1a4bc201d2fc18e6ffacff62f919c)

### **3. 安装应用**

之前编译打包好的应用，使用 adb install xxx.apk 进行安装

![](/files/025cd0e839cf51500b7308f91b314b43c2ac949a)

### **4. 查看运行应用**

可以在眼镜上看到刚刚安装的应用，如下图

![](/files/32ddfd6b9dd9f1cf4b1a8c66a10c4a2b3ac42bb0)

单击 TP 操作板，运行应用，简单效果如下

<figure><img src="/files/4OFS9dprnHuWFP3QupUF" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rayneo.gitbook.io/rayneo-devdoc/x-xi-lie/unity-kai-fa/ar-neng-li-api/6dof-slam.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
