Godot Shader笔记:3D着色器(三)

原文地址:Docs » Shading » Shading reference » Spatial shaders

内置片元属性(fragment built-ins)

Godot片元函数的默认用途是设置对象材质的属性,并使内置渲染器处理最终的着色。然而你并不需要使用所有这些属性,如果你对其不做任何改写,Godot将优化掉相应的功能。

Built-in Description
in vec4 FRAGCOORD Fragment coordinate, pixel adjusted. In screen space.
in mat4 WORLD_MATRIX Model space to world space transform.
in mat4 INV_CAMERA_MATRIX World space to view space transform.
in mat4 CAMERA_MATRIX View space to world space transform.
in mat4 PROJECTION_MATRIX View space to clip space transform.
in mat4 INV_PROJECTION_MATRIX Clip space to view space transform.
in float TIME Elapsed total time in seconds.
in vec2 VIEWPORT_SIZE Size of viewport (in pixels).
in vec3 VERTEX Vertex that comes from vertex function (default, in view space).
in vec3 VIEW Vector from camera to fragment position (in view space).
in bool FRONT_FACING True if current face is front face.
inout vec3 NORMAL Normal that comes from vertex function (default, in view space).
inout vec3 TANGENT Tangent that comes from vertex function.
inout vec3 BINORMAL Binormal that comes from vertex function.
out vec3 NORMALMAP Set normal here if reading normal from a texture instead of NORMAL.
out float NORMALMAP_DEPTH Depth from variable above. Defaults to 1.0.
in vec2 UV UV that comes from vertex function.
in vec2 UV2 UV2 that comes from vertex function.
in vec4 COLOR COLOR that comes from vertex function.
out vec3 ALBEDO Albedo (default white).
out float ALPHA Alpha (0..1); if written to, the material will go to the transparent pipeline.
out float METALLIC Metallic (0..1).
out float SPECULAR Specular. Defaults to 0.5, best not to modify unless you want to change IOR.
out float ROUGHNESS Roughness (0..1).
out float RIM Rim (0..1). If used, Godot calculates rim lighting.
out float RIM_TINT Rim Tint, goes from 0 (white) to 1 (albedo). If used, Godot calculates rim lighting.
out float CLEARCOAT Small added specular blob. If used, Godot calculates Clearcoat.
out float CLEARCOAT_GLOSS Gloss of Clearcoat. If used, Godot calculates Clearcoat.
out float ANISOTROPY For distorting the specular blob according to tangent space.
out vec2 ANISOTROPY_FLOW Distortion direction, use with flowmaps.
out float SSS_STRENGTH Strength of Subsurface Scattering. If used, Subsurface Scattering will be applied to object.
out vec3 TRANSMISSION Transmission mask (default 0,0,0). Allows light to pass through object. Only applied if used.
out float AO Strength of Ambient Occlusion. For use with pre-baked AO.
out float AO_LIGHT_AFFECT How much AO affects lights (0..1; default 0).
out vec3 EMISSION Emission color (can go over 1,1,1 for HDR).
sampler2D SCREEN_TEXTURE Built-in Texture for reading from the screen. Mipmaps contain increasingly blurred copies.
sampler2D DEPTH_TEXTURE Built-in Texture for reading depth from the screen. Must convert to linear using INV_PROJECTION.
out float DEPTH Custom depth value (0..1).
in vec2 SCREEN_UV Screen UV coordinate for current pixel.
in vec2 POINT_COORD Point Coordinate for drawing points with POINT_SIZE.
out float ALPHA_SCISSOR If written to, values below a certain amount of alpha are discarded.
in bool OUTPUT_IS_SRGB True when calculations happen in sRGB color space (true in GLES2, false in GLES3).
内置光属性(light built-ins)

自定义光函数完全是可选的。你可以将渲染模式render_mode设置为unshaded 从而忽略掉光函数。如果没有光函数,Godot将会使用片元函数中的材质属性来计算光照(取决于渲染模式render_mode)。

编写光函数实际上就是给DIFFUSE_LIGHT (慢反射光)或者SPECULAR_LIGHT (镜面反射光) 赋一些值。如果什么值都不赋,将意味着不处理任何光。

光函数会对每一个光源进行逐像素调用,并且会对每一种光源类型循环调用。

下面是一个使用兰博特光照模型(Lambertian lighting model)的自定义光函数的范例:

void light() {
 DIFFUSE_LIGHT += dot(NORMAL, LIGHT) * ATTENUATION * ALBEDO;
}

如果你想让光照叠加,可以使用+=运算符将各个光叠加到DIFFUSE_LIGHT

内置属性 描述
in vec4 FRAGCOORD Fragment coordinate, pixel adjusted.
in mat4 WORLD_MATRIX Model space to world space transform.
in mat4 INV_CAMERA_MATRIX World space to view space transform.
in mat4 CAMERA_MATRIX View space to world space transform.
in mat4 PROJECTION_MATRIX View space to clip space transform.
in mat4 INV_PROJECTION_MATRIX Clip space to view space transform.
in float TIME Elapsed total time in seconds.
in vec2 VIEWPORT_SIZE Size of viewport (in pixels).
in vec3 NORMAL Normal vector, in view space.
in vec3 VIEW View vector, in view space.
in vec3 LIGHT Light Vector, in view space.
in vec3 LIGHT_COLOR Color of light multiplied by energy.
in vec3 ATTENUATION Attenuation based on distance or shadow.
in vec3 ALBEDO Base albedo.
in vec3 TRANSMISSION Transmission mask.
in float ROUGHNESS Roughness.
out vec3 DIFFUSE_LIGHT Diffuse light result.
out vec3 SPECULAR_LIGHT Specular light result.
in bool OUTPUT_IS_SRGB True when calculations happen in sRGB color space (true in GLES2, false in GLES3).

你可能感兴趣的:(Godot Shader笔记:3D着色器(三))