Skip to content

Shaders

Homak edited this page Nov 25, 2026 · 10 revisions

What are shaders?

Shaders are programs that run on the GPU, therefore being super fast for rendering and math as a whole. In OpenGL, shaders are written in a different language called GLSL, it's really similar to java but we suggest you learn it before proceeding here.

Defining a Shader

To register your shader, use org.carpentry.citrine.api.client.shader.CitrineShaderRegistry

public static final CitrineShaderHandle MAGIC_SHADER = CitrineShaderRegistry.register(MainModClass.id("magic"));
// or use register(Identifier id, VertexFormat format)
public static final CitrineShaderHandle MAGIC_SHADER = CitrineShaderRegistry.register(MainModClass.id("magic"), VertexFormats.POSITION_COLOR_TEXTURE);

Now create the shader files assets/modid/shaders/core/magic.json

{
  "vertex": "citrine:plane", // this almost always remains unchanged
  "fragment": "citrine:magic",
  "attributes": ["Position", "Color", "UV0"], // matches the VertexFormat
  "samplers": [],
  "uniforms": [ // default uniforms provided by the library
    { "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1] },
    { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1] },
    { "name": "Size", "type": "float", "count": 2, "values": [1.0, 1.0] },
    { "name": "Time", "type": "float", "count": 1, "values": [1.0] }
  ]
}

assets/modid/shaders/core/magic.fsh

#version 150

uniform vec2 Size;
uniform float Time;

in vec4 vertexColor;
out vec4 fragColor;

void main() {
    vec2 uv = gl_FragCoord.xy / Size;
    fragColor = vec4(uv.x, uv.y, sin(Time / 5) / 2 + 0.5, 1);
}

Uniforms

Uniforms are a way to transfer data from the CPU to the GPU. Citrine already supplies a handful of them, but you can add your own if needed:

new CitrineShaderQuad(Supplier<ShaderProgram> program, List<CitrineShaderUniform> uniformsToAdd)
//or CitrineShaderQuad.uniforms(Consumer<ShaderProgram> uniforms)

Don't forget to add them to your shader .json

{
  "vertex": "citrine:plane", // this almost always remains unchanged
  "fragment": "citrine:magic",
  "attributes": ["Position", "Color", "UV0"], // matches the VertexFormat
  "samplers": [],
  "uniforms": [ // default uniforms provided by the library
    { "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1] },
    { "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1] },
    { "name": "Size", "type": "float", "count": 2, "values": [1.0, 1.0] },
    { "name": "Time", "type": "float", "count": 1, "values": [1.0] },
    { "name": "MagicVector", "type": "float", "count": 2, "values": [1.0, 1.0] }
  ]
}

Rendering the Shader effect

To actually render the shader, use org.carpentry.citrine.api.client.shader.CitrineShaderQuad

org.carpentry.citrine.api.client.shader.CitrineShaderQuad shader = new CitrineShaderQuad(CitrineShaders.MAGIC_SHADER);
        HudRenderCallback.EVENT.register((drawContext, tickDelta) -> {
            shader.draw(drawContext, drawContext.getScaledWindowWidth() / 2f - 100, drawContext.getScaledWindowHeight() / 2f - 100, 200, 200);
            // can accept a Matrix4f instead of DrawContext
            //shader.draw(drawContext.getMatrices().peek().getPositionMatrix(), drawContext.getScaledWindowWidth() / 2f, drawContext.getScaledWindowHeight() / 2f, 100, 100);
        });

This renders a colored square at the center of the screen 2026-11-23_10 55 52

Clone this wiki locally