using_gridmaps.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. .. _doc_using_gridmaps:
  2. Using GridMaps
  3. ==============
  4. Introduction
  5. ------------
  6. :ref:`Gridmaps <class_GridMap>` are a tool for creating 3D
  7. game levels, similar to the way :ref:`TileMap <doc_using_tilemaps>`
  8. works in 2D. You start with a predefined collection of 3D meshes (a
  9. :ref:`class_MeshLibrary`) that can be placed on a grid,
  10. as if you were building a level with an unlimited amount of Lego blocks.
  11. Collisions and navigation can also be added to the meshes, just like you
  12. would do with the tiles of a tilemap.
  13. Example project
  14. ---------------
  15. To learn how GridMaps work, start by downloading the sample project:
  16. `gridmap_starter.zip <https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/gridmap_starter.zip>`_.
  17. Unzip this project and add it to the Project Manager using the "Import"
  18. button. You may get a popup saying that it needs to be converted to a newer Godot
  19. version, click **Convert project.godot**.
  20. Creating a MeshLibrary
  21. ----------------------
  22. To begin, you need a :ref:`class_MeshLibrary`, which is a collection
  23. of individual meshes that can be used in the gridmap. Open the "mesh_library_source.tscn"
  24. scene to see an example of how to set up the mesh library.
  25. .. image:: img/gridmap_meshlibrary1.webp
  26. As you can see, this scene has a :ref:`class_Node3D` node as its root, and
  27. a number of :ref:`class_MeshInstance3D` node children.
  28. If you don't need any physics in your scene, then you're done. However, in most
  29. cases you'll want to assign collision bodies to the meshes.
  30. Collisions
  31. ----------
  32. You can manually assign a :ref:`class_StaticBody3D` and
  33. :ref:`class_CollisionShape3D` to each mesh. Alternatively, you can use the "Mesh" menu
  34. to automatically create the collision body based on the mesh data.
  35. .. image:: img/gridmap_create_body.webp
  36. Note that a "Convex" collision body will work better for simple meshes. For more
  37. complex shapes, select "Create Trimesh Static Body". Once each mesh has
  38. a physics body and collision shape assigned, your mesh library is ready to
  39. be used.
  40. .. image:: img/gridmap_mesh_scene.webp
  41. Materials
  42. ---------
  43. Only the materials from within the meshes are used when generating the mesh
  44. library. Materials set on the node will be ignored.
  45. NavigationMeshes
  46. ----------------
  47. Like all mesh instances, MeshLibrary items can be assigned a :ref:`class_NavigationMesh`
  48. resource, which can be created manually, or baked as described below.
  49. To create the NavigationMesh from a MeshLibrary scene export, place a
  50. :ref:`class_NavigationRegion3D` child node below the main MeshInstance3D for the GridMap
  51. item. Add a valid NavigationMesh resource to the NavigationRegion3D and some source
  52. geometry nodes below and bake the NavigationMesh.
  53. .. note::
  54. With small grid cells it is often necessary to reduce the NavigationMesh properties
  55. for agent radius and region minimum size.
  56. .. image:: img/meshlibrary_scene.png
  57. Nodes below the NavigationRegion3D are ignored for the MeshLibrary scene export, so
  58. additional nodes can be added as source geometry just for baking the navmesh.
  59. .. warning::
  60. The baked cell size of the NavigationMesh must match the NavigationServer map cell
  61. size to properly merge the navigation meshes of different grid cells.
  62. MeshLibrary format
  63. ------------------
  64. To summarize the specific constraints of the MeshLibrary format, a MeshLibrary
  65. scene has a Node3D as the root node, and several child nodes which will become
  66. MeshLibrary items. Each child of the root node should:
  67. - Be a :ref:`class_MeshInstance3D`, which will become the MeshLibrary item. Only
  68. this visual mesh will be exported.
  69. - Have a material, in the mesh's material slot, *not* the MeshInstance3D's
  70. material slots.
  71. - Have up to one :ref:`class_StaticBody3D` child, for collision. The
  72. StaticBody3D should have one or more :ref:`class_CollisionShape3D` children.
  73. - Have up to one :ref:`class_NavigationRegion3D` child, for navigation. The
  74. NavigationRegion3D can have one or more additional :ref:`class_MeshInstance3D`
  75. children, which can be baked for navigation, but won't be exported as a visual
  76. mesh.
  77. Only this specific format is recognized. Other node types placed as children
  78. will not be recognized and exported. GridMap is not a general-purpose system for
  79. placing *nodes* on a grid, but rather a specific, optimized system, designed to
  80. place *meshes* with collisions and navigation.
  81. Exporting the MeshLibrary
  82. -------------------------
  83. To export the library, click on **Scene > Export As... > MeshLibrary...**, and save it
  84. as a resource.
  85. .. image:: img/gridmap_export.webp
  86. You can find an already exported MeshLibrary in the project named "MeshLibrary.tres".
  87. Using GridMap
  88. -------------
  89. Create a new scene and add a GridMap node. Add the mesh library by dragging
  90. the resource file from the FileSystem dock and dropping it in the **Mesh Library**
  91. property in the Inspector.
  92. .. image:: img/gridmap_mesh_library_inspector.webp
  93. Inspector properties
  94. ~~~~~~~~~~~~~~~~~~~~
  95. The **Physics Material** setting allows you to override the physics material for
  96. every mesh in the NavigationMesh.
  97. Under **Cells**, the **Size** property should be set to the size of your meshes. You
  98. can leave it at the default value for the demo. Uncheck the **Center Y** property.
  99. The **Collision** options allow you to set the collision layer, collision mask, and
  100. priority for the entire grid. For more information on how those work see the
  101. :ref:`doc_physics_index` section.
  102. Under **Navigation** is the "Bake Navigation" option. If enabled it creates a
  103. navigation region for each cell that uses a mesh library item with a navigation
  104. mesh.
  105. If you click on the MeshLibrary itself in the inspector you can adjust settings for
  106. individual meshes, such as their navigation mesh, navigation layers, or if the mesh
  107. casts shadows.
  108. .. image:: img/gridmap_mesh_library_settings.webp
  109. GridMap panel
  110. ~~~~~~~~~~~~~
  111. At the bottom of the editor is the GridMap panel, which should have opened
  112. automatically when you added the GridMap node.
  113. .. image:: img/gridmap_panel.webp
  114. From left to right in the toolbar:
  115. - **Transform**: Adds a gizmo to the scene that allows you to change the
  116. relative position and rotation of the gridmap in the scene.
  117. - **Selection**: While active you can select an area in the viewport, click and drag
  118. to select more than one space on the grid.
  119. - **Erase**: While active, click in the viewport and delete meshes.
  120. - **Paint**: While active, click in the viewport and add whatever mesh is currently
  121. selected in the GridMap panel to the scene.
  122. - **Pick**: While active, clicking on a gridmap mesh in the viewport will cause
  123. it to be selected in the GridMap panel.
  124. - **Fill**: Fill the area that has been selected in the viewport with whatever mesh
  125. is selected in the GridMap bottom panel.
  126. - **Move**: Move whatever mesh or meshes are currently selected in the viewport.
  127. - **Duplicate**: Create a copy of whatever the selected mesh or meshes in the
  128. GridMap are.
  129. - **Delete**: Similar to erase, but for the entire selected area.
  130. - **Cursor Rotate X**: While the paint tool is selected, this will rotate the mesh
  131. that will be painted on the X-axis. This will also rotate selected areas if they
  132. are being moved.
  133. - **Cursor Rotate Y**: While the paint tool is selected, this will rotate the mesh
  134. that will be painted on the Y-axis. This will also rotate selected areas if they
  135. are being moved.
  136. - **Cursor Rotate Z**: While the paint tool is selected, this will rotate the mesh
  137. that will be painted on the Z-axis. This will also rotate selected areas if they
  138. are being moved.
  139. - **Change Grid Floor**: Adjusts what floor is currently being worked on, can be
  140. changed with the arrows or typing
  141. - **Filter Meshes**: Used to search for a specific mesh in the bottom panel.
  142. - **Zoom**: Controls the zoom level on meshes in the bottom panel.
  143. - **Layout toggles**: These two buttons toggle between different layouts for meshes
  144. in the bottom panel.
  145. - **Tools dropdown**: This button opens a dropdown menu with a few more options.
  146. .. image:: img/gridmap_dropdown.webp
  147. Clicking on **Settings** in that dropdown brings up a window that allows you to
  148. change the **Pick Distance**, which is the maximum distance at which tiles can be placed
  149. on a GridMap, relative to the camera position (in meters).
  150. Using GridMap in code
  151. ---------------------
  152. See :ref:`class_GridMap` for details on the node's methods and member variables.