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

feat: Sunset/sunrise fog implementation. #58

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat(sunset): 🌇 Finalize sunset fog
  • Loading branch information
IMB11 committed Dec 27, 2024
commit 6e9f759ce742efa370b5f6e1fa18c3ad8abb442e
58 changes: 18 additions & 40 deletions src/main/java/dev/imb11/fog/client/FogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import dev.imb11.fog.config.FogConfig;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.CubicSampler;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.LightType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -233,64 +235,40 @@ public float getUndergroundFactor(@NotNull MinecraftClient client, float deltaTi
fogEndValue *= this.currentEndMultiplier.get(tickDelta);

// Sunset
float[] sunsetAdjustedColors = applySunsetLogic(client, fogRed, fogGreen, fogBlue);
float[] sunsetAdjustedColors = applySunsetLogic(client, fogRed, fogGreen, fogBlue, tickDelta);
fogRed = sunsetAdjustedColors[0];
fogGreen = sunsetAdjustedColors[1];
fogBlue = sunsetAdjustedColors[2];

return new FogSettings(fogStartValue, fogEndValue, fogRed, fogGreen, fogBlue);
}

public float sunsetSunriseBlendFactor = 0.0F;

/**
* Applies sunset color blending similar to vanilla Minecraft's implementation.
*/
private float[] applySunsetLogic(MinecraftClient client, float red, float green, float blue) {
private float[] applySunsetLogic(MinecraftClient client, float red, float green, float blue, float tickDelta) {
if (!FogConfig.getInstance().disableSunsetFog && client.world != null) {
// Retrieve the current dimension type (e.g., Overworld, Nether, End)
float skyAngle = client.world.getSkyAngle(1.0F); // 1.0F for exact time

// Use the isSunRisingOrSetting method to check for sunrise/sunset
if (client.world.getDimensionEffects().isSunRisingOrSetting(skyAngle)) {
// Retrieve the sunset color from configuration
Color sunsetColor = Color.from(FogConfig.getInstance().sunsetColor);
float skyAngle = client.world.getSkyAngle(tickDelta);
float blendSpeed = 0.0005F; // Speed of blending

// Calculate the blend factor based on how close the sky angle is to sunset
float blendFactor = calculateSunsetBlendFactor(client.world);
Vec3d sunColor = Vec3d.unpackRgb(client.world.getDimensionEffects().getSkyColor(skyAngle));

// Interpolate each color component towards the sunset color
red = MathHelper.lerp(blendFactor, red, sunsetColor.red / 255f);
green = MathHelper.lerp(blendFactor, green, sunsetColor.green / 255f);
blue = MathHelper.lerp(blendFactor, blue, sunsetColor.blue / 255f);
if (client.world.getDimensionEffects().isSunRisingOrSetting(skyAngle)) {
sunsetSunriseBlendFactor = Math.min(sunsetSunriseBlendFactor + (blendSpeed * tickDelta), 1.0F);
} else {
sunsetSunriseBlendFactor = Math.max(sunsetSunriseBlendFactor - (blendSpeed * tickDelta), 0.0F);
}

// Interpolate each color component towards the sunset color
red = MathHelper.lerp(sunsetSunriseBlendFactor, red, (float) sunColor.x);
green = MathHelper.lerp(sunsetSunriseBlendFactor, green, (float) sunColor.y);
blue = MathHelper.lerp(sunsetSunriseBlendFactor, blue, (float) sunColor.z);
}

return new float[]{red, green, blue};
}

/**
* Calculates the blending factor for sunset based on the world's sky angle.
*/
private float calculateSunsetBlendFactor(ClientWorld world) {
long time = world.getTimeOfDay() % 24000;

float blendFactor = 0.0f;

final int SUNSET_START = 12000;
final int SUNSET_END = 13850;
final int SUNSET_MIDPOINT = (SUNSET_START + SUNSET_END) / 2;
final float SUNSET_DURATION = (SUNSET_MIDPOINT - SUNSET_START);

// Sunset period: blendFactor increases from 0.0 to 1.0 and back to 0.0
if (time >= SUNSET_START && time <= SUNSET_END) {
if (time <= SUNSET_MIDPOINT) { // First half: 0.0 to 1.0
blendFactor = (float) (time - SUNSET_START) / SUNSET_DURATION;
} else { // Second half: 1.0 to 0.0
blendFactor = (float) (SUNSET_END - time) / SUNSET_DURATION;
}
blendFactor = MathHelper.clamp(blendFactor, 0.0f, 1.0f);
}
return blendFactor;
}

public record FogSettings(double fogStart, double fogEnd, float fogRed, float fogGreen, float fogBlue) {}
}
5 changes: 5 additions & 0 deletions src/main/java/dev/imb11/fog/client/util/color/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.imb11.fog.client.util.math.MathUtil;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
Expand Down Expand Up @@ -43,6 +44,10 @@ public static Color from(java.awt.Color awtColor) {
return colorCache.computeIfAbsent(awtColor, color -> new Color(color.getRed(), color.getGreen(), color.getBlue()));
}

public Vec3d asVec3d() {
return new Vec3d(red / 255.0f, green / 255.0f, blue / 255.0f);
}

@Override
public String toString() {
return String.format("Color(%s, %s, %s)", red, green, blue);
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/dev/imb11/fog/config/FogConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public class FogConfig {
@SerialEntry
public Color newMoonColor = new Color(0, 0, 0, 255);
@SerialEntry
public Color sunsetColor = new Color(206, 113, 45, 255);
@SerialEntry
public boolean disableSunsetFog = false;

public static @NotNull FogConfig getInstance() {
Expand Down Expand Up @@ -167,9 +165,6 @@ public static void save() {
"disable_sunset_fog", defaults.disableSunsetFog,
() -> config.disableSunsetFog, val -> config.disableSunsetFog = val
))
.option(HELPER.get(
"sunset_color", defaults.sunsetColor, () -> config.sunsetColor, val -> config.sunsetColor = val
))
.option(Option.<Boolean>createBuilder().name(
HELPER.getText(EntryType.OPTION_NAME, "disable_cloud_whitening")).description(
initialFogStart -> OptionDescription.createBuilder().text(
Expand Down