Skip to content

Commit

Permalink
Compatible with different draw modes
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed May 9, 2024
1 parent c14592d commit 80e0e1c
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 107 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

Make the camera more realistic in the first-person view.
Supported versions: 1.18-1.20 Forge & Fabric.
Download the mod
from [Releases](https://github.com/xTracr/RealCamera/releases), [Modrinth](https://modrinth.com/mod/real-camera)
or [CurseForge](https://curseforge.com/minecraft/mc-mods/real-camera)
Download the mod from [Releases](https://github.com/xTracr/RealCamera/releases), [Modrinth](https://modrinth.com/mod/real-camera) or [CurseForge](https://curseforge.com/minecraft/mc-mods/real-camera)
Snapshots are [here](https://github.com/xTracr/RealCamera/actions/workflows/build.yml)

## Features ##
Expand All @@ -17,21 +15,19 @@ Snapshots are [here](https://github.com/xTracr/RealCamera/actions/workflows/buil
* Use F6 to toggle the feature on or off and other hotkeys to adjust the camera.
* Configure these features in the config screen (Cloth Config required).
* Theoretically, most mod models are supported, but need to be configured manually:
* First, set the key binding for `Open Model View GUI`.
* First, set the key binding for `Open Model View Screen`.
* Open the model view gui and left click with left Alt held to select the corresponding face of the model, scroll with left Alt held to switch between the different layers of the model.
* By clicking the `Selecting` button on the left, switch between the three to select the `Forward Vector`, `Upward Vector`, and `Target Plane`.
* Enter the `Preview` section, where you can see the relative relationship between the camera and the model and make certain adjustments (you can also adjust through key bindings).
* Enter a name and save (if needed, other settings can be changed).
* Enter a name and save (if needed, other settings such as priority can be changed).

### Dependencies ###

* Fabric:
* [Fabric Loader](https://fabricmc.net/use/installer/)
* [Fabric API](https://modrinth.com/mod/fabric-api)

* Forge:
* [Forge Mod Loader](https://files.minecraftforge.net/)

* Both:
* (Optional but recommended) [Cloth Config API](https://modrinth.com/mod/cloth-config)

Expand All @@ -43,8 +39,8 @@ Snapshots are [here](https://github.com/xTracr/RealCamera/actions/workflows/buil

* Incompatible:
* OptiFine
* Armourer's Workshop
* Armors based on GeckoLib
* Compatible:
* most camera mods
* most player model mods

## [Change Log](changelog.md#English) ##
9 changes: 4 additions & 5 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

使第一人称视角下的摄像头更加真实。
支持的版本: 1.18-1.20 Forge & Fabric
[Releases](https://github.com/xTracr/RealCamera/releases)[Modrinth](https://modrinth.com/mod/real-camera)
[CurseForge](https://curseforge.com/minecraft/mc-mods/real-camera)下载
[Releases](https://github.com/xTracr/RealCamera/releases)[Modrinth](https://modrinth.com/mod/real-camera)[CurseForge](https://curseforge.com/minecraft/mc-mods/real-camera)下载
快照版在[这里](https://github.com/xTracr/RealCamera/actions/workflows/build.yml)

## 特性 ##
Expand All @@ -20,7 +19,7 @@
* 打开模型视图界面,左Alt+左键选择模型的对应的面,左Alt+滚轮可以在模型的不同层间切换
* 通过点击左侧的`选择`按钮,在三者间切换,选好`向前矢量``向上矢量``目标平面`
* 进入`预览`部分,在这里可以看到摄像头与模型的相对关系并进行一定的调整(也可以通过按键绑定调整)
* 输入名称并保存(如果需要,还有其他设置可以更改
* 输入名称并保存(如果需要,还有其他设置如优先级可以更改

### 依赖项目 ###

Expand All @@ -40,8 +39,8 @@

* 不兼容:
* OptiFine
* Armourer's Workshop
* 基于GeckoLib的盔甲
* 兼容:
* 多数摄像头模组
* 多数模型模组

## [更新日志](changelog.md#中文) ##
13 changes: 6 additions & 7 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ public static void renderCameraEntity(VertexConsumerProvider vertexConsumers) {
.rotate(RotationAxis.POSITIVE_Y.rotationDegrees(yaw + 180.0f))
.transpose().invert();
Matrix4f positionMatrix = new Matrix4f(normalMatrix).translate(offset.subtract(pos).toVector3f());
recorder.drawByAnother(vertexConsumers, record -> {
if (currentTarget.disabledTextureIds.stream().anyMatch(record.textureId()::contains)) return new VertexRecorder.Vertex[0][];
recorder.drawByAnother(vertexConsumers, record -> currentTarget.disabledTextureIds.stream().noneMatch(record.textureId()::contains), record -> {
final double depth = currentTarget.disablingDepth;
final int vertexCount = record.additionalVertexCount();
return Arrays.stream(record.vertices()).map(quad -> {
VertexRecorder.Vertex[] newQuad = new VertexRecorder.Vertex[vertexCount];
for (int j = 0; j < vertexCount ; j++) newQuad[j] = quad[j].transform(positionMatrix, normalMatrix);
for (VertexRecorder.Vertex vertex : newQuad) if (vertex.z() < -depth) return newQuad;
final int primitiveLength = record.primitiveLength();
return Arrays.stream(record.primitives()).map(primitive -> {
VertexRecorder.Vertex[] newPrimitive = new VertexRecorder.Vertex[primitiveLength];
for (int j = 0; j < primitiveLength ; j++) newPrimitive[j] = primitive[j].transform(positionMatrix, normalMatrix);
for (VertexRecorder.Vertex vertex : newPrimitive) if (vertex.z() < -depth) return newPrimitive;
return null;
}).filter(Objects::nonNull).toArray(VertexRecorder.Vertex[][]::new);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.lang.reflect.Method;
import java.util.function.Supplier;

public final class CompatibilityHelper {
public class CompatibilityHelper {
private static Class<?> NEA_NEAnimationsLoader = null;
private static Method NEA_playerTransformer_setDeltaTick = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

public final class DisableHelper {
public class DisableHelper {
private static final Map<String, Predicate<LivingEntity>> predicates = new HashMap<>();

public static void initialize() {
registerOr("mainFeature", LivingEntity::isSleeping);
registerOr("renderModel", entity -> entity instanceof PlayerEntity player && player.isUsingSpyglass());
registerOr("renderModel", entity -> ConfigFile.config().getDisableRenderItems().contains(getItemId(entity.getMainHandStack())));
registerOr("renderModel", entity -> ConfigFile.config().getDisableRenderItems().contains(getItemId(entity.getOffHandStack())));
registerOr("renderModel", entity -> ConfigFile.config().getDisableRenderItems().contains(Registries.ITEM.getId(entity.getMainHandStack().getItem()).toString()));
registerOr("renderModel", entity -> ConfigFile.config().getDisableRenderItems().contains(Registries.ITEM.getId(entity.getOffHandStack().getItem()).toString()));
}

public static void registerOr(String type, Predicate<LivingEntity> predicate) {
Expand All @@ -30,8 +29,4 @@ public static boolean isDisabled(String type, Entity cameraEntity) {
if (ConfigFile.config().isClassic() || predicate == null) return false;
return cameraEntity instanceof LivingEntity entity && predicate.test(entity);
}

private static String getItemId(ItemStack stack) {
return Registries.ITEM.getId(stack.getItem()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public static Screen create(Screen parent) {
.setTooltip(LocUtil.CONFIG_TOOLTIP("adjustOffset"))
.setSaveConsumer(b -> config.binding.adjustOffset = b)
.build());
binding.addEntry(entryBuilder.startBooleanToggle(LocUtil.CONFIG_OPTION("renderStuckObjects"), config.renderModel)
binding.addEntry(entryBuilder.startBooleanToggle(LocUtil.CONFIG_OPTION("renderStuckObjects"), config.binding.renderStuckObjects)
.setDefaultValue(true)
.setTooltip(LocUtil.CONFIG_TOOLTIP("renderStuckObjects"))
.setSaveConsumer(b -> config.renderModel = b)
.setSaveConsumer(b -> config.binding.renderStuckObjects = b)
.build());
binding.addEntry(entryBuilder.startStrList(LocUtil.CONFIG_OPTION("disableRenderItems"), config.binding.disableRenderItems)
.setDefaultValue(ModConfig.Binding.defaultDisableRenderItems)
Expand Down
70 changes: 35 additions & 35 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class ModelAnalyser extends VertexRecorder {
private static final Set<RenderLayer> unfocusableLayers = Set.of(RenderLayer.getArmorGlint(), RenderLayer.getArmorEntityGlint(), RenderLayer.getGlintTranslucent(), RenderLayer.getGlint(), RenderLayer.getDirectGlint(), RenderLayer.getEntityGlint(), RenderLayer.getDirectEntityGlint());
private static final int quadArgb = 0x6F3333CC, forwardArgb = 0xFF00CC00, upwardArgb = 0xFFCC0000, leftArgb = 0xFF0000CC;
private static final int primitiveArgb = 0x6F3333CC, forwardArgb = 0xFF00CC00, upwardArgb = 0xFFCC0000, leftArgb = 0xFF0000CC;
private static final int focusedArgb = 0x7FFFFFFF, sideArgb = 0x3FFFFFFF;
private final BindingTarget target;
private final Matrix3f normal = new Matrix3f();
Expand All @@ -32,16 +32,16 @@ public ModelAnalyser(BindingTarget target) {
this.target = target;
}

private static boolean intersects(Vertex[] quad, List<Vertex[]> quads) {
private static boolean intersects(Vertex[] p1, List<Vertex[]> primitives) {
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;
for (Vertex[] p2 : primitives) for (Vertex v1 : p1) for (Vertex v2 : p2) if (v1.pos().squaredDistanceTo(v2.pos()) < precision) return true;
return false;
}

private static void drawQuad(DrawContext context, Vertex[] quad, int argb, int offset) {
private static void drawPrimitive(DrawContext context, Vertex[] primitive, 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();
if (quad.length == 3) vertexConsumer.vertex(quad[2].x(), quad[2].y(), quad[2].z() + offset).color(argb).next();
for (Vertex vertex : primitive) vertexConsumer.vertex(vertex.x(), vertex.y(), vertex.z() + offset).color(argb).next();
if (primitive.length == 3) vertexConsumer.vertex(primitive[2].x(), primitive[2].y(), primitive[2].z() + offset).color(argb).next();
context.draw();
}

Expand All @@ -62,13 +62,13 @@ public void initialize(int entitySize, int mouseX, int mouseY, int layers, boole
records.removeAll(removedRecords);
List<Triple> sortByDepth = new ArrayList<>();
records.stream().filter(record -> !unfocusableLayers.contains(record.renderLayer())).forEach(record -> {
Vertex[][] vertices = record.vertices();
for (int i = 0, size = vertices.length; i < size; i++) {
Vertex[][] primitives = record.primitives();
for (int i = 0, primitiveCount = primitives.length; i < primitiveCount; i++) {
Polygon polygon = new Polygon();
Vertex[] quad = vertices[i];
for (Vertex vertex : quad) polygon.addPoint((int) vertex.x(), (int) vertex.y());
Vertex[] primitive = primitives[i];
for (Vertex vertex : primitive) polygon.addPoint((int) vertex.x(), (int) vertex.y());
if (!polygon.contains(mouseX, mouseY)) continue;
Vertex point = quad[0];
Vertex point = primitive[0];
double deltaZ = 0;
if (point.normalZ() != 0) deltaZ = (point.normalX() * (mouseX - point.x()) + point.normalY() * (mouseY - point.y())) / point.normalZ();
sortByDepth.add(new Triple(point.z() + deltaZ, record, i));
Expand Down Expand Up @@ -97,16 +97,16 @@ public String focusedTextureId() {
public Vec2f getFocusedUV() {
if (focusedIndex == -1 || focusedRecord == null) return null;
float u = 0, v = 0;
Vertex[] quad = focusedRecord.vertices()[focusedIndex];
for (Vertex vertex : quad) {
Vertex[] primitive = focusedRecord.primitives()[focusedIndex];
for (Vertex vertex : primitive) {
u += vertex.u();
v += vertex.v();
}
return new Vec2f(u / quad.length, v / quad.length);
return new Vec2f(u / primitive.length, v / primitive.length);
}

public void previewEffect(DrawContext context, int entitySize, boolean canSelect) {
drawByAnother(context.getVertexConsumers(), BuiltRecord::vertices);
drawByAnother(context.getVertexConsumers(), record -> true, BuiltRecord::primitives);
context.draw();
if (canSelect) drawFocused(context);
if (normal.m00() == 0 && normal.m11() == 0 && normal.m22() == 0) return;
Expand All @@ -117,55 +117,55 @@ public void previewEffect(DrawContext context, int entitySize, boolean canSelect
}

public void drawModelWithNormals(DrawContext context, int entitySize) {
drawByAnother(context.getVertexConsumers(), BuiltRecord::vertices);
drawByAnother(context.getVertexConsumers(), record -> true, BuiltRecord::primitives);
context.draw();
drawPolyhedron(context);
drawFocused(context);
if (currentRecord == null) return;
Vertex[] quad;
if ((quad = getQuad(currentRecord, target.posU, target.posV)) != null) drawQuad(context, quad, quadArgb, 1000);
if ((quad = getQuad(currentRecord, target.forwardU, target.forwardV)) != null) drawNormal(context, getPos(quad, target.forwardU, target.forwardV), quad[0].normal(), entitySize / 2, forwardArgb);
if ((quad = getQuad(currentRecord, target.upwardU, target.upwardV)) != null) drawNormal(context, getPos(quad, target.upwardU, target.upwardV), quad[0].normal(), entitySize / 2, upwardArgb);
Vertex[] primitive;
if ((primitive = getPrimitive(currentRecord, target.posU, target.posV)) != null) drawPrimitive(context, primitive, primitiveArgb, 1000);
if ((primitive = getPrimitive(currentRecord, target.forwardU, target.forwardV)) != null) drawNormal(context, getPos(primitive, target.forwardU, target.forwardV), primitive[0].normal(), entitySize / 2, forwardArgb);
if ((primitive = getPrimitive(currentRecord, target.upwardU, target.upwardV)) != null) drawNormal(context, getPos(primitive, target.upwardU, target.upwardV), primitive[0].normal(), entitySize / 2, upwardArgb);
}

private void drawFocused(DrawContext context) {
if (focusedIndex == -1 || focusedRecord == null) return;
Vertex[] focused = focusedRecord.vertices()[focusedIndex];
drawQuad(context, focused, focusedArgb, 1100);
int size = focused.length;
Vertex[] reversed = new Vertex[size];
for (int i = 0; i < size; i++) reversed[i] = focused[size - 1 - i];
drawQuad(context, reversed, focusedArgb, 1100);
Vertex[] focused = focusedRecord.primitives()[focusedIndex];
drawPrimitive(context, focused, focusedArgb, 1100);
int length = focused.length;
Vertex[] reversed = new Vertex[length];
for (int i = 0; i < length; i++) reversed[i] = focused[length - 1 - i];
drawPrimitive(context, reversed, focusedArgb, 1100);
}

private void drawPolyhedron(DrawContext context) {
if (focusedIndex == -1 || focusedRecord == null) return;
List<Vertex[]> polyhedron = new ArrayList<>();
polyhedron.add(focusedRecord.vertices()[focusedIndex]);
polyhedron.add(focusedRecord.primitives()[focusedIndex]);
List<Integer> indexes = new ArrayList<>(List.of(focusedIndex));
Vertex[][] vertices = focusedRecord.vertices();
Vertex[][] primitives = focusedRecord.primitives();
final int primitiveCount = primitives.length;
boolean added;
int size = focusedRecord.quadCount();
do {
added = false;
for (int i = 0; i < size; i++) {
Vertex[] quad = vertices[i];
if (indexes.contains(i) | !intersects(quad, polyhedron)) continue;
polyhedron.add(quad);
for (int i = 0; i < primitiveCount; i++) {
Vertex[] primitive = primitives[i];
if (indexes.contains(i) | !intersects(primitive, polyhedron)) continue;
polyhedron.add(primitive);
indexes.add(i);
added = true;
}
} while (added);
List<Integer> resultIndexes = new ArrayList<>(List.of(focusedIndex));
for (int i = focusedIndex + 1; i < size; i++) {
for (int i = focusedIndex + 1; i < primitiveCount; i++) {
if (!indexes.contains(i)) break;
resultIndexes.add(i);
}
for (int i = focusedIndex - 1; i >= 0; i--) {
if (!indexes.contains(i)) break;
resultIndexes.add(i);
}
resultIndexes.forEach(i -> drawQuad(context, vertices[i], sideArgb, 1000));
resultIndexes.forEach(i -> drawPrimitive(context, primitives[i], sideArgb, 1000));
}

record Triple(double depth, BuiltRecord record, int index) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ protected BindingTarget generateBindingTarget() {

protected void loadBindingTarget(BindingTarget target) {
if (target.isEmpty()) return;
disabledIds.clear();
disabledIds.addAll(target.disabledTextureIds);
nameField.setText(target.name);
textureIdField.setText(target.textureId);
priorityField.setValue(target.priority);
Expand All @@ -320,6 +318,8 @@ protected void loadBindingTarget(BindingTarget target) {
pitchSlider.setValue(target.pitch);
yawSlider.setValue(target.yaw);
rollSlider.setValue(target.roll);
disabledIds.clear();
disabledIds.addAll(target.disabledTextureIds);
}

private ButtonWidget createButton(Text message, int width, ButtonWidget.PressAction onPress) {
Expand Down
Loading

0 comments on commit 80e0e1c

Please sign in to comment.