Skip to content

Commit

Permalink
Added pause button and show cube button, renamed vertex data catcher
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed Feb 2, 2024
1 parent c5b8970 commit 263bb86
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 92 deletions.
34 changes: 18 additions & 16 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.xtracr.realcamera.config.ModConfig;
import com.xtracr.realcamera.mixin.PlayerEntityRendererAccessor;
import com.xtracr.realcamera.util.MathUtil;
import com.xtracr.realcamera.util.VertexDataCatcher;
import com.xtracr.realcamera.util.VertexDataCatcherProvider;
import com.xtracr.realcamera.util.VertexRecorder;
import com.xtracr.realcamera.util.VertexRecorderProvider;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
Expand All @@ -30,9 +30,8 @@

public class RealCameraCore {
private static final ModConfig config = ConfigFile.modConfig;
private static VertexDataCatcherProvider catcherProvider = new VertexDataCatcherProvider();
private static VertexRecorderProvider recorderProvider = new VertexRecorderProvider();
private static String status = "Successful";

private static boolean renderingPlayer = false;
private static boolean active = false;
private static float pitch, yaw, roll;
Expand Down Expand Up @@ -69,6 +68,10 @@ public static Vec3d getPos() {
return pos;
}

public static Vec3d getModelOffset() {
return modelOffset;
}

public static void setModelOffset(Vec3d vec3d) {
modelOffset = vec3d;
}
Expand All @@ -82,14 +85,13 @@ public static boolean isActive() {
return active;
}

public static void renderPlayer(Vec3d cameraPos, MatrixStack matrices, VertexConsumerProvider vertexConsumers) {
public static void renderPlayer(Vec3d offset, MatrixStack matrices, VertexConsumerProvider vertexConsumers) {
matrices.push();
cameraPos = cameraPos.subtract(modelOffset);
matrices.peek().getPositionMatrix().transpose().invertAffine()
.translate((float) -cameraPos.getX(), (float) -cameraPos.getY(), (float) -cameraPos.getZ());
.translate((float) -offset.getX(), (float) -offset.getY(), (float) -offset.getZ());
matrices.peek().getNormalMatrix().transpose().invert();
// TODO
catcherProvider.drawByAnother(matrices, vertexConsumers, null, null);
recorderProvider.drawByAnother(matrices, vertexConsumers, null, null);
matrices.pop();
}

Expand All @@ -100,9 +102,9 @@ public static void computeCamera(MinecraftClient client, float tickDelta) {

// GameRenderer.renderWorld
MatrixStack matrixStack = new MatrixStack();
catcherProvider = new VertexDataCatcherProvider();
virtualRender(client, tickDelta, matrixStack, catcherProvider);
VertexDataCatcher catcher = catcherProvider.getUnion(new VertexDataCatcher());
recorderProvider = new VertexRecorderProvider();
virtualRender(client, tickDelta, matrixStack, recorderProvider);
VertexRecorder unionRecorder = recorderProvider.getUnion(new VertexRecorder());

// ModelPart$Cuboid.renderCuboid
Vector4f offset = matrixStack.peek().getPositionMatrix().transform(new Vector4f((float) (config.getBindingZ() * config.getScale()),
Expand All @@ -111,12 +113,12 @@ public static void computeCamera(MinecraftClient client, float tickDelta) {
pos = new Vec3d(offset.x(), offset.y(), offset.z());
Matrix3f normal = matrixStack.peek().getNormalMatrix().scale(1.0F, -1.0F, -1.0F);
if (config.binding.experimental) try {
if (catcher.vertexCount() <= 0) throw new NullPointerException("Vertices not found");
if (unionRecorder.vertexCount() <= 0) throw new NullPointerException("Vertices not found");
List<Integer> indexList = config.binding.indexListMap.get(config.binding.nameOfList);
Vec3d front = catcher.getNormal(indexList.get(0));
Vec3d up = catcher.getNormal(indexList.get(1));
Vec3d front = unionRecorder.getNormal(indexList.get(0));
Vec3d up = unionRecorder.getNormal(indexList.get(1));
Vec3d center = Vec3d.ZERO;
for (int i : indexList.subList(2, indexList.size())) center = center.add(catcher.getPos(i));
for (int i : indexList.subList(2, indexList.size())) center = center.add(unionRecorder.getPos(i));
if (!MathUtil.isFinite(front) || !MathUtil.isFinite(up) || !MathUtil.isFinite(center)) throw new ArithmeticException();
normal.set(up.crossProduct(front).toVector3f(), up.toVector3f(), front.toVector3f());
Vector3f vec3f = normal.transform(new Vector3f((float) (config.getBindingZ() * config.getScale()),
Expand All @@ -135,7 +137,7 @@ public static void computeCamera(MinecraftClient client, float tickDelta) {
roll = config.isRollingBound() ? (float) eulerAngle.getZ() : config.getBindingRoll();
}

private static void virtualRender(MinecraftClient client, float tickDelta, MatrixStack matrixStack, VertexDataCatcherProvider provider) {
private static void virtualRender(MinecraftClient client, float tickDelta, MatrixStack matrixStack, VertexRecorderProvider provider) {
ClientPlayerEntity player = client.player;
// WorldRenderer.render
if (player.age == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private void clamp() {

public static class Binding {
public static final Map<String, List<Integer>> defaultIndexListMap = Map.of(
"minecraft_head", Arrays.asList(12, 0, -9, 12, 13, 14, 15));
"minecraft_head", Arrays.asList(12, 0, 12, 12, 13, 14, 15));
public VanillaModelPart vanillaModelPart = VanillaModelPart.head;
public boolean experimental = false;
public boolean adjustOffset = true;
Expand Down
34 changes: 17 additions & 17 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelAnalyser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xtracr.realcamera.gui;

import com.xtracr.realcamera.util.VertexDataCatcher;
import com.xtracr.realcamera.util.VertexDataCatcherProvider;
import com.xtracr.realcamera.util.VertexRecorder;
import com.xtracr.realcamera.util.VertexRecorderProvider;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
Expand All @@ -14,9 +14,9 @@
import java.util.Comparator;
import java.util.List;

public class ModelAnalyser extends VertexDataCatcherProvider {
public class ModelAnalyser extends VertexRecorderProvider {
private final float precision = 0.00001f;
private final VertexDataCatcher catcher = new VertexDataCatcher();
private final VertexRecorder unionRecorder = new VertexRecorder();
private final List<List<Integer>> quads = new ArrayList<>();

public List<Integer> getQuad(int index) {
Expand All @@ -30,11 +30,11 @@ public List<Integer> getQuad(int index) {
}

public void analyse() {
getUnion(catcher);
int size = catcher.vertexCount();
getUnion(unionRecorder);
int size = unionRecorder.vertexCount();
quads.add(new ArrayList<>(List.of(0)));
for (int i = 1; i < size; i++) {
double dotProduct = catcher.getNormal(i - 1).dotProduct(catcher.getNormal(i));
double dotProduct = unionRecorder.getNormal(i - 1).dotProduct(unionRecorder.getNormal(i));
if (dotProduct >= 1 - precision) quads.get(quads.size() - 1).add(i);
else quads.add(new ArrayList<>(List.of(i)));
}
Expand All @@ -44,9 +44,9 @@ public int getFocusedIndex(int mouseX, int mouseY, int layers) {
List<Pair<Integer, Float>> sortByDepth = new ArrayList<>();
for (List<Integer> vertices : quads) {
Polygon quad = new Polygon();
vertices.forEach(i -> quad.addPoint((int) catcher.getPos(i).getX(), (int) catcher.getPos(i).getY()));
Vector3f normal = catcher.getNormal(vertices.get(0)).toVector3f();
Vector3f point = catcher.getPos(vertices.get(0)).toVector3f();
vertices.forEach(i -> quad.addPoint((int) unionRecorder.getPos(i).getX(), (int) unionRecorder.getPos(i).getY()));
Vector3f normal = unionRecorder.getNormal(vertices.get(0)).toVector3f();
Vector3f point = unionRecorder.getPos(vertices.get(0)).toVector3f();
float deltaZ = 0;
if (normal.z() != 0) deltaZ = (normal.x() * (mouseX - point.x()) + normal.y() * (mouseY - point.y())) / normal.z();
if (quad.contains(mouseX, mouseY)) sortByDepth.add(new Pair<>(vertices.get(0), point.z() + deltaZ));
Expand All @@ -57,12 +57,12 @@ public int getFocusedIndex(int mouseX, int mouseY, int layers) {
}

public void drawQuad(DrawContext context, int vertex, int argb) {
if (vertex >= catcher.vertexCount()) return;
if (vertex >= unionRecorder.vertexCount()) return;
drawQuad(context, getQuad(vertex), argb, 1000);
}

public void drawPolyhedron(DrawContext context, int vertex, int argb) {
if (vertex >= catcher.vertexCount()) return;
if (vertex >= unionRecorder.vertexCount()) return;
List<Integer> highlight = getQuad(vertex);
List<List<Integer>> polyhedron = new ArrayList<>(List.of(highlight));
boolean added;
Expand Down Expand Up @@ -92,12 +92,12 @@ public void drawPolyhedron(DrawContext context, int vertex, int argb) {
}

public void drawNormal(DrawContext context, int vertex, int length, int argb) {
if (vertex >= catcher.vertexCount()) return;
if (vertex >= unionRecorder.vertexCount()) return;
VertexConsumer vertexConsumer = context.getVertexConsumers().getBuffer(RenderLayer.getLineStrip());
Vector3f normal = catcher.getNormal(vertex).toVector3f();
Vector3f normal = unionRecorder.getNormal(vertex).toVector3f();
Vector3f start = new Vector3f();
List<Integer> quad = getQuad(vertex);
quad.forEach(i -> start.add(catcher.getPos(i).toVector3f()));
quad.forEach(i -> start.add(unionRecorder.getPos(i).toVector3f()));
start.mul(1 / (float) quad.size());
Vector3f end = new Vector3f(normal).mul(length).add(start);
vertexConsumer.vertex(start.x(), start.y(), start.z() + 1200f).color(argb).normal(normal.x(), normal.y(), normal.z()).next();
Expand All @@ -108,7 +108,7 @@ public void drawNormal(DrawContext context, int vertex, int length, int argb) {
private boolean intersects(List<Integer> quad, List<List<Integer>> quads) {
boolean ret = false;
for (List<Integer> p : quads) for (int v1 : quad) for (int v2 : p) {
if (catcher.getPos(v1).squaredDistanceTo(catcher.getPos(v2)) < precision) {
if (unionRecorder.getPos(v1).squaredDistanceTo(unionRecorder.getPos(v2)) < precision) {
ret = true;
break;
}
Expand All @@ -118,7 +118,7 @@ private boolean intersects(List<Integer> quad, List<List<Integer>> quads) {

private void drawQuad(DrawContext context, List<Integer> quad, int argb, int offset) {
VertexConsumer vertexConsumer = context.getVertexConsumers().getBuffer(RenderLayer.getGui());
quad.stream().map(catcher::getPos).forEach(pos -> vertexConsumer.vertex(pos.getX(), pos.getY(), pos.getZ() + offset).color(argb).next());
quad.stream().map(unionRecorder::getPos).forEach(pos -> vertexConsumer.vertex(pos.getX(), pos.getY(), pos.getZ() + offset).color(argb).next());
context.draw();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ModelViewScreen extends Screen {
private static final String KEY_TOOLTIP = "screen.tooltip.xtracr_" + RealCamera.MODID + "_modelView_";
protected int xSize = 400, ySize = 220;
protected int x, y;
protected boolean shouldPause = false;
protected boolean shouldPause = false, showCube = false;
private int entitySize = 80;
private double entityX, entityY;
private float yaw, pitch, xRot, yRot;
Expand All @@ -49,6 +49,8 @@ public class ModelViewScreen extends Screen {
private final DoubleValueSlider pitchWidget = new DoubleValueSlider((xSize - ySize)/2 - 15, 18, 0.5D,
-90.0D, 90.0D, d -> Text.translatable(KEY_WIDGET + "pitch", MathUtil.round(d, 2)), d -> pitch = (float) d);
private final ButtonWidget resetWidget = ButtonWidget.builder(Text.translatable(KEY_WIDGET + "reset"), button -> reset()).size((xSize - ySize)/2 - 15, 18).build();
private final ButtonWidget pauseWidget = ButtonWidget.builder(Text.translatable(KEY_WIDGET + "pause"), button -> shouldPause = !shouldPause).size((xSize - ySize)/2 - 15, 18).build();
private final ButtonWidget showCubeWidget = ButtonWidget.builder(Text.translatable(KEY_WIDGET + "showCube"), button -> showCube = !showCube).size((xSize - ySize)/2 - 15, 18).build();

public ModelViewScreen() {
super(Text.translatable(KEY_SCREEN + "title"));
Expand All @@ -67,6 +69,8 @@ protected void init() {
selectFrontButton.setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "selectFront")));
selectUpButton.setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "selectUp")));
selectPosButton.setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "selectPos")));
pauseWidget.setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "pause")));
showCubeWidget.setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "showCube")));
addDrawableChild(resetWidget).setPosition(x + 5, y + 4);
addDrawableChild(yawWidget).setPosition(x + 5, y + 26);
addDrawableChild(pitchWidget).setPosition(x + 5, y + 48);
Expand All @@ -79,15 +83,23 @@ protected void init() {
addDrawableChild(saveButton).setPosition(x + 5, y + 136);
addDrawableChild(loadButton).setPosition(x + (xSize - ySize)/4, y + 136);
addDrawableChild(nameWidget).setTooltip(Tooltip.of(Text.translatable(KEY_WIDGET + "listName")));
addDrawableChild(pauseWidget).setPosition(x + (xSize + ySize) / 2 + 10, y + 4);
addDrawableChild(showCubeWidget).setPosition(x + (xSize + ySize) / 2 + 10, y + 26);
}

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);// 1.20.1 only
super.render(context, mouseX, mouseY, delta);
drawEntity(context, x + (xSize - ySize) / 2, y, x + (xSize + ySize) / 2, y + ySize, mouseX, mouseY, this.client.player);
}

@Override
public void renderBackground(DrawContext context) {
super.renderBackground(context);
context.fill(x, y, x + (xSize - ySize) / 2 - 5, y + ySize, 0xFF555555);
context.fill(x + (xSize - ySize) / 2, y, x + (xSize + ySize) / 2, y + ySize, 0xFF222222);
context.fill(x + (xSize + ySize) / 2 + 5, y, x + xSize, y + ySize, 0xFF555555);
super.render(context, mouseX, mouseY, delta);
drawEntity(context, x + (xSize - ySize) / 2, y, x + (xSize + ySize) / 2, y + ySize, mouseX, mouseY, this.client.player);
}

protected void drawEntity(DrawContext context, int x1, int y1, int x2, int y2, int mouseX, int mouseY, LivingEntity entity) {
Expand Down Expand Up @@ -131,7 +143,10 @@ protected void drawEntity(DrawContext context, float x, float y, int mouseX, int
analyser.analyse();
focusedIndex = analyser.getFocusedIndex(mouseX, mouseY, layers);
analyser.drawQuad(context, posIndex, 0x6F3333CC);
if (focusedIndex > -1) analyser.drawPolyhedron(context, focusedIndex, 0x5FFFFFFF);
if (focusedIndex > -1) {
if (showCube) analyser.drawPolyhedron(context, focusedIndex, 0x5FFFFFFF);
else analyser.drawQuad(context, focusedIndex, 0x7FFFFFFF);
}
analyser.drawNormal(context, frontIndex, entitySize / 2, 0xFF00CC00);
analyser.drawNormal(context, upIndex, entitySize / 2, 0xFFCC0000);
posPolygon = analyser.getQuad(posIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.xtracr.realcamera.util.CrosshairUtil;
import com.xtracr.realcamera.util.RaycastUtil;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.Perspective;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
Expand All @@ -17,17 +15,13 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GameRenderer.class)
public abstract class MixinGameRenderer {
@Unique
private static boolean realCamera$toggled = false;

@Shadow
@Final MinecraftClient client;

Expand All @@ -47,25 +41,6 @@ public abstract class MixinGameRenderer {
return CrosshairUtil.capturedEntityHitResult;
}

@Inject(method = "renderHand", at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/util/math/MatrixStack;push()V"))
private void realCamera$setThirdPerson(MatrixStack matrices, Camera camera, float tickDelta, CallbackInfo cInfo) {
if (ConfigFile.modConfig.isRendering() && !ConfigFile.modConfig.shouldDisableRendering(client) && RealCameraCore.isActive() &&
!ConfigFile.modConfig.allowRenderingHand(client)) {
client.options.setPerspective(Perspective.THIRD_PERSON_BACK);
realCamera$toggled = true;
}
}

@Inject(method = "renderHand", at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/util/math/MatrixStack;pop()V"))
private void realCamera$setFirstPerson(CallbackInfo cInfo) {
if (realCamera$toggled) {
client.options.setPerspective(Perspective.FIRST_PERSON);
realCamera$toggled = false;
}
}

@Inject(method = "renderWorld", at = @At("HEAD"))
private void realCamera$onRenderWorldHEAD(float tickDelta, long limitTime, MatrixStack matrices, CallbackInfo cInfo) {
RealCameraCore.init(client);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.xtracr.realcamera.mixin;

import com.xtracr.realcamera.RealCameraCore;
import com.xtracr.realcamera.config.ConfigFile;
import com.xtracr.realcamera.config.ModConfig;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.item.HeldItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(HeldItemRenderer.class)
public abstract class MixinHeldItemRenderer {
@Shadow
@Final private MinecraftClient client;

@Inject(method = "renderFirstPersonItem", at = @At("HEAD"), cancellable = true)
private void realcamera$cancelRendering(AbstractClientPlayerEntity player, float tickDelta, float pitch, Hand hand, float swingProgress,
ItemStack item, float equipProgress, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, CallbackInfo cInfo) {
ModConfig config = ConfigFile.modConfig;
if (player instanceof ClientPlayerEntity && RealCameraCore.isActive() && config.isRendering() &&
!config.shouldDisableRendering(client) && !config.allowRenderingHand(client)) {
cInfo.cancel();
}
}
}
Loading

0 comments on commit 263bb86

Please sign in to comment.