Skip to content

Commit

Permalink
Optimized some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed Mar 3, 2024
1 parent b6bebcb commit 7025e76
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 108 deletions.
10 changes: 4 additions & 6 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.joml.Vector4f;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -137,16 +136,15 @@ public static void computeCamera(MinecraftClient client, float tickDelta) {
if (config().binding.experimental) {
List<BindingTarget> targetList = new ArrayList<>();
if (config().binding.autoBind) {
Collection<BindingTarget> targetSet = config().binding.targetMap.values();
recorder.setCurrent(renderLayer -> targetSet.stream().anyMatch(t -> renderLayer.toString().contains(t.textureId())));
List<BindingTarget> targets = config().binding.targetList;
recorder.setCurrent(renderLayer -> targets.stream().anyMatch(t -> renderLayer.toString().contains(t.textureId())));
String textureId = recorder.currentTextureId();
if (textureId != null) targetList.addAll(targetSet.stream().filter(t -> textureId.contains(t.textureId())).toList());
if (textureId != null) targetList.addAll(targets.stream().filter(t -> textureId.contains(t.textureId())).toList());
}
targetList.add(config().getTarget(config().binding.nameOfList));
targetList.add(config().getTarget(config().binding.targetName));
for (BindingTarget target : targetList) {
try {
recorder.setCurrent(renderLayer -> renderLayer.toString().contains(target.textureId()));
if (recorder.quadCount() <= 0) throw new NullPointerException("Vertices not found");
pos = recorder.getTargetPosAndRot(target, normal);
currentTarget = target;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.xtracr.realcamera.util.ReflectUtil;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.Vec3d;

import java.lang.reflect.Method;
import java.util.Optional;
Expand Down Expand Up @@ -31,11 +30,4 @@ public static void scaleMatrices(MatrixStack matrixStack, Entity entity, float t
final float heightScale = (float) ReflectUtil.invokeMethod(getModelHeightScale, null, entity, tickDelta).orElse(1.0f);
matrixStack.peek().getPositionMatrix().scale(widthScale, heightScale, widthScale);
}

public static Vec3d scaleVec3d(Vec3d vec3d, Entity entity, float tickDelta) {
if (!loaded) return vec3d;
final float widthScale = (float) ReflectUtil.invokeMethod(getModelWidthScale, null, entity, tickDelta).orElse(1.0f);
final float heightScale = (float) ReflectUtil.invokeMethod(getModelHeightScale, null, entity, tickDelta).orElse(1.0f);
return vec3d.multiply(widthScale, heightScale, widthScale);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ public class PhysicsModCompat {

public static <E extends Entity> void renderStart(EntityRenderDispatcher dispatcher, E entity, double x, double y, double z, float yRot, float renderPercent, MatrixStack matrixStack) {
if (!loaded) return;
Object world = ReflectUtil.getFieldValue(EntityRenderDispatcher_worldField, dispatcher).get();
if ((boolean) ReflectUtil.invokeMethod(ConfigClient_areOceanPhysicsEnabled, null).orElse(false) && world instanceof ClientWorld clientWorld) {
Object oceanWorld = ReflectUtil.invokeMethod(PhysicsWorld_getOceanWorld, ReflectUtil.invokeMethod(PhysicsMod_getPhysicsWorld, ReflectUtil.invokeMethod(PhysicsMod_getInstance, null, clientWorld).get()).get()).get();
ReflectUtil.invokeMethod(OceanWorld_computeEntityOffset, oceanWorld, matrixStack.peek().getPositionMatrix(), matrixStack.peek().getNormalMatrix(), clientWorld, entity, x, y, z, 0.0d, 0.0d, 0.0d, yRot, renderPercent);
try {
Object world = ReflectUtil.getFieldValue(EntityRenderDispatcher_worldField, dispatcher).get();
if ((boolean) ReflectUtil.invokeMethod(ConfigClient_areOceanPhysicsEnabled, null).orElse(false) && world instanceof ClientWorld clientWorld) {
Object oceanWorld = ReflectUtil.invokeMethod(PhysicsWorld_getOceanWorld, ReflectUtil.invokeMethod(PhysicsMod_getPhysicsWorld, ReflectUtil.invokeMethod(PhysicsMod_getInstance, null, clientWorld).get()).get()).get();
ReflectUtil.invokeMethod(OceanWorld_computeEntityOffset, oceanWorld, matrixStack.peek().getPositionMatrix(), matrixStack.peek().getNormalMatrix(), clientWorld, entity, x, y, z, 0.0d, 0.0d, 0.0d, yRot, renderPercent);
}
} catch (Exception ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public static Screen create(Screen parent) {
disable.addEntry(disableModWhen.build());

experimental.addEntry(entryBuilder.startBooleanToggle(Text.literal("Enabled"), config.binding.experimental)
.setDefaultValue(false)
.setDefaultValue(true)
.setSaveConsumer(b -> config.binding.experimental = b)
.build());
experimental.addEntry(entryBuilder.startBooleanToggle(Text.literal("Auto Bind"), config.binding.autoBind)
Expand Down
20 changes: 11 additions & 9 deletions common/src/main/java/com/xtracr/realcamera/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.util.math.MathHelper;

import java.util.*;
import java.util.stream.IntStream;

public class ModConfig {
protected static final double MIN_DOUBLE = -64.0d;
Expand Down Expand Up @@ -81,7 +82,7 @@ public boolean isAdjustingOffset() {
}

public BindingTarget getTarget(String name) {
return binding.targetMap.get(name);
return binding.targetList.stream().filter(target -> target.name().equals(name)).findAny().orElse(null);
}

public boolean isXBound() {
Expand Down Expand Up @@ -129,7 +130,11 @@ public void setAdjustOffset(boolean value) {
}

public void putTarget(BindingTarget target) {
if (!target.isEmpty()) binding.targetMap.put(target.name(), target);
if (target.isEmpty()) return;
IntStream.range(0, binding.targetList.size())
.filter(i -> binding.targetList.get(i).name().equals(target.name()))
.findAny()
.ifPresentOrElse(i -> binding.targetList.set(i, target), () -> binding.targetList.add(target));
}

public void adjustBindingX(boolean add) {
Expand Down Expand Up @@ -299,12 +304,12 @@ private void clamp() {
}

public static class Binding {
public List<BindingTarget> targetList = new ArrayList<>(BindingTarget.defaultTargets);
public VanillaModelPart vanillaModelPart = VanillaModelPart.head;
public boolean experimental = false;
public boolean experimental = true;
public boolean adjustOffset = true;
public boolean autoBind = true;
public String nameOfList = "minecraft_head";
public LinkedHashMap<String, BindingTarget> targetMap = new LinkedHashMap<>();
public String targetName = "minecraft_head";
public boolean bindX = true;
public boolean bindY = true;
public boolean bindZ = true;
Expand All @@ -318,10 +323,7 @@ public static class Binding {

private void clamp() {
if (vanillaModelPart == null) vanillaModelPart = VanillaModelPart.head;
if (targetMap == null || targetMap.isEmpty()) {
targetMap = new LinkedHashMap<>();
BindingTarget.defaultTargets.forEach(target -> targetMap.put(target.name(), target));
}
if (targetList == null || targetList.isEmpty()) targetList = new ArrayList<>(BindingTarget.defaultTargets);
cameraX = MathHelper.clamp(cameraX, MIN_DOUBLE, MAX_DOUBLE);
cameraY = MathHelper.clamp(cameraY, MIN_DOUBLE, MAX_DOUBLE);
cameraZ = MathHelper.clamp(cameraZ, MIN_DOUBLE, MAX_DOUBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,41 @@
public class CyclingTexturedButton extends PressableWidget {
private static final Identifier ICON_TEXTURE = new Identifier(RealCamera.MODID, "textures/gui/icon.png");
protected final Identifier texture;
protected final int textureWidth, textureHeight, vOffset, maximum;
protected int u, v;
private int value = 0;
protected final int textureWidth, textureHeight, vOffset, size;
protected int u, v, value;

public CyclingTexturedButton(int u, int v, int maximum) {
this(0, 0, 16, 16, u, v, maximum);
public CyclingTexturedButton(int u, int v, int value, int size) {
this(0, 0, 16, 16, u, v, value, size);
}

public CyclingTexturedButton(int x, int y, int width, int height, int u, int v, int maximum) {
this(x, y, width, height, u, v, height, maximum, ICON_TEXTURE, 256, 256, Text.empty());
public CyclingTexturedButton(int x, int y, int width, int height, int u, int v, int value, int size) {
this(x, y, width, height, u, v, height, value, size, ICON_TEXTURE, 256, 256, Text.empty());
}

public CyclingTexturedButton(int x, int y, int width, int height, int u, int v, int vOffset, int maximum, Identifier texture, int textureWidth, int textureHeight, Text message) {
public CyclingTexturedButton(int x, int y, int width, int height, int u, int v, int vOffset, int value, int size, Identifier texture, int textureWidth, int textureHeight, Text message) {
super(x, y, width, height, message);
this.u = u;
this.v = v;
this.vOffset = vOffset;
this.maximum = maximum;
this.size = size;
this.texture = texture;
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
setValue(value);
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = (value % maximum + maximum) % maximum;
}

private void cycle(int amount) {
value = (value + amount % maximum + maximum) % maximum;
this.value = (value % size + size) % size;
}

@Override
public void onPress() {
if (Screen.hasShiftDown()) cycle(-1);
else cycle(1);
if (Screen.hasShiftDown()) setValue(value - 1);
else setValue(value + 1);
}

@Override
Expand Down
28 changes: 13 additions & 15 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelAnalyser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xtracr.realcamera.gui;

import com.xtracr.realcamera.config.BindingTarget;
import com.xtracr.realcamera.util.Triple;
import com.xtracr.realcamera.util.VertexRecorder;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderLayer;
Expand All @@ -19,7 +18,7 @@ public class ModelAnalyser extends VertexRecorder {
private BuiltRecord focusedRecord;

private static boolean intersects(Vertex[] quad, List<Vertex[]> quads) {
final float precision = 0.00001f;
final float precision = 1.0E-05f;
for (Vertex[] q : quads)
for (Vertex v1 : quad)
for (Vertex v2 : q) if (v1.pos().squaredDistanceTo(v2.pos()) < precision) return true;
Expand All @@ -28,8 +27,7 @@ private static boolean intersects(Vertex[] quad, List<Vertex[]> quads) {

private static void drawQuad(DrawContext context, Vertex[] quad, int argb, int offset) {
VertexConsumer vertexConsumer = context.getVertexConsumers().getBuffer(RenderLayer.getGui());
for (Vertex vertex : quad)
vertexConsumer.vertex(vertex.x(), vertex.y(), vertex.z() + offset).color(argb).next();
for (Vertex vertex : quad) vertexConsumer.vertex(vertex.x(), vertex.y(), vertex.z() + offset).color(argb).next();
if (quad.length == 3) vertexConsumer.vertex(quad[2].x(), quad[2].y(), quad[2].z() + offset).color(argb).next();
context.draw();
}
Expand Down Expand Up @@ -61,7 +59,7 @@ public Vec2f getCenterUV(int quadIndex) {
}

public int getFocusedIndex(int mouseX, int mouseY, int layers) {
List<Triple<Double, BuiltRecord, Integer>> sortByDepth = new ArrayList<>();
List<Triple> sortByDepth = new ArrayList<>();
records.forEach(record -> {
Vertex[][] vertices = record.vertices();
for (int i = 0, size = vertices.length; i < size; i++) {
Expand All @@ -71,34 +69,32 @@ public int getFocusedIndex(int mouseX, int mouseY, int layers) {
Vertex point = quad[0];
double deltaZ = 0;
if (point.normalZ() != 0) deltaZ = (point.normalX() * (mouseX - point.x()) + point.normalY() * (mouseY - point.y())) / point.normalZ();
if (polygon.contains(mouseX, mouseY)) sortByDepth.add(new Triple<>(point.z() + deltaZ, record, i));
if (polygon.contains(mouseX, mouseY)) sortByDepth.add(new Triple(point.z() + deltaZ, record, i));
}
});
if (sortByDepth.isEmpty()) return -1;
sortByDepth.sort(Comparator.comparingDouble(triple -> -triple.getLeft()));
Triple<Double, BuiltRecord, Integer> result = sortByDepth.get(Math.min(sortByDepth.size() - 1, layers));
focusedRecord = result.getMiddle();
return result.getRight();
sortByDepth.sort(Comparator.comparingDouble(triple -> -triple.depth));
Triple result = sortByDepth.get(Math.min(sortByDepth.size() - 1, layers));
focusedRecord = result.record;
return result.index;
}

public void preview(DrawContext context, BindingTarget target, int entitySize, int forwardArgb, int upwardArgb, int leftArgb) {
Matrix3f normal = new Matrix3f();
drawByAnother(context.getVertexConsumers());
context.draw();
target.setOffsetX(entitySize * target.offsetX());
target.setOffsetY(entitySize * target.offsetY());
target.setOffsetZ(entitySize * target.offsetZ());
Matrix3f normal = new Matrix3f();
Vec3d pos;
try {
pos = getTargetPosAndRot(target, normal);
} catch (Exception exception) {
drawByAnother(context.getVertexConsumers());
context.draw();
return;
}
normal.rotateLocal((float) Math.toRadians(target.yaw()), normal.m10, normal.m11, normal.m12);
normal.rotateLocal((float) Math.toRadians(target.pitch()), normal.m00, normal.m01, normal.m02);
normal.rotateLocal((float) Math.toRadians(target.roll()), normal.m20, normal.m21, normal.m22);
drawByAnother(context.getVertexConsumers());
context.draw();
drawNormal(context, pos, new Vec3d(normal.m20(), normal.m21(), -normal.m22()), entitySize / 3, forwardArgb);
drawNormal(context, pos, new Vec3d(normal.m10(), normal.m11(), -normal.m12()), entitySize / 6, upwardArgb);
drawNormal(context, pos, new Vec3d(normal.m00(), normal.m01(), -normal.m02()), entitySize / 6, leftArgb);
Expand Down Expand Up @@ -151,4 +147,6 @@ public void drawNormal(DrawContext context, float u, float v, int length, int ar
if (currentRecord == null || (quad = getQuad(currentRecord, u, v)) == null) return;
drawNormal(context, getPos(quad, u, v), quad[0].normal(), length, argb);
}

record Triple(double depth, BuiltRecord record, int index) {}
}
Loading

0 comments on commit 7025e76

Please sign in to comment.