天空着色器
Sky shaders are a special type of shader used for drawing sky backgrounds
and for updating radiance cubemaps which are used for image-based lighting
(IBL). Sky shaders only have one processing function, the sky()
function.
用到天空着色器的地方有三个。
首先是在你选择使用 Sky 作为场景的背景时,会使用天空着色器来绘制天空。
其次是在使用 Sky 作为环境光颜色或反射时,会使用天空着色器来更新辐射率立方体贴图。
最后是会使用天空着色器来绘制低分辨率的子阶段,可以用在高分辨率背景或立方体贴图阶段。
综上,一帧之内最多可能调用六次天空着色器,不过实际操作中很难达到那么多次,因为辐射率立方体贴图并不需要每帧都更新,并且也不是所有的子阶段都会用到。你可以根据调用的时机改变着色器的行为,检查布尔值 AT_*_PASS
即可。例如:
shader_type sky;
void sky() {
if (AT_CUBEMAP_PASS) {
// Sets the radiance cubemap to a nice shade of blue instead of doing
// expensive sky calculations
COLOR = vec3(0.2, 0.6, 1.0);
} else {
// Do expensive sky calculations for background sky only
COLOR = get_sky_color(EYEDIR);
}
}
使用天空着色器绘制背景时,屏幕上所有未被遮挡的片段都会执行该着色器。不过对于背景的子阶段而言,子阶段中的每个像素都会执行该着色器。
使用天空着色器更新辐射率立方体贴图时,立方体贴图中的每个像素都会执行该天空着色器。换句话说,只有在辐射率立方体贴图需要更新时才会执行该着色器。着色器参数发生改变后就需要更新辐射率立方体贴图。例如着色器中用到了 TIME
的话,那么辐射率立方体贴图就会需要每一帧都更新。下列更改会强制进行辐射率立方体贴图的更新:
用到
TIME
。用到
POSITION
并且相机位置发生了改变。用到
LIGHTX_*
属性并且 DirectionalLight3D 发生了改变。着色器中的任意 uniform 发生了改变。
屏幕大小发生了变化并且用到了子阶段。
Try to avoid updating the radiance cubemap needlessly. If you do need to update the radiance cubemap each frame, make sure your Sky process mode is set to PROCESS_MODE_REALTIME.
Note that the process mode only affects the rendering of the radiance cubemap. The visible sky is always rendered by calling the fragment shader for every pixel. With complex fragment shaders, this can result in a high rendering overhead. If the sky is static (the conditions listed above are met) or changes slowly, running the full fragment shader every frame is not needed. This can be avoided by rendering the full sky into the radiance cubemap, and reading from this cubemap when rendering the visible sky. With a completely static sky, this means that it needs to be rendered only once.
The following code renders the full sky into the radiance cubemap and reads from that cubemap for displaying the visible sky:
shader_type sky;
void sky() {
if (AT_CUBEMAP_PASS) {
vec3 dir = EYEDIR;
vec4 col = vec4(0.0);
// Complex color calculation
COLOR = col.xyz;
ALPHA = 1.0;
} else {
COLOR = texture(RADIANCE, EYEDIR).rgb;
}
}
This way, the complex calculations happen only in the cubemap pass, which can be optimized by setting the sky's process mode and the radiance size to get the desired balance between performance and visual fidelity.
渲染模式
Subpasses allow you to do more expensive calculations at a lower resolution to speed up your shaders. For example the following code renders clouds at a lower resolution than the rest of the sky:
shader_type sky;
render_mode use_half_res_pass;
void sky() {
if (AT_HALF_RES_PASS) {
// Run cloud calculation for 1/4 of the pixels
vec4 color = generate_clouds(EYEDIR);
COLOR = color.rgb;
ALPHA = color.a;
} else {
// At full resolution pass, blend sky and clouds together
vec3 color = generate_sky(EYEDIR);
COLOR = color + HALF_RES_COLOR.rgb * HALF_RES_COLOR.a;
}
}
渲染模式 |
描述 |
---|---|
use_half_res_pass |
允许着色器对半分辨率阶段进行写入和访问。 |
use_quarter_res_pass |
允许着色器对四分之一分辨率阶段进行写入和访问。 |
disable_fog |
使用后,雾不会影响天空。 |
内置
Values marked as in
are read-only. Values marked as out
can optionally
be written to and will not necessarily contain sensible values. Samplers cannot
be written to so they are not marked.
全局内置
Global built-ins are available everywhere, including in custom functions.
LIGHTX
灯光有 4 个,可以通过 LIGHT0
、LIGHT1
、LIGHT2
、LIGHT3
访问。
内置 |
描述 |
---|---|
in float TIME |
Global time since the engine has started, in seconds. It repeats after every |
in vec3 POSITION |
相机的位置,使用世界空间。 |
samplerCube RADIANCE |
辐射度立方体贴图。只能在背景阶段读取。使用前请检查 |
in bool AT_HALF_RES_PASS |
|
in bool AT_QUARTER_RES_PASS |
|
in bool AT_CUBEMAP_PASS |
|
in bool LIGHTX_ENABLED |
|
in float LIGHTX_ENERGY |
|
in vec3 LIGHTX_DIRECTION |
|
in vec3 LIGHTX_COLOR |
|
in float LIGHTX_SIZE |
Angular diameter of |
in float PI |
圆周率常量 |
in float TAU |
|
in float E |
|
Sky 内置
内置 |
描述 |
---|---|
in vec3 EYEDIR |
Normalized direction of current pixel. Use this as your basic direction for procedural effects. |
in vec2 SCREEN_UV |
Screen UV coordinate for current pixel. Used to map a texture to the full screen. |
in vec2 SKY_COORDS |
Sphere UV. Used to map a panorama texture to the sky. |
in vec4 HALF_RES_COLOR |
Color value of corresponding pixel from half resolution pass. Uses linear filter. |
in vec4 QUARTER_RES_COLOR |
Color value of corresponding pixel from quarter resolution pass. Uses linear filter. |
out vec3 COLOR |
输出颜色。 |
out float ALPHA |
Output alpha value, can only be used in subpasses. |
out vec4 FOG |