diff --git a/build.gradle b/build.gradle index b66f675..b3489d9 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { // modRuntimeOnly include(fabricApi.module("fabric-rendering-data-attachment-v1", project.fabric_version)) // modRuntimeOnly include(fabricApi.module("fabric-rendering-fluids-v1", project.fabric_version)) // modRuntimeOnly include(fabricApi.module("fabric-resource-loader-v0", project.fabric_version)) - modRuntimeOnly "maven.modrinth:lithium:${project.lithium_version}" +// modRuntimeOnly "maven.modrinth:lithium:${project.lithium_version}" modRuntimeOnly "maven.modrinth:spark:${project.spark_version}" modRuntimeOnly include(fabricApi.module("fabric-command-api-v2", project.fabric_version)) modRuntimeOnly include(fabricApi.module("fabric-lifecycle-events-v1", project.fabric_version)) diff --git a/src/main/java/codes/ztereohype/example/ExampleMod.java b/src/main/java/codes/ztereohype/example/ExampleMod.java index 72f1d1f..a6e9a4e 100644 --- a/src/main/java/codes/ztereohype/example/ExampleMod.java +++ b/src/main/java/codes/ztereohype/example/ExampleMod.java @@ -5,17 +5,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ExampleMod implements ModInitializer { - // This logger is used to write text to the console and the log file. - // It is considered best practice to use your mod id as the logger's name. - // That way, it's clear which mod wrote info, warnings, and errors. public static final Logger LOGGER = LoggerFactory.getLogger("modid"); + public static boolean toggle = true; @Override - public void onInitialize() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - // However, some things (like resources) may still be uninitialized. - // Proceed with mild caution. - - LOGGER.info("Hello Fabric world!"); - } + public void onInitialize() {} } diff --git a/src/main/java/codes/ztereohype/example/Star.java b/src/main/java/codes/ztereohype/example/Star.java index a04ec12..94d6913 100644 --- a/src/main/java/codes/ztereohype/example/Star.java +++ b/src/main/java/codes/ztereohype/example/Star.java @@ -6,69 +6,71 @@ import java.util.ArrayList; import java.util.List; public class Star { - private final double xCoord; - private final double yCoord; - private final double zCoord; + private final float xCoord; + private final float yCoord; + private final float zCoord; - private final double sinPolarAngle; - private final double cosPolarAngle; + private final float sinPolarAngle; + private final float cosPolarAngle; - private final double projSin; - private final double projCos; + private final float projSin; + private final float projCos; - private final double twinkleSpeed; - private final double initialRadius; + private final float twinkleSpeed; + private final float minRadius; + private final float maxRadius; - private double currentAngle; - private double currentRadius; + private float currentAngle; + private float currentRadius; - public Star(double randX, double randY, double randZ, double initialRadius, double twinkleSpeed) { - double invsqrtDistance = Mth.fastInvSqrt(randX * randX + randY * randY + randZ * randZ); - this.xCoord = randX * invsqrtDistance * 100.0; - this.yCoord = randY * invsqrtDistance * 100.0; - this.zCoord = randZ * invsqrtDistance * 100.0; + public Star(float randX, float randY, float randZ, float radius, float twinkleSpeed) { + float invsqrtDistance = Mth.fastInvSqrt(randX * randX + randY * randY + randZ * randZ); + this.xCoord = randX * invsqrtDistance * 100.0F; + this.yCoord = randY * invsqrtDistance * 100.0F; + this.zCoord = randZ * invsqrtDistance * 100.0F; double polarAngle = Math.atan2(randX, randZ); - this.sinPolarAngle = Math.sin(polarAngle); - this.cosPolarAngle = Math.cos(polarAngle); + this.sinPolarAngle = (float) Math.sin(polarAngle); + this.cosPolarAngle = (float) Math.cos(polarAngle); // magic projection fuckery?? double proj = Math.atan2(Math.sqrt(randX * randX + randZ * randZ), randY); - this.projSin = Math.sin(proj); - this.projCos = Math.cos(proj); + this.projSin = (float) Math.sin(proj); + this.projCos = (float) Math.cos(proj); this.twinkleSpeed = twinkleSpeed; - this.initialRadius = initialRadius; - this.currentRadius = initialRadius; + this.minRadius = radius - 0.15f; + this.maxRadius = radius + 0.15f; + this.currentRadius = radius; this.currentAngle = twinkleSpeed; //just so they dont all start straight } public void tick(int ticks) { - currentAngle += 0.007d * twinkleSpeed; - currentRadius = Mth.lerp(Math.sin(ticks * twinkleSpeed / 10f) * 0.5f + 0.5f, initialRadius - 0.15f, initialRadius + 0.15f) ; + currentAngle += 0.007f * twinkleSpeed; + currentRadius = Mth.lerp(Mth.sin(ticks * twinkleSpeed / 10f) * 0.5f + 0.5f, minRadius, maxRadius); } //return 4*3 coords for 4 vertices - public double[] getVertices() { - double[] vertices = new double[12]; + public float[] getVertices() { + float[] vertices = new float[12]; - double cosRot = Math.cos(currentAngle); - double sinRot = Math.sin(currentAngle); + float cosRot = Mth.cos(currentAngle); + float sinRot = Mth.sin(currentAngle); for(int v = 0; v < 4; ++v) { // shift the vector to the 4 corners: // vec 0, 1 --> -rad; vec 2, 3 --> +rad - double xShift = (double)((v & 2) - 1) * currentRadius; + float xShift = ((v & 2) - 1) * currentRadius; // vec 1, 2 --> +rad; vec 3, 0 --> -rad - double yShift = (double)(((v + 1) & 2) - 1) * currentRadius; + float yShift = (((v + 1) & 2) - 1) * currentRadius; // magic projection fuckery to turn the shift into an offset applying rotation and polar bs - double aa = xShift * cosRot - yShift * sinRot; - double ab = yShift * cosRot + xShift * sinRot; - double ae = - aa * projCos; - double yOffset = aa * projSin; - double xOffset = ae * sinPolarAngle - ab * cosPolarAngle; - double zOffset = ab * sinPolarAngle + ae * cosPolarAngle; + float aa = xShift * cosRot - yShift * sinRot; + float ab = yShift * cosRot + xShift * sinRot; + float ae = - aa * projCos; + float yOffset = aa * projSin; + float xOffset = ae * sinPolarAngle - ab * cosPolarAngle; + float zOffset = ab * sinPolarAngle + ae * cosPolarAngle; vertices[v * 3 ] = xCoord + xOffset; vertices[v * 3 + 1] = yCoord + yOffset; diff --git a/src/main/java/codes/ztereohype/example/StarManager.java b/src/main/java/codes/ztereohype/example/StarManager.java index 6b2dac2..4a16bdf 100644 --- a/src/main/java/codes/ztereohype/example/StarManager.java +++ b/src/main/java/codes/ztereohype/example/StarManager.java @@ -9,9 +9,9 @@ import net.minecraft.util.RandomSource; import java.util.ArrayList; import java.util.List; +//todo: redo all of the star rendering to be able to use the same vertex buffer object with a uniform or with the posestack public class StarManager { -// public static VertexBuffer starBuffer = new VertexBuffer(); - + private static final int STAR_ATTEMPTS = 1500; public static ArrayList starList = new ArrayList<>(); public static BufferBuilder.RenderedBuffer generateStars(BufferBuilder bufferBuilder, long seed) { @@ -79,7 +79,6 @@ public class StarManager { double zOffset = ab * sinPolarAngle + ae * cosPolarAngle; bufferBuilder.vertex(xCoord + xOffset, yCoord + yOffset, zCoord + zOffset).endVertex(); - System.out.println("x: " + (xCoord + xOffset) + ", y: " + (yCoord + yOffset) + ", z: " + (zCoord + zOffset)); } } } @@ -89,36 +88,38 @@ public class StarManager { public static void generateStarList() { RandomSource randomSource = RandomSource.create(123L); - for(int i = 0; i < 65000; ++i) { + starList.clear(); + + for(int i = 0; i < STAR_ATTEMPTS; ++i) { // -1..1 - double randX = randomSource.nextFloat() * 2.0F - 1.0F; - double randY = randomSource.nextFloat() * 2.0F - 1.0F; - double randZ = randomSource.nextFloat() * 2.0F - 1.0F; + float randX = randomSource.nextFloat() * 2.0F - 1.0F; + float randY = randomSource.nextFloat() * 2.0F - 1.0F; + float randZ = randomSource.nextFloat() * 2.0F - 1.0F; // 0.15..0.25 ??? - double starRadius = 0.15F + randomSource.nextFloat() * 0.1F; + float starRadius = 0.15F + randomSource.nextFloat() * 0.1F; double squaredDistance = randX * randX + randY * randY + randZ * randZ; if (squaredDistance < 1.0 && squaredDistance > 0.01) { - starList.add(new Star(randX, randY, randZ, starRadius, 0.3 + randomSource.nextDouble() * 0.4)); + starList.add(new Star(randX, randY, randZ, starRadius, 0.3f + randomSource.nextFloat() * 0.4f)); } } } public static void updateStars(int ticks, VertexBuffer starBuffer) { - Tesselator tesselator = Tesselator.getInstance(); - BufferBuilder bufferBuilder = tesselator.getBuilder(); - RenderSystem.setShader(GameRenderer::getPositionShader); - + if (!ExampleMod.toggle) return; + BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder(); bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); - +// for (Star star : starList) { star.tick(ticks); - double[] vertexList = star.getVertices(); - for (int i = 0; i < 12; i += 3) { - bufferBuilder.vertex(vertexList[i], vertexList[i + 1], vertexList[i + 2]).endVertex(); - } + float[] vertexList = star.getVertices(); + + bufferBuilder.vertex(vertexList[0], vertexList[1], vertexList[2]).endVertex(); + bufferBuilder.vertex(vertexList[3], vertexList[4], vertexList[5]).endVertex(); + bufferBuilder.vertex(vertexList[6], vertexList[7], vertexList[8]).endVertex(); + bufferBuilder.vertex(vertexList[9], vertexList[10], vertexList[11]).endVertex(); } starBuffer.bind(); diff --git a/src/main/java/codes/ztereohype/example/mixin/ExampleMixin.java b/src/main/java/codes/ztereohype/example/mixin/ExampleMixin.java index 4923729..84ad198 100644 --- a/src/main/java/codes/ztereohype/example/mixin/ExampleMixin.java +++ b/src/main/java/codes/ztereohype/example/mixin/ExampleMixin.java @@ -36,11 +36,14 @@ public class ExampleMixin { // @Inject(at = @At(value="INVOKE", shift = At.Shift.AFTER, target = "Lcom/mojang/blaze3d/vertex/VertexBuffer;bind()V", ordinal = 1), method = "renderSky") // private void pushPoseStack(PoseStack poseStack, Matrix4f projectionMatrix, float partialTick, Camera camera, boolean bl, Runnable skyFogSetup, CallbackInfo ci) { -// poseStack.pushPose(); -// poseStack.scale(40,1,1); +//// float[] scaling = ExampleMod.getScaling(); +//// poseStack.pushPose(); +//// poseStack.scale(scaling[0], scaling[1], scaling[2]); +// // } +// // @Inject(at = @At(value="INVOKE", shift = At.Shift.AFTER, target = "Lcom/mojang/blaze3d/vertex/VertexBuffer;drawWithShader(Lcom/mojang/math/Matrix4f;Lcom/mojang/math/Matrix4f;Lnet/minecraft/client/renderer/ShaderInstance;)V", ordinal = 1), method = "renderSky") // private void popPoseStack(PoseStack poseStack, Matrix4f projectionMatrix, float partialTick, Camera camera, boolean bl, Runnable skyFogSetup, CallbackInfo ci) { -// poseStack.popPose(); +//// poseStack.popPose(); // } } diff --git a/src/main/java/codes/ztereohype/example/mixin/LightTextureInvoker.java b/src/main/java/codes/ztereohype/example/mixin/LightTextureInvoker.java new file mode 100644 index 0000000..35a5ccd --- /dev/null +++ b/src/main/java/codes/ztereohype/example/mixin/LightTextureInvoker.java @@ -0,0 +1,15 @@ +package codes.ztereohype.example.mixin; + +import com.mojang.blaze3d.platform.NativeImage; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.client.renderer.LightTexture; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.level.dimension.DimensionType; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(LightTexture.class) +public interface LightTextureInvoker { + @Accessor NativeImage getLightPixels(); +} diff --git a/src/main/java/codes/ztereohype/example/mixin/MixinDebug.java b/src/main/java/codes/ztereohype/example/mixin/MixinDebug.java new file mode 100644 index 0000000..538e3b3 --- /dev/null +++ b/src/main/java/codes/ztereohype/example/mixin/MixinDebug.java @@ -0,0 +1,39 @@ +package codes.ztereohype.example.mixin; + +import com.mojang.blaze3d.platform.NativeImage; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiComponent; +import net.minecraft.client.gui.components.DebugScreenOverlay; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(DebugScreenOverlay.class) +public abstract class MixinDebug { + @Final @Shadow private Minecraft minecraft; + + @Inject(method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;)V", at = @At("HEAD")) + private void injectRender(PoseStack poseStack, CallbackInfo ci) { + int pixelSize = 6; + + NativeImage lightPixels = ((LightTextureInvoker) minecraft.gameRenderer.lightTexture()).getLightPixels(); + for (int x = 0; x < lightPixels.getWidth(); x++) { + for (int y = 0; y < lightPixels.getHeight(); y++) { + int colour = lightPixels.getPixelRGBA(x, y); + + int b = colour & 0xFF; + int g = colour >> 8 & 0xFF; + int r = colour >> 16 & 0xFF; + + int xCoord = minecraft.getWindow().getGuiScaledWidth() - (x * pixelSize); + int yCoord = minecraft.getWindow().getGuiScaledHeight() - (y * pixelSize); + + GuiComponent.fill(poseStack, xCoord, yCoord, xCoord + pixelSize, yCoord + pixelSize, 0xFF000000 | b << 16 | g << 8 | r); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/codes/ztereohype/example/mixin/MixinKeyboardHandler.java b/src/main/java/codes/ztereohype/example/mixin/MixinKeyboardHandler.java new file mode 100644 index 0000000..edbd232 --- /dev/null +++ b/src/main/java/codes/ztereohype/example/mixin/MixinKeyboardHandler.java @@ -0,0 +1,26 @@ +package codes.ztereohype.example.mixin; + +import codes.ztereohype.example.ExampleMod; +import codes.ztereohype.example.StarManager; +import net.minecraft.client.KeyboardHandler; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(KeyboardHandler.class) +public class MixinKeyboardHandler { + private long cooldown = 0; + @Inject(at = @At("HEAD"), method = "keyPress") + private void printKey(long windowPointer, int key, int scanCode, int action, int modifiers, CallbackInfo ci) { + if (key == 92) { +// StarManager.generateStarList(); + long time = System.currentTimeMillis(); + if (time - cooldown > 200) { + ExampleMod.toggle = !ExampleMod.toggle; + cooldown = time; + } + + } + } +} diff --git a/src/main/java/codes/ztereohype/example/mixin/MixinLightTexutre.java b/src/main/java/codes/ztereohype/example/mixin/MixinLightTexutre.java new file mode 100644 index 0000000..ae3b07a --- /dev/null +++ b/src/main/java/codes/ztereohype/example/mixin/MixinLightTexutre.java @@ -0,0 +1,64 @@ +package codes.ztereohype.example.mixin; + +import codes.ztereohype.example.ExampleMod; +import com.mojang.math.Vector3f; +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.client.renderer.LightTexture; +import net.minecraft.util.Mth; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyVariable; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +@Mixin(LightTexture.class) +public class MixinLightTexutre { +// @ModifyVariable(method = "updateLightTexture", +// at = @At("STORE"), +// ordinal = 9) +// private float mixinRedBrightness(float original) { +// if (ExampleMod.toggle) { +// return 1F; +// } else { +// return original; +// } +// } +// @ModifyArgs( +// method = "updateLightTexture", +// at = @At(value = "INVOKE", target = "Lcom/mojang/math/Vector3f;set(FFF)V", ordinal = 0) +// ) +// private void mixinRedBrightness(Args args) { +// if (ExampleMod.toggle) { +// args.set(0, 1F); +// args.set(1, args.get(1)); +// args.set(2, args.get(2)); +// } +// } + + @Inject( + method = "updateLightTexture", + at = @At(value = "INVOKE", target = "Lcom/mojang/math/Vector3f;clamp(FF)V", shift = At.Shift.BEFORE, ordinal = 2), + locals = LocalCapture.CAPTURE_FAILHARD + ) + private void injectWarmLight(float partialTicks, CallbackInfo ci, ClientLevel clientLevel, float f, float g, float h, float i, float j, float l, float k, Vector3f vector3f, float m, Vector3f vector3f2, int n, int o, float p, float q, float r, float s, float t, boolean bl, float v, Vector3f vector3f5) { + //todo: find a way to make a touch more saturated and brighter(?) + if (ExampleMod.toggle) { + Vector3f warmTint = new Vector3f(0.36F, 0.15F, -0.19F); + + float warmness = o / 15f * // increase w/ blocklight + (1f - vector3f.x() * (1 - n/15f)) * // decrease in skylight w/ dayness + Math.min((15 - o)/9f, 1f); // decrease for the 3 highest block light levels + + warmTint.mul(warmness); + warmTint.add(1f, 1f, 1f); + + Vector3f dramaticFactor = vector3f2.copy(); + dramaticFactor.mul(0.25f); + dramaticFactor.add(0.75f, 0.75f, 0.76f); + + vector3f2.mul(dramaticFactor.x(), dramaticFactor.y(), dramaticFactor.z()); + vector3f2.mul(warmTint.x(), warmTint.y(), warmTint.z()); + } + } +} diff --git a/src/main/resources/modid.mixins.json b/src/main/resources/modid.mixins.json index 1682925..cf6affe 100644 --- a/src/main/resources/modid.mixins.json +++ b/src/main/resources/modid.mixins.json @@ -4,9 +4,14 @@ "package": "codes.ztereohype.example.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "ExampleMixin" + "ExampleMixin", + "MixinKeyboardHandler" + ], + "client": [ + "LightTextureInvoker", + "MixinDebug", + "MixinLightTexutre" ], - "client": [], "server": [], "injectors": { "defaultRequire": 1 diff --git a/src/test/java/codes/ztereohype/example/PerformanceTest.java b/src/test/java/codes/ztereohype/example/PerformanceTest.java new file mode 100644 index 0000000..6060ea5 --- /dev/null +++ b/src/test/java/codes/ztereohype/example/PerformanceTest.java @@ -0,0 +1,13 @@ +package codes.ztereohype.example; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +public class PerformanceTest { + @Test + public void test() { + + } +} diff --git a/src/test/java/codes/ztereohype/example/performanceTest.java b/src/test/java/codes/ztereohype/example/performanceTest.java deleted file mode 100644 index 25f97c2..0000000 --- a/src/test/java/codes/ztereohype/example/performanceTest.java +++ /dev/null @@ -1,8 +0,0 @@ -package codes.ztereohype.example; - -import org.junit.jupiter.api.Test; - -//public class performanceTest { -// @Test -// public void -//}