Converting GLSL to Godot shaders¶
Dieses Dokument erklärt die Unterschiede zwischen Godots Shader-Sprache und GLSL und gibt praktische Ratschläge zur Migration von Shadern aus anderen Quellen wie Shadertoy und The Book of Shaders in Godot-Shader.
Genauere Informationen über Godots Shader-Sprache gibt es im eigenen Artikel: Shading Language.
GLSL¶
Godot verwendet eine auf GLSL basierende Shader-Sprache mit einigen verbesserten Funktionen. Dementsprechend sind die meisten in GLSL verfügbaren Funktionen in Godots Shader-Sprache verfügbar.
Shader Programme¶
In GLSL verwendet jeder Shader ein separates Programm. Sie haben ein Programm für den Vertex-Shader und eines für den Fragment-Shader. In Godot haben Sie einen einzelnen Shader, der eine vertex
- und/oder eine fragment
-Funktion enthält. Wenn Sie nur eines schreiben, liefert Godot das andere.
Mit Godot können einheitliche Variablen und Funktionen gemeinsam genutzt werden, indem die Fragment- und Vertex-Shader in einer Datei definiert werden. In GLSL können die Vertex- und Fragment-Programme nur dann Variablen gemeinsam nutzen, wenn Variationen verwendet werden.
Vertex-Attribute¶
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
. For a complete list, see the Shading language
reference.
gl_Position (OpenGL)¶
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.
Variiert¶
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.
Haupt¶
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
.
Konstanten¶
Global array constants are not supported in Godot 3.x. You can fake the functionality by using a uniform initialized to the value, but you will not benefit from the increased speed from using a constant.
Makros¶
Entsprechend der Ähnlichkeit mit C können Sie mit GLSL Makros verwenden. Im Allgemeinen wird #define
verwendet, um Konstanten oder kleine Funktionen zu definieren. Es gibt keine einfache Möglichkeit Definitionen in Godots Shader-Sprache zu übersetzen. Wenn es sich um eine definierte Funktion handelt ersetzen Sie sie durch eine Funktion. Wenn es sich um eine Konstante handelt, ersetzen Sie sie durch ein Uniform. Für andere Makros (#if
, #ifdef
usw.) gibt es kein Äquivalent, da sie während der Präprozessorphase der Kompilierung ausgeführt werden.
Variablen¶
GLSL verfügt über viele integrierte, fest codierte Variablen. Diese Variablen sind keine Uniforms, daher können sie nicht vom Hauptprogramm aus bearbeitet werden.
Variable |
Art |
Equivalent |
Beschreibung |
---|---|---|---|
gl_FragColor |
out vec4 |
COLOR |
Ausgabefarbe für jeden Pixel. |
gl_FragCoord |
vec4 |
FRAGCOORD |
Für Vollbild-Quadrate. Verwenden Sie für kleinere Quadrate UV. |
gl_Position (OpenGL) |
vec4 |
VERTEX |
Position des Vertex, Ausgabe vom Vertex-Shader. |
gl_PointSize |
float |
POINT_SIZE |
Größe des Punkt-Primitivs. |
gl_PointCoord |
vec2 |
POINT_COORD |
Position on point when drawing Point primitives. |
gl_FrontFacing |
bool |
FRONT_FACING |
True if front face of primitive. |
Koordinaten¶
gl_FragCoord
in GLSL and FRAGCOORD
in the Godot shading language use the
same coordinate system. If using UV in Godot, the y-coordinate will be flipped
upside down.
Präzision¶
In GLSL, you can define the precision of a given type (float or int) at the top
of the shader with the precision
keyword. In Godot, you can set the
precision of individual variables as you need by placing precision qualifiers
lowp
, mediump
, and highp
before the type when defining the variable.
For more information, see the Shading Language
reference.
Shadertoy¶
Shadertoy is a website that makes it easy to write fragment shaders and create pure magic.
Shadertoy does not give the user full control over the shader. It handles all the input and uniforms and only lets the user write the fragment shader.
Typen¶
Shadertoy uses the webgl spec, so it runs a slightly different version of GLSL. However, it still has the regular types, including constants and macros.
mainImage¶
The main point of entry to a Shadertoy shader is the mainImage
function.
mainImage
has two parameters, fragColor
and fragCoord
, which
correspond to COLOR
and FRAGCOORD
in Godot, respectively. These
parameters are handled automatically in Godot, so you do not need to include
them as parameters yourself. Anything in the mainImage
function should be
copied into the fragment
function when porting to Godot.
Variablen¶
In order to make writing fragment shaders straightforward and easy, Shadertoy handles passing a lot of helpful information from the main program into the fragment shader for you. A few of these have no equivalents in Godot because Godot has chosen not to make them available by default. This is okay because Godot gives you the ability to make your own uniforms. For variables whose equivalents are listed as "Provide with Uniform", users are responsible for creating that uniform themselves. The description gives the reader a hint about what they can pass in as a substitute.
Variable |
Art |
Equivalent |
Beschreibung |
---|---|---|---|
fragColor |
out vec4 |
COLOR |
Ausgabefarbe für jeden Pixel. |
fragCoord |
vec2 |
FRAGCOORD.xy |
Für Vollbild-Quadrate. Verwenden Sie für kleinere Quadrate UV. |
iResolution |
vec3 |
1.0 / SCREEN_PIXEL_SIZE |
Kann auch manuell übergeben werden. |
iTime |
float |
TIME |
Zeit seit dem Start des Shaders. |
iTimeDelta |
float |
Mit Uniform versehen |
Zeit um den vorherigen Frame zu rendern. |
iFrame |
float |
Mit Uniform versehen |
Frame Nummer. |
iChannelTime[4] |
float |
Mit Uniform versehen |
Zeit seit Start der gegebenen Textur. |
iMouse |
vec4 |
Mit Uniform versehen |
Mausposition in Pixelkoordinaten. |
iDate |
vec4 |
Mit Uniform versehen |
Aktuelles Datum in Sekunden. |
iChannelResolution[4] |
vec3 |
1.0 / TEXTURE_PIXEL_SIZE |
Auflösung der gegebenen Textur. |
iChanneli |
Sampler2D |
TEXTURE |
Godot bietet nur einen eingebauten; Nutzer können mehr erzeugen. |
Koordinaten¶
fragCoord
verhält sich genauso wie gl_FragCoord
in GLSL und FRAGCOORD
in Godot.
Das Buch der Shader¶
Ähnlich wie bei Shadertoy bietet The Book of Shaders Zugriff auf einen Fragment-Shader im Webbrowser, mit dem der Benutzer interagieren kann. Der Benutzer kann nur Fragment-Shader-Code mit einer festgelegten Liste von übergebenen Uniforms schreiben und kann keine zusätzlichen Uniforms hinzufügen.
Für weitere Hilfe beim Portieren von Shadern auf verschiedene Frameworks im Allgemeinen bietet The Book of Shaders eine Seite zum Ausführen von Shadern in verschiedenen Frameworks.
Typen¶
Das Buch der Shader verwendet die Webgl-Spezifikation, sodass eine etwas andere Version von GLSL ausgeführt wird. Es gibt jedoch weiterhin die regulären Typen, einschließlich Konstanten und Makros.
Haupt¶
Der Einstiegspunkt für einen Buch der Shader Fragment-Shader ist main
, genau wie in GLSL. Alles was in einer main
-Funktion von Buch der Shader geschrieben ist, sollte in Godots fragment
-Funktion kopiert werden.
Variablen¶
Das Buch der Shader hält sich genauer an GLSL als Shadertoy. Es implementiert auch weniger Uniforms als Shadertoy.
Variable |
Art |
Equivalent |
Beschreibung |
---|---|---|---|
gl_FragColor |
out vec4 |
COLOR |
Ausgabefarbe für jeden Pixel. |
gl_FragCoord |
vec4 |
FRAGCOORD |
Für Vollbild-Quadrate. Verwenden Sie für kleinere Quadrate UV. |
u_resolution |
vec2 |
1.0 / SCREEN_PIXEL_SIZE |
Kann auch manuell übergeben werden. |
u_time |
float |
TIME |
Zeit seit dem Start des Shaders. |
u_mouse |
vec2 |
Mit Uniform versehen |
Mausposition in Pixelkoordinaten. |
Koordinaten¶
Das Buch der Shader verwendet dasselbe Koordinatensystem wie GLSL.