Skip to content

Spatial shaders

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.

Render modes

Render modeDescription
blend_mixMix blend mode (alpha is transparency), default.
blend_addAdditive blend mode.
blend_subSubtractive blend mode.
blend_mulMultiplicative blend mode.
depth_draw_opaqueOnly draw depth for opaque geometry (not transparent).
depth_draw_alwaysAlways draw depth (opaque and transparent).
depth_draw_neverNever draw depth.
depth_prepass_alphaDo opaque depth pre-pass for transparent geometry.
depth_test_disabledDisable depth testing.
sss_mode_skinSubsurface Scattering mode for skin.
cull_backCull back-faces (default).
cull_frontCull front-faces.
cull_disabledCulling disabled (double sided).
unshadedResult is just albedo. No lighting/shading happens in material.
wireframeGeometry draws using lines.
diffuse_burleyBurley (Disney PBS) for diffuse (default).
diffuse_lambertLambert shading for diffuse.
diffuse_lambert_wrapLambert wrapping (roughness dependent) for diffuse.
diffuse_toonToon shading for diffuse.
specular_schlick_ggxSchlick-GGX for specular (default).
specular_toonToon for specular.
specular_disabledDisable specular.
skip_vertex_transformVERTEX/NORMAL/etc. need to be transformed manually in vertex function.
world_vertex_coordsVERTEX/NORMAL/etc. are modified in world coordinates instead of local.
ensure_correct_normalsUse when non-uniform scale is applied to mesh.
shadows_disabledDisable computing shadows in shader.
ambient_light_disabledDisable contribution from ambient light and radiance map.
shadow_to_opacityLighting modifies the alpha so shadowed areas are opaque and non-shadowed areas are transparent. Useful for overlaying shadows onto a camera feed in AR.
vertex_lightingUse vertex-based lighting.
particle_trailsEnables the trails when used on particles geometry.
alpha_to_coverageAlpha antialiasing mode, see here for more.
alpha_to_coverage_and_oneAlpha antialiasing mode, see here for more.
fog_disabledDisable receiving depth-based or volumetric fog. Useful for blend_add materials like particles.

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.

Built-inDescription
in float TIMEGlobal time, in seconds.
in float PIA PI constant (3.141592). A ration of circle's circumference to its diameter and amount of radians in half turn.
in float TAUA TAU constant (6.283185). An equivalent of PI * 2 and amount of radians in full turn.
in float EAn E constant (2.718281). Euler's number and a base of the natural logarithm.

Vertex built-ins

Vertex data (VERTEX, NORMAL, TANGENT, BITANGENT) are presented in local model space. If not written to, these values will not be modified and be passed through as they came.

They can optionally be presented in world space by using the world_vertex_coords render mode.

Users can disable the built-in modelview transform (projection will still happen later) and do it manually with the following code:

glsl
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);
}

Other built-ins, such as UV, UV2 and COLOR, are also passed through to the fragment function if not modified.

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: Rotation angle in radians.

  • y: Phase during lifetime (0 to 1).

  • z: Animation frame.

This allows you to easily adjust the shader to a particle system using default particles material. When writing a custom particle shader, this value can be used as desired.

Built-inDescription
in vec2 VIEWPORT_SIZESize of viewport (in pixels).
in mat4 VIEW_MATRIXWorld space to view space transform.
in mat4 INV_VIEW_MATRIXView space to world space transform.
in mat4 MAIN_CAM_INV_VIEW_MATRIXView space to world space transform of camera used to draw the current viewport.
in mat4 INV_PROJECTION_MATRIXClip space to view space transform.
in vec3 NODE_POSITION_WORLDNode world space position.
in vec3 NODE_POSITION_VIEWNode view space position.
in vec3 CAMERA_POSITION_WORLDCamera world space position.
in vec3 CAMERA_DIRECTION_WORLDCamera world space direction.
in uint CAMERA_VISIBLE_LAYERSCull layers of the camera rendering the current pass.
in bool OUTPUT_IS_SRGBtrue when output is in sRGB color space (this is true in the Compatibility renderer, false in Forward+ and Forward Mobile).
in int INSTANCE_IDInstance ID for instancing.
in vec4 INSTANCE_CUSTOMInstance custom data (for particles, mostly).
in int VIEW_INDEXThe view that we are rendering. VIEW_MONO_LEFT (0) for Mono (not multiview) or left eye, VIEW_RIGHT (1) for right eye.
in int VIEW_MONO_LEFTConstant for Mono or left eye, always 0.
in int VIEW_RIGHTConstant for right eye, always 1.
in vec3 EYE_OFFSETPosition offset for the eye being rendered. Only applicable for multiview rendering.
inout vec3 VERTEXVertex in local coordinates.
in int VERTEX_IDThe index of the current vertex in the vertex buffer.
inout vec3 NORMALNormal in local coordinates.
inout vec3 TANGENTTangent in local coordinates.
inout vec3 BINORMALBinormal in local coordinates.
out vec4 POSITIONIf written to, overrides final vertex position.
inout vec2 UVUV main channel.
inout vec2 UV2UV secondary channel.
inout vec4 COLORColor from vertices.
out float ROUGHNESSRoughness for vertex lighting.
inout float POINT_SIZEPoint size for point rendering.
inout mat4 MODELVIEW_MATRIXModel space to view space transform (use if possible).
inout mat3 MODELVIEW_NORMAL_MATRIX
in mat4 MODEL_MATRIXModel space to world space transform.
in mat3 MODEL_NORMAL_MATRIX
inout mat4 PROJECTION_MATRIXView space to clip space transform.
in uvec4 BONE_INDICES
in vec4 BONE_WEIGHTS
in vec4 CUSTOM0
in vec4 CUSTOM1
in vec4 CUSTOM2
in vec4 CUSTOM3

INFO

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.

INFO

INV_VIEW_MATRIX is the matrix used for rendering the object in that pass, not like 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.

Fragment built-ins

The default use of a Godot fragment processor function is to set up the material properties of your object and to let the built-in renderer handle the final shading. However, you are not required to use all these properties, and if you don't write to them, Godot will optimize away the corresponding functionality.

Built-inDescription
in vec2 VIEWPORT_SIZESize of viewport (in pixels).
in vec4 FRAGCOORDCoordinate 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 bool FRONT_FACINGtrue if current face is front facing.
in vec3 VIEWNormalized vector from fragment position to camera (in view space). This is the same for both perspective and orthogonal cameras.
in vec2 UVUV that comes from vertex function.
in vec2 UV2UV2 that comes from vertex function.
in vec4 COLORCOLOR that comes from vertex function.
in vec2 POINT_COORDPoint Coordinate for drawing points with POINT_SIZE.
in bool OUTPUT_IS_SRGBtrue when output is in sRGB color space (this is true in the Compatibility renderer, false in Forward+ and Forward Mobile).
in mat4 MODEL_MATRIXModel space to world space transform.
in mat3 MODEL_NORMAL_MATRIX
in mat4 VIEW_MATRIXWorld space to view space transform.
in mat4 INV_VIEW_MATRIXView space to world space transform.
in mat4 PROJECTION_MATRIXView space to clip space transform.
in mat4 INV_PROJECTION_MATRIXClip space to view space transform.
in vec3 NODE_POSITION_WORLDNode position, in world space.
in vec3 NODE_POSITION_VIEWNode position, in view space.
in vec3 CAMERA_POSITION_WORLDCamera position, in world space.
in vec3 CAMERA_DIRECTION_WORLDCamera direction, in world space.
in uint CAMERA_VISIBLE_LAYERSCull layers of the camera rendering the current pass.
in vec3 VERTEXVertex that comes from vertex function (default, in view space).
inout vec3 LIGHT_VERTEXA writable version of VERTEX that can be used to alter light and shadows. Writing to this will not change the position of the fragment.
in int VIEW_INDEXThe view that we are rendering. VIEW_MONO_LEFT (0) for Mono (not multiview) or left eye, VIEW_RIGHT (1) for right eye.
in int VIEW_MONO_LEFTConstant for Mono or left eye, always 0.
in int VIEW_RIGHTConstant for right eye, always 1.
in vec3 EYE_OFFSETPosition offset for the eye being rendered. Only applicable for multiview rendering.
sampler2D SCREEN_TEXTURERemoved in Godot 4. Use a sampler2D with hint_screen_texture instead.
in vec2 SCREEN_UVScreen UV coordinate for current pixel.
sampler2D DEPTH_TEXTURERemoved in Godot 4. Use a sampler2D with hint_depth_texture instead.
out float DEPTHCustom depth value (0..1). If DEPTH is being written to in any shader branch, then you are responsible for setting the DEPTH for all other branches. Otherwise, the graphics API will leave them uninitialized.
inout vec3 NORMALNormal that comes from vertex function (default, in view space).
inout vec3 TANGENTTangent that comes from vertex function.
inout vec3 BINORMALBinormal that comes from vertex function.
out vec3 NORMAL_MAPSet normal here if reading normal from a texture instead of NORMAL.
out float NORMAL_MAP_DEPTHDepth from variable above. Defaults to 1.0.
out vec3 ALBEDOAlbedo (default white).
out float ALPHAAlpha (0..1); if read from or written to, the material will go to the transparent pipeline.
out float ALPHA_SCISSOR_THRESHOLDIf written to, values below a certain amount of alpha are discarded.
out float ALPHA_HASH_SCALE
out float ALPHA_ANTIALIASING_EDGE
out vec2 ALPHA_TEXTURE_COORDINATE
out float METALLICMetallic (0..1).
out float SPECULARSpecular. Defaults to 0.5, best not to modify unless you want to change IOR.
out float ROUGHNESSRoughness (0..1).
out float RIMRim (0..1). If used, Godot calculates rim lighting.
out float RIM_TINTRim Tint, goes from 0 (white) to 1 (albedo). If used, Godot calculates rim lighting.
out float CLEARCOATSmall added specular blob. If used, Godot calculates Clearcoat.
out float CLEARCOAT_GLOSSGloss of Clearcoat. If used, Godot calculates Clearcoat.
out float ANISOTROPYFor distorting the specular blob according to tangent space.
out vec2 ANISOTROPY_FLOWDistortion direction, use with flowmaps.
out float SSS_STRENGTHStrength of Subsurface Scattering. If used, Subsurface Scattering will be applied to object.
out vec4 SSS_TRANSMITTANCE_COLOR
out float SSS_TRANSMITTANCE_DEPTH
out float SSS_TRANSMITTANCE_BOOST
inout vec3 BACKLIGHT
out float AOStrength of Ambient Occlusion. For use with pre-baked AO.
out float AO_LIGHT_AFFECTHow much AO affects lights (0..1; default 0).
out vec3 EMISSIONEmission color (can go over 1,1,1 for HDR).
out vec4 FOGIf written to, blends final pixel color with FOG.rgb based on FOG.a.
out vec4 RADIANCEIf written to, blends environment map radiance with RADIANCE.rgb based on RADIANCE.a.
out vec4 IRRADIANCEIf written to, blends environment map IRRADIANCE with IRRADIANCE.rgb based on IRRADIANCE.a.

INFO

Shaders going through the transparent pipeline when ALPHA is written to may exhibit transparency sorting issues. Read the Transparency sorting for more information and ways to avoid issues.

Light built-ins

Writing light processor functions is completely optional. You can skip the light function by setting render_mode to unshaded. 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:

glsl
void light() {
    DIFFUSE_LIGHT += clamp(dot(NORMAL, LIGHT), 0.0, 1.0) * ATTENUATION * LIGHT_COLOR;
}

If you want the lights to add together, add the light contribution to DIFFUSE_LIGHT using +=, rather than overwriting it.

WARNING

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.)

Built-inDescription
in vec2 VIEWPORT_SIZESize of viewport (in pixels).
in vec4 FRAGCOORDCoordinate 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 mat4 MODEL_MATRIXModel space to world space transform.
in mat4 INV_VIEW_MATRIXView space to world space transform.
in mat4 VIEW_MATRIXWorld space to view space transform.
in mat4 PROJECTION_MATRIXView space to clip space transform.
in mat4 INV_PROJECTION_MATRIXClip space to view space transform.
in vec3 NORMALNormal vector, in view space.
in vec2 UVUV that comes from vertex function.
in vec2 UV2UV2 that comes from vertex function.
in vec3 VIEWView vector, in view space.
in vec3 LIGHTLight Vector, in view space.
in vec3 LIGHT_COLORColor of light multiplied by energy * PI. The PI multiplication is present because physically-based lighting models include a division by PI.
in float SPECULAR_AMOUNT2.0 * light_specular property for OmniLight3D and SpotLight3D. 1.0 for DirectionalLight3D.
in bool LIGHT_IS_DIRECTIONALtrue if this pass is a DirectionalLight3D.
in float ATTENUATIONAttenuation based on distance or shadow.
in vec3 ALBEDOBase albedo.
in vec3 BACKLIGHT
in float METALLICMetallic.
in float ROUGHNESSRoughness.
in bool OUTPUT_IS_SRGBtrue when output is in sRGB color space (this is true in the Compatibility renderer, false in Forward+ and Forward Mobile).
out vec3 DIFFUSE_LIGHTDiffuse light result.
out vec3 SPECULAR_LIGHTSpecular light result.
out float ALPHAAlpha (0..1); if written to, the material will go to the transparent pipeline.

INFO

Shaders going through the transparent pipeline when ALPHA is written to may exhibit transparency sorting issues. Read the Transparency sorting for more information and ways to avoid issues.

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. Signed distance field global illumination (SDFGI) sharp reflections are not visible on transparent materials (only rough reflections are visible on transparent materials).