Skip to content

Commit

Permalink
Added 'Update Model Again' option
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed Mar 8, 2024
1 parent 7f183f7 commit 6d7a9a2
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 143 deletions.
53 changes: 27 additions & 26 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ public class RealCameraCore {
private static float pitch, yaw, roll;

public static float getPitch(float f) {
if (currentTarget.bindRotation()) return pitch;
if (currentTarget.bindRotation) return pitch;
return f;
}

public static float getYaw(float f) {
if (currentTarget.bindRotation()) return yaw;
if (currentTarget.bindRotation) return yaw;
return f;
}

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

public static Vec3d getPos(Vec3d vec3d) {
return new Vec3d(currentTarget.bindX() ? pos.getX() : vec3d.getX(), currentTarget.bindY() ? pos.getY() : vec3d.getY(), currentTarget.bindZ() ? pos.getZ() : vec3d.getZ());
return new Vec3d(currentTarget.bindX ? pos.getX() : vec3d.getX(), currentTarget.bindY ? pos.getY() : vec3d.getY(), currentTarget.bindZ ? pos.getZ() : vec3d.getZ());
}

public static Vec3d getCameraPos(Vec3d vec3d) {
return new Vec3d(currentTarget.bindX() ? cameraPos.getX() : vec3d.getX(), currentTarget.bindY() ? cameraPos.getY() : vec3d.getY(), currentTarget.bindZ() ? cameraPos.getZ() : vec3d.getZ());
return new Vec3d(currentTarget.bindX ? cameraPos.getX() : vec3d.getX(), currentTarget.bindY ? cameraPos.getY() : vec3d.getY(), currentTarget.bindZ ? cameraPos.getZ() : vec3d.getZ());
}

public static void setCameraPos(Vec3d vec3d) {
Expand All @@ -62,28 +62,10 @@ public static boolean isActive() {
return active;
}

public static void renderEntity(VertexConsumerProvider vertexConsumers) {
Matrix3f normalMatrix = new Matrix3f().rotate(RotationAxis.POSITIVE_Z.rotationDegrees(roll))
.rotate(RotationAxis.POSITIVE_X.rotationDegrees(pitch))
.rotate(RotationAxis.POSITIVE_Y.rotationDegrees(yaw + 180.0f))
.transpose().invert();
Matrix4f positionMatrix = new Matrix4f(normalMatrix).translate((float) -pos.getX(), (float) -pos.getY(), (float) -pos.getZ());
BiFunction<RenderLayer, VertexRecorder.Vertex[], VertexRecorder.Vertex[]> function = (renderLayer, vertices) -> {
double depth = currentTarget.disablingDepth();
int count = vertices.length;
VertexRecorder.Vertex[] quad = new VertexRecorder.Vertex[count];
for (int i = 0; i < count; i++) quad[i] = vertices[i].transform(positionMatrix, normalMatrix);
for (VertexRecorder.Vertex vertex : quad) if (vertex.z() < -depth) return quad;
return null;
};
recorder.drawByAnother(vertexConsumers, renderLayer -> true, function);
}

public static void computeCamera(MinecraftClient client, float tickDelta) {
// GameRenderer.renderWorld
public static void updateModel(MinecraftClient client, float tickDelta) {
recorder.clear();
Entity entity = client.getCameraEntity();
// WorldRenderer.render
Entity entity = client.getCameraEntity();
EntityRenderDispatcher dispatcher = client.getEntityRenderDispatcher();
dispatcher.configure(client.world, client.gameRenderer.getCamera(), client.targetedEntity);
if (entity.age == 0) {
Expand All @@ -98,11 +80,30 @@ public static void computeCamera(MinecraftClient client, float tickDelta) {
MathHelper.lerp(tickDelta, entity.prevYaw, entity.getYaw()),
tickDelta, new MatrixStack(), recorder, dispatcher.getLight(entity, tickDelta));
recorder.buildLastRecord();
}

public static void renderCameraEntity(VertexConsumerProvider vertexConsumers) {
Matrix3f normalMatrix = new Matrix3f().rotate(RotationAxis.POSITIVE_Z.rotationDegrees(roll))
.rotate(RotationAxis.POSITIVE_X.rotationDegrees(pitch))
.rotate(RotationAxis.POSITIVE_Y.rotationDegrees(yaw + 180.0f))
.transpose().invert();
Matrix4f positionMatrix = new Matrix4f(normalMatrix).translate((float) -pos.getX(), (float) -pos.getY(), (float) -pos.getZ());
BiFunction<RenderLayer, VertexRecorder.Vertex[], VertexRecorder.Vertex[]> function = (renderLayer, vertices) -> {
double depth = currentTarget.disablingDepth;
int count = vertices.length;
VertexRecorder.Vertex[] quad = new VertexRecorder.Vertex[count];
for (int i = 0; i < count; i++) quad[i] = vertices[i].transform(positionMatrix, normalMatrix);
for (VertexRecorder.Vertex vertex : quad) if (vertex.z() < -depth) return quad;
return null;
};
recorder.drawByAnother(vertexConsumers, renderLayer -> true, function);
}

public static void computeCamera() {
currentTarget = new BindingTarget();
Matrix3f normal = new Matrix3f();
for (BindingTarget target : config().getTargetList()) {
if (!recorder.setCurrent(renderLayer -> renderLayer.toString().contains(target.textureId()))) continue;
if (!recorder.setCurrent(renderLayer -> renderLayer.toString().contains(target.textureId))) continue;
try {
pos = recorder.getTargetPosAndRot(target, normal);
currentTarget = target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class BindingTarget {
createDefaultTarget("skin_head", "minecraft:skins/", 5, false),
createDefaultTarget("minecraft_head_2", "minecraft:textures/entity/player/", 0, true),
createDefaultTarget("skin_head_2", "minecraft:skins/", 0, true));
private final String name, textureId;
private final int priority;
private final float forwardU, forwardV, upwardU, upwardV, posU, posV, disablingDepth;
private final boolean bindX, bindY, bindZ, bindRotation;
protected double scale, offsetX, offsetY, offsetZ;
protected float pitch, yaw, roll;
public final String name, textureId;
public final int priority;
public final float forwardU, forwardV, upwardU, upwardV, posU, posV, disablingDepth;
public final boolean bindX, bindY, bindZ, bindRotation;
public double scale, offsetX, offsetY, offsetZ;
public float pitch, yaw, roll;

public BindingTarget() {
this(null, null, 0, 0, 0, 0, 0, 0, 0, 0.2f, false, true, false, false, 1, 0, 0, 0, 0, 0, 0);
Expand Down Expand Up @@ -52,70 +52,6 @@ public boolean isEmpty() {
return name == null;
}

public String name() {
return name;
}

public String textureId() {
return textureId;
}

public int priority() {
return priority;
}

public float forwardU() {
return forwardU;
}

public float forwardV() {
return forwardV;
}

public float upwardU() {
return upwardU;
}

public float upwardV() {
return upwardV;
}

public float posU() {
return posU;
}

public float posV() {
return posV;
}

public float disablingDepth() {
return disablingDepth;
}

public boolean bindX() {
return bindX;
}

public boolean bindY() {
return bindY;
}

public boolean bindZ() {
return bindZ;
}

public boolean bindRotation() {
return bindRotation;
}

public double scale() {
return scale;
}

public void setScale(double scale) {
this.scale = scale;
}

public double offsetX() {
return offsetX * scale;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public static Screen create(Screen parent) {
.setTooltip(Text.translatable(TOOLTIP + "adjustOffset"))
.setSaveConsumer(b -> config.binding.adjustOffset = b)
.build());
binding.addEntry(entryBuilder.startBooleanToggle(Text.translatable(OPTION + "updateModel"), config.binding.updateModel)
.setDefaultValue(true)
.setTooltip(Text.translatable(TOOLTIP + "updateModel"))
.setSaveConsumer(b -> config.binding.updateModel = b)
.build());

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public void adjustOffsetZ(int count) {
}

// binding
public boolean updateModelAgain() {
return binding.updateModel;
}

public List<BindingTarget> getTargetList() {
binding.clamp();
return binding.targetList;
Expand All @@ -158,10 +162,10 @@ public List<BindingTarget> getTargetList() {
public void putTarget(BindingTarget target) {
if (target.isEmpty()) return;
IntStream.range(0, binding.targetList.size())
.filter(i -> binding.targetList.get(i).name().equals(target.name()))
.filter(i -> binding.targetList.get(i).name.equals(target.name))
.findAny()
.ifPresentOrElse(i -> binding.targetList.set(i, target), () -> binding.targetList.add(target));
binding.targetList.sort(Comparator.comparingInt(t -> -t.priority()));
binding.targetList.sort(Comparator.comparingInt(t -> -t.priority));
}

public static class Classic {
Expand Down Expand Up @@ -204,6 +208,7 @@ public AdjustMode cycle() {

public static class Binding {
public boolean adjustOffset = true;
public boolean updateModel = true;
public List<BindingTarget> targetList = new ArrayList<>(BindingTarget.defaultTargets);

private void clamp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import java.util.function.Function;

public class DoubleSliderWidget extends SliderWidget {
private final Function<Double, Text> textFactory;
private final double min, max;
private final Function<Double, Text> textFunction;

public DoubleSliderWidget(int width, int height, double value, double min, double max, Function<Double, Text> textFunction) {
super(0, 0, width, height, textFunction.apply(value), MathHelper.clamp(0, (value - min) / (max - min), 1));
public DoubleSliderWidget(int width, int height, double value, double min, double max, Function<Double, Text> textFactory) {
super(0, 0, width, height, textFactory.apply(value), MathHelper.clamp(0, (value - min) / (max - min), 1));
this.textFactory = textFactory;
this.min = min;
this.max = max;
this.textFunction = textFunction;
}

public double getValue() {
Expand All @@ -28,7 +28,7 @@ public void setValue(double value) {

@Override
protected void updateMessage() {
setMessage(textFunction.apply(getValue()));
setMessage(textFactory.apply(getValue()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public int getFocusedIndex(int mouseX, int mouseY, int layers) {
public void preview(DrawContext context, BindingTarget target, int entitySize, int forwardArgb, int upwardArgb, int leftArgb) {
drawByAnother(context.getVertexConsumers());
context.draw();
target.setScale(entitySize * target.scale());
target.scale *= entitySize;
Matrix3f normal = new Matrix3f();
Vec3d pos;
try {
Expand Down
44 changes: 22 additions & 22 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelViewScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void initRightWidgets(final int page) {
addDrawableChild(new TexturedButton(x + xSize - 21, y + 4, 16, 16, 16, 32, button -> initWidgets(category, (page + 1) % pages)));
for (int i = page * widgetsPerPage; i < Math.min((page + 1) * widgetsPerPage, targetList.size()); i++) {
BindingTarget target = targetList.get(i);
String name = target.name();
String name = target.name;
adder.add(createButton(Text.literal(name), widgetWidth * 2 - 18, button -> loadBindingTarget(target)));
adder.add(new TexturedButton(32, 32, button -> {
targetList.remove(target);
Expand Down Expand Up @@ -262,27 +262,27 @@ protected BindingTarget generateBindingTarget() {

protected void loadBindingTarget(BindingTarget target) {
if (target.isEmpty()) return;
nameField.setText(target.name());
textureIdField.setText(target.textureId());
priorityField.setValue(target.priority());
forwardUField.setValue(target.forwardU());
forwardVField.setValue(target.forwardV());
upwardUField.setValue(target.upwardU());
upwardVField.setValue(target.upwardV());
posUField.setValue(target.posU());
posVField.setValue(target.posV());
depthField.setValue(target.disablingDepth());
scaleField.setValue((float) target.scale());
bindXButton.setValue(target.bindX() ? 0 : 1);
offsetXSlider.setValue(target.offsetX());
bindYButton.setValue(target.bindY() ? 0 : 1);
offsetYSlider.setValue(target.offsetY());
bindZButton.setValue(target.bindZ() ? 0 : 1);
offsetZSlider.setValue(target.offsetZ());
bindRotButton.setValue(target.bindRotation() ? 0 : 1);
pitchSlider.setValue(target.pitch());
yawSlider.setValue(target.yaw());
rollSlider.setValue(target.roll());
nameField.setText(target.name);
textureIdField.setText(target.textureId);
priorityField.setValue(target.priority);
forwardUField.setValue(target.forwardU);
forwardVField.setValue(target.forwardV);
upwardUField.setValue(target.upwardU);
upwardVField.setValue(target.upwardV);
posUField.setValue(target.posU);
posVField.setValue(target.posV);
depthField.setValue(target.disablingDepth);
scaleField.setValue((float) target.scale);
bindXButton.setValue(target.bindX ? 0 : 1);
offsetXSlider.setValue(target.offsetX);
bindYButton.setValue(target.bindY ? 0 : 1);
offsetYSlider.setValue(target.offsetY);
bindZButton.setValue(target.bindZ ? 0 : 1);
offsetZSlider.setValue(target.offsetZ);
bindRotButton.setValue(target.bindRotation ? 0 : 1);
pitchSlider.setValue(target.pitch);
yawSlider.setValue(target.yaw);
rollSlider.setValue(target.roll);
}

private ButtonWidget createButton(Text message, int width, ButtonWidget.PressAction onPress) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class MixinCamera {
private float yaw;

@Inject(method = "update", at = @At("RETURN"))
private void realCamera$updateCamera(BlockView area, Entity focusedEntity, boolean thirdPerson, boolean inverseView, float tickDelta, CallbackInfo cInfo) {
private void realcamera$updateCamera(BlockView area, Entity focusedEntity, boolean thirdPerson, boolean inverseView, float tickDelta, CallbackInfo cInfo) {
if (!RealCameraCore.isActive()) return;
final ModConfig config = ConfigFile.modConfig;
Vec3d startVec = pos;
Expand All @@ -57,12 +57,12 @@ public abstract class MixinCamera {
setPos(prevPos);
setRotation(RealCameraCore.getYaw(yaw), RealCameraCore.getPitch(pitch));
}
realCamera$clipToSpace(startVec);
realcamera$clipToSpace(startVec);
RealCameraCore.setCameraPos(pos);
}

@Unique
private void realCamera$clipToSpace(Vec3d startVec) {
private void realcamera$clipToSpace(Vec3d startVec) {
Vec3d offset = pos.subtract(startVec);
final float depth = 0.085F;
for (int i = 0; i < 8; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class MixinGameRenderer {
@Final private Camera camera;

@ModifyVariable(method = "updateTargetedEntity", at = @At("STORE"), ordinal = 0)
private EntityHitResult realCamera$modifyEntityHitResult(EntityHitResult entityHitResult) {
private EntityHitResult realcamera$modifyEntityHitResult(EntityHitResult entityHitResult) {
CrosshairUtil.capturedEntityHitResult = entityHitResult;
if (!ConfigFile.modConfig.isCrosshairDynamic() && RealCameraCore.isActive()) {
Vec3d startVec = RaycastUtil.getStartVec();
Expand All @@ -43,15 +43,16 @@ public abstract class MixinGameRenderer {
}

@Inject(method = "renderWorld", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/Camera;update(Lnet/minecraft/world/BlockView;Lnet/minecraft/entity/Entity;ZZF)V"))
private void realCamera$onBeforeCameraUpdate(float tickDelta, long limitTime, MatrixStack matrices, CallbackInfo cInfo) {
private void realcamera$onBeforeCameraUpdate(float tickDelta, long limitTime, MatrixStack matrices, CallbackInfo cInfo) {
RealCameraCore.init(client);
if (RealCameraCore.isActive() && !ConfigFile.modConfig.isClassic()) {
RealCameraCore.computeCamera(client, tickDelta);
RealCameraCore.updateModel(client, tickDelta);
RealCameraCore.computeCamera();
}
}

@Inject(method = "renderWorld", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/WorldRenderer;setupFrustum(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/util/math/Vec3d;Lorg/joml/Matrix4f;)V"))
private void realCamera$onBeforeSetupFrustum(CallbackInfo cInfo) {
private void realcamera$onBeforeSetupFrustum(CallbackInfo cInfo) {
if (RealCameraCore.isActive() && !ConfigFile.modConfig.isClassic()) {
((CameraAccessor) camera).invokeSetPos(RealCameraCore.getCameraPos(camera.getPos()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
@Mixin(InGameHud.class)
public abstract class MixinInGameHud {
@Inject(method = "renderCrosshair(Lnet/minecraft/client/gui/DrawContext;)V", at = @At("HEAD"))
private void realCamera$onRenderCrosshairHEAD(DrawContext context, CallbackInfo cInfo) {
private void realcamera$onRenderCrosshairHEAD(DrawContext context, CallbackInfo cInfo) {
if (ConfigFile.modConfig.isCrosshairDynamic() && RealCameraCore.isActive()) {
context.getMatrices().push();
CrosshairUtil.translateMatrices(context.getMatrices());
}
}

@Inject(method = "renderCrosshair(Lnet/minecraft/client/gui/DrawContext;)V", at = @At("RETURN"))
private void realCamera$onRenderCrosshairRETURN(DrawContext context, CallbackInfo cInfo) {
private void realcamera$onRenderCrosshairRETURN(DrawContext context, CallbackInfo cInfo) {
if (ConfigFile.modConfig.isCrosshairDynamic() && RealCameraCore.isActive()) {
context.getMatrices().pop();
}
Expand Down
Loading

0 comments on commit 6d7a9a2

Please sign in to comment.