Up to date
This page is up to date for Godot 4.1
.
If you still find outdated information, please open an issue.
Converting GLSL to Godot shaders¶
This document explains the differences between Godot's shading language and GLSL and gives practical advice on how to migrate shaders from other sources, such as Shadertoy and The Book of Shaders, into Godot shaders.
For detailed information on Godot's shading language, please refer to the Shading Language reference.
GLSL¶
Godot uses a shading language based on GLSL with the addition of a few quality-of-life features. Accordingly, most features available in GLSL are available in Godot's shading language.
Shader programs¶
In GLSL, each shader uses a separate program. You have one program for the
vertex shader and one for the fragment shader. In Godot, you have a single
shader that contains a vertex
and/or a fragment
function. If you only
choose to write one, Godot will supply the other.
Godot allows uniform variables and functions to be shared by defining the fragment and vertex shaders in one file. In GLSL, the vertex and fragment programs cannot share variables except when varyings are used.
Vertex attributes¶
In GLSL, you can pass in per-vertex information using attributes and have the
flexibility to pass in as much or as little as you want. In Godot, you have a
set number of input attributes, including VERTEX
(position), COLOR
,
UV
, UV2
, NORMAL
. Each shaders' page in the shader reference section
of the documentation comes with a complete list of its vertex attributes.
gl_Position¶
gl_Position
receives the final position of a vertex specified in the vertex
shader. It is specified by the user in clip space. Typically, in GLSL, the model
space vertex position is passed in using a vertex attribute called position
and you handle the conversion from model space to clip space manually.
In Godot, VERTEX
specifies the vertex position in model space at the
beginning of the vertex
function. Godot also handles the final conversion to
clip space after the user-defined vertex
function is run. If you want to
skip the conversion from model to view space, you can set the render_mode
to
skip_vertex_transform
. If you want to skip all transforms, set
render_mode
to skip_vertex_transform
and set the PROJECTION_MATRIX
to mat4(1.0)
in order to nullify the final transform from view space to clip
space.
Varyings¶
Varyings are a type of variable that can be passed from the vertex shader to the
fragment shader. In modern GLSL (3.0 and up), varyings are defined with the
in
and out
keywords. A variable going out of the vertex shader is
defined with out
in the vertex shader and in
inside the fragment shader.
Main¶
In GLSL, each shader program looks like a self-contained C-style program.
Accordingly, the main entry point is main
. If you are copying a vertex
shader, rename main
to vertex
and if you are copying a fragment shader,
rename main
to fragment
.
Macros¶
The Godot shader preprocessor supports the following macros:
* #define
/ #undef
* #if
, #elif
, #else
, #endif
, defined()
, #ifdef
, #ifndef
* #include
(only .gdshaderinc
files and with a maximum depth of 25)
* #pragma disable_preprocessor
, which disables preprocessing for the rest of the file
Variables¶
GLSL has many built-in variables that are hard-coded. These variables are not uniforms, so they are not editable from the main program.
Variable |
Type |
Equivalent |
Description |
---|---|---|---|
gl_FragColor |
out vec4 |
COLOR |
Output color for each pixel. |
gl_FragCoord |
vec4 |
FRAGCOORD |
For full screen quads. For smaller quads, use UV. |
gl_Position |
vec4 |
VERTEX |
Position of Vertex, output from Vertex Shader. |
gl_PointSize |
float |
POINT_SIZE |
Size of Point primitive. |
gl_PointCoord |