Skip to content

Commit

Permalink
-Get circles working again, starting on chemistry
Browse files Browse the repository at this point in the history
  • Loading branch information
Cassunshine committed Feb 12, 2024
1 parent 778e0ee commit 54ce8f0
Show file tree
Hide file tree
Showing 55 changed files with 1,105 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
import cassunshine.thework.TheWorkMod;
import cassunshine.thework.alchemy.circle.AlchemyCircle;
import cassunshine.thework.alchemy.circle.node.AlchemyNode;
import cassunshine.thework.alchemy.circle.node.type.AlchemyNodeType;
import cassunshine.thework.alchemy.circle.node.type.AlchemyNodeTypes;
import cassunshine.thework.alchemy.circle.ring.AlchemyRing;
import cassunshine.thework.blockentities.alchemycircle.AlchemyCircleBlockEntity;
import cassunshine.thework.blocks.TheWorkBlocks;
import cassunshine.thework.rendering.blockentities.alchemy_block.nodes.AlchemyNodeTypeRenderers;
import cassunshine.thework.rendering.util.RenderingUtilities;
import cassunshine.thework.utils.TheWorkUtils;
import com.google.common.hash.HashCode;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
Expand All @@ -24,15 +21,14 @@
import net.minecraft.item.BlockItem;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.LightType;
import org.spongepowered.asm.mixin.injection.selectors.ElementNode;

import java.util.ArrayList;
import java.util.Random;

public class AlchemyCircleBlockEntityRenderer implements BlockEntityRenderer<AlchemyCircleBlockEntity> {

public static final Random renderRandom = new Random();
public static final Random seededRenderRandom = new Random();

private static final float LINE_THICKNESS = 1 / 16.0f;
private static final float HALF_LINE_THICKNESS = LINE_THICKNESS / 2.0f;
Expand All @@ -41,6 +37,8 @@ public class AlchemyCircleBlockEntityRenderer implements BlockEntityRenderer<Alc

private ArrayList<AlchemyNode> specialRenders = new ArrayList<>();

public boolean wobble = false;

public AlchemyCircleBlockEntityRenderer(BlockEntityRendererFactory.Context context) {

}
Expand Down Expand Up @@ -71,7 +69,14 @@ public void render(AlchemyCircleBlockEntity entity, float tickDelta, MatrixStack

RenderingUtilities.setupLightOverlay(light, overlay);
RenderingUtilities.setupNormal(0, 1, 0);
RenderingUtilities.setupColor(255, 255, 255, 255);


if (!circle.isActive)
RenderingUtilities.setupColor(240, 240, 240, 255);
else
RenderingUtilities.setupColor(230, 240, 255, 255);

RenderingUtilities.setupWobble(circle.isActive ? 0.01f : 0);


try {
Expand All @@ -81,18 +86,18 @@ public void render(AlchemyCircleBlockEntity entity, float tickDelta, MatrixStack
drawFullCircle(0.5f, 8);
drawFullCircle(0.5f, 4);

renderRandom.setSeed(entity.getPos().asLong());
seededRenderRandom.setSeed(entity.getPos().asLong());

drawCirclePips(0.5f + LINE_THICKNESS, 0, MathHelper.PI, 4, 1 / 8.0f, false);
drawCirclePips(0.5f + LINE_THICKNESS, MathHelper.PI, MathHelper.TAU, 4, 1 / 8.0f, false);

for (int i = 0; i < circle.rings.size(); i++) {
var ring = circle.rings.get(i);
var nextRing = circle.isOutward ?
(i == circle.rings.size() - 1 ? null : circle.rings.get(i + 1)) :
(i == 0 ? null : circle.rings.get(i - 1));
//var nextRing = circle.isOutward ?
//(i == circle.rings.size() - 1 ? null : circle.rings.get(i + 1)) :
//(i == 0 ? null : circle.rings.get(i - 1));

drawRing(ring, nextRing);
drawRing(ring, null);
}

for (AlchemyNode node : specialRenders) {
Expand All @@ -109,65 +114,46 @@ public void render(AlchemyCircleBlockEntity entity, float tickDelta, MatrixStack


private void drawRing(AlchemyRing ring, AlchemyRing next) {

renderRandom.setSeed(Float.hashCode(ring.radius));

var clockwise = ring.isClockwise;
var nodeWidthAngle = MathHelper.lerp(0.5f / ring.circumference, 0, MathHelper.TAU);
var spinMult = clockwise ? 1 : -1;

seededRenderRandom.setSeed(Float.hashCode(ring.radius));

for (int i = 0; i < ring.nodes.length; i++) {

var indexNext = ring.getNextNodeIndex(i);

//Calculate the properties for this node.
var nodeThis = ring.getNode(i);
var nextNode = ring.getNode(indexNext);

var progressThis = nodeThis.index / (float) ring.nodes.length;
var angleThis = progressThis * MathHelper.TAU;
angleThis = TheWorkUtils.wrapRadians(angleThis);

var progressNext = nextNode.index / (float) ring.nodes.length;
var angleNext = progressNext * MathHelper.TAU;
angleNext = TheWorkUtils.wrapRadians(angleNext);

//If node is special, do the special rendering for it later.
if (nodeThis.nodeType != AlchemyNodeTypes.NONE)
specialRenders.add(nodeThis);

if (nodeThis.nodeType != AlchemyNodeTypes.NONE)
angleThis -= nodeWidthAngle * spinMult;
if (nextNode.nodeType != AlchemyNodeTypes.NONE)
angleNext += nodeWidthAngle * spinMult;


int length = MathHelper.ceil(TheWorkUtils.angleBetweenRadians(progressNext * MathHelper.TAU, progressThis * MathHelper.TAU) * ring.radius);

drawPippedCircleSegment(ring.radius, angleThis, angleNext, length, length * 3, ring.isClockwise ? LINE_THICKNESS : -LINE_THICKNESS * 3, ring.isClockwise ? 0.1f : -0.05f);
var pathThis = ring.paths[i];

//Draw the arc between this node and the next.
int length = MathHelper.ceil(pathThis.length);
drawPippedCircleSegment(ring.radius, pathThis.startAngle, pathThis.endAngle, length, length * 3, ring.isClockwise ? LINE_THICKNESS : -LINE_THICKNESS * 3, ring.isClockwise ? 0.1f : -0.05f);
}
}

private void drawNode(AlchemyNode node) {
RenderingUtilities.pushMat();

try {
var customRenderer = AlchemyNodeTypeRenderers.get(node.nodeType);

RenderingUtilities.translateMatrix(MathHelper.sin(node.getAngle()) * node.ring.radius, 0, MathHelper.cos(node.getAngle()) * node.ring.radius);
RenderingUtilities.rotateMatrix(0, node.getAngle() + MathHelper.PI, 0);

//TODO - Check perf on this
RenderingUtilities.setupRenderLayer(getLayer(node.ring.circle));
drawFullCircle(0.5f, 8);

drawFullCircle(0.5f, customRenderer == null ? 8 : customRenderer.circleSides);

//Render item
if (!node.heldStack.isEmpty()) {
if (node.heldStack.getItem() instanceof BlockItem blockItem && blockItem.getBlock() == TheWorkBlocks.ALCHEMY_JAR_BLOCK) {
RenderingUtilities.translateMatrix(-0.5f, 0, -0.5f);
RenderingUtilities.renderBlock(TheWorkBlocks.ALCHEMY_JAR_BLOCK.getDefaultState(), LightmapTextureManager.MAX_LIGHT_COORDINATE, OverlayTexture.DEFAULT_UV);
} else {
renderRandom.setSeed(node.getPosition().hashCode());
seededRenderRandom.setSeed(node.getPosition().hashCode());
float time = (float) TheWorkClient.getTime();
time += renderRandom.nextFloat(MathHelper.TAU);
time += seededRenderRandom.nextFloat(MathHelper.TAU);

RenderingUtilities.translateMatrix(0, MathHelper.sin((time * 2) % MathHelper.TAU) * 0.1f, 0);
RenderingUtilities.rotateMatrix(0, time * 2.0f, 0);
Expand All @@ -177,7 +163,6 @@ private void drawNode(AlchemyNode node) {
}

//Run custom renderer
var customRenderer = AlchemyNodeTypeRenderers.get(node.nodeType);
if (customRenderer != null)
customRenderer.render(node);

Expand Down Expand Up @@ -236,7 +221,7 @@ private void drawCirclePips(float radius, float startAngle, float endAngle, int
float progressThis = (offsetPips ? i + 0.5f : i) / (float) pips;
float angleThis = TheWorkUtils.lerpRadians(progressThis, startAngle, endAngle);

drawPip(radius, angleThis, MathHelper.lerp(renderRandom.nextFloat(), -0.03f, 0.03f) + pipHeight);
drawPip(radius, angleThis, MathHelper.lerp(seededRenderRandom.nextFloat(), -0.03f, 0.03f) + pipHeight);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public static <T extends AlchemyNodeType> AlchemyNodeTypeRenderer get(AlchemyNod

static {
register(AlchemyNodeTypes.DECONSTRUCT, new RuneAlchemyNodeTypeRenderer().withSides(4));
//register(AlchemyNodeTypes.CONSTRUCT, new RuneAlchemyNodeTypeRenderer().withSides(6));
register(AlchemyNodeTypes.CONSTRUCT, new RuneAlchemyNodeTypeRenderer().withSides(6));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package cassunshine.thework.rendering.particles;

import cassunshine.thework.alchemy.circle.path.AlchemyPath;
import cassunshine.thework.alchemy.circle.ring.AlchemyRing;
import cassunshine.thework.particles.TheWorkParticles;
import cassunshine.thework.utils.TheWorkUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.particle.*;
import net.minecraft.client.world.ClientWorld;
Expand All @@ -10,35 +14,34 @@

public class RadialParticle extends SpriteBillboardParticle {

public static int color = Integer.MAX_VALUE;
private float radius;
private float startAngle;
private float endAngle;

private double radius;
private double startAngle;
private double endAngle;
private float progress = 0;

private Vec3d startPos;

boolean direction = true;
boolean direction;

protected RadialParticle(ClientWorld clientWorld, double x, double y, double z, double radius, double startAngle, double endAngle) {
super(clientWorld, x + MathHelper.sin((float) startAngle) * radius, y, z + MathHelper.cos((float) startAngle) * radius);

this.radius = radius;
this.startAngle = startAngle;
this.endAngle = endAngle;
this.radius = (float) radius - (1 / 32.0f);
this.startAngle = (float) startAngle;
this.endAngle = (float) endAngle;

startPos = new Vec3d(x, y, z);
scale = 0.05f;

double circumference = MathHelper.TAU * radius;

prevPosX = x + MathHelper.sin((float) (startAngle - ((0.1 * MinecraftClient.getInstance().getTickDelta()) / circumference))) * radius;
var angle = getAngle(progress);
prevPosX = x + MathHelper.sin(angle) * this.radius;
prevPosY = y;
prevPosZ = z + MathHelper.cos((float) (startAngle - ((0.1 * MinecraftClient.getInstance().getTickDelta()) / circumference))) * radius;
prevPosZ = z + MathHelper.cos(angle) * this.radius;

this.maxAge = Integer.MAX_VALUE;

this.setColor(ColorHelper.Argb.getRed(color) / 255.0f, ColorHelper.Argb.getGreen(color) / 255.0f, ColorHelper.Argb.getBlue(color) / 255.0f);
this.setColor(ColorHelper.Argb.getRed(TheWorkParticles.radialColor) / 255.0f, ColorHelper.Argb.getGreen(TheWorkParticles.radialColor) / 255.0f, ColorHelper.Argb.getBlue(TheWorkParticles.radialColor) / 255.0f);

direction = startAngle < endAngle;
}
Expand All @@ -48,24 +51,27 @@ public ParticleTextureSheet getType() {
return ParticleTextureSheet.PARTICLE_SHEET_OPAQUE;
}

private float getAngle(float progress) {
float pathLength = (radius * MathHelper.TAU) * (TheWorkUtils.angleBetweenRadians(startAngle, endAngle) / MathHelper.TAU);
return TheWorkUtils.lerpRadians(progress / pathLength, startAngle, endAngle);
}

@Override
public void tick() {
super.tick();

if (direction && startAngle >= endAngle)
this.markDead();
if(!direction && startAngle <= endAngle)
this.markDead();
float pathLength = (radius * MathHelper.TAU) * (TheWorkUtils.angleBetweenRadians(startAngle, endAngle) / MathHelper.TAU);

if (progress > pathLength)
markDead();

x = startPos.x + MathHelper.sin((float) startAngle) * radius;
z = startPos.z + MathHelper.cos((float) startAngle) * radius;
//Move along path...
progress += AlchemyPath.TRAVEL_SPEED;

double circumference = MathHelper.TAU * radius;
float currentAngle = TheWorkUtils.lerpRadians(progress / pathLength, startAngle, endAngle);

if(direction)
startAngle += MathHelper.TAU * (0.1f / circumference);
else
startAngle -= MathHelper.TAU * (0.1f / circumference);
x = startPos.x + MathHelper.sin(currentAngle) * radius;
z = startPos.z + MathHelper.cos(currentAngle) * radius;
}

public static class Factory implements ParticleFactory<DefaultParticleType> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
import net.minecraft.util.math.MatrixUtil;
import net.minecraft.world.World;
import org.joml.Quaternionf;
import org.joml.Random;

public class RenderingUtilities {

private static final Random renderRandom = new Random();

private static MatrixStack stack;
private static VertexConsumerProvider consumers;
private static VertexConsumer consumer;

private static int r, g, b, a;
private static float normalX, normalY, normalZ;

private static float wobbleAmount = 0;

private static int light;
private static int overlay;

Expand Down Expand Up @@ -56,8 +62,12 @@ public static void setupNormal(float x, float y, float z) {
normalZ = z;
}

public static void setupWobble(float wobble) {
wobbleAmount = wobble;
}

public static void saneVertex(float x, float y, float z, float u, float v) {
consumer.vertex(stack.peek().getPositionMatrix(), x, y, z).color(r, g, b, a).texture(u, v).overlay(overlay).light(light).normal(stack.peek().getNormalMatrix(), normalX, normalY, normalZ).next();
consumer.vertex(stack.peek().getPositionMatrix(), x + getWobble(), y, z + getWobble()).color(r, g, b, a).texture(u, v).overlay(overlay).light(light).normal(stack.peek().getNormalMatrix(), normalX, normalY, normalZ).next();
}

public static void renderItem(ItemStack stack, World world, int light, int overlay) {
Expand Down Expand Up @@ -90,4 +100,10 @@ public static void rotateMatrix(float x, float y, float z) {
stack.multiply(new Quaternionf().rotationZ(z));
}

public static float getWobble() {
if (wobbleAmount <= 0)
return 0;
return (renderRandom.nextFloat() - 0.5f) * wobbleAmount;
}

}
Loading

0 comments on commit 54ce8f0

Please sign in to comment.