This commit is contained in:
ZtereoHYPE 2022-07-21 00:17:45 +02:00
parent 8f7e5a5f57
commit b15b7a1d87
11 changed files with 103 additions and 242 deletions

View file

@ -1,13 +1,11 @@
package codes.ztereohype.example; package codes.ztereohype.example;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("modid"); public static boolean toggle = true;
public static boolean toggle = true;
@Override @Override
public void onInitialize() {} public void onInitialize() {
}
} }

View file

@ -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<Star> 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();
}
}

View file

@ -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();
// }
}

View file

@ -1,15 +1,12 @@
package codes.ztereohype.example.mixin; package codes.ztereohype.example.mixin;
import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LightTexture; 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.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;
@Mixin(LightTexture.class) @Mixin(LightTexture.class)
public interface LightTextureInvoker { public interface LightTextureInvoker {
@Accessor NativeImage getLightPixels(); @Accessor
NativeImage getLightPixels();
} }

View file

@ -1,7 +1,7 @@
package codes.ztereohype.example.mixin; package codes.ztereohype.example.mixin;
import codes.ztereohype.example.ExampleMod; import codes.ztereohype.example.ExampleMod;
import codes.ztereohype.example.StarManager; import codes.ztereohype.example.sky.StarManager;
import net.minecraft.client.KeyboardHandler; import net.minecraft.client.KeyboardHandler;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@ -11,10 +11,11 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(KeyboardHandler.class) @Mixin(KeyboardHandler.class)
public class MixinKeyboardHandler { public class MixinKeyboardHandler {
private long cooldown = 0; private long cooldown = 0;
@Inject(at = @At("HEAD"), method = "keyPress") @Inject(at = @At("HEAD"), method = "keyPress")
private void printKey(long windowPointer, int key, int scanCode, int action, int modifiers, CallbackInfo ci) { private void printKey(long windowPointer, int key, int scanCode, int action, int modifiers, CallbackInfo ci) {
if (key == 92) { if (key == 92) {
// StarManager.generateStarList(); StarManager.generateStarList();
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
if (time - cooldown > 200) { if (time - cooldown > 200) {
ExampleMod.toggle = !ExampleMod.toggle; ExampleMod.toggle = !ExampleMod.toggle;

View file

@ -4,38 +4,14 @@ import codes.ztereohype.example.ExampleMod;
import com.mojang.math.Vector3f; import com.mojang.math.Vector3f;
import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.LightTexture;
import net.minecraft.util.Mth;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; 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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
@Mixin(LightTexture.class) @Mixin(LightTexture.class)
public class MixinLightTexutre { 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( @Inject(
method = "updateLightTexture", method = "updateLightTexture",
at = @At(value = "INVOKE", target = "Lcom/mojang/math/Vector3f;clamp(FF)V", shift = At.Shift.BEFORE, ordinal = 2), 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) { 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(?) //todo: find a way to make a touch more saturated and brighter(?)
if (ExampleMod.toggle) { 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 float warmness = o / 15f * // increase w/ blocklight
(1f - vector3f.x() * (1 - n/15f)) * // decrease in skylight w/ dayness (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 Math.min((15 - o) / 9f, 1f); // decrease for the 3 highest block light levels
warmTint.mul(warmness); warmTint.mul(warmness);
warmTint.add(1f, 1f, 1f); warmTint.add(1f, 1f, 1f);
@ -59,6 +35,8 @@ public class MixinLightTexutre {
vector3f2.mul(dramaticFactor.x(), dramaticFactor.y(), dramaticFactor.z()); vector3f2.mul(dramaticFactor.x(), dramaticFactor.y(), dramaticFactor.z());
vector3f2.mul(warmTint.x(), warmTint.y(), warmTint.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
} }
} }
} }

View file

@ -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);
}
}

View file

@ -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 net.minecraft.util.Mth;
import java.util.ArrayList;
import java.util.List;
public class Star { public class Star {
private final float xCoord; private final float xCoord;
private final float yCoord; private final float yCoord;
@ -46,18 +44,16 @@ public class Star {
} }
public void tick(int ticks) { public void tick(int ticks) {
currentAngle += 0.007f * twinkleSpeed; currentAngle += 0.07f * twinkleSpeed;
currentRadius = Mth.lerp(Mth.sin(ticks * twinkleSpeed / 10f) * 0.5f + 0.5f, minRadius, maxRadius); currentRadius = Mth.lerp(Mth.sin(ticks * twinkleSpeed) * 0.5f + 0.5f, minRadius, maxRadius);
} }
//return 4*3 coords for 4 vertices //return 4*3 coords for 4 vertices
public float[] getVertices() { public void setVertices(BufferBuilder bufferBuilder) {
float[] vertices = new float[12];
float cosRot = Mth.cos(currentAngle); float cosRot = Mth.cos(currentAngle);
float sinRot = Mth.sin(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: // shift the vector to the 4 corners:
// vec 0, 1 --> -rad; vec 2, 3 --> +rad // vec 0, 1 --> -rad; vec 2, 3 --> +rad
float xShift = ((v & 2) - 1) * currentRadius; 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 // magic projection fuckery to turn the shift into an offset applying rotation and polar bs
float aa = xShift * cosRot - yShift * sinRot; float aa = xShift * cosRot - yShift * sinRot;
float ab = yShift * cosRot + xShift * sinRot; float ab = yShift * cosRot + xShift * sinRot;
float ae = - aa * projCos; float ae = -aa * projCos;
float yOffset = aa * projSin; float yOffset = aa * projSin;
float xOffset = ae * sinPolarAngle - ab * cosPolarAngle; float xOffset = ae * sinPolarAngle - ab * cosPolarAngle;
float zOffset = ab * sinPolarAngle + ae * cosPolarAngle; float zOffset = ab * sinPolarAngle + ae * cosPolarAngle;
vertices[v * 3 ] = xCoord + xOffset; bufferBuilder.vertex(xCoord + xOffset, yCoord + yOffset, zCoord + zOffset).endVertex();
vertices[v * 3 + 1] = yCoord + yOffset;
vertices[v * 3 + 2] = zCoord + zOffset;
} }
return vertices;
} }
} }

View file

@ -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<Star> 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();
}
}

View file

@ -2,7 +2,6 @@
"schemaVersion": 1, "schemaVersion": 1,
"id": "modid", "id": "modid",
"version": "${version}", "version": "${version}",
"name": "Example Mod", "name": "Example Mod",
"description": "This is an example description! Tell everyone what your mod is about!", "description": "This is an example description! Tell everyone what your mod is about!",
"authors": [ "authors": [
@ -12,10 +11,8 @@
"homepage": "https://fabricmc.net/", "homepage": "https://fabricmc.net/",
"sources": "https://github.com/FabricMC/fabric-example-mod" "sources": "https://github.com/FabricMC/fabric-example-mod"
}, },
"license": "CC0-1.0", "license": "CC0-1.0",
"icon": "assets/modid/icon.png", "icon": "assets/modid/icon.png",
"environment": "*", "environment": "*",
"entrypoints": { "entrypoints": {
"main": [ "main": [
@ -25,7 +22,6 @@
"mixins": [ "mixins": [
"modid.mixins.json" "modid.mixins.json"
], ],
"depends": { "depends": {
"fabricloader": ">=0.14.6", "fabricloader": ">=0.14.6",
"minecraft": "~1.19", "minecraft": "~1.19",

View file

@ -3,11 +3,10 @@
"minVersion": "0.8", "minVersion": "0.8",
"package": "codes.ztereohype.example.mixin", "package": "codes.ztereohype.example.mixin",
"compatibilityLevel": "JAVA_17", "compatibilityLevel": "JAVA_17",
"mixins": [ "mixins": [],
"ExampleMixin",
"MixinKeyboardHandler"
],
"client": [ "client": [
"MixinKeyboardHandler",
"MixinStarRendering",
"LightTextureInvoker", "LightTextureInvoker",
"MixinDebug", "MixinDebug",
"MixinLightTexutre" "MixinLightTexutre"