Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 1.20 #3

Draft
wants to merge 8 commits into
base: 1.20
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

Customizable Crosshair Mod for Minecraft

### only supports 1.21
### 1.20 support coming soon
### download on [modrinth](https://modrinth.com/mod/cubes-crosshairs)

### supports 1.20-1.20.6 and 1.21


## Current Features

- Crosshair has customizable options via command line (see Options section below)
- Crosshair indicated attack cooldown
- Crosshair changes to a red color when targeting an entity


## Planned Features

- Crosshair changes colors depending on what you are looking at (ex. red for hostile mobs, green when holding the right tool to break a block)
- Crosshair indicates attack cooldown
- Config will save
- Crosshair indicates block breaking progress
- More customizable options
- Configuration will save


## Options

Expand All @@ -27,11 +37,12 @@ Customizable Crosshair Mod for Minecraft
| Alpha | cube:set_alpha | Crosshair alpha value. 0.0 = Transparent, 1.0 = Opaque |
| Feather | cube:set_feather | How much to blend the edge of the crosshair. Default is 0.001 |
| Disabled Alpha | cube:set_disabled_alpha | Alpha value for disabled portion of crosshair (only visible with progress bar) |

## Screenshots

![Example 7](https://github.com/XtraCube/cubes-crosshairs/blob/master/examples/example7.png?raw=true)
![Example 6](https://github.com/XtraCube/cubes-crosshairs/blob/master/examples/example6.png?raw=true)
![Example 5](https://github.com/XtraCube/cubes-crosshairs/blob/master/examples/example5.png?raw=true)
![Example 1](https://github.com/XtraCube/cubes-crosshairs/blob/master/examples/example1.png?raw=true)
![Example 2](https://github.com/XtraCube/cubes-crosshairs/blob/master/examples/example2.png?raw=true)
![Example 3](https://github.com/XtraCube/cubes-crosshairs/blob/master/examples/example3.png?raw=true)
![Example 4](https://github.com/XtraCube/cubes-crosshairs/blob/master/examples/example4.png?raw=true)

Binary file added examples/example5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/example6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/example7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.21+build.9
loader_version=0.15.11

# Mod Properties
mod_version=0.0.1-1.21
mod_version=0.0.2+1.21
maven_group=dev.xtracube.crosshairs
archives_base_name=cubes-crosshair

Expand Down
25 changes: 22 additions & 3 deletions src/main/java/dev/xtracube/crosshairs/client/CrosshairOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.systems.RenderSystem;
import dev.xtracube.crosshairs.ShaderSupplier;
import dev.xtracube.crosshairs.mixin.ClientPlayerInteractionManagerMixin;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -30,11 +31,12 @@ public void onHudRender(DrawContext drawContext, RenderTickCounter tickCounter)

MinecraftClient mc = MinecraftClient.getInstance();

// from here to the velocityY*= line, is all calculation for making the mouse movement offset the crosshair
double mouseX = mc.mouse.getX();
double mouseY = mc.mouse.getY();

int mouseDeltaX = (int) (mouseX - lastMouseX);
int mouseDeltaY = (int) (mouseY - lastMouseY);
double mouseDeltaX = mouseX - lastMouseX;
double mouseDeltaY = mouseY - lastMouseY;

lastMouseX = mouseX;
lastMouseY = mouseY;
Expand All @@ -45,6 +47,7 @@ public void onHudRender(DrawContext drawContext, RenderTickCounter tickCounter)
velocityX *= VelocityModifier;
velocityY *= VelocityModifier;

// init variables for the rendering step
int x1 = 0;
int y1 = 0;

Expand All @@ -53,18 +56,34 @@ public void onHudRender(DrawContext drawContext, RenderTickCounter tickCounter)

int z = 0;

// drawContext.drawCenteredTextWithShadow(mc.textRenderer, String.valueOf(), x2/2, y2/2, 0xFFFFFFFF);

// special shader logic like cooldowns
var shader = ShaderSupplier.INSTANCE.get();
shader.getUniform("Offset").set(new float[] {(float)velocityX, (float)velocityY});
shader.getUniform("Cooldown").set(mc.player.getAttackCooldownProgress(mc.player.getItemUseTime()+tickCounter.getTickDelta(true)));

float cooldown = 1.0f;

if (mc.interactionManager != null && mc.interactionManager.isBreakingBlock()) {
// block breaking progress
cooldown = 1f - ((ClientPlayerInteractionManagerMixin) mc.interactionManager ).getCurrentBreakingProgress();
}
else if (mc.player != null) {
// attack cooldown
cooldown = mc.player.getAttackCooldownProgress(mc.player.getItemUseTime()+tickCounter.getTickDelta(true));
}

shader.getUniform("Cooldown").set(cooldown);

// change color if targeted entity can be attacked
if (mc.targetedEntity != null && mc.targetedEntity.isAttackable()) {
shader.getUniform("Saturation").set(.75f);
}
else {
shader.getUniform("Saturation").set(0f);
}

// draw the shader in a quad over the entire screen
Matrix4f matrix4f = drawContext.getMatrices().peek().getPositionMatrix();
BufferBuilder bufferBuilder = Tessellator.getInstance().begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION);
bufferBuilder.vertex(matrix4f, (float)x1, (float)y1, (float)z);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package dev.xtracube.crosshairs;
package dev.xtracube.crosshairs.client;

import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import dev.xtracube.crosshairs.client.CrosshairOverlay;
import dev.xtracube.crosshairs.ShaderSupplier;
import dev.xtracube.crosshairs.commands.*;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.text.Text;

public class CubesCrosshairClient implements ClientModInitializer {
@Override
Expand Down Expand Up @@ -93,6 +95,27 @@ public void onInitializeClient() {
.executes(new SingleFloatShaderCommand("DisabledAlpha")))
.build(),

ClientCommandManager
.literal("cube:set_shape")
.then(ClientCommandManager.argument("shape", StringArgumentType.string())
.executes(context -> {
var string = StringArgumentType.getString(context, "shape");
return switch (string) {
case "circle" -> {
ShaderSupplier.INSTANCE.get().getUniform("Shape").set(0);
yield 0;
}
case "rect" -> {
ShaderSupplier.INSTANCE.get().getUniform("Shape").set(1);
yield 0;
}
case null, default -> {
context.getSource().sendError(Text.literal("Invalid shape. Valid shapes are 'circle' and 'rect'"));
yield -1;
}
};
}))
.build()

};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.xtracube.crosshairs.mixin;

import net.minecraft.client.network.ClientPlayerInteractionManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ClientPlayerInteractionManager.class)
public interface ClientPlayerInteractionManagerMixin {

@Accessor
float getCurrentBreakingProgress();

}
76 changes: 51 additions & 25 deletions src/main/resources/assets/minecraft/shaders/core/crosshair.fsh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#version 150 core

// Author XtraCube
// https://www.xtracube.dev

#version 150 core

#define PI 3.1415926538
#define TWO_PI 6.28318530718

Expand All @@ -15,7 +15,8 @@ uniform vec2 Offset;
uniform float SpinSpeed;
uniform float CooldownOffset;

// size
// size and shape
uniform int Shape;
uniform float Radius;
uniform float Thickness;

Expand Down Expand Up @@ -44,39 +45,64 @@ vec3 hsb2rgb( in vec3 c ){
return c.z * mix(vec3(1.0), rgb, c.y);
}

void main(){
// normalized coordinate (fit to screen height)
vec2 st = gl_FragCoord.xy/ScreenSize.y;
// normalized center coordinate (adjusted for height fit)
vec2 center = vec2(ScreenSize.x/(2*ScreenSize.y), 0.5) + Offset/ScreenSize;

// distance to center from coordinate
// return alpha for circle shape
float circle(vec2 st, vec2 center) {
float dist = distance(st, center);

// alpha for feather
float a_feather = smoothstep(Thickness-Feather, Thickness+Feather, dist-Radius+Thickness)
-smoothstep(Thickness-Feather, Thickness+Feather, dist-Radius);
return smoothstep(Radius-Feather, Radius+Feather, dist+Thickness/2.)-
smoothstep(Radius-Feather, Radius+Feather, dist-Thickness/2.);
}

// return alpha for square shape
float rect(vec2 st, vec2 center)
{
vec2 bl = step(vec2(.5-Radius-Thickness/2.)+center,st); // bottom-left
vec2 tr = step(vec2(.5-Radius-Thickness/2.)-center,1.0-st); // top-right

float outer = bl.x * bl.y * tr.x * tr.y;

bl = step(vec2(.5-Radius+Thickness/2.)+center,st); // bottom-left
tr = step(vec2(.5-Radius+Thickness/2.)-center,1.0-st); // top-right

float inner = bl.x * bl.y * tr.x * tr.y;

return outer-inner;
}

float progress(vec2 st, vec2 center){
// to account for smoothstep losing a couple pixels
float pre_progress = Cooldown + Cooldown*Feather;

// calculate angle to center in radians
vec2 toCenter = center - st;
float angle = mod(atan(toCenter.y,toCenter.x)+CooldownOffset*PI, TWO_PI);

// initialize color and progress alpha
vec3 color = vec3(0.0);
float a_progress = 1.0;

// calculate rainbow color based on angle
// color = hsb2rgb( vec3(( angle/TWO_PI ) + GameTime * 2400 * SpinSpeed, Saturation, Brightness ));
return clamp( DisabledAlpha + smoothstep( pre_progress, pre_progress-Feather, angle/TWO_PI ), 0.0, 1.0);
}

void main(){
// normalized coordinate (fit to screen height)
vec2 st = gl_FragCoord.xy/ScreenSize.y;

// calculate center even when resolution isnt square
vec2 center = vec2(ScreenSize.x/(2.*ScreenSize.y), 0.5) + Offset/ScreenSize;

// shape function
float shape;

// normal color calculation
color = hsb2rgb(vec3(Hue, Saturation, Brightness));
if (Shape < 1) {
shape = circle(st, center);
}
else {
shape = rect(st, center-vec2(0.5));
}

// to account for smoothstep losing a couple pixels
float pre_progress = Cooldown + Cooldown*Feather;
float alpha = BaseAlpha * shape * progress(st, center);

// calculate alpha using progress
a_progress = clamp( DisabledAlpha + smoothstep( pre_progress, pre_progress-Feather, angle/TWO_PI ), 0.0, 1.0);
// HSB color calculation
vec3 color = hsb2rgb(vec3(Hue, Saturation, Brightness));

// final fragColor calculation, multiplying various alphas
fragColor = vec4(color, BaseAlpha * a_feather * a_progress);
fragColor = vec4(color, alpha);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
{ "name": "SpinSpeed", "type": "float", "count": 1, "values": [ 1.0 ]},
{ "name": "CooldownOffset", "type": "float", "count": 1, "values": [ -1.5 ] },

{ "name": "Shape", "type": "int", "count": 1, "values": [ 0 ]},
{ "name": "Radius", "type": "float", "count": 1, "values": [ 0.05 ]},
{ "name": "Thickness", "type": "float", "count": 1, "values": [ 0.005 ]},

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/cubes-crosshair.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"compatibilityLevel": "JAVA_21",
"mixins": [
],
"client": [
"ClientPlayerInteractionManagerMixin"
],
"injectors": {
"defaultRequire": 1
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
"dev.xtracube.crosshairs.CubesCrosshair"
],
"client": [
"dev.xtracube.crosshairs.CubesCrosshairClient"
"dev.xtracube.crosshairs.client.CubesCrosshairClient"
],
"fabric-datagen": [
"dev.xtracube.crosshairs.CubesCrosshairDataGenerator"
]
},
"mixins": [
"cubes-crosshair.mixins.json"
],
"depends": {
"fabricloader": ">=0.15.11",
Expand Down