GLSL Shader Loader
This is a static shader source bundler for WebGL program, provide a possibility for management shader source by creating separate files.
glsl-shader-loader for Webpack, supports for import GLSL functions from file and generates a shader string for WebGL program.
GLSL Shader Loader provids many features as following.
- Allow import
.glsl
source file as a Javacript string for WebGL program - Support
import
statement in.glsl
file that can extract GLSL functions from other files (includes nested reference) - Remove invalid import if the function will not be called
- Repeated functions are imported only once
- Syntax tree analysis and error detection
Install
npm install --save-dev glsl-shader-loader
Configuration
In your webpack configuration:
module: rules: test: /\.$/ use: loader: 'glsl-shader-loader' options: {}
Usage
You can import GLSL functions with #pragma loader:
statements in .glsl
file
-
Import specified function by name
#pragma loader: import {nameA, nameB} from './file.glsl';
-
Import the only function in file by a new name
#pragma loader: import rename from './file.glsl';
-
NOTE:
- Only if there is a single function in
.glsl
file will you be able to rename it - If the imported function is not called, the function source will not insert in shader source
- In case two functions have the same name, only import once
- Imported function will replace the position of import statement in order
- Only if there is a single function in
Options
Name | Type | Default | Description |
---|---|---|---|
root | {String} | undefined | Specify the root path of source |
root
configuration:
loader: 'glsl-shader-loader' options: root: '/src/shaders'
Use /
redirect to the specified directory.
e.g.
#pragma loader: import {light} from '/lights.glsl';
will searchlights.glsl
under the pathprojectRoot/src/shaders/
Example
A directory structured like this:
.
├─ app.js
├─ fragmentShaderSource.glsl
└─ /collections/
├─ light.glsl
└─ random.glsl
Setting up shaders in app.js you might write code like this:
gl...
In shader code fragmentShaderSource.glsl, import randomDirection
and spotLight
from file:
precision mediump float; varying vec4 v_color;varying vec3 v_normal; ... void
light.glsl
vec3 vec3
random.glsl
vec3
import fragmentShaderSource from './fragmentShaderSource.glsl'
Will return this JavaScript string:
precision mediump float; varying vec4 v_color;varying vec3 v_normal; vec3 vec3 ... void