index.rst 6.3 KB

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