使用 GridMap

前言

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

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

示例项目

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

Unzip this project and add it to the Project Manager using the "Import" button. You may get a popup saying that it needs to be converted to a newer Godot version, click Convert project.godot.

创建 MeshLibrary

首先,你需要一个 MeshLibrary,它是可以在网格地图中使用的 Mesh 的集合。打开“mesh_library_source.tscn”场景以查看如何设置网格库的示例。

../../_images/gridmap_meshlibrary1.webp

如你所见,该场景有一个 Node3D 节点作为其根节点,以及许多 MeshInstance3D 节点子节点。

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

碰撞

你可以手动为每个 Mesh 指定 StaticBody3DCollisionShape3D。或者,你可以使用“网格”菜单根据 Mesh 数据自动创建碰撞体。

../../_images/gridmap_create_body.webp

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

../../_images/gridmap_mesh_scene.webp

材质

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

MeshLibrary format

To summarize the specific constraints of the MeshLibrary format, a MeshLibrary scene has a Node3D as the root node, and several child nodes which will become MeshLibrary items. Each child of the root node should:

  • Be a MeshInstance3D, which will become the MeshLibrary item. Only this visual mesh will be exported.

  • Have a material, in the mesh's material slot, not the MeshInstance3D's material slots.

  • Have up to one StaticBody3D child, for collision. The StaticBody3D should have one or more CollisionShape3D children.

  • Have up to one NavigationRegion3D child, for navigation. The NavigationRegion3D can have one or more additional MeshInstance3D children, which can be baked for navigation, but won't be exported as a visual mesh.

Only this specific format is recognized. Other node types placed as children will not be recognized and exported. GridMap is not a general-purpose system for placing nodes on a grid, but rather a specific, optimized system, designed to place meshes with collisions and navigation.

导出 MeshLibrary

要导出库,点击 场景 > 导出为.. > MeshLibrary... ,并将其保存为资源。

../../_images/gridmap_export.webp

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

使用 GridMap

Create a new scene and add a GridMap node. Add the mesh library by dragging the resource file from the FileSystem dock and dropping it in the Mesh Library property in the Inspector.

../../_images/gridmap_mesh_library_inspector.webp

Inspector properties

The Physics Material setting allows you to override the physics material for every mesh in the NavigationMesh.

Under Cells, the Size property should be set to the size of your meshes. You can leave it at the default value for the demo. Uncheck the Center Y property.

The Collision options allow you to set the collision layer, collision mask, and priority for the entire grid. For more information on how those work see the 物理 section.

Under Navigation is the "Bake Navigation" option. If enabled it creates a navigation region for each cell that uses a mesh library item with a navigation mesh.

If you click on the MeshLibrary itself in the inspector you can adjust settings for individual meshes, such as their navigation mesh, navigation layers, or if the mesh casts shadows.

../../_images/gridmap_mesh_library_settings.webp

GridMap panel

At the bottom of the editor is the GridMap panel, which should have opened automatically when you added the GridMap node.

../../_images/gridmap_panel.webp

From left to right in the toolbar:

  • Transform: Adds a gizmo to the scene that allows you to change the relative position and rotation of the gridmap in the scene.

  • Selection: While active you can select an area in the viewport, click and drag to select more than one space on the grid.

  • Erase: While active, click in the viewport and delete meshes.

  • Paint: While active, click in the viewport and add whatever mesh is currently selected in the GridMap panel to the scene.

  • Pick: While active, clicking on a gridmap mesh in the viewport will cause it to be selected in the GridMap panel.

  • Fill: Fill the area that has been selected in the viewport with whatever mesh is selected in the GridMap bottom panel.

  • Move: Move whatever mesh or meshes are currently selected in the viewport.

  • Duplicate: Create a copy of whatever the selected mesh or meshes in the GridMap are.

  • Delete: Similar to erase, but for the entire selected area.

  • Cursor Rotate X: While the paint tool is selected, this will rotate the mesh that will be painted on the X-axis. This will also rotate selected areas if they are being moved.

  • Cursor Rotate Y: While the paint tool is selected, this will rotate the mesh that will be painted on the Y-axis. This will also rotate selected areas if they are being moved.

  • Cursor Rotate Z: While the paint tool is selected, this will rotate the mesh that will be painted on the Z-axis. This will also rotate selected areas if they are being moved.

  • Change Grid Floor: Adjusts what floor is currently being worked on, can be changed with the arrows or typing

  • Filter Meshes: Used to search for a specific mesh in the bottom panel.

  • Zoom: Controls the zoom level on meshes in the bottom panel.

  • Layout toggles: These two buttons toggle between different layouts for meshes in the bottom panel.

  • Tools dropdown: This button opens a dropdown menu with a few more options.

../../_images/gridmap_dropdown.webp

Clicking on Settings in that dropdown brings up a window that allows you to change the Pick Distance, which is the maximum distance at which tiles can be placed on a GridMap, relative to the camera position (in meters).

在代码中使用 GridMap

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