Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import com.github.epsilon.graphics.shaders.BlurShader
import com.github.epsilon.gui.lib.RoundRect
import com.github.epsilon.gui.lib.Shadow
import com.github.epsilon.gui.lib.UiScope
import com.github.epsilon.kernel.jvm.NonNullContext
import com.github.epsilon.kernel.jvm.runSafe
import com.github.epsilon.gui.lib.Item
import net.minecraft.client.DeltaTracker
import net.minecraft.client.gui.GuiGraphicsExtractor
import net.minecraft.world.item.ItemStack
import java.awt.Color

/**
Expand Down Expand Up @@ -71,44 +68,13 @@ object Inventory : HudModule("Inventory HUD", 0f, 0f, 180f, 80f) {
val slotX = padding + col * (slotSize + gap)
val slotY = padding + row * (slotSize + gap)
RoundRect(slotX, slotY, slotSize, slotSize, slotRadius, slotColor)
val stack = mc.player?.inventory?.getItem(9 + row * 9 + col) ?: continue
Item(
stack, slotX + scale, slotY + scale, 16f * scale,
decorations = showCount && stack.count > 1
)
}
}
}

override fun renderOverlay(graphics: GuiGraphicsExtractor, deltaTracker: DeltaTracker) {
runSafe { renderItems(graphics) }
}

context(nn: NonNullContext)
private fun renderItems(graphics: GuiGraphicsExtractor) {
val scale = this.scale.toFloat()
val slotSize = SLOT_SIZE * scale
val gap = SLOT_GAP * scale
val padding = PADDING * scale

for (row in 0 until 3) {
for (col in 0 until 9) {
val slotIndex = 9 + row * 9 + col
val stack = nn.player.inventory.getItem(slotIndex)
if (!stack.isEmpty) {
// 物品 overlay 走 GuiGraphics,绝对坐标(与 HUD 元素持久化位置一致)
val slotX = x + padding + col * (slotSize + gap)
val slotY = y + padding + row * (slotSize + gap)
drawItem(graphics, stack, slotX, slotY, scale)
}
}
}
}

private fun drawItem(graphics: GuiGraphicsExtractor, stack: ItemStack, slotX: Float, slotY: Float, scale: Float) {
graphics.pose().pushMatrix()
graphics.pose().translate(slotX + scale, slotY + scale)
graphics.pose().scale(scale, scale)
graphics.item(stack, 0, 0)
if (showCount && stack.count > 1) {
graphics.itemDecorations(mc.font, stack, 0, 0, stack.count.toString())
}
graphics.pose().popMatrix()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.github.epsilon.managers.HealthManager
import com.github.epsilon.modules.impl.combat.KillAura
import com.google.common.base.Suppliers
import net.minecraft.client.DeltaTracker
import net.minecraft.client.gui.GuiGraphicsExtractor
import net.minecraft.client.player.AbstractClientPlayer
import net.minecraft.client.renderer.texture.AbstractTexture
import net.minecraft.util.Mth
Expand Down Expand Up @@ -288,6 +287,12 @@ object TargetHUD : HudModule("Target HUD", 0f, 0f, 180f, 80f) {

Text(nameText, scaledTextStartX, scaledContentY, scaledTextScale, withAlpha(textColor, animationScale))
Text(healthText, scaledHealthTextX, scaledContentY, scaledTextScale, withAlpha(textColor, animationScale))
renderEquipmentItems(
target, scaledTextStartX,
Mth.lerp(animationScale, centerY, contentY + textHeight + 2.8f * panelScale),
EQUIPMENT_ITEM_SCALE * panelScale * animationScale, 1.5f * panelScale * animationScale,
withAlpha(Color.WHITE, animationScale)
)
}

/**
Expand Down Expand Up @@ -404,60 +409,13 @@ object TargetHUD : HudModule("Target HUD", 0f, 0f, 180f, 80f) {
}
}

override fun renderOverlay(graphics: GuiGraphicsExtractor, deltaTracker: DeltaTracker) {
if (style == Style.Akrien) {
return
}

val panelScale = scale.toFloat()
val target = renderedTarget ?: return
if (visibilityProgress <= 0.01f) return

val textRenderer = textRendererSupplier.get()
val panelWidth = panelWidthSetting.toFloat() * panelScale
val panelHeight = panelHeightSetting.toFloat() * panelScale
val animationScale = Easing.EASE_OUT_SINE.function.apply(visibilityProgress.coerceIn(0.0f, 1.0f))
val pad = 5.0f * panelScale
val barHeight = healthBarHeight.toFloat() * panelScale
val innerHeight = maxOf(1.0f, panelHeight - pad * 2.0f)
val contentAreaHeight = maxOf(1.0f, innerHeight - pad - barHeight)
val headSize = minOf(contentAreaHeight, maxOf(26.0f * panelScale, panelHeight * 0.6f) * 1.05f)
val textScale = maxOf(0.45f, nameSize.toFloat() / 14.0f) * panelScale
val textHeight = textRenderer.getHeight(textScale)
val contentRowHeight = maxOf(headSize, textHeight)
val contentBlockHeight = contentRowHeight + pad + barHeight
val contentStartY = y + pad + maxOf(0.0f, (innerHeight - contentBlockHeight) / 2.0f)
val headY = contentStartY + (contentRowHeight - headSize) / 2.0f
val headX = x + pad
val textStartX = headX + headSize + pad
val contentY = headY + 2.0f * panelScale
val equipmentY = contentY + textHeight + 2.8f * panelScale
val equipmentScale = EQUIPMENT_ITEM_SCALE * panelScale
val equipmentGap = 1.5f * panelScale
val centerX = x + panelWidth / 2.0f
val centerY = y + panelHeight / 2.0f
val scaledEquipmentX = Mth.lerp(animationScale, centerX, textStartX)
val scaledEquipmentY = Mth.lerp(animationScale, centerY, equipmentY)
val scaledEquipmentScale = equipmentScale * animationScale
val scaledEquipmentGap = equipmentGap * animationScale

renderEquipmentItems(
graphics,
target,
scaledEquipmentX,
scaledEquipmentY,
scaledEquipmentScale,
scaledEquipmentGap
)
}

private fun renderEquipmentItems(
graphics: GuiGraphicsExtractor,
private fun UiScope.renderEquipmentItems(
target: LivingEntity,
startX: Float,
y: Float,
scale: Float,
gap: Float,
color: Color,
) {
val equipmentItems: MutableList<ItemStack> = ArrayList(5)
appendEquipmentItem(equipmentItems, target.mainHandItem)
Expand All @@ -473,7 +431,7 @@ object TargetHUD : HudModule("Target HUD", 0f, 0f, 180f, 80f) {
val itemSize = 16.0f * scale
var itemX = startX
for (i in equipmentItems.indices) {
drawItem(graphics, target, equipmentItems[i], itemX, y, scale, target.id + i)
Item(equipmentItems[i], itemX + scale, y + scale, itemSize, color, target, target.id + i)
itemX += itemSize + gap
}
}
Expand All @@ -484,22 +442,6 @@ object TargetHUD : HudModule("Target HUD", 0f, 0f, 180f, 80f) {
}
}

private fun drawItem(
graphics: GuiGraphicsExtractor,
owner: LivingEntity,
stack: ItemStack,
x: Float,
y: Float,
scale: Float,
seed: Int
) {
graphics.pose().pushMatrix()
graphics.pose().translate(x + scale, y + scale)
graphics.pose().scale(scale, scale)
graphics.item(owner, stack, 0, 0, seed)
graphics.pose().popMatrix()
}

private fun resolveTarget(): LivingEntity? {
val target = KillAura.target
if (isRenderableTarget(target)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ import com.github.epsilon.elements.impl.island.instance.LandInstance
import com.github.epsilon.elements.impl.island.pattern.impl.CheckPattern
import com.github.epsilon.graphics.renderers.TextRenderer
import com.github.epsilon.gui.lib.*
import com.github.epsilon.kernel.MinecraftWrapper.mc
import com.github.epsilon.kernel.jvm.runSafe
import com.github.epsilon.kernel.render.animation.Animation
import com.github.epsilon.kernel.render.animation.Easing
import com.github.epsilon.kernel.time.TimerUtils
import net.minecraft.client.color.block.BlockTintSource
import net.minecraft.client.renderer.texture.TextureAtlasSprite
import net.minecraft.world.item.BlockItem
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.block.state.BlockState
import java.awt.Color
import java.util.function.Supplier
import kotlin.math.abs
Expand All @@ -34,8 +28,7 @@ class ScaffoldBlocksInstance(
private val countAnim = Animation(Easing.DECELERATE, 560)

private val iconRefreshTimer = TimerUtils()
private var iconSprite: TextureAtlasSprite? = null
private var iconTint: Color = Color.WHITE
private var iconStack: ItemStack = ItemStack.EMPTY

private var lastUpdateNs: Long = System.nanoTime()
private var spinAngleDeg: Float = 0f
Expand Down Expand Up @@ -71,13 +64,13 @@ class ScaffoldBlocksInstance(
lastUpdateNs = nowNs
spinAngleDeg = (spinAngleDeg + SPIN_DEGREES_PER_SECOND * deltaSeconds) % 360f

if (iconSprite == null || iconRefreshTimer.every(350L)) {
updateIconTexture(iconSupplier.get())
if (iconStack.isEmpty || iconRefreshTimer.every(350L)) {
iconStack = iconSupplier.get().copy()
}
}

override fun draw(scope: UiScope, width: Float, height: Float) {
drawBlockTexture(scope, height)
drawBlockIcon(scope, height)

val ringRadius = (RING_SIZE - RING_STROKE) / 2f
val centerX = width - PADDING - RING_SIZE / 2f
Expand All @@ -99,32 +92,8 @@ class ScaffoldBlocksInstance(
drawRollingCount(scope, centerX, centerY)
}

private fun updateIconTexture(stack: ItemStack) {
iconSprite = null
iconTint = Color.WHITE
if (stack.isEmpty) return
val blockItem = stack.item as? BlockItem ?: return

val state: BlockState = blockItem.block.defaultBlockState()
iconSprite = mc.modelManager.blockStateModelSet.getParticleMaterial(state).sprite()

val tintSource: BlockTintSource = mc.blockColors.getTintSource(state, 0) ?: return
runSafe {
val tint = tintSource.colorAsTerrainParticle(state, level, player.blockPosition())
iconTint = Color(tint, true)
}
}

private fun drawBlockTexture(scope: UiScope, height: Float) {
val sprite = iconSprite ?: return

val iconY = (height - ICON_SIZE) / 2f
scope.RoundedTexture(
sprite.atlasLocation(),
PADDING, iconY, ICON_SIZE, ICON_SIZE, ICON_RADIUS,
sprite.u0, sprite.v0, sprite.u1, sprite.v1,
fade(iconTint)
)
private fun drawBlockIcon(scope: UiScope, height: Float) {
scope.Item(iconStack, PADDING, (height - ICON_SIZE) / 2f, ICON_SIZE, fade(Color.WHITE))
}

private fun drawRollingCount(scope: UiScope, centerX: Float, centerY: Float) {
Expand Down Expand Up @@ -236,7 +205,6 @@ class ScaffoldBlocksInstance(

private const val PADDING = 6f
private const val ICON_SIZE = 26f
private const val ICON_RADIUS = ICON_SIZE * 0.4f
private const val GAP = 6f
private const val RING_SIZE = 26f
private const val RING_STROKE = 2.5f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,65 @@ package com.github.epsilon.events.impl
import com.github.epsilon.events.api.EventBus
import com.github.epsilon.events.api.IEvent
import com.github.epsilon.events.api.IPosting
import com.github.epsilon.graphics.renderers.TextRenderer
import com.github.epsilon.graphics.schedulers.render2d.Render2DScheduler
import net.minecraft.client.gui.GuiGraphicsExtractor

open class Render2DEvent protected constructor(val guiGraphics: GuiGraphicsExtractor) {

class Level(guiGraphics: GuiGraphicsExtractor) : Render2DEvent(guiGraphics), IEvent, IPosting by Companion {
companion object : EventBus()
/** 每次读取分配后续图层;相关图元应复用返回的 canvas,仅在当前事件派发期间使用。 */
val canvas: Render2DScheduler.Canvas
get() = backend().canvas(nextLayer++)

/** 共享字体度量;文字通过 canvas 提交。 */
val textMetrics: TextRenderer
get() = backend().textMetrics()

/** 模糊、目标切换、原版或独立 renderer 绘制前提交已排队命令。 */
fun flush() {
checkActive()
scheduler?.flush()
}

private fun checkActive() {
check(activeEvent === this) { "Render2DEvent.Level requires its active event dispatch" }
}

private fun backend(): Render2DScheduler {
checkActive()
return scheduler ?: Render2DScheduler().also {
scheduler = it
it.beginFrame()
}
}

companion object : EventBus() {
private var scheduler: Render2DScheduler? = null
private var activeEvent: Level? = null
private var nextLayer = 0

/** 渲染线程派发 Level 事件时管理共享后端,异常时也清理当帧记录并结束几何生命周期。 */
override fun post(event: Any) {
require(event is Level)
check(activeEvent == null) { "Render2DEvent.Level dispatch is already active" }
activeEvent = event
nextLayer = 0
try {
scheduler?.clear()
scheduler?.beginFrame()
super.post(event)
event.flush()
} finally {
try {
scheduler?.clear()
scheduler?.endFrame()
} finally {
activeEvent = null
}
}
}
}
}

class HUD(guiGraphics: GuiGraphicsExtractor) : Render2DEvent(guiGraphics), IEvent, IPosting by Companion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ object LuminRenderPipelines {
.withCull(false)
.build()

/** 原版物品图集和 PIP 纹理保存预乘 alpha;淡出时 RGB 与 alpha 同步衰减。 */
val ITEM_TEXTURE: RenderPipeline = RenderPipeline.builder(NO_BLEND_DEPTH_SNIPPET)
.withLocation(ResourceLocationUtils.getIdentifier("pipelines/item_texture"))
.withVertexBinding(0, LuminVertexFormats.TEXTURE)
.withPrimitiveTopology(PrimitiveTopology.QUADS)
.withVertexShader(ResourceLocationUtils.getIdentifier("texture"))
.withFragmentShader(ResourceLocationUtils.getIdentifier("texture"))
.withBindGroupLayout(BindGroupLayouts.SAMPLER0)
.withColorTargetState(ColorTargetState(BlendFunction.TRANSLUCENT_PREMULTIPLIED_ALPHA))
.withShaderDefine("EPSILON_PREMULTIPLIED_ALPHA")
.withCull(false)
.build()

val TRIANGLE: RenderPipeline = RenderPipeline.builder(NO_BLEND_DEPTH_SNIPPET)
.withLocation(ResourceLocationUtils.getIdentifier("pipelines/triangle"))
.withVertexBinding(0, DefaultVertexFormat.POSITION_COLOR)
Expand Down
31 changes: 31 additions & 0 deletions common/src/main/kotlin/com/github/epsilon/graphics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,37 @@ safely. Standalone renderers keep their existing ring-buffer behavior.

---

## Shared world labels

`Render2DEvent.Level` owns one lazy shared `Render2DScheduler`. Crystal Aura, TNT Timer, Logout Spots,
Name Tags and ESP 2D share its text metrics, geometry and draw backend. The event bus manages the render scope
and flushes before returning to `MixinGuiRenderer` for vanilla level GUI rendering. Cleanup runs even when submission fails;
the scheduler's GPU resources follow `RendererManager` shutdown.

Within `safeEventHandler<Render2DEvent.Level> { event -> ... }`, use `event.textMetrics` to measure and `event.canvas`
to submit. Each read of `event.canvas` reserves the next layer, preserving module and label order. Keep the returned
canvas for related primitives; read it again for a later layer. These APIs are only valid during that event's dispatch.
Modules must not clear or close the shared backend. Call `event.flush()` before
blur, target changes, vanilla or standalone renderer draws. Name Tags uses this barrier before its background blur.
HUD and screen UI keep their own render scopes.

## Items and block previews on Canvas

Use `canvas.addItem(stack, x, y, size = 16f)` or `canvas.addBlock(block, x, y, size = 16f)`.
The UI DSL exposes the same operations as `Item(...)` and `Block(...)`. Positions and sizes use Canvas coordinates;
items follow the same layers, affine transforms, tint and scissor as shapes and text. `owner` and `seed` preserve
entity-dependent item models. `decorations = true` adds the vanilla durability/cooldown geometry and a Canvas-font
count with a shadow; `countText` optionally overrides the count. Blocks use their inventory item model, including
resource-pack overrides; blocks without an item form produce no icon. This is not an arbitrary world BlockState renderer.

Commands snapshot the stack when recorded. Persistent UI must recompose when the stack/components/count change.
Every flush resolves `ItemModelResolver` in GUI display context, then uses vanilla `GuiItemAtlas` or
`OversizedItemRenderer` to prepare textures before opening the Canvas pass. Foil and animated models retain vanilla
rendering behavior. Atlas UVs are never stored in retained geometry. The final quads use premultiplied alpha, including
Canvas tint/fade. Resource reloads and GUI-scale changes invalidate item textures; the scheduler closes them at shutdown.
Inventory HUD, Target HUD equipment, registry item/block previews and the island scaffold icon use this path instead
of deferred `GuiGraphicsExtractor` item overlays.

## Retained UI and ordered 2D rendering

`UiScene` owns the composition tree and a shared 2D backend. Composition nodes keep their own display lists and child
Expand Down
Loading
Loading