Skip to content

Commit

Permalink
Continued to improve the clipping feature - #23, #32, #37
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed Dec 10, 2023
1 parent 7196e7a commit 32ad765
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 58 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ allprojects {
apply plugin: "architectury-plugin"
apply plugin: "maven-publish"

archivesBaseName = rootProject.archives_base_name
base {
archivesName = rootProject.archives_base_name
}

version = rootProject.mod_version
group = rootProject.maven_group

Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* 0.5.4-beta:
* Added feature #15, fixed #35
* Changed the way of clipping to space to avoid #23 and #32
* 0.5.5-beta:

* 0.4.0-alpha:
* Fixed an issue where the camera would not follow the model action when the player started/ended sneaking
Expand Down Expand Up @@ -99,6 +100,7 @@
* 0.5.4-beta:
* 添加了功能#15, 修复#35
* 修改了clip to space的方式来避免#23#32
* 0.5.5-beta:

* 0.4.0-alpha:
* 修复了玩家开始/结束潜行时摄像头未跟上模型动作的问题
Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ loom {

dependencies {

modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
modCompileOnly "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"

// Cloth Config
modCompileOnly("me.shedaniel.cloth:cloth-config:${rootProject.cloth_config_version}") {
Expand Down
21 changes: 13 additions & 8 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.*;
import net.minecraft.world.RaycastContext;
import org.joml.Matrix3f;
import org.joml.Vector4f;
Expand All @@ -31,6 +28,7 @@ public class RealCameraCore {

private static String status = "Successful";
private static float cameraRoll = 0.0F;
private static Vec3d modelOffset = Vec3d.ZERO;

public static String getStatus() {
return status;
Expand All @@ -40,6 +38,10 @@ public static float getRoll() {
return cameraRoll;
}

public static Vec3d getModelOffset() {
return modelOffset;
}

public static boolean isActive() {
MinecraftClient client = MinecraftClient.getInstance();
return config.isEnabled() && client.options.getPerspective().isFirstPerson() && client.gameRenderer.getCamera() != null
Expand All @@ -48,6 +50,7 @@ public static boolean isActive() {

public static void updateCamera(Camera camera, MinecraftClient client, float tickDelta) {
cameraRoll = 0.0F;
modelOffset = Vec3d.ZERO;

if (config.isRendering() && !config.disableRenderingWhen(client)) {
((CameraAccessor) camera).setThirdPerson(true);
Expand Down Expand Up @@ -125,7 +128,12 @@ private static void bindingModeUpdate(Camera camera, MinecraftClient client, flo

Vec3d referVec = camera.getPos();
((CameraAccessor) camera).invokeMoveBy(-offset.z(), offset.y(), -offset.x());
Vec3d prevPos = camera.getPos();
Box box = player.getBoundingBox();
double restrictedY = MathHelper.clamp(camera.getPos().getY(), box.minY + 0.1D, box.maxY - 0.1D);
referVec = new Vec3d(referVec.getX(), restrictedY, referVec.getZ());
clipCameraToSpace(camera, referVec);
modelOffset = camera.getPos().subtract(prevPos);

Matrix3f normal = matrixStack.peek().getNormalMatrix().scale(1.0F, -1.0F, -1.0F);
normal.rotateLocal((float) Math.toRadians(config.getBindingYaw()), normal.m10, normal.m11, normal.m12);
Expand All @@ -142,8 +150,7 @@ private static void bindingModeUpdate(Camera camera, MinecraftClient client, flo
private static void clipCameraToSpace(Camera camera, Vec3d referVec) {
if (!config.doClipToSpace()) return;
Vec3d offset = camera.getPos().subtract(referVec);
boolean hitted = false;
final float depth = 0.1F;
final float depth = 0.08F;
for (int i = 0; i < 8; ++i) {
float f = depth * ((i & 1) * 2 - 1);
float g = depth * ((i >> 1 & 1) * 2 - 1);
Expand All @@ -155,10 +162,8 @@ private static void clipCameraToSpace(Camera camera, Vec3d referVec) {
double l = hitResult.getPos().distanceTo(start);
if (hitResult.getType() == HitResult.Type.MISS || l >= offset.length()) continue;
offset = offset.multiply(l / offset.length());
hitted = true;
}
((CameraAccessor) camera).invokeSetPos(referVec.add(offset));
if (hitted && offset.length() <= 0.8F) ((CameraAccessor) camera).setThirdPerson(false);
}

private static void virtualRender(AbstractClientPlayerEntity player, PlayerEntityRenderer playerRenderer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static String getModelPartName() {
*/
public static boolean shouldDisableRender(String modelPartName) {
ModConfig.Disable.optionalParts.add(modelPartName);
return Flags.isRenderingWorld && config.shouldDisableRender(modelPartName) && RealCameraCore.isActive();
return Flags.isRenderingClientPlayer && config.shouldDisableRender(modelPartName) && RealCameraCore.isActive();
}

public static boolean virtualRender(float tickDelta, MatrixStack matrixStack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ public void adjustBindingZ(boolean add) {
}

// classic
public Classic.AdjustMode getClassicAdjustMode() {
return classic.adjustMode;
}

public double getClassicX() {
return classic.cameraX;
}
Expand Down Expand Up @@ -375,8 +371,8 @@ public static class Compats {
}

public static class Disable {
public static final Set<String> optionalParts = new HashSet<>(Set.of("head", "hat", "helmet"));
protected static final List<String> defaultParts = Arrays.asList("head", "hat", "helmet");
public static final Set<String> optionalParts = new HashSet<>(Set.of("head", "hat", "slot_head"));
protected static final List<String> defaultParts = Arrays.asList("head", "hat", "slot_head");
protected static final Triple<String, List<String>, List<String>> defaultTriple = new Triple<>
("holding", List.of("new item id"), List.of("new action"));
protected static final List<Triple<String, List<String>, List<String>>> defaultConditions = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xtracr.realcamera.mixins;

import com.xtracr.realcamera.api.VirtualRenderer;
import com.xtracr.realcamera.utils.Flags;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
Expand All @@ -14,7 +13,6 @@
public class MixinPlayerEntity {
@Inject(method = "getEquippedStack", at = @At("HEAD"), cancellable = true)
private void realCamera$onGetEquippedStackHEAD(EquipmentSlot slot, CallbackInfoReturnable<ItemStack> cInfo) {
if (!Flags.isRenderingClientPlayer) return;
if (VirtualRenderer.shouldDisableRender("slot_head") && slot == EquipmentSlot.HEAD ||
VirtualRenderer.shouldDisableRender("slot_chest") && slot == EquipmentSlot.CHEST ||
VirtualRenderer.shouldDisableRender("slot_legs") && slot == EquipmentSlot.LEGS ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xtracr.realcamera.mixins;

import com.xtracr.realcamera.RealCameraCore;
import com.xtracr.realcamera.api.VirtualRenderer;
import com.xtracr.realcamera.utils.Flags;
import net.minecraft.client.network.AbstractClientPlayerEntity;
Expand All @@ -10,10 +11,12 @@
import net.minecraft.client.render.entity.PlayerEntityRenderer;
import net.minecraft.client.render.entity.model.PlayerEntityModel;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.Vec3d;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerEntityRenderer.class)
public abstract class MixinPlayerEntityRenderer
Expand All @@ -35,9 +38,16 @@ public MixinPlayerEntityRenderer(Context ctx, PlayerEntityModel<AbstractClientPl
Flags.isRenderingClientPlayer = false;
}

@Inject(method = "getPositionOffset*", at = @At("RETURN"), cancellable = true)
private void realCamera$translateModel(AbstractClientPlayerEntity abstractClientPlayerEntity, float f, CallbackInfoReturnable<Vec3d> cInfo) {
if (RealCameraCore.isActive() && abstractClientPlayerEntity instanceof ClientPlayerEntity) {
Vec3d returnVec = cInfo.getReturnValue().add(RealCameraCore.getModelOffset());
cInfo.setReturnValue(returnVec);
}
}

@Inject(method = "setModelPose", at = @At("RETURN"))
private void realCamera$onSetModelPoseRETURN(AbstractClientPlayerEntity player, CallbackInfo cInfo) {
if (!Flags.isRenderingClientPlayer) return;
if (VirtualRenderer.shouldDisableRender("head")) model.head.visible = false;
if (VirtualRenderer.shouldDisableRender("hat")) model.hat.visible = false;
if (VirtualRenderer.shouldDisableRender("body")) model.body.visible = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xtracr.realcamera.mixins;

import com.xtracr.realcamera.api.VirtualRenderer;
import com.xtracr.realcamera.utils.Flags;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.feature.PlayerHeldItemFeatureRenderer;
import net.minecraft.client.render.model.json.ModelTransformationMode;
Expand All @@ -19,7 +18,6 @@ public abstract class MixinPlayerHeldItemFeatureRenderer {
@Inject(method = "renderItem", at = @At("HEAD"), cancellable = true)
private void realCamera$onRenderItemHEAD(LivingEntity entity, ItemStack stack, ModelTransformationMode transformationMode,
Arm arm, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, CallbackInfo cInfo) {
if (!Flags.isRenderingClientPlayer) return;
if (VirtualRenderer.shouldDisableRender("heldItem")) cInfo.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xtracr.realcamera.utils;

public class Flags {
public static boolean isRenderingWorld = false;
public static boolean isRenderingClientPlayer = false;
}
9 changes: 2 additions & 7 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id "com.github.johnrengelman.shadow" version "7.1.2"
}

archivesBaseName = rootProject.archivesBaseName + "-fabric"
archivesBaseName = rootProject.archives_base_name + "-" + rootProject.minecraft_version + "-fabric"

architectury {
platformSetupLoomIde()
Expand Down Expand Up @@ -48,18 +48,13 @@ shadowJar {
exclude "architectury.common.json"

configurations = [project.configurations.shadowCommon]
archiveClassifier.set("dev-shadow")
archiveClassifier = "dev-shadow"
}

remapJar {
injectAccessWidener = true
inputFile.set shadowJar.archiveFile
dependsOn shadowJar
archiveClassifier.set(null)
}

jar {
archiveClassifier.set("dev")
}

sourcesJar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

public class EventHandler {
public static void onWorldRenderStart(WorldRenderContext context) {
Flags.isRenderingWorld = true;
if (ConfigFile.modConfig.isCrosshairDynamic() && RealCameraCore.isActive()) {
CrosshairUtils.update(MinecraftClient.getInstance(), context.camera(),
context.matrixStack().peek().getPositionMatrix(), context.projectionMatrix());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xtracr.realcamera;

import com.xtracr.realcamera.utils.Flags;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -16,7 +15,6 @@ public void onInitializeClient() {

ClientTickEvents.END_CLIENT_TICK.register(KeyBindings::handle);
WorldRenderEvents.START.register(EventHandler::onWorldRenderStart);
WorldRenderEvents.END.register(context -> Flags.isRenderingWorld = false);

KeyBindingHelper.registerKeyBinding(KeyBindings.TOGGLE_PERSPECTIVE);
KeyBindingHelper.registerKeyBinding(KeyBindings.TOGGLE_ADJUST_MODE);
Expand Down
11 changes: 3 additions & 8 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id "com.github.johnrengelman.shadow" version "7.1.2"
}

archivesBaseName = rootProject.archivesBaseName + "-forge"
archivesBaseName = rootProject.archives_base_name + "-" + rootProject.minecraft_version + "-forge"

architectury {
platformSetupLoomIde()
Expand Down Expand Up @@ -36,7 +36,7 @@ dependencies {
modRuntimeOnly "me.shedaniel.cloth:cloth-config-forge:${rootProject.cloth_config_version}"

// Epic Fight
modCompileOnly "curse.maven:epic-fight-mod-405076:4902504"
//modCompileOnly "curse.maven:epic-fight-mod-405076:4902504"

common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionForge")) { transitive = false }
Expand All @@ -55,17 +55,12 @@ shadowJar {
exclude "architectury.common.json"

configurations = [project.configurations.shadowCommon]
archiveClassifier.set("dev-shadow")
archiveClassifier = "dev-shadow"
}

remapJar {
inputFile.set shadowJar.archiveFile
dependsOn shadowJar
archiveClassifier.set(null)
}

jar {
archiveClassifier.set("dev")
}

sourcesJar {
Expand Down
3 changes: 0 additions & 3 deletions forge/src/main/java/com/xtracr/realcamera/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ public static void onCameraUpdate(ComputeCameraAngles event) {
@SubscribeEvent
public static void onRenderWorldStage(RenderLevelStageEvent event) {
if (RenderLevelStageEvent.Stage.AFTER_SKY.equals(event.getStage())) {
Flags.isRenderingWorld = true;
if (ConfigFile.modConfig.isCrosshairDynamic() && RealCameraCore.isActive()) {
CrosshairUtils.update(MinecraftClient.getInstance(), event.getCamera(),
event.getPoseStack().peek().getPositionMatrix(), event.getProjectionMatrix());
}
} else if (RenderLevelStageEvent.Stage.AFTER_WEATHER.equals(event.getStage())) {
Flags.isRenderingWorld = false;
}
}
}
19 changes: 9 additions & 10 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
org.gradle.jvmargs=-Xmx4G
org.gradle.parallel=true
# Loader Properties
minecraft_version=1.20.2
minecraft_version=1.20.4
enabled_platforms=fabric,forge
forge_version=1.20.2-48.0.49
fabric_loader_version=0.15.0
yarn_mappings=1.20.2+build.4
forge_version=1.20.4-49.0.3
fabric_loader_version=0.15.1
yarn_mappings=1.20.4+build.1
# Mod Properties
modid=realcamera
mod_version=0.5.4-beta
mod_version=0.5.5-beta
maven_group=com.xtracr.realcamera
archives_base_name=realcamera-1.20.2
archives_base_name=realcamera
# Dependencies
fabric_api_version=0.91.1+1.20.2
cloth_config_version=12.0.111
modmenu_version=8.0.0-beta.2
fabric_api_version=0.91.2+1.20.4
cloth_config_version=13.0.114
modmenu_version=9.0.0-pre.1
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
8 changes: 6 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -130,10 +131,13 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pluginManagement {

include("common")
include("fabric")
include("forge")
//include("forge")

rootProject.name = "RealCamera"

0 comments on commit 32ad765

Please sign in to comment.