using_gridmaps.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.
  19. Creating a MeshLibrary
  20. ----------------------
  21. To begin, you need a :ref:`class_MeshLibrary`, which is a collection
  22. of individual meshes that can be used in the gridmap. Open the "mesh_library_source.tscn"
  23. scene to see an example of how to set up the mesh library.
  24. .. image:: img/gridmap_meshlibrary1.png
  25. As you can see, this scene has a :ref:`class_Node3D` node as its root, and
  26. a number of :ref:`class_MeshInstance3D` node children.
  27. If you don't need any physics in your scene, then you're done. However, in most
  28. cases you'll want to assign collision bodies to the meshes.
  29. Collisions
  30. ----------
  31. You can manually assign a :ref:`class_StaticBody3D` and
  32. :ref:`class_CollisionShape3D` to each mesh. Alternatively, you can use the "Mesh" menu
  33. to automatically create the collision body based on the mesh data.
  34. .. image:: img/gridmap_create_body.png
  35. Note that a "Convex" collision body will work better for simple meshes. For more
  36. complex shapes, select "Create Trimesh Static Body". Once each mesh has
  37. a physics body and collision shape assigned, your mesh library is ready to
  38. be used.
  39. .. image:: img/gridmap_mesh_scene.png
  40. Materials
  41. ---------
  42. Only the materials from within the meshes are used when generating the mesh
  43. library. Materials set on the node will be ignored.
  44. NavigationMeshes
  45. ----------------
  46. Like all mesh instances, MeshLibrary items can be assigned a :ref:`class_NavigationMesh`
  47. resource, which can be created manually, or baked as described below.
  48. To create the NavigationMesh from a MeshLibrary scene export, place a
  49. :ref:`class_NavigationRegion3D` child node below the main MeshInstance3D for the GridMap
  50. item. Add a valid NavigationMesh resource to the NavigationRegion3D and some source
  51. geometry nodes below and bake the NavigationMesh.
  52. .. note::
  53. With small grid cells it is often necessary to reduce the NavigationMesh properties
  54. for agent radius and region minimum size.
  55. .. image:: img/meshlibrary_scene.png
  56. Nodes below the NavigationRegion3D are ignored for the MeshLibrary scene export, so
  57. additional nodes can be added as source geometry just for baking the navmesh.
  58. .. warning::
  59. The baked cell size of the NavigationMesh must match the NavigationServer map cell
  60. size to properly merge the navigation meshes of different grid cells.
  61. MeshLibrary format
  62. ------------------
  63. To summarize the specific constraints of the MeshLibrary format, a MeshLibrary
  64. scene has a Node3D as the root node, and several child nodes which will become
  65. MeshLibrary items. Each child of the root node should:
  66. - Be a :ref:`class_MeshInstance3D`, which will become the MeshLibrary item. Only
  67. this visual mesh will be exported.
  68. - Have a material, in the mesh's material slot, *not* the MeshInstance3D's
  69. material slots.
  70. - Have up to one :ref:`class_StaticBody3D` child, for collision. The
  71. StaticBody3D should have one or more :ref:`class_CollisionShape3D` children.
  72. - Have up to one :ref:`class_NavigationRegion3D` child, for navigation. The
  73. NavigationRegion3D can have one or more additional :ref:`class_MeshInstance3D`
  74. children, which can be baked for navigation, but won't be exported as a visual
  75. mesh.
  76. Only this specific format is recognized. Other node types placed as children
  77. will not be recognized and exported. GridMap is not a general-purpose system for
  78. placing *nodes* on a grid, but rather a specific, optimized system, designed to
  79. place *meshes* with collisions and navigation.
  80. Exporting the MeshLibrary
  81. -------------------------
  82. To export the library, click on **Scene > Export As... > MeshLibrary...**, and save it
  83. as a resource.
  84. .. image:: img/gridmap_export.png
  85. You can find an already exported MeshLibrary in the project named "MeshLibrary.tres".
  86. Using GridMap
  87. -------------
  88. Create a new scene and add a GridMap node. Add the mesh library by dragging
  89. the resource file from the FileSystem dock and dropping it in the "Theme" property
  90. in the Inspector.
  91. .. image:: img/gridmap_main.png
  92. The "Cell/Size" property should be set to the size of your meshes. You can leave
  93. it at the default value for the demo. Set the "Center Y" property to "Off".
  94. Now you can start designing the level by choosing a tile from the palette and
  95. placing it with Left-Click in the editor window. Use Right-click to remove a tile.
  96. Use the arrows next to the "GridMap" menu to change the floor that you are working on.
  97. Click on the "GridMap" menu to see options and shortcuts. For example, pressing
  98. :kbd:`S` rotates a tile around the y-axis.
  99. .. image:: img/gridmap_menu.png
  100. Holding :kbd:`Shift` and dragging with the left mouse button will draw a selection
  101. box. You can duplicate or clear the selected area using the respective menu
  102. options.
  103. .. image:: img/gridmap_select.png
  104. In the menu, you can also change the axis you're drawing on, as well as shift
  105. the drawing plane higher or lower on its axis.
  106. .. image:: img/gridmap_shift_axis.png
  107. Using GridMap in code
  108. ---------------------
  109. See :ref:`class_GridMap` for details on the node's methods and member variables.