Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

CanvasItemシェーダー

CanvasItemシェーダーは、Godotですべての2D要素を描画するために使用されます。これらには、CanvasItemsから継承するすべてのノード、およびすべてのGUI要素が含まれます。

CanvasItemシェーダーには、Spatialシェーダーよりも少ない組み込み変数と関数が含まれていますが、頂点、フラグメント、およびライトプロセッサー関数と同じ基本構造を維持しています。

レンダリングモード

レンダリングモード

説明

blend_mix

ミックスブレンドモード(アルファは透明度)、デフォルト。

blend_add

加算ブレンドモード。

blend_sub

減算ブレンドモード。

blend_mul

乗法ブレンドモード。

blend_premul_alpha

事前乗算されたアルファブレンドモード。

blend_disabled

ブレンドを無効にすると、値(アルファを含む)がそのまま書き込まれます。

unshaded

結果は単なるアルベドです。マテリアルではライティング/シェーディングは発生しません。

light_only

ライトパスでのみ描画します。

skip_vertex_transform

VERTEX needs to be transformed manually in vertex function.

world_vertex_coords

VERTEX is modified in world coordinates instead of local.

Built-ins

"in"としてマークされた値は読み取り専用です。"out"とマークされた値はオプションの書き込み用であり、必ずしも適切な値を含むとは限りません。"inout"とマークされた値は、適切なデフォルト値を提供し、オプションで書き込むことができます。サンプラーは書込みの対象ではなく、マークされていません。

Global built-ins

Global built-ins are available everywhere, including custom functions.

ビルトイン

説明

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 define a global shader uniform to add a "scaled" TIME variable if desired.

in float PI

A PI constant (3.141592). A ration of circle's circumference to its diameter and amount of radians in half turn.

in float TAU

A TAU constant (6.283185). An equivalent of PI * 2 and amount of radians in full turn.

in float E

A E constant (2.718281). Euler's number and a base of the natural logarithm.

頂点用ビルトイン

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

The user can disable the built-in model to world transform (world to screen and projection will still happen later) and do it manually with the following code:

shader_type canvas_item;
render_mode skip_vertex_transform;

void vertex() {

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

UVやCOLORなどの他のビルトインも、変更されていない場合はフラグメント関数に渡されます。

インスタンス化の場合、INSTANCE_CUSTOM変数にはインスタンスのカスタムデータが含まれます。パーティクルを使用する場合、この情報は通常次のとおりです:

  • x: ラジアン単位の回転角度。

  • y: ライフタイム中のフェーズ(0〜1)。

  • z: アニメーションフレーム。

ビルトイン

説明

in mat4 MODEL_MATRIX

Local space to world space transform. World space is the coordinates you normally use in the editor.

in mat4 CANVAS_MATRIX

World space to canvas space transform. In canvas space the origin is the upper-left corner of the screen and coordinates ranging from (0, 0) to viewport size.

in mat4 SCREEN_MATRIX

Canvas space to clip space. In clip space coordinates ranging from (-1, -1) to (1, 1).

in int INSTANCE_ID

インスタンス化のためのインスタンスID。

in vec4 INSTANCE_CUSTOM

インスタンスのカスタムデータ。

in bool AT_LIGHT_PASS

Always false.

in vec2 TEXTURE_PIXEL_SIZE

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

inout vec2 VERTEX

Vertex, in local space.

in int VERTEX_ID

The index of the current vertex in the vertex buffer.

inout vec2 UV

Normalized texture coordinates. Range from 0 to 1.

inout vec4 COLOR

頂点プリミティブの色。

inout float POINT_SIZE

ポイント描画のポイントサイズ。

フラグメント用ビルトイン

Certain Nodes (for example, Sprite2Ds) 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 provides the texture color in the COLOR built-in variable multiplied by the node's color. To read the texture color by itself, you can use:

COLOR = texture(TEXTURE, UV);

Similarly, if a normal map is used in the CanvasTexture, 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;

ビルトイン

説明

in vec4 FRAGCOORD

ピクセルの中心の座標。画面スペース内。xy はウィンドウ内の位置を指定し、zDEPTH を使用しない場合のフラグメントの深さを指定します。原点は左下です。

in vec2 SCREEN_PIXEL_SIZE

個々のピクセルのサイズ。解像度の逆数に等しい。

in vec2 POINT_COORD

描画ポイントの座標。

sampler2D TEXTURE

デフォルトの2Dテクスチャ。

in vec2 TEXTURE_PIXEL_SIZE

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

in bool AT_LIGHT_PASS

Always false.

sampler2D SPECULAR_SHININESS_TEXTURE

Specular shininess texture of this object.

in vec4 SPECULAR_SHININESS

Specular shininess color, as sampled from the texture.

in vec2 UV

頂点関数からのUV。

in vec2 SCREEN_UV

現在のピクセルのスクリーンUV座標。

sampler2D SCREEN_TEXTURE

Removed in Godot 4. Use a sampler2D with hint_screen_texture instead.

inout vec3 NORMAL

**NORMAL_TEXTURE ** からの法線の読み込み。書き込み可能。

sampler2D NORMAL_TEXTURE

デフォルトの2D法線テクスチャ。

out vec3 NORMAL_MAP

Configures normal maps meant for 3D for use in 2D. If used, overrides NORMAL.

out float NORMAL_MAP_DEPTH

スケーリングのための法線マップの深さ。

inout vec2 VERTEX

Pixel position in screen space.

inout vec2 SHADOW_VERTEX

Same as VERTEX but can be written to alter shadows.

inout vec3 LIGHT_VERTEX

Same as VERTEX but can be written to alter lighting. Z component represents height.

inout vec4 COLOR

Color from vertex function multiplied by the TEXTURE color. Also output color value.

ライト用ビルトイン

Light processor functions work differently in Godot 4.x than they did in Godot 3.x. In Godot 4.x all lighting is done during the regular draw pass. In other words, Godot no longer draws the object again for each light.

Use render_mode unshaded if you do not want the light processor function to run. Use render_mode light_only if you only want to see the impact of lighting on an object; this can be useful when you only want the object visible where it is covered by light.

If you define a light function it will replace the built in light function, even if your light function is empty.

Below is an example of a light shader that takes a CanvasItem's normal map into account:

void light() {
  float cNdotL = max(0.0, dot(NORMAL, LIGHT_DIRECTION));
  LIGHT = vec4(LIGHT_COLOR.rgb * COLOR.rgb * LIGHT_ENERGY * cNdotL, LIGHT_COLOR.a);
}

ビルトイン

説明

in vec4 FRAGCOORD

ピクセルの中心の座標。画面スペース内。xy はウィンドウ内の位置を指定し、zDEPTH を使用しない場合のフラグメントの深さを指定します。原点は左下です。

in vec3 NORMAL

Input Normal.

in vec4 COLOR

Input Color. This is the output of the fragment function.

in vec2 UV

頂点関数からのUV。フラグメント関数のUVと同等です。

sampler2D TEXTURE

CanvasItemに使用されている現在のテクスチャ。

in vec2 TEXTURE_PIXEL_SIZE

Normalized pixel size of TEXTURE. For a Sprite2D with a TEXTURE of size 64x32px, TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)

in vec2 SCREEN_UV

現在のピクセルのスクリーンUV座標。

in vec2 POINT_COORD

ポイントスプライトのUV。

in vec4 LIGHT_COLOR

Color of Light multiplied by Light's texture.

in float LIGHT_ENERGY

Energy multiplier of Light.

in vec3 LIGHT_POSITION

Position of Light in screen space. If using a DirectionalLight2D this is always vec3(0,0,0).

in vec3 LIGHT_DIRECTION

Direction of Light in screen space.

in bool LIGHT_IS_DIRECTIONAL

true if this pass is a DirectionalLight2D.

in vec3 LIGHT_VERTEX

Pixel position, in screen space as modified in the fragment function.

inout vec4 LIGHT

Output color for this Light.

in vec4 SPECULAR_SHININESS

Specular shininess, as set in the object's texture.

out vec4 SHADOW_MODULATE

Multiply shadows cast at this point by this color.

SDF functions

There are a few additional functions implemented to sample an automatically generated Signed Distance Field texture. These functions available for Fragment and Light functions of CanvasItem shaders.

The signed distance field is generated from LightOccluder2D nodes present in the scene with the SDF Collision property enabled (which is the default). See the 2D lights and shadows documentation for more information.

関数

説明

float texture_sdf (vec2 sdf_pos)

Performs an SDF texture lookup.

vec2 texture_sdf_normal (vec2 sdf_pos)

Calculates a normal from the SDF texture.

vec2 sdf_to_screen_uv (vec2 sdf_pos)

SDFをスクリーンUVに変換します。

vec2 screen_uv_to_sdf (vec2 uv)

Converts screen UV to a SDF.