Skip to content

Commit

Permalink
Feat: Add offscreen style to tracers (#3546)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski authored Apr 30, 2023
1 parent ba1cd6e commit 1cac537
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public void rotatedQuad(double x, double y, double width, double height, double
rTex.texQuad(x, y, width, height, rotation, texture.get(width, height), color);
}

public void triangle(double x1, double y1, double x2, double y2, double x3, double y3, Color color) {
r.triangle(x1, y1, x2, y2, x3, y3 ,color);
}

public void text(String text, double x, double y, Color color, boolean title) {
texts.add(getOp(textPool, x, y, color).set(text, theme.textRenderer(), title));
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/meteordevelopment/meteorclient/renderer/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ public void quad(int i1, int i2, int i3, int i4) {
growIfNeeded();
}

public void triangle(int i1, int i2, int i3) {
long p = indicesPointer + indicesCount * 4L;

memPutInt(p, i1);
memPutInt(p + 4, i2);
memPutInt(p + 8, i3);

indicesCount += 3;
growIfNeeded();
}

public void growIfNeeded() {
// Vertices
if ((vertexI + 1) * primitiveVerticesSize >= vertices.capacity()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public void render(MatrixStack matrices) {
lines.render(matrices);
}

// Tris
public void triangle(double x1, double y1, double x2, double y2, double x3, double y3, Color color) {
triangles.triangle(
triangles.vec2(x1, y1).color(color).next(),
triangles.vec2(x2, y2).color(color).next(),
triangles.vec2(x3, y3).color(color).next()
);
}

// Lines

public void line(double x1, double y1, double x2, double y2, Color color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public void quad(double x, double y, double width, double height, Color cTopLeft
Renderer2D.COLOR.quad(x, y, width, height, cTopLeft, cTopRight, cBottomRight, cBottomLeft);
}

public void triangle(double x1, double y1, double x2, double y2, double x3, double y3, Color color) {
Renderer2D.COLOR.triangle(x1, y1, x2, y2, x3, y3, color);
}

public void texture(Identifier id, double x, double y, double width, double height, Color color) {
GL.bindTexture(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,52 @@

package meteordevelopment.meteorclient.systems.modules.render;

import com.mojang.blaze3d.systems.RenderSystem;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.events.render.Render2DEvent;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
import meteordevelopment.meteorclient.gui.renderer.GuiRenderer;
import meteordevelopment.meteorclient.renderer.Renderer2D;
import meteordevelopment.meteorclient.settings.*;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.systems.friends.Friends;
import meteordevelopment.meteorclient.systems.hud.HudRenderer;
import meteordevelopment.meteorclient.systems.modules.Categories;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.entity.EntityUtils;
import meteordevelopment.meteorclient.utils.entity.Target;
import meteordevelopment.meteorclient.utils.player.PlayerUtils;
import meteordevelopment.meteorclient.utils.render.NametagUtils;
import meteordevelopment.meteorclient.utils.render.RenderUtils;
import meteordevelopment.meteorclient.utils.render.color.Color;
import meteordevelopment.meteorclient.utils.render.color.SettingColor;
import meteordevelopment.orbit.EventHandler;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import org.joml.Vector2f;
import org.joml.Vector3d;

import java.time.Duration;
import java.time.Instant;
import java.util.Timer;

public class Tracers extends Module {
private final SettingGroup sgGeneral = settings.getDefaultGroup();
private final SettingGroup sgAppearance = settings.createGroup("Appearance");
private final SettingGroup sgColors = settings.createGroup("Colors");

public enum TracerStyle {
Lines,
Offscreen
};

// General

private final Setting<Object2BooleanMap<EntityType<?>>> entities = sgGeneral.add(new EntityTypeListSetting.Builder()
Expand All @@ -54,17 +76,26 @@ public class Tracers extends Module {

// Appearance

private final Setting<TracerStyle> style = sgAppearance.add(new EnumSetting.Builder<TracerStyle>()
.name("style")
.description("What display mode should be used")
.defaultValue(TracerStyle.Lines)
.build()
);

private final Setting<Target> target = sgAppearance.add(new EnumSetting.Builder<Target>()
.name("target")
.description("What part of the entity to target.")
.defaultValue(Target.Body)
.visible(() -> style.get() == TracerStyle.Lines)
.build()
);

private final Setting<Boolean> stem = sgAppearance.add(new BoolSetting.Builder()
.name("stem")
.description("Draw a line through the center of the tracer target.")
.defaultValue(true)
.visible(() -> style.get() == TracerStyle.Lines)
.build()
);

Expand All @@ -77,6 +108,44 @@ public class Tracers extends Module {
.build()
);

private final Setting<Integer> distanceOffscreen = sgAppearance.add(new IntSetting.Builder()
.name("distance-offscreen")
.description("Offscreen's distance from center.")
.defaultValue(200)
.min(0)
.sliderMax(500)
.visible(() -> style.get() == TracerStyle.Offscreen)
.build()
);

private final Setting<Integer> sizeOffscreen = sgAppearance.add(new IntSetting.Builder()
.name("size-offscreen")
.description("Offscreen's size.")
.defaultValue(10)
.min(2)
.sliderMax(50)
.visible(() -> style.get() == TracerStyle.Offscreen)
.build()
);

private final Setting<Boolean> blinkOffscreen = sgAppearance.add(new BoolSetting.Builder()
.name("blink-offscreen")
.description("Make offscreen Blink.")
.defaultValue(true)
.visible(() -> style.get() == TracerStyle.Offscreen)
.build()
);

private final Setting<Double> blinkOffscreenSpeed = sgAppearance.add(new DoubleSetting.Builder()
.name("blink-offscreen-speed")
.description("Offscreen's blink speed.")
.defaultValue(4)
.min(1)
.sliderMax(15)
.visible(() -> style.get() == TracerStyle.Offscreen && blinkOffscreen.get())
.build()
);

// Colors

public final Setting<Boolean> distance = sgColors.add(new BoolSetting.Builder()
Expand Down Expand Up @@ -143,39 +212,50 @@ public class Tracers extends Module {
);

private int count;
private Instant initTimer = Instant.now();

public Tracers() {
super(Categories.Render, "tracers", "Displays tracer lines to specified entities.");
}

private boolean shouldBeIgnored(Entity entity) {
return !PlayerUtils.isWithin(entity, maxDist.get()) || (!Modules.get().isActive(Freecam.class) && entity == mc.player) || !entities.get().getBoolean(entity.getType()) || (ignoreFriends.get() && entity instanceof PlayerEntity && Friends.get().isFriend((PlayerEntity) entity)) || (!showInvis.get() && entity.isInvisible()) | !EntityUtils.isInRenderDistance(entity);
}

private Color getEntityColor(Entity entity) {
Color color;

if (distance.get()) {
if (friendOverride.get() && entity instanceof PlayerEntity && Friends.get().isFriend((PlayerEntity) entity)) {
color = Config.get().friendColor.get();
}
else color = EntityUtils.getColorFromDistance(entity);
}
else if (entity instanceof PlayerEntity) {
color = PlayerUtils.getPlayerColor(((PlayerEntity) entity), playersColor.get());
}
else {
color = switch (entity.getType().getSpawnGroup()) {
case CREATURE -> animalsColor.get();
case WATER_AMBIENT, WATER_CREATURE, UNDERGROUND_WATER_CREATURE, AXOLOTLS -> waterAnimalsColor.get();
case MONSTER -> monstersColor.get();
case AMBIENT -> ambientColor.get();
default -> miscColor.get();
};
}

return new Color(color);
}

@EventHandler
private void onRender(Render3DEvent event) {
if (mc.options.hudHidden) return;
if (mc.options.hudHidden || style.get() == TracerStyle.Offscreen) return;
count = 0;

for (Entity entity : mc.world.getEntities()) {
if (!PlayerUtils.isWithin(entity, maxDist.get()) || (!Modules.get().isActive(Freecam.class) && entity == mc.player) || !entities.get().getBoolean(entity.getType()) || (ignoreFriends.get() && entity instanceof PlayerEntity && Friends.get().isFriend((PlayerEntity) entity)) || (!showInvis.get() && entity.isInvisible()) | !EntityUtils.isInRenderDistance(entity)) continue;
if (shouldBeIgnored(entity)) continue;

Color color;

if (distance.get()) {
if (friendOverride.get() && entity instanceof PlayerEntity && Friends.get().isFriend((PlayerEntity) entity)) {
color = Config.get().friendColor.get();
}
else color = EntityUtils.getColorFromDistance(entity);
}
else if (entity instanceof PlayerEntity) {
color = PlayerUtils.getPlayerColor(((PlayerEntity) entity), playersColor.get());
}
else {
color = switch (entity.getType().getSpawnGroup()) {
case CREATURE -> animalsColor.get();
case WATER_AMBIENT, WATER_CREATURE, UNDERGROUND_WATER_CREATURE, AXOLOTLS -> waterAnimalsColor.get();
case MONSTER -> monstersColor.get();
case AMBIENT -> ambientColor.get();
default -> miscColor.get();
};
}
Color color = getEntityColor(entity);

double x = entity.prevX + (entity.getX() - entity.prevX) * event.tickDelta;
double y = entity.prevY + (entity.getY() - entity.prevY) * event.tickDelta;
Expand All @@ -192,6 +272,103 @@ else if (entity instanceof PlayerEntity) {
}
}

@EventHandler
public void onRender2D(Render2DEvent event) {
if (mc.options.hudHidden || style.get() != TracerStyle.Offscreen) return;
count = 0;

Renderer2D.COLOR.begin();

for (Entity entity : mc.world.getEntities()) {
if (shouldBeIgnored(entity)) continue;

Color color = getEntityColor(entity);

if (blinkOffscreen.get())
color.a *= getAlpha();

Vec2f screenCenter = new Vec2f(mc.getWindow().getFramebufferWidth() / 2.f, mc.getWindow().getFramebufferHeight() / 2.f);

Vector3d projection = new Vector3d(entity.prevX, entity.prevY, entity.prevZ);
boolean projSucceeded = NametagUtils.to2D(projection, 1, false, false);

if (projSucceeded && projection.x > 0.f && projection.x < mc.getWindow().getFramebufferWidth() && projection.y > 0.f && projection.y < mc.getWindow().getFramebufferHeight())
continue;

projection = new Vector3d(entity.prevX, entity.prevY, entity.prevZ);
NametagUtils.to2D(projection, 1, false, true);

Vector2f angle = vectorAngles(new Vector3d(screenCenter.x - projection.x, screenCenter.y - projection.y, 0));
angle.y += 180;

float angleYawRad = (float) Math.toRadians(angle.y);

Vector2f newPoint = new Vector2f(screenCenter.x + distanceOffscreen.get() * (float) Math.cos(angleYawRad),
screenCenter.y + distanceOffscreen.get() * (float) Math.sin(angleYawRad));

Vector2f trianglePoints[] = {
new Vector2f(newPoint.x - sizeOffscreen.get(), newPoint.y - sizeOffscreen.get()),
new Vector2f(newPoint.x + sizeOffscreen.get() * 0.73205f, newPoint.y),
new Vector2f(newPoint.x - sizeOffscreen.get(), newPoint.y + sizeOffscreen.get())
};

rotateTriangle(trianglePoints, angle.y);

Renderer2D.COLOR.triangle(trianglePoints[0].x, trianglePoints[0].y, trianglePoints[1].x, trianglePoints[1].y, trianglePoints[2].x,
trianglePoints[2].y, color);

count++;
}

Renderer2D.COLOR.render(null);
}

private void rotateTriangle(Vector2f[] points, float ang) {
Vector2f triangleCenter = new Vector2f(0, 0);
triangleCenter.add(points[0]).add(points[1]).add(points[2]).div(3.f);
float theta = (float)Math.toRadians(ang);
float cos = (float)Math.cos(theta);
float sin = (float)Math.sin(theta);
for (int i = 0; i < 3; i++) {
Vector2f point = new Vector2f(points[i].x, points[i].y).sub(triangleCenter);

Vector2f newPoint = new Vector2f(point.x * cos - point.y * sin, point.x * sin + point.y * cos);
newPoint.add(triangleCenter);

points[i] = newPoint;
}
}

private Vector2f vectorAngles(final Vector3d forward) {
float tmp, yaw, pitch;

if (forward.x == 0 && forward.y == 0) {
yaw = 0;
if (forward.z > 0)
pitch = 270;
else
pitch = 90;
} else {
yaw = (float)(Math.atan2(forward.y, forward.x) * 180 / Math.PI);
if (yaw < 0)
yaw += 360;

tmp = (float)Math.sqrt(forward.x * forward.x + forward.y * forward.y);
pitch = (float)(Math.atan2(-forward.z, tmp) * 180 / Math.PI);
if (pitch < 0)
pitch += 360;
}

return new Vector2f(pitch, yaw);
}

private float getAlpha() {
double speed = blinkOffscreenSpeed.get() / 4.0;
double duration = Math.abs(Duration.between(Instant.now(), initTimer).toMillis()) * speed;

return (float)Math.abs((duration % 1000) - 500) / 500.f;
}

@Override
public String getInfoString() {
return Integer.toString(count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public static boolean to2D(Vector3d pos, double scale) {
}

public static boolean to2D(Vector3d pos, double scale, boolean distanceScaling) {
return to2D(pos, scale, distanceScaling, false);
}

public static boolean to2D(Vector3d pos, double scale, boolean distanceScaling, boolean allowBehind) {
NametagUtils.scale = scale;
if (distanceScaling) {
NametagUtils.scale *= getScale(pos);
Expand All @@ -52,15 +56,22 @@ public static boolean to2D(Vector3d pos, double scale, boolean distanceScaling)
vec4.mul(model, mmMat4);
mmMat4.mul(projection, pmMat4);

if (pmMat4.w <= 0.0f) return false;
boolean behind = pmMat4.w <= 0.f;

if (behind && !allowBehind) return false;

toScreen(pmMat4);
double x = pmMat4.x * mc.getWindow().getFramebufferWidth();
double y = pmMat4.y * mc.getWindow().getFramebufferHeight();

if (behind) {
x = mc.getWindow().getFramebufferWidth() - x;
y = mc.getWindow().getFramebufferHeight() - y;
}

if (Double.isInfinite(x) || Double.isInfinite(y)) return false;

pos.set(x / windowScale, mc.getWindow().getFramebufferHeight() - y / windowScale, pmMat4.z);
pos.set(x / windowScale, mc.getWindow().getFramebufferHeight() - y / windowScale, allowBehind ? pmMat4.w : pmMat4.z);
return true;
}

Expand Down

0 comments on commit 1cac537

Please sign in to comment.