diff --git a/src/main/java/codes/ztereohype/example/ExampleMod.java b/src/main/java/codes/ztereohype/example/ExampleMod.java index a6e9a4e..fb45bc8 100644 --- a/src/main/java/codes/ztereohype/example/ExampleMod.java +++ b/src/main/java/codes/ztereohype/example/ExampleMod.java @@ -1,13 +1,11 @@ package codes.ztereohype.example; import net.fabricmc.api.ModInitializer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class ExampleMod implements ModInitializer { - public static final Logger LOGGER = LoggerFactory.getLogger("modid"); - public static boolean toggle = true; + public static boolean toggle = true; - @Override - public void onInitialize() {} + @Override + public void onInitialize() { + } } diff --git a/src/main/java/codes/ztereohype/example/StarManager.java b/src/main/java/codes/ztereohype/example/StarManager.java deleted file mode 100644 index 4a16bdf..0000000 --- a/src/main/java/codes/ztereohype/example/StarManager.java +++ /dev/null @@ -1,129 +0,0 @@ -package codes.ztereohype.example; - -import com.mojang.blaze3d.systems.RenderSystem; -import com.mojang.blaze3d.vertex.*; -import net.minecraft.client.renderer.GameRenderer; -import net.minecraft.util.Mth; -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 { - private static final int STAR_ATTEMPTS = 1500; - public static ArrayList starList = new ArrayList<>(); - - public static BufferBuilder.RenderedBuffer generateStars(BufferBuilder bufferBuilder, long seed) { - RandomSource randomSource = RandomSource.create(seed); - bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); - - for(int i = 0; i < 1500; ++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; - - // 0.15..0.25 ??? - double starRadius = 0.15F + randomSource.nextFloat() * 0.5F; - - double squaredDistance = randX * randX + randY * randY + randZ * randZ; - if (squaredDistance < 1.0 && squaredDistance > 0.01) { - double invsqrtDistance = Mth.fastInvSqrt(squaredDistance); - - // star center coords - double xCoord = randX * invsqrtDistance * 100.0; - double yCoord = randY * invsqrtDistance * 100.0; - double zCoord = randZ * invsqrtDistance * 100.0; - - // rad angle of polar coords - double polarAngle = Math.atan2(randX, randZ); - double sinPolarAngle = Math.sin(polarAngle); - double cosPolarAngle = Math.cos(polarAngle); - - // magic projection fuckery?? - double p = Math.atan2(Math.sqrt(randX * randX + randZ * randZ), randY); - double q = Math.sin(p); - double r = Math.cos(p); - - // random rotation in rad - double rot = randomSource.nextDouble() * Math.PI * 2.0; - double sinRot = Math.sin(rot); - double cosRot = Math.cos(rot); - - for(int v = 0; v < 4; ++v) { - // x: - // 0 0 0 0 & 0 0 1 0 --> 0 --> -.20 - // 0 0 0 1 & 0 0 1 0 --> 0 --> -.20 - // 0 0 1 0 & 0 0 1 0 --> 2 --> +.20 - // 0 0 1 1 & 0 0 1 0 --> 2 --> +.20 - - // y: - // 0 0 0 1 & 0 0 1 0 --> 0 --> -.20 - // 0 0 1 0 & 0 0 1 0 --> 2 --> +.20 - // 0 0 1 1 & 0 0 1 0 --> 2 --> +.20 - // 0 1 0 0 & 0 0 1 0 --> 0 --> -.20 - - // shift the vector to the 4 corners: - // vec 0, 1 --> -rad; vec 2, 3 --> +rad - double xShift = (double)((v & 2) - 1) * starRadius; - // vec 1, 2 --> +rad; vec 3, 0 --> -rad - double yShift = (double)(((v + 1) & 2) - 1) * starRadius; - - // 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 = 0.0 * q - aa * r; - double yOffset = aa * q + 0.0 * r; - double xOffset = ae * sinPolarAngle - ab * cosPolarAngle; - double zOffset = ab * sinPolarAngle + ae * cosPolarAngle; - - bufferBuilder.vertex(xCoord + xOffset, yCoord + yOffset, zCoord + zOffset).endVertex(); - } - } - } - - return bufferBuilder.end(); - } - - public static void generateStarList() { - RandomSource randomSource = RandomSource.create(123L); - starList.clear(); - - for(int i = 0; i < STAR_ATTEMPTS; ++i) { - // -1..1 - 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 ??? - 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.3f + randomSource.nextFloat() * 0.4f)); - } - } - } - - public static void updateStars(int ticks, VertexBuffer starBuffer) { - if (!ExampleMod.toggle) return; - BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder(); - bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); -// - for (Star star : starList) { - star.tick(ticks); - - 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(); - starBuffer.upload(bufferBuilder.end()); - VertexBuffer.unbind(); - } -} diff --git a/src/main/java/codes/ztereohype/example/mixin/ExampleMixin.java b/src/main/java/codes/ztereohype/example/mixin/ExampleMixin.java deleted file mode 100644 index 84ad198..0000000 --- a/src/main/java/codes/ztereohype/example/mixin/ExampleMixin.java +++ /dev/null @@ -1,49 +0,0 @@ -package codes.ztereohype.example.mixin; - -import codes.ztereohype.example.ExampleMod; -import codes.ztereohype.example.StarManager; -import com.mojang.blaze3d.vertex.*; -import com.mojang.math.Matrix4f; -import net.minecraft.client.Camera; -import net.minecraft.client.gui.screens.TitleScreen; -import net.minecraft.client.renderer.LevelRenderer; -import net.minecraft.util.RandomSource; -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.ModifyArg; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(LevelRenderer.class) -public class ExampleMixin { - @Shadow private VertexBuffer starBuffer; - @Shadow private int ticks; - - @Inject(at = @At("HEAD"), method = "createStars", cancellable = true) - private void generateStars(CallbackInfo ci) { - StarManager.generateStarList(); - starBuffer = new VertexBuffer(); - StarManager.updateStars(0, starBuffer); - ci.cancel(); - } - - @Inject(at = @At("HEAD"), method = "tick") - private void tickStars(CallbackInfo ci) { - StarManager.updateStars(ticks, starBuffer); - } - -// @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) { -//// 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(); -// } -} diff --git a/src/main/java/codes/ztereohype/example/mixin/LightTextureInvoker.java b/src/main/java/codes/ztereohype/example/mixin/LightTextureInvoker.java index 35a5ccd..17f3b1f 100644 --- a/src/main/java/codes/ztereohype/example/mixin/LightTextureInvoker.java +++ b/src/main/java/codes/ztereohype/example/mixin/LightTextureInvoker.java @@ -1,15 +1,12 @@ 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(); + @Accessor + NativeImage getLightPixels(); } diff --git a/src/main/java/codes/ztereohype/example/mixin/MixinKeyboardHandler.java b/src/main/java/codes/ztereohype/example/mixin/MixinKeyboardHandler.java index edbd232..36dcfa0 100644 --- a/src/main/java/codes/ztereohype/example/mixin/MixinKeyboardHandler.java +++ b/src/main/java/codes/ztereohype/example/mixin/MixinKeyboardHandler.java @@ -1,7 +1,7 @@ package codes.ztereohype.example.mixin; import codes.ztereohype.example.ExampleMod; -import codes.ztereohype.example.StarManager; +import codes.ztereohype.example.sky.StarManager; import net.minecraft.client.KeyboardHandler; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -11,10 +11,11 @@ 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(); + StarManager.generateStarList(); long time = System.currentTimeMillis(); if (time - cooldown > 200) { ExampleMod.toggle = !ExampleMod.toggle; diff --git a/src/main/java/codes/ztereohype/example/mixin/MixinLightTexutre.java b/src/main/java/codes/ztereohype/example/mixin/MixinLightTexutre.java index ae3b07a..e580abb 100644 --- a/src/main/java/codes/ztereohype/example/mixin/MixinLightTexutre.java +++ b/src/main/java/codes/ztereohype/example/mixin/MixinLightTexutre.java @@ -4,38 +4,14 @@ 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), @@ -44,11 +20,11 @@ public class MixinLightTexutre { 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); + Vector3f warmTint = new Vector3f(0.36F, 0.13F, -0.15F); 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 + (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); @@ -59,6 +35,8 @@ public class MixinLightTexutre { vector3f2.mul(dramaticFactor.x(), dramaticFactor.y(), dramaticFactor.z()); vector3f2.mul(warmTint.x(), warmTint.y(), warmTint.z()); + +// if (o == 0 && vector3f.x() < 0.1f) vector3f2.mul(0.96f, 0.96f, 1.05f); //todo: smoothly make night bluer no ifs } } } diff --git a/src/main/java/codes/ztereohype/example/mixin/MixinStarRendering.java b/src/main/java/codes/ztereohype/example/mixin/MixinStarRendering.java new file mode 100644 index 0000000..020157b --- /dev/null +++ b/src/main/java/codes/ztereohype/example/mixin/MixinStarRendering.java @@ -0,0 +1,29 @@ +package codes.ztereohype.example.mixin; + +import codes.ztereohype.example.sky.StarManager; +import com.mojang.blaze3d.vertex.VertexBuffer; +import net.minecraft.client.renderer.LevelRenderer; +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(LevelRenderer.class) +public class MixinStarRendering { + @Shadow private VertexBuffer starBuffer; + @Shadow private int ticks; + + @Inject(at = @At("HEAD"), method = "createStars", cancellable = true) + private void generateStars(CallbackInfo ci) { + StarManager.generateStarList(); + starBuffer = new VertexBuffer(); + StarManager.updateStars(0, starBuffer); + ci.cancel(); + } + + @Inject(at = @At("HEAD"), method = "tick") + private void tickStars(CallbackInfo ci) { + StarManager.updateStars(ticks, starBuffer); + } +} diff --git a/src/main/java/codes/ztereohype/example/Star.java b/src/main/java/codes/ztereohype/example/sky/Star.java similarity index 80% rename from src/main/java/codes/ztereohype/example/Star.java rename to src/main/java/codes/ztereohype/example/sky/Star.java index 94d6913..f2de1e0 100644 --- a/src/main/java/codes/ztereohype/example/Star.java +++ b/src/main/java/codes/ztereohype/example/sky/Star.java @@ -1,10 +1,8 @@ -package codes.ztereohype.example; +package codes.ztereohype.example.sky; +import com.mojang.blaze3d.vertex.BufferBuilder; import net.minecraft.util.Mth; -import java.util.ArrayList; -import java.util.List; - public class Star { private final float xCoord; private final float yCoord; @@ -46,18 +44,16 @@ public class Star { } public void tick(int ticks) { - currentAngle += 0.007f * twinkleSpeed; - currentRadius = Mth.lerp(Mth.sin(ticks * twinkleSpeed / 10f) * 0.5f + 0.5f, minRadius, maxRadius); + currentAngle += 0.07f * twinkleSpeed; + currentRadius = Mth.lerp(Mth.sin(ticks * twinkleSpeed) * 0.5f + 0.5f, minRadius, maxRadius); } //return 4*3 coords for 4 vertices - public float[] getVertices() { - float[] vertices = new float[12]; - + public void setVertices(BufferBuilder bufferBuilder) { float cosRot = Mth.cos(currentAngle); float sinRot = Mth.sin(currentAngle); - for(int v = 0; v < 4; ++v) { + for (int v = 0; v < 4; ++v) { // shift the vector to the 4 corners: // vec 0, 1 --> -rad; vec 2, 3 --> +rad float xShift = ((v & 2) - 1) * currentRadius; @@ -67,16 +63,12 @@ public class Star { // magic projection fuckery to turn the shift into an offset applying rotation and polar bs float aa = xShift * cosRot - yShift * sinRot; float ab = yShift * cosRot + xShift * sinRot; - float ae = - aa * projCos; + 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; - vertices[v * 3 + 2] = zCoord + zOffset; + bufferBuilder.vertex(xCoord + xOffset, yCoord + yOffset, zCoord + zOffset).endVertex(); } - - return vertices; } } diff --git a/src/main/java/codes/ztereohype/example/sky/StarManager.java b/src/main/java/codes/ztereohype/example/sky/StarManager.java new file mode 100644 index 0000000..8871007 --- /dev/null +++ b/src/main/java/codes/ztereohype/example/sky/StarManager.java @@ -0,0 +1,49 @@ +package codes.ztereohype.example.sky; + +import codes.ztereohype.example.ExampleMod; +import com.mojang.blaze3d.vertex.*; +import net.minecraft.util.RandomSource; + +import java.util.ArrayList; + +//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 { + private static final int STAR_ATTEMPTS = 1500; + public static ArrayList starList = new ArrayList<>(); + + //todo: add a noise for patterns and star size + public static void generateStarList() { + RandomSource randomSource = RandomSource.create(123L); + starList.clear(); + + for (int i = 0; i < STAR_ATTEMPTS; ++i) { + // -1..1 + 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 ??? + 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.03f + randomSource.nextFloat() * 0.04f)); + } + } + } + + public static void updateStars(int ticks, VertexBuffer starBuffer) { + if (!ExampleMod.toggle) return; + BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder(); + bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); + + for (Star star : starList) { + star.tick(ticks); + star.setVertices(bufferBuilder); + } + + starBuffer.bind(); + starBuffer.upload(bufferBuilder.end()); + VertexBuffer.unbind(); + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 120beb1..c5e0f55 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -2,7 +2,6 @@ "schemaVersion": 1, "id": "modid", "version": "${version}", - "name": "Example Mod", "description": "This is an example description! Tell everyone what your mod is about!", "authors": [ @@ -12,10 +11,8 @@ "homepage": "https://fabricmc.net/", "sources": "https://github.com/FabricMC/fabric-example-mod" }, - "license": "CC0-1.0", "icon": "assets/modid/icon.png", - "environment": "*", "entrypoints": { "main": [ @@ -25,7 +22,6 @@ "mixins": [ "modid.mixins.json" ], - "depends": { "fabricloader": ">=0.14.6", "minecraft": "~1.19", diff --git a/src/main/resources/modid.mixins.json b/src/main/resources/modid.mixins.json index cf6affe..75e64e3 100644 --- a/src/main/resources/modid.mixins.json +++ b/src/main/resources/modid.mixins.json @@ -3,11 +3,10 @@ "minVersion": "0.8", "package": "codes.ztereohype.example.mixin", "compatibilityLevel": "JAVA_17", - "mixins": [ - "ExampleMixin", - "MixinKeyboardHandler" - ], + "mixins": [], "client": [ + "MixinKeyboardHandler", + "MixinStarRendering", "LightTextureInvoker", "MixinDebug", "MixinLightTexutre"