Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

3D 简介

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

在3D中, 数学比2D更复杂一些, 所以看看Wiki里的 向量数学 将有助于为有效开发3D游戏铺平道路(它是为游戏开发者, 而不是数学家或工程师, 特别创建的).

Node3D node

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

备注

Be aware that "Spatial" Nodes are now called "Node3D" starting with Godot 4. Any Godot 3.x references to "Spatial" Nodes refer to "Node3D" in Godot 4.

../../_images/tuto_3d1.png

Node3Ds have a local transform, which is relative to the parent node (as long as the parent node is also of or inherits from the type Node3D). This transform can be accessed as a 4×3 Transform3D, or as 3 Vector3 members representing location, Euler rotation (X, Y and Z angles) and scale.

../../_images/tuto_3d2.png

3D 内容

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

Manually authored models (using 3D modeling software)

There are two pipelines to import 3D models in Godot. The first and most common one is by 导入 3D 场景, which allows you to import entire scenes (exactly as they look in the 3D modeling software), including animation, skeletal rigs, blend shapes, etc.

The second pipeline is by importing simple .OBJ files as mesh resources, which can be then put inside a MeshInstance3D node for display.

生成的几何体

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

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

即时几何体

If, instead, you need to generate simple geometry that will be updated often, Godot provides a special node, ImmediateMesh, which provides an OpenGL 1.x style immediate-mode API to create points, lines, triangles, etc.

3D 中的 2D

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

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

环境

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

3D 视口

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

../../_images/tuto_3d3.png

默认的3D场景导航控制类似于Blender(旨在在自由软件工作流程中具有某种一致性), 但在编辑器设置中也包含了自定义鼠标按钮和行为使其看上去像其他工具的选项:

../../_images/tuto_3d4.png

坐标系

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

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

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

  • X 是两边

  • Y 是上/下

  • Z 是前/后

See this chart for comparison with other 3D software:

3D coordinate systems comparison chart

Image by Freya Holmér

空间与操纵小工具

在 3D 视图中移动物体是通过操纵器小工具完成的。每个轴用一种颜色表示:红、绿、蓝分别代表 X、Y、Z。这种惯例也适用于网格和其他小工具(也适用于着色器语言、Vector3、Color 等组件的顺序)。

../../_images/tuto_3d5.png

一些有用的键盘绑定:

  • 在移动, 缩放或旋转时, 按 Ctrl 键, 可进行吸附放置或旋转.

  • 要将视图集中在所选对象上, 按 F.

视图菜单

视图选项由视口工具栏中的“视图”菜单控制。

../../_images/tuto_3d6.png

你可以通过这个菜单在编辑器的 3D 视图中隐藏小工具:

../../_images/tuto_3d6_1.png

要隐藏特定类型的小工具,你可以在“视图”菜单中把它们切换掉。

../../_images/tuto_3d6_2.png

默认环境

从项目管理器中创建时,3D环境具有默认的天空.

../../_images/tuto_3d8.png

Given how physically-based rendering works, it is advised to always try to work with a default environment in order to provide indirect and reflected light to your objects.

相机

No matter how many objects are placed in the 3D space, nothing will be displayed unless a Camera3D is also added to the scene. Cameras can work in either orthogonal or perspective projections:

../../_images/tuto_3d10.png

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

../../_images/tuto_3d11.png

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

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

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

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

灯光

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

Most outdoor scenes have a directional light (the sun or moon), while indoor scenes typically have several positional lights (lamps, torches, …). See 3D 灯光和阴影 for more information on setting up lights in Godot.