CanvasItem(2D)シェーダー

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

CanvasItem shaders contain fewer built-in variables and functionality than Spatial shaders, but they maintain the same basic structure with vertex, fragment, and light processor functions.

レンダリングモード

レンダリングモード

説明

blend_mix

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

blend_add

加算ブレンドモード。

blend_sub

減算ブレンドモード。

blend_mul

乗法ブレンドモード。

blend_premul_alpha

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

blend_disabled

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

unshaded

ALBEDO をそのまま出力します。マテリアルではライティング/シェーディングを行いません。

light_only

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

skip_vertex_transform

VERTEX needs to be transformed manually in the vertex() function.

world_vertex_coords

VERTEX is modified in world coordinates instead of local.

ビルトイン

Values marked as in are read-only. Values marked as out can optionally be written to and will not necessarily contain sensible values. Values marked as inout provide a sensible default value, and can optionally be written to. Samplers cannot be written to so they are not marked.

Not all built-ins are available in all processing functions. To access a vertex built-in from the fragment() function, you can use a varying. The same applies for accessing fragment built-ins from the light() function.

グローバルビルトイン

グローバルビルトインは、カスタム関数を含め、どこでも使用できます。

ビルトイン

説明

in float TIME

Global time since the engine has started, in seconds. It repeats after every 3,600 seconds (which can be changed with the rollover setting). It's affected by time_scale but not by pausing. If you need a TIME variable that is not affected by time scale, add your own global shader uniform and update it each frame.

in float PI

PI 円周率の定数 (3.141592)。円の円周とその直径の比率、および半回転のラジアン量。

in float TAU

定数 TAU` は円周率の2倍 (6.283185)。 PI * 2 と、1回転のラジアン量に相当します。

in float E

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

頂点プロセッサーのビルトイン

頂点データ (VERTEX) はローカル空間 (Node2D の原点を基準としたピクセル座標) に表示されます。書き込まれない場合、これらの値は変更されずそのまま渡されます。

ユーザーはモデルのワールド変換を無効にし (ワールドからスクリーンへの変換と投影は後で行われます)、次のコードを使用して手動で行うことができます:

shader_type canvas_item;
render_mode skip_vertex_transform;

void vertex() {

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

Other built-ins, such as UV and COLOR, are also passed through to the fragment() function if not modified.

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

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

  • y: Phase during lifetime (0.0 to 1.0).

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

ビルトイン

説明

in mat4 MODEL_MATRIX

ローカル空間からワールド空間への変換。ワールド空間はエディタで通常使用される座標です。

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, 0.0) to viewport size.

in mat4 SCREEN_MATRIX

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

in int INSTANCE_ID

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

in vec4 INSTANCE_CUSTOM

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

in bool AT_LIGHT_PASS

常に false.

in vec2 TEXTURE_PIXEL_SIZE

デフォルトの2Dテクスチャの正規化されたピクセルサイズ。サイズが64x32pxのテクスチャを持つSprite2Dの場合、TEXTURE_PIXEL_SIZE = vec2(1/64, 1/32)

inout vec2 VERTEX

Vertex position, in local space.

in int VERTEX_ID

頂点バッファー内の現在の頂点インデックス。

inout vec2 UV

Normalized texture coordinates. Range from 0.0 to 1.0.

inout vec4 COLOR

Color from vertex primitive multiplied by CanvasItem's modulate multiplied by CanvasItem's self_modulate.

inout float POINT_SIZE

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

in vec4 CUSTOM0

Custom value from vertex primitive.

in vec4 CUSTOM1

Custom value from vertex primitive.

フラグメントプロセッサーのビルトイン

COLOR and TEXTURE

The built-in variable COLOR is used for a few things:

  • In the vertex() function, COLOR contains the color from the vertex primitive multiplied by the CanvasItem's modulate multiplied by the CanvasItem's self_modulate.

  • In the fragment() function, the input value COLOR is that same value multiplied by the color from the default TEXTURE (if present).

  • In the fragment() function, COLOR is also the final output.

Certain nodes (for example, Sprite2D) display a texture by default, for example texture. When using a custom fragment() function, you have a few options on how to sample this texture.

To read only the contents of the default texture, ignoring the vertex COLOR:

void fragment() {
  COLOR = texture(TEXTURE, UV);
}

To read the contents of the default texture multiplied by vertex COLOR:

void fragment() {
  // Equivalent to an empty fragment() function, since COLOR is also the output variable.
  COLOR = COLOR;
}

To read only the vertex COLOR in fragment(), ignoring the main texture, you must pass COLOR as a varying, then read it in fragment():

varying vec4 vertex_color;
void vertex() {
  vertex_color = COLOR;
}
void fragment() {
  COLOR = vertex_color;
}

NORMAL

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 NORMAL_MAP property. Godot will handle converting it for use in 2D and overwriting NORMAL.

NORMAL_MAP = texture(NORMAL_TEXTURE, UV).rgb;

ビルトイン

説明

in vec4 FRAGCOORD

Coordinate of pixel center. In screen space. xy specifies position in viewport. Upper-left of the viewport is the origin, (0.0, 0.0).

in vec2 SCREEN_PIXEL_SIZE

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

in vec4 REGION_RECT

Visible area of the sprite region in format (x, y, width, height). Varies according to Sprite2D's region_enabled property.

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

常に false.

sampler2D SPECULAR_SHININESS_TEXTURE

このオブジェクトの鏡面光沢テクスチャ。

in vec4 SPECULAR_SHININESS

テクスチャからサンプリングされた鏡面光沢色。

in vec2 UV

UV from the vertex() function. For Sprite2D with region enabled, this will sample the entire texture. Use REGION_RECT instead to sample only the region defined in the Sprite2D's properties.

in vec2 SCREEN_UV

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

sampler2D SCREEN_TEXTURE

Godot 4では削除されました。代わりに sampler2Dhint_screen_texture を使用してください。

inout vec3 NORMAL

Normal read from NORMAL_TEXTURE. Writable.

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

Normal map depth for scaling.

inout vec2 VERTEX

スクリーン空間内のピクセル位置。

inout vec2 SHADOW_VERTEX

VERTEX と同じですが、影を変更するために書き込むことができます。

inout vec3 LIGHT_VERTEX

VERTEX と同じですが、照明を変更するために書き込むことができます。 Z成分は高さを表します。

inout vec4 COLOR

COLOR from the vertex() function multiplied by the TEXTURE color. Also output color value.

ライトプロセッサーのビルトイン

ライトプロセッサー関数は、Godot 4.x と Godot 3.x では動作が異なります。 Godot 4.x では、すべてのライティングは通常の描画パス中に行われます。言い換えれば Godot はライトごとにオブジェクトを再描画しなくなりました。

Use the unshaded render mode if you do not want the light() function to run. Use the light_only render mode 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.

以下はCanvasItemの法線マップを考慮したライト シェーダーの例です。

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

Coordinate of pixel center. In screen space. xy specifies position in viewport. Upper-left of the viewport is the origin, (0.0, 0.0).

in vec3 NORMAL

Input normal.

in vec4 COLOR

Input color. This is the output of the fragment() function.

in vec2 UV

UV from the vertex() function, equivalent to the UV in the fragment() function.

sampler2D TEXTURE

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

in vec2 TEXTURE_PIXEL_SIZE

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

in vec2 SCREEN_UV

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

in vec2 POINT_COORD

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

in vec4 LIGHT_COLOR

Color of the Light2D. If the light is a PointLight2D, multiplied by the light's texture.

in float LIGHT_ENERGY

Energy multiplier of the Light2D.

in vec3 LIGHT_POSITION

Position of the Light2D in screen space. If using a DirectionalLight2D this is always (0.0, 0.0, 0.0).

in vec3 LIGHT_DIRECTION

Direction of the Light2D 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 Light2D.

in vec4 SPECULAR_SHININESS

オブジェクトのテクスチャで設定される鏡面光沢率。

out vec4 SHADOW_MODULATE

投影される影に掛ける色。

SDF関数

There are a few additional functions implemented to sample an automatically generated Signed Distance Field texture. These functions available for the fragment() and light() functions of CanvasItem shaders. Custom functions may also use them as long as they called from supported functions.

Signed Distance Field は、SDF Collision プロパティが有効(デフォルト設定) になっているシーン内に存在する LightOccluder2D ノードから生成されます。詳細については、2Dライトとシャドウ ドキュメントを参照してください。

関数

説明

float texture_sdf (vec2 sdf_pos)

SDFテクスチャのルックアップを実行します。

vec2 texture_sdf_normal (vec2 sdf_pos)

SDFテクスチャから法線を計算します。

vec2 sdf_to_screen_uv (vec2 sdf_pos)

Converts an SDF to screen UV.

vec2 screen_uv_to_sdf (vec2 uv)

Converts screen UV to an SDF.