Chuyển đến nội dung

Bài 23 · 3D Games

3D Scenes, Meshes and Materials

Build your first Godot 4 3D scene: Node3D transforms, MeshInstance3D, primitive meshes, StandardMaterial3D PBR settings, imported glTF models and the maths behind them, with Python stand-ins.

Moving from 2D to 3D adds one axis and a lot of new ideas: meshes instead of sprites, materials instead of plain colors, lights and a perspective camera. Godot's 3D nodes mirror the 2D ones, so much of what you know carries over. This lesson covers the scene basics: transforms, meshes and materials.

The Godot editor and engine cannot run on this site, so the engine code is read-only and the runnable blocks are Python stand-ins that model the maths (rotation, normals, color). They are labelled each time. Next up after this: a 3D character controller.

Coordinate system and Node3D

Godot 3D is right-handed with Y up: +X is right, +Y is up, and -Z is forward (the direction a camera or a look_at target faces). One unit is conventionally one meter. Node3D is the base of every 3D node and has position, rotation (Euler angles in radians) and scale, or a full transform (a Basis plus an origin). Children are positioned relative to their parent, exactly like in 2D.

GDSCRIPT
extends Node3D

func _ready() -> void:
	position = Vector3(0, 1, -5)
	rotation_degrees = Vector3(0, 45, 0)   # spin around the Y axis
	scale = Vector3.ONE * 2.0

func _process(delta: float) -> void:
	rotate_y(deg_to_rad(90.0) * delta)     # 90 degrees per second

Meshes: the shape you see

A mesh is a list of triangles. The MeshInstance3D node displays a mesh. For prototypes use the built-in primitives (BoxMesh, SphereMesh, CylinderMesh, CapsuleMesh, PlaneMesh, TorusMesh); for real art import a glTF (.glb or .gltf) file, which Godot's importer turns into a scene with meshes, materials, skeletons and animations. Drag the file into the FileSystem dock and double-click it to adjust import settings.

Meshes are only drawn, not solid. For collisions add a StaticBody3D with a CollisionShape3D. A shortcut: select a MeshInstance3D and use Mesh > Create Collision Shape to build a convex or trimesh shape from it. Remember to keep collision shapes simple.

flowchart TD
    W["Node3D: World"] --> F["StaticBody3D: Floor"]
    F --> FM["MeshInstance3D (PlaneMesh)"]
    F --> FC["CollisionShape3D (BoxShape3D)"]
    W --> P["MeshInstance3D: Crate"]
    W --> L["DirectionalLight3D"]
    W --> C["Camera3D"]

A minimal 3D level: a solid floor, a crate, one light and one camera.

Python Stand-in (Python, not Godot): rotating a point around the Y axis

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

After 90 degrees the forward vector points to -X (left): positive Y rotation turns the object counter-clockwise when viewed from above.

Materials: how a surface looks

A material is a resource attached to a mesh surface. The default StandardMaterial3D implements physically based rendering (PBR) with these important properties:

  • Albedo: the base color or texture.
  • Metallic: 0 for plastic, wood and skin; 1 for bare metal.
  • Roughness: 0 is a mirror, 1 is fully matte.
  • Emission: the surface glows (and, with global illumination, lights neighbours).
  • Normal map: fake small bumps with a texture, without adding triangles.
  • Transparency: alpha blend, alpha scissor (cutouts such as leaves) or alpha hash.

Create the material in the Inspector (Mesh > Surface Material Override) or in code:

GDSCRIPT
func make_gold() -> StandardMaterial3D:
	var mat := StandardMaterial3D.new()
	mat.albedo_color = Color("#ffcc33")
	mat.metallic = 1.0
	mat.roughness = 0.25
	return mat

func _ready() -> void:
	$Crate.material_override = make_gold()

For effects the standard material cannot do, write a ShaderMaterial with Godot's shading language (shader_type spatial;) or build one visually with the Visual Shader editor. Shared materials are one resource used by many meshes, so editing one changes all; call duplicate() when each instance needs its own color.

Normals: what lighting needs

Lighting uses each surface's normal, the direction pointing out of it. The brightness of a diffuse surface is the dot product of the normal and the direction to the light, clamped at zero.

Python Stand-in (Python, not Godot): diffuse brightness from normal and light direction

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The top and right faces share the light; the left face (facing away) gets nothing from this light, and the front gets no direct light at all.

Python Stand-in (Python, not Godot): mixing albedo with a roughness-dependent highlight

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is a toy model to show how the three settings interact, not the real Godot shader; the actual engine uses a Cook-Torrance-style BRDF.

Performance habit: reuse meshes

Every unique mesh and material costs draw calls. Put the same MeshInstance3D scene many times and Godot batches what it can; for thousands of copies (grass, bullets) use a MultiMeshInstance3D. We return to this in optimizing 2D and 3D scenes.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which direction is forward in Godot 3D?

    Godot is right-handed with Y up, and -Z is forward (what Camera3D and look_at face).
  2. 2. A MeshInstance3D with a BoxMesh is placed in the scene. Will the player collide with it?

    Meshes are visual only. Add a StaticBody3D (or another body) with a collision shape for collisions.
  3. 3. What do the Metallic and Roughness values of StandardMaterial3D control?

    They are core PBR parameters: metallic from plastic to metal, roughness from mirror to matte.
  4. 4. You change the color of a material shared by ten meshes. What happens?

    Resources are shared by reference. Use duplicate() if one instance needs its own copy.

Summary

  • Godot 3D is right-handed, Y up, -Z forward, one unit is one meter.
  • MeshInstance3D draws meshes; collisions need a physics body and shape.
  • Import models as glTF; use StandardMaterial3D (albedo, metallic, roughness, emission, normal map).
  • Lighting depends on surface normals; shared materials are shared resources.

Next: A 3D Character Controller

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.