| 1 | package net.minecraft.client.renderer.fog.environment;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Camera;
|
| 6 | import net.minecraft.client.DeltaTracker;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 9 | import net.minecraft.client.renderer.fog.FogData;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.util.ARGB;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.level.LightLayer;
|
| 16 | import net.minecraft.world.level.biome.Biome;
|
| 17 | import net.minecraft.world.level.material.FogType;
|
| 18 | import org.joml.Vector3f;
|
| 19 | import org.joml.Vector3fc;
|
| 20 | import org.joml.Vector4fc;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class AtmosphericFogEnvironment extends FogEnvironment {
|
| 25 | private static final int MIN_RAIN_FOG_SKY_LIGHT = 8;
|
| 26 | private static final float RAIN_FOG_START_OFFSET = -160.0F;
|
| 27 | private static final float RAIN_FOG_END_OFFSET = -256.0F;
|
| 28 | private float rainFogMultiplier;
|
| 29 |
|
| 30 | @Override
|
| 31 | public Vector3fc getBaseColor(final ClientLevel level, final Camera camera, final int renderDistance, final float partialTicks) {
|
| 32 | Vector3fc fogColor = camera.attributeProbe().getValue(EnvironmentAttributes.FOG_COLOR, partialTicks);
|
| 33 | if (renderDistance >= 4) {
|
| 34 | float sunAngle = camera.attributeProbe().getValue(EnvironmentAttributes.SUN_ANGLE, partialTicks) * (float) (Math.PI / 180.0);
|
| 35 | float sunX = Mth.sin(sunAngle) > 0.0F ? -1.0F : 1.0F;
|
| 36 | Vector3fc forwardVector = camera.isPanoramicMode() ? camera.panoramicForwards() : camera.forwardVector();
|
| 37 | float lookingAtTheSunFactor = forwardVector.dot(sunX, 0.0F, 0.0F);
|
| 38 | if (lookingAtTheSunFactor > 0.0F) {
|
| 39 | Vector4fc color = camera.attributeProbe().getValue(EnvironmentAttributes.SUNRISE_SUNSET_COLOR, partialTicks);
|
| 40 | float alpha = color.w();
|
| 41 | if (alpha > 0.0F) {
|
| 42 | fogColor = ARGB.srgbLerp(lookingAtTheSunFactor * alpha, fogColor, new Vector3f().set(color));
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | Vector3fc skyColor = camera.attributeProbe().getValue(EnvironmentAttributes.SKY_COLOR, partialTicks);
|
| 48 | skyColor = applyWeatherDarken(skyColor, level.getRainLevel(partialTicks), level.getThunderLevel(partialTicks));
|
| 49 | float skyFogEnd = Math.min(camera.attributeProbe().getValue(EnvironmentAttributes.SKY_FOG_END_DISTANCE, partialTicks) / 16.0F, renderDistance);
|
| 50 | float skyColorMixFactor = Mth.clampedLerp(skyFogEnd / 32.0F, 0.25F, 1.0F);
|
| 51 | skyColorMixFactor = 1.0F - (float)Math.pow(skyColorMixFactor, 0.25);
|
| 52 | return ARGB.srgbLerp(skyColorMixFactor, fogColor, new Vector3f().set(skyColor));
|
| 53 | }
|
| 54 |
|
| 55 | private static Vector3fc applyWeatherDarken(Vector3fc color, final float rainLevel, final float thunderLevel) {
|
| 56 | if (rainLevel > 0.0F) {
|
| 57 | float rainColorModifier = 1.0F - rainLevel * 0.5F;
|
| 58 | float rainBlueColorModifier = 1.0F - rainLevel * 0.4F;
|
| 59 | color = ARGB.scaleRGB(color, rainColorModifier, rainColorModifier, rainBlueColorModifier);
|
| 60 | }
|
| 61 |
|
| 62 | if (thunderLevel > 0.0F) {
|
| 63 | color = ARGB.scaleRGB(color, 1.0F - thunderLevel * 0.5F);
|
| 64 | }
|
| 65 |
|
| 66 | return color;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void setupFog(final FogData fog, final Camera camera, final ClientLevel level, final float renderDistance, final DeltaTracker deltaTracker) {
|
| 71 | this.updateRainFogState(camera, level, deltaTracker);
|
| 72 | float partialTicks = deltaTracker.getGameTimeDeltaPartialTick(false);
|
| 73 | fog.environmentalStart = camera.attributeProbe().getValue(EnvironmentAttributes.FOG_START_DISTANCE, partialTicks);
|
| 74 | fog.environmentalEnd = camera.attributeProbe().getValue(EnvironmentAttributes.FOG_END_DISTANCE, partialTicks);
|
| 75 | fog.environmentalStart = fog.environmentalStart + -160.0F * this.rainFogMultiplier;
|
| 76 | float minRainFogEnd = Math.min(96.0F, fog.environmentalEnd);
|
| 77 | fog.environmentalEnd = Math.max(minRainFogEnd, fog.environmentalEnd + -256.0F * this.rainFogMultiplier);
|
| 78 | fog.skyEnd = Math.min(renderDistance, camera.attributeProbe().getValue(EnvironmentAttributes.SKY_FOG_END_DISTANCE, partialTicks));
|
| 79 | fog.cloudEnd = Math.min(
|
| 80 | Minecraft.getInstance().options.cloudRange().get() * 16, camera.attributeProbe().getValue(EnvironmentAttributes.CLOUD_FOG_END_DISTANCE, partialTicks)
|
| 81 | );
|
| 82 | if (Minecraft.getInstance().gui.hud.getBossOverlay().shouldCreateWorldFog()) {
|
| 83 | fog.environmentalStart = Math.min(fog.environmentalStart, 10.0F);
|
| 84 | fog.environmentalEnd = Math.min(fog.environmentalEnd, 96.0F);
|
| 85 | fog.skyEnd = fog.environmentalEnd;
|
| 86 | fog.cloudEnd = fog.environmentalEnd;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private void updateRainFogState(final Camera camera, final ClientLevel level, final DeltaTracker deltaTracker) {
|
| 91 | BlockPos blockPos = camera.blockPosition();
|
| 92 | Biome biome = level.getBiome(blockPos).value();
|
| 93 | float deltaTicks = deltaTracker.getGameTimeDeltaTicks();
|
| 94 | float partialTicks = deltaTracker.getGameTimeDeltaPartialTick(false);
|
| 95 | boolean rainsInBiome = biome.hasPrecipitation();
|
| 96 | float skyLightLevelMultiplier = Mth.clamp((level.getLightEngine().getLayerListener(LightLayer.SKY).getLightValue(blockPos) - 8.0F) / 7.0F, 0.0F, 1.0F);
|
| 97 | float targetRainFogMultiplier = level.getRainLevel(partialTicks) * skyLightLevelMultiplier * (rainsInBiome ? 1.0F : 0.5F);
|
| 98 | this.rainFogMultiplier = this.rainFogMultiplier + (targetRainFogMultiplier - this.rainFogMultiplier) * deltaTicks * 0.2F;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public boolean isApplicable(final @Nullable FogType fogType, final Entity entity) {
|
| 103 | return fogType == FogType.ATMOSPHERIC;
|
| 104 | }
|
| 105 | }
|
| 106 |
|