🇨🇳 中文 (Chinese - China)
🇨🇳 中文 (Chinese - China)
外观
🇨🇳 中文 (Chinese - China)
🇨🇳 中文 (Chinese - China)
外观
本页面基于这个版本编写:
1.20.4
本页面基于这个版本编写:
1.20.4
在 基本渲染概念 页面和 使用绘制上下文 中,我们已经简要介绍了如何将内容渲染到Hud,因此在本页中,我们将重点介绍 HudRenderCallback
事件和 deltaTick
参数。
由 Fabric API 提供的 HudRenderCallback
事件每帧都会被调用,用于向 HUD 渲染内容。
要注册此事件,只需调用 HudRenderCallback.EVENT.register
并传入一个以 DrawContext
和一个 float
(deltaTick) 为参数的 lambda 表达式即可。
绘制上下文可用于访问游戏提供的各种渲染工具,并访问原始矩阵堆栈。
您应该查看 使用绘制上下文 页面,了解有关绘制上下文的更多信息。
deltaTick
参数从上一帧到现在的时间间隔,以秒为单位。 这可以用来制作动画和其他基于时间的效果。
示例:让一个颜色随时间变化 假设你想让一个颜色随时间变化。 你可以使用 deltaTick
参数来完成它。
public class HudRenderingEntrypoint implements ClientModInitializer {
private static final Identifier EXAMPLE_LAYER = Identifier.of(FabricDocsReference.MOD_ID, "hud-example-layer");
@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 and automatically add 200 after every layer.
HudLayerRegistrationCallback.EVENT.register(layeredDrawer -> layeredDrawer.attachLayerBefore(IdentifiedLayer.CHAT, EXAMPLE_LAYER, HudRenderingEntrypoint::render));
}
private static void render(DrawContext context, RenderTickCounter 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.getMeasuringTimeMs() / 1000.0;
// "lerp" simply means "linear interpolation", which is a fancy way of saying "blend".
float lerpedAmount = MathHelper.abs(MathHelper.sin((float) currentTime));
int lerpedColor = ColorHelper.lerp(lerpedAmount, color, targetColor);
// Draw a square with the lerped color.
// x1, x2, y1, y2, z, color
context.fill(0, 0, 10, 10, 0, lerpedColor);
}
}