Skip to content

Commit

Permalink
Allowed to disable rendering vertices based on depth relative to the …
Browse files Browse the repository at this point in the history
…screen
  • Loading branch information
xTracr committed Feb 3, 2024
1 parent be4b766 commit 89c13da
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 101 deletions.
17 changes: 13 additions & 4 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.joml.Vector4f;

Expand Down Expand Up @@ -84,11 +85,19 @@ public static boolean isActive() {

public static void renderPlayer(Vec3d offset, MatrixStack matrices, VertexConsumerProvider vertexConsumers) {
matrices.push();
matrices.peek().getPositionMatrix().transpose().invertAffine()
Matrix4f positionMatrix = matrices.peek().getPositionMatrix().transpose().invertAffine()
.translate((float) -offset.getX(), (float) -offset.getY(), (float) -offset.getZ());
matrices.peek().getNormalMatrix().transpose().invert();
// TODO
recorder.drawByAnother(matrices, vertexConsumers, null, null);
Matrix3f normalMatrix = matrices.peek().getNormalMatrix().transpose().invert();
VertexRecorder.VertexPredicate predicate = (renderLayer, vertices, index) -> {
Vec3d center = Vec3d.ZERO;
double depth = config.disable.depth;
for (VertexRecorder.Vertex vertex : vertices) {
center = center.add(vertex.pos());
if (vertex.z() < -depth) return true;
}
return center.getZ() < -depth * vertices.length;
};
recorder.drawByAnother(vertex -> vertex.transform(positionMatrix, normalMatrix), vertexConsumers, renderLayer -> true, predicate);
matrices.pop();
}

Expand Down
70 changes: 38 additions & 32 deletions common/src/main/java/com/xtracr/realcamera/config/ConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public static Screen create(Screen parent) {
.setSaveConsumer(b -> config.general.renderModel = b)
.build());
general.addEntry(entryBuilder.startDoubleField(Text.translatable(OPTION + "adjustStep"), config.general.adjustStep)
.setDefaultValue(0.25D)
.setMin(0.0D)
.setDefaultValue(0.25)
.setMin(0)
.setMax(ModConfig.MAX_DOUBLE)
.setTooltip(Text.translatable(TOOLTIP + "adjustStep"))
.setSaveConsumer(d -> config.general.adjustStep = d)
.build());
general.addEntry(entryBuilder.startDoubleField(Text.translatable(OPTION + "scale"), config.general.scale)
.setDefaultValue(1.0D)
.setMin(0.0D)
.setDefaultValue(1)
.setMin(0)
.setMax(ModConfig.MAX_DOUBLE)
.setTooltip(Text.translatable(TOOLTIP + "scale"))
.setSaveConsumer(d -> config.general.scale = d)
Expand All @@ -96,19 +96,19 @@ public static Screen create(Screen parent) {
SubCategoryBuilder bindingCameraOffset = entryBuilder.startSubCategory(Text.translatable(CATEGORY + "cameraOffset"))
.setTooltip(Text.translatable(TOOLTIP + "bindingOffset"), Text.translatable(TOOLTIP + "bindingOffset_n"));
bindingCameraOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "cameraOffset", "X"), config.binding.cameraX)
.setDefaultValue(0.0D)
.setDefaultValue(0)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.binding.cameraX = d)
.build());
bindingCameraOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "cameraOffset", "Y"), config.binding.cameraY)
.setDefaultValue(0.0D)
.setDefaultValue(0)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.binding.cameraY = d)
.build());
bindingCameraOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "cameraOffset", "Z"), config.binding.cameraZ)
.setDefaultValue(0.0D)
.setDefaultValue(0)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.binding.cameraZ = d)
Expand All @@ -129,21 +129,21 @@ public static Screen create(Screen parent) {
.setSaveConsumer(b -> config.binding.bindRolling = b)
.build());
bindingCameraRotation.add(entryBuilder.startFloatField(Text.translatable(OPTION + "pitch"), config.binding.pitch)
.setDefaultValue(0.0F)
.setMin(-180.0F)
.setMax(180.0F)
.setDefaultValue(0.0f)
.setMin(-180.0f)
.setMax(180.0f)
.setSaveConsumer(f -> config.binding.pitch = f)
.build());
bindingCameraRotation.add(entryBuilder.startFloatField(Text.translatable(OPTION + "yaw"), config.binding.yaw)
.setDefaultValue(0.0F)
.setMin(-180.0F)
.setMax(180.0F)
.setDefaultValue(0.0f)
.setMin(-180.0f)
.setMax(180.0f)
.setSaveConsumer(f -> config.binding.yaw = f)
.build());
bindingCameraRotation.add(entryBuilder.startFloatField(Text.translatable(OPTION + "roll"), config.binding.roll)
.setDefaultValue(0.0F)
.setMin(-180.0F)
.setMax(180.0F)
.setDefaultValue(0.0f)
.setMin(-180.0f)
.setMax(180.0f)
.setSaveConsumer(f -> config.binding.roll = f)
.build());
binding.addEntry(bindingCameraRotation.build());
Expand All @@ -156,19 +156,19 @@ public static Screen create(Screen parent) {
SubCategoryBuilder classicCameraOffset = entryBuilder.startSubCategory(Text.translatable(CATEGORY + "cameraOffset"))
.setTooltip(Text.translatable(TOOLTIP + "classicOffset"), Text.translatable(TOOLTIP + "classicOffset_n"));
classicCameraOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "cameraOffset", "X"), config.classic.cameraX)
.setDefaultValue(-60.0D)
.setDefaultValue(-60)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.classic.cameraX = d)
.build());
classicCameraOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "cameraOffset", "Y"), config.classic.cameraY)
.setDefaultValue(2.0D)
.setDefaultValue(2)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.classic.cameraY = d)
.build());
classicCameraOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "cameraOffset", "Z"), config.classic.cameraZ)
.setDefaultValue(-16.0D)
.setDefaultValue(-16)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.classic.cameraZ = d)
Expand All @@ -177,19 +177,19 @@ public static Screen create(Screen parent) {
SubCategoryBuilder classicCenterOffset = entryBuilder.startSubCategory(Text.translatable(CATEGORY + "centerOffset"))
.setTooltip(Text.translatable(TOOLTIP + "centerOffset"));
classicCenterOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "centerOffset", "X"), config.classic.centerX)
.setDefaultValue(0.0D)
.setDefaultValue(0)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.classic.centerX = d)
.build());
classicCenterOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "centerOffset", "Y"), config.classic.centerY)
.setDefaultValue(-3.4D)
.setDefaultValue(-3.4)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.classic.centerY = d)
.build());
classicCenterOffset.add(entryBuilder.startDoubleField(Text.translatable(OPTION + "centerOffset", "Z"), config.classic.centerZ)
.setDefaultValue(0.0D)
.setDefaultValue(0)
.setMin(ModConfig.MIN_DOUBLE)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.classic.centerZ = d)
Expand All @@ -198,21 +198,21 @@ public static Screen create(Screen parent) {
SubCategoryBuilder classicCameraRotation = entryBuilder.startSubCategory(Text.translatable(CATEGORY + "cameraRotation"))
.setTooltip(Text.translatable(TOOLTIP + "cameraRotation"), Text.translatable(TOOLTIP + "cameraRotation_n"));
classicCameraRotation.add(entryBuilder.startFloatField(Text.translatable(OPTION + "pitch"), config.classic.pitch)
.setDefaultValue(0.0F)
.setMin(-180.0F)
.setMax(180.0F)
.setDefaultValue(0.0f)
.setMin(-180.0f)
.setMax(180.0f)
.setSaveConsumer(f -> config.classic.pitch = f)
.build());
classicCameraRotation.add(entryBuilder.startFloatField(Text.translatable(OPTION + "yaw"), config.classic.yaw)
.setDefaultValue(18.0F)
.setMin(-180.0F)
.setMax(180.0F)
.setDefaultValue(18.0f)
.setMin(-180.0f)
.setMax(180.0f)
.setSaveConsumer(f -> config.classic.yaw = f)
.build());
classicCameraRotation.add(entryBuilder.startFloatField(Text.translatable(OPTION + "roll"), config.classic.roll)
.setDefaultValue(0.0F)
.setMin(-180.0F)
.setMax(180.0F)
.setDefaultValue(0.0f)
.setMin(-180.0f)
.setMax(180.0f)
.setSaveConsumer(f -> config.classic.roll = f)
.build());
classic.addEntry(classicCameraRotation.build());
Expand Down Expand Up @@ -316,13 +316,19 @@ public static Screen create(Screen parent) {
.build());
disable.addEntry(disableModWhen.build());

experimental.addEntry(entryBuilder.startBooleanToggle(Text.translatable(OPTION + "enabled"), config.binding.experimental)
experimental.addEntry(entryBuilder.startBooleanToggle(Text.literal("Enabled"), config.binding.experimental)
.setDefaultValue(false)
.setSaveConsumer(b -> config.binding.experimental = b)
.build());
experimental.addEntry(entryBuilder.startStrField(Text.literal("Name Of List"), config.binding.nameOfList)
.setSaveConsumer(s -> config.binding.nameOfList = s)
.build());
experimental.addEntry(entryBuilder.startDoubleField(Text.literal("Disabling Depth"), config.disable.depth)
.setDefaultValue(0.2)
.setMin(0.01)
.setMax(ModConfig.MAX_DOUBLE)
.setSaveConsumer(d -> config.disable.depth = d)
.build());

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public static class Disable {
public boolean sneaking = false;
public boolean sleeping = false;
public boolean screenOpened = false;
public double depth = 0.2;

private void clamp() {
if (disabledModelParts == null) disabledModelParts = defaultParts;
Expand Down
26 changes: 16 additions & 10 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.util.math.Vec3d;
import org.joml.Vector3f;

import java.awt.*;
Expand All @@ -27,12 +26,13 @@ public int getFocusedIndex(int mouseX, int mouseY, int layers) {
for (BuiltRecord record : records) for (int i = 0; i < record.quadCount(); i++) {
Vertex[] quad = record.vertices()[i];
Polygon polygon = new Polygon();
for (Vertex vertex : quad) polygon.addPoint((int) vertex.pos().getX(), (int) vertex.pos().getY());
Vector3f normal = quad[0].normal().toVector3f();
Vector3f point = quad[0].pos().toVector3f();
for (Vertex vertex : quad) polygon.addPoint((int) vertex.x(), (int) vertex.y());
Vertex point = quad[0];
Vector3f normal = new Vector3f(point.normalX(), point.normalY(), point.normalZ());
Vector3f mousePos = new Vector3f((float) point.x(), (float) point.y(), (float) point.z());
float deltaZ = 0;
if (normal.z() != 0) deltaZ = (normal.x() * (mouseX - point.x()) + normal.y() * (mouseY - point.y())) / normal.z();
if (polygon.contains(mouseX, mouseY)) sortByDepth.add(new Triple<>(point.z() + deltaZ, record, i));
if (normal.z() != 0) deltaZ = (normal.x() * (mouseX - mousePos.x()) + normal.y() * (mouseY - mousePos.y())) / normal.z();
if (polygon.contains(mouseX, mouseY)) sortByDepth.add(new Triple<>(mousePos.z() + deltaZ, record, i));
}
if (sortByDepth.isEmpty()) return -1;
sortByDepth.sort(Comparator.comparingDouble(triple -> -triple.getLeft()));
Expand All @@ -45,6 +45,13 @@ public void drawQuad(DrawContext context, int quadIndex, int argb, boolean drawF
BuiltRecord record = drawFocused ? focusedRecord : currentRecord;
if (record == null || quadIndex >= record.quadCount()) return;
drawQuad(context, record.vertices()[quadIndex], argb, 1000);
if (drawFocused) {
Vertex[] highlight = record.vertices()[quadIndex];
int size = highlight.length;
Vertex[] reversed = new Vertex[size];
for (int i = 0; i < size; i++) reversed[i] = highlight[size - 1 - i];
drawQuad(context, reversed, argb, 1000);
}
}

public void drawPolyhedron(DrawContext context, int quadIndex, int argb) {
Expand Down Expand Up @@ -94,19 +101,18 @@ public void drawNormal(DrawContext context, int quadIndex, int length, int argb)
context.draw();
}

private boolean intersects(Vertex[] quad, List<Vertex[]> quads) {
private static boolean intersects(Vertex[] quad, List<Vertex[]> quads) {
final float precision = 0.00001f;
boolean ret = false;
for (Vertex[] p : quads) for (Vertex v1 : quad)
if (Arrays.stream(p).anyMatch(v2 -> v1.pos().squaredDistanceTo(v2.pos()) < precision)) ret = true;
return ret;
}

private void drawQuad(DrawContext context, Vertex[] quad, int argb, int offset) {
private static void drawQuad(DrawContext context, Vertex[] quad, int argb, int offset) {
VertexConsumer vertexConsumer = context.getVertexConsumers().getBuffer(RenderLayer.getGui());
for (Vertex vertex : quad) {
Vec3d pos = vertex.pos();
vertexConsumer.vertex(pos.getX(), pos.getY(), pos.getZ() + offset).color(argb).next();
vertexConsumer.vertex(vertex.x(), vertex.y(), vertex.z() + offset).color(argb).next();
}
context.draw();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected void drawEntity(DrawContext context, float x, float y, int mouseX, int
ModelAnalyser analyser = new ModelAnalyser();
entityRenderDispatcher.render(entity, 0, -entity.getHeight() / 2.0f, 0, 0.0f, 1.0f, context.getMatrices(), analyser, 0xF000F0);
analyser.buildLastRecord();
analyser.drawByAnother(context.getVertexConsumers(), null, null); // TODO
analyser.drawByAnother(context.getVertexConsumers(), renderLayer -> true, (renderLayer, vertices, index) -> true); // TODO
context.draw();
analyser.setCurrent(renderLayer -> renderLayer.toString().equals(renderTypeWidget.getText()), 0);
focusedIndex = analyser.getFocusedIndex(mouseX, mouseY, layers);
Expand Down
Loading

0 comments on commit 89c13da

Please sign in to comment.