Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Shaders spatiaux
Spatial shaders are used for shading 3D objects. They are the most complex type of shader Godot offers. Spatial shaders are highly configurable with different render modes and different rendering options (e.g. Subsurface Scattering, Transmission, Ambient Occlusion, Rim lighting, etc.). Users can optionally write vertex, fragment, and light processor functions to affect how objects are drawn.
Mode de rendu
For visual examples of these render modes, see Standard Material 3D and ORM Material 3D.
Mode de rendu |
Description |
|---|---|
blend_mix |
Mode de fusion par mélange (alpha est la transparence), par défaut. |
blend_add |
Mode de fusion additif. |
blend_sub |
Mode de fusion substractif. |
blend_mul |
Mode de fusion multiplicatif. |
blend_premul_alpha |
Premultiplied alpha blend mode (fully transparent = add, fully opaque = mix). |
depth_draw_opaque |
Rend seulement la profondeur pour la géométrie opaque (non-transparente). |
depth_draw_always |
Rend toujours la profondeur (opaque et transparent). |
depth_draw_never |
Ne rend jamais la profondeur. |
depth_prepass_alpha |
Fait une pré-passe de profondeur opaque pour les géométries transparentes. |
depth_test_disabled |
Désactive le test de profondeur. |
depth_test_default |
Depth test will discard the pixel if it is behind other pixels. In Forward+ only, the pixel is also discarded if it's at the exact same depth as another pixel. |
depth_test_inverted |
Depth test will discard the pixel if it is in front of other pixels. Useful for stencil effects. |
sss_mode_skin |
Subsurface Scattering mode for skin (optimizes visuals for human skin, e.g. boosted red channel). |
cull_back |
Élimine les faces arrière (par défaut). |
cull_front |
Élimine les faces avant. |
cull_disabled |
Élimination des faces désactivé (double face). |
unshaded |
Result is just albedo. No lighting/shading happens in material, making it faster to render. |
wireframe |
Geometry draws using lines (useful for troubleshooting).
When using the Compatibility renderer, you must call
|
debug_shadow_splits |
Directional shadows are drawn using different colors for each split (useful for troubleshooting). |
diffuse_burley |
Burley (Disney PBS) for diffuse (default). |
diffuse_lambert |
Lambert shading for diffuse. |
diffuse_lambert_wrap |
Lambert-wrap shading (roughness-dependent) for diffuse. |
diffuse_toon |
Ombrage toon pour la diffuse. |
specular_schlick_ggx |
Schlick-GGX for direct light specular lobes (default). |
specular_toon |
Toon for direct light specular lobes. |
specular_disabled |
Disable direct light specular lobes. Doesn't affect reflected light (use |
skip_vertex_transform |
|
world_vertex_coords |
|
ensure_correct_normals |
Use when non-uniform scale is applied to mesh (note: currently unimplemented). |
shadows_disabled |
Disable computing shadows in shader. The shader will not receive shadows, but can still cast them. |
ambient_light_disabled |
Désactive la contribution de la lumière ambiante et de la carte de radiance. |
shadow_to_opacity |
L'éclairage modifie la transparence alpha de sorte que les zones ombrées soient opaques et les zones non ombrées soient transparentes. Cela peut servir à surimposer des ombres sur des images fournie par une caméra en réalité augmentée. |
vertex_lighting |
Use vertex-based lighting instead of per-pixel lighting. |
particle_trails |
Enables the trails when used on particle geometry. |
alpha_to_coverage |
Mode d'anticrénelage pour la transparence, voir ici pour en savoir plus. |
alpha_to_coverage_and_one |
Mode d'anticrénelage pour la transparence, voir ici pour en savoir plus. |
fog_disabled |
Disable receiving depth-based or volumetric fog. Useful for |
Stencil modes
Note
Stencil support is experimental, use at your own risk. We will try to not break compatibility as much as possible, but if significant flaws are found in the API, it may change in the next minor version.
Stencil operations are a set of operations that allow writing to an efficient buffer in an hardware-accelerated manner. This is generally used to mask in or out parts of the scene.
Some of the most well-known uses are:
Outlines: Mask out the inner mesh that is being outlined to avoid inner outlines.
X-Ray: Display a mesh behind other objects.
Portals: Draw geometry that is normally "impossible" (non-Euclidian) by masking objects.
Note
You can only read from the stencil buffer in the transparent pass. Any attempt to read in the opaque pass will fail, as it's currently not supported behavior.
Note that for compositor effects, the main renderer's stencil buffer can't be copied to a custom texture.
Stencil mode |
Description |
|---|---|
read |
Read from the stencil buffer. |
write |
Write reference value to the stencil buffer. |
write_if_depth_fail |
Write reference value to the stencil buffer if the depth test fails. |
compare_always |
Always pass stencil test. |
compare_equal |
Pass stencil test if the reference value is equal to the stencil buffer value. |
compare_not_equal |
Pass stencil test if the reference value is not equal to the stencil buffer value. |
compare_less |
Pass stencil test if the reference value is less than the stencil buffer value. |
compare_less_or_equal |
Pass stencil test if the reference value is less than or equal to the stencil buffer value. |
compare_greater |
Pass stencil test if the reference value is greater than the stencil buffer value. |
compare_greater_or_equal |
Pass stencil test if the reference value is greater than or equal to the stencil buffer value. |
Variables intégrées
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.
Variables intégrées Globales
Les modules intégrés globaux sont disponibles partout, y compris dans les fonctions personnalisées.
Intégré |
Description |
|---|---|
in float TIME |
Global time since the engine has started, in seconds. It repeats after every |
in float PI |
A |
in float TAU |
A |
in float E |
An |
in bool OUTPUT_IS_SRGB |
|
in float CLIP_SPACE_FAR |
Clip space far |
in bool IS_MULTIVIEW |
|
in bool IN_SHADOW_PASS |
|
Variables intégrées de sommet
Les données de sommet (VERTEX, NORMAL, TANGENT, et BITANGENT) sont présentées dans l'espace du modèle (aussi appelé espace local). Si elles ne sont pas réécrites, ces valeurs ne seront pas modifiées et seront transmises telles quelles, puis transformées dans l'espace de la vue pour être utilisées dans fragment().
Elles peuvent optionnellement être présentées dans l'espace global en utilisant le mode de rendu world_vertex_coords.
Les utilisateurs peuvent désactiver la transformation modèle-vue intégrée (la projection aura quand même lieu plus tard) et s'en occuper manuellement avec le code suivant :
shader_type spatial;
render_mode skip_vertex_transform;
void vertex() {
VERTEX = (MODELVIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
NORMAL = normalize((MODELVIEW_MATRIX * vec4(NORMAL, 0.0)).xyz);
BINORMAL = normalize((MODELVIEW_MATRIX * vec4(BINORMAL, 0.0)).xyz);
TANGENT = normalize((MODELVIEW_MATRIX * vec4(TANGENT, 0.0)).xyz);
}
D'autres variables intégrées, telles que UV, UV2 et COLOR, sont aussi directement transmises à la fonction fragment() si elles ne sont pas modifiées.
Users can override the modelview and projection transforms using the POSITION built-in. If POSITION is written
to anywhere in the shader, it will always be used, so the user becomes responsible for ensuring that it always has
an acceptable value. When POSITION is used, the value from VERTEX is ignored and projection does not happen.
However, the value passed to the fragment shader still comes from VERTEX.
For instancing, the INSTANCE_CUSTOM variable contains the instance custom data. When using particles, this information
is usually:
x : Angle de rotation en radians.
y: Phase during lifetime (
0.0to1.0).z : Trame d'animation.
This allows you to easily adjust the shader to a particle system using default particle material. When writing a custom particle shader, this value can be used as desired.
Intégré |
Description |
|---|---|
in vec2 VIEWPORT_SIZE |
Taille de la fenêtre d'affichage (en pixels). |
in mat4 VIEW_MATRIX |
Transformation de l'espace global à l'espace de vue. |
in mat4 INV_VIEW_MATRIX |
Transformation de l'espace de vue à l'espace global. |
in mat4 MAIN_CAM_INV_VIEW_MATRIX |
View space to world space transform of the camera used to draw the current viewport. |
in mat4 INV_PROJECTION_MATRIX |
Transformation de l'espace de découpage à l'espace de vue. |
in vec3 NODE_POSITION_WORLD |
Node position, in world space. |
in vec3 NODE_POSITION_VIEW |
Node position, in view space. |
in vec3 CAMERA_POSITION_WORLD |
Camera position, in world space. Represents the midpoint of the two eyes when in multiview/stereo rendering. |
in vec3 CAMERA_DIRECTION_WORLD |
Camera direction, in world space. |
in uint CAMERA_VISIBLE_LAYERS |
Cull layers of the camera rendering the current pass. |
in int INSTANCE_ID |
Identifiant de l'instance pour l'instanciation. |
in vec4 INSTANCE_CUSTOM |
Données personnalisées de l'instance (pour les particules, principalement). |
in int VIEW_INDEX |
The view that we are rendering.
|
in int VIEW_MONO_LEFT |
Constant for Mono or left eye, always |
in int VIEW_RIGHT |
Constante pour l’œil droit, toujours |
in vec3 EYE_OFFSET |
Position offset for the eye being rendered, in view space. Only applicable for multiview rendering. |
inout vec3 VERTEX |
Position of the vertex, in model space.
In world space if |
in int VERTEX_ID |
L'index du sommet actuel dans le buffer des sommets. |
inout vec3 NORMAL |
Normale dans l'espace modèle. Dans l'espace global si |
inout vec3 TANGENT |
Tangente dans l'espace modèle. Dans l'espace global si |
inout vec3 BINORMAL |
Binormale dans l'espace modèle. Dans l'espace global si |
out vec4 POSITION |
If written to on any branch, overrides final vertex position in clip space. |
inout vec2 UV |
Canal UV principal. |
inout vec2 UV2 |
Canal UV secondaire. |
inout vec4 COLOR |
Color from vertices. Limited to values between |
out float ROUGHNESS |
Rugosité pour l'éclairage du sommet. |
inout float POINT_SIZE |
Taille des points pour le rendu en points. |
inout mat4 MODELVIEW_MATRIX |
Transformation de l'espace du modèle/local vers l'espace de la vue (à utiliser si possible). |
inout mat3 MODELVIEW_NORMAL_MATRIX |
|
in mat4 MODEL_MATRIX |
Transformation de l'espace du modèle/local à l'espace global. |
in mat3 MODEL_NORMAL_MATRIX |
|
inout mat4 PROJECTION_MATRIX |
Transformation de l'espace de vue à l'espace de découpage. |
in uvec4 BONE_INDICES |
|
in vec4 BONE_WEIGHTS |
|
in vec4 CUSTOM0 |
Custom value from vertex primitive. When using extra
UVs, |
in vec4 CUSTOM1 |
Custom value from vertex primitive. When using extra
UVs, |
in vec4 CUSTOM2 |
Custom value from vertex primitive. When using extra
UVs, |
in vec4 CUSTOM3 |
Custom value from vertex primitive. |
out float Z_CLIP_SCALE |
If written to on any branch, scales the vertex towards
the camera to avoid clipping into things like walls.
Lighting and shadows will continue to work correctly
when this is written to, but screen-space effects like
SSAO and SSR may break with lower scales. Try to keep
this value as close to |
Note
MODELVIEW_MATRIX combines both the MODEL_MATRIX and VIEW_MATRIX and is better suited when floating point issues may arise. For example, if the object is very far away from the world origin, you may run into floating point issues when using the separated MODEL_MATRIX and VIEW_MATRIX.
Note
INV_VIEW_MATRIX is the matrix used for rendering the object in that pass, unlike MAIN_CAM_INV_VIEW_MATRIX, which is the matrix of the camera in the scene. In the shadow pass, INV_VIEW_MATRIX's view is based on the camera that is located at the position of the light.
Variables intégrées de fragment
L'utilisation par défaut d'une fonction de processeur de fragments dans Godot consiste à configurer les propriétés des matériaux de votre objet et à laisser le rendu intégré gérer l'ombrage final. Cependant, vous n'êtes pas obligé d'utiliser toutes ces propriétés, et si vous ne les écrivez pas, Godot optimisera les fonctionnalités correspondantes.
Intégré |
Description |
|---|---|
in vec2 VIEWPORT_SIZE |
Taille de la fenêtre d'affichage (en pixels). |
in vec4 FRAGCOORD |
Coordinate of pixel center in screen space. |
in bool FRONT_FACING |
|
in vec3 VIEW |
Normalized vector from fragment position to camera (in view space). This is the same for both perspective and orthogonal cameras. |
in vec2 UV |
UV that comes from the |
in vec2 UV2 |
UV2 that comes from the |
in vec4 COLOR |
COLOR that comes from the |
in vec2 POINT_COORD |
Point coordinate for drawing points with |
in mat4 MODEL_MATRIX |
Transformation de l'espace du modèle/local à l'espace global. |
in mat3 MODEL_NORMAL_MATRIX |
Model/local space to world space transform for normals. This is the same as |
in mat4 VIEW_MATRIX |
Transformation de l'espace global à l'espace de vue. |
in mat4 INV_VIEW_MATRIX |
Transformation de l'espace de vue à l'espace global. |
in mat4 PROJECTION_MATRIX |
Transformation de l'espace de vue à l'espace de découpage. |
in mat4 INV_PROJECTION_MATRIX |
Transformation de l'espace de découpage à l'espace de vue. |
in vec3 NODE_POSITION_WORLD |
Node position, in world space. |
in vec3 NODE_POSITION_VIEW |
Node position, in view space. |
in vec3 CAMERA_POSITION_WORLD |
Camera position, in world space. Represents the midpoint of the two eyes when in multiview/stereo rendering. |
in vec3 CAMERA_DIRECTION_WORLD |
Camera direction, in world space. |
in uint CAMERA_VISIBLE_LAYERS |
Cull layers of the camera rendering the current pass. |
in vec3 VERTEX |
Position of the fragment (pixel), in view space. It is the |
inout vec3 LIGHT_VERTEX |
A writable version of |
in int VIEW_INDEX |
The view that we are rendering. Used to distinguish between views in multiview/stereo rendering.
|
in int VIEW_MONO_LEFT |
Constant for Mono or left eye, always |
in int VIEW_RIGHT |
Constante pour l’œil droit, toujours |
in vec3 EYE_OFFSET |
Position offset for the eye being rendered, in view space. Only applicable for multiview rendering. |
sampler2D SCREEN_TEXTURE |
Removed in Godot 4. Use a |
in vec2 SCREEN_UV |
Screen UV coordinate for the current pixel. |
sampler2D DEPTH_TEXTURE |
Removed in Godot 4. Use a |
out float DEPTH |
Custom depth value (range |
inout vec3 NORMAL |
Normal that comes from the |
inout vec3 TANGENT |
Tangent that comes from the |
inout vec3 BINORMAL |
Binormal that comes from the |
out vec3 NORMAL_MAP |
Set normal here in tangent space if reading normal from a texture instead of |
out float NORMAL_MAP_DEPTH |
Depth from |
out vec3 BENT_NORMAL_MAP |
Set bent normal map here in tangent space to enable bent normals. This is used to improve specular occlusion, and requires a specially authored bent normal map. The blue channel is ignored, as it's reconstructed in the engine instead. This allows bent normal maps with RGTC compression to work. |
out vec3 ALBEDO |
Albedo (default white). Base color. |
out float ALPHA |
Alpha (range |
out float ALPHA_SCISSOR_THRESHOLD |
If written to on any branch, values below a certain amount of alpha are discarded. |
out float ALPHA_HASH_SCALE |
Alpha hash scale when using the alpha hash transparency mode. Defaults to |
out float ALPHA_ANTIALIASING_EDGE |
The threshold below which alpha to coverage antialiasing should be used. Defaults to |
out vec2 ALPHA_TEXTURE_COORDINATE |
The texture coordinate to use for alpha-to-coverge antialiasing. Requires the
|
out float PREMUL_ALPHA_FACTOR |
Premultiplied alpha factor. Only effective if |
out float METALLIC |
Metallic (range |
out float SPECULAR |
Specular (not physically accurate to change). Defaults to |
out float ROUGHNESS |
Roughness (range |
out float RIM |
Rim (range |
out float RIM_TINT |
Rim Tint, range from |
out float CLEARCOAT |
Small specular blob added on top of the existing one. If used, Godot calculates clearcoat. |
out float CLEARCOAT_GLOSS |
Gloss of clearcoat. If used, Godot calculates clearcoat. |
out float ANISOTROPY |
Pour déformer la touche spéculaire en fonction de l'espace de tangente. |
out vec2 ANISOTROPY_FLOW |
Direction de la distorsion, à utiliser avec les flowmaps. |
out float SSS_STRENGTH |
Strength of subsurface scattering. If used, subsurface scattering will be applied to the object. |
out vec4 SSS_TRANSMITTANCE_COLOR |
Color of subsurface scattering transmittance. If used, subsurface scattering transmittance will be applied to the object. |
out float SSS_TRANSMITTANCE_DEPTH |
Depth of subsurface scattering transmittance. Higher values allow the effect to reach deeper into the object. |
out float SSS_TRANSMITTANCE_BOOST |
Boosts the subsurface scattering transmittance if set above |
inout vec3 BACKLIGHT |
Color of backlighting (works like direct light, but it's received even if the normal is slightly facing away from the light). If used, backlighting will be applied to the object. Can be used as a cheaper approximation of subsurface scattering. |
out float AO |
Strength of ambient occlusion. For use with pre-baked AO. |
out float AO_LIGHT_AFFECT |
How much ambient occlusion affects direct light (range |
out vec3 EMISSION |
Emission color (can go over |
out vec4 FOG |
If written to on any branch, blends final pixel color with |
out vec4 RADIANCE |
If written to on any branch, blends environment map radiance with |
out vec4 IRRADIANCE |
If written to on any branch, blends environment map irradiance with |
Note
Les shaders passant par le pipeline transparent lorsque ALPHA est écrit peuvent présenter des problèmes de tri par transparence. Lisez la section tri par transparence dans la page des limitations de rendu 3D pour plus d'informations et moyens d'éviter les problèmes.
Variables intégrées de lumière
Writing light processor functions is completely optional. You can skip the light() function by using
the unshaded render mode. If no light function is written, Godot will use the material properties
written to in the fragment() function to calculate the lighting for you (subject to the render mode).
The light() function is called for every light in every pixel. It is called within a loop for each light type.
Below is an example of a custom light() function using a Lambertian lighting model:
void light() {
if (LIGHT_IS_AREA) {
// Area light GGX shading.
DIFFUSE_LIGHT += LIGHT_AREA_DIFFUSE_MULTIPLIER * ATTENUATION * LIGHT_COLOR;
SPECULAR_LIGHT += LIGHT_AREA_SPECULAR_MULTIPLIER * ATTENUATION * LIGHT_COLOR * SPECULAR_AMOUNT;
} else {
// Used for all other light types (directional, omni, spot).
DIFFUSE_LIGHT += clamp(dot(NORMAL, LIGHT), 0.0, 1.0) * ATTENUATION * LIGHT_COLOR / PI;
}
}
Si vous voulez que les lumières s'additionnent, ajoutez la contribution de la lumière à DIFFUSE_LIGHT en utilisant +=, plutôt que de l'écraser.
Avertissement
The light() function won't be run if the vertex_lighting render mode is enabled, or if
Rendering > Quality > Shading > Force Vertex Shading
is enabled in the Project Settings. (It's enabled by default on mobile platforms.)
Intégré |
Description |
|---|---|
in vec2 VIEWPORT_SIZE |
Taille de la fenêtre d'affichage (en pixels). |
in vec4 FRAGCOORD |
Coordinate of pixel center in screen space. |
in mat4 MODEL_MATRIX |
Transformation de l'espace du modèle/local à l'espace global. |
in mat4 INV_VIEW_MATRIX |
Transformation de l'espace de vue à l'espace global. |
in mat4 VIEW_MATRIX |
Transformation de l'espace global à l'espace de vue. |
in mat4 PROJECTION_MATRIX |
Transformation de l'espace de vue à l'espace de découpage. |
in mat4 INV_PROJECTION_MATRIX |
Transformation de l'espace de découpage à l'espace de vue. |
in vec3 NORMAL |
Vecteur Normal, dans l'espace de vue. |
in vec2 SCREEN_UV |
Screen UV coordinate for the current pixel. |
in vec2 UV |
UV that comes from the |
in vec2 UV2 |
UV2 that comes from the |
in vec3 VIEW |
Vecteur Vue, dans l'espace de vue. |
in vec3 LIGHT |
Light vector, in view space. |
in vec3 LIGHT_COLOR |
Light color multiplied by
light energy multiplied by
|
in float SPECULAR_AMOUNT |
For OmniLight3D and SpotLight3D,
|
in bool LIGHT_IS_DIRECTIONAL |
|
in float ATTENUATION |
Atténuation basée sur la distance ou l'ombre. |
in vec3 ALBEDO |
Albédo de base. |
in vec3 BACKLIGHT |
|
in float METALLIC |
Métallique. |
in float ROUGHNESS |
Rugosité. |
out vec3 DIFFUSE_LIGHT |
Résultat de la lumière diffuse. |
out vec3 SPECULAR_LIGHT |
Résultat de la lumière spéculaire. |
out float ALPHA |
Alpha (range |
Note
Les shaders passant par le pipeline transparent lorsque ALPHA est écrit peuvent présenter des problèmes de tri par transparence. Lisez la section tri par transparence dans la page des limitations de rendu 3D pour plus d'informations et moyens d'éviter les problèmes.
Transparent materials also cannot cast shadows or appear in
hint_screen_texture and hint_depth_texture uniforms. This in turn prevents those
materials from appearing in screen-space reflections or refraction.
SDFGI sharp reflections are not visible on transparent
materials (only rough reflections are visible on transparent materials).