Brapi
一个用于 Fabric 模组的 UI 渲染库。支持圆角矩形、文本、渐变、纹理和九宫格精灵,并提供简单的 API。未来还将支持 SVG 光栅化器、阴影、更多文本选项、可切换的即时模式渲染以及线条绘制。
为什么选择 Brapi
在 Minecraft 中制作美观的 UI 比应有的难度更大。原版工具对于基本方框的处理尚可,但一旦你想要平滑的圆角、圆形或美观的文本,就会遇到瓶颈。
通常你只有两个选择:
- 引入像 Skia 这样庞大的图形库,首次配置困难且会导致模组体积显著增加
- 从零开始编写自己的着色器,这意味着要处理额外的设置和样板代码,而大多数模组开发者更希望跳过这些步骤
Brapi 提供了第三种选择。它是一个轻量级层,建立在 Minecraft 现有的渲染系统之上,让你无需痛苦即可获得出色的视觉效果。
主要优势:
- 零样板代码:无需繁琐的设置。只需创建
BRender,添加绘图调用,然后执行 flush(刷新)即可。
- 简化复杂形状:仅用一行代码即可绘制圆形、轮廓线和圆角矩形(甚至可以是不同大小的圆角)。
- 自动批处理与分层:Brapi 会自动确定正确的绘制顺序,因此你的 UI 能够正确显示,无需你进行微观管理。
? 设置
作为依赖项添加
将仓库和依赖项添加到 build.gradle 中:
repositories {
exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
}
dependencies {
modImplementation "maven.modrinth:brapi:0.26+${minecraft_version}+fabric" // 目前仅支持 Minecraft 版本 1.21.11
}
fabric.mod.json:
"depends": {
"brapi": "*"
}
在代码中使用
创建一个 BRender 实例,添加绘图调用,然后调用 flush(graphics) 提交。所有内容将在帧末按层级排序后渲染。
? 所有颜色均为 ARGB 格式:0xFFFF0000 = 不透明红色,0x80FF0000 = 50% 透明红色。
? 坐标单位为 GUI 缩放单位:在缩放比例为 3 时,(100, 100) 等于 (300, 300) 像素。
填充形状
bRender.rect(100, 100, 100, 100, 0xFFFF0000, layer); - 100x100 红色矩形
bRender.roundRect(100, 100, 100, 100, 0xFFFF0000, 10, layer); - 圆角矩形,半径 10px
bRender.roundRect(100, 100, 100, 100, 0xFFFF0000, 10, 5, layer); - 左上/右下半径 10px,右上/左下半径 5px
bRender.roundRect(100, 100, 100, 100, 0xFFFF0000, 10, 5, 20, 30, layer); - 各个角的独立半径
bRender.circle(100, 100, 50, 0xFFFF0000, layer); - 圆形,半径 50px
渐变
所有填充形状均支持通过 Gradient 和 GradientDirection 实现渐变效果:
bRender.roundRect(100, 100, 100, 100, gradient, GradientDirection.LEFT_RIGHT, 10, layer);
方向包括:LEFT_RIGHT(从左到右)、TOP_BOTTOM(从上到下)、TOP_LEFT_BOTTOM_RIGHT(从左上到右下)、TOP_RIGHT_BOTTOM_LEFT(从右上到左下)
描边(仅轮廓)
bRender.stroke(100, 100, 100, 100, 0xFFFF0000, 2, layer); - 2px 宽度的矩形轮廓
bRender.strokeRounded(100, 100, 100, 100, 0xFFFF0000, 10, 2, layer); - 2px 宽度的圆角轮廓,半径 10px
bRender.strokeRounded(100, 100, 100, 100, 0xFFFF0000, 10, 5, 2, layer); - 左上/右下半径 10px,右上/左下半径 5px,宽度 2px
bRender.strokeRounded(100, 100, 100, 100, 0xFFFF0000, 10, 5, 20, 30, 2, layer); - 各个角的独立半径
填充 + 描边
bRender.roundRectStroked(100, 100, 100, 100, 0xFF0000FF, 0xFFFF0000, 10, 2, layer); - 蓝色填充,2px 红色描边
文本
bRender.drawText(font, "Hello!", 100, 100, 24, 0xFFFFFFFF, layer); - BFont 文本
bRender.drawText(font, formattedCharSequence, 100, 100, 24, 0xFFFFFFFF, layer); - 格式化文本,支持逐字符颜色
纹理
bRender.drawTexture(tex, 100, 100, 200, 150, 0xFFFFFFFF, linear, layer); - 拉伸以填充区域
bRender.drawTextureCropped(tex, 100, 100, 64, 64, 0, 0, 32, 32, 0xFFFFFFFF, linear, layer); - 裁剪纹理
bRender.drawTextureTiled(tex, 100, 100, 200, 200, 0xFFFFFFFF, linear, layer); - 平铺纹理
bRender.drawTexture9Slice(slice, 100, 100, 300, 200, 0xFFFFFFFF, linear, layer); - 九宫格纹理
? 刷新(Flush)
BRender r = new BRender();
r.roundRect(...);
r.flush(graphics); // 提交到绘制列表
//或者
r.flush(graphics, BRender.WithOffset(100, 100)); // 带有 100 像素偏移量
Brapi
A UI rendering library for Fabric mods. Supports rounded rectangles, text, gradients, textures, and 9-slice sprites with a simple API. With future support for SVG rasterizer, shadows, more text options, toggleable immediate mode rendering and lines.
Why
Making good-looking UIs in Minecraft is harder than it should be. Vanilla's tools work fine for basic boxes, but as soon as you want smooth rounded corners, circles, or nice-looking text, you hit a wall.
Normally you're stuck with two options:
- Bring in a heavy graphics library like Skia, which is hard to set-up for first time and makes the mod size very big
- Write your own shaders from scratch, which means dealing with extra setup and boilerplate most modders would rather skip
Brapi gives a third option. It's a lightweight layer that sits on top of Minecraft's existing rendering system, so you get the nice visuals without the pain.
Key Benefits:
- Zero Boilerplate: No setup hassle. Just create a BRender, add your draw calls, and flush.
- Complex Shapes Made Simple: Draw circles, outlines, and rounded rectangles (even with different corner sizes) in a single line of code.
- Automatic Batching & Layering: Brapi figures out the right draw order on its own, so your UI looks right without you micromanaging it.
? Setup
Adding as dependency
Add the repository and dependency to build.gradle:
repositories {
exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
}
dependencies {
modImplementation "maven.modrinth:brapi:0.26+${minecraft_version}+fabric" // only supported minecraft version is 1.21.11 right now
}
fabric.mod.json:
"depends": {
"brapi": "*"
}
Using in code
Create a BRender instance, add draw calls, then call flush(graphics) to submit. Everything renders at end of frame sorted by layer.
? All colors are ARGB: 0xFFFF0000 = opaque red, 0x80FF0000 = 50% transparent red.
? Coordinates are in GuiScale units: (100, 100) at scale 3 = (300, 300) pixels.
Filled shapes
bRender.rect(100, 100, 100, 100, 0xFFFF0000, layer); - red 100x100 rectangle
bRender.roundRect(100, 100, 100, 100, 0xFFFF0000, 10, layer); - rounded rectangle, 10px radius
bRender.roundRect(100, 100, 100, 100, 0xFFFF0000, 10, 5, layer); - 10px TL/BR, 5px TR/BL radius
bRender.roundRect(100, 100, 100, 100, 0xFFFF0000, 10, 5, 20, 30, layer); - individual corner radii
bRender.circle(100, 100, 50, 0xFFFF0000, layer); - circle, 50px radius
Gradients
All filled shapes support gradients via Gradient and GradientDirection:
bRender.roundRect(100, 100, 100, 100, gradient, GradientDirection.LEFT_RIGHT, 10, layer);
Directions: LEFT_RIGHT, TOP_BOTTOM, TOP_LEFT_BOTTOM_RIGHT, TOP_RIGHT_BOTTOM_LEFT
Strokes (outline only)
bRender.stroke(100, 100, 100, 100, 0xFFFF0000, 2, layer); - 2px outline rectangle
bRender.strokeRounded(100, 100, 100, 100, 0xFFFF0000, 10, 2, layer); - 2px rounded outline, 10px radius
bRender.strokeRounded(100, 100, 100, 100, 0xFFFF0000, 10, 5, 2, layer); - 10px TL/BR, 5px TR/BL
bRender.strokeRounded(100, 100, 100, 100, 0xFFFF0000, 10, 5, 20, 30, 2, layer); - individual radii
Filled + stroke
bRender.roundRectStroked(100, 100, 100, 100, 0xFF0000FF, 0xFFFF0000, 10, 2, layer); - blue fill, 2px red stroke
Text
bRender.drawText(font, "Hello!", 100, 100, 24, 0xFFFFFFFF, layer); - BFont text
bRender.drawText(font, formattedCharSequence, 100, 100, 24, 0xFFFFFFFF, layer); - formatted text with per-character colors
Textures
bRender.drawTexture(tex, 100, 100, 200, 150, 0xFFFFFFFF, linear, layer); - stretch to fill
bRender.drawTextureCropped(tex, 100, 100, 64, 64, 0, 0, 32, 32, 0xFFFFFFFF, linear, layer); - crop
bRender.drawTextureTiled(tex, 100, 100, 200, 200, 0xFFFFFFFF, linear, layer); - tile
bRender.drawTexture9Slice(slice, 100, 100, 300, 200, 0xFFFFFFFF, linear, layer); - 9-slice
? Flushing
BRender r = new BRender();
r.roundRect(...);
r.flush(graphics); // submit to draw list
//OR
r.flush(graphics, BRender.WithOffset(100, 100)); // with 100 pixel offset
评论 0
登录后参与评分
还没有评论,来抢沙发吧