csg_tools.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. .. _doc_csg_tools:
  2. 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. .. image:: img/csg.gif
  11. Introduction to CSG nodes
  12. -------------------------
  13. Like other features of Godot, CSG is supported in the form of nodes. These are
  14. the CSG nodes:
  15. - :ref:`CSGBox <class_CSGBox>`
  16. - :ref:`CSGCylinder <class_CSGCylinder>` (also supports cone)
  17. - :ref:`CSGSphere <class_CSGSphere>`
  18. - :ref:`CSGTorus <class_CSGTorus>`
  19. - :ref:`CSGPolygon <class_CSGPolygon>`
  20. - :ref:`CSGMesh <class_CSGMesh>`
  21. - :ref:`CSGCombiner <class_CSGcombiner>`
  22. .. image:: img/csg_nodes.png
  23. .. image:: img/csg_mesh.png
  24. CSG tools features
  25. ~~~~~~~~~~~~~~~~~~
  26. Every CSG node supports 3 kinds of boolean operations:
  27. - **Union:** Geometry of both primitives is merged, intersecting geometry
  28. is removed.
  29. - **Intersection:** Only intersecting geometry remains, the rest is removed.
  30. - **Subtraction:** The second shape is subtracted from the first, leaving a dent
  31. with its shape.
  32. .. image:: img/csg_operation_menu.png
  33. .. image:: img/csg_operation.png
  34. CSGPolygon
  35. ~~~~~~~~~~
  36. The :ref:`CSGPolygon <class_CSGPolygon>` node extrude along a Polygon drawn in
  37. 2D (in X,Y coordinates) in the following ways:
  38. - **Depth:** Extruded back a given amount.
  39. - **Spin:** Extruded while spinning around its origin.
  40. - **Path:** Extruded along a Path node. This operation is commonly called
  41. lofting.
  42. .. image:: img/csg_poly_mode.png
  43. .. image:: img/csg_poly.png
  44. .. note:: The **Path** mode must be provided with a :ref:`Path <class_Path>`
  45. node to work. In the Path node, draw the path and the polygon in
  46. CSGPolygon will extrude along the given path.
  47. Custom meshes
  48. ~~~~~~~~~~~~~
  49. Any mesh can be used for :ref:`CSGMesh <class_CSGMesh>`; the mesh can be
  50. modelled in other software and imported into Godot. Multiple materials are
  51. supported. There are some restrictions for geometry:
  52. - it must be closed,
  53. - it must not self-intersect,
  54. - it must not contain internal faces,
  55. - every edge must connect to only two other faces.
  56. .. image:: img/csg_custom_mesh.png
  57. CSGCombiner
  58. ~~~~~~~~~~~
  59. The :ref:`CSGCombiner <class_CSGCombiner>` node is an empty shape used for
  60. organization. It will only combine children nodes.
  61. Processing order
  62. ~~~~~~~~~~~~~~~~
  63. Every CSG node will first process its children nodes and their operations:
  64. union, intersection or subtraction, in tree order, and apply them to itself one
  65. after the other.
  66. .. note:: In the interest of performance, make sure CSG geometry remains
  67. relatively simple, as complex meshes can take a while to process.
  68. If adding objects together (such as table and room objects), create
  69. them as separate CSG trees. Forcing too many objects in a single tree
  70. will eventually start affecting performance.
  71. Only use binary operations where you actually need them.
  72. Prototyping a level
  73. -------------------
  74. We will prototype a room to practice the use of CSG tools.
  75. .. tip:: Working in **Orthogonal** projection gives a better view when combining
  76. the CSG shapes.
  77. Our level will contain these objects:
  78. - a room,
  79. - a bed,
  80. - a lamp,
  81. - a desk,
  82. - a bookshelf.
  83. Create a scene with a Spatial node as root node.
  84. .. tip:: The default lighting of the environment doesn't provide clear shading
  85. at some angles. Change the display mode using **Display Overdraw** in
  86. the 3D viewport menu, or add a DirectionalLight node to help you see
  87. clearly.
  88. .. image:: img/csg_overdraw.png
  89. Create a CSGBox and name it ``room``, enable **Invert Faces** and change the
  90. dimensions of your room.
  91. .. image:: img/csg_room.png
  92. .. image:: img/csg_room_invert.png
  93. Next, create a CSGCombiner and name it ``desk``.
  94. A desk has one surface and 4 legs:
  95. - Create 1 CSGBox children node in **Union** mode for the surface
  96. and adjust the dimensions.
  97. - Create 4 CSGBox children nodes in **Union** mode for the legs
  98. and adjust the dimensions.
  99. Adjust their placement to resemble a desk.
  100. .. image:: img/csg_desk.png
  101. .. note:: CSG nodes inside a CSGCombiner will only process their operation
  102. within the combiner. Therefore, CSGCombiners are used to organize
  103. CSG nodes.
  104. Create a CSGCombiner and name it ``bed``.
  105. Our bed consists of 3 parts: the bed, the mattress and a pillow. Create a CSGBox
  106. and adjust its dimension for the bed. Create another CSGBox and adjust its
  107. dimension for the mattress.
  108. .. image:: img/csg_bed_mat.png
  109. We will create another CSGCombiner named ``pillow`` as the child of ``bed``.
  110. The scene tree should look like this:
  111. .. image:: img/csg_bed_tree.png
  112. We will combine 3 CSGSphere nodes in **Union** mode to form a pillow. Scale the
  113. Y axis of the spheres and enable **Smooth Faces**.
  114. .. image:: img/csg_pillow_smooth.png
  115. Select the ``pillow`` node and switch the mode to **Subtraction**; the combined
  116. spheres will cut a hole into the mattress.
  117. .. image:: img/csg_pillow_hole.png
  118. Try to re-parent the ``pillow`` node to the root ``Spatial`` node; the hole will
  119. disappear.
  120. .. note:: This is to illustrate the effect of CSG processing order.
  121. Since the root node is not a CSG node, the CSGCombiner nodes are
  122. the end of the operations; this shows the use of CSGCombiner to
  123. organize the CSG scene.
  124. Undo the re-parent after observing the effect. The bed you've built should look
  125. like this:
  126. .. image:: img/csg_bed.png
  127. Create a CSGCombiner and name it ``lamp``.
  128. A lamp consists of 3 parts: the stand, the pole and the lampshade.
  129. Create a CSGCylinder, enable the **Cone** option and make it the stand. Create
  130. another CSGCylinder and adjust the dimensions to use it as a pole.
  131. .. image:: img/csg_lamp_pole_stand.png
  132. We will use a CSGPolygon for the lampshade. Use the **Spin** mode for the
  133. CSGPolygon and draw a `trapezoid <https://en.wikipedia.org/wiki/Trapezoid>`_
  134. while in **Front View** (numeric keypad 1); this shape will extrude around the
  135. origin and form the lampshade.
  136. .. image:: img/csg_lamp_spin.png
  137. .. image:: img/csg_lamp_polygon.png
  138. .. image:: img/csg_lamp_extrude.png
  139. Adjust the placement of the 3 parts to make it look like a lamp.
  140. .. image:: img/csg_lamp.png
  141. Create a CSGCombiner and name it ``bookshelf``.
  142. We will use 3 CSGBox nodes for the bookshelf. Create a CSGBox and adjust its
  143. dimensions; this will be the size of the bookshelf.
  144. .. image:: img/csg_shelf_big.png
  145. Duplicate the CSGBox and shorten the dimensions of each axis and change the mode
  146. to **Subtraction**.
  147. .. image:: img/csg_shelf_subtract.png
  148. .. image:: img/csg_shelf_subtract_menu.png
  149. You've almost built a shelf. Create one more CSGBox for dividing the shelf into
  150. two levels.
  151. .. image:: img/csg_shelf.png
  152. Position your furniture in your room as you like and your scene should look
  153. this:
  154. .. image:: img/csg_room_result.png
  155. You've successfully prototyped a room level with the CSG tools in Godot.
  156. CSG tools can be used for designing all kinds of levels, such as a maze
  157. or a city; explore its limitations when designing your game.