Custom Shader


A plasma effect computed entirely on the GPU by a custom shader written in Haxe (src/shaders/Plasma.hx), with ceramic's cross-platform shader system: the same shader class is transpiled to the native shader language of each backend (GLSL, ShaderLab...). Its uniforms are updated from the scene at every frame.

// In src/shaders/Plasma.hx: the shader itself, in Haxe
class Plasma extends Shader<Plasma_Vert, Plasma_Frag> {}

class Plasma_Frag extends Frag {

     var mainTex:Sampler2D;
     var time:Float;       // Custom uniforms, set from Haxe
     var resolution:Vec2;

     var tcoord:Vec2;
     var color:Vec4;

    function main():Vec4 {
        // ... plasma math using sin(), vec3()... (see full source)
    }

}
// In the scene (plasmaShader is typed as shaders.Plasma):
override function preload() {
    assets.add(shaders.Plasma);
}

override function create() {
    plasmaShader = assets.shader(shaders.Plasma);

    var quad = new Quad();
    quad.size(width, height);
    quad.shader = plasmaShader;
    add(quad);
}

override function update(delta:Float) {
    elapsed += delta;
    // Feed our custom uniforms every frame, using the typed setters
    // generated from the shader's @param declarations
    plasmaShader.time(elapsed);
    plasmaShader.resolution(width, height);
}

Next example ➔ Dear ImGui