Skip to content

Commit

Permalink
Improve assembly line laser render (#2495)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2811c8e)
  • Loading branch information
M-W-K authored and ALongStringOfNumbers committed Jun 22, 2024
1 parent 0e8f6d2 commit 4b76678
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
26 changes: 22 additions & 4 deletions src/main/java/gregtech/client/particle/GTLaserBeamParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.client.renderer.texture.SimpleTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;

import codechicken.lib.vec.Vector3;
Expand All @@ -34,11 +35,20 @@ public class GTLaserBeamParticle extends GTParticle {
private float emit;
private boolean doubleVertical;

private int activeTime;
private final int fadeInTime;

public GTLaserBeamParticle(@Nullable MetaTileEntity mte, @NotNull Vector3 startPos, @NotNull Vector3 endPos) {
this(mte, startPos, endPos, 0);
}

public GTLaserBeamParticle(@Nullable MetaTileEntity mte, @NotNull Vector3 startPos, @NotNull Vector3 endPos,
int fadeInTime) {
super(startPos.x, startPos.y, startPos.z);
this.mte = mte;
this.direction = endPos.copy().subtract(startPos);
this.setRenderRange(64);
this.fadeInTime = fadeInTime;
}

@Override
Expand Down Expand Up @@ -99,6 +109,15 @@ public float getAlpha() {
return this.alpha;
}

public float getAlpha(float partialTicks) {
if (this.fadeInTime > this.activeTime) {
return (float) (this.alpha * MathHelper.clampedLerp(
(double) this.activeTime / this.fadeInTime,
(double) (this.activeTime + 1) / this.fadeInTime, partialTicks));
}
return this.alpha;
}

/**
* Set emit speed.
*
Expand Down Expand Up @@ -131,9 +150,8 @@ public void onUpdate() {
if (mte == null || mte.isValid() &&
mte.getWorld().isBlockLoaded(mte.getPos(), false) &&
mte.getWorld().getTileEntity(mte.getPos()) == mte.getHolder()) {
return;
}
setExpired();
this.activeTime++;
} else setExpired();
}

@Override
Expand Down Expand Up @@ -169,7 +187,7 @@ public void renderParticle(@NotNull BufferBuilder buffer, @NotNull EffectRenderC
bodyTexture.getGlTextureId(),
headTexture == null ? -1 :
headTexture.getGlTextureId(),
direction, cameraDirection, beamHeight, headWidth, alpha, offset);
direction, cameraDirection, beamHeight, headWidth, this.getAlpha(context.partialTicks()), offset);
GlStateManager.translate(context.cameraX() - posX, context.cameraY() - posY, context.cameraZ() - posZ);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class MetaTileEntityAssemblyLine extends RecipeMapMultiblockController {
@SideOnly(Side.CLIENT)
private GTLaserBeamParticle[][] beamParticles;
private int beamCount;
private int beamTime;

public MetaTileEntityAssemblyLine(ResourceLocation metaTileEntityId) {
super(metaTileEntityId, RecipeMaps.ASSEMBLY_LINE_RECIPES);
Expand Down Expand Up @@ -189,14 +190,22 @@ public void update() {
// beams and the maximum progress in the recipe.
int beamTime = Math.max(1, maxProgress / maxBeams);

int currentBeamCount = Math.min(maxBeams, getRecipeMapWorkable().getProgress() / beamTime);
int beamCount = Math.min(maxBeams, getRecipeMapWorkable().getProgress() / beamTime + 1);

if (currentBeamCount != beamCount) {
beamCount = currentBeamCount;
if (beamCount != this.beamCount) {
if (beamCount < this.beamCount) {
// if beam count decreases, the last beam in the queue needs to be removed for the sake of fade
// time.
this.beamCount = Math.max(0, beamCount - 1);
writeCustomData(GregtechDataCodes.UPDATE_PARTICLE, this::writeParticles);
}
this.beamTime = beamTime;
this.beamCount = beamCount;
writeCustomData(GregtechDataCodes.UPDATE_PARTICLE, this::writeParticles);
}
} else if (beamCount != 0) {
beamCount = 0;
this.beamTime = 0;
this.beamCount = 0;
writeCustomData(GregtechDataCodes.UPDATE_PARTICLE, this::writeParticles);
}
}
Expand Down Expand Up @@ -239,11 +248,13 @@ public void onRemoval() {

private void writeParticles(@NotNull PacketBuffer buf) {
buf.writeVarInt(beamCount);
buf.writeVarInt(beamTime);
}

@SideOnly(Side.CLIENT)
private void readParticles(@NotNull PacketBuffer buf) {
beamCount = buf.readVarInt();
beamTime = buf.readVarInt();
if (beamParticles == null) {
beamParticles = new GTLaserBeamParticle[17][2];
}
Expand Down Expand Up @@ -306,7 +317,7 @@ private void readParticles(@NotNull PacketBuffer buf) {
@NotNull
@SideOnly(Side.CLIENT)
private GTLaserBeamParticle createALParticles(Vector3 startPos, Vector3 endPos) {
return new GTLaserBeamParticle(this, startPos, endPos)
return new GTLaserBeamParticle(this, startPos, endPos, beamTime)
.setBody(LASER_LOCATION)
.setBeamHeight(0.125f)
// Try commenting or adjusting on the next four lines to see what happens
Expand Down

0 comments on commit 4b76678

Please sign in to comment.