csg_tools.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. .. _doc_csg_tools:
  2. Prototyping levels with CSG
  3. ===========================
  4. CSG stands for **Constructive Solid Geometry**, and is a tool to combine basic
  5. shapes or custom meshes to create more complex shapes. In 3D modelling software,
  6. CSG is mostly known as "Boolean Operators".
  7. Level prototyping is one of the main uses of CSG in Godot. This technique allows
  8. users to create simple versions of most common shapes by combining primitives.
  9. Interior environments can be created by using inverted primitives.
  10. .. note:: The CSG nodes in Godot are mainly intended for prototyping. There is
  11. no built-in support for UV mapping or editing 3D polygons (though
  12. extruded 2D polygons can be used with the CSGPolygon node).
  13. If you're looking for an easy to use level design tool for a project,
  14. you may want to use `Qodot <https://github.com/Shfty/qodot-plugin>`__
  15. instead. It lets you design levels using
  16. `TrenchBroom <https://kristianduske.com/trenchbroom/>`__ and import
  17. them in Godot.
  18. .. image:: img/csg.gif
  19. Introduction to CSG nodes
  20. -------------------------
  21. Like other features of Godot, CSG is supported in the form of nodes. These are
  22. the CSG nodes:
  23. - :ref:`CSGBox <class_CSGBox>`
  24. - :ref:`CSGCylinder <class_CSGCylinder>` (also supports cone)
  25. - :ref:`CSGSphere <class_CSGSphere>`
  26. - :ref:`CSGTorus <class_CSGTorus>`
  27. - :ref:`CSGPolygon <class_CSGPolygon>`
  28. - :ref:`CSGMesh <class_CSGMesh>`
  29. - :ref:`CSGCombiner <class_CSGcombiner>`
  30. .. image:: img/csg_nodes.png
  31. .. image:: img/csg_mesh.png
  32. CSG tools features
  33. ~~~~~~~~~~~~~~~~~~
  34. Every CSG node supports 3 kinds of boolean operations:
  35. - **Union:** Geometry of both primitives is merged, intersecting geometry
  36. is removed.
  37. - **Intersection:** Only intersecting geometry remains, the rest is removed.
  38. - **Subtraction:** The second shape is subtracted from the first, leaving a dent
  39. with its shape.
  40. .. image:: img/csg_operation_menu.png
  41. .. image:: img/csg_operation.png
  42. CSGPolygon
  43. ~~~~~~~~~~
  44. The :ref:`CSGPolygon <class_CSGPolygon>` node extrude along a Polygon drawn in
  45. 2D (in X, Y coordinates) in the following ways:
  46. - **Depth:** Extruded back a given amount.
  47. - **Spin:** Extruded while spinning around its origin.
  48. - **Path:** Extruded along a Path node. This operation is commonly called
  49. lofting.
  50. .. image:: img/csg_poly_mode.png
  51. .. image:: img/csg_poly.png
  52. .. note:: The **Path** mode must be provided with a :ref:`Path <class_Path>`
  53. node to work. In the Path node, draw the path and the polygon in
  54. CSGPolygon will extrude along the given path.
  55. Custom meshes
  56. ~~~~~~~~~~~~~
  57. Any mesh can be used for :ref:`CSGMesh <class_CSGMesh>`; the mesh can be
  58. modelled in other software and imported into Godot. Multiple materials are
  59. supported. There are some restrictions for geometry:
  60. - it must be closed,
  61. - it must not self-intersect,
  62. - it must not contain internal faces,
  63. - every edge must connect to only two other faces.
  64. .. image:: img/csg_custom_mesh.png
  65. CSGCombiner
  66. ~~~~~~~~~~~
  67. The :ref:`CSGCombiner <class_CSGCombiner>` node is an empty shape used for
  68. organization. It will only combine children nodes.
  69. Processing order
  70. ~~~~~~~~~~~~~~~~
  71. Every CSG node will first process its children nodes and their operations:
  72. union, intersection or subtraction, in tree order, and apply them to itself one
  73. after the other.
  74. .. note:: In the interest of performance, make sure CSG geometry remains
  75. relatively simple, as complex meshes can take a while to process.
  76. If adding objects together (such as table and room objects), create
  77. them as separate CSG trees. Forcing too many objects in a single tree
  78. will eventually start affecting performance.
  79. Only use binary operations where you actually need them.
  80. Prototyping a level
  81. -------------------
  82. We will prototype a room to practice the use of CSG tools.
  83. .. tip:: Working in **Orthogonal** projection gives a better view when combining
  84. the CSG shapes.
  85. Our level will contain these objects:
  86. - a room,
  87. - a bed,
  88. - a lamp,
  89. - a desk,
  90. - a bookshelf.
  91. Create a scene with a Spatial node as root node.
  92. .. tip:: The default lighting of the environment doesn't provide clear shading
  93. at some angles. Change the display mode using **Display Overdraw** in
  94. the 3D viewport menu, or add a DirectionalLight node to help you see
  95. clearly.
  96. .. image:: img/csg_overdraw.png
  97. Create a CSGBox and name it ``room``, enable **Invert Faces** and change the
  98. dimensions of your room.
  99. .. image:: img/csg_room.png
  100. .. image:: img/csg_room_invert.png
  101. Next, create a CSGCombiner and name it ``desk``.
  102. A desk has one surface and 4 legs:
  103. - Create 1 CSGBox children node in **Union** mode for the surface
  104. and adjust the dimensions.
  105. - Create 4 CSGBox children nodes in **Union** mode for the legs
  106. and adjust the dimensions.
  107. Adjust their placement to resemble a desk.
  108. .. image:: img/csg_desk.png
  109. .. note:: CSG nodes inside a CSGCombiner will only process their operation
  110. within the combiner. Therefore, CSGCombiners are used to organize
  111. CSG nodes.
  112. Create a CSGCombiner and name it ``bed``.
  113. Our bed consists of 3 parts: the bed, the mattress and a pillow. Create a CSGBox
  114. and adjust its dimension for the bed. Create another CSGBox and adjust its
  115. dimension for the mattress.
  116. .. image:: img/csg_bed_mat.png
  117. We will create another CSGCombiner named ``pillow`` as the child of ``bed``.
  118. The scene tree should look like this:
  119. .. image:: img/csg_bed_tree.png
  120. We will combine 3 CSGSphere nodes in **Union** mode to form a pillow. Scale the
  121. Y axis of the spheres and enable **Smooth Faces**.
  122. .. image:: img/csg_pillow_smooth.png
  123. Select the ``pillow`` node and switch the mode to **Subtraction**; the combined
  124. spheres will cut a hole into the mattress.
  125. .. image:: img/csg_pillow_hole.png
  126. Try to re-parent the ``pillow`` node to the root ``Spatial`` node; the hole will
  127. disappear.
  128. .. note:: This is to illustrate the effect of CSG processing order.
  129. Since the root node is not a CSG node, the CSGCombiner nodes are
  130. the end of the operations; this shows the use of CSGCombiner to
  131. organize the CSG scene.
  132. Undo the re-parent after observing the effect. The bed you've built should look
  133. like this:
  134. .. image:: img/csg_bed.png
  135. Create a CSGCombiner and name it ``lamp``.
  136. A lamp consists of 3 parts: the stand, the pole and the lampshade.
  137. Create a CSGCylinder, enable the **Cone** option and make it the stand. Create
  138. another CSGCylinder and adjust the dimensions to use it as a pole.
  139. .. image:: img/csg_lamp_pole_stand.png
  140. We will use a CSGPolygon for the lampshade. Use the **Spin** mode for the
  141. CSGPolygon and draw a `trapezoid <https://en.wikipedia.org/wiki/Trapezoid>`_
  142. while in **Front View** (numeric keypad 1); this shape will extrude around the
  143. origin and form the lampshade.
  144. .. image:: img/csg_lamp_spin.png
  145. .. image:: img/csg_lamp_polygon.png
  146. .. image:: img/csg_lamp_extrude.png
  147. Adjust the placement of the 3 parts to make it look like a lamp.
  148. .. image:: img/csg_lamp.png
  149. Create a CSGCombiner and name it ``bookshelf``.
  150. We will use 3 CSGBox nodes for the bookshelf. Create a CSGBox and adjust its
  151. dimensions; this will be the size of the bookshelf.
  152. .. image:: img/csg_shelf_big.png
  153. Duplicate the CSGBox and shorten the dimensions of each axis and change the mode
  154. to **Subtraction**.
  155. .. image:: img/csg_shelf_subtract.png
  156. .. image:: img/csg_shelf_subtract_menu.png
  157. You've almost built a shelf. Create one more CSGBox for dividing the shelf into
  158. two levels.
  159. .. image:: img/csg_shelf.png
  160. Position your furniture in your room as you like and your scene should look
  161. this:
  162. .. image:: img/csg_room_result.png
  163. You've successfully prototyped a room level with the CSG tools in Godot.
  164. CSG tools can be used for designing all kinds of levels, such as a maze
  165. or a city; explore its limitations when designing your game.
  166. Using prototype textures
  167. ------------------------
  168. Godot's :ref:`doc_spatial_material` supports *triplanar mapping*, which can be
  169. used to automatically apply a texture to arbitrary objects without distortion.
  170. This is handy when using CSG as Godot doesn't support editing UV maps on CSG
  171. nodes yet. Triplanar mapping is relatively slow, which usually restricts its
  172. usage to organic surfaces like terrain. Still, when prototyping, it can be used
  173. to quickly apply textures to CSG-based levels.
  174. .. note:: If you need some textures for prototyping, Kenney made a
  175. `set of CC0-licensed prototype textures <https://kenney.nl/assets/prototype-textures>`__.
  176. There are two ways to apply a material to a CSG node:
  177. - Applying it to a CSGCombiner node as a material override
  178. (**Geometry > Material Override** in the Inspector). This will affect its
  179. children automatically, but will make it impossible to change the material in
  180. individual children.
  181. - Applying a material to individual nodes (**Material** in the Inspector). This
  182. way, each CSG node can have its own appearance. Subtractive CSG nodes will
  183. apply their material to the nodes they're "digging" into.
  184. To apply triplanar mapping to a CSG node, select it, go to the Inspector, click
  185. the **[empty]** text next to **Material Override** (or **Material** for
  186. individual CSG nodes). Choose **New SpatialMaterial**. Click the newly created
  187. material's icon to edit it. Unfold the **Albedo** section and load a texture
  188. into the **Texture** property. Now, unfold the **Uv1** section and check
  189. **Triplanar**. You can change the texture offset and scale on each axis by
  190. playing with the **Scale** and **Offset** properties just above. Higher values
  191. in the **Scale** property will cause the texture to repeat more often.
  192. .. tip:: You can copy a SpatialMaterial to reuse it across CSG nodes. To do so,
  193. click the dropdown arrow next to a material property in the Inspector
  194. and choose **Copy**. To paste it, select the node you'd like to apply
  195. the material onto, click the dropdown arrow next to its material
  196. property then choose **Paste**.