-
Notifications
You must be signed in to change notification settings - Fork 0
Shaders
Shaders in Heirloom are written in GLSL. They are almost 100% vanilla GLSL but have been extended to support an #include
directive. A standard include is expected for shaders to work correctly. They are standard/standard.vert
or standard/standard.frag
. This provides the correct input/output, uniforms and structures of each shader stage as well as the standard shader library.
This guide assumes you have some existing GLSL knowledge.
The default shaders used in Heirloom are the following:
default.vert
#include "standard/standard.vert"
vec2 vertexProgram(vec2 position)
{
// simple pass-through
return position;
}
default.frag
#include "standard/standard.frag"
vec4 fragmentProgram(vec4 color)
{
// simple pass-through
return color;
}
The following is a simple "invert color" shader.
#include "standard/standard.frag"
vec4 fragmentProgram(vec4 color)
{
// Invert RGB values
return vec4(1.0 - color.rgb, color.a);
}
Another simple example, a distortion shader. This time uses an image and a time uniform.
#include "standard/standard.frag"
uniform sampler2D uNoiseImage;
uniform vec4 uNoiseImage_UVRect;
uniform float uTime;
vec4 fragmentProgram(vec4 color)
{
vec2 offset = atlas(uNoiseImage, uNoiseImage_UVRect, vec2(uTime / 5.0, uTime / 2.0) + frag.uv).xy * 2.0 - 1.0;
return atlas(uMainImage, uMainImage_UVRect, frag.uv + offset * 0.05);
}
Note: In the example above, recognize the uniform uNoiseImage_UVRect
. The _UVRect
is a necessary part of sampling images in Heirloom as images are automatically packed into a texture atlas. The _UVRect
uniforms are automatically populated when the sampler uniform is set in the application.
distort.SetUniform("uNoiseImage", myNoiseImage);
// uNoiseImage_UVRect was automatically set above
Samples the texture img
with atlas encoding rectangle rect
.
Computes the luminance (grayscale) value of the input color.
Not all GLSL uniform types are backed by a type within Heirloom. The following describes the types that can be used to update a uniform variable.
Note: Uniform blocks are currently not supported or exposed by any API in Heirloom. Future versions may expose this functionality.
- Float types
- A
float
can be set withfloat
- A
vec2
can be set withVector
,float[2]
- A
vec3
can be set withfloat[3]
- A
vec4
can be set withRectangle
,Color
orfloat[4]
- A
- Integer types
- A
int
can be set withint
- A
ivec2
can be set withIntVector
orint[2]
- A
ivec3
can be set withint[3]
- A
ivec4
can be set withIntRectangle
,ColorBytes
orint[4]
- A
- Unsigned Integer types
- A
uint
can be set withuint
- A
uvec2
can be set withuint[2]
- A
uvec3
can be set withuint[3]
- A
uvec4
can be set withuint[4]
- A
- Boolean types
- A
bool
can be set withbool
orint
- A
bvec2
can be set withbool[2]
- A
bvec3
can be set withbool[3]
- A
bvec4
can be set withbool[4]
- A
- Matrix
- A
mat2x3
can be set withMatrix
- All other matrix types can be set with
float[]
- A
- Sampler types
- A
sampler2D
can be set withImage
orSurface
- All other sampler types have no representation
- A
Arrays of the above GLSL type are just the one-dimensional array form of the above. If you have a uniform of vec4[8]
for a list of 8 colors, you can provide Color[8]
or float[32]
.
Copyright © 2020 Chris Chamberlain
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
-
The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
-
Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
-
This notice may not be removed or altered from any source distribution.