surfacetool.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. .. _doc_surfacetool:
  2. Using the SurfaceTool
  3. =====================
  4. The :ref:`SurfaceTool <class_surfacetool>` provides a useful interface for constructing geometry.
  5. The interface is similar to the :ref:`ImmediateMesh <class_ImmediateMesh>` class. You
  6. set each per-vertex attribute (e.g. normal, uv, color) and then when you add a vertex it
  7. captures the attributes.
  8. The SurfaceTool also provides some useful helper functions like ``index()`` and ``generate_normals()``.
  9. Attributes are added before each vertex is added:
  10. .. tabs::
  11. .. code-tab:: gdscript GDScript
  12. st.set_normal() # Overwritten by normal below.
  13. st.set_normal() # Added to next vertex.
  14. st.set_color() # Added to next vertex.
  15. st.add_vertex() # Captures normal and color above.
  16. st.set_normal() # Normal never added to a vertex.
  17. .. code-tab:: csharp
  18. st.SetNormal(); // Overwritten by normal below.
  19. st.SetNormal(); // Added to next vertex.
  20. st.SetColor(); // Added to next vertex.
  21. st.AddVertex(); // Captures normal and color above.
  22. st.SetNormal(); // Normal never added to a vertex.
  23. When finished generating your geometry with the :ref:`SurfaceTool <class_surfacetool>`
  24. call ``commit()`` to finish generating the mesh. If an :ref:`ArrayMesh <class_ArrayMesh>` is passed
  25. to ``commit()`` then it appends a new surface to the end of the ArrayMesh. While if nothing is passed
  26. in, ``commit()`` returns an ArrayMesh.
  27. .. tabs::
  28. .. code-tab:: gdscript GDScript
  29. st.commit(mesh)
  30. # Or:
  31. var mesh = st.commit()
  32. .. code-tab:: csharp
  33. st.Commit(mesh);
  34. // Or:
  35. var mesh = st.Commit();
  36. Code creates a triangle with indices
  37. .. tabs::
  38. .. code-tab:: gdscript GDScript
  39. var st = SurfaceTool.new()
  40. st.begin(Mesh.PRIMITIVE_TRIANGLES)
  41. # Prepare attributes for add_vertex.
  42. st.set_normal(Vector3(0, 0, 1))
  43. st.set_uv(Vector2(0, 0))
  44. # Call last for each vertex, adds the above attributes.
  45. st.add_vertex(Vector3(-1, -1, 0))
  46. st.set_normal(Vector3(0, 0, 1))
  47. st.set_uv(Vector2(0, 1))
  48. st.add_vertex(Vector3(-1, 1, 0))
  49. st.set_normal(Vector3(0, 0, 1))
  50. st.set_uv(Vector2(1, 1))
  51. st.add_vertex(Vector3(1, 1, 0))
  52. # Commit to a mesh.
  53. var mesh = st.commit()
  54. .. code-tab:: csharp
  55. var st = new SurfaceTool();
  56. st.Begin(Mesh.PrimitiveType.Triangles);
  57. // Prepare attributes for AddVertex.
  58. st.SetNormal(new Vector3(0, 0, 1));
  59. st.SetUV(new Vector2(0, 0));
  60. // Call last for each vertex, adds the above attributes.
  61. st.AddVertex(new Vector3(-1, -1, 0));
  62. st.SetNormal(new Vector3(0, 0, 1));
  63. st.SetUV(new Vector2(0, 1));
  64. st.AddVertex(new Vector3(-1, 1, 0));
  65. st.SetNormal(new Vector3(0, 0, 1));
  66. st.SetUV(new Vector2(1, 1));
  67. st.AddVertex(new Vector3(1, 1, 0));
  68. // Commit to a mesh.
  69. var mesh = st.Commit();
  70. You can optionally add an index array, either by calling ``add_index()`` and adding
  71. vertices to the index array or by calling ``index()`` which shrinks the vertex array
  72. to remove duplicate vertices.
  73. .. tabs::
  74. .. code-tab:: gdscript GDScript
  75. # Creates a quad from four corner vertices.
  76. # add_index does not need to be called before add_vertex.
  77. st.add_index(0)
  78. st.add_index(1)
  79. st.add_index(2)
  80. st.add_index(1)
  81. st.add_index(3)
  82. st.add_index(2)
  83. # Alternatively:
  84. st.index()
  85. .. code-tab:: csharp
  86. // Creates a quad from four corner vertices.
  87. // AddIndex does not need to be called before AddVertex.
  88. st.AddIndex(0);
  89. st.AddIndex(1);
  90. st.AddIndex(2);
  91. st.AddIndex(1);
  92. st.AddIndex(3);
  93. st.AddIndex(2);
  94. // Alternatively:
  95. st.Index();
  96. Similarly, if you have an index array, but you want each vertex to be unique (e.g. because
  97. you want to use unique normals or colors per face instead of per-vertex), you can call ``deindex()``.
  98. .. tabs::
  99. .. code-tab:: gdscript GDScript
  100. st.deindex()
  101. .. code-tab:: csharp
  102. st.Deindex();
  103. If you don't add custom normals yourself, you can add them using ``generate_normals()``, which should
  104. be called after generating geometry and before committing the mesh using ``commit()`` or
  105. ``commit_to_arrays()``. Calling ``generate_normals(true)`` will flip the resulting normals. As a side
  106. note, ``generate_normals()`` only works if the primitive type is set to ``Mesh.PRIMITIVE_TRIANGLES``.
  107. You may notice that normal mapping or other material properties look broken on
  108. the generated mesh. This is because normal mapping **requires** the mesh to
  109. feature *tangents*, which are separate from *normals*. You can either add custom
  110. tangents manually, or generate them automatically with
  111. ``generate_tangents()``. This method requires that each vertex have UVs and
  112. normals set already.
  113. .. tabs::
  114. .. code-tab:: gdscript GDScript
  115. st.generate_normals()
  116. st.generate_tangents()
  117. .. code-tab:: csharp
  118. st.GenerateNormals();
  119. st.GenerateTangents();
  120. By default, when generating normals, they will be calculated on a per-face basis. If you want
  121. smooth vertex normals, when adding vertices, call ``add_smooth_group()``. ``add_smooth_group()``
  122. needs to be called while building the geometry, e.g. before the call to ``add_vertex()``
  123. (if non-indexed) or ``add_index()`` (if indexed).