练习 - 使用求解器创建智能对象放置

已完成

求解器是混合现实工具包 (MRTK) 的组件之一。 我们来了解一下两种类型的求解器及其各自的功能。

求解器在 MRTK 中的位置

MRTK 的求解器位于 MRTK SDK 文件夹中。 若要查看项目中可用的求解器,请在“项目”窗口中转到“包”>“MRTK 空间操作”>求解器

Screenshot that shows the location of the solvers folder.

方向指示器求解器

使用方向指示器求解器将用户定向到对象。 以下是操作方法:

  1. 在“项目”窗口中,转到“资产”>“MRTK.Tutorials.GettingStarted”>“Prefab”。 将 Chevron 预制件拖到“层次结构”窗口,并将其转换位置设置为 X = 0, Y = 1.5, Z = 2,以将其放置在 RoverExplorer 对象旁边。

    Screenshot that shows the newly added Chevron prefab selected.

  2. 将新添加的 Chevron 对象重命名为“Indicator”。 然后,在“检查器”窗口中,选择“添加组件”以添加“DirectionalIndicator”。

    Screenshot that shows the DirectionalIndicator solver component added.

  3. 按如下所示配置“DirectionalIndicator”和“SolverHandler”组件:

    a. 验证“SolverHandler”组件的“跟踪目标类型”是否设置为“头部”。

    b. 通过将“RoverExplorer”从“层次结构”拖到“无(转换)”字段,将其分配给“DirectionalIndicator”组件的“方向目标”。

    c. 将“视图偏移”更改为“0.2”。

    Screenshot that shows the DirectionalIndicator solver component configured.

  4. 要进入“游戏”模式,请按“播放”。 按住鼠标右键,同时向左或向右移动鼠标以旋转注视方向。

    请注意,当你将视线从 RoverExplorer 对象移开时,Indicator 对象将出现并指向 RoverExplorer 对象。

    Screenshot that shows the Play mode split view with DirectionalIndicator solver in use.

点击以放置求解器

使用点击以放置求解器来重新定位对象。 以下是操作方法:

  1. 在“层次结构”窗口中,选择“RoverExplorer”>“RoverAssembly”对象。 然后,在“检查器”中,使用“添加组件”添加“点击以放置”组件。 按如下所示配置组件:

    a. 验证“SolverHandler”组件的“跟踪目标类型”是否设置为“头部”。

    b. 取消选中“使用默认表面法线偏移”,并确保“表面法线偏移”设置为 0。

    c. 选中“保持方向垂直”复选框。

    Screenshot that shows the Tap To Place solver component added and configured.

  2. 接下来,使用“添加组件”来添加“有状态交互”组件。 在“检查器”窗口中,找到“On Clicked ()”事件。 选择“+”图标以添加新事件。 按如下所示配置事件:

    a. 通过将 RoverAssembly 对象从“层次结构”拖到“无(对象)”字段,将其指定为侦听器的“On Clicked ()”事件。

    b. 从“非功能”下拉列表中,选择“TapToPlace”>“StartPlacement()”,以便在触发事件时调用此方法。

    Screenshot that shows the Tap To Place On Placing Started event configured.

  3. 在“层次结构”窗口中的“RoverAssembly”对象仍处于选中状态的情况下,在“检查器”窗口中查找“On Placing Started ()”事件。 选择“+”图标以添加新事件。

    Screenshot that shows the Tap To Place On Placing Started event added.

  4. 按如下所示配置事件:

    a. 通过将“RoverAssembly”对象从“层次结构”拖到“无(对象)”字段,将其指定为侦听器的“On Placeing Started ()”事件。

    b. 从“无函数”下拉列表中,选择“TapToPlace”>float SurfaceNormalOffset”,以在触发事件时更新“SurfaceNormalOffset”属性值。

    c. 验证参数是否设置为“0”。

    Screenshot that shows the Tap To Place On Placing Started event configured.

  5. 在“层次结构”中,右键单击某个空白处。 选择“3D 对象”>“立方体”以创建一个代表地面的临时对象,并按如下所示配置“转换”组件:

    • 位置:X = 0, Y = 0.15, Z = 6
    • 旋转:X = 0, Y = 0, Z = 0
    • 缩放:X = 10, Y = 0.2, Z = 10

    Screenshot that shows the temporary ground Cube object added and positioned.

  6. 要进入“游戏”模式,请按“播放”。 然后按住鼠标右键,同时向下移动鼠标,直到看到“RoverAssembly”对象为止。

    Screenshot that shows the Play mode split view, with gaze hitting the RoverAssembly object.

  7. 按住空格键并单击鼠标左键进行隔空敲击,然后开始点击以放置过程。

    Screenshot that shows the Play mode split view, with placing started.

  8. 按住鼠标右键,同时向左或向右移动鼠标以旋转注视方向。 如果对放置感到满意,请按住空格键并单击鼠标左键,执行隔空敲击以结束放置。

    Screenshot that shows the Play mode split view, with placing ended.

  9. 在“游戏”模式下完成对该功能的测试后,右键单击 Cube 对象,然后选择“删除”将其从场景中删除。

    Screenshot that shows how to delete the temporary ground Cube.