Shader
Represents a GPU shader program for custom rendering effects.
Shaders are programs that run on the GPU to transform vertices and calculate pixel colors. Ceramic uses shaders for all rendering, from basic sprite drawing to complex visual effects.
Key features:
- Support for vertex and fragment shaders
- Uniform variables for passing data to shaders
- Multiple texture slot support
- Custom vertex attributes
- Automatic shader compilation and linking
Common uses:
- Visual effects (blur, glow, distortion)
- Color manipulation (hue shift, contrast)
- Custom rendering techniques
- Post-processing filters
- Special material effects
Ceramic provides several built-in shaders:
- 'shader:textured' - Standard textured rendering (default)
- 'shader:pixelArt' - High-quality pixel art scaling
- Various effect shaders depending on plugins
// Load and apply a custom shader
var shader = assets.shader('myEffect').clone();
shader.setFloat('intensity', 0.5);
shader.setVec2('resolution', screen.width, screen.height);
myVisual.shader = shader;
// Create shader from source (if supported)
#if ceramic_shader_vert_frag
var customShader = Shader.fromSource(
vertexShaderCode,
fragmentShaderCode
);
#end
// Animate shader uniforms
app.onUpdate(this, delta -> {
shader.setFloat('time', Timer.now);
shader.setColor('tint', Color.fromHSB(
(Timer.now * 60) % 360, 1, 1
));
});
Static Members
Creates a shader from vertex and fragment shader source code.
The expected shading language depends on the backend:
- Clay/Web backends: GLSL ES
- Unity backend: Unity shader language
- Future backends may support different languages
This method is only available when the backend supports runtime shader compilation (ceramic_shader_vert_frag flag).
Name | Type | Description |
---|---|---|
vertSource |
String | Vertex shader source code |
fragSource |
String | Fragment shader source code |
Returns | Description |
---|---|
Shader | New shader instance, or null if compilation fails |
Instance Members
backendItem: backend.Shader
The backend-specific shader implementation. Used internally by the rendering system.
asset: ShaderAsset
The shader asset this shader was loaded from. Null if created programmatically.
attributes: ReadOnlyArray<ShaderAttribute>
All vertex attributes used by this shader. Includes standard attributes (position, texCoord, color) plus any custom attributes.
customAttributes: ReadOnlyArray<ShaderAttribute>
Custom vertex attributes beyond the standard ones. Used for passing additional per-vertex data to shaders.
customFloatAttributesSize: Int
Total size of custom float attributes in the vertex buffer. Calculated from customAttributes array.
destroy(): Void
clone(): Shader
Creates a copy of this shader with independent uniform values.
Useful when you need multiple instances of the same shader with different parameters.
Returns | Description |
---|---|
Shader | A new shader instance with the same program but separate uniforms |
Sets an integer uniform variable.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
value |
Int | The integer value to set |
Sets a float uniform variable.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
value |
Float | The float value to set |
Sets a color uniform variable (RGB with full alpha). The color is passed as vec4 with alpha = 1.0.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
color |
Color | The color value (alpha ignored) |
setAlphaColor(name: String, color: AlphaColor): Void
Sets a color uniform variable with alpha (RGBA). The color is passed as vec4 including alpha channel.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
color |
AlphaColor | The color value with alpha |
Sets a vec2 uniform variable.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
x |
Float | The x component |
y |
Float | The y component |
Sets a vec3 uniform variable.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
x |
Float | The x component |
y |
Float | The y component |
z |
Float | The z component |
Sets a vec4 uniform variable.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
x |
Float | The x component |
y |
Float | The y component |
z |
Float | The z component |
w |
Float | The w component |
Sets a float array uniform variable. Useful for passing multiple values or matrices.
Name | Type | Description |
---|---|---|
name |
String | The uniform variable name in the shader |
array |
Array<Float> | The array of float values |
Sets a texture uniform variable.
Textures are bound to numbered slots (0, 1, 2, etc.). Slot 0 is typically used for the main texture.
Name | Type | Description |
---|---|---|
name |
String | The uniform sampler2D variable name in the shader |
slot |
Int | The texture slot index (0-based) |
texture |
Texture | The texture to bind, or null to unbind |
Sets a mat4 uniform variable from a Transform. Converts the transform to a 4x4 matrix for the shader.
Name | Type | Description |
---|---|---|
name |
String | The uniform mat4 variable name in the shader |
transform |
Transform | The transform to convert to matrix |
new(backendItem: backend.Shader, ?customAttributes: ReadOnlyArray<ShaderAttribute>): Void
Creates a new shader instance.
Standard vertex attributes are automatically included:
- vertexPosition (vec3): Vertex position in model space
- vertexTCoord (vec2): Texture coordinates
- vertexColor (vec4): Vertex color with alpha
Name | Type | Default | Description |
---|---|---|---|
backendItem |
backend.Shader | Backend-specific shader implementation | |
customAttributes |
ReadOnlyArray<ShaderAttribute> | (optional) | Optional additional vertex attributes |
Private Members
toString(): String
Returns |
---|
String |
Metadata
Name | Parameters |
---|---|
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |