Skip to content

Commit

Permalink
make background opacity accurate with some math πŸ€“πŸ€“πŸ€“
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jul 22, 2024
1 parent d7a0a3c commit 517e745
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public abstract class RenderMixin {
)
private void polyNametag$drawBackground(Entity entity, String str, double x, double y, double z, int maxDistance, CallbackInfo ci) {
if (!ModConfig.INSTANCE.enabled) return;
NametagRenderingKt.setDrawingWithDepth(true);
NametagRenderingKt.drawFrontBackground(str, entity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static org.polyfrost.polynametag.render.NametagRenderingKt.drawFrontBackground;

@Mixin(value = RendererLivingEntity.class, priority = 1001)
public abstract class RendererLivingEntityMixin {

Expand Down Expand Up @@ -94,7 +92,7 @@ private void cancel(EntityLivingBase entity, double x, double y, double z, Callb
private void drawBG(EntityLivingBase entity, double x, double y, double z, CallbackInfo ci) {
if (!ModConfig.INSTANCE.enabled) return;
if (PolyNametag.INSTANCE.getShouldDrawIndicator() && ModConfig.INSTANCE.getEssentialOffset()) GlStateManager.translate(5f, 0f, 0f);
drawFrontBackground(entity.getDisplayName().getFormattedText(), NametagRenderingKt.getBackBackgroundGLColorOrEmpty(), entity);
NametagRenderingKt.drawFrontBackground(entity.getDisplayName().getFormattedText(), NametagRenderingKt.getBackBackgroundGLColorOrEmpty(), entity);
}

@Redirect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private void drawBG(LevelheadTag tag, EntityPlayer entityIn, double x, double y,
int stringWidth = Minecraft.getMinecraft().fontRendererObj.getStringWidth(tag.getString()) / 2;
NametagRenderingKt.drawFrontBackground(-stringWidth - 2, stringWidth + 1, NametagRenderingKt.getBackBackgroundGLColorOrEmpty(), entityIn);
GlStateManager.enableDepth();
NametagRenderingKt.setDrawingWithDepth(true);
NametagRenderingKt.drawFrontBackground(-stringWidth - 2, stringWidth + 1, entityIn);
GlStateManager.depthMask(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import kotlin.math.sin

var drawingText = false

var drawingWithDepth = false

internal fun shouldDrawBackground() =
ModConfig.background && (!PolyNametag.isPatcher || !PatcherConfig.disableNametagBoxes)

val NO_COLOR = Color(0f, 0f, 0f, 0f)
fun getBackBackgroundGLColorOrEmpty(): Color =
if (shouldDrawBackground()) with(ModConfig.backgroundColor) {
Color(red, green, blue, alpha.coerceAtMost(0x3F))
Color(red, green, blue, alpha.coerceAtMost(63))
} else {
NO_COLOR
}
Expand Down Expand Up @@ -68,12 +70,17 @@ fun drawBackground(xStart: Double, xEnd: Double, color: Color, entity: Entity) {
GL11.glTranslated((realStart + xEnd) / 2f, 3.5, 0.01)
GL11.glBegin(GL11.GL_TRIANGLE_FAN)
with(color) {
GL11.glColor4f(red / 255f, green / 255f, blue / 255f, alpha / 255f)
val a = alpha.coerceAtMost(63)
val realAlpha = if (drawingWithDepth) (alpha - a) / (255 - a).toFloat() else alpha / 255f
GL11.glColor4f(red / 255f, green / 255f, blue / 255f, realAlpha)
}
drawingWithDepth = false

val halfWidth = (xEnd - realStart) / 2f + ModConfig.paddingX

val radius = if (ModConfig.rounded) ModConfig.cornerRadius.coerceAtMost(4.5f + ModConfig.paddingY) else 0f
val radius = if (ModConfig.rounded) ModConfig.cornerRadius.coerceAtMost(4.5f + ModConfig.paddingY).coerceAtMost(halfWidth.toFloat()) else 0f

val width = (xEnd - realStart) / 2f + ModConfig.paddingX - radius
val width = halfWidth - radius

val distanceFromPlayer = entity.getDistanceToEntity(mc.thePlayer)
val quality = ((distanceFromPlayer * 4 + 10).coerceAtMost(350f) / 4).toInt()
Expand Down

0 comments on commit 517e745

Please sign in to comment.