I remember the times i started to develop games in high school, (back in around 2017) shaders were not popular like they are now.
After i started to spend time in game development i needed to learn about shaders. I was asking the same question "What are the shaders?" as can be guessed easily, newbies are asking the same question, and trying to find answers.
What is a Shader in Godot Engine?
In this article we will be mostly looking in Godot Engine's perspective. But most of the information is applicable to other engines and libraries as well. The most important difference is Godot has it's own shading language, you need to learn coding in other languages for different engines / libraries.
Shaders are programs like a GDScript program. There are several differences between a regular C program and a shader.
Shaders Run on GPU
Shaders run on GPU instead of CPU. Due to GPU's nature they can process large amounts of programs in parallel. Modern graphic cards can run thousands of instructions in parallel which is leading to an incredible rendering speed. For instance RTX 2060 has 1920 CUDA cores instead of only 8-16 cores in modern CPU's.
They Run in Isolation
A shader program runs for each vertex / pixel in parallel. Since they are running in parallel you can not store data between frames. On each frame they re-initiate "threads". Therefore we can call them stateless, furthermore you need to think can code in a different way.
Looping is not Necessary
If you were coding in GDScript or any other language, you need to loop over all pixels to manipulate their position, color and lighting. Since shaders run separately for each pixel, looping is not required to manipulate all pixels.
Shaders in Godot Engine
Learning GLSL (OpenGL Shader Language) is hard and requires a lot of effort to develop a good, performant shader. Godot Engine has done a favor to us. Godot Engine has it's own shading language which handles low level stuff and makes easier to code complex shaders.
Shader Functions in Godot Engine
Godot Engine Shading Language has three main functions:
- vertex(): vertex() function runs over each vertex and sets their positions.
- fragment(): does the coloring mostly.
- light(): controls the lighting of a pixel.
Shader Types in Godot Engine
There are five types of shaders:
- spatial: For 3D rendering.
- canvas_item: For 2D rendering.
- particle: For particles.
- sky: to render skies. (New in Godot 4.0!)
- fog: to render FogVolumes. (New in Godot 4.0!)
In Godot, you can control the shader type with shader_type instruction like:
shader_type spatial;
This instruction must be used as the first line of a shader.
If you are using spatial or canvas_item shader type you can use:
- vertex()
- fragment()
- light()
If you are using particle shader type you can only use vertex(), start() and process() function.
Only sky() function can be used in sky shader type and only fog() can be used in fog shader type.
About Shader Series
Personally, i spend a lot of time to understand shaders and i enjoy text tutorials instead of YouTube videos. I want to help people like me and i want to create reference for myself. Also content for new shader types (fog and sky) is not released, i want to release content for them.

Comments
Post a Comment