本文将详细讲述在Holotoolkit项目中提供的SpatialMapping(空间映射)组件的工作机理,通过了解其工作机制及流程,以便进行个性化编制。
之前在sun_t89的博客中有关于空间映射的简单说明这里写链接内容 感谢sun_t89的工作,而本文是深入阐述其内在实现机制,通过了解该机制来实现一些比较灵活的定制方案。
SpatialMapping组件工作机理
SpatialMapping(空间映射)组件主要由三个脚本组成,分别为:
SpatialMappingObserver
SpatialMappingManager
SpatialMappingSource
其功能分别为
SpatialMappingObserver 继承 spatialMappingSource,定期对周围环境进行扫描,并生成或更新surface数据。
SpatialMappingManager 负责控制SpatialMappingObserver是否工作,提供获取环境mesh列表的接口等。例如设置mesh材质,设置阴影,设置显示等操作。
SpatialMappingSource 负责扫描获得的mesh存储和管理工作,例如添加,更新 ,移除,提取等操作。
通过上面的功能描述,可以发现SpatialMappingObserver是核心代码,现在重点分析该代码
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VR.WSA;
namespace HoloToolkit.Unity.SpatialMapping
{
/// <summary>
/// Spatial Mapping Observer states.
/// </summary>
public enum ObserverStates
{
/// <summary>
/// The SurfaceObserver is currently running.
/// </summary>
Running = 0,
/// <summary>
/// The SurfaceObserver is currently idle.
/// </summary>
Stopped = 1
}
/// <summary>
/// The SpatialMappingObserver class encapsulates the SurfaceObserver into an easy to use
//SpatialMappingObeserver class将SurfaceObeserver以简单易用的方式进行封装
/// object that handles managing the observed surfaces and the rendering of surface geometry.
//负责管理被扫描后的平面和几何平面的渲染
/// </summary>
public class SpatialMappingObserver : SpatialMappingSource
{
[Tooltip("The number of triangles to calculate per cubic meter.")]
public float TrianglesPerCubicMeter = 500f;
[Tooltip("The extents of the observation volume.")]
public Vector3 Extents = Vector3.one * 10.0f;
[Tooltip("How long to wait (in sec) between Spatial Mapping updates.")]
public float TimeBetweenUpdates = 3.5f;
/// <summary>
/// Event for hooking when surfaces are changed.
/// </summary>
public event SurfaceObserver.SurfaceChangedDelegate SurfaceChanged;
/// <summary>
/// Event for hooking when the data for a surface is ready.
/// </summary>
public event SurfaceObserver.SurfaceDataReadyDelegate DataReady;
/// <summary>
/// Indicates the current state of the Surface Observer.
/// </summary>
public ObserverStates ObserverState { get; private set; }
/// <summary>
/// Our Surface Observer object for generating/updating Spatial Mapping data.
/// </summary>
private SurfaceObserver observer;
/// <summary>
/// A dictionary of surfaces that our Surface Observer knows about.
/// Key: surface id
/// Value: GameObject containing a Mesh, a MeshRenderer and a Material
/// </summary>
private Dictionary<int, GameObject> surfaces = new Dictionary<int, GameObject>();
/// <summary>
/// A dictionary of surfaces which need to be cleaned up and readded for reuse.
/// Key: ID of the surface currently updating
/// Value: A struct encapsulating Visual and Collider mesh to be cleaned up
/// </summary>
private Dictionary<int, GameObject> pendingCleanup = new Dictionary<int, GameObject>();
/// <summary>
/// A queue of clean su

本文深入解析Hololens SpatialMapping组件的工作原理,包括SpatialMappingObserver、SpatialMappingManager和SpatialMappingSource三个核心脚本的作用。SpatialMappingObserver负责环境扫描与更新,SpatialMappingManager控制工作状态并提供接口,SpatialMappingSource处理扫描结果的存储和管理。通过理解这一机制,开发者可以进行更灵活的定制。
最低0.47元/天 解锁文章
730

被折叠的 条评论
为什么被折叠?



