csg_tools.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. .. _doc_csg_tools:
  2. CSG
  3. ===
  4. 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".
  5. The aim of CSG in Godot is for it to be used in level prototyping. This technique allows users to create simple versions of most common shapes by combining primitives. Interior environments can be created by using inverted primitives.
  6. .. image:: img/csg.gif
  7. Introduction to CSG nodes
  8. -------------------------
  9. Like other features of Godot, CSG is supported in the form of nodes; these are the CSG nodes:
  10. - :ref:`CSGBox <class_CSGBox>`
  11. - :ref:`CSGCylinder <class_CSGCylinder>` (Also supports cone)
  12. - :ref:`CSGSphere <class_CSGSphere>`
  13. - :ref:`CSGTorus <class_CSGTorus>`
  14. - :ref:`CSGPolygon <class_CSGPolygon>`
  15. - :ref:`CSGMesh <class_CSGMesh>`
  16. - :ref:`CSGCombiner <class_CSGcombiner>`
  17. .. image:: img/csg_nodes.png
  18. .. image:: img/csg_mesh.png
  19. CSG tools features
  20. ~~~~~~~~~~~~~~~~~~
  21. Every CSG node supports 3 kinds of boolean operations:
  22. - Union: Geometry of both primitives is merged, intersecting geometry is removed.
  23. - Intersection: Only intersecting geometry remains, the rest is removed.
  24. - Subtraction: The second shape is subtracted from the first, leaving a dent with its shape.
  25. .. image:: img/csg_operation_menu.png
  26. .. image:: img/csg_operation.png
  27. CSGPolygon
  28. ~~~~~~~~~~
  29. The :ref:`CSGPolygon <class_CSGPolygon>` node extrude along a Polygon drawn in 2D (in X,Y coordinates) in the following ways:
  30. - Depth: Extruded back a given amount.
  31. - Spin: Extruded while spinning around it's origin.
  32. - Path: Extruded along a Path node. This operation is commonly called lofting.
  33. .. image:: img/csg_poly_mode.png
  34. .. image:: img/csg_poly.png
  35. .. note:: The ``Path`` mode must be provided with a :ref:`Path <class_Path>` node to work. In the ``Path`` node, draw the path and the polygon in ``CSGPolygon`` will extrude along the given path.
  36. Custom meshes
  37. ~~~~~~~~~~~~~
  38. Any mesh can be used for :ref:`CSGMesh <class_CSGMesh>`; the mesh can be modelled in other software and imported into Godot. Multiple materials are supported. There are some restrictions for geometry:
  39. - It must be closed
  40. - It must not self-intersect
  41. - It must not contain internal faces
  42. - Every edge must connect to only two other faces
  43. .. image:: img/csg_custom_mesh.png
  44. CSGCombiner
  45. ~~~~~~~~~~~
  46. The :ref:`CSGCombiner <class_CSGCombiner>` node is an empty shape used for organization. It will only combine children nodes.
  47. Process order
  48. ~~~~~~~~~~~~~
  49. 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.
  50. .. note:: A note on 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), please 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.
  51. Prototyping a level
  52. -------------------
  53. We will prototype a room to practice the use of CSG tools.
  54. .. tip:: Working in ``Orthogonal`` projection gives a better view when combining the CSG shapes.
  55. Our level will contain these objects:
  56. 1. A room
  57. 2. A bed
  58. 3. A lamp
  59. 4. A desk
  60. 5. A bookshelf
  61. Create a scene with a Spatial node as root node.
  62. .. tip:: The default lighting of the environment cannot provide clear shading at some angles; display in ``overdraw`` mode or add a direction light to help you see clearly.
  63. .. image:: img/csg_overdraw.png
  64. Then, create a CSGBox and name it room, enable ``Invert Faces`` and change the dimensions of your room.
  65. .. image:: img/csg_room.png
  66. .. image:: img/csg_room_invert.png
  67. Next, create a CSGCombiner and name it ``desk``.
  68. A desk has one surface and 4 legs.
  69. Create 1 ``CSGBox`` children node in ``union`` mode for the surface and adjust the dimensions.
  70. Create 4 ``CSGBox`` children nodes in ``union`` mode for the legs and adjust the dimensions.
  71. Adjust their placement to resemble a desk.
  72. .. image:: img/csg_desk.png
  73. .. note:: CSG nodes inside a ``CSGCombiner`` will only process their operation within the combiner, therefore ``CSGCombiners`` are used to organize the CSG nodes.
  74. Create a ``CSGCombiner`` and name it ``bed``.
  75. Our bed consists of 3 parts: the bed, the mattress, and a pillow.
  76. Create a ``CSGBox`` and adjust its dimension for the bed. Create another ``CSGBox`` and adjust its dimension for the mattress.
  77. .. image:: img/csg_bed_mat.png
  78. We will create another ``CSGCombiner``, named ``pillow``, as the child of ``bed``. The scene tree should look like this:
  79. .. image:: img/csg_bed_tree.png
  80. We will combine 3 ``CSGshpere`` in ``union`` mode to form a pillow. Scale the y-axis of the spheres and enable ``smooth faces``.
  81. .. image:: img/csg_pillow_smooth.png
  82. Select the `pillow` node and switch the mode to ``subtraction``; the combined spheres will cut a hole into the mattress.
  83. .. image:: img/csg_pillow_hole.png
  84. Try to re-parent the ``pillow`` node to the root ``Spatial`` node; the hole will disappear.
  85. .. note:: This is to illustrate the effect of CSG process 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.
  86. Undo the re-parent after observing the effect.
  87. The bed you built should look like this:
  88. .. image:: img/csg_bed.png
  89. Create a ``CSGCombiner`` and name it ``lamp``.
  90. A lamp consists of 3 parts: the stand, the pole, and the lampshade.
  91. 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.
  92. .. image:: img/csg_lamp_pole_stand.png
  93. We will use a ``CSGPolygon`` for the lampshade. Use the ``Spin`` mode for the ``CSGPolygon`` and draw a `trapezoid <https://en.wikipedia.org/wiki/Trapezoid>`_ while in ``Front View`` (numeric keypad 1); this shape will extrude around the origin and form the lampshade.
  94. .. image:: img/csg_lamp_spin.png
  95. .. image:: img/csg_lamp_polygon.png
  96. .. image:: img/csg_lamp_extrude.png
  97. Adjust the placement of the 3 parts to make it a ``lamp``.
  98. .. image:: img/csg_lamp.png
  99. Create a ``CSGCombiner`` and name it ``bookshelf``.
  100. We will use 3 ``CSGBox`` for the bookshelf. Create a ``CSGBox`` and adjust its dimension, this will be the size of the bookshelf.
  101. .. image:: img/csg_shelf_big.png
  102. Duplicate the ``CSGBox`` and shorten the dimension of each axis and change the mode to ``Subtraction``.
  103. .. image:: img/csg_shelf_subtract.png
  104. .. image:: img/csg_shelf_subtract_menu.png
  105. You've almost built a shelf. Create one more ``CSGBox`` for dividing the shelf into two levels.
  106. .. image:: img/csg_shelf.png
  107. Position your furniture in your room as you like and your scene should look this:
  108. .. image:: img/csg_room_result.png
  109. You've successfully prototyped a room level with the CSG tools in Godot. CSG tools can be used for designing all kinds of level, such as a maze or a city; explore the limitations of it when designing your game.