Shader

Include Cheatsheet

Share code between shaders with the

A shader file can pull in another shader file with #include "path". The directive is replaced by the contents of the referenced file before the shader is compiled, so shared helpers live in one place.

Godot's own #include only resolves res:// paths, so Vital expands it itself, relative to the resource folder.


Syntax

Place it on its own line, usually below shader_type and render_mode.

#include "path"
ArgumentTypeDescription
pathstringPath of the shader file, relative to the resource folder like the shader's own path

How It Works

TopicBehavior
Resolved bycore.shader.create, when the shader is loaded from a file
Replaced withThe text of the included file, in place of the directive line
Included onceA file is only included once per shader, so it can be included from several places without redefining its functions
NestingIncluded files can include other files, up to 8 levels deep
CommentsA line that starts with // is not a directive

Limitations

What is not allowed, and what happens when it is broken.

CaseBehavior
core.shader.create_from_rawIncludes are not expanded, there is no resource folder to read from
Missing fileError when the shader is created
Path without quotesError when the shader is created
Nesting deeper than 8 levelsError when the shader is created
shader_type, render_mode, fragment(), vertex() in an included fileNot allowed, they belong in the shader that includes it. Keep included files to functions, constants and uniforms

Examples

files/shaders/helper.gdshader
float sk_h21(vec2 p) {
    vec3 q = fract(vec3(p.xyx) * 0.1031);
    q += dot(q, q.yzx + 33.33);
    return fract((q.x + q.y) * q.z);
}
Share a helper between two shaders
local raindrops = core.shader.create("files/shaders/raindrops.gdshader", core.shader.shader_mode.SPATIAL)
local snowflakes = core.shader.create("files/shaders/snowflakes.gdshader", core.shader.shader_mode.SPATIAL)
files/shaders/raindrops.gdshader
shader_type spatial;

#include "files/shaders/helper.gdshader"

void fragment() {
    ALBEDO = vec3(sk_h21(UV * 64.0));
}

On this page