画布项着色器
画布组件着色器用于绘制Godot中的所有二维元素。这包括从画布组件继承的所有节点,以及所有图形用户界面元素。
画布组件着色器比空间着色器包含更少的内置变量和功能,但它们与顶点、片段和光处理器功能保持相同的基本结构。
Render modes
Render mode | 描述 |
---|---|
blendmix | 混合混合模式(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 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). |
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 | 下一个坐标。 |
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 | 像素中心的坐标。在屏幕空间中。如果“DEPTH”未被使用,则“xy”指定窗口中的位置,“z”表示片段深度。原点在左下角。 |
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 | 值从浅色纹理和输出颜色。可以修改。如果不使用,则忽略光照函数。 |