画布项着色器

画布组件着色器用于绘制Godot中的所有二维元素。这包括从画布组件继承的所有节点,以及所有图形用户界面元素。

画布组件着色器比空间着色器包含更少的内置变量和功能,但它们与顶点、片段和光处理器功能保持相同的基本结构。

Render modes

Render mode 描述
blend_mix 混合混合模式(alpha是透明度),默认。
blend_add 添加剂混合模式。
blend_sub 减法混合模式。
blend_mul 乘法混合模式。
blend_premul_alpha Pre-multiplied alpha blend mode.
混合_禁用 禁用混合,值(包括透明通道)按原样编写。
unshaded 结果只是反照率。 材质中不会发生照明/阴影。
light_only Only draw on light pass.
skip_vertex_transform VERTEX/NORMAL/etc need to be transformed manually in vertex function.

Built-ins

Values marked as "in" are read-only. Values marked as "out" are for optional writing and will not necessarily contain sensible values. Values marked as "inout" provide a sensible default value, and can optionally be written to. Samplers are not subjects of writing and they are not marked.

Global built-ins

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

内建的 描述
in float TIME Global time, in seconds. It's subject to the rollover setting (which is 3,600 -1 hour- by default).

Vertex built-ins

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.

The user can disable the built-in modelview transform (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 = (EXTRA_MATRIX * (WORLD_MATRIX * vec4(VERTEX, 0.0, 1.0))).xy;
}

注解

“世界_矩阵”实际上是一个模型视图矩阵。它接受本地空间中的输入,并将其转换为视图空间。

为了得到顶点的世界空间坐标,你必须通过一个定制的统一值,如下所示:

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

然后,在你的顶点着色器:

uniform mat4 global_transform;
varying vec2 world_position;

void vertex(){
    world_position = (global_transform * 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.

对于实例化,INSTANCE_CUSTOM变量包含实例自定义数据。 使用粒子时,此信息通常是:

  • ** x**: 以弧度表示的旋转角度。
  • ** y**: 寿命期间的相位(0到1)。
  • ** z**: 动画帧。
内建的 描述
在mat4 ** WORLD_MATRIX ** 图像空间到视图空间的转换。
in mat4 EXTRA_MATRIX 额外的转变。
in mat4 PROJECTION_MATRIX 查看空间以剪切空间变换。
in vec4 INSTANCE_CUSTOM 实例自定义数据。
in bool AT_LIGHT_PASS true if this is a light pass.
inout vec2 VERTEX Vertex, in image space.
in vec2 TEXTURE_PIXEL_SIZE 默认2D纹理的标准化像素大小。 对于纹理大小为64x32px的Sprite,** TEXTURE_PIXEL_SIZE ** =:code:vec2(1 / 64,1 / 32)
inout vec2 UV UV.
inout vec4 COLOR 来自顶点原语的颜色。
in vec4 MODULATE Final modulate color. If used, COLOR will not be multiplied by modulate automatically after the fragment function.
inout float POINT_SIZE 点绘图的点大小。

Fragment built-ins

某些节点(例如:ref:精灵 <class_Sprite>)默认情况下显示纹理。但是,当自定义片段函数附加到这些节点时,需要手工完成纹理的查找。Godot在“颜色”内置变量中不提供纹理颜色;要读取这些节点的纹理颜色,请使用:

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;
内建的 描述
在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 Normal read from NORMAL_TEXTURE. Writable.
out vec3 NORMALMAP 配置用于三维的法线贴图,以便在二维中使用。如果使用,则覆盖**法线**。
inout float NORMALMAP_DEPTH 用于缩放的法线贴图深度。
in vec2 UV 来自顶点功能的UV。
inout vec4 COLOR 从顶点函数和输出片段颜色。如果未使用,将设置为**纹理**颜色。
in vec4 MODULATE Final modulate color. If used, COLOR will not be multiplied by modulate automatically after the fragment function.
in sampler2D TEXTURE 默认的2D纹理。
in sampler2D NORMAL_TEXTURE 默认2D普通纹理。
in vec2 TEXTURE_PIXEL_SIZE 默认2D纹理的标准化像素大小。 对于纹理大小为64x32px的Sprite,** TEXTURE_PIXEL_SIZE ** =:code:vec2(1 / 64,1 / 32)
in vec2 SCREEN_UV Screen UV for use with SCREEN_TEXTURE.
in vec2 SCREEN_PIXEL_SIZE 单个像素的大小。 等于分辨率的倒数。
in vec2 POINT_COORD 协调绘图点。
in bool AT_LIGHT_PASS true if this is a light pass.
in sampler2D SCREEN_TEXTURE 屏幕纹理,mipmap包含高斯模糊版本。

内置灯光

光照处理器功能在二维中与在三维中工作不同。在画布组件着色时,着色器为被绘制的对象调用一次,然后每一束光接触场景中的物体一次。如果您不希望该对象发生任何光传递,请使用渲染_模式中的“无阴影”。如果您只想让光通过该对象,使用渲染_模式中的“仅有_光照”;当您只想让被光覆盖的对象可见时,这是非常有用的。

当着色器处于光通道时,“在_光照_通道”变量将为“真”。

内建的 描述
在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 输入正常。 虽然传入了这个值,但 正常计算仍然发生在此函数之外
in vec2 UV 来自顶点函数的UV,相当于片段函数中的UV。
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 CanvasItem使用的当前纹理。
in vec2 TEXTURE_PIXEL_SIZE 默认2D纹理的标准化像素大小。 对于纹理大小为64x32px的Sprite,** TEXTURE_PIXEL_SIZE ** =:code:vec2(1 / 64,1 / 32)
in vec2 SCREEN_UV SCREEN_TEXTURE Coordinate (for using with screen texture).
in vec2 POINT_COORD UV for Point 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 光照的高度。只有在使用法线时才有效。
inout vec4 LIGHT_COLOR 光的颜色。
in vec2 LIGHT_UV 紫外线对质地轻。
out vec4 SHADOW_COLOR Shadow Color of Light.
inout vec4 LIGHT 值从浅色纹理和输出颜色。可以修改。如果不使用,则忽略光照函数。