3D 简介

创造一个 3D 游戏很有挑战性。那个多出来的的 Z 轴使许多有助于使 2D 游戏变得简单的常用技术不再起作用。为了帮助习惯这种转变,值得一提的是,Godot在2D和3D上使用了类似的API。大多数节点是相同的,并且存在 2D 和 3D 版本。事实上,可以看看 3D 平台游戏教程或 3D 运动学角色教程,它们大部分与2D对应教程相同。

An example 3D game demo created using Godot

Godot Third Person Shooter (TPS) Demo, available on the Github repository or the Asset Library.

In 3D, math is a little more complex than in 2D. For an introduction to the relevant math written for game developers, not mathemeticians or engineers, check out 向量数学 and 使用 3D 变换.

3D workspace

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

../../_images/tuto_3d3.webp

Similar to 2D, the tabs below the workspace selector are used to change between currently opened scenes or create a new one using the plus (+) button. The left and right docks should be familiar from editor introduction.

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

Main toolbar

Some buttons in the main toolbar are the same as those in the 2D workspace. A brief explanation is 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 recap of main functionality of each button with its default shortcut is provided below from left to right:

../../_images/3d_toolbar.webp
  • Select Mode (Q): Allows selection of nodes in the viewport. Left clicking on a node to select one. Left clicking and dragging a rectangle selects all nodes within the rectangle's boundaries, once released. Holding Shift while selecting adds more nodes to the selection. Clicking on a selected node while holding Shift 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. See 空间与操纵小工具 for more details.

  • Rotate Mode (E): Enables rotation mode for the selected nodes. See 空间与操纵小工具 for more details.

  • Scale Mode (R): Enables scaling and displays scaling gizmos in different axes for the selected nodes. See 空间与操纵小工具 for 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 using Ctrl + 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 if any of the children are selected. Using Ctrl + G ungroups them. Additionally, clicking the ungroup button in the scene tree performs the same action.

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

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

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

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

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

  • Edit Sun and Environment Settings (three dots): Opens the menu to configure preview sunlight and environment settings. See Preview environment and light for 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 on Settings in this view menu opens a window to change the Vertical Field of View (VFOV) parameter (in degrees), Z-Near, and Z-Far values.

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

View menu of viewport

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

../../_images/tuto_3d6_1.webp

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

坐标系

Godot 在 3D 中使用的都是公制,1 个单位等于 1 米。物理和其他区域都为此而调整过。因此,试图使用不同的比例通常是一个坏主意(除非你知道你在做什么)。

使用 3D 资产时,最好始终使用正确的比例(在 3D 建模软件中将单位设置为公制)。Godot 允许在导入后缩放,尽管在大多数时都没问题,但在极少数情况下,会在渲染或物理等敏感区域带来浮点精度问题(从而导致故障或伪影)。所以,确保你的艺术家始终在正确的比例下进行创作!

Y 坐标用于“向上”。至于水平 X/Z 轴,Godot 使用 右手 坐标系。这意味着对于大多数需要对齐的对象(例如灯光或相机),Z 轴被用作“指向”方向。这个约定的大致意思是:

  • X 是两边

  • Y 是上/下

  • Z 是前/后

有关与其他 3D 软件的比较,请参阅此图表:

3D坐标系比较图表

图像来自 Freya Holmér

空间与操纵小工具

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

../../_images/tuto_3d5.webp

一些有用的键盘绑定:

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

  • 要将视图居中到所选对象上,请按 F

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 rectangles can be clicked, held, and dragged.

If the transform mode is changed from Select Mode to Scale Mode, the arrows will be replaced by cubes, which can be dragged to scale an object as if the object is being moved.

使用 Blender 风格的变换快捷键

从 Godot 4.2 开始,可以启用 Blender 风格的快捷键来平移、旋转和缩放节点。在 Blender 中,这些快捷键分别是:

  • G 用于平移

  • R 用于旋转

  • S 用于缩放

在聚焦于 3D 编辑器视口的同时,按下快捷键,可以移动鼠标或输入数字以将选定节点移动指定的 3D 单位量。你可以通过用字母指定特定的轴,然后指定距离(如果使用键盘来输入值),以此来将选中节点的平移限制到特定的轴上。

例如,要将选中物体向上移动 2.5 个单位,请按顺序输入以下序列(Y+ 在 Godot 中是向上,也就是延 Y 轴正方向):

G-Y-2-.-5-Enter

如果要在 Godot 中使用 Blender 风格的变换快捷键,请转到编辑器设置的 快捷键 选项卡,然后在 Spatial Editor 部分中进行以下调整:

  • 开始平移变换绑定到 G

  • 开始旋转变换绑定到 R

  • 开始缩放变换绑定到 S

  • 最后,取消 缩放模式 快捷键的绑定,使其快捷键不会与 开始旋转变换 冲突。

小技巧

More shortcuts can be found on the 3D / Spatial editor page.

Node3D 节点

Node2D 是 2D 的基础节点。Control 是所有 GUI 的基础节点。同理,3D 引擎中的所有 3D 物体都使用 Node3D 节点。

../../_images/tuto_3d1.webp

Node3D 有一个局部变换,它是相对于父节点而言的(只要父节点也是或继承自 Node3D 类型)。这个变换可以以 3×4 的 Transform3D 形式访问,也可以以 3 个 Vector3 成员的形式访问,分别代表位置、欧拉旋转(X、Y、Z 角度)和缩放。

../../_images/tuto_3d2.webp

3D 内容

与 2D 中加载图像内容和绘图都非常直观相比,3D 就有所不同,会稍微难一些。3D 内容需要使用特殊的 3D 工具(通常称为数字内容创建工具,Digital Content Creation 工具,简称 DCC)来创建,然后导出到某种交换文件格式,才能被 Godot 导入。这是因为 3D 格式的标准化不及图像。

手动制作的模型(使用 3D 建模软件)

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 in the 3D modeling software), including animation, skeletal rigs, blend shapes, or as simple resources.

参见

See 导入 3D 场景 for more on importing.

生成的几何体

可以直接使用 ArrayMesh 资源创建自定义几何体。只需创建数组并使用 ArrayMesh.add_surface_from_arrays() 函数即可。也可以使用辅助类 SurfaceTool,它提供了更直接的 API 和辅助工具,用于索引、生成法线、切线等。

在任何情况下,这种方法都是为了用于生成静态几何体(不会经常更新的模型),因为创建顶点数组并将它们提交给3D API具有显著的性能开销。

备注

To learn about prototyping inside Godot or using external tools, see 使用 CSG 设计关卡原型.

即时几何体

相反,如果你需要生成经常更新的简单几何体,Godot 提供了一种特殊的 ImmediateMesh 资源,它可以在 MeshInstance3D 节点中使用。这提供了 OpenGL 1.x 风格的即时模式 API 来创建点、线、三角形等。

3D 中的 2D

虽然Godot包装了强大的2D引擎,但许多类型的游戏会使用在3D环境中的2D效果。 通过使用不旋转的固定相机(正交或透视),可以使用诸如 Sprite3DAnimatedSprite3D 等节点来创建混合了具有3D背景,更逼真的视差,灯光/阴影效果等的2D游戏。

当然,缺点在于与普通2D相比增加了复杂性并降低了与普通2D 相比的性能,以及缺乏进行像素工作时的参考。

环境

除了编辑场景之外,编辑环境通常也很常见。 Godot提供了一个 WorldEnvironment 节点,该节点允许更改背景颜色,模式(就像放一个天空盒时那样)以及应用多种内置处理后效果。 环境可以在Camera中被覆写。

Preview environment and light

默认情况下,如果 3D 场景中没有 WorldEnvironment 或者 DirectionalLight3D 节点,就会打开对应的预览项,为该场景布光。

预览灯光和预览环境只会在编辑器的场景中显示。运行场景或导出后的项目时不会对场景产生影响。

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

../../_images/tuto_3d8.webp

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

../../_images/tuto_3d9.webp

同一个项目中,不同的场景使用的是相同的预览太阳和预览环境。因此,在这里作出的调整应该适合所有需要预览灯光和预览环境的场景。

相机

无论在 3D 空间中放置多少物体,除非在场景中添加了 Camera3D,否则不会显示任何内容。相机可以在正交或透视投影中工作:

../../_images/tuto_3d10.webp

摄像机与父视口或其祖先视口相关联,且仅显示到他们上面。 由于场景树的根是一个视口,默认情况下会在其上显示摄像机,但如果需要子视口(作为渲染目标或画中画),则需要自己的子摄像头才能显示。

../../_images/tuto_3d11.png

处理多台摄像机时,每个视口都遵循以下规则:

  • 如果场景树中没有摄像机,则第一个进入的摄像机将成为活跃摄像机。进入场景的其他摄像机将被忽略(除非它们被设置为 current)。

  • 如果相机设置了“current”属性,则无论场景中是否有其他相机,都会使用它。如果该属性已设置,它将变为活动状态,取代之前的摄像机。

  • 如果活动摄像机离开了场景树,则按树形顺序排列的第一台摄像机将取代它。

灯光

背景环境发出一些出现在表面上的环境光。然而,如果场景中没有任何光源,除非背景环境非常明亮,否则场景会显得很暗。

大多数室外场景都有平行光(太阳或月亮),而室内场景通常有多个位置光(灯、火把等)。有关在 Godot 中设置灯光的更多信息,请参阅 3D 灯光和阴影