2D CanvasItem Shader

CanvasItem-Shader (Canvas=Zeichenfläche) werden verwendet, um alle 2D-Elemente in Godot zu zeichnen. Dazu gehören alle Nodes, die von CanvasItems erben, sowie alle GUI-Elemente.

CanvasItem-Shader enthalten weniger integrierte Variablen und Funktionen als Spatial-Shader, behalten jedoch dieselbe Grundstruktur mit Vertex-, Fragment- und Lichtprozessorfunktionen bei.

Render-Modi

Render Modus

Beschreibung

blend_mix

Mischmodus (Alpha ist Transparenz), Standard.

blend_add

Additiver Mischungsmodus.

blend_sub

Subtraktiver Mischungsmodus.

blend_mul

Multiplikativer Mischungsmodus.

blend_premul_alpha

Vormultiplizierter Alpha-Mischmodus.

blend_disabled

Deaktiviert das Mischen. Werte (einschließlich Alpha) werden unverändert geschrieben.

unshaded

Das Ergebnis ist einfaches Albedo. Kein Licht/Schatten auf diesem Material.

light_only

Nur bei Lichtdurchlass zeichnen.

skip_vertex_transform

VERTEX/NORMAL/etc müssen manuell in die Vertex-Funktion transformiert werden.

integrierte Elemente

Mit "in" gekennzeichnete Werte sind schreibgeschützt. Mit "out" gekennzeichnete Werte dienen zum optionalen Schreiben und enthalten nicht unbedingt sinnvolle Werte. Als "inout" gekennzeichnete Werte stellen einen sinnvollen Standardwert dar und können optional beschrieben werden. Sampler können nicht geschrieben werden und sind nicht markiert.

Global integrierte Elemente

Globale integrierte Elemente sind überall verfügbar, einschließlich benutzerdefinierter Funktionen.

Eingebettet

Beschreibung

in float TIME

Global time since the engine has started, in seconds (always positive). It's subject to the rollover setting (which is 3,600 seconds by default). It's not affected by time_scale or pausing, but you can override the TIME variable's time scale by calling VisualServer.set_shader_time_scale() with the desired time scale factor as parameter (1.0 being the default).

integrierte Vertex-Elemente

Vertex data (VERTEX) is presented in local space (pixel coordinates, relative to the camera). If not written to, these values will not be modified and be passed through as they came.

Der Benutzer kann die integrierte Modellansichtstransformation deaktivieren (die Projektion erfolgt später noch) und manuell mit dem folgenden Code ausführen:

shader_type canvas_item;
render_mode skip_vertex_transform;

void vertex() {

    VERTEX = (WORLD_MATRIX * (EXTRA_MATRIX * vec4(VERTEX, 0.0, 1.0))).xy;
}

Bemerkung

WORLD_MATRIX ist eigentlich eine Modelansicht-Matrix. Es nimmt Eingaben im lokalen Raum auf und wandelt sie in den Sichtraum um.

In order to get the world space coordinates of a vertex, you have to pass in a custom uniform like so:

material.set_shader_param("global_transform", get_global_transform())

Dann in Ihrem Vertex-Shader:

uniform mat4 global_transform;
varying vec2 world_position;

void vertex(){
    world_position = (global_transform * vec4(VERTEX, 0.0, 1.0)).xy;
}

world_position kann dann entweder in den Vertex- oder Fragmentfunktionen verwendet werden.

Andere integrierte Funktionen wie UV und COLOR werden ebenfalls an die Fragmentfunktion übergeben, wenn sie nicht verändert wurden.

For instancing, the INSTANCE_CUSTOM variable contains the instance custom data. When using particles, this information is usually:

  • x: Rotationswinkel in Radiant.

  • y: Phase während der Laufzeit (0 bis 1).

  • z: Animations-Frame.

Eingebettet

Beschreibung

in mat4 WORLD_MATRIX

Bildraum zum Anzeigen der Raumtransformation.

in mat4 EXTRA_MATRIX

Extra Transformation.

in mat4 PROJECTION_MATRIX

Ansichtsbereich im Ausschnittsbereich umwandeln.

in vec4 INSTANCE_CUSTOM

Benutzerdefinierte Daten instanzieren.

in bool AT_LIGHT_PASS

true if this is a light pass.

inout vec2 VERTEX

Vertex, im Bildraum.

in vec2 TEXTURE_PIXEL_SIZE

Normalized pixel size of default 2D texture. For a Sprite with a texture of size 64x32px, TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)

inout vec2 UV

Textur Koordinaten.

inout vec4 COLOR

Farbe vom Vertex-Primitiv.

in vec4 MODULATE

Final modulate color. If used, COLOR will not be multiplied by modulate automatically after the fragment function.

inout float POINT_SIZE

Punktgröße für die Punktzeichnung.

eingebaute Fragmente-Typen

Certain Nodes (for example, Sprites) display a texture by default. However, when a custom fragment function is attached to these nodes, the texture lookup needs to be done manually. Godot does not provide the texture color in the COLOR built-in variable; to read the texture color for such nodes, use:

COLOR = texture(TEXTURE, UV);

This differs from the behavior of the built-in normal map. If a normal map is attached, Godot uses it by default and assigns its value to the built-in NORMAL variable. If you are using a normal map meant for use in 3D, it will appear inverted. In order to use it in your shader, you must assign it to the NORMALMAP property. Godot will handle converting it for use in 2D and overwriting NORMAL.

NORMALMAP = texture(NORMAL_TEXTURE, UV).rgb;

Eingebettet

Beschreibung

in vec4 FRAGCOORD

Coordinate of pixel center. In screen space. xy specifies position in window, z specifies fragment depth if DEPTH is not used. Origin is lower-left.

inout vec3 NORMAL

Normales Lesen von NORMAL_TEXTURE. Beschreibbar.

out vec3 NORMALMAP

Konfiguriert normale Karten für 3D zur Verwendung in 2D. Bei Verwendung wird NORMAL überschrieben.

inout float NORMALMAP_DEPTH

NormalMap-Tiefe für die Skalierung.

in vec2 UV

UV aus Vertex-Funktion.

inout vec4 COLOR

Color from vertex function and output fragment color. If unused, will be set to TEXTURE color.

in vec4 MODULATE

Final modulate color. If used, COLOR will not be multiplied by modulate automatically after the fragment function.

in sampler2D TEXTURE

Standard 2D Textur.

in sampler2D NORMAL_TEXTURE

Standard-2D-Normaltextur.

in vec2 TEXTURE_PIXEL_SIZE

Normalized pixel size of default 2D texture. For a Sprite with a texture of size 64x32px, TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)

in vec2 SCREEN_UV

Bildschirm UV zur Verwendung mit SCREEN_TEXTURE.

in vec2 SCREEN_PIXEL_SIZE

Größe der einzelnen Pixel. Entspricht dem Inversen der Auflösung.

in vec2 POINT_COORD

Koordinaten für Zeichnungspunkte.

in bool AT_LIGHT_PASS

true if this is a light pass.

in sampler2D SCREEN_TEXTURE

Bildschirmtextur, Mipmaps enthalten Gaußsche unscharfe Versionen.

Eingebaute Beleuchtungsarten

Light processor functions work differently in 2D than they do in 3D. In CanvasItem shaders, the shader is called once for the object being drawn, and then once for each light touching that object in the scene. Use render_mode unshaded if you do not want any light passes to occur for that object. Use render_mode light_only if you only want light passes to occur for that object; this can be useful when you only want the object visible where it is covered by light.

When the shader is on a light pass, the AT_LIGHT_PASS variable will be true.

Eingebettet

Beschreibung

in vec4 FRAGCOORD

Coordinate of pixel center. In screen space. xy specifies position in window, z specifies fragment depth if DEPTH is not used. Origin is lower-left.

in vec3 NORMAL

Input Normal. Although this value is passed in, normal calculation still happens outside of this function.

in vec2 UV

UV aus der Vertex-Funktion, äquivalent zum UV in der Fragment-Funktion.

in vec4 COLOR

Input Color. This is the output of the fragment function (with final modulation applied, if MODULATE is not used in any function of the shader).

in vec4 MODULATE

Final modulate color. If used, COLOR will not be multiplied by modulate automatically after the fragment function.

sampler2D TEXTURE

Aktuelle Textur für CanvasItem.

in vec2 TEXTURE_PIXEL_SIZE

Normalized pixel size of default 2D texture. For a Sprite with a texture of size 64x32px, TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)

in vec2 SCREEN_UV

SCREEN_TEXTURE Koordinate (zur Verwendung mit Bildschirmtextur).

in vec2 POINT_COORD

UV für Punkt Sprite.

inout vec2 LIGHT_VEC

Vector from light to fragment in local coordinates. It can be modified to alter illumination direction when normal maps are used.

inout vec2 SHADOW_VEC

Vector from light to fragment in local coordinates. It can be modified to alter shadow computation.

inout float LIGHT_HEIGHT

Höhe des Lichts. Nur wirksam, wenn Normals verwendet werden.

inout vec4 LIGHT_COLOR

Farbe des Lichts.

in vec2 LIGHT_UV

UV für Beleuchtungstextur.

out vec4 SHADOW_COLOR

Schattenfarbe des Lichts.

inout vec4 LIGHT

Value from the Light texture and output color. Can be modified. If not used, the light function is ignored.