| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 | .. _doc_visual_shaders:Using VisualShaders===================VisualShaders are the visual alternative for creating shaders.As shaders are inherently linked to visuals, the graph-based approach withpreviews of textures, materials, etc. offers a lot of additional conveniencecompared to purely script-based shaders. On the other hand, VisualShaders do notexpose all features of the shader script and using both in parallel might benecessary for specific effects... note::    If you are not familiar with shaders, start by reading    :ref:`doc_introduction_to_shaders`.Creating a VisualShader-----------------------VisualShaders can be created in any :ref:`class_ShaderMaterial`. To begin usingVisualShaders, create a new ``ShaderMaterial`` in an object of your choice... image:: img/shader_material_create_mesh.webpThen assign a :ref:`class_Shader` resource to the ``Shader`` property... image:: img/visual_shader_create.webpClick on the new ``Shader`` resource and the Create Shader dialog willopen automatically. Change the Type option to :ref:`class_VisualShader`in the dropdown, then give it a name... image:: img/visual_shader_create2.webpClick on the visual shader you just created to open the Shader Editor.The layout of the Shader Editor comprises four parts, a file list onthe left, the upper toolbar, the graph itself, and a material previewon the right that can be toggled off.. image:: img/visual_shader_editor2.webpFrom left to right in the toolbar:- The arrow can be used to toggle the files panel's visibility.- The ``File`` button opens a dropdown menu for saving, loading, and creating  files.- The ``Add Node`` button displays a popup menu to let you add nodes to the  shader graph.- The drop-down menu is the shader type: Vertex, Fragment and Light. Like for  script shaders, it defines what built-in nodes will be available.- The following buttons and number input control the zooming level, grid  snapping and distance between grid lines (in pixels).- The toggle controls if the graph minimap in the bottom right of the editor  is visible or not.- The automatically arrange selected nodes button will try to organize any  nodes you have selected as efficiently and cleanly as possible.- The Manage Varyings button opens a dropdown that lets you add or remove a  varying.- The show generated code button shows shader code corresponding to your graph.- The toggle turns the material preview on or off.- The ``Online Docs`` button opens this documentation page in your web browser.- The last button allows you to put the shader editor in its own window,  separate from the rest of the editor... note::    Although VisualShaders do not require coding, they share the same logic with    script shaders. It is advised to learn the basics of both to have a good    understanding of the shading pipeline.    The visual shader graph is converted to a script shader behind the scene,    and you can see this code by pressing the last button in the toolbar. This    can be convenient to understand what a given node does and how to reproduce    it in scripts.Using the Visual Shader Editor------------------------------By default, every new ``VisualShader`` will have an output node. Every nodeconnection ends at one of the output node's sockets. A node is the basic unit tocreate your shader. To add a new node, click on the ``Add Node`` button on theupper left corner or right click on any empty location in the graph, and a menuwill pop up... image:: img/vs_popup.webpThis popup has the following properties:- If you right-click on the graph, this menu will be called at the cursor  position and the created node, in that case, will also be placed under that  position; otherwise, it will be created at the graph's center.- It can be resized horizontally and vertically allowing more content to be  shown. Size transform and tree content position are saved between the calls,  so if you suddenly closed the popup you can easily restore its previous state.- The ``Expand All`` and ``Collapse All`` options in the drop-down option menu  can be used to easily list the available nodes.- You can also drag and drop nodes from the popup onto the graph.While the popup has nodes sorted in categories, it can seem overwhelming atfirst. Try to add some of the nodes, plug them in the output socket and observewhat happens.When connecting any ``scalar`` output to a ``vector`` input, all components ofthe vector will take the value of the scalar.When connecting any ``vector`` output to a ``scalar`` input, the value of thescalar will be the average of the vector's components.Visual Shader node interface------------------------------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... figure:: img/vs_node.webpThese ports are colored to differentiate type of port:.. |scalar| image:: img/vs_scalar.webp.. |vector| image:: img/vs_vector.webp.. |boolean| image:: img/vs_boolean.webp.. |transform| image:: img/vs_transform.webp.. |sampler| image:: img/vs_sampler.webp.. list-table:: Port types   :widths: auto   :header-rows: 1   * - Type     - Color     - Description     - Example   * - Scalar     - Gray     - Scalar is a single value.     - |scalar|   * - Vector     - Purple     - Vector is a set of values.     - |vector|   * - Boolean     - Green     - On or off, true or false.     - |boolean|   * - Transform     - Pink     - A matrix, usually used to transform vertices.     - |transform|   * - Sampler     - Orange     - A texture sampler. It can be used to sample textures.     - |sampler|All of the types are used in the calculations of vertices, fragments, and lights in the shader. For example: matrix multiplication, vector addition, or scalar division.There are other types but these are the main ones.Visual Shader nodes-------------------Below are some special nodes that are worth knowing about. The list is notexhaustive and might be expanded with more nodes and examples.Expression node~~~~~~~~~~~~~~~The ``Expression`` node allows you to write Godot Shading Language (GLSL-like)expressions inside your visual shaders. The node has buttons to add any amountof required input and output ports and can be resized. You can also set up thename and type of each port. The expression you have entered will applyimmediately to the material (once the focus leaves the expression text box). Anyparsing or compilation errors will be printed to the Output tab. The outputs areinitialized to their zero value by default. The node is located under theSpecial tab and can be used in all shader modes.The possibilities of this node are almost limitless – you can write complexprocedures, and use all the power of text-based shaders, such as loops, the``discard`` keyword, extended types, etc. For example:.. image:: img/vs_expression2.pngReroute node~~~~~~~~~~~~The ``Reroute`` node is used purely for organizational purposes. In a complicatedshader with many nodes you may find that the paths between nodes can makethings hard to read. Reroute, as its name suggests, allows you to adjust the pathbetween nodes to make things easier to read. You can even have multiple reroutenodes for a single path, which can be used to make right angles... image:: img/vs_reroute.webpTo move a reroute node move your mouse cursor above it, and grab the handle thatappears... image:: img/vs_reroute_handle.webpFresnel node~~~~~~~~~~~~The ``Fresnel`` node is designed to accept normal and view vectors and producesa scalar which is the saturated dot product between them. Additionally, you cansetup the inversion and the power of equation. The ``Fresnel`` node is great foradding a rim-like lighting effect to objects... image:: img/vs_fresnel.webpBoolean node~~~~~~~~~~~~The ``Boolean`` node can be converted to ``Scalar`` or ``Vector`` to represent``0`` or ``1`` and ``(0, 0, 0)`` or ``(1, 1, 1)`` respectively. This propertycan be used to enable or disable some effect parts with one click... image:: img/vs_boolean.gifIf node~~~~~~~The ``If`` node allows you to setup a vector which will be returned the resultof the comparison between ``a`` and ``b``. There are three vectors which can bereturned: ``a == b`` (in that case the tolerance parameter is provided as acomparison threshold – by default it is equal to the minimal value, i.e.``0.00001``), ``a > b`` and ``a < b``... image:: img/vs_if.pngSwitch node~~~~~~~~~~~The ``Switch`` node returns a vector if the boolean condition is ``true`` or``false``. ``Boolean`` was introduced above. If you want to convert a vectorto a true boolean, all components of the vector should be non-zero... image:: img/vs_switch.webpMesh Emitter~~~~~~~~~~~~The ``Mesh Emitter`` node is used for emitting particles from mesh vertices. This isonly available for shaders that are in ``Particles`` mode.Keep in mind that not all 3D objects are mesh files. a glTF file can't be draggedand dropped into the graph. However, you can create an inherited scene from it,save the mesh in that scene as its own file, and use that... image:: img/vs_meshemitter.webpYou can also drag and drop obj files into the graph editor to add the nodefor that specific mesh, other mesh files will not work for this.
 |