introduction_to_3d.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. .. _doc_introduction_to_3d:
  2. Introduction to 3D
  3. ==================
  4. Creating a 3D game can be challenging. That extra Z coordinate makes
  5. many of the common techniques that helped to make 2D games simpler no
  6. longer work. To aid in this transition, it is worth mentioning that
  7. Godot uses similar APIs for 2D and 3D. Most nodes are the same and
  8. are present in both 2D and 3D versions. In fact, it is worth checking
  9. the 3D platformer tutorial, or the 3D kinematic character tutorials,
  10. which are almost identical to their 2D counterparts.
  11. In 3D, math is a little more complex than in 2D, so also checking the
  12. :ref:`doc_vector_math` entry in the wiki (which was especially created for game
  13. developers, not mathematicians or engineers) will help pave the way for you
  14. to develop 3D games efficiently.
  15. Node3D node
  16. ~~~~~~~~~~~
  17. :ref:`Node2D <class_Node2D>` is the base node for 2D.
  18. :ref:`Control <class_Control>` is the base node for everything GUI.
  19. Following this reasoning, the 3D engine uses the :ref:`Node3D <class_Node3D>`
  20. node for everything 3D.
  21. .. image:: img/tuto_3d1.webp
  22. Node3Ds have a local transform, which is relative to the parent
  23. node (as long as the parent node is also of **or inherits from** the type
  24. Node3D). This transform can be accessed as a 4×3
  25. :ref:`Transform3D <class_Transform3D>`, or as 3 :ref:`Vector3 <class_Vector3>`
  26. members representing location, Euler rotation (X, Y and Z angles) and
  27. scale.
  28. .. image:: img/tuto_3d2.webp
  29. 3D content
  30. ~~~~~~~~~~
  31. Unlike 2D, where loading image content and drawing is straightforward, 3D is a
  32. little more difficult. The content needs to be created with special 3D tools
  33. (also called Digital Content Creation tools, or DCCs) and exported to an
  34. exchange file format to be imported in Godot. This is required since 3D formats
  35. are not as standardized as images.
  36. Manually authored models (using 3D modeling software)
  37. -----------------------------------------------------
  38. .. FIXME: Needs update to properly description Godot 3.x workflow
  39. (used to reference a non existing doc_importing_3d_meshes importer).
  40. There are two pipelines to import 3D models in Godot. The first and most common
  41. one is by :ref:`doc_importing_3d_scenes`, which allows you to import entire
  42. scenes (exactly as they look in the 3D modeling software), including animation,
  43. skeletal rigs, blend shapes, etc.
  44. The second pipeline is by importing simple .OBJ files as mesh resources,
  45. which can be then put inside a :ref:`MeshInstance3D <class_MeshInstance3D>`
  46. node for display.
  47. Generated geometry
  48. ------------------
  49. It is possible to create custom geometry by using the
  50. :ref:`ArrayMesh <class_ArrayMesh>` resource directly. Simply create your arrays
  51. and use the :ref:`ArrayMesh.add_surface_from_arrays() <class_ArrayMesh_method_add_surface_from_arrays>`
  52. function. A helper class is also available, :ref:`SurfaceTool <class_SurfaceTool>`,
  53. which provides a more straightforward API and helpers for indexing,
  54. generating normals, tangents, etc.
  55. In any case, this method is meant for generating static geometry (models
  56. that will not be updated often), as creating vertex arrays and
  57. submitting them to the 3D API has a significant performance cost.
  58. Immediate geometry
  59. ------------------
  60. If, instead, you need to generate simple geometry that will be updated often,
  61. Godot provides a special :ref:`ImmediateMesh <class_ImmediateMesh>` resource
  62. that can be used in a :ref:`MeshInstance3D <class_MeshInstance3D>` node.
  63. This provides an OpenGL 1.x-style immediate-mode API to create points, lines,
  64. triangles, etc.
  65. 2D in 3D
  66. --------
  67. While Godot packs a powerful 2D engine, many types of games use 2D in a
  68. 3D environment. By using a fixed camera (either orthogonal or
  69. perspective) that does not rotate, nodes such as
  70. :ref:`Sprite3D <class_Sprite3D>` and
  71. :ref:`AnimatedSprite3D <class_AnimatedSprite3D>`
  72. can be used to create 2D games that take advantage of mixing with 3D
  73. backgrounds, more realistic parallax, lighting/shadow effects, etc.
  74. The disadvantage is, of course, that added complexity and reduced
  75. performance in comparison to plain 2D, as well as the lack of reference
  76. of working in pixels.
  77. Environment
  78. ~~~~~~~~~~~
  79. Besides editing a scene, it is often common to edit the environment.
  80. Godot provides a :ref:`WorldEnvironment <class_WorldEnvironment>`
  81. node that allows changing the background color, mode (as in, put a
  82. skybox), and applying several types of built-in post-processing effects.
  83. Environments can also be overridden in the Camera.
  84. 3D viewport
  85. ~~~~~~~~~~~
  86. Editing 3D scenes is done in the 3D tab. This tab can be selected
  87. manually, but it will be automatically enabled when a Node3D node is
  88. selected.
  89. .. image:: img/tuto_3d3.webp
  90. Default 3D scene navigation controls are similar to Blender (aiming to
  91. have some sort of consistency in the free software pipeline..), but
  92. options are included to customize mouse buttons and behavior to be
  93. similar to other tools in the Editor Settings:
  94. .. image:: img/tuto_3d4.webp
  95. Coordinate system
  96. -----------------
  97. Godot uses the `metric <https://en.wikipedia.org/wiki/Metric_system>`__
  98. system for everything in 3D, with 1 unit being equal to 1 meter.
  99. Physics and other areas are tuned for this scale. Therefore, attempting to use a
  100. different scale is usually a bad idea (unless you know what you are doing).
  101. When working with 3D assets, it's always best to work in the correct scale (set
  102. the unit to metric in your 3D modeling software). Godot allows scaling
  103. post-import and, while this works in most cases, in rare situations it may
  104. introduce floating-point precision issues (and thus, glitches or artifacts) in
  105. delicate areas such as rendering or physics. Make sure your artists always work
  106. in the right scale!
  107. The Y coordinate is used for "up". As for the horizontal X/Z axes, Godot uses a
  108. **right-handed** coordinate system. This means that for most objects that need
  109. alignment (such as lights or cameras), the Z axis is used as a "pointing
  110. towards" direction. This convention roughly means that:
  111. - **X** is sides
  112. - **Y** is up/down
  113. - **Z** is front/back
  114. See this chart for comparison with other 3D software:
  115. .. figure:: img/introduction_to_3d_coordinate_systems.webp
  116. :align: center
  117. :alt: 3D coordinate systems comparison chart
  118. Image by `Freya Holmér <https://twitter.com/FreyaHolmer>`__
  119. Space and manipulation gizmos
  120. -----------------------------
  121. Moving objects in the 3D view is done through the manipulator gizmos.
  122. Each axis is represented by a color: Red, Green, Blue represent X, Y, Z
  123. respectively. This convention applies to the grid and other gizmos too
  124. (and also to the shader language, ordering of components for
  125. Vector3, Color, etc.).
  126. .. image:: img/tuto_3d5.webp
  127. Some useful keybindings:
  128. - To snap placement or rotation, press :kbd:`Ctrl` while moving, scaling
  129. or rotating.
  130. - To center the view on the selected object, press :kbd:`F`.
  131. Using Blender-style transform shortcuts
  132. ---------------------------------------
  133. Since Godot 4.2, you can enable Blender-style shortcuts for translating,
  134. rotating and scaling nodes. In Blender, these shortcuts are:
  135. - :kbd:`G` for translating
  136. - :kbd:`R` for rotating
  137. - :kbd:`S` for scaling
  138. After pressing a shortcut key while focusing on the 3D editor viewport,
  139. move the mouse or enter a number to move the selected node(s) by the
  140. specified amount in 3D units. You can constrain movement to a specific
  141. axis by specifying the axis as a letter, then the distance (if entering a
  142. value with the keyboard).
  143. For instance, to move the selection upwards by 2.5 units, enter the
  144. following sequence in order (Y+ is upwards in Godot):
  145. :kbd:`G`-:kbd:`Y`-:kbd:`2`-:kbd:`.`-:kbd:`5`-:kbd:`Enter`
  146. To use Blender-style transform shortcuts in Godot, go to the Editor Settings'
  147. **Shortcuts** tab, then in the Spatial Editor section:
  148. - Bind **Begin Translate Transformation** to :kbd:`G`.
  149. - Bind **Begin Rotate Transformation** to :kbd:`R`.
  150. - Bind **Begin Scale Transformation** to :kbd:`S`.
  151. - Finally, unbind **Scale Mode** so that its shortcut won't conflict with
  152. **Begin Rotate Transformation**.
  153. View menu
  154. ---------
  155. The view options are controlled by the "View" menu in the viewport's toolbar.
  156. .. image:: img/tuto_3d6.webp
  157. You can hide the gizmos in the 3D view of the editor through this menu:
  158. .. image:: img/tuto_3d6_1.webp
  159. To hide a specific type of gizmos, you can toggle them off in the "View" menu.
  160. .. image:: img/tuto_3d6_2.webp
  161. preview environment and light
  162. -----------------------------
  163. By default, any 3D scene that doesn't have a :ref:`WorldEnvironment <class_WorldEnvironment>`
  164. node, or a :ref:`DirectionalLight3D <class_DirectionalLight3D>`, will have
  165. a preview turned on for what it's missing to light the scene.
  166. The preview light and environment will only be visible in the scene while
  167. in the editor. If you run the scene or export the project they will not
  168. affect the scene.
  169. The preview light and environment can be turned on or off from the top menu
  170. by clicking on their respective icon, and the 3 dots dropdown menu next to
  171. those icons can be used to adjust the properties of the preview environment
  172. and light.
  173. .. image:: img/tuto_3d8.webp
  174. The same preview sun and environment is used for every scene in the same project,
  175. So only make adjustments that would apply to all of the scenes you will need a preview
  176. light and environment for.
  177. Cameras
  178. -------
  179. No matter how many objects are placed in the 3D space, nothing will be
  180. displayed unless a :ref:`Camera3D <class_Camera3D>` is
  181. also added to the scene. Cameras can work in either orthogonal or
  182. perspective projections:
  183. .. image:: img/tuto_3d10.webp
  184. Cameras are associated with (and only display to) a parent or grandparent
  185. viewport. Since the root of the scene tree is a viewport, cameras will
  186. display on it by default, but if sub-viewports (either as render target
  187. or picture-in-picture) are desired, they need their own children cameras
  188. to display.
  189. .. image:: img/tuto_3d11.png
  190. When dealing with multiple cameras, the following rules are enforced for
  191. each viewport:
  192. - If no cameras are present in the scene tree, the first one that
  193. enters it will become the active camera. Further cameras entering the
  194. scene will be ignored (unless they are set as *current*).
  195. - If a camera has the "*current*" property set, it will be used
  196. regardless of any other camera in the scene. If the property is set,
  197. it will become active, replacing the previous camera.
  198. - If an active camera leaves the scene tree, the first camera in
  199. tree-order will take its place.
  200. Lights
  201. ------
  202. The background environment emits some ambient light which appears on surfaces.
  203. Still, without any light sources placed in the scene, the scene will appear
  204. quite dark unless the background environment is very bright.
  205. Most outdoor scenes have a directional light (the sun or moon), while indoor
  206. scenes typically have several positional lights (lamps, torches, …).
  207. See :ref:`doc_lights_and_shadows` for more information on setting up lights in Godot.