Introduction to 3D

Creating a 3D game can be challenging. That extra Z coordinate makesmany of the common techniques that helped to make 2D games simpler nolonger work. To aid in this transition, it is worth mentioning thatGodot uses similar APIs for 2D and 3D. Most nodes are the same andare present in both 2D and 3D versions. In fact, it is worth checkingthe 3D platformer tutorial, or the 3D kinematic character tutorials,which are almost identical to their 2D counterparts.

An example 3D game demo created using Godot

Godot Third Person Shooter (TPS) Demo, available on theGithub repository or theAsset Library.

In 3D, math is a little more complex than in 2D. For an introduction to therelevant math written for game developers, not mathemeticians or engineers,check outVector math andUsing 3D transforms.

3D workspace

Editing 3D scenes is done in the 3D workspace. This workspace can be selectedmanually, but it will be automatically selected when a Node3D node isselected.

../../_images/tuto_3d3.webp

Similar to 2D, the tabs below the workspace selector are used to change betweencurrently opened scenes or create a new one using the plus (+) button. The left andright docks should be familiar fromeditor introduction.

Below the scene selector, the main toolbar is visible, and beneath the main toolbaris the 3D viewport.

Main toolbar

Some buttons in the main toolbar are the same as those in the 2D workspace. A brief explanationis given with the shortcut if the mouse cursor is hovered over a button for one second.Some buttons may have additional functionality if another keypress is performed. A recapof main functionality of each button with its default shortcut is provided below fromleft to right:

../../_images/3d_toolbar.webp
  • Select Mode (Q): Allows selection of nodes in the viewport. Left clickingon a node to select one. Left clicking and dragging a rectangle selects allnodes within the rectangle's boundaries, once released.HoldingShift while selecting adds more nodes to the selection.Clicking on a selected node while holdingShift deselects the node.In this mode, you can use the gizmos to perform movement or rotation.

  • Move Mode (W): Enables move (or translate) mode for the selected nodes.SeeSpace and manipulation gizmos for more details.

  • Rotate Mode (E): Enables rotation mode for the selected nodes. SeeSpace and manipulation gizmos for more details.

  • Scale Mode (R): Enables scaling and displays scaling gizmos in differentaxes for the selected nodes. SeeSpace and manipulation gizmosfor more details.

  • Show the list of selectable nodes at the clicked position: As the description suggests,this provides a list of selectable nodes at the clicked position as a context menu,if there is more than one node in the clicked area.

  • Lock (Ctrl+L) the selected nodes, preventing selection and movement in the viewport.Clicking the button again (or usingCtrl+Shift+L) unlocks the selected nodes.Locked nodes can only be selected in the scene tree.They can easily be identified with a padlock next to their node names in the scene tree.Clicking on this padlock also unlocks the nodes.

  • Group selected nodes (Ctrl+G). This allows selection of the root node ifany of the children are selected.UsingCtrl+G ungroups them. Additionally, clicking the ungroup button inthe scene tree performs the same action.

  • Ruler Mode (M): When enabled you can click and drag to measure distance in the scenein meters.

  • Use Local Space (T): If enabled, gizmos of a node are drawn using the current node'srotation angle instead of theglobal viewport axes.

  • Use Snap (Y): If enabled, movement, and rotation snap to grid. Snapping can alsotemporarily be activated usingCtrl while performing the action.The settings for changing snap options are explained below.

  • Toggle preview sunlight: If no DirectionalLight3D exist in the scene, a previewof sunlight can be used as a light source. SeePreview environment and light for more details.

  • Toggle preview environment: If no WorldEnvironment exists in the scene, a preview of theenvironment can be used as a placeholder. SeePreview environment and light for more details.

  • Edit Sun and Environment Settings (three dots): Opens the menu to configure previewsunlight and environment settings. SeePreview environment and lightfor more details.

  • Transform menu: It has three options:

    • Snap Object to Floor: Snaps an object to a solid floor.

    • Transform Dialog: Opens a dialog to adjust transform parameters (translate, rotate, scale,and transform) manually.

    • Snap Settings: Allows you to change transform, rotate snap (in degrees), and scale snap(in percent) settings.

  • View menu: Controls the view options and enables additional viewports:

../../_images/tuto_3d6.webp

In this menu, you can also show/hide grids, which are set to 1x1 meter by default,and the origin, where the blue, green, and red axis lines intersect.Moreover, specific types of gizmos can be toggled in this menu.

../../_images/tuto_3d6_2.webp

An open eye means that the gizmo is visible, a closed eye means it is hidden.A half-open eye means that it is also visible through opaque surfaces.

Clicking onSettings in this view menu opens a window to change theVertical Field of View (VFOV) parameter(in degrees),Z-Near, andZ-Far values.

Next to the View menu, additional buttons may be visible. In the toolbar imageat the beginning of this chapter, an additionalMesh button appears because aMeshInstance3D is selected. This menu provides some quick actions or tools towork on a specific node or selection.

View menu of viewport

Below theSelect tool, in the 3D viewport, clicking on the three dots opens theView menu for the viewport.Hiding all shown gizmos in the editor's 3D view can also be performed throughthis menu:

../../_images/tuto_3d6_1.webp

This menu also displays the current view type and enables quick adjustment of theviewport's viewing angle. Additionally, it offers options to modify the appearance ofnodes within the viewport.

Coordinate system

Godot uses themetricsystem for everything in 3D, with 1 unit being equal to 1 meter.Physics and other areas are tuned for this scale. Therefore, attempting to use adifferent scale is usually a bad idea (unless you know what you are doing).

When working with 3D assets, it's always best to work in the correct scale (setthe unit to metric in your 3D modeling software). Godot allows scalingpost-import and, while this works in most cases, in rare situations it mayintroduce floating-point precision issues (and thus, glitches or artifacts) indelicate areas such as rendering or physics. Make sure your artists always workin the right scale!

The Y coordinate is used for "up". As for the horizontal X/Z axes, Godot uses aright-handed coordinate system. This means that for most objects that needalignment (such as lights or cameras), the Z axis is used as a "pointingtowards" direction. This convention roughly means that:

  • X is sides

  • Y is up/down

  • Z is front/back

See this chart for comparison with other 3D software:

3D coordinate systems comparison chart

Image byFreya Holmér

Space and manipulation gizmos

Moving, rotating, and scaling objects in the 3D view is done through themanipulator gizmos.Each axis is represented by a color: Red, Green, Blue represent X, Y, Zrespectively. This convention applies to the grid and other gizmos too(and also to the shader language, ordering of components forVector3, Color, etc.).

../../_images/tuto_3d5.webp

Some useful keybindings:

  • To snap placement or rotation, pressCtrl while moving, scaling,or rotating.

  • To center the view on the selected object, pressF.

In the viewport, the arrows can be clicked and held to move the object on an axis.The arcs can be clicked and held to rotate the object.To lock one axis and move the object freely in the other two axes, the colored rectanglescan be clicked, held, and dragged.

If the transform mode is changed fromSelect Mode toScale Mode, the arrows will bereplaced by cubes, which can be dragged to scale an object as if the object is being moved.

Navigating the 3D environment

In 3D environments, it is often important to adjust the viewpoint or anglefrom which you are viewing the scene.In Godot, navigating the 3D environment in the viewport (or spatial editor)can be done in multiple ways.

The default 3D scene navigation controls are similar to Blender (aiming tohave some sort of consistency in the free software pipeline), butoptions are included to customize mouse buttons and behavior to besimilar to other tools in the Editor Settings. To change the controlsto Maya or Modo controls, you can navigate toEditor Settings > Editors > 3D.Then, underNavigation, search forNavigation Scheme.

../../_images/tuto_3d4.webp

Using the default settings, the following shortcuts control how one cannavigate in the viewport:

Pressing the middle mouse button and dragging the mouse allows you to orbit aroundthe center of what is on the screen.

It is also possible to left-click and hold the manipulator gizmo locatedon the top right of the viewport to orbit around the center:

../../_images/tuto_3d_gizmo.webp

Left-clicking on one of the colored circles will set the view to the chosenorthogonal and the viewport's view menu will be updated accordingly.

../../_images/tuto_3d_updated_view_menu.webp

If thePerspective view is enabled on the viewport (can be seen on the viewport's View menu,not the View menu on the main toolbar), holding down the right mouse button on the viewportor pressingShift+F switches to "free-look" mode.In this mode you can move the mouse to look around, use theWASD keys to fly around the view,E to go up, andQ togo down. To disable this mode, release the right mouse button or pressShift+F again.

In the free-look mode, you can temporarily increase the flyingspeed usingShift or decrease it usingAlt. To change and keep thespeed modifier usemousewheelup ormousewheeldown, to increase ordecrease it, respectively.

In orthogonal mode, holding the right mouse button will pan the view instead.UseKeypad5 to toggle between perspective and orthogonal view.

Using Blender-style transform shortcuts

Since Godot 4.2, you can enable Blender-style shortcuts for translating,rotating and scaling nodes. In Blender, these shortcuts are:

  • G for translating

  • R for rotating

  • S for scaling

After pressing a shortcut key while focusing on the 3D editor viewport,move the mouse or enter a number to move the selected node(s) by thespecified amount in 3D units. You can constrain movement to a specificaxis by specifying the axis as a letter, then the distance (if entering avalue with the keyboard).

For instance, to move the selection upwards by 2.5 units, enter thefollowing sequence in order (Y+ is upwards in Godot):

G-Y-2-.-5-Enter

To use Blender-style transform shortcuts in Godot, go to the Editor Settings'Shortcuts tab, then in the Spatial Editor section:

  • BindBegin Translate Transformation toG.

  • BindBegin Rotate Transformation toR.

  • BindBegin Scale Transformation toS.

  • Finally, unbindScale Mode so that its shortcut won't conflict withBegin Rotate Transformation.

Tip

More shortcuts can be found on the3D / Spatial editor page.

Node3D node

Node2D is the base node for 2D.Control is the base node for everything GUI.Following this reasoning, the 3D engine uses theNode3Dnode for everything 3D.

../../_images/tuto_3d1.webp

Node3Ds have a local transform, which is relative to the parentnode (as long as the parent node is also ofor inherits from the typeNode3D). This transform can be accessed as a 3×4Transform3D, or as 3Vector3members representing location, Euler rotation (X, Y and Z angles) andscale.

../../_images/tuto_3d2.webp

3D content

Unlike 2D, where loading image content and drawing is straightforward, 3D is alittle more difficult. The content needs to be created with special 3D tools(also called Digital Content Creation tools, or DCCs) and exported to anexchange file format to be imported in Godot. This is required since 3D formatsare not as standardized as images.

Manually authored models (using 3D modeling software)

It is possible to import 3D models in Godot created in external tools.Depending on the format, you can import entire scenes (exactly as they look inthe 3D modeling software), including animation, skeletal rigs, blend shapes, oras simple resources.

See also

SeeImporting 3D scenes for more on importing.

Generated geometry

It is possible to create custom geometry by using theArrayMesh resource directly. Simply create your arraysand use theArrayMesh.add_surface_from_arrays()function. A helper class is also available,SurfaceTool,which provides a more straightforward API and helpers for indexing,generating normals, tangents, etc.

In any case, this method is meant for generating static geometry (modelsthat will not be updated often), as creating vertex arrays andsubmitting them to the 3D API has a significant performance cost.

Note

To learn about prototyping inside Godot or using external tools, seePrototyping levels with CSG.

Immediate geometry

If, instead, you need to generate simple geometry that will be updated often,Godot provides a specialImmediateMesh resourcethat can be used in aMeshInstance3D node.This provides an OpenGL 1.x-style immediate-mode API to create points, lines,triangles, etc.

2D in 3D

While Godot packs a powerful 2D engine, many types of games use 2D in a3D environment. By using a fixed camera (either orthogonal orperspective) that does not rotate, nodes such asSprite3D andAnimatedSprite3Dcan be used to create 2D games that take advantage of mixing with 3Dbackgrounds, more realistic parallax, lighting/shadow effects, etc.

The disadvantage is, of course, that added complexity and reducedperformance in comparison to plain 2D, as well as the lack of referenceof working in pixels.

Environment

Besides editing a scene, it is often common to edit the environment.Godot provides aWorldEnvironmentnode that allows changing the background color, mode (as in, put askybox), and applying several types of built-in post-processing effects.Environments can also be overridden in the Camera.

Preview environment and light

By default, any 3D scene that doesn't have aWorldEnvironmentnode, or aDirectionalLight3D, will havea preview turned on for what it's missing to light the scene.

The preview light and environment will only be visible in the scene whilein the editor. If you run the scene or export the project they will notaffect the scene.

The preview light and environment can be turned on or off from the top menuby clicking on their respective icon.

../../_images/tuto_3d8.webp

The three dots dropdown menu next to those icons can be used to adjust the propertiesof the preview environment and light if they are enabled.

../../_images/tuto_3d9.webp

The same preview sun and environment is used for every scene in the same project,So only make adjustments that would apply to all of the scenes you will need a previewlight and environment for.

Cameras

No matter how many objects are placed in the 3D space, nothing will bedisplayed unless aCamera3D isalso added to the scene. Cameras can work in either orthogonal orperspective projections:

../../_images/tuto_3d10.webp

Cameras are associated with (and only display to) a parent or grandparentviewport. Since the root of the scene tree is a viewport, cameras willdisplay on it by default, but if sub-viewports (either as render targetor picture-in-picture) are desired, they need their own children camerasto display.

../../_images/tuto_3d11.png

When dealing with multiple cameras, the following rules are enforced foreach viewport:

  • If no cameras are present in the scene tree, the first one thatenters it will become the active camera. Further cameras entering thescene will be ignored (unless they are set ascurrent).

  • If a camera has the "current" property set, it will be usedregardless of any other camera in the scene. If the property is set,it will become active, replacing the previous camera.

  • If an active camera leaves the scene tree, the first camera intree-order will take its place.

Lights

The background environment emits some ambient light which appears on surfaces.Still, without any light sources placed in the scene, the scene will appearquite dark unless the background environment is very bright.

Most outdoor scenes have a directional light (the sun or moon), while indoorscenes typically have several positional lights (lamps, torches, …).See3D lights and shadows for more information on setting up lights in Godot.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.