Skip to content

Commit

Permalink
Upgrade to 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed Jun 14, 2024
1 parent af45841 commit 1aac5fc
Show file tree
Hide file tree
Showing 31 changed files with 297 additions and 308 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## [中文](README_ZH.md) ##

Make the camera more realistic in the first-person view.
Supported versions: 1.18-1.20 Forge & Fabric.
Supported versions: 1.20-1.21 Fabric & NeoForge.
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)

Expand All @@ -24,16 +24,15 @@ Snapshots are [here](https://github.com/xTracr/RealCamera/actions/workflows/buil
### 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)

## FAQ ##

* WIP
* A part of the model (e.g. hair) is always in the way, how to make it invisible?
* Increasing this value may help
![screenshot_2024_6_2_22_42](https://github.com/xTracr/RealCamera/assets/57320980/78c246e8-34aa-4979-89de-780ee907870b)

### Compatibility ###

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

使第一人称视角下的摄像头更加真实。
支持的版本: 1.18-1.20 Forge & Fabric
支持的版本: 1.20-1.21 Fabric & NeoForge
[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 @@ -24,16 +24,15 @@
### 依赖项目 ###

* 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:
* (可选但建议)[Cloth Config API](https://modrinth.com/mod/cloth-config)

## 常见问题 ##

* 施工中
* 模型的一部分(如头发)始终挡在面前,怎样隐藏它?
* 增加这个值或许有所帮助
![screenshot_2024_6_2_22_42](https://github.com/xTracr/RealCamera/assets/57320980/78c246e8-34aa-4979-89de-780ee907870b)

### 兼容性 ###

Expand Down
29 changes: 13 additions & 16 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ public static float getRoll(float f) {
return f;
}

public static Vec3 getPos(Vec3 vec3d) {
return new Vec3(currentTarget.bindX ? pos.x() : vec3d.x(), currentTarget.bindY ? pos.y() : vec3d.y(), currentTarget.bindZ ? pos.z() : vec3d.z());
public static Vec3 getPos(Vec3 vec) {
return new Vec3(currentTarget.bindX ? pos.x() : vec.x(), currentTarget.bindY ? pos.y() : vec.y(), currentTarget.bindZ ? pos.z() : vec.z());
}

public static Vec3 getCameraPos(Vec3 vec3d) {
return new Vec3(currentTarget.bindX ? cameraPos.x() : vec3d.x(), currentTarget.bindY ? cameraPos.y() : vec3d.y(), currentTarget.bindZ ? cameraPos.z() : vec3d.z());
public static Vec3 getCameraPos(Vec3 vec) {
return new Vec3(currentTarget.bindX ? cameraPos.x() : vec.x(), currentTarget.bindY ? cameraPos.y() : vec.y(), currentTarget.bindZ ? cameraPos.z() : vec.z());
}

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

public static void initialize(Minecraft client) {
Entity entity = client.getCameraEntity();
active = ConfigFile.config().enabled() && client.options.getCameraType().isFirstPerson() && client.gameRenderer.getMainCamera() != null && entity != null && !DisableHelper.isDisabled("mainFeature", entity);
active = ConfigFile.config().enabled() && client.options.getCameraType().isFirstPerson() && entity != null && !DisableHelper.isDisabled("mainFeature", entity);
rendering = active && ConfigFile.config().renderModel() && !DisableHelper.isDisabled("renderModel", entity);
}

Expand Down Expand Up @@ -88,31 +88,28 @@ public static void updateModel(Minecraft client, float tickDelta) {
recorder.buildRecords();
}

public static void renderCameraEntity(MultiBufferSource vertexConsumers, Matrix4f projectionMatrix) {
public static void renderCameraEntity(MultiBufferSource bufferSource, Matrix4f projectionMatrix) {
Vector3f vertexOffset = offset.subtract(pos).toVector3f();
Matrix3f normalMatrix = new Matrix3f().rotateZ((float) Math.toRadians(roll)).rotateX((float) Math.toRadians(pitch)).rotateY((float) Math.toRadians(yaw + 180.0f)).transpose().invert();
Matrix4f positionMatrix = new Matrix4f(normalMatrix).translate(vertexOffset);
final double m02 = positionMatrix.m02(), m12 = positionMatrix.m12(), m22 = positionMatrix.m22(), m32 = positionMatrix.m32();
normalMatrix.mulLocal(new Matrix3f(projectionMatrix).invert());
positionMatrix.set(normalMatrix).translate(vertexOffset);
recorder.drawRecords(record -> {
recorder.forEachRecord(record -> {
if (currentTarget.disabledTextureIds.stream().anyMatch(record.textureId()::contains)) return;
VertexConsumer buffer = vertexConsumers.getBuffer(record.renderLayer());
if (!record.renderLayer().canConsolidateConsecutiveGeometry()) {
for (VertexRecorder.Vertex vertex : record.vertices()) vertex.apply(buffer);
VertexConsumer buffer = bufferSource.getBuffer(record.renderType());
if (!record.renderType().canConsolidateConsecutiveGeometry()) {
VertexRecorder.renderVertices(record.vertices(), buffer);
return;
}
final double depth = currentTarget.disablingDepth;
boolean shouldDraw;
for (VertexRecorder.Vertex[] primitive : record.primitives()) {
shouldDraw = false;
for (VertexRecorder.Vertex vertex : primitive) {
if (Math.fma(m02, vertex.x(), Math.fma(m12, vertex.y(), Math.fma(m22, vertex.z(), m32))) < -depth) {
shouldDraw = true;
VertexRecorder.renderVertices(primitive, buffer, positionMatrix, normalMatrix);
break;
}
}
if (shouldDraw) for (VertexRecorder.Vertex vertex : primitive) vertex.transform(positionMatrix, normalMatrix).apply(buffer);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void onPress() {
}

@Override
public void renderWidget(GuiGraphics context, int mouseX, int mouseY, float delta) {
context.fill(getX(), getY(), getX() + getWidth(), getY() + getHeight(), 0xFF646464);
context.blit(texture, getX(), getY(), u, v + value * vOffset, width, height, textureWidth, textureHeight);
if (isHoveredOrFocused()) context.renderOutline(getX(), getY(), getWidth(), getHeight(), 0xFFFFFFFF);
public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
graphics.fill(getX(), getY(), getX() + getWidth(), getY() + getHeight(), 0xFF646464);
graphics.blit(texture, getX(), getY(), u, v + value * vOffset, width, height, textureWidth, textureHeight);
if (isHoveredOrFocused()) graphics.renderOutline(getX(), getY(), getWidth(), getHeight(), 0xFFFFFFFF);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import java.util.function.Function;

public class DoubleSliderWidget extends AbstractSliderButton {
public class DoubleSlider extends AbstractSliderButton {
private final Function<Double, Component> textFactory;
private final double min, max;

public DoubleSliderWidget(int width, int height, double value, double min, double max, Function<Double, Component> textFactory) {
public DoubleSlider(int width, int height, double value, double min, double max, Function<Double, Component> textFactory) {
super(0, 0, width, height, textFactory.apply(value), Mth.clamp(0, (value - min) / (max - min), 1));
this.textFactory = textFactory;
this.min = min;
Expand Down
60 changes: 28 additions & 32 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Set;

public class ModelAnalyser extends VertexRecorder {
private static final Set<RenderType> unfocusableLayers = Set.of(RenderType.armorEntityGlint(), RenderType.glintTranslucent(), RenderType.glint(), RenderType.entityGlint(), RenderType.entityGlintDirect());
private static final Set<RenderType> UNFOCUSABLE_RENDER_TYPES = Set.of(RenderType.armorEntityGlint(), RenderType.glintTranslucent(), RenderType.glint(), RenderType.entityGlint(), RenderType.entityGlintDirect());
private static final int primitiveArgb = 0x6F3333CC, forwardArgb = 0xFF00CC00, upwardArgb = 0xFFCC0000, leftArgb = 0xFF0000CC;
private static final int focusedArgb = 0x7FFFFFFF, sideArgb = 0x3FFFFFFF;
private final BindingTarget target;
Expand All @@ -38,19 +38,19 @@ private static boolean intersects(Vertex[] p1, List<Vertex[]> primitives) {
return false;
}

private static void drawPrimitive(GuiGraphics context, Vertex[] primitive, int argb, int offset) {
VertexConsumer vertexConsumer = context.bufferSource().getBuffer(RenderType.gui());
for (Vertex vertex : primitive) vertexConsumer.vertex(vertex.x(), vertex.y(), vertex.z() + offset).color(argb).endVertex();
if (primitive.length == 3) vertexConsumer.vertex(primitive[2].x(), primitive[2].y(), primitive[2].z() + offset).color(argb).endVertex();
context.flush();
private static void drawPrimitive(GuiGraphics graphics, Vertex[] primitive, int argb, int offset) {
VertexConsumer buffer = graphics.bufferSource().getBuffer(RenderType.gui());
for (Vertex vertex : primitive) buffer.addVertex(vertex.x(), vertex.y(), vertex.z() + offset).setColor(argb);
if (primitive.length == 3) buffer.addVertex(primitive[2].x(), primitive[2].y(), primitive[2].z() + offset).setColor(argb);
graphics.flush();
}

private static void drawNormal(GuiGraphics context, Vec3 start, Vec3 normal, int length, int argb) {
private static void drawNormal(GuiGraphics graphics, Vec3 start, Vec3 normal, int length, int argb) {
Vec3 end = normal.scale(length).add(start);
VertexConsumer vertexConsumer = context.bufferSource().getBuffer(RenderType.lineStrip());
vertexConsumer.vertex((float) start.x(), (float) start.y(), (float) (start.z() + 1200f)).color(argb).normal((float) normal.x(), (float) normal.y(), (float) normal.z()).endVertex();
vertexConsumer.vertex((float) end.x(), (float) end.y(), (float) (end.z() + 1200f)).color(argb).normal((float) normal.x(), (float) normal.y(), (float) normal.z()).endVertex();
context.flush();
VertexConsumer buffer = graphics.bufferSource().getBuffer(RenderType.lineStrip());
buffer.addVertex((float) start.x(), (float) start.y(), (float) (start.z() + 1200f)).setColor(argb).setNormal((float) normal.x(), (float) normal.y(), (float) normal.z());
buffer.addVertex((float) end.x(), (float) end.y(), (float) (end.z() + 1200f)).setColor(argb).setNormal((float) normal.x(), (float) normal.y(), (float) normal.z());
graphics.flush();
}

public void analyse(int entitySize, int mouseX, int mouseY, int layers, boolean hideDisabled, String idInField) {
Expand All @@ -61,7 +61,7 @@ public void analyse(int entitySize, int mouseX, int mouseY, int layers, boolean
}).toList();
records.removeAll(removedRecords);
List<Triple> sortByDepth = new ArrayList<>();
records.stream().filter(record -> !unfocusableLayers.contains(record.renderLayer())).forEach(record -> {
records.stream().filter(record -> !UNFOCUSABLE_RENDER_TYPES.contains(record.renderType())).forEach(record -> {
Vertex[][] primitives = record.primitives();
for (int i = 0, primitiveCount = primitives.length; i < primitiveCount; i++) {
Polygon polygon = new Polygon();
Expand Down Expand Up @@ -104,40 +104,36 @@ public Vec2 getFocusedUV() {
return new Vec2(u / primitive.length, v / primitive.length);
}

public void previewEffect(GuiGraphics context, int entitySize, boolean canSelect) {
defaultDraw(context.bufferSource());
context.flush();
if (canSelect) drawFocused(context);
public void previewEffect(GuiGraphics graphics, int entitySize, boolean canSelect) {
if (canSelect) drawFocused(graphics);
if (normal.m00() == 0 && normal.m11() == 0 && normal.m22() == 0) return;
Vec3 start = new Vec3(position);
drawNormal(context, start, new Vec3(normal.m20(), normal.m21(), normal.m22()), entitySize / 3, forwardArgb);
drawNormal(context, start, new Vec3(normal.m10(), normal.m11(), normal.m12()), entitySize / 6, upwardArgb);
drawNormal(context, start, new Vec3(normal.m00(), normal.m01(), normal.m02()), entitySize / 6, leftArgb);
drawNormal(graphics, start, new Vec3(normal.m20(), normal.m21(), normal.m22()), entitySize / 3, forwardArgb);
drawNormal(graphics, start, new Vec3(normal.m10(), normal.m11(), normal.m12()), entitySize / 6, upwardArgb);
drawNormal(graphics, start, new Vec3(normal.m00(), normal.m01(), normal.m02()), entitySize / 6, leftArgb);
}

public void drawModelWithNormals(GuiGraphics context, int entitySize) {
defaultDraw(context.bufferSource());
context.flush();
drawPolyhedron(context);
drawFocused(context);
public void drawNormals(GuiGraphics graphics, int entitySize) {
drawPolyhedron(graphics);
drawFocused(graphics);
if (currentRecord == null) return;
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);
if ((primitive = getPrimitive(currentRecord, target.posU, target.posV)) != null) drawPrimitive(graphics, primitive, primitiveArgb, 1000);
if ((primitive = getPrimitive(currentRecord, target.forwardU, target.forwardV)) != null) drawNormal(graphics, getPos(primitive, target.forwardU, target.forwardV), primitive[0].normal(), entitySize / 2, forwardArgb);
if ((primitive = getPrimitive(currentRecord, target.upwardU, target.upwardV)) != null) drawNormal(graphics, getPos(primitive, target.upwardU, target.upwardV), primitive[0].normal(), entitySize / 2, upwardArgb);
}

private void drawFocused(GuiGraphics context) {
private void drawFocused(GuiGraphics graphics) {
if (focusedIndex == -1 || focusedRecord == null) return;
Vertex[] focused = focusedRecord.primitives()[focusedIndex];
drawPrimitive(context, focused, focusedArgb, 1100);
drawPrimitive(graphics, 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);
drawPrimitive(graphics, reversed, focusedArgb, 1100);
}

private void drawPolyhedron(GuiGraphics context) {
private void drawPolyhedron(GuiGraphics graphics) {
if (focusedIndex == -1 || focusedRecord == null) return;
List<Vertex[]> polyhedron = new ArrayList<>();
polyhedron.add(focusedRecord.primitives()[focusedIndex]);
Expand All @@ -164,7 +160,7 @@ private void drawPolyhedron(GuiGraphics context) {
if (!indexes.contains(i)) break;
resultIndexes.add(i);
}
resultIndexes.forEach(i -> drawPrimitive(context, primitives[i], sideArgb, 1000));
resultIndexes.forEach(i -> drawPrimitive(graphics, primitives[i], sideArgb, 1000));
}

record Triple(double depth, BuiltRecord record, int index) {}
Expand Down
Loading

0 comments on commit 1aac5fc

Please sign in to comment.