Skip to content

Commit

Permalink
Added 'preview' feature
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed Feb 27, 2024
1 parent 8742e42 commit d204614
Show file tree
Hide file tree
Showing 23 changed files with 597 additions and 256 deletions.
28 changes: 16 additions & 12 deletions common/src/main/java/com/xtracr/realcamera/KeyBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import com.xtracr.realcamera.gui.ModelViewScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;

import java.util.Collection;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public final class KeyBindings {
private static final ModConfig config = ConfigFile.modConfig;
private static final String KEY_CATEGORY = "key.category.xtracr_" + RealCamera.MODID;
private static final String KEY_ID = "key.xtracr_" + RealCamera.MODID + "_";
public static final Collection<KeyBinding> KEY_BINDINGS = new HashSet<>();
private static final List<KeyBinding> KEY_BINDINGS = new ArrayList<>();
private static final KeyBinding MODEL_VIEW_GUI = createKeyBinding("modelViewGui");
private static final KeyBinding TOGGLE_PERSPECTIVE = createKeyBinding("togglePerspective", GLFW.GLFW_KEY_F6);
private static final KeyBinding TOGGLE_ADJUST_MODE = createKeyBinding("toggleAdjustMode");
Expand All @@ -28,7 +27,7 @@ public final class KeyBindings {
private static final KeyBinding ADJUST_RIGHT = createKeyBinding("adjustRIGHT");

public static KeyBinding createKeyBinding(String id) {
return createKeyBinding(id, InputUtil.UNKNOWN_KEY.getCode());
return createKeyBinding(id, GLFW.GLFW_KEY_UNKNOWN);
}

public static KeyBinding createKeyBinding(String id, int code) {
Expand All @@ -37,8 +36,13 @@ public static KeyBinding createKeyBinding(String id, int code) {
return keyBinding;
}

public static void register(Consumer<KeyBinding> registerer) {
KEY_BINDINGS.forEach(registerer);
}

public static void handle(MinecraftClient client) {
if (client.player == null) return;
final ModConfig config = ConfigFile.modConfig;
while (MODEL_VIEW_GUI.wasPressed()) {
client.setScreen(new ModelViewScreen());
}
Expand All @@ -58,32 +62,32 @@ public static void handle(MinecraftClient client) {
ConfigFile.save();
}
while (ADJUST_LEFT.wasPressed()) {
if (config.isClassic()) config.adjustClassicZ(true);
if (config.isClassic()) config.adjustClassicZ(1);
else config.adjustBindingZ(true);
ConfigFile.save();
}
while (ADJUST_RIGHT.wasPressed()) {
if (config.isClassic()) config.adjustClassicZ(false);
if (config.isClassic()) config.adjustClassicZ(-1);
else config.adjustBindingZ(false);
ConfigFile.save();
}
while (ADJUST_UP.wasPressed()) {
if (config.isClassic()) config.adjustClassicY(true);
if (config.isClassic()) config.adjustClassicY(1);
else config.adjustBindingY(true);
ConfigFile.save();
}
while (ADJUST_DOWN.wasPressed()) {
if (config.isClassic()) config.adjustClassicY(false);
if (config.isClassic()) config.adjustClassicY(-1);
else config.adjustBindingY(false);
ConfigFile.save();
}
while (ADJUST_FRONT.wasPressed()) {
if (config.isClassic()) config.adjustClassicX(true);
if (config.isClassic()) config.adjustClassicX(1);
else config.adjustBindingX(true);
ConfigFile.save();
}
while (ADJUST_BACK.wasPressed()) {
if (config.isClassic()) config.adjustClassicX(false);
if (config.isClassic()) config.adjustClassicX(-1);
else config.adjustBindingX(false);
ConfigFile.save();
}
Expand Down
109 changes: 55 additions & 54 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.xtracr.realcamera.api.VirtualRenderer;
import com.xtracr.realcamera.compat.PehkuiCompat;
import com.xtracr.realcamera.compat.PhysicsModCompat;
import com.xtracr.realcamera.config.BindingTarget;
import com.xtracr.realcamera.config.ConfigFile;
import com.xtracr.realcamera.config.ModConfig;
import com.xtracr.realcamera.mixin.PlayerEntityRendererAccessor;
Expand All @@ -25,17 +26,16 @@
import net.minecraft.util.math.Vec3d;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.joml.Vector4f;

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

public class RealCameraCore {
private static final ModConfig config = ConfigFile.modConfig;
private static VertexRecorder recorder = new VertexRecorder();
private static BindingTarget currentTarget = new BindingTarget();
private static String status = "Successful";
private static boolean renderingPlayer = false;
private static boolean active = false;
Expand All @@ -55,40 +55,40 @@ public static void setRenderingPlayer(boolean value) {
}

public static float getPitch(float f) {
if (config.isRotationBound()) return pitch;
else return f + config.getBindingPitch();
if (currentTarget.bindRotation()) return pitch;
else return f + currentTarget.pitch();
}

public static float getYaw(float f) {
if (config.isRotationBound()) return yaw;
else return f - config.getBindingYaw();
if (currentTarget.bindRotation()) return yaw;
else return f - currentTarget.yaw();
}

public static float getRoll(float f) {
if (config.isClassic()) return f + config.getClassicRoll();
else if (config.isRotationBound()) return roll;
else return f + config.getBindingRoll();
if (config().isClassic()) return f + config().getClassicRoll();
else if (currentTarget.bindRotation()) return roll;
else return f + currentTarget.roll();
}

public static Vec3d getPos(Vec3d vec3d) {
return new Vec3d(config.isXBound() ? pos.getX() : vec3d.getX() + config.getBindingX(),
config.isYBound() ? pos.getY() : vec3d.getY() + config.getBindingY(),
config.isZBound() ? pos.getZ() : vec3d.getZ() + config.getBindingZ());
return new Vec3d(currentTarget.bindX() ? pos.getX() : vec3d.getX() + currentTarget.offsetX(),
currentTarget.bindY() ? pos.getY() : vec3d.getY() + currentTarget.offsetY(),
currentTarget.bindZ() ? pos.getZ() : vec3d.getZ() + currentTarget.offsetZ());
}

public static Vec3d getCameraPos(Vec3d vec3d) {
return new Vec3d(config.isXBound() ? cameraPos.getX() : vec3d.getX() + config.getBindingX(),
config.isYBound() ? cameraPos.getY() : vec3d.getY() + config.getBindingY(),
config.isZBound() ? cameraPos.getZ() : vec3d.getZ() + config.getBindingZ());
return new Vec3d(currentTarget.bindX() ? cameraPos.getX() : vec3d.getX() + currentTarget.offsetX(),
currentTarget.bindY() ? cameraPos.getY() : vec3d.getY() + currentTarget.offsetY(),
currentTarget.bindZ() ? cameraPos.getZ() : vec3d.getZ() + currentTarget.offsetZ());
}

public static void setCameraPos(Vec3d vec3d) {
cameraPos = vec3d;
}

public static void init(MinecraftClient client) {
active = config.isEnabled() && client.options.getPerspective().isFirstPerson() && client.gameRenderer.getCamera() != null
&& client.player != null && !config.shouldDisableMod(client);
active = config().isEnabled() && client.options.getPerspective().isFirstPerson() && client.gameRenderer.getCamera() != null
&& client.player != null && !config().shouldDisableMod(client);
}

public static boolean isActive() {
Expand All @@ -103,20 +103,24 @@ public static void renderPlayer(VertexConsumerProvider vertexConsumers) {
Matrix4f positionMatrix = matrixStack.peek().getPositionMatrix().transpose().invertAffine()
.translate((float) -pos.getX(), (float) -pos.getY(), (float) -pos.getZ());
Matrix3f normalMatrix = matrixStack.peek().getNormalMatrix().transpose().invert();
BiPredicate<RenderLayer, VertexRecorder.Vertex[]> biPredicate = (renderLayer, vertices) -> {
double depth = config.disable.depth, centerZ = 0;
for (VertexRecorder.Vertex vertex : vertices) {
if (vertex.z() < -depth) return true;
BiFunction<RenderLayer, VertexRecorder.Vertex[], VertexRecorder.Vertex[]> function = (renderLayer, vertices) -> {
double depth = currentTarget.disablingDepth(), centerZ = 0;
int size = vertices.length;
VertexRecorder.Vertex[] newQuad = new VertexRecorder.Vertex[size];
for (int i = 0; i < size; i++) newQuad[i] = vertices[i].transform(positionMatrix, normalMatrix);
for (VertexRecorder.Vertex vertex : newQuad) {
if (vertex.z() < -depth) return newQuad;
centerZ += vertex.z();
}
return centerZ < -depth * vertices.length;
return centerZ < -depth * vertices.length ? newQuad : null;
};
recorder.drawByAnother(vertex -> vertex.transform(positionMatrix, normalMatrix), vertexConsumers, renderLayer -> true, biPredicate);
recorder.drawByAnother(vertexConsumers, renderLayer -> true, function);
}

public static void computeCamera(MinecraftClient client, float tickDelta) {
roll = config.getClassicRoll();
if (config.isClassic()) return;
roll = config().getClassicRoll();
currentTarget = new BindingTarget();
if (config().isClassic()) return;

// GameRenderer.renderWorld
MatrixStack matrixStack = new MatrixStack();
Expand All @@ -125,48 +129,45 @@ public static void computeCamera(MinecraftClient client, float tickDelta) {
recorder.buildLastRecord();

// ModelPart$Cuboid.renderCuboid
Vector4f offset = matrixStack.peek().getPositionMatrix().transform(new Vector4f((float) (config.getBindingZ() * config.getScale()),
-(float) (config.getBindingY() * config.getScale()) - 0.125f,
-(float) (config.getBindingX() * config.getScale()) - 0.225f, 1.0F));
Vector4f offset = matrixStack.peek().getPositionMatrix().transform(new Vector4f((float) (config().getBindingZ() * config().getScale()),
-(float) (config().getBindingY() * config().getScale()) - 0.125f,
-(float) (config().getBindingX() * config().getScale()) - 0.225f, 1.0f));
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) {
List<ModConfig.Binding.Target> targetList = new ArrayList<>();
if (config.binding.autoBind) {
Collection<ModConfig.Binding.Target> targetSet = config.binding.targetMap.values();
Matrix3f normal = matrixStack.peek().getNormalMatrix().scale(1.0f, -1.0f, -1.0f);
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())));
String textureId = recorder.currentTextureId();
if (textureId != null) targetList.addAll(targetSet.stream().filter(t -> textureId.contains(t.textureId())).toList());
}
targetList.add(config.binding.targetMap.get(config.binding.nameOfList));
for (ModConfig.Binding.Target target : targetList) {
targetList.add(config().binding.targetMap.get(config().binding.nameOfList));
for (BindingTarget target : targetList) {
try {
recorder.setCurrent(renderLayer -> renderLayer.toString().contains(target.textureId()));
if (recorder.quadCount() <= 0) throw new NullPointerException("Vertices not found");
Vec3d front = recorder.getNormal(target.forwardU(), target.forwardV());
Vec3d up = recorder.getNormal(target.upwardU(), target.upwardV());
Vec3d center = recorder.getPos(target.posU(), target.posV());
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()),
(float) (config.getBindingY() * config.getScale()),
(float) (config.getBindingX() * config.getScale())));
pos = center.add(vec3f.x(), vec3f.y(), vec3f.z());
pos = recorder.getTargetPosAndRot(target, normal);
currentTarget = target;
break;
} catch (Exception ignored) {
}
}
}

normal.rotateLocal((float) Math.toRadians(config.getBindingYaw()), normal.m10, normal.m11, normal.m12);
normal.rotateLocal((float) Math.toRadians(config.getBindingPitch()), normal.m00, normal.m01, normal.m02);
normal.rotateLocal((float) Math.toRadians(config.getBindingRoll()), normal.m20, normal.m21, normal.m22);
Vec3d eulerAngle = MathUtil.getEulerAngleYXZ(normal).multiply(180.0D / Math.PI);
normal.rotateLocal((float) Math.toRadians(currentTarget.yaw()), normal.m10, normal.m11, normal.m12);
normal.rotateLocal((float) Math.toRadians(currentTarget.pitch()), normal.m00, normal.m01, normal.m02);
normal.rotateLocal((float) Math.toRadians(currentTarget.roll()), normal.m20, normal.m21, normal.m22);
Vec3d eulerAngle = MathUtil.getEulerAngleYXZ(normal).multiply(180.0d / Math.PI);
pitch = (float) eulerAngle.getX();
yaw = (float) -eulerAngle.getY();
roll = (float) eulerAngle.getZ();
}

private static ModConfig config() {
return ConfigFile.modConfig;
}

private static void virtualRender(MinecraftClient client, float tickDelta, MatrixStack matrixStack, VertexConsumerProvider consumers) {
ClientPlayerEntity player = client.player;
// WorldRenderer.render
Expand All @@ -182,21 +183,21 @@ private static void virtualRender(MinecraftClient client, float tickDelta, Matri
matrixStack.push();
EntityRenderDispatcher dispatcher = client.getEntityRenderDispatcher();
dispatcher.configure(client.world, client.gameRenderer.getCamera(), player);
if (config.binding.experimental) dispatcher.render(player, renderOffset.getX(), renderOffset.getY(), renderOffset.getZ(),
if (config().binding.experimental) dispatcher.render(player, renderOffset.getX(), renderOffset.getY(), renderOffset.getZ(),
MathHelper.lerp(tickDelta, player.prevYaw, player.getYaw()), tickDelta, matrixStack, consumers, dispatcher.getLight(player, tickDelta));
matrixStack.pop();
// EntityRenderDispatcher.render
if (config.compatPhysicsMod())
if (config().compatPhysicsMod())
PhysicsModCompat.renderStart(client.getEntityRenderDispatcher(), player, renderOffset.getX(), renderOffset.getY(),
renderOffset.getZ(), MathHelper.lerp(tickDelta, player.prevYaw, player.getYaw()), tickDelta, matrixStack);

PlayerEntityRenderer playerRenderer = (PlayerEntityRenderer) client.getEntityRenderDispatcher().getRenderer(player);
renderOffset = renderOffset.add(playerRenderer.getPositionOffset(player, tickDelta));
matrixStack.translate(renderOffset.getX(), renderOffset.getY(), renderOffset.getZ());

if (config.compatPehkui()) PehkuiCompat.scaleMatrices(matrixStack, player, tickDelta);
if (config().compatPehkui()) PehkuiCompat.scaleMatrices(matrixStack, player, tickDelta);

if (config.isUsingModModel()) {
if (config().isUsingModModel()) {
status = "Successful";
try {
matrixStack.push();
Expand Down Expand Up @@ -257,6 +258,6 @@ private static void virtualRender(MinecraftClient client, float tickDelta, Matri
playerModel.setAngles(player, o, n, l, k, m);
// AnimalModel.render
// ModelPart.render
config.getVanillaModelPart().get(playerRenderer.getModel()).rotate(matrixStack);
config().getVanillaModelPart().get(playerRenderer.getModel()).rotate(matrixStack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public class PehkuiCompat {

public static void scaleMatrices(MatrixStack matrixStack, Entity entity, float tickDelta) {
if (!loaded) return;
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);
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);
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);
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 @@ -52,7 +52,7 @@ public static <E extends Entity> void renderStart(EntityRenderDispatcher dispatc
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);
ReflectUtil.invokeMethod(OceanWorld_computeEntityOffset, oceanWorld, matrixStack.peek().getPositionMatrix(), matrixStack.peek().getNormalMatrix(), clientWorld, entity, x, y, z, 0.0d, 0.0d, 0.0d, yRot, renderPercent);
}
}
}
Loading

0 comments on commit d204614

Please sign in to comment.