使用网格地图

简介

Gridmaps 是一种用于创建3D游戏关卡的工具,方式类似于2D中使用 TileMap 。使用一个定义好的可以放在一个网格中的 3D mesh (a MeshLibrary) (译注,grid和mesh都译作网格,这里为了清晰,mesh直接使用英文,grid则译作网格)对象集合搭建关卡,就像您使用无限多数量的乐高积木搭建世界一样。

就像使用tilemap的tile一样,碰撞和导航也可以添加到网格中。

示例项目

要学习GridMaps是怎么样工作的,请先下载示例项目: gridmap_demo.zip

解压缩此项目并使用``导入``按钮将其添加到项目管理器。

创建Mesh库

首先,您需要一个 MeshLibrary,它是可以在gridmap中使用的Mesh的集合。 打开``MeshLibrary_Source.tscn``场景以查看如何设置网格库的示例。

../../_images/gridmap_meshlibrary1.png

可以看到,这个场景有一个 Spatial 节点作为它的根,以及一些 MeshInstance 子节点。

如果您的场景中不需要任何物理,那么到这里就完成了。 但是,在大多数情况下,您还需要给网格指定碰撞体。

碰撞

您可以手动为每个Mesh指定 StaticBodyCollisionShape。或者,您可以使用``网格(Mesh)``菜单根据Mesh数据自动创建碰撞体。

../../_images/gridmap_create_body.png

请注意,凸(Convex)``碰撞体对于简单Mesh效果更好。 对于更复杂的形状,请选择``创建Trimesh静态体。 一旦每个网格都分配了物理主体和碰撞形状,就可以使用Mesh库了。

../../_images/gridmap_mesh_scene.png

材质

在生成网格库时,只使用网格内的材质。节点上设置的材质将被忽略。

导出Mesh库

要导出库,请单击``场景`` ->``转换为…`` ->``MeshLibrary(网格库)…``,并将其另存为资源。

../../_images/gridmap_export.png

可以在项目中找到已导出的名为“MeshLibrary.tres”的Mesh库。

使用网格图(GridMap)

创建一个新场景并添加一个GridMap节点。 通过从``文件系统(FileSystem)``扩展面板拖动资源文件并将其放入Inspector中的``Theme(主题)``属性中来添加Mesh库。

../../_images/gridmap_main.png

单元格/大小(Cell/Size)``属性应设置为Mesh的大小。 在本示例中您可以使用默认值。 将``Center Y``属性设置为``Off

Now you can start designing the level by choosing a tile from the palette and placing it with Left-Click in the editor window. To remove a tile, hold Shift and use Right-click.

Click on the “GridMap” menu to see options and shortcuts. For example, pressing S rotates a tile around the y-axis.

../../_images/gridmap_menu.png

Holding Shift and dragging with the left mouse button will draw a selection box. You can duplicate or clear the selected area using the respective menu options.

../../_images/gridmap_select.png

In the menu, you can also change the axis you’re drawing on, as well as shift the drawing plane higher or lower on its axis.

../../_images/gridmap_shift_axis.png

在代码中使用GridMap

有关节点方法和成员变量的详细信息,请参阅 GridMap