Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

Introduction to shaders

This page explains what shaders are and will give you an overview of how they work in Godot. For a detailed reference of the engine's shading language, see Shading language.

Shaders are a special kind of program that runs on Graphics Processing Units (GPUs). They were initially used to shade 3D scenes but can nowadays do much more. You can use them to control how the engine draws geometry and pixels on the screen, allowing you to achieve all sorts of effects.

Modern rendering engines like Godot draw everything with shaders: graphics cards can run thousands of instructions in parallel, leading to incredible rendering speed.

Because of their parallel nature, though, shaders don't process information the way a typical program does. Shader code runs on each vertex or pixel in isolation. You cannot store data between frames either. As a result, when working with shaders, you need to code and think differently from other programming languages.

Suppose you want to update all the pixels in a texture to a given color. In GDScript, your code would use for loops:

for x in range(width):
  for y in range(height):
    set_color(x, y, some_color)

Your code is already part of a loop in a shader, so the corresponding code would look like this.

void fragment() {
  COLOR = some_color;
}

Note

The graphics card calls the fragment() function once or more for each pixel it has to draw. More on that below.

Shaders in Godot

Godot provides a shading language based on the popular OpenGL Shading Language (GLSL) but simplified. The engine handles some of the lower-level initialization work for you, making it easier to write complex shaders.

In Godot, shaders are made up of main functions called "processor functions". Processor functions are the entry point for your shader into the program. There are seven different processor functions.

  1. The vertex() function runs over all the vertices in the mesh and sets their positions and some other per-vertex variables. Used in canvas_item shaders and spatial shaders.

  2. The fragment() function runs for every pixel covered by the mesh. It uses values output by the vertex() function, interpolated between the vertices. Used in canvas_item shaders and spatial shaders.

  3. The light() function runs for every pixel and for every light. It takes variables from the fragment() function and from its previous runs. Used in canvas_item shaders and spatial shaders.

  4. The start() function runs for every particle in a particle system once when the particle is first spawned. Used in particles shaders.

  5. The process() function runs for every particle in a particle system for each frame. Used in particles shaders.

  6. The sky() function runs for every pixel in the radiance cubemap when the radiance cubemap needs to be updated, and for every pixel on the current screen. Used in sky shaders.

  7. The fog() function runs for every froxel in the volumetric fog froxel buffer that intersects with the FogVolume. Used by fog shaders.

Warning

The light() function won't 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.

Note

Godot also exposes an API for users to write totally custom GLSL shaders. For more information see Using compute shaders.

Shader types

Instead of supplying a general-purpose configuration for all uses (2D, 3D, particles, sky, fog), you must specify the type of shader you're writing. Different types support different render modes, built-in variables, and processing functio