index.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. :allow_comments: False
  2. .. _doc_procedural_geometry:
  3. Procedural geometry
  4. ===================
  5. There are many ways to procedurally generate geometry in Godot. In this tutorial series,
  6. we will explore a few of them. Each technique has its own benefits and drawbacks, so
  7. it is best to understand each one and how it can be useful in a given situation.
  8. .. toctree::
  9. :maxdepth: 1
  10. :name: toc-procedural_geometry
  11. arraymesh
  12. meshdatatool
  13. surfacetool
  14. immediatemesh
  15. .. note::
  16. All the procedural geometry generation methods described here run on the
  17. CPU. Godot doesn't support generating geometry on the GPU yet.
  18. What is geometry?
  19. -----------------
  20. Geometry is a fancy way of saying shape. In computer graphics, geometry is typically represented
  21. by an array of positions called "vertices". In Godot, geometry is represented by Meshes.
  22. What is a Mesh?
  23. ---------------
  24. Many things in Godot have mesh in their name: the :ref:`Mesh <class_Mesh>`, the
  25. :ref:`ArrayMesh <class_ArrayMesh>`, the :ref:`ImmediateMesh
  26. <class_ImmediateMesh>`, the :ref:`MeshInstance3D <class_MeshInstance3D>`, the
  27. :ref:`MultiMesh <class_MultiMesh>`, and the :ref:`MultiMeshInstance3D
  28. <class_MultiMeshInstance3D>`. While they are all related, they have slightly
  29. different uses.
  30. Meshes and ArrayMeshes are resources that are drawn using a MeshInstance3D node. Resources like
  31. Meshes and ArrayMeshes cannot be added to the scene directly. A MeshInstance3D represents one
  32. instance of a mesh in your scene. You can reuse a single mesh in multiple MeshInstance3Ds
  33. to draw it in different parts of your scene with different materials or transformations (scale,
  34. rotation, position etc.).
  35. If you are going to draw the same object many times, it can be helpful to use a MultiMesh with
  36. a MultiMeshInstance3D. MultiMeshInstance3Ds draw meshes thousands of times very
  37. cheaply by taking advantage of hardware instancing. The drawback with
  38. using a MultiMeshInstance3D is that each of your mesh's surfaces are limited to one material for
  39. all instances. It uses an instance array to store different colors and transformations for each
  40. instance, but all the instances of each surface use the same material.
  41. What a Mesh is
  42. --------------
  43. A Mesh is composed of one or more surfaces. A surface is an array composed of multiple sub-arrays
  44. containing vertices, normals, UVs, etc. Normally the process of constructing surfaces and meshes is
  45. hidden from the user in the :ref:`RenderingServer <class_RenderingServer>`, but with ArrayMeshes, the user can construct a Mesh
  46. manually by passing in an array containing the surface information.
  47. Surfaces
  48. ^^^^^^^^
  49. Each surface has its own material. Alternatively, you can override the material for all surfaces
  50. in the Mesh when you use a MeshInstance3D using the :ref:`material_override <class_GeometryInstance3D_property_material_override>` property.
  51. Surface array
  52. ^^^^^^^^^^^^^
  53. The surface array is an array of length ``ArrayMesh.ARRAY_MAX``. Each position in the array is
  54. filled with a sub-array containing per-vertex information. For example, the array located at
  55. ``ArrayMesh.ARRAY_NORMAL`` is a :ref:`PackedVector3Array <class_PackedVector3Array>` of vertex normals.
  56. See :ref:`Mesh.ArrayType <enum_Mesh_ArrayType>` for more information.
  57. The surface array can be indexed or non-indexed. Creating a non-indexed array is as easy as not assigning
  58. an array at the index ``ArrayMesh.ARRAY_INDEX``. A non-indexed array stores unique vertex information for
  59. every triangle, meaning that when two triangles share a vertex, the vertex is duplicated in the array. An
  60. indexed surface array only stores vertex information for each unique vertex and then also stores an array
  61. of indices which maps out how to construct the triangles from the vertex array. In general, using an indexed
  62. array is faster, but it means you have to share vertex data between triangles, which is not always desired
  63. (e.g. when you want per-face normals).
  64. Tools
  65. -----
  66. Godot provides different ways of accessing and working with geometry. More information on each will
  67. be provided in the following tutorials.
  68. ArrayMesh
  69. ^^^^^^^^^
  70. The ArrayMesh resource extends Mesh to add a few different quality of life functions and, most
  71. importantly, the ability to construct a Mesh surface through scripting.
  72. For more information about the ArrayMesh, please see the :ref:`ArrayMesh tutorial <doc_arraymesh>`.
  73. MeshDataTool
  74. ^^^^^^^^^^^^
  75. The MeshDataTool is a resource that converts Mesh data into arrays of vertices, faces, and edges that can
  76. be modified at runtime.
  77. For more information about the MeshDataTool, please see the :ref:`MeshDataTool tutorial <doc_meshdatatool>`.
  78. SurfaceTool
  79. ^^^^^^^^^^^
  80. The SurfaceTool allows the creation of Meshes using an OpenGL 1.x immediate mode style interface.
  81. For more information about the SurfaceTool, please see the :ref:`SurfaceTool tutorial <doc_surfacetool>`.
  82. ImmediateMesh
  83. ^^^^^^^^^^^^^
  84. ImmediateMesh is a mesh that uses an immediate mode style interface (like
  85. SurfaceTool) to draw objects. The difference between ImmediateMesh and the
  86. SurfaceTool is that ImmediateMesh is drawn directly with code dynamically, while
  87. the SurfaceTool is used to generate a Mesh that you can do whatever you want
  88. with.
  89. ImmediateMesh is useful for prototyping because of its straightforward API, but
  90. it is slow because the geometry is rebuilt each time you make a change. It is
  91. most useful for adding simple geometry for visual debugging (e.g. by drawing
  92. lines to visualize physics raycasts etc.).
  93. For more information about ImmediateMesh, please see the :ref:`ImmediateMesh tutorial <doc_immediatemesh>`.
  94. Which one should I use?
  95. -----------------------
  96. Which approach you use depends on what you are trying to do and what kind of procedure you are comfortable with.
  97. Both SurfaceTool and ArrayMesh are excellent for generating static geometry (meshes) that don't change over time.
  98. Using an ArrayMesh is slightly faster than using a SurfaceTool, but the API is a little more challenging.
  99. Additionally, SurfaceTool has a few quality of life methods such as ``generate_normals()`` and ``index()``.
  100. ImmediateMesh is more limited than both ArrayMesh and SurfaceTool. However, if
  101. you need the geometry to change every frame anyway, it provides a much easier
  102. interface that can be slightly faster than generating an ArrayMesh every frame.
  103. The MeshDataTool is not fast, but it gives you access to all kinds of properties of the mesh that you don't get with the others
  104. (edges, faces, etc.). It is incredibly useful when you need that sort of data to transform the mesh, but it is not a good idea
  105. to use it if that extra information is not needed. The MeshDataTool is best used if you are going to be using an algorithm that requires
  106. access to the face or edge array.