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.
Checking the stable version of the documentation...
Prototipazione dei livelli con 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 modeling 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 the most common shapes by combining primitives. Interior environments can be created by using inverted primitives.
Nota
The CSG nodes in Godot are mainly intended for prototyping. There is no built-in support for UV mapping or editing 3D polygons (though extruded 2D polygons can be used with the CSGPolygon3D node).
If you're looking for an easy to use level design tool for a project, you may want to use FuncGodot or Cyclops Level Builder instead.
Vedi anche
È possibile scoprire come utilizzare i nodi CSG per costruire varie forme (ad esempio scale o strade) attraverso il progetto demo Constructive Solid Geometry.
Introduzione ai nodi CSG
Like other features of Godot, CSG is supported in the form of nodes. These are the CSG nodes:
CSGCylinder3D (supporta anche il cono)
CSG tools features
Every CSG node supports 3 kinds of boolean operations:
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.
CSGPolygon
The CSGPolygon3D node extrude along a Polygon drawn in 2D (in X, Y coordinates) in the following ways:
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.
Nota
The Path mode must be provided with a Path3D node to work. In the Path node, draw the path and the polygon in CSGPolygon3D will extrude along the given path.
Mesh personalizzate
Custom meshes can be used for CSGMesh3D as long as the mesh is manifold. The mesh can be modeled in other software and imported into Godot. Multiple materials are supported.
For a mesh to be used as a CSG mesh, it is required to:
sia chiusa
have each edge connect to only two faces
have volume
And it is recommended to avoid:
negative volume
auto-intersezioni
Facce interiori
Godot uses the manifold library to implement CSG meshes. The technical definition of "manifold" used by Godot is the following, adapted from that library's definition of "manifold":
Every edge of every triangle must contain the same two vertices (by index) as exactly one other triangle edge, and the start and end vertices must switch places between these two edges. The triangle vertices must appear in clockwise order when viewed from the outside of the Godot Engine manifold mesh.
Making an existing mesh manifold with Blender
If you have an existing mesh that is not already manifold, you can make it manifold using Blender.
In Blender, install and enable the 3D Print Toolbox addon.
Select the mesh you want to make manifold. Open the sidebar by clicking on the arrow:
In the 3D Print tab, under Clean Up, click the Make Manifold button:
The mesh should now be manifold, and can be used as a custom mesh.
CSGCombiner3D
The CSGCombiner3D node is an empty shape used for organization. It will only combine children nodes.
Ordine di elaborazione
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.
Nota
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.
Prototyping a level
We will prototype a room to practice the use of CSG tools.
Suggerimento
Working in Orthogonal projection gives a better view when combining the CSG shapes.
Il nostro livello conterrà questi oggetti:
una stanza,
un letto,
una lampada,
una scrivania,
una libreria.
Create a scene with a Node3D node as root node.
Suggerimento
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.
Crea un oggetto CSGBox3D e chiamalo room, abilita Invert Faces e cambia le dimensioni della stanza.
Successivamente, crea un CSGCombiner3D e denominalo desk.
Una scrivania ha una superficie e 4 gambe:
Crea un nodo figlio CSGBox3D in modalità Union per la superficie e regola le dimensioni.
Crea 4 nodi figli CSGBox3D in modalità Union per le gambe e regola le dimensioni.
Regola la loro posizione per farli assomigliare a una scrivania.
Nota
CSG nodes inside a CSGCombiner3D will only process their operation within the combiner. Therefore, CSGCombiner3Ds are used to organize CSG nodes.
Create a CSGCombiner3D and name it bed.
Our bed consists of 3 parts: the bed, the mattress and a pillow. Create a CSGBox3D and adjust its dimension for the bed. Create another CSGBox3D and adjust its dimension for the mattress.
We will create another CSGCombiner3D named pillow as the child of bed.
The scene tree should look like this:
We will combine 3 CSGSphere3D nodes in Union mode to form a pillow. Scale the Y axis of the spheres and enable Smooth Faces.
Select the pillow node and switch the mode to Subtraction; the combined
spheres will cut a hole into the mattress.
Try to re-parent the pillow node to the root Node3D node; the hole will
disappear.
Nota
This is to illustrate the effect of CSG processing order. Since the root node is not a CSG node, the CSGCombiner3D nodes are the end of the operations; this shows the use of CSGCombiner3D to organize the CSG scene.
Undo the re-parent after observing the effect. The bed you've built should look like this:
Create a CSGCombiner3D and name it lamp.
A lamp consists of 3 parts: the stand, the pole and the lampshade. Create a CSGCylinder3D, enable the Cone option and make it the stand. Create another CSGCylinder3D and adjust the dimensions to use it as a pole.
We will use a CSGPolygon3D for the lampshade. Use the Spin mode for the CSGPolygon3D and draw a trapezoid while in Front View (numeric keypad 1); this shape will extrude around the origin and form the lampshade.
Adjust the placement of the 3 parts to make it look like a lamp.
Create a CSGCombiner3D and name it bookshelf.
Utilizzeremo 3 nodi CSGBox3D per la libreria. Crea un nodo CSGBox3D e regola le sue dimensioni; queste corrisponderanno alle dimensioni della libreria.
Duplica il CSGBox3D, riduci le dimensioni di ciascun asse e cambia la modalità in Subtraction.
Hai quasi costruito una mensola. Crea un altro CSGBox3D per dividere la mensola in due livelli.
Disponi i mobili nella stanza come preferisci e la scena dovrebbe assomigliare a questa:
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.
Utilizzare texture prototipiche
Godot's Standard Material 3D and ORM Material 3D supports triplanar mapping, which can be used to automatically apply a texture to arbitrary objects without distortion. This is handy when using CSG as Godot doesn't support editing UV maps on CSG nodes yet. Triplanar mapping is relatively slow, which usually restricts its usage to organic surfaces like terrain. Still, when prototyping, it can be used to quickly apply textures to CSG-based levels.
Nota
Se hai bisogno di texture per la prototipazione, Kenney ha creato un insieme di texture prototipiche con licenza CC0.
Ci sono due modi per applicare un materiale a un nodo CSG:
Applicandolo a un nodo CSGCombiner3D come materiale sostitutivo (Geometry > Material Override nell'Ispettore). Questo influenzerà automaticamente i suoi figli, ma renderà impossibile cambiare il materiale nei singoli figli.
Applicando un materiale ai singoli nodi (Material nell'Ispettore). In questo modo, ogni nodo CSG può avere il proprio aspetto. I nodi CSG sottrattivi applicheranno il loro materiale ai nodi in cui "scavano".
To apply triplanar mapping to a CSG node, select it, go to the Inspector, click the [empty] text next to Material Override (or Material for individual CSG nodes). Choose New StandardMaterial3D. Click the newly created material's icon to edit it. Unfold the Albedo section and load a texture into the Texture property. Now, unfold the Uv1 section and check Triplanar. You can change the texture offset and scale on each axis by playing with the Scale and Offset properties just above. Higher values in the Scale property will cause the texture to repeat more often.
Suggerimento
You can copy a StandardMaterial3D to reuse it across CSG nodes. To do so, click the dropdown arrow next to a material property in the Inspector and choose Copy. To paste it, select the node you'd like to apply the material onto, click the dropdown arrow next to its material property then choose Paste.
Convertire in MeshInstance3D
Dalla versione 4.4 di Godot, è possibile convertire un nodo CSG e i suoi figli in un nodo MeshInstance3D.
Ciò ha diversi vantaggi:
Preparare le lightmap, dato che è possibile generare le UV2 su un MeshInstance3D.
Precalcolare l'occlusion culling, poiché il precalcolo dell'occlusion culling prende in considerazione solo i MeshInstance3D.
Tempi di caricamento più rapidi, poiché la mesh CSG non si deve più ricostruire al caricamento della scena.
Prestazioni migliori quando si aggiorna la trasformazione del nodo se la mesh è utilizzata all'interno di un altro nodo CSG.
Per convertire un nodo CSG in un nodo MeshInstance3D, selezionalo e poi scegli CSG > Bake Mesh Instance nella barra degli strumenti. Il nodo MeshInstance3D verrà creato come nodo fratello. Tieni presente che il nodo CSG utilizzato per il precalcolo non viene nascosto automaticamente, quindi ricorda di nasconderlo manualmente per evitare che la sua geometria si sovrapponga al MeshInstance3D appena creato.
Puoi anche creare una forma di collisione trimesh tramite CSG > Bake Collision Shape. Il nodo CollisionShape3D generato deve essere un figlio di un nodo StaticBody3D o AnimatableBody3D per poter funzionare.
Suggerimento
Remember to keep the original CSG node in the scene tree, so that you can perform changes to the geometry later if needed. To make changes to the geometry, remove the MeshInstance3D node and make the root CSG node visible again.
Esportare come glTF
Può essere utile buttare giù idee per un livello tramite CSG, poi esportarlo come modello 3D per importarlo in un software di modellazione 3D. Puoi farlo selezionando Scena > Export as ... > glTF 2.0 Scene.