| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Camera;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 8 | import net.minecraft.client.player.LocalPlayer;
|
| 9 | import net.minecraft.client.renderer.state.LightmapRenderState;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.profiling.Profiler;
|
| 13 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 14 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 15 | import net.minecraft.world.effect.MobEffects;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import org.joml.Vector3f;
|
| 18 | import org.joml.Vector3fc;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class LightmapRenderStateExtractor {
|
| 22 | public static final Vector3fc WHITE = new Vector3f(1.0F, 1.0F, 1.0F);
|
| 23 | private boolean needsUpdate;
|
| 24 | private final GameRenderer renderer;
|
| 25 | private final Minecraft minecraft;
|
| 26 | private final RandomSource randomSource = RandomSource.create();
|
| 27 | private float blockLightFlicker;
|
| 28 |
|
| 29 | public LightmapRenderStateExtractor(final GameRenderer renderer, final Minecraft minecraft) {
|
| 30 | this.renderer = renderer;
|
| 31 | this.minecraft = minecraft;
|
| 32 | }
|
| 33 |
|
| 34 | public void tick() {
|
| 35 | this.blockLightFlicker = this.blockLightFlicker
|
| 36 | + (this.randomSource.nextFloat() - this.randomSource.nextFloat()) * this.randomSource.nextFloat() * this.randomSource.nextFloat() * 0.1F;
|
| 37 | this.blockLightFlicker *= 0.9F;
|
| 38 | this.needsUpdate = true;
|
| 39 | }
|
| 40 |
|
| 41 | private float calculateDarknessScale(final LivingEntity camera, final float darknessGamma, final float partialTickTime) {
|
| 42 | float darkness = 0.45F * darknessGamma;
|
| 43 | return Math.max(0.0F, Mth.cos((camera.tickCount - partialTickTime) * (float) Math.PI * 0.025F) * darkness);
|
| 44 | }
|
| 45 |
|
| 46 | public void extract(final LightmapRenderState renderState, final float partialTicks) {
|
| 47 | renderState.needsUpdate = this.needsUpdate;
|
| 48 | if (this.needsUpdate) {
|
| 49 | ClientLevel level = this.minecraft.level;
|
| 50 | LocalPlayer player = this.minecraft.player;
|
| 51 | if (level != null && player != null) {
|
| 52 | ProfilerFiller profiler = Profiler.get();
|
| 53 | profiler.push("lightmap");
|
| 54 | Camera camera = this.renderer.mainCamera();
|
| 55 | renderState.blockFactor = this.blockLightFlicker + 1.4F;
|
| 56 | renderState.blockLightTint = camera.attributeProbe().getValue(EnvironmentAttributes.BLOCK_LIGHT_TINT, partialTicks);
|
| 57 | renderState.skyFactor = camera.attributeProbe().getValue(EnvironmentAttributes.SKY_LIGHT_FACTOR, partialTicks);
|
| 58 | renderState.skyLightColor = camera.attributeProbe().getValue(EnvironmentAttributes.SKY_LIGHT_COLOR, partialTicks);
|
| 59 | EndFlashState endFlashState = level.endFlashState();
|
| 60 | if (endFlashState != null && !this.minecraft.options.hideLightningFlash().get()) {
|
| 61 | float intensity = endFlashState.getIntensity(partialTicks);
|
| 62 | if (this.minecraft.gui.hud.getBossOverlay().shouldCreateWorldFog()) {
|
| 63 | renderState.skyFactor += intensity / 3.0F;
|
| 64 | } else {
|
| 65 | renderState.skyFactor += intensity;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | renderState.ambientColor = camera.attributeProbe().getValue(EnvironmentAttributes.AMBIENT_LIGHT_COLOR, partialTicks);
|
| 70 | float brightnessOption = this.minecraft.options.gamma().get().floatValue();
|
| 71 | float darknessEffectScaleOption = this.minecraft.options.darknessEffectScale().get().floatValue();
|
| 72 | float darknessEffectBrightnessModifier = player.getEffectBlendFactor(MobEffects.DARKNESS, partialTicks) * darknessEffectScaleOption;
|
| 73 | renderState.brightness = Math.max(0.0F, brightnessOption - darknessEffectBrightnessModifier);
|
| 74 | renderState.darknessEffectScale = this.calculateDarknessScale(player, darknessEffectBrightnessModifier, partialTicks) * darknessEffectScaleOption;
|
| 75 | float waterVision = player.getWaterVision();
|
| 76 | if (player.hasEffect(MobEffects.NIGHT_VISION)) {
|
| 77 | renderState.nightVisionEffectIntensity = GameRenderer.nightVisionScale(player, partialTicks);
|
| 78 | } else if (waterVision > 0.0F && player.hasEffect(MobEffects.CONDUIT_POWER)) {
|
| 79 | renderState.nightVisionEffectIntensity = waterVision;
|
| 80 | } else {
|
| 81 | renderState.nightVisionEffectIntensity = 0.0F;
|
| 82 | }
|
| 83 |
|
| 84 | renderState.nightVisionColor = camera.attributeProbe().getValue(EnvironmentAttributes.NIGHT_VISION_COLOR, partialTicks);
|
| 85 | renderState.bossOverlayWorldDarkening = this.renderer.bossOverlayWorldDarkening(partialTicks);
|
| 86 | profiler.pop();
|
| 87 | this.needsUpdate = false;
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|