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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* The turret's data is copied into a {@link State} when the frame is extracted, and drawn from it in {@link #submit}.
*/
Expand All @@ -39,6 +40,7 @@ public static final class State extends BlockEntityRenderState {
Face face = Face.UP;
@Nullable
RenderType renderType;
boolean damageAmp;
}

private final Map<TurretType, TurretModel> models;
Expand All @@ -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
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Vector3f> 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<Vector3f> 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<Vector3f> 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<Vector3f> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Panel> 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<Panel> boxTopCorners(float halfSize, float topY, float size) {
List<Panel> 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<Panel> panels = new ArrayList<>();
private int rgb = 0xCC0000;
private float alpha = 0.85F;
private float surfaceOffset = 0.0001F;

private Builder() {
}

public Builder panels(List<Panel> 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);
}
}
}
Loading