introduction_to_3d.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 simple 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` in the wiki (which were especially created for game
  13. developers, not mathematicians or engineers) will help pave the way into
  14. efficiently developing 3D games.
  15. Spatial 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:`Spatial <class_Spatial>`
  20. node for everything 3D.
  21. .. image:: img/tuto_3d1.png
  22. Spatial nodes have a local transform, which is relative to the parent
  23. node (as long as the parent node is also **or inherits** of type
  24. Spatial). This transform can be accessed as a 4x3
  25. :ref:`Transform <class_Transform>`, 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.png
  29. 3D content
  30. ~~~~~~~~~~
  31. Unlike 2D, where loading image content and drawing is straightforward,
  32. 3D is a little more difficult. The content needs to be created with
  33. special 3D tool (usually referred to as DCCs) and exported to an
  34. exchange file format in order to be imported in Godot (3D formats are
  35. not as standardized as images).
  36. DCC-created models
  37. ------------------
  38. There are two pipelines to import 3D models in Godot. The first and most
  39. common one is through the :ref:`doc_importing_3d_scenes` importer, which allows to import
  40. entire scenes (just as they look in the DCC), including animation,
  41. skeletal rigs, blend shapes, etc.
  42. The second pipeline is through the :ref:`doc_importing_3d_meshes` importer. This
  43. second method allows importing simple .OBJ files as mesh resources,
  44. which can be then put inside a :ref:`MeshInstance <class_MeshInstance>`
  45. node for display.
  46. Generated geometry
  47. ------------------
  48. It is possible to create custom geometry by using the
  49. :ref:`Mesh <class_Mesh>` resource directly. Simply create your arrays
  50. and use the :ref:`Mesh.add_surface() <class_Mesh_add_surface>`
  51. function. A helper class is also available, :ref:`SurfaceTool <class_SurfaceTool>`,
  52. which provides a more straightforward API and helpers for indexing,
  53. generating normals, tangents, etc.
  54. In any case, this method is meant for generating static geometry (models
  55. that will not be updated often), as creating vertex arrays and
  56. submitting them to the 3D API has a significant performance cost.
  57. Immediate geometry
  58. ------------------
  59. If, instead, there is a requirement to generate simple geometry that
  60. will be updated often, Godot provides a special node,
  61. :ref:`ImmediateGeometry <class_ImmediateGeometry>`
  62. which provides an OpenGL 1.x style immediate-mode API to create points,
  63. lines, triangles, etc.
  64. 2D in 3D
  65. --------
  66. While Godot packs a powerful 2D engine, many types of games use 2D in a
  67. 3D environment. By using a fixed camera (either orthogonal or
  68. perspective) that does not rotate, nodes such as
  69. :ref:`Sprite3D <class_Sprite3D>` and
  70. :ref:`AnimatedSprite3D <class_AnimatedSprite3D>`
  71. can be used to create 2D games that take advantage of mixing with 3D
  72. backgrounds, more realistic parallax, lighting/shadow effects, etc.
  73. The disadvantage is, of course, that added complexity and reduced
  74. performance in comparison to plain 2D, as well as the lack of reference
  75. of working in pixels.
  76. Environment
  77. ~~~~~~~~~~~
  78. Besides editing a scene, it is often common to edit the environment.
  79. Godot provides a :ref:`WorldEnvironment <class_WorldEnvironment>`
  80. node that allows changing the background color, mode (as in, put a
  81. skybox), and applying several types of built-in post-processing effects.
  82. Environments can also be overridden in the Camera.
  83. 3D viewport
  84. ~~~~~~~~~~~
  85. Editing 3D scenes is done in the 3D tab. This tab can be selected
  86. manually, but it will be automatically enabled when a Spatial node is
  87. selected.
  88. .. image:: img/tuto_3d3.png
  89. Default 3D scene navigation controls are similar to Blender (aiming to
  90. have some sort of consistency in the free software pipeline..), but
  91. options are included to customize mouse buttons and behavior to be
  92. similar to other tools in Editor Settings:
  93. .. image:: img/tuto_3d4.png
  94. Coordinate system
  95. -----------------
  96. Godot uses the `metric <https://en.wikipedia.org/wiki/Metric_system>`__
  97. system for everything. 3D Physics and other areas are tuned for this, so
  98. attempting to use a different scale is usually a bad idea (unless you
  99. know what you are doing).
  100. When working with 3D assets, it's always best to work in the correct
  101. scale (set your DCC to metric). Godot allows scaling post-import and,
  102. while this works in most cases, in rare situations it may introduce
  103. floating point precision issues (and thus, glitches or artifacts) in
  104. delicate areas such as rendering or physics. So, make sure your artists
  105. always work in the right scale!
  106. The Y coordinate is used for "up", though for most objects that need
  107. alignment (like lights, cameras, capsule collider, vehicle, etc.), the Z
  108. axis is used as a "pointing towards" direction. This convention roughly
  109. means that:
  110. - **X** is sides
  111. - **Y** is up/down
  112. - **Z** is front/back
  113. Space and manipulation gizmos
  114. -----------------------------
  115. Moving objects in the 3D view is done through the manipulator gizmos.
  116. Each axis is represented by a color: Red, Green, Blue represent X,Y,Z
  117. respectively. This convention applies to the grid and other gizmos too
  118. (and also to the shader language, ordering of components for
  119. Vector3,Color,etc.).
  120. .. image:: img/tuto_3d5.png
  121. Some useful keybindings:
  122. - To snap motion or rotation, press the "s" key while moving, scaling
  123. or rotating.
  124. - To center the view on the selected object, press the "f" key.
  125. View menu
  126. ---------
  127. The view options are controlled by the "[ view ]" menu. Pay attention to
  128. this little menu inside the window because it is often overlooked!
  129. .. image:: img/tuto_3d6.png
  130. Default environment
  131. -------------------
  132. When created from the Project Manager, the 3D environment has a default sky.
  133. .. image:: img/tuto_3d8.png
  134. Given how physically based rendering works, it is advised to always try to
  135. work with a default environment in order to provide indirect and reflected
  136. light to your objects.
  137. Cameras
  138. -------
  139. No matter how many objects are placed in 3D space, nothing will be
  140. displayed unless a :ref:`Camera <class_Camera>` is
  141. also added to the scene. Cameras can either work in orthogonal or
  142. perspective projections:
  143. .. image:: img/tuto_3d10.png
  144. Cameras are associated with and only display to a parent or grandparent
  145. viewport. Since the root of the scene tree is a viewport, cameras will
  146. display on it by default, but if sub-viewports (either as render target
  147. or picture-in-picture) are desired, they need their own children cameras
  148. to display.
  149. .. image:: img/tuto_3d11.png
  150. When dealing with multiple cameras, the following rules are followed for
  151. each viewport:
  152. - If no cameras are present in the scene tree, the first one that
  153. enters it will become the active camera. Further cameras entering the
  154. scene will be ignored (unless they are set as *current*).
  155. - If a camera has the "*current*" property set, it will be used
  156. regardless of any other camera in the scene. If the property is set,
  157. it will become active, replacing the previous camera.
  158. - If an active camera leaves the scene tree, the first camera in
  159. tree-order will take its place.
  160. Lights
  161. ------
  162. There is no limitation on the number of lights nor of types of lights in
  163. Godot. As many as desired can be added (as long as performance allows).