introduction_to_3d.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. .. figure:: img/godot-tps-demo.webp
  12. :align: center
  13. :alt: An example 3D game demo created using Godot
  14. Godot Third Person Shooter (TPS) Demo, available on the
  15. `Github repository <https://github.com/godotengine/tps-demo>`__ or the
  16. :ref:`Asset Library <doc_project_manager_downloading_demos>`.
  17. In 3D, math is a little more complex than in 2D, so also checking the
  18. :ref:`doc_vector_math` entry in the wiki (which was especially created for game
  19. developers, not mathematicians or engineers) will help pave the way for you
  20. to develop 3D games efficiently.
  21. 3D workspace
  22. ~~~~~~~~~~~~
  23. Editing 3D scenes is done in the 3D workspace. This workspace can be selected
  24. manually, but it will be automatically selected when a Node3D node is
  25. selected.
  26. .. image:: img/tuto_3d3.webp
  27. Similar to 2D, the tabs below the workspace selector are used to change between
  28. currently opened scenes or create a new one using the plus (+) button. The left and
  29. right docks should be familiar from :ref:`editor introduction <doc_editor_introduction>`.
  30. Below the scene selector, the main toolbar is visible, and beneath the main toolbar
  31. is the 3D viewport.
  32. Main toolbar
  33. ------------
  34. Some buttons in the main toolbar are the same as those in the 2D workspace. A brief explanation
  35. is given with the shortcut if the mouse cursor is hovered over a button for one second.
  36. Some buttons may have additional functionality if another keypress is performed. A recap
  37. of main functionality of each button with its default shortcut is provided below from
  38. left to right:
  39. .. image:: img/3d_toolbar.webp
  40. - **Select Mode** (:kbd:`Q`): Allows selection of nodes in the viewport. Left clicking
  41. on a node to select one. Left clicking and dragging a rectangle selects all
  42. nodes within the rectangle's boundaries, once released.
  43. Holding :kbd:`Shift` while selecting adds more nodes to the selection.
  44. Clicking on a selected node while holding :kbd:`Shift` deselects the node.
  45. In this mode, you can use the gizmos to perform movement or rotation.
  46. - **Move Mode** (:kbd:`W`): Enables move (or translate) mode for the selected nodes.
  47. See :ref:`doc_introduction_to_3d_space_and_manipulation` for more details.
  48. - **Rotate Mode** (:kbd:`E`): Enables rotation mode for the selected nodes. See
  49. :ref:`doc_introduction_to_3d_space_and_manipulation` for more details.
  50. - **Scale Mode** (:kbd:`R`): Enables scaling and displays scaling gizmos in different
  51. axes for the selected nodes. See :ref:`doc_introduction_to_3d_space_and_manipulation`
  52. for more details.
  53. - **Show the list of selectable nodes at the clicked position**: As the description suggests,
  54. this provides a list of selectable nodes at the clicked position as a context menu,
  55. if there is more than one node in the clicked area.
  56. - **Lock** (:kbd:`Ctrl + L`) the selected nodes, preventing selection and movement in the viewport.
  57. Clicking the button again (or using :kbd:`Ctrl + Shift + L`) unlocks the selected nodes.
  58. Locked nodes can only be selected in the scene tree.
  59. They can easily be identified with a padlock next to their node names in the scene tree.
  60. Clicking on this padlock also unlocks the nodes.
  61. - **Group selected nodes** (:kbd:`Ctrl + G`). This allows selection of the root node if
  62. any of the children are selected.
  63. Using :kbd:`Ctrl + G` ungroups them. Additionally, clicking the ungroup button in
  64. the scene tree performs the same action.
  65. - **Use Local Space** (:kbd:`T`): If enabled, gizmos of a node are drawn using the current node's
  66. rotation angle instead of the :ref:`global viewport axes <doc_introduction_to_3d_coordinate_system>`.
  67. - **Use Snap** (:kbd:`Y`): If enabled, movement, and rotation snap to grid. Snapping can also
  68. temporarily be activated using :kbd:`Ctrl` while performing the action.
  69. The settings for changing snap options are explained below.
  70. - **Project Camera Override**: This action temporarily replaces the active camera in the level
  71. (e.g., the camera following the player) with the camera in the editor's viewport, allowing you
  72. to move freely and inspect the level's different parts, while game is running.
  73. - **Toggle preview sunlight**: If no DirectionalLight3D exist in the scene, a preview
  74. of sunlight can be used as a light source. See
  75. :ref:`doc_introduction_to_3d_preview_environment_light` for more details.
  76. - **Toggle preview environment**: If no WorldEnvironment exists in the scene, a preview of the
  77. environment can be used as a placeholder. See
  78. :ref:`doc_introduction_to_3d_preview_environment_light` for more details.
  79. - **Edit Sun and Environment Settings (three dots)**: Opens the menu to configure preview
  80. sunlight and environment settings. See :ref:`doc_introduction_to_3d_preview_environment_light`
  81. for more details.
  82. - **Transform menu**: It has three options:
  83. - *Snap Object to Floor*: Snaps an object to a solid floor.
  84. - *Transform Dialog*: Opens a dialog to adjust transform parameters (translate, rotate, scale,
  85. and transform) manually.
  86. - *Snap Settings*: Allows you to change transform, rotate snap (in degrees), and scale snap
  87. (in percent) settings.
  88. - **View menu**: Controls the view options and enables additional viewports:
  89. .. image:: img/tuto_3d6.webp
  90. In this menu, you can also show/hide grids, which are set to 1x1 meter by default,
  91. and the origin, where the blue, green, and red axis lines intersect.
  92. Moreover, specific types of gizmos can be toggled in this menu.
  93. .. image:: img/tuto_3d6_2.webp
  94. An open eye means that the gizmo is visible, a closed eye means it is hidden.
  95. A half-open eye means that it is also visible through opaque surfaces.
  96. Clicking on *Settings* in this view menu opens a window to change the
  97. *Vertical Field of View (VFOV)* parameter
  98. (in degrees), *Z-Near*, and *Z-Far* values.
  99. Next to the View menu, additional buttons may be visible. In the toolbar image
  100. at the beginning of this chapter, an additional *Mesh* button appears because a
  101. MeshInstance3D is selected. This menu provides some quick actions or tools to
  102. work on a specific node or selection.
  103. View menu of viewport
  104. ---------------------
  105. Below the *Select* tool, in the 3D viewport, clicking on the three dots opens the
  106. **View menu** for the viewport.
  107. Hiding all shown gizmos in the editor's 3D view can also be performed through
  108. this menu:
  109. .. image:: img/tuto_3d6_1.webp
  110. This menu also displays the current view type and enables quick adjustment of the
  111. viewport's viewing angle. Additionally, it offers options to modify the appearance of
  112. nodes within the viewport.
  113. .. _doc_introduction_to_3d_coordinate_system:
  114. Coordinate system
  115. -----------------
  116. Godot uses the `metric <https://en.wikipedia.org/wiki/Metric_system>`__
  117. system for everything in 3D, with 1 unit being equal to 1 meter.
  118. Physics and other areas are tuned for this scale. Therefore, attempting to use a
  119. different scale is usually a bad idea (unless you know what you are doing).
  120. When working with 3D assets, it's always best to work in the correct scale (set
  121. the unit to metric in your 3D modeling software). Godot allows scaling
  122. post-import and, while this works in most cases, in rare situations it may
  123. introduce floating-point precision issues (and thus, glitches or artifacts) in
  124. delicate areas such as rendering or physics. Make sure your artists always work
  125. in the right scale!
  126. The Y coordinate is used for "up". As for the horizontal X/Z axes, Godot uses a
  127. **right-handed** coordinate system. This means that for most objects that need
  128. alignment (such as lights or cameras), the Z axis is used as a "pointing
  129. towards" direction. This convention roughly means that:
  130. - **X** is sides
  131. - **Y** is up/down
  132. - **Z** is front/back
  133. See this chart for comparison with other 3D software:
  134. .. figure:: img/introduction_to_3d_coordinate_systems.webp
  135. :align: center
  136. :alt: 3D coordinate systems comparison chart
  137. Image by `Freya Holmér <https://twitter.com/FreyaHolmer>`__
  138. .. _doc_introduction_to_3d_space_and_manipulation:
  139. Space and manipulation gizmos
  140. -----------------------------
  141. Moving, rotating, and scaling objects in the 3D view is done through the
  142. manipulator gizmos.
  143. Each axis is represented by a color: Red, Green, Blue represent X, Y, Z
  144. respectively. This convention applies to the grid and other gizmos too
  145. (and also to the shader language, ordering of components for
  146. Vector3, Color, etc.).
  147. .. image:: img/tuto_3d5.webp
  148. Some useful keybindings:
  149. - To snap placement or rotation, press :kbd:`Ctrl` while moving, scaling,
  150. or rotating.
  151. - To center the view on the selected object, press :kbd:`F`.
  152. In the viewport, the arrows can be clicked and held to move the object on an axis.
  153. The arcs can be clicked and held to rotate the object.
  154. To lock one axis and move the object freely in the other two axes, the colored rectangles
  155. can be clicked, held, and dragged.
  156. If the transform mode is changed from *Select Mode* to *Scale Mode*, the arrows will be
  157. replaced by cubes, which can be dragged to scale an object as if the object is being moved.
  158. Navigating the 3D environment
  159. -----------------------------
  160. In 3D environments, it is often important to adjust the viewpoint or angle
  161. from which you are viewing the scene.
  162. In Godot, navigating the 3D environment in the viewport (or spatial editor)
  163. can be done in multiple ways.
  164. The default 3D scene navigation controls are similar to Blender (aiming to
  165. have some sort of consistency in the free software pipeline), but
  166. options are included to customize mouse buttons and behavior to be
  167. similar to other tools in the Editor Settings. To change the controls
  168. to Maya or Modo controls, you can navigate to **Editor Settings > Editors > 3D**.
  169. Then, under *Navigation*, search for *Navigation Scheme*.
  170. .. image:: img/tuto_3d4.webp
  171. Using the default settings, the following shortcuts control how one can
  172. navigate in the viewport:
  173. Pressing the middle mouse button and dragging the mouse allows you to orbit around
  174. the center of what is on the screen.
  175. It is also possible to left-click and hold the manipulator gizmo located
  176. on the top right of the viewport to orbit around the center:
  177. .. image:: img/tuto_3d_gizmo.webp
  178. Left-clicking on one of the colored circles will set the view to the chosen
  179. orthogonal and the viewport's view menu will be updated accordingly.
  180. .. image:: img/tuto_3d_updated_view_menu.webp
  181. If the *Perspective* view is enabled on the viewport (can be seen on the viewport's View menu,
  182. not the View menu on the main toolbar), holding down the right mouse button on the viewport
  183. or pressing :kbd:`Shift + F` switches to "free-look" mode.
  184. In this mode you can move the mouse to look around, use the :kbd:`W` :kbd:`A`
  185. :kbd:`S` :kbd:`D` keys to fly around the view, :kbd:`E` to go up, and :kbd:`Q` to
  186. go down. To disable this mode, release the right mouse button or press
  187. :kbd:`Shift + F` again.
  188. In the free-look mode, you can temporarily increase the flying
  189. speed using :kbd:`Shift` or decrease it using :kbd:`Alt`. To change and keep the
  190. speed modifier use :kbd:`mouse wheel up` or :kbd:`mouse wheel down`, to increase or
  191. decrease it, respectively.
  192. In orthogonal mode, holding the right mouse button will pan the view instead.
  193. Use :kbd:`Keypad 5` to toggle between perspective and orthogonal view.
  194. Using Blender-style transform shortcuts
  195. ---------------------------------------
  196. Since Godot 4.2, you can enable Blender-style shortcuts for translating,
  197. rotating and scaling nodes. In Blender, these shortcuts are:
  198. - :kbd:`G` for translating
  199. - :kbd:`R` for rotating
  200. - :kbd:`S` for scaling
  201. After pressing a shortcut key while focusing on the 3D editor viewport,
  202. move the mouse or enter a number to move the selected node(s) by the
  203. specified amount in 3D units. You can constrain movement to a specific
  204. axis by specifying the axis as a letter, then the distance (if entering a
  205. value with the keyboard).
  206. For instance, to move the selection upwards by 2.5 units, enter the
  207. following sequence in order (Y+ is upwards in Godot):
  208. :kbd:`G`-:kbd:`Y`-:kbd:`2`-:kbd:`.`-:kbd:`5`-:kbd:`Enter`
  209. To use Blender-style transform shortcuts in Godot, go to the Editor Settings'
  210. **Shortcuts** tab, then in the Spatial Editor section:
  211. - Bind **Begin Translate Transformation** to :kbd:`G`.
  212. - Bind **Begin Rotate Transformation** to :kbd:`R`.
  213. - Bind **Begin Scale Transformation** to :kbd:`S`.
  214. - Finally, unbind **Scale Mode** so that its shortcut won't conflict with
  215. **Begin Rotate Transformation**.
  216. .. tip:: More shortcuts can be found on the
  217. :ref:`doc_default_key_mapping_shortcuts_spatial_editor` page.
  218. Node3D node
  219. ~~~~~~~~~~~
  220. :ref:`Node2D <class_Node2D>` is the base node for 2D.
  221. :ref:`Control <class_Control>` is the base node for everything GUI.
  222. Following this reasoning, the 3D engine uses the :ref:`Node3D <class_Node3D>`
  223. node for everything 3D.
  224. .. image:: img/tuto_3d1.webp
  225. Node3Ds have a local transform, which is relative to the parent
  226. node (as long as the parent node is also of **or inherits from** the type
  227. Node3D). This transform can be accessed as a 3×4
  228. :ref:`Transform3D <class_Transform3D>`, or as 3 :ref:`Vector3 <class_Vector3>`
  229. members representing location, Euler rotation (X, Y and Z angles) and
  230. scale.
  231. .. image:: img/tuto_3d2.webp
  232. 3D content
  233. ~~~~~~~~~~
  234. Unlike 2D, where loading image content and drawing is straightforward, 3D is a
  235. little more difficult. The content needs to be created with special 3D tools
  236. (also called Digital Content Creation tools, or DCCs) and exported to an
  237. exchange file format to be imported in Godot. This is required since 3D formats
  238. are not as standardized as images.
  239. Manually authored models (using 3D modeling software)
  240. -----------------------------------------------------
  241. .. FIXME: Needs update to properly description Godot 3.x workflow
  242. (used to reference a non existing doc_importing_3d_meshes importer).
  243. It is possible to import 3D models in Godot created in external tools.
  244. Depending on the format, you can import entire scenes (exactly as they look in
  245. the 3D modeling software), including animation, skeletal rigs, blend shapes, or
  246. as simple resources.
  247. .. seealso:: See :ref:`doc_importing_3d_scenes` for more on importing.
  248. Generated geometry
  249. ------------------
  250. It is possible to create custom geometry by using the
  251. :ref:`ArrayMesh <class_ArrayMesh>` resource directly. Simply create your arrays
  252. and use the :ref:`ArrayMesh.add_surface_from_arrays() <class_ArrayMesh_method_add_surface_from_arrays>`
  253. function. A helper class is also available, :ref:`SurfaceTool <class_SurfaceTool>`,
  254. which provides a more straightforward API and helpers for indexing,
  255. generating normals, tangents, etc.
  256. In any case, this method is meant for generating static geometry (models
  257. that will not be updated often), as creating vertex arrays and
  258. submitting them to the 3D API has a significant performance cost.
  259. .. note:: To learn about prototyping inside Godot or using external tools, see
  260. :ref:`doc_csg_tools`.
  261. Immediate geometry
  262. ------------------
  263. If, instead, you need to generate simple geometry that will be updated often,
  264. Godot provides a special :ref:`ImmediateMesh <class_ImmediateMesh>` resource
  265. that can be used in a :ref:`MeshInstance3D <class_MeshInstance3D>` node.
  266. This provides an OpenGL 1.x-style immediate-mode API to create points, lines,
  267. triangles, etc.
  268. 2D in 3D
  269. --------
  270. While Godot packs a powerful 2D engine, many types of games use 2D in a
  271. 3D environment. By using a fixed camera (either orthogonal or
  272. perspective) that does not rotate, nodes such as
  273. :ref:`Sprite3D <class_Sprite3D>` and
  274. :ref:`AnimatedSprite3D <class_AnimatedSprite3D>`
  275. can be used to create 2D games that take advantage of mixing with 3D
  276. backgrounds, more realistic parallax, lighting/shadow effects, etc.
  277. The disadvantage is, of course, that added complexity and reduced
  278. performance in comparison to plain 2D, as well as the lack of reference
  279. of working in pixels.
  280. Environment
  281. ~~~~~~~~~~~
  282. Besides editing a scene, it is often common to edit the environment.
  283. Godot provides a :ref:`WorldEnvironment <class_WorldEnvironment>`
  284. node that allows changing the background color, mode (as in, put a
  285. skybox), and applying several types of built-in post-processing effects.
  286. Environments can also be overridden in the Camera.
  287. .. _doc_introduction_to_3d_preview_environment_light:
  288. Preview environment and light
  289. -----------------------------
  290. By default, any 3D scene that doesn't have a :ref:`WorldEnvironment <class_WorldEnvironment>`
  291. node, or a :ref:`DirectionalLight3D <class_DirectionalLight3D>`, will have
  292. a preview turned on for what it's missing to light the scene.
  293. The preview light and environment will only be visible in the scene while
  294. in the editor. If you run the scene or export the project they will not
  295. affect the scene.
  296. The preview light and environment can be turned on or off from the top menu
  297. by clicking on their respective icon.
  298. .. image:: img/tuto_3d8.webp
  299. The three dots dropdown menu next to those icons can be used to adjust the properties
  300. of the preview environment and light if they are enabled.
  301. .. image:: img/tuto_3d9.webp
  302. The same preview sun and environment is used for every scene in the same project,
  303. So only make adjustments that would apply to all of the scenes you will need a preview
  304. light and environment for.
  305. Cameras
  306. -------
  307. No matter how many objects are placed in the 3D space, nothing will be
  308. displayed unless a :ref:`Camera3D <class_Camera3D>` is
  309. also added to the scene. Cameras can work in either orthogonal or
  310. perspective projections:
  311. .. image:: img/tuto_3d10.webp
  312. Cameras are associated with (and only display to) a parent or grandparent
  313. viewport. Since the root of the scene tree is a viewport, cameras will
  314. display on it by default, but if sub-viewports (either as render target
  315. or picture-in-picture) are desired, they need their own children cameras
  316. to display.
  317. .. image:: img/tuto_3d11.png
  318. When dealing with multiple cameras, the following rules are enforced for
  319. each viewport:
  320. - If no cameras are present in the scene tree, the first one that
  321. enters it will become the active camera. Further cameras entering the
  322. scene will be ignored (unless they are set as *current*).
  323. - If a camera has the "*current*" property set, it will be used
  324. regardless of any other camera in the scene. If the property is set,
  325. it will become active, replacing the previous camera.
  326. - If an active camera leaves the scene tree, the first camera in
  327. tree-order will take its place.
  328. Lights
  329. ------
  330. The background environment emits some ambient light which appears on surfaces.
  331. Still, without any light sources placed in the scene, the scene will appear
  332. quite dark unless the background environment is very bright.
  333. Most outdoor scenes have a directional light (the sun or moon), while indoor
  334. scenes typically have several positional lights (lamps, torches, …).
  335. See :ref:`doc_lights_and_shadows` for more information on setting up lights in Godot.