Skip to content

Commit

Permalink
Merge pull request #555 from Talia-12/1.20.1
Browse files Browse the repository at this point in the history
Bring texture-based pattern rendering to 1.20
  • Loading branch information
object-Object authored Dec 24, 2023
2 parents 704765d + c5643a1 commit 6117323
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.petrak.hexcasting.client.entity;

import at.petrak.hexcasting.client.render.PatternTextureManager;
import at.petrak.hexcasting.client.render.RenderLib;
import at.petrak.hexcasting.common.entities.EntityWallScroll;
import com.mojang.blaze3d.systems.RenderSystem;
Expand Down Expand Up @@ -97,10 +98,14 @@ public void render(EntityWallScroll wallScroll, float yaw, float partialTicks, P
vertex(mat, norm, light, verts, dx, dy, dz, 1, 1 - margin, 0, 1, 0);

ps.popPose();

if (PatternTextureManager.useTextures && wallScroll.points != null)
PatternTextureManager.renderPatternForScroll(wallScroll.points.pointsKey, ps, bufSource, light, wallScroll.points.zappyPoints, wallScroll.blockSize, wallScroll.getShowsStrokeOrder());
}

if (wallScroll.zappyPoints != null) {
var points = wallScroll.zappyPoints;
//TODO: remove old rendering if not needed anymore for comparison
if (!PatternTextureManager.useTextures && wallScroll.points != null) {
var points = wallScroll.points.zappyPoints;
ps.pushPose();

ps.mulPose(Axis.YP.rotationDegrees(180f));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package at.petrak.hexcasting.client.render;

import net.minecraft.world.phys.Vec2;

import java.util.List;

public class HexPatternPoints {
public List<Vec2> zappyPoints = null;
public String pointsKey = null; //TODO: if a string key isnt performant enough override hashcode for points

public HexPatternPoints(List<Vec2> zappyPoints) {
this.zappyPoints = zappyPoints;
pointsKey = PatternTextureManager.getPointsKey(zappyPoints);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.petrak.hexcasting.client.render.be;

import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.client.render.PatternTextureManager;
import at.petrak.hexcasting.client.render.RenderLib;
import at.petrak.hexcasting.common.blocks.akashic.BlockAkashicBookshelf;
import at.petrak.hexcasting.common.blocks.akashic.BlockEntityAkashicBookshelf;
Expand Down Expand Up @@ -31,6 +32,13 @@ public void render(BlockEntityAkashicBookshelf tile, float pPartialTick, PoseSta

var bs = tile.getBlockState();

if(PatternTextureManager.useTextures) {
PatternTextureManager.renderPatternForAkashicBookshelf(tile, pattern, ps, buffer, light, bs);
return;
}

//TODO: remove old rendering if not needed anymore for comparison

var oldShader = RenderSystem.getShader();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
RenderSystem.enableDepthTest();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.petrak.hexcasting.client.render.be;

import at.petrak.hexcasting.client.render.PatternTextureManager;
import at.petrak.hexcasting.client.render.RenderLib;
import at.petrak.hexcasting.common.blocks.circles.BlockEntitySlate;
import at.petrak.hexcasting.common.blocks.circles.BlockSlate;
Expand All @@ -13,8 +14,8 @@
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.state.properties.AttachFace;
import net.minecraft.world.phys.Vec2;
import org.joml.AxisAngle4f;
import org.joml.Quaternionf;

import java.util.ArrayList;

public class BlockEntitySlateRenderer implements BlockEntityRenderer<BlockEntitySlate> {
public BlockEntitySlateRenderer(BlockEntityRendererProvider.Context ctx) {
Expand All @@ -24,12 +25,18 @@ public BlockEntitySlateRenderer(BlockEntityRendererProvider.Context ctx) {
@Override
public void render(BlockEntitySlate tile, float pPartialTick, PoseStack ps,
MultiBufferSource buffer, int light, int overlay) {
if (tile.pattern == null) {
if (tile.pattern == null)
return;
}

var bs = tile.getBlockState();

if(PatternTextureManager.useTextures && !bs.getValue(BlockSlate.ENERGIZED)) {
PatternTextureManager.renderPatternForSlate(tile, tile.pattern, ps, buffer, light, bs);
return;
}

//TODO: remove old rendering if not needed anymore for comparison

var oldShader = RenderSystem.getShader();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
RenderSystem.enableDepthTest();
Expand All @@ -49,51 +56,68 @@ public void render(BlockEntitySlate tile, float pPartialTick, PoseStack ps,
ps.mulPose(Axis.ZP.rotation(neg * Mth.HALF_PI * quarters));
}

// Resolution is the number of sub-voxels in the block for rendering purposes, 16 is the default
// padding is the space to leave on the edges free of pattern
var resolution = 16;
var padding = resolution * PatternTextureManager.paddingByBlockSize / PatternTextureManager.resolutionByBlockSize;

// and now Z is out?
ps.translate(0, 0, -0.5);
ps.scale(1 / 16f, 1 / 16f, 1 / 16f);
ps.scale(1f / resolution, 1f / resolution, 1f / resolution);
ps.translate(0, 0, 1.01);

// yoink code from the pattern greeble
// Do two passes: one with a random size to find a good COM and one with the real calculation
var com1 = tile.pattern.getCenter(1);
var isLit = bs.getValue(BlockSlate.ENERGIZED);
var variance = isLit ? 2.5f : 0.5f;
var speed = isLit ? 0.1f : 0f;

var lines1 = tile.pattern.toLines(1, Vec2.ZERO);
var stupidHash = tile.getBlockPos().hashCode();
var zappyPattern = RenderLib.makeZappy(lines1, RenderLib.findDupIndices(tile.pattern.positions()),
10, variance, speed, 0.2f, 0f, 1f, stupidHash);

var maxDx = -1f;
var maxDy = -1f;
for (var dot : lines1) {
var dx = Mth.abs(dot.x - com1.x);
if (dx > maxDx) {
maxDx = dx;
}
var dy = Mth.abs(dot.y - com1.y);
if (dy > maxDy) {
maxDy = dy;
}
// always do space calculations with the static version of the pattern
// so that it doesn't jump around resizing itself.
var zappyPatternSpace = RenderLib.makeZappy(lines1, RenderLib.findDupIndices(tile.pattern.positions()),
10, 0.5f, 0f, 0.2f, 0f, 1f, stupidHash);

double minX = Double.MAX_VALUE, maxX = Double.MIN_VALUE, minY = Double.MAX_VALUE, maxY = Double.MIN_VALUE;
for (Vec2 point : zappyPatternSpace)
{
minX = Math.min(minX, point.x);
maxX = Math.max(maxX, point.x);
minY = Math.min(minY, point.y);
maxY = Math.max(maxY, point.y);
}
var scale = Math.min(3.8f, Math.min(16 / 2.5f / maxDx, 16 / 2.5f / maxDy));

var com2 = tile.pattern.getCenter(scale);
var lines2 = tile.pattern.toLines(scale, com2.negated());
// For some reason it is mirrored left to right and i can't seem to posestack-fu it into shape
for (int i = 0; i < lines2.size(); i++) {
var v = lines2.get(i);
lines2.set(i, new Vec2(-v.x, v.y));
double rangeX = maxX - minX;
double rangeY = maxY - minY;

double scale = Math.min((resolution - 2 * padding) / rangeX, (resolution - 2 * padding) / rangeY);

double offsetX = ((- 2 * padding) - rangeX * scale) / 2;
double offsetY = ((- 2 * padding) - rangeY * scale) / 2;

var zappyRenderSpace = new ArrayList<Vec2>();

for (Vec2 point : zappyPattern) {
zappyRenderSpace.add(new Vec2(
(float) (((point.x - minX) * scale + offsetX) + padding),
(float) (((point.y - minY) * scale + offsetY) + padding)
));
}

var isLit = bs.getValue(BlockSlate.ENERGIZED);
var variance = isLit ? 2.5f : 0.5f;
var speed = isLit ? 0.1f : 0f;
var stupidHash = tile.getBlockPos().hashCode();
var zappy = RenderLib.makeZappy(lines2, RenderLib.findDupIndices(tile.pattern.positions()),
10, variance, speed, 0.2f, 0f, 1f, stupidHash);
// For some reason it is mirrored left to right and i can't seem to posestack-fu it into shape
for (int i = 0; i < zappyRenderSpace.size(); i++) {
var v = zappyRenderSpace.get(i);
zappyRenderSpace.set(i, new Vec2(-v.x, v.y));
}

int outer = isLit ? 0xff_64c8ff : 0xff_d2c8c8;
int inner = isLit ? RenderLib.screenCol(outer) : 0xc8_322b33;
RenderLib.drawLineSeq(ps.last().pose(), zappy, 1f, 0f, outer, outer);
RenderLib.drawLineSeq(ps.last().pose(), zappy, 0.4f, 0.01f, inner, inner);
RenderLib.drawLineSeq(ps.last().pose(), zappyRenderSpace, 1f, 0f, outer, outer);
RenderLib.drawLineSeq(ps.last().pose(), zappyRenderSpace, 0.4f, 0.01f, inner, inner);

ps.popPose();
RenderSystem.setShader(() -> oldShader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import at.petrak.hexcasting.api.casting.iota.Iota;
import at.petrak.hexcasting.api.casting.iota.IotaType;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.client.render.HexPatternPoints;
import at.petrak.hexcasting.common.lib.HexBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -22,6 +23,8 @@ public class BlockEntityAkashicBookshelf extends HexBlockEntity {
// For both these cases we save just the tag of the iota.
private CompoundTag iotaTag = null;

public HexPatternPoints points;

public BlockEntityAkashicBookshelf(BlockPos pWorldPosition, BlockState pBlockState) {
super(HexBlockEntities.AKASHIC_BOOKSHELF_TILE, pWorldPosition, pBlockState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import at.petrak.hexcasting.api.block.HexBlockEntity;
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.client.render.HexPatternPoints;
import at.petrak.hexcasting.common.lib.HexBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -14,6 +15,7 @@ public class BlockEntitySlate extends HexBlockEntity {

@Nullable
public HexPattern pattern;
public HexPatternPoints points;

public BlockEntitySlate(BlockPos pos, BlockState state) {
super(HexBlockEntities.SLATE_TILE, pos, state);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package at.petrak.hexcasting.common.command;

import at.petrak.hexcasting.client.render.PatternTextureManager;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;

public class PatternTexturesCommand
{
public static void add(LiteralArgumentBuilder<CommandSourceStack> cmd) {
cmd.then(Commands.literal("textureToggle")
.requires(dp -> dp.hasPermission(Commands.LEVEL_ADMINS))
.executes(ctx -> {
PatternTextureManager.useTextures = !PatternTextureManager.useTextures;
return 1;
}));
cmd.then(Commands.literal("textureRepaint")
.requires(dp -> dp.hasPermission(Commands.LEVEL_ADMINS))
.executes(ctx -> {
PatternTextureManager.repaint();
return 1;
}));
cmd.then(Commands.literal("textureSetResolutionScaler")
.requires(dp -> dp.hasPermission(Commands.LEVEL_ADMINS))
.then(Commands.argument("integer", IntegerArgumentType.integer()).executes(ctx -> {
PatternTextureManager.setResolutionScaler(IntegerArgumentType.getInteger(ctx, "integer"));
PatternTextureManager.repaint();
return 1;
})));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import at.petrak.hexcasting.api.casting.math.HexPattern;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.api.utils.NBTHelper;
import at.petrak.hexcasting.client.render.HexPatternPoints;
import at.petrak.hexcasting.client.render.RenderLib;
import at.petrak.hexcasting.common.items.storage.ItemScroll;
import at.petrak.hexcasting.common.lib.HexItems;
Expand Down Expand Up @@ -43,11 +44,13 @@ public class EntityWallScroll extends HangingEntity {
EntityDataSerializers.BOOLEAN);

public ItemStack scroll;
@Nullable
public HexPattern pattern;
public boolean isAncient;
public int blockSize;
// Client-side only!
public List<Vec2> zappyPoints;
@Nullable
public HexPatternPoints points;

public EntityWallScroll(EntityType<? extends EntityWallScroll> type, Level world) {
super(type, world);
Expand Down Expand Up @@ -75,14 +78,15 @@ public void recalculateDisplay() {
var dots = pair.getSecond();
var readOffset = this.getShowsStrokeOrder() ? RenderLib.DEFAULT_READABILITY_OFFSET : 0f;
var lastProp = this.getShowsStrokeOrder() ? RenderLib.DEFAULT_LAST_SEGMENT_LEN_PROP : 1f;
this.zappyPoints = RenderLib.makeZappy(dots, RenderLib.findDupIndices(pattern.positions()), 10, 0.4f,
var zappyPoints = RenderLib.makeZappy(dots, RenderLib.findDupIndices(pattern.positions()), 10, 0.4f,
0f, 0f, readOffset, lastProp, this.getId());
this.points = new HexPatternPoints(zappyPoints);
}

this.isAncient = NBTHelper.hasString(scroll, ItemScroll.TAG_OP_ID);
} else {
this.pattern = null;
this.zappyPoints = null;
this.points = null;
this.isAncient = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import at.petrak.hexcasting.common.command.BrainsweepCommand;
import at.petrak.hexcasting.common.command.ListPerWorldPatternsCommand;
import at.petrak.hexcasting.common.command.PatternTexturesCommand;
import at.petrak.hexcasting.common.command.RecalcPatternsCommand;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -14,6 +15,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
BrainsweepCommand.add(mainCmd);
ListPerWorldPatternsCommand.add(mainCmd);
RecalcPatternsCommand.add(mainCmd);
PatternTexturesCommand.add(mainCmd);

dispatcher.register(mainCmd);
}
Expand Down

0 comments on commit 6117323

Please sign in to comment.