Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
Dynamic Shading system
Browse files Browse the repository at this point in the history
allows you to use your brightness slider with Methane as well.
  • Loading branch information
AnOpenSauceDev committed Feb 2, 2024
1 parent a8bcc1b commit 4cff23d
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/modrinth/methane/MethaneSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
public class MethaneSettings implements ConfigData {
//TODO: we should transition to translatable comments (@Comment(Text.translatable("YOUR.THING.HERE")))


@Comment("auto-magically darkens the world at night!")
public boolean dynamicShading = true;

@Comment("Methane's initial state. (You should set this to 'Yes' if you use sodium.)")
public boolean modstate = true;

Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/modrinth/methane/client/BrightnessUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.modrinth.methane.client;

import com.github.anopensaucedev.libmcdevfabric.Debug;
import com.modrinth.methane.Methane;
import com.modrinth.methane.mixin.accessor.WorldRendererBuiltChunkStorageAccessor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.BuiltChunkStorage;

import java.util.Arrays;

public class BrightnessUtil {

// our default brightness value * the user's gamma
public static float DEFAULT_LIGHT_VALUE = grabBaseGamma(); // 1.0F = fully lit, 0.0F = complete darkness

public static float grabBaseGamma(){
float f = MinecraftClient.getInstance().options.getGamma().getValue().floatValue() + 0.1f;
f += 1.0f;
return f / 2;
}

public static float calculateBrightnessScale(){
MinecraftClient client = MinecraftClient.getInstance();

float scale = 1.0F; // 1 = normal, 0 = pitch black.

long time = client.world.getTimeOfDay();
if(time >= 0 && time <= 12000){ // 0 <-> 12000 ticks is when the sky is fully bright

} else if (time >= 12001 && time <= 12999) { // 12001 <-> 13000 is when the sky gets a bit darker
scale = 0.9F;
} else if (time >= 13000 && time <= 17000) { // darker... (mobs start spawning!)
scale = 0.5F;
} else if (time >= 17001 && time <= 20000) { // darker...
scale = 0.25F;
} else if (time >= 20001 && time <= 23000) { // lighter!
scale = 0.5F;
} else if (time >= 23001 && time <= 24000) { // day!
scale = 1.0F;
}



return scale;
}

public static void rebuildChunks(MinecraftClient client){
BuiltChunkStorage storage = ((WorldRendererBuiltChunkStorageAccessor) client.worldRenderer).getChunks();
Arrays.stream(storage.chunks).parallel().forEach(builtChunk -> {
builtChunk.scheduleRebuild(true);
});
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/modrinth/methane/client/MethaneClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class MethaneClient implements ClientModInitializer {

public static final Identifier METHANE_RESP_PACKET = new Identifier("methane_server","pong");

public int REBUILD_TICKS_THRESHOLD = 20*20; // rebuild every x seconds
static int ticks = 0;

public static boolean intToBoolConversion(int i){
return i != 0; // if "i" is not zero, return true
Expand All @@ -39,6 +41,7 @@ public void onInitializeClient() {




ClientPlayNetworking.registerGlobalReceiver(METHANE_STATE_PACKET, ((client, handler, buf, responseSender) -> {


Expand Down Expand Up @@ -71,12 +74,23 @@ public void onInitializeClient() {
);



ClientTickEvents.END_CLIENT_TICK.register(client -> {

ticks++;


if(Methane.ModActive && Methane.settings.dynamicShading && client.player != null){
if(ticks > REBUILD_TICKS_THRESHOLD){
BrightnessUtil.rebuildChunks(client); // has a *very* tiny performance impact, that only happens once every 20 seconds. It's also multithreaded!
ticks = 0;
}
}


if(client.player == null){ // I'm assuming that ClientPlayerEntity is only ever null if you quit the server.
Methane.ServerForbidsChanging = false;

}else if(Methane.playerBlockingPacket){ // I wanted to avoid this at all costs, but it looks like I have to do this hack.

Methane.playerBlockingPacket = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public void doLightUpdates(CallbackInfoReturnable<Integer> cir) {
if(Methane.ModActive && !Methane.settings.useOldLightingEngine) cir.cancel();
}


@Inject(method = "getLight", at = @At("HEAD"),cancellable = true)
public void huh(BlockPos pos, int ambientDarkness, CallbackInfoReturnable<Integer> cir){
if(Methane.ModActive) {
cir.setReturnValue(15);
cir.cancel();
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.modrinth.methane.mixin.accessor;

import net.minecraft.client.render.BuiltChunkStorage;
import net.minecraft.client.render.WorldRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(WorldRenderer.class)
public interface WorldRendererBuiltChunkStorageAccessor {

@Accessor
BuiltChunkStorage getChunks();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.modrinth.methane.mixin.getLightLevelHack;

import com.modrinth.methane.Methane;
import com.modrinth.methane.client.BrightnessUtil;
import net.minecraft.client.render.chunk.ChunkRendererRegion;
import net.minecraft.util.math.Direction;
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.CallbackInfoReturnable;

import static com.modrinth.methane.client.BrightnessUtil.calculateBrightnessScale;

@Mixin(ChunkRendererRegion.class)
public class ChunkRenderReigonMixin {


//impl. of: BlockRenderView$getBrightness
@Inject(method = "getBrightness", at = @At("HEAD"), cancellable = true)
public void MethaneSetCustomLightLevel(Direction direction, boolean shaded, CallbackInfoReturnable<Float> cir){
if(Methane.ModActive && Methane.settings.dynamicShading) {
cir.setReturnValue(BrightnessUtil.grabBaseGamma() * calculateBrightnessScale()); // only gets darker, never brighter. 1.0F = fully lit.
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.modrinth.methane.mixin.getLightLevelHack;

import com.modrinth.methane.Methane;
import com.modrinth.methane.client.BrightnessUtil;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.math.Direction;
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.CallbackInfoReturnable;

import static com.modrinth.methane.client.BrightnessUtil.calculateBrightnessScale;

@Mixin(ClientWorld.class)
public class ClientWorldMixin {

//TODO: lerp brightness to be less jarring

@Shadow @Final private MinecraftClient client;



@Inject(method = "getBrightness", at = @At("HEAD"), cancellable = true)
public void MethaneSetCustomLightLevel(Direction direction, boolean shaded, CallbackInfoReturnable<Float> cir){
if(Methane.ModActive && Methane.settings.dynamicShading) {
cir.setReturnValue(BrightnessUtil.grabBaseGamma() * calculateBrightnessScale()); // only gets darker, never brighter. 1.0F = fully lit.
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/assets/methane/lang/en_au.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"text.autoconfig.methane.option.fogSettings.disableThickFog": "Disable Nether Fog",
"text.autoconfig.methane.option.fogSettings.disableSkyFog": "Disable Sky Fog",
"text.autoconfig.methane.option.fogSettings.persistFogSettings": "Persist Fog?",
"text.autoconfig.methane.option.dynamicShading": "Enable Dynamic Shading?",
"text.autoconfig.methane.option.fogSettings": "Fog Overrides",
"text.autoconfig.methane.option.modstate": "Start with Methane on? ",
"text.autoconfig.methane.option.brightness": "(experimental) Default terrain brightness: ",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/methane/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"text.autoconfig.methane.option.fogSettings.disableThickFog": "Disable Nether Fog",
"text.autoconfig.methane.option.fogSettings.disableSkyFog": "Disable Sky Fog",
"text.autoconfig.methane.option.fogSettings.persistFogSettings": "Persist Fog?",
"text.autoconfig.methane.option.dynamicShading": "Enable Dynamic Shading?",
"text.autoconfig.methane.option.fogSettings": "Fog Overrides",
"text.autoconfig.methane.option.modstate": "Start with Methane on? ",
"text.autoconfig.methane.option.brightness": "(experimental) Default terrain brightness: ",
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/methane.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"LightingUpdateMixin",
"SaneParticles",
"TitleScreenMixin",
"WorldRendererMixin"
"WorldRendererMixin",
"accessor.WorldRendererBuiltChunkStorageAccessor",
"getLightLevelHack.ChunkRenderReigonMixin",
"getLightLevelHack.ClientWorldMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 4cff23d

Please sign in to comment.