visual_shaders.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. .. _doc_visual_shaders:
  2. Using VisualShaders
  3. ===================
  4. VisualShaders are the visual alternative for creating shaders.
  5. As shaders are inherently linked to visuals, the graph-based approach with
  6. previews of textures, materials, etc. offers a lot of additional convenience
  7. compared to purely script-based shaders. On the other hand, VisualShaders do not
  8. expose all features of the shader script and using both in parallel might be
  9. necessary for specific effects.
  10. .. note::
  11. If you are not familiar with shaders, start by reading
  12. :ref:`doc_introduction_to_shaders`.
  13. Creating a VisualShader
  14. -----------------------
  15. VisualShaders can be created in any :ref:`class_ShaderMaterial`. To begin using
  16. VisualShaders, create a new ``ShaderMaterial`` in an object of your choice.
  17. .. image:: img/shader_material_create_mesh.webp
  18. Then assign a :ref:`class_Shader` resource to the ``Shader`` property.
  19. .. image:: img/visual_shader_create.webp
  20. Click on the new ``Shader`` resource and the Create Shader dialog will
  21. open automatically. Change the Type option to :ref:`class_VisualShader`
  22. in the dropdown, then give it a name.
  23. .. image:: img/visual_shader_create2.webp
  24. Click on the visual shader you just created to open the Shader Editor.
  25. The layout of the Shader Editor comprises four parts, a file list on
  26. the right, the upper toolbar, the graph itself, and a material preview
  27. on the right that can be toggled off
  28. .. image:: img/visual_shader_editor2.webp
  29. From left to right in the toolbar:
  30. - The ``Add Node`` button displays a popup menu to let you add nodes to the
  31. shader graph.
  32. - The drop-down menu is the shader type: Vertex, Fragment and Light. Like for
  33. script shaders, it defines what built-in nodes will be available.
  34. - The following buttons and number input control the zooming level, grid
  35. snapping and distance between grid lines (in pixels).
  36. - The toggle controls if the graph minimap in the bottom right of the editor
  37. is visible or not.
  38. - The automatically arrange selected nodes button will try to organize any
  39. nodes you have selected as efficiently and cleanly as possible.
  40. - The Manage Varyings button opens a dropdown that lets you add or remove a
  41. varying.
  42. - The show generated code button shows shader code corresponding to your graph.
  43. - The last icon toggles the material preview on or off.
  44. .. note::
  45. Although VisualShaders do not require coding, they share the same logic with
  46. script shaders. It is advised to learn the basics of both to have a good
  47. understanding of the shading pipeline.
  48. The visual shader graph is converted to a script shader behind the scene,
  49. and you can see this code by pressing the last button in the toolbar. This
  50. can be convenient to understand what a given node does and how to reproduce
  51. it in scripts.
  52. Using the Visual Shader Editor
  53. ------------------------------
  54. By default, every new ``VisualShader`` will have an output node. Every node
  55. connection ends at one of the output node's sockets. A node is the basic unit to
  56. create your shader. To add a new node, click on the ``Add Node`` button on the
  57. upper left corner or right click on any empty location in the graph, and a menu
  58. will pop up.
  59. .. image:: img/vs_popup.webp
  60. This popup has the following properties:
  61. - If you right-click on the graph, this menu will be called at the cursor
  62. position and the created node, in that case, will also be placed under that
  63. position; otherwise, it will be created at the graph's center.
  64. - It can be resized horizontally and vertically allowing more content to be
  65. shown. Size transform and tree content position are saved between the calls,
  66. so if you suddenly closed the popup you can easily restore its previous state.
  67. - The ``Expand All`` and ``Collapse All`` options in the drop-down option menu
  68. can be used to easily list the available nodes.
  69. - You can also drag and drop nodes from the popup onto the graph.
  70. While the popup has nodes sorted in categories, it can seem overwhelming at
  71. first. Try to add some of the nodes, plug them in the output socket and observe
  72. what happens.
  73. When connecting any ``scalar`` output to a ``vector`` input, all components of
  74. the vector will take the value of the scalar.
  75. When connecting any ``vector`` output to a ``scalar`` input, the value of the
  76. scalar will be the average of the vector's components.
  77. Visual Shader node interface
  78. ------------------------------
  79. Visual shader nodes have input and output ports. The input ports are located on the left side of the node, and output ports are located on the right side of the node.
  80. .. figure:: img/vs_node.webp
  81. These ports are colored to differentiate type of port:
  82. .. |scalar| image:: img/vs_scalar.webp
  83. .. |vector| image:: img/vs_vector.webp
  84. .. |boolean| image:: img/vs_boolean.webp
  85. .. |transform| image:: img/vs_transform.webp
  86. .. |sampler| image:: img/vs_sampler.webp
  87. .. list-table:: Port types
  88. :widths: auto
  89. :header-rows: 1
  90. * - Type
  91. - Color
  92. - Description
  93. - Example
  94. * - Scalar
  95. - Gray
  96. - Scalar is a single value.
  97. - |scalar|
  98. * - Vector
  99. - Purple
  100. - Vector is a set of values.
  101. - |vector|
  102. * - Boolean
  103. - Green
  104. - On or off, true or false.
  105. - |boolean|
  106. * - Transform
  107. - Pink
  108. - A matrix, usually used to transform vertices.
  109. - |transform|
  110. * - Sampler
  111. - Orange
  112. - A texture sampler. It can be used to sample textures.
  113. - |sampler|
  114. All of the types are used in the calculations of vertices, fragments, and lights in the shader. For example: matrix multiplication,
  115. vector addition, or scalar division.
  116. There are other types but these are the main ones.
  117. Visual Shader nodes
  118. -------------------
  119. Below are some special nodes that are worth knowing about. The list is not
  120. exhaustive and might be expanded with more nodes and examples.
  121. Expression node
  122. ~~~~~~~~~~~~~~~
  123. The ``Expression`` node allows you to write Godot Shading Language (GLSL-like)
  124. expressions inside your visual shaders. The node has buttons to add any amount
  125. of required input and output ports and can be resized. You can also set up the
  126. name and type of each port. The expression you have entered will apply
  127. immediately to the material (once the focus leaves the expression text box). Any
  128. parsing or compilation errors will be printed to the Output tab. The outputs are
  129. initialized to their zero value by default. The node is located under the
  130. Special tab and can be used in all shader modes.
  131. The possibilities of this node are almost limitless – you can write complex
  132. procedures, and use all the power of text-based shaders, such as loops, the
  133. ``discard`` keyword, extended types, etc. For example:
  134. .. image:: img/vs_expression2.png
  135. Reroute node
  136. ~~~~~~~~~~~~
  137. The ``Reroute`` node is used purely for organizational purposes. In a complicated
  138. shader with many nodes you may find that the paths between nodes can make
  139. things hard to read. Reroute, as its name suggests, allows you to adjust the path
  140. between nodes to make things easier to read. You can even have multiple reroute
  141. nodes for a single path, which can be used to make right angles.
  142. .. image:: img/vs_reroute.webp
  143. To move a reroute node move your mouse cursor above it, and grab the handle that
  144. appears.
  145. .. image:: img/vs_reroute_handle.webp
  146. Fresnel node
  147. ~~~~~~~~~~~~
  148. The ``Fresnel`` node is designed to accept normal and view vectors and produces
  149. a scalar which is the saturated dot product between them. Additionally, you can
  150. setup the inversion and the power of equation. The ``Fresnel`` node is great for
  151. adding a rim-like lighting effect to objects.
  152. .. image:: img/vs_fresnel.webp
  153. Boolean node
  154. ~~~~~~~~~~~~
  155. The ``Boolean`` node can be converted to ``Scalar`` or ``Vector`` to represent
  156. ``0`` or ``1`` and ``(0, 0, 0)`` or ``(1, 1, 1)`` respectively. This property
  157. can be used to enable or disable some effect parts with one click.
  158. .. image:: img/vs_boolean.gif
  159. If node
  160. ~~~~~~~
  161. The ``If`` node allows you to setup a vector which will be returned the result
  162. of the comparison between ``a`` and ``b``. There are three vectors which can be
  163. returned: ``a == b`` (in that case the tolerance parameter is provided as a
  164. comparison threshold – by default it is equal to the minimal value, i.e.
  165. ``0.00001``), ``a > b`` and ``a < b``.
  166. .. image:: img/vs_if.png
  167. Switch node
  168. ~~~~~~~~~~~
  169. The ``Switch`` node returns a vector if the boolean condition is ``true`` or
  170. ``false``. ``Boolean`` was introduced above. If you want to convert a vector
  171. to a true boolean, all components of the vector should be non-zero.
  172. .. image:: img/vs_switch.webp
  173. Mesh Emitter
  174. ~~~~~~~~~~~~
  175. The ``Mesh Emitter`` node is used for emitting particles from mesh vertices. This is
  176. only available for shaders that are in ``Particles`` mode.
  177. Keep in mind that not all 3D objects are mesh files. a glTF file can't be dragged
  178. and dropped into the graph. However, you can create an inherited scene from it,
  179. save the mesh in that scene as it's own file, and use that.
  180. .. image:: img/vs_meshemitter.webp
  181. You can also drag and drop obj files into the graph editor to add the node
  182. for that specific mesh, other mesh files will not work for this.