How shader chunk imports work
mainThe plugin allows you to modularize shaders using a custom #include keyword. When you use #include path/to/file;, the plugin resolves the file, injects its content into the main shader, and handles dependencies recursively.
Key behaviors:
- Automatic Extensions: If you omit the extension in the
#includestatement (e.g.,#include utils/chunk1), the plugin uses thedefaultExtensionoption (defaulting toglsl) to find the file. - Deduplication: If
removeDuplicatedImportsis set totrue, the plugin will automatically remove chunks that have already been included to prevent errors. - Three.js Compatibility: If using
three.jsr0.99+, standard Three.js includes like#include <common>are ignored by this plugin as they are handled by the library itself.
// main.frag
#include chunk0.frag;
void main (void) {
// ...
}
// chunk0.frag
#include utils/chunk1; // Automatically resolves to utils/chunk1.glsl
vec4 chunkFn () {
return vec4(chunkRGB(), 1.0);
}