Godot shader language style guide

This style guide lists conventions to write elegant shaders. The goal is to encourage writing clean, readable code and promote consistency across projects, discussions, and tutorials. Hopefully, this will also support the development of auto-formatting tools.

Since the Godot shader language is close to C-style languages and GLSL, this guide is inspired by Godot’s own GLSL formatting. You can view an example of a GLSL file in Godot’s source code here.

Руководства по стилю не являются жестким сводом правил. Иногда у вас не будет возможности применить ниже приведенные рекомендации. Когда это случится, поступайте по своему усмотрению и попросите коллег-разработчиков поделиться своими соображениями.

В целом, поддержание согласованности кода в ваших проектах и в команде важнее, чем следование этому руководству.

Примечание

Godot’s built-in shader editor uses a lot of these conventions by default. Let it help you.

Here is a complete shader example based on these guidelines:

shader_type canvas_item;
// Screen-space shader to adjust a 2D scene's brightness, contrast
// and saturation. Taken from
// https://github.com/godotengine/godot-demo-projects/blob/master/2d/screen_space_shaders/shaders/BCS.shader

uniform float brightness = 0.8;
uniform float contrast = 1.5;
uniform float saturation = 1.8;

void fragment() {
    vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;

    c.rgb = mix(vec3(0.0), c.rgb, brightness);
    c.rgb = mix(vec3(0.5), c.rgb, contrast);
    c.rgb = mix(vec3(dot(vec3(1.0), c.rgb) * 0.33333), c.rgb, saturation);

    COLOR.rgb = c;
}

Formatting

Encoding and special characters

  • Use line feed (LF) characters to break lines, not CRLF or CR. (editor default)
  • Используйте символ перевода строки в конце каждого файла. (editor default)
  • Use UTF-8 encoding without a byte order mark. (editor default)
  • Use Tabs instead of spaces for indentation. (editor default)

Отступы

Each indent level should be one tab greater than the block containing it.

Хорошо:

void fragment() {
    COLOR = vec3(1.0, 1.0, 1.0);
}

Плохо:

void fragment() {
        COLOR = vec3(1.0, 1.0, 1.0);
}

Используйте 2 уровня отступа, чтобы отличать строки продолжения от обычных блоков кода.

Хорошо:

vec2 st = vec2(
        atan(NORMAL.x, NORMAL.z),
        acos(NORMAL.y));

Плохо:

vec2 st = vec2(
    atan(NORMAL.x, NORMAL.z),
    acos(NORMAL.y));

Разрывы строки и пустые строки

For a general indentation rule, follow the «1TBS Style» which recommends placing the brace associated with a control statement on the same line. Always use braces for statements, even if they only span one line. This makes them easier to refactor and avoids mistakes when adding more lines to an if statement or similar.

Хорошо:

void fragment() {
    if (true) {
        // ...
    }
}

Плохо:

void fragment()
{
    if (true)
        // ...
}

Пустые строки

Surround function definitions with one (and only one) blank line:

void do_something() {
    // ...
}

void fragment() {
    // ...
}

Use one (and only one) blank line inside functions to separate logical sections.

Line length

Придерживайтесь длине строк кода не более 100 символов.

If you can, try to keep lines under 80 characters. This helps to read the code on small displays and with two shaders opened side-by-side in an external text editor. For example, when looking at a differential revision.

One statement per line

Never combine multiple statements on a single line.

Хорошо:

void fragment() {
    ALBEDO = vec3(1.0);
    EMISSION = vec3(1.0);
}

Плохо:

void fragment() {
    ALBEDO = vec3(1.0); EMISSION = vec3(1.0);
}

Единственным исключением из этого правила является тернарный оператор:

void fragment() {
     bool should_be_white = true;
     ALBEDO = should_be_white ? vec3(1.0) : vec3(0.0);
 }

Comment spacing

Regular comments should start with a space, but not code that you comment out. This helps differentiate text comments from disabled code.

Хорошо:

// This is a comment.
//return;

Плохо:

//This is a comment.
// return;

Don’t use multiline comment syntax if your comment can fit on a single line:

/* This is another comment. */

Примечание

In the shader editor, to make the selected code a comment (or uncomment it), press Ctrl + K. This feature adds or removes // at the start of the selected lines.

Пробел

Always use one space around operators and after commas. Also, avoid extraneous spaces in function calls.

Хорошо:

COLOR.r = 5.0;
COLOR.r = COLOR.g + 0.1;
COLOR.b = some_function(1.0, 2.0);

Плохо:

COLOR.r=5.0;
COLOR.r = COLOR.g+0.1;
COLOR.b = some_function (1.0,2.0);

Не используйте пробелы для вертикального выравнивания выражений:

ALBEDO.r   = 1.0;
EMISSION.r = 1.0;

Floating-point numbers

Always specify at least one digit for both the integer and fractional part. This makes it easier to distinguish floating-point numbers from integers, as well as distinguishing numbers greater than 1 from those lower than 1.

Хорошо:

void fragment() {
    ALBEDO.rgb = vec3(5.0, 0.1, 0.2);
}

Плохо:

void fragment() {
    ALBEDO.rgb = vec3(5., .1, .2);
}

Accessing vector members

Use r, g, b, and a when accessing a vector’s members if it contains a color. If the vector contains anything else than a color, use x, y, z, and w. This allows those reading your code to better understand what the underlying data represents.

Хорошо:

COLOR.rgb = vec3(5.0, 0.1, 0.2);

Плохо:

COLOR.xyz = vec3(5.0, 0.1, 0.2);

Naming conventions

These naming conventions follow the Godot Engine style. Breaking these will make your code clash with the built-in naming conventions, leading to inconsistent code.

Functions and variables

Use snake_case to name functions and variables:

void some_function() {
     float some_variable = 0.5;
}

Константы

Write constants with CONSTANT_CASE, that is to say in all caps with an underscore (_) to separate words:

const float GOLDEN_RATIO = 1.618;

Code order

We suggest to organize shader code this way:

01. shader type declaration
02. render mode declaration
03. // docstring

04. uniforms
05. constants
06. varyings

07. other functions
08. vertex() function
09. fragment() function
10. light() function

Мы оптимизировали порядок, чтобы упростить чтение кода сверху вниз и помочь разработчикам, которые читают этот код впервые, понять, как он работает и избежать ошибок, связанных с порядком объявления переменных.

This code order follows two rules of thumb:

  1. Metadata and properties first, followed by methods.
  2. «Public» comes before «private». In a shader language’s context, «public» refers to what’s easily adjustable by the user (uniforms).

Local variables

Declare local variables as close as possible to their first use. This makes it easier to follow the code, without having to scroll too much to find where the variable was declared.