🇬🇧 English
🇬🇧 English
Appearance
🇬🇧 English
🇬🇧 English
Appearance
This page is written for:
1.21
This page is written for:
1.21
We already briefly touched on rendering things to the hud in the Basic Rendering Concepts page and Using The Drawing Context, so on this page we'll stick to the HudRenderCallback
event and the deltaTick
parameter.
The HudRenderCallback
event - provided by Fabric API - is called every frame, and is used to render things to the HUD.
To register to this event, you can simply call HudRenderCallback.EVENT.register
and pass in a lambda that takes a DrawContext
and a float
(deltaTick) as parameters.
The draw context can be used to access the various rendering utilities provided by the game, and access the raw matrix stack.
You should check out the Draw Context page to learn more about the draw context.
The deltaTick
refers to the time since the last frame, in seconds. This can be used to make animations and other time-based effects.
For example, let's say you want to lerp a color over time. You can use the deltaTickManager
to get the deltaTick, and store it over time to lerp the color:
HudRenderCallback.EVENT.register((context, tickDeltaManager) -> {
int color = 0xFFFF0000; // Red
int targetColor = 0xFF00FF00; // Green
// Total tick delta is stored in a field, so we can use it later.
totalTickDelta += tickDeltaManager.getTickDelta(true);
// "lerp" simply means "linear interpolation", which is a fancy way of saying "blend".
float lerpedAmount = MathHelper.abs(MathHelper.sin(totalTickDelta / 50F));
int lerpedColor = ColorHelper.Argb.lerp(lerpedAmount, color, targetColor);
// Draw a square with the lerped color.
// x1, x2, y1, y2, z, color
context.fill(0, 0, 100, 100, 0, lerpedColor);
});