Using VisualShaders

Just as VisualScript is an alternative for users that prefer a graphicalapproach to coding, VisualShaders are the visual alternative for creatingshaders.

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 readingIntroduction to shaders.

Creating a VisualShader

VisualShaders can be created in anyShaderMaterial. To begin usingVisualShaders, create a newShaderMaterial in an object of your choice.

../../_images/shader_material_create_mesh.png

Then assign aVisualShader resource to theShader property.

../../_images/visual_shader_create.png

Click on the newVisualShader resource and the Visual Shader Editor willopen automatically. The layout of the Visual Shader Editor comprises two parts:the upper toolbar and the graph itself.

../../_images/visual_shader_editor2.png

From left to right in the toolbar:

  • TheAddNode button displays a popup menu to let you add nodes to theshader graph.

  • The drop-down menu is the shader type: Vertex, Fragment and Light. Like forscript shaders, it defines what built-in nodes will be available.

  • The following buttons and number input control the zooming level, gridsnapping and distance between grid lines (in pixels).

  • The last icon shows the generated shader code corresponding to your graph.

Note

Although VisualShaders do not require coding, they share the same logic withscript shaders. It is advised to learn the basics of both to have a goodunderstanding 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. Thiscan be convenient to understand what a given node does and how to reproduceit in scripts.

Using the Visual Shader Editor

By default, every newVisualShader 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 theAddNode button on theupper left corner or right click on any empty location in the graph, and a menuwill pop up.

../../_images/vs_popup.png

This popup has the following properties:

  • If you right-click on the graph, this menu will be called at the cursorposition and the created node, in that case, will also be placed under thatposition; otherwise, it will be created at the graph's center.

  • It can be resized horizontally and vertically allowing more content to beshown. 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.

  • TheExpandAll andCollapseAll options in the drop-down option menucan 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 anyscalar output to avector input, all components ofthe vector will take the value of the scalar.

When connecting anyvector output to ascalar input, the value of thescalar will be the average of the vector's components.

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

TheExpression 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.

../../_images/vs_expression.gif

The possibilities of this node are almost limitless – you can write complexprocedures, and use all the power of text-based shaders, such as loops, thediscard keyword, extended types, etc. For example:

../../_images/vs_expression2.png

Fresnel node

TheFresnel 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. TheFresnel node is great foradding a rim-like lighting effect to objects.

../../_images/vs_fresnel.png

Boolean node

TheBoolean node can be converted toScalar orVector to represent0 or1 and(0,0,0) or(1,1,1) respectively. This propertycan be used to enable or disable some effect parts with one click.

../../_images/vs_boolean.gif

If node

TheIf node allows you to setup a vector which will be returned the resultof the comparison betweena andb. 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 anda<b.

../../_images/vs_if.png

Switch node

TheSwitch node returns a vector if the boolean condition istrue orfalse.Boolean was introduced above. If you convert a vector to a trueboolean, all components of the vector should be above zero.

../../_images/vs_switch.png

Note

TheSwitch node is only available on the GLES3 backed. If you aretargeting GLES2 devices, you cannot useswitch statements.