From 88da1b306aafd548d84cee002b74a2544bfee908 Mon Sep 17 00:00:00 2001 From: Keridos <2742845+Keridos@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:04:04 +0100 Subject: [PATCH] feat: damage amp LEDs on the turret base plate (ported from 1.20.1+-damageamp-render) The LEDs are drawn with OMLib's GlowGeometry; the base syncs a damageAmp flag to clients. omlib points at 1.21.11-damageamp-render. --- CHANGELOG.md | 4 + omlib | 2 +- .../render/blockentity/DamageAmpRenderer.java | 34 ++++ .../render/blockentity/TurretRenderer.java | 8 +- .../render/effect/GlowDotsRenderer.java | 150 ++++++++++++++++++ .../render/effect/GlowPanelsRenderer.java | 137 ++++++++++++++++ .../blockentity/turret/TurretBlockEntity.java | 15 +- 7 files changed, 346 insertions(+), 4 deletions(-) create mode 100644 src/client/java/omtteam/openmodularturrets/client/render/blockentity/DamageAmpRenderer.java create mode 100644 src/client/java/omtteam/openmodularturrets/client/render/effect/GlowDotsRenderer.java create mode 100644 src/client/java/omtteam/openmodularturrets/client/render/effect/GlowPanelsRenderer.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1dd4d9..9a9fb3ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ Before a release, `./gradlew releaseChangelog` turns *Unreleased* into the relea ## [Unreleased] +### Added + +- Turrets on a base with a damage amp addon show a small red light on each corner of their base plate + ### Changed - Config: `bases.allowContraptionMovement` is deprecated on Minecraft 1.21.11; it never had an effect here, as no contraption mod (Create, Create Aeronautics) is supported on this version diff --git a/omlib b/omlib index 70b649bd..efca2e13 160000 --- a/omlib +++ b/omlib @@ -1 +1 @@ -Subproject commit 70b649bd1bbee83c59ba4b4053699c3bda7c9014 +Subproject commit efca2e13178cb5fecd29cfd2e85f3870e69ff976 diff --git a/src/client/java/omtteam/openmodularturrets/client/render/blockentity/DamageAmpRenderer.java b/src/client/java/omtteam/openmodularturrets/client/render/blockentity/DamageAmpRenderer.java new file mode 100644 index 00000000..91f8a9f9 --- /dev/null +++ b/src/client/java/omtteam/openmodularturrets/client/render/blockentity/DamageAmpRenderer.java @@ -0,0 +1,34 @@ +package omtteam.openmodularturrets.client.render.blockentity; + +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.renderer.SubmitNodeCollector; +import omtteam.openmodularturrets.client.render.effect.GlowPanelsRenderer; + +/** + * Damage amp effect on turrets: a small red LED in each upper corner of the turret's baseplate, lighting the top + * face and both side faces at the corner. + *

+ * The LEDs are submitted to the frame's {@link SubmitNodeCollector}. + */ +public final class DamageAmpRenderer { + /** Top of the baseplate in the turret's local frame (the plate is 12×12×1 pixels at the bottom of the block). */ + private static final float PLATE_TOP = -0.4375F; + /** Half side of the baseplate (6 pixels). */ + private static final float PLATE_HALF_SIZE = 0.375F; + /** LED edge length, under the plate's thickness of one pixel. */ + private static final float LED_SIZE = 0.04F; + + private static final GlowPanelsRenderer LEDS = GlowPanelsRenderer.builder() + .panels(GlowPanelsRenderer.boxTopCorners(PLATE_HALF_SIZE, PLATE_TOP, LED_SIZE)) + .colour(0xFF1A1A) + .alpha(0.85F) + .build(); + + private DamageAmpRenderer() { + } + + /** @param poseStack at the block center, in the turret's local frame (x = right, y = up, z = forward) */ + public static void submit(PoseStack poseStack, SubmitNodeCollector collector) { + LEDS.render(poseStack, collector); + } +} diff --git a/src/client/java/omtteam/openmodularturrets/client/render/blockentity/TurretRenderer.java b/src/client/java/omtteam/openmodularturrets/client/render/blockentity/TurretRenderer.java index fb1eca37..5b5a9ae5 100644 --- a/src/client/java/omtteam/openmodularturrets/client/render/blockentity/TurretRenderer.java +++ b/src/client/java/omtteam/openmodularturrets/client/render/blockentity/TurretRenderer.java @@ -26,7 +26,8 @@ /** * Turret renderer (1.12.2 {@code AbstractTurretRenderer}): orients the model on its mounting face with the frame - * from {@link TurretMath}, then applies the interpolated yaw and pitch. Concealed turrets are not drawn. + * from {@link TurretMath}, then applies the interpolated yaw and pitch. Concealed turrets are not drawn. A base with a + * damage amp addon adds the {@link DamageAmpRenderer corner LEDs} to every turret's base plate. *

* The turret's data is copied into a {@link State} when the frame is extracted, and drawn from it in {@link #submit}. */ @@ -39,6 +40,7 @@ public static final class State extends BlockEntityRenderState { Face face = Face.UP; @Nullable RenderType renderType; + boolean damageAmp; } private final Map models; @@ -65,6 +67,7 @@ public void extractRenderState(TurretBlockEntity turret, State state, float part turret.isActive(), turret.getRenderSpin(partialTick)); state.face = turret.getFace(); state.renderType = model.renderTypeFor(state.pose); + state.damageAmp = turret.hasDamageAmp(); } @Override @@ -75,6 +78,9 @@ public void submit(State state, PoseStack poseStack, SubmitNodeCollector collect poseStack.mulPose(rotationFor(state.face)); submitModel(state.model, state.pose, state.renderType, poseStack, collector, state.lightCoords, OverlayTexture.NO_OVERLAY, state.breakProgress); + if (state.damageAmp) { + DamageAmpRenderer.submit(poseStack, collector); + } poseStack.popPose(); } diff --git a/src/client/java/omtteam/openmodularturrets/client/render/effect/GlowDotsRenderer.java b/src/client/java/omtteam/openmodularturrets/client/render/effect/GlowDotsRenderer.java new file mode 100644 index 00000000..0c345573 --- /dev/null +++ b/src/client/java/omtteam/openmodularturrets/client/render/effect/GlowDotsRenderer.java @@ -0,0 +1,150 @@ +package omtteam.openmodularturrets.client.render.effect; + +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import net.minecraft.client.renderer.SubmitNodeCollector; +import omtteam.omlib.client.compat.ClientCompat; +import omtteam.omlib.client.render.fx.GlowGeometry; +import org.joml.Matrix4f; +import org.joml.Vector3f; + +import java.util.ArrayList; +import java.util.List; + +/** + * Small glowing lights at fixed points: a bright core sphere inside an optional faint halo, optionally pulsing. + * Instances are immutable and configured with {@link #builder()}; one instance can be shared by any number of + * renderers. + */ +public final class GlowDotsRenderer { + private final List points; + private final int rgb; + private final float coreRadius; + private final float coreAlpha; + private final float haloRadius; + private final float haloAlpha; + private final int slices; + private final int stacks; + private final double pulseTicks; + private final float pulseMin; + + private GlowDotsRenderer(Builder builder) { + this.points = List.copyOf(builder.points); + this.rgb = builder.rgb; + this.coreRadius = builder.coreRadius; + this.coreAlpha = builder.coreAlpha; + this.haloRadius = builder.haloRadius; + this.haloAlpha = builder.haloAlpha; + this.slices = builder.slices; + this.stacks = builder.stacks; + this.pulseTicks = builder.pulseTicks; + this.pulseMin = builder.pulseMin; + } + + public static Builder builder() { + return new Builder(); + } + + /** The four corners of a square of half side length {@code halfSize}, centred on the y axis at height {@code y}. */ + public static List squareCorners(float halfSize, float y) { + return List.of(new Vector3f(-halfSize, y, -halfSize), new Vector3f(halfSize, y, -halfSize), + new Vector3f(halfSize, y, halfSize), new Vector3f(-halfSize, y, halfSize)); + } + + /* + * Submits the dots ({@link ClientCompat#lightning()}). + * + * @param poseStack the frame the points are given in + * @param ticks animation time in ticks, including the partial tick (only used when pulsing) + */ + public void render(PoseStack poseStack, SubmitNodeCollector collector, double ticks) { + collector.submitCustomGeometry(poseStack, ClientCompat.lightning(), + (pose, consumer) -> draw(consumer, pose.pose(), ticks)); + } + + private void draw(VertexConsumer consumer, Matrix4f pose, double ticks) { + float brightness = brightness(ticks); + int core = GlowGeometry.argb(rgb, coreAlpha * brightness); + int halo = GlowGeometry.argb(rgb, haloAlpha * brightness); + for (Vector3f point : points) { + GlowGeometry.sphere(consumer, pose, point.x(), point.y(), point.z(), coreRadius, slices, stacks, core); + if (haloRadius > coreRadius && haloAlpha > 0) { + GlowGeometry.sphere(consumer, pose, point.x(), point.y(), point.z(), haloRadius, slices, stacks, halo); + } + } + } + + /** Brightness factor: 1 without pulsing, otherwise a sine between {@code pulseMin} and 1. */ + private float brightness(double ticks) { + if (pulseTicks <= 0) return 1.0F; + double wave = 0.5D + 0.5D * Math.sin(2.0D * Math.PI * (ticks % pulseTicks) / pulseTicks); + return (float) (pulseMin + (1.0D - pulseMin) * wave); + } + + /** Configuration of a {@link GlowDotsRenderer}; the defaults draw steady small red dots without a halo. */ + public static final class Builder { + private final List points = new ArrayList<>(); + private int rgb = 0xFF1A1A; + private float coreRadius = 0.02F; + private float coreAlpha = 0.9F; + private float haloRadius; + private float haloAlpha; + private int slices = 8; + private int stacks = 6; + private double pulseTicks; + private float pulseMin = 1.0F; + + private Builder() { + } + + public Builder point(float x, float y, float z) { + points.add(new Vector3f(x, y, z)); + return this; + } + + public Builder points(List points) { + points.forEach(point -> this.points.add(new Vector3f(point))); + return this; + } + + /** RGB colour of the lights. */ + public Builder colour(int rgb) { + this.rgb = rgb; + return this; + } + + public Builder core(float radius, float alpha) { + this.coreRadius = radius; + this.coreAlpha = alpha; + return this; + } + + /** A larger, fainter sphere around each core (radius not above the core radius = no halo). */ + public Builder halo(float radius, float alpha) { + this.haloRadius = radius; + this.haloAlpha = alpha; + return this; + } + + /** Tessellation of the spheres (longitude segments, latitude bands). */ + public Builder detail(int slices, int stacks) { + this.slices = Math.max(3, slices); + this.stacks = Math.max(2, stacks); + return this; + } + + /** + * @param periodTicks ticks of one pulse (0 = steady light) + * @param minBrightness brightness factor at the dimmest point of a pulse, in [0, 1] + */ + public Builder pulse(double periodTicks, float minBrightness) { + this.pulseTicks = Math.max(0.0D, periodTicks); + this.pulseMin = Math.max(0.0F, Math.min(1.0F, minBrightness)); + return this; + } + + public GlowDotsRenderer build() { + return new GlowDotsRenderer(this); + } + } +} diff --git a/src/client/java/omtteam/openmodularturrets/client/render/effect/GlowPanelsRenderer.java b/src/client/java/omtteam/openmodularturrets/client/render/effect/GlowPanelsRenderer.java new file mode 100644 index 00000000..4c408ca4 --- /dev/null +++ b/src/client/java/omtteam/openmodularturrets/client/render/effect/GlowPanelsRenderer.java @@ -0,0 +1,137 @@ +package omtteam.openmodularturrets.client.render.effect; + +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import net.minecraft.client.renderer.SubmitNodeCollector; +import omtteam.omlib.client.compat.ClientCompat; +import omtteam.omlib.client.render.fx.GlowGeometry; +import org.joml.Matrix4f; +import org.joml.Vector3f; + +import java.util.ArrayList; +import java.util.List; + +/** + * Small, uniformly lit panels on surfaces, like LEDs. Instances are immutable and configured with {@link #builder()}; + * one instance can be shared by any number of renderers. + */ +public final class GlowPanelsRenderer { + /** + * A parallelogram panel on a surface: {@code anchor}, {@code anchor + edgeA}, {@code anchor + edgeA + edgeB}, + * {@code anchor + edgeB}. {@code outward} is the surface's unit + * normal; the panel is lifted off the surface along it so the two do not z-fight. Both edges must point away from + * the anchor along the surface: the lifted panel is extended back past the anchor by the same distance, so panels + * on adjacent faces sharing an anchor (like {@link #boxTopCorners}) still meet at the corner without a gap. + */ + public record Panel(Vector3f anchor, Vector3f edgeA, Vector3f edgeB, Vector3f outward) { + } + + private final List panels; + private final int color; + private final float alpha; + private final float surfaceOffset; + + private GlowPanelsRenderer(Builder builder) { + this.panels = List.copyOf(builder.panels); + this.color = builder.rgb; + this.alpha = builder.alpha; + this.surfaceOffset = builder.surfaceOffset; + } + + public static Builder builder() { + return new Builder(); + } + + /** + * Three panels on each of the four upper corners of a box centred on the y axis: one on the top face and one on + * each of the two side faces, all anchored at the corner itself (so they read as one lit corner). The panels lie + * exactly on the faces; the renderer lifts them off. + * + * @param halfSize half the side length of the box's top face + * @param topY height of the box's top face + * @param size edge length of each panel + */ + public static List boxTopCorners(float halfSize, float topY, float size) { + List panels = new ArrayList<>(); + Vector3f up = new Vector3f(0, 1, 0); + for (int sx = -1; sx <= 1; sx += 2) { + for (int sz = -1; sz <= 1; sz += 2) { + Vector3f anchor = new Vector3f(sx * halfSize, topY, sz * halfSize); + Vector3f inX = new Vector3f(-sx * size, 0, 0); + Vector3f inZ = new Vector3f(0, 0, -sz * size); + Vector3f down = new Vector3f(0, -size, 0); + panels.add(new Panel(anchor, inX, inZ, up)); + panels.add(new Panel(anchor, down, inZ, new Vector3f(sx, 0, 0))); + panels.add(new Panel(anchor, inX, down, new Vector3f(0, 0, sz))); + } + } + return panels; + } + + /* + * Submits the panels ({@link ClientCompat#lightning()}). + * + * @param poseStack the frame the panels are given in + */ + public void render(PoseStack poseStack, SubmitNodeCollector collector) { + collector.submitCustomGeometry(poseStack, ClientCompat.lightning(), + (pose, consumer) -> draw(consumer, pose.pose())); + } + + private void draw(VertexConsumer consumer, Matrix4f pose) { + int lit = GlowGeometry.argb(color, alpha); + int[] colours = {lit, lit, lit, lit}; + for (Panel panel : panels) { + GlowGeometry.doubleSidedQuad(consumer, pose, corners(panel, surfaceOffset), colours); + } + } + + /** + * Corners of the panel lifted by {@code offset}. The anchor moves back by + * {@code offset} along both edges, onto the corner where the lifted panels of adjacent faces meet. + */ + private static float[][] corners(Panel panel, float offset) { + Vector3f backA = new Vector3f(panel.edgeA()).normalize(offset); + Vector3f backB = new Vector3f(panel.edgeB()).normalize(offset); + Vector3f o = new Vector3f(panel.outward()).mul(offset).add(panel.anchor()).sub(backA).sub(backB); + Vector3f a = new Vector3f(panel.edgeA()).add(backA); + Vector3f b = new Vector3f(panel.edgeB()).add(backB); + return new float[][]{ + {o.x(), o.y(), o.z()}, + {o.x() + a.x(), o.y() + a.y(), o.z() + a.z()}, + {o.x() + a.x() + b.x(), o.y() + a.y() + b.y(), o.z() + a.z() + b.z()}, + {o.x() + b.x(), o.y() + b.y(), o.z() + b.z()}}; + } + + /** Configuration of a {@link GlowPanelsRenderer}; the defaults draw red panels. */ + public static final class Builder { + private final List panels = new ArrayList<>(); + private int rgb = 0xCC0000; + private float alpha = 0.85F; + private float surfaceOffset = 0.0001F; + + private Builder() { + } + + public Builder panels(List panels) { + this.panels.addAll(panels); + return this; + } + + /** RGB colour of the panels. */ + public Builder colour(int rgb) { + this.rgb = rgb; + return this; + } + + /** Alpha of the panels. */ + public Builder alpha(float alpha) { + this.alpha = alpha; + return this; + } + + public GlowPanelsRenderer build() { + return new GlowPanelsRenderer(this); + } + } +} diff --git a/src/main/java/omtteam/openmodularturrets/blockentity/turret/TurretBlockEntity.java b/src/main/java/omtteam/openmodularturrets/blockentity/turret/TurretBlockEntity.java index 151b3449..1cf9a1f5 100644 --- a/src/main/java/omtteam/openmodularturrets/blockentity/turret/TurretBlockEntity.java +++ b/src/main/java/omtteam/openmodularturrets/blockentity/turret/TurretBlockEntity.java @@ -74,6 +74,8 @@ public class TurretBlockEntity extends CompatBlockEntity { private float syncedPitch; private int ticksSinceSync; private boolean active; + /** Whether the base has a damage amp addon; synced to clients (the turret shows its damage amp LEDs). */ + private boolean damageAmp; /** Ammo taken by the last {@link #beginShot()}, if the turret uses ammo. */ @Nullable @@ -187,8 +189,10 @@ public static void serverTick(Level level, BlockPos pos, BlockState state, Turre } else { turret.forgetTarget(); } - if (engaging != turret.active) { + boolean amped = logic.getDamageAmpLevel() > 0; + if (engaging != turret.active || amped != turret.damageAmp) { turret.active = engaging; + turret.damageAmp = amped; turret.syncToClient(); } turret.syncAimIfChanged(); @@ -202,6 +206,11 @@ public boolean isActive() { return active; } + /** Whether the base has a damage amp addon (synced to clients). */ + public boolean hasDamageAmp() { + return damageAmp; + } + /** * 1.12.2 {@code AbstractDirectedTurret.update} / {@code TurretHead.updateChecks}: aiming turrets turn towards the * target's eyes every tick (1.12.2 turrets had no effective yaw/pitch limits; the pitch is now limited to @@ -477,6 +486,7 @@ protected void readData(CompoundTag tag, HolderLookup.Provider registries) { yaw = NbtCompat.getFloat(tag, "Yaw", 0); pitch = NbtCompat.getFloat(tag, "Pitch", 0); active = NbtCompat.getBoolean(tag, "Active", false); + damageAmp = NbtCompat.getBoolean(tag, "DamageAmp", false); if (!renderInitialised) { renderYaw = prevRenderYaw = yaw; renderPitch = prevRenderPitch = pitch; @@ -490,11 +500,12 @@ protected void writeData(CompoundTag tag, HolderLookup.Provider registries) { tag.putInt("TicksSinceShot", state.getTicksSinceShot()); } - /** Clients only need the aim and whether the turret is active. */ + /** Clients only need the aim, whether the turret is active and whether it shows the damage amp LEDs. */ @Override protected void writeClientData(CompoundTag tag, HolderLookup.Provider registries) { tag.putFloat("Yaw", yaw); tag.putFloat("Pitch", pitch); tag.putBoolean("Active", active); + tag.putBoolean("DamageAmp", damageAmp); } }