visual_shaders.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. .. _doc_visual_shaders:
  2. Using VisualShaders
  3. ===================
  4. Just as VisualScript is an alternative for users that prefer a graphical
  5. approach to coding, VisualShaders are the visual alternative for creating
  6. shaders.
  7. As shaders are inherently linked to visuals, the graph-based approach with
  8. previews of textures, materials, etc. offers a lot of additional convenience
  9. compared to purely script-based shaders. On the other hand, VisualShaders do not
  10. expose all features of the shader script and using both in parallel might be
  11. necessary for specific effects.
  12. .. note::
  13. If you are not familiar with shaders, start by reading
  14. :ref:`doc_what_are_shaders`.
  15. Creating a VisualShader
  16. -----------------------
  17. VisualShaders can be created in any :ref:`class_ShaderMaterial`. To begin using
  18. VisualShaders, create a new ``ShaderMaterial`` in an object of your choice.
  19. .. image:: img/shader_material_create_mesh.png
  20. Then assign a :ref:`class_VisualShader` resource to the ``Shader`` property.
  21. .. image:: img/visual_shader_create.png
  22. Click on the new ``VisualShader`` resource and the Visual Shader Editor will
  23. open automatically. The layout of the Visual Shader Editor comprises two parts:
  24. the upper toolbar and the graph itself.
  25. .. image:: img/visual_shader_editor2.png
  26. From left to right in the toolbar:
  27. - The ``Add Node`` button displays a popup menu to let you add nodes to the
  28. shader graph.
  29. - The drop-down menu is the shader type: Vertex, Fragment and Light. Like for
  30. script shaders, it defines what built-in nodes will be available.
  31. - The following buttons and number input control the zooming level, grid
  32. snapping and distance between grid lines (in pixels).
  33. - The last icon shows the generated shader code corresponding to your graph.
  34. .. note::
  35. Although VisualShaders do not require coding, they share the same logic with
  36. script shaders. It is advised to learn the basics of both to have a good
  37. understanding of the shading pipeline.
  38. The visual shader graph is converted to a script shader behind the scene,
  39. and you can see this code by pressing the last button in the toolbar. This
  40. can be convenient to understand what a given node does and how to reproduce
  41. it in scripts.
  42. Using the Visual Shader Editor
  43. ------------------------------
  44. By default, every new ``VisualShader`` will have an output node. Every node
  45. connection ends at one of the output node's sockets. A node is the basic unit to
  46. create your shader. To add a new node, click on the ``Add Node`` button on the
  47. upper left corner or right click on any empty location in the graph, and a menu
  48. will pop up.
  49. .. image:: img/vs_popup.png
  50. This popup has the following properties:
  51. - If you right-click on the graph, this menu will be called at the cursor
  52. position and the created node, in that case, will also be placed under that
  53. position; otherwise, it will be created at the graph's center.
  54. - It can be resized horizontally and vertically allowing more content to be
  55. shown. Size transform and tree content position are saved between the calls,
  56. so if you suddenly closed the popup you can easily restore its previous state.
  57. - The ``Expand All`` and ``Collapse All`` options in the drop-down option menu
  58. can be used to easily list the available nodes.
  59. - You can also drag and drop nodes from the popup onto the graph.
  60. While the popup has nodes sorted in categories, it can seem overwhelming at
  61. first. Try to add some of the nodes, plug them in the output socket and observe
  62. what happens.
  63. When connecting any ``scalar`` output to a ``vector`` input, all components of
  64. the vector will take the value of the scalar.
  65. When connecting any ``vector`` output to a ``scalar`` input, the value of the
  66. scalar will be the average of the vector's components.
  67. Visual Shader nodes
  68. -------------------
  69. Below are some special nodes that are worth knowing about. The list is not
  70. exhaustive and might be expanded with more nodes and examples.
  71. Expression node
  72. +++++++++++++++
  73. The ``Expression`` node allows you to write Godot Shading Language (GLSL-like)
  74. expressions inside your visual shaders. The node has buttons to add any amount
  75. of required input and output ports and can be resized. You can also set up the
  76. name and type of each port. The expression you have entered will apply
  77. immediately to the material (once the focus leaves the expression text box). Any
  78. parsing or compilation errors will be printed to the Output tab. The outputs are
  79. initialized to their zero value by default. The node is located under the
  80. Special tab and can be used in all shader modes.
  81. .. image:: img/vs_expression.gif
  82. The possibilities of this node are almost limitless – you can write complex
  83. procedures, and use all the power of text-based shaders, such as loops, the
  84. ``discard`` keyword, extended types, etc. For example:
  85. .. image:: img/vs_expression2.png
  86. Fresnel node
  87. ++++++++++++
  88. The ``Fresnel`` node is designed to accept normal and view vectors and produces
  89. a scalar which is the saturated dot product between them. Additionally, you can
  90. setup the inversion and the power of equation. The ``Fresnel`` node is great for
  91. adding a rim-like lighting effect to objects.
  92. .. image:: img/vs_fresnel.png
  93. Boolean node
  94. ++++++++++++
  95. The ``Boolean`` node can be converted to ``Scalar`` or ``Vector`` to represent
  96. ``0`` or ``1`` and ``(0, 0, 0)`` or ``(1, 1, 1)`` respectively. This property
  97. can be used to enable or disable some effect parts with one click.
  98. .. image:: img/vs_boolean.gif
  99. If node
  100. +++++++
  101. The ``If`` node allows you to setup a vector which will be returned the result
  102. of the comparison between ``a`` and ``b``. There are three vectors which can be
  103. returned: ``a == b`` (in that case the tolerance parameter is provided as a
  104. comparison threshold – by default it is equal to the minimal value, i.e.
  105. ``0.00001``), ``a > b`` and ``a < b``.
  106. .. image:: img/vs_if.png
  107. Switch node
  108. +++++++++++
  109. The ``Switch`` node returns a vector if the boolean condition is ``true`` or
  110. ``false``. ``Boolean`` was introduced above. If you convert a vector to a true
  111. boolean, all components of the vector should be above zero.
  112. .. image:: img/vs_switch.png
  113. .. note::
  114. The ``Switch`` node is only available on the GLES3 backed. If you are
  115. targeting GLES2 devices, you cannot use ``switch`` statements.