CSG

CSG stands for Constructive Solid Geometry, and is a tool to combine basic shapes or custom meshes to create more complex shapes. In 3D modelling software, CSG is mostly known as “Boolean Operators”.

Level prototyping is one of the main uses of CSG in Godot. This technique allows users to create simple versions of most common shapes by combining primitives. Interior environments can be created by using inverted primitives.

../../_images/csg.gif

CSG节点简介

Like other features of Godot, CSG is supported in the form of nodes. These are the CSG nodes:

  • CSGBox
  • CSGCylinder (also supports cone)
  • CSGSphere
  • CSGTorus
  • CSGPolygon
  • CSGMesh
  • :ref:`CSGCombiner <class_CSGcombiner>’
../../_images/csg_nodes.png ../../_images/csg_mesh.png

CSG工具功能

每个CSG节点都支持3种布尔运算:

  • Union: Geometry of both primitives is merged, intersecting geometry is removed.
  • Intersection: Only intersecting geometry remains, the rest is removed.
  • Subtraction: The second shape is subtracted from the first, leaving a dent with its shape.
../../_images/csg_operation_menu.png ../../_images/csg_operation.png

CSGPolygon

CSGPolygon 节点沿着以2D(X,Y坐标)绘制的多边形以下列方式拉伸:

  • Depth: Extruded back a given amount.
  • Spin: Extruded while spinning around its origin.
  • Path: Extruded along a Path node. This operation is commonly called lofting.
../../_images/csg_poly_mode.png ../../_images/csg_poly.png

注解

The Path mode must be provided with a Path node to work. In the Path node, draw the path and the polygon in CSGPolygon will extrude along the given path.

自定义网格

Any mesh can be used for CSGMesh; the mesh can be modelled in other software and imported into Godot. Multiple materials are supported. There are some restrictions for geometry:

  • it must be closed,
  • it must not self-intersect,
  • it must not contain internal faces,
  • every edge must connect to only two other faces.
../../_images/csg_custom_mesh.png

CSGCombiner

CSGCombiner 节点是用于组织的空形状。 它只会组合子节点。

Processing order

Every CSG node will first process its children nodes and their operations: union, intersection or subtraction, in tree order, and apply them to itself one after the other.

注解

In the interest of performance, make sure CSG geometry remains relatively simple, as complex meshes can take a while to process. If adding objects together (such as table and room objects), create them as separate CSG trees. Forcing too many objects in a single tree will eventually start affecting performance. Only use binary operations where you actually need them.

原型设计

我们将对房间进行原型设计以练习使用CSG工具。

小技巧

Working in Orthogonal projection gives a better view when combining the CSG shapes.

我们的关卡将包含以下对象:

  • 一个房间,
  • 一张床,
  • 一盏灯,
  • 一张桌子,
  • 一个书架。

使用Spatial节点作为根节点创建场景。

小技巧

The default lighting of the environment doesn’t provide clear shading at some angles. Change the display mode using Display Overdraw in the 3D viewport menu, or add a DirectionalLight node to help you see clearly.

../../_images/csg_overdraw.png

Create a CSGBox and name it room, enable Invert Faces and change the dimensions of your room.

../../_images/csg_room.png ../../_images/csg_room_invert.png

接下来,创建一个CSGCombiner并将其命名为 desk

A desk has one surface and 4 legs:

  • Create 1 CSGBox children node in Union mode for the surface and adjust the dimensions.
  • Create 4 CSGBox children nodes in Union mode for the legs and adjust the dimensions.

Adjust their placement to resemble a desk.

../../_images/csg_desk.png

注解

CSG nodes inside a CSGCombiner will only process their operation within the combiner. Therefore, CSGCombiners are used to organize CSG nodes.

Create a CSGCombiner and name it bed.

Our bed consists of 3 parts: the bed, the mattress and a pillow. Create a CSGBox and adjust its dimension for the bed. Create another CSGBox and adjust its dimension for the mattress.

../../_images/csg_bed_mat.png

We will create another CSGCombiner named pillow as the child of bed. The scene tree should look like this:

../../_images/csg_bed_tree.png

We will combine 3 CSGSphere nodes in Union mode to form a pillow. Scale the Y axis of the spheres and enable Smooth Faces.

../../_images/csg_pillow_smooth.png

Select the pillow node and switch the mode to Subtraction; the combined spheres will cut a hole into the mattress.

../../_images/csg_pillow_hole.png

Try to re-parent the pillow node to the root Spatial node; the hole will disappear.

注解

This is to illustrate the effect of CSG processing order. Since the root node is not a CSG node, the CSGCombiner nodes are the end of the operations; this shows the use of CSGCombiner to organize the CSG scene.

Undo the re-parent after observing the effect. The bed you’ve built should look like this:

../../_images/csg_bed.png

Create a CSGCombiner and name it lamp.

A lamp consists of 3 parts: the stand, the pole and the lampshade. Create a CSGCylinder, enable the Cone option and make it the stand. Create another CSGCylinder and adjust the dimensions to use it as a pole.

../../_images/csg_lamp_pole_stand.png

We will use a CSGPolygon for the lampshade. Use the Spin mode for the CSGPolygon and draw a trapezoid while in Front View (numeric keypad 1); this shape will extrude around the origin and form the lampshade.

../../_images/csg_lamp_spin.png ../../_images/csg_lamp_polygon.png ../../_images/csg_lamp_extrude.png

Adjust the placement of the 3 parts to make it look like a lamp.

../../_images/csg_lamp.png

Create a CSGCombiner and name it bookshelf.

We will use 3 CSGBox nodes for the bookshelf. Create a CSGBox and adjust its dimensions; this will be the size of the bookshelf.

../../_images/csg_shelf_big.png

Duplicate the CSGBox and shorten the dimensions of each axis and change the mode to Subtraction.

../../_images/csg_shelf_subtract.png ../../_images/csg_shelf_subtract_menu.png

You’ve almost built a shelf. Create one more CSGBox for dividing the shelf into two levels.

../../_images/csg_shelf.png

根据您的喜好将您的家具放在房间里,您的场景应该是这样的:

../../_images/csg_room_result.png

You’ve successfully prototyped a room level with the CSG tools in Godot. CSG tools can be used for designing all kinds of levels, such as a maze or a city; explore its limitations when designing your game.