Version: 2018.4
SpatialMapping 组件常规设置
空间映射渲染器 (Spatial Mapping Renderer)

Spatial Mapping Collider

The Spatial Mapping Collider component allows holographic content to interact with real-world physical Surfaces. This component handles creating, updating, and destroying the Surface GameObject Colliders in the Scene.

The component periodically queries the system for Surface changes in the physical world. When the system reports Surface changes, the Spatial Mapping Collider component prioritizes when each reported Surface is baked by Unity. Every time a Surface is baked by the system, a new GameObject is then generated by Unity, containing a Mesh Filter and Mesh Collider component. A Surface with a Mesh Collider allows raycasts to collide with it as with any other mesh in Unity. See Camera Raycast documentation for more information.

Note: This component only updates the Mesh Collider components of Surface GameObjects, and not the Mesh Renderers.

Note: Spatial Mapping Colliders update with less latency than Spatial Mapping Mesh Renderers. This means that Colliders update faster than Renderers.

Spatial Mapping Collider component as it appears in the Unity Editor
Spatial Mapping Collider component as it appears in the Unity Editor

Collider Settings

Setting Description
Enable Collisions Check this box to enable Surface Mesh Colliders. This means that holographic content can collide with Surfaces.
Mesh Layer Set the Layer property on all the Surface Mesh Colliders. Note that you need to set Layers for raycasts. When performing a raycast, you must indicate which Layers you want the ray intersection to test against. See Camera Raycast documentation for more information, and see Example script: SpatialSurface raycast, below.

By default, Unity assigns all GameObjects to the Default Layer. However, it is good practice to assign your GameObjects to a specific Layer. We recommend that you create a new layer specifically for Spatial Mapping Surfaces and assign this Layer to the Mesh Layer property.
Physic Material Specify which Physic Material to assign to each Surface GameObject’s Mesh Collider. The default is None (Physic Material).

A Physic Material specifies how other Rigidbody components should interact with it. For example, you might want to create a Surface that simulates ice, and therefore applies less friction to a GameObject moving on it.

The Spatial Mapping Collider component applies its Physic Material to all of the Mesh Colliders on its Surface GameObjects. See documentation on Physic Material for more information

General Settings

SpatialMapping 组件常规设置

以下是__空间映射渲染器 (Spatial Mapping Renderer)__ 和__空间映射碰撞体 (Spatial Mapping Collider)__ 组件的共同设置。

设置 描述
Surface Parent 选择希望由空间映射组件生成的表面游戏对象继承自的__表面父级 (Surface Parent)__ 游戏对象。将此设置保留为 None(Game Object) 可自动生成表面父游戏对象。
Freeze Updates 选中此复选框可停止组件向系统查询表面更改。

注意:__每个空间映射组件会定期在空间映射数据中查询物理空间中的表面更改。查询和烘焙表面的过程存在内存、性能和功耗方面的成本。对于希望基本为静态的环境,建议让用户在一段时间内观察环境而不更新表面游戏对象。

如果希望模拟中的环境在很大程度上为静态且不变(如棋盘游戏),可以在应用程序启动时预先扫描所需的表面数据,然后将
Freeze Updates__ 属性设置为 true 以防止进一步更新。这样可以略微提高性能并降低功耗。
Time Between Updates 指定对物理空间中的表面更改进行查询的间隔时间(以小数格式的秒为单位,如 3.7 或 4.6)。默认值为 2.5 秒。请注意,查询越频繁,内存、性能和功耗的成本就越高。
Removal Update Count 在系统删除表面游戏对象之前的更新次数。在此上下文中,可将一次更新视为一帧。默认为 10 次更新。

注意:__当空间映射通知组件表面游戏对象不再位于 SurfaceObserver 的包围体中时(即不再位于系统报告的规定区域中时),将开始进行删除更新倒计时。此设置允许您指定此事件之后在空间映射删除表面游戏对象之前应执行的更新次数。 | | Level of Detail| 组件生成的网格质量(__LowMediumHigh__)。默认质量为 Medium。质量越高,生成的碰撞体或渲染网格越精细和准确。使用较低质量的设置可以在性能和功耗方面降低成本。请参阅此表下的图了解三个 Level Of Detail__ 模式的示例。
Bounding Volume Type 组件的包围体区域形状,应用程序在其中接收空间映射数据。此设置可以是 SphereAxis Aligned Box。默认为 Axis Aligned Box

注意:__包围体是系统报告物理表面更改并限制空间映射网格范围的规定区域。| | Size In Meters| 组件使用的包围体的大小(以米为单位)。按半径配置 Sphere__;默认半径为 2 米。按面积配置 Axis Aligned Box__;默认值为 Vector3 (4,4,4)。

注意:__观察者的包围体是供空间映射报告物理表面更改的规定区域。

细节级别 (Level of Detail)

两个空间映射组件都允许为每个组件生成的空间网格指定三个细节级别之一(Low、Medium 或 High),如下图所示。

空间映射网格的三个细节级别模式
空间映射网格的三个细节级别模式

如果可能,请将 Spatial Mapping Colliders 的 Level of Detail 设置为 Low。这样将在计算碰撞交点时提高性能并降低功耗。但是,将 Level of Detail 设置为 Low 可能会对应用程序的视觉效果产生负面影响。

Using the Spatial Mapping Collider

Surface GameObjects

When you add a Spatial Mapping Collider component to a GameObject in your Scene, it generates its own separate set of Surface GameObjects during runtime. These appear in your Scene parented to a Surface Parent GameObject.

The Surface GameObjects generated by the Spatial Mapping Collider contain the following components:

Components of a Surface GameObject generated by a Spatial Mapping Collider
Components of a Surface GameObject generated by a Spatial Mapping Collider

If you add multiple Spatial Mapping Collider components to your Scene, each individual component generates its own set of Surface GameObjects. For example, if two GameObjects in your Scene contain a Spatial Mapping Collider then your Scene contains two sets of Surface GameObjects; one generated by each component at runtime. This is important to keep in mind for optimization purposes. s Note: When first generated by the Spatial Mapping system, all Surface GameObjects are assigned to the Default Layer. However, it is good practice to assign your GameObjects to a specific Layer, because raycasting is an expensive calculation to perform. By using Layers, you can filter which GameObjects you are doing your raycast calculations against, and so optimise performance. If you don’t have a lot of complicated Meshes on your Default Layer, then doing a raycast test for collision won’t have a large performance cost. However, it is best to organize your GameObjects into Layers to reduce the complexity of raycast tests when doing collisions. You can change the layer assigned to all Surface GameObjects generated by a specific Spatial Mapping Collider component by changing the Collider Settings > Mesh Layer property.

Example script: SpatialSurface raycast on a tap gesture event

The following example demonstrates how to raycast against GameObjects on the SpatialSurface Layer and use a GestureRecognizer to initiate this raycast upon detection of a tap gesture event.


using UnityEngine;
using System.Collections; 

//Required for Gesture Recognizer
using UnityEngine.XR.WSA.Input;


public class CustomLayerCollision : MonoBehaviour
{

    //use this to capture gesture events

    private GestureRecognizer GR_recognizer;

    void Start()
    {
        //Initialise the GestureRecognizer

        GR_recognizer = new GestureRecognizer();

        //Initiate gesture capture

        GR_recognizer.StartCapturingGestures();

        //Tell the GestureRecognizer which events to listen for

        GR_recognizer.SetRecognizableGestures(GestureSettings.Tap);

        //Subscribe the Tapped event to the DetectCollisions method

        //This makes this method be called any time the GestureRecognizer detects a tap event

        GR_recognizer.Tapped += DetectCollisions;

        //When the user initiates a tap gesture, the DetectCollisions   

        //method is called by the Tapped event delegate to do a collision test using Raycasting

        //See [GestureRecognizer API reference](../ScriptReference/XR.WSA.Input.GestureRecognizer.html) for more details
    }
    //Method to fire a raycast on a Tap event
    public void DetectCollisions(TappedEventArgs tapEvent)
    {
        // Raycast against all GameObjects that are on either the
        // spatial Surface or UI layers.

        int layerMask = 1 << LayerMask.NameToLayer("SpatialSurface");

        //construct a Ray using the position and forward direction of the User’s head
        Ray GazeRay = new Ray(tapEvent.headPose.position, tapEvent.headPose.forward);

        //Raycast using constructed Ray from Gaze and store collisions in array hits

        RaycastHit[] hits = Physics.RaycastAll(GazeRay, float.MaxValue, layerMask);

        if (hits.Length > 0)
        {
            foreach (RaycastHit hit in hits)
            {
                Debug.Log(string.Format("Hit Object **\"**{0}**\"** at position **\"**{1}**\"**", hit.collider.gameObject, hit.point));
            }
        }
        else
        {
            Debug.Log("Nothing was hit.");
        }
    }
}

See documentation on Layers and Raycasting from Camera for more information.

  • 2018–05–01 页面已发布并进行了编辑审查

  • Spatial Mapping for Hololens documentation updated in 2017.3

SpatialMapping 组件常规设置
空间映射渲染器 (Spatial Mapping Renderer)