Skip to content

Commit

Permalink
Compatible with union VertexConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed May 7, 2024
1 parent 923e52d commit 7fb7807
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 85 deletions.
1 change: 0 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ loom {
}

dependencies {

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

// Cloth Config
Expand Down
35 changes: 18 additions & 17 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.joml.Matrix4f;
import org.joml.Vector3f;

import java.util.Arrays;
import java.util.Objects;

public class RealCameraCore {
private static final VertexRecorder recorder = new VertexRecorder();
public static BindingTarget currentTarget = new BindingTarget();
Expand Down Expand Up @@ -83,12 +86,9 @@ public static void updateModel(MinecraftClient client, float tickDelta) {
entity.lastRenderZ = entity.getZ();
}
// WorldRenderer.renderEntity
offset = new Vec3d(MathHelper.lerp(tickDelta, entity.lastRenderX, entity.getX()),
MathHelper.lerp(tickDelta, entity.lastRenderY, entity.getY()),
MathHelper.lerp(tickDelta, entity.lastRenderZ, entity.getZ()));
dispatcher.render(entity, 0, 0, 0, MathHelper.lerp(tickDelta, entity.prevYaw, entity.getYaw()),
tickDelta, new MatrixStack(), recorder, dispatcher.getLight(entity, tickDelta));
recorder.buildLastRecord();
offset = new Vec3d(MathHelper.lerp(tickDelta, entity.lastRenderX, entity.getX()), MathHelper.lerp(tickDelta, entity.lastRenderY, entity.getY()), MathHelper.lerp(tickDelta, entity.lastRenderZ, entity.getZ()));
dispatcher.render(entity, 0, 0, 0, MathHelper.lerp(tickDelta, entity.prevYaw, entity.getYaw()), tickDelta, new MatrixStack(), recorder, dispatcher.getLight(entity, tickDelta));
recorder.buildRecords();
}

public static void renderCameraEntity(VertexConsumerProvider vertexConsumers) {
Expand All @@ -97,24 +97,25 @@ 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,
renderLayer -> currentTarget.disabledTextureIds.stream().noneMatch(renderLayer.toString()::contains),
(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, record -> {
if (currentTarget.disabledTextureIds.stream().anyMatch(record.textureId()::contains)) return new VertexRecorder.Vertex[0][];
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;
return null;
}).filter(Objects::nonNull).toArray(VertexRecorder.Vertex[][]::new);
});
}

public static void computeCamera() {
currentTarget = new BindingTarget();
Matrix3f normal = new Matrix3f();
for (BindingTarget target : ConfigFile.config().getTargetList()) {
Vector3f position = new Vector3f();
if (recorder.getTargetPosAndRot(target, normal, position) == null || !(Math.abs(normal.determinant() - 1) <= 0.01f)) continue;
if (recorder.getTargetPosAndRot(target, normal, position) == null || !(Math.abs(normal.determinant() - 1) <= 0.01f) || !Float.isFinite(position.lengthSquared())) continue;
pos = new Vec3d(position).add(offset);
currentTarget = target;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -16,8 +17,8 @@ public final class DisableHelper {
public static void initialize() {
registerOr("mainFeature", LivingEntity::isSleeping);
registerOr("renderModel", entity -> entity instanceof PlayerEntity player && player.isUsingSpyglass());
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()));
registerOr("renderModel", entity -> ConfigFile.config().getDisableRenderItems().contains(getItemId(entity.getMainHandStack())));
registerOr("renderModel", entity -> ConfigFile.config().getDisableRenderItems().contains(getItemId(entity.getOffHandStack())));
}

public static void registerOr(String type, Predicate<LivingEntity> predicate) {
Expand All @@ -29,4 +30,8 @@ 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 @@ -32,7 +32,7 @@ private static BindingTarget createDefaultTarget(String name, String textureId,
.upwardU(0.1875f).upwardV(0.075f)
.posU(0.1875f).posV(0.2f)
.bindX(shouldBind).bindZ(shouldBind).bindRotation(shouldBind)
.offsetX(-0.12)
.offsetX(-0.1)
.disabledTextureIds(List.of("minecraft:textures/entity/enderdragon/dragon.png"));
}

Expand Down
34 changes: 15 additions & 19 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

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 focusedArgb = 0x7FFFFFFF, sideArgb = 0x3FFFFFFF;
private final BindingTarget target;
Expand Down Expand Up @@ -46,22 +48,20 @@ private static void drawQuad(DrawContext context, Vertex[] quad, int argb, int o
private static void drawNormal(DrawContext context, Vec3d start, Vec3d normal, int length, int argb) {
Vec3d end = normal.multiply(length).add(start);
VertexConsumer vertexConsumer = context.getVertexConsumers().getBuffer(RenderLayer.getLineStrip());
vertexConsumer.vertex(start.getX(), start.getY(), start.getZ() + 1200f).color(argb)
.normal((float) normal.getX(), (float) normal.getY(), (float) normal.getZ()).next();
vertexConsumer.vertex(end.getX(), end.getY(), end.getZ() + 1200f).color(argb)
.normal((float) normal.getX(), (float) normal.getY(), (float) normal.getZ()).next();
vertexConsumer.vertex(start.getX(), start.getY(), start.getZ() + 1200f).color(argb).normal((float) normal.getX(), (float) normal.getY(), (float) normal.getZ()).next();
vertexConsumer.vertex(end.getX(), end.getY(), end.getZ() + 1200f).color(argb).normal((float) normal.getX(), (float) normal.getY(), (float) normal.getZ()).next();
context.draw();
}

public void initialize(int entitySize, int mouseX, int mouseY, int layers, boolean hideDisabled, String idInField) {
buildLastRecord();
buildRecords();
List<BuiltRecord> removedRecords = records.stream().filter(record -> {
boolean isIdInField = !idInField.isBlank() && record.renderLayer().toString().contains(idInField);
return (hideDisabled && isIdInField) || (!isIdInField && target.disabledTextureIds.stream().anyMatch(record.renderLayer().toString()::contains));
boolean isIdInField = !idInField.isBlank() && record.textureId().contains(idInField);
return (hideDisabled && isIdInField) || (!isIdInField && target.disabledTextureIds.stream().anyMatch(record.textureId()::contains));
}).toList();
records.removeAll(removedRecords);
List<Triple> sortByDepth = new ArrayList<>();
records.forEach(record -> {
records.stream().filter(record -> !unfocusableLayers.contains(record.renderLayer())).forEach(record -> {
Vertex[][] vertices = record.vertices();
for (int i = 0, size = vertices.length; i < size; i++) {
Polygon polygon = new Polygon();
Expand All @@ -70,8 +70,7 @@ public void initialize(int entitySize, int mouseX, int mouseY, int layers, boole
if (!polygon.contains(mouseX, mouseY)) continue;
Vertex point = quad[0];
double deltaZ = 0;
if (point.normalZ() != 0)
deltaZ = (point.normalX() * (mouseX - point.x()) + point.normalY() * (mouseY - point.y())) / point.normalZ();
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 All @@ -92,7 +91,7 @@ public void initialize(int entitySize, int mouseX, int mouseY, int layers, boole

public String focusedTextureId() {
if (focusedRecord == null) return null;
return getTextureId(focusedRecord);
return focusedRecord.textureId();
}

public Vec2f getFocusedUV() {
Expand All @@ -107,7 +106,7 @@ public Vec2f getFocusedUV() {
}

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

public void drawModelWithNormals(DrawContext context, int entitySize) {
drawByAnother(context.getVertexConsumers(), renderLayer -> true, (renderLayer, vertices) -> vertices);
drawByAnother(context.getVertexConsumers(), BuiltRecord::vertices);
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);
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);
}

private void drawFocused(DrawContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public MixinClientPlayerEntity(ClientWorld world, GameProfile profile) {
}

@Inject(method = "getLeashPos", at = @At("HEAD"), cancellable = true)
private void realcamera$onGetLeashPosHEAD(float delta, CallbackInfoReturnable<Vec3d> cInfo) {
private void realcamera$atGetLeashPosHEAD(float delta, CallbackInfoReturnable<Vec3d> cInfo) {
if (RealCameraCore.isRendering()) cInfo.setReturnValue(super.getLeashPos(delta));
}

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$atRenderCrosshairHEAD(DrawContext context, CallbackInfo cInfo) {
if (ConfigFile.config().dynamicCrosshair() && 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$atRenderCrosshairRETURN(DrawContext context, CallbackInfo cInfo) {
if (ConfigFile.config().dynamicCrosshair() && RealCameraCore.isActive()) {
context.getMatrices().pop();
}
Expand Down
Loading

0 comments on commit 7fb7807

Please sign in to comment.