Skip to content

Commit

Permalink
Merge pull request #18 from der-fruhling-entertainment/feat/camera-ro…
Browse files Browse the repository at this point in the history
…lling

Implement vertical camera roll & improve vertical camera movement
  • Loading branch information
der-fruhling authored Apr 22, 2024
2 parents 1cfae5b + 4a1d7b6 commit d34368a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.derfruhling.minecraft.create.trainperspective;

public interface Camera3D {
float getZRot();
void setRotation3D(float y, float x, float z);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.derfruhling.minecraft.create.trainperspective;

import net.derfruhling.minecraft.create.trainperspective.mixin.CameraMixin;
import net.minecraft.client.Camera;

public class MixUtil {
private MixUtil() {}

public static Camera3D asCamera3D(Camera camera) {
return (Camera3D) camera;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
package net.derfruhling.minecraft.create.trainperspective.mixin;

import net.derfruhling.minecraft.create.trainperspective.Camera3D;
import net.derfruhling.minecraft.create.trainperspective.PlayerPerspectiveBehavior;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(Camera.class)
public class CameraMixin {
@Implements({@Interface(iface = Camera3D.class, prefix = "ctp$")})
public abstract class CameraMixin {
@Shadow private Entity entity;
@Shadow private float xRot;
@Shadow private float yRot;
@Unique private float ctp$zRot;

@ModifyArg(method = "setup", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Camera;setRotation(FF)V", ordinal = 0), index = 1)
public float modifyXRotPrimary(float xRot) {
if(entity instanceof LocalPlayer player) {
var persp = (PlayerPerspectiveBehavior) Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player);
return xRot - persp.getLean();
} else return xRot;
@Shadow @Final private Quaternionf rotation;
@Shadow @Final private Vector3f forwards;
@Shadow @Final private Vector3f up;
@Shadow @Final private Vector3f left;

@Shadow protected abstract void setRotation(float f, float g);

@Unique
public void ctp$setRotation3D(float y, float x, float z) {
this.xRot = x;
this.yRot = y;
this.ctp$zRot = z;
this.rotation.rotationYXZ(-y * 0.017453292F, x * 0.017453292F, z * 0.017453292F);
this.forwards.set(0.0F, 0.0F, 1.0F).rotate(this.rotation);
this.up.set(0.0F, 1.0F, 0.0F).rotate(this.rotation);
this.left.set(1.0F, 0.0F, 0.0F).rotate(this.rotation);
}

@Unique
public float ctp$getZRot() {
return this.ctp$zRot;
}

@ModifyArg(method = "setup", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Camera;setRotation(FF)V", ordinal = 1), index = 1)
public float modifyXRotThirdPersonMirrored(float invertedXRot) {
@Redirect(method = "setup", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Camera;setRotation(FF)V"))
public void modifyRotationsPrimary(Camera instance, float y, float x) {
if(entity instanceof LocalPlayer player) {
var persp = (PlayerPerspectiveBehavior) Minecraft.getInstance().getEntityRenderDispatcher().getRenderer(player);
return invertedXRot + persp.getLean();
} else return invertedXRot;
ctp$setRotation3D(y,
x + persp.getLean() * Mth.cos(y * Mth.DEG_TO_RAD),
360.0f - (persp.getLean() * Mth.sin(y * Mth.DEG_TO_RAD)));
} else setRotation(y, x);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.derfruhling.minecraft.create.trainperspective.mixin;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.derfruhling.minecraft.create.trainperspective.MixUtil;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.GameRenderer;
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(GameRenderer.class)
public class GameRendererMixin {
@Shadow @Final private Camera mainCamera;

@Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/PoseStack;mulPose(Lorg/joml/Quaternionf;)V", ordinal = 3, shift = At.Shift.AFTER))
public void applyZRotation(float f, long l, PoseStack poseStack, CallbackInfo ci) {
poseStack.mulPose(Axis.ZP.rotationDegrees(MixUtil.asCamera3D(mainCamera).getZRot()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"package": "net.derfruhling.minecraft.create.trainperspective.mixin",
"compatibilityLevel": "JAVA_8",
"client": [
"PlayerRendererMixin",
"CameraMixin",
"ContraptionColliderMixin",
"CameraMixin"
"GameRendererMixin",
"PlayerRendererMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit d34368a

Please sign in to comment.