Abbiamo già parlato brevemente di come renderizzare cose sulla Hud nelle pagine Concetti di Rendering di Base e Usare il Drawing Context, per cui in questa pagina ci concentreremo sull'evento HudRenderCallback e sul parametro deltaTick.
HudRenderCallback
L'evento HudRenderCallback - fornito dall'API di Fabric - viene chiamato ogni frame, e viene usato per renderizzare cose sul HUD.
Per registrarsi a questo evento, puoi semplicemente chiamare HudRenderCallback.EVENT.register e passare una lambda che prende come parametri un DrawContext e un float (deltaTick).
Il contesto di disegno può essere usato per accedere a varie utilità di rendering fornite dal gioco, e per accedere allo stack di matrici puro.
Dovresti dare un'occhiata alla pagina Usare il Contesto di Disegno per saperne di più riguardo al contesto di disegno.
DeltaTick
Il parametro deltaTick è il tempo trascorso dall'ultimo frame, in secondi. Questo può essere usato per fare animazioni e altri effetti basati sul tempo.
Esempio: Interpolare un Colore nel Tempo
Immagina di voler interpolare linearmente un colore nel tempo. Puoi usare il parametro deltaTick per farlo.
java
public class HudRenderingEntrypoint implements ClientModInitializer {
@Override
public void onInitializeClient() {
// Attach our rendering code to before the chat hud layer. Our layer will render right before the chat. The API will take care of z spacing.
HudElementRegistry.attachElementBefore(VanillaHudElements.CHAT, ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "before_chat"), HudRenderingEntrypoint::render);
}
private static void render(GuiGraphics context, DeltaTracker tickCounter) {
int color = 0xFFFF0000; // Red
int targetColor = 0xFF00FF00; // Green
// You can use the Util.getMeasuringTimeMs() function to get the current time in milliseconds.
// Divide by 1000 to get seconds.
double currentTime = Util.getMillis() / 1000.0;
// "lerp" simply means "linear interpolation", which is a fancy way of saying "blend".
float lerpedAmount = Mth.abs(Mth.sin((float) currentTime));
int lerpedColor = ARGB.lerp(lerpedAmount, color, targetColor);
// Draw a square with the lerped color.
// x1, x2, y1, y2, color
context.fill(0, 0, 10, 10, lerpedColor);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


