| 1 | package net.minecraft.client.renderer.fog;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.blaze3d.buffers.Std140Builder;
|
| 5 | import com.mojang.blaze3d.buffers.Std140SizeCalculator;
|
| 6 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 7 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 8 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 9 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 10 | import java.nio.ByteBuffer;
|
| 11 | import java.util.List;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.Camera;
|
| 15 | import net.minecraft.client.DeltaTracker;
|
| 16 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 17 | import net.minecraft.client.player.LocalPlayer;
|
| 18 | import net.minecraft.client.renderer.GameRenderer;
|
| 19 | import net.minecraft.client.renderer.MappableRingBuffer;
|
| 20 | import net.minecraft.client.renderer.fog.environment.AtmosphericFogEnvironment;
|
| 21 | import net.minecraft.client.renderer.fog.environment.BlindnessFogEnvironment;
|
| 22 | import net.minecraft.client.renderer.fog.environment.DarknessFogEnvironment;
|
| 23 | import net.minecraft.client.renderer.fog.environment.FogEnvironment;
|
| 24 | import net.minecraft.client.renderer.fog.environment.LavaFogEnvironment;
|
| 25 | import net.minecraft.client.renderer.fog.environment.PowderedSnowFogEnvironment;
|
| 26 | import net.minecraft.client.renderer.fog.environment.WaterFogEnvironment;
|
| 27 | import net.minecraft.util.Mth;
|
| 28 | import net.minecraft.world.effect.MobEffects;
|
| 29 | import net.minecraft.world.entity.Entity;
|
| 30 | import net.minecraft.world.entity.LivingEntity;
|
| 31 | import net.minecraft.world.level.material.FogType;
|
| 32 | import org.joml.Vector3fc;
|
| 33 | import org.joml.Vector4f;
|
| 34 | import org.lwjgl.system.MemoryStack;
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | public class FogRenderer implements AutoCloseable {
|
| 38 | public static final int FOG_UBO_SIZE = new Std140SizeCalculator().putVec4().putFloat().putFloat().putFloat().putFloat().putFloat().putFloat().get();
|
| 39 | private static final List<FogEnvironment> FOG_ENVIRONMENTS = Lists.newArrayList(
|
| 40 | new LavaFogEnvironment(),
|
| 41 | new PowderedSnowFogEnvironment(),
|
| 42 | new BlindnessFogEnvironment(),
|
| 43 | new DarknessFogEnvironment(),
|
| 44 | new WaterFogEnvironment(),
|
| 45 | new AtmosphericFogEnvironment()
|
| 46 | );
|
| 47 | private static boolean fogEnabled = true;
|
| 48 | private final GpuBuffer emptyBuffer;
|
| 49 | private final MappableRingBuffer regularBuffer;
|
| 50 |
|
| 51 | public FogRenderer() {
|
| 52 | GpuDevice device = RenderSystem.getDevice();
|
| 53 | this.regularBuffer = new MappableRingBuffer(() -> "Fog UBO", 130, FOG_UBO_SIZE);
|
| 54 |
|
| 55 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 56 | ByteBuffer buffer = stack.malloc(FOG_UBO_SIZE);
|
| 57 | this.updateBuffer(buffer, 0, new Vector4f(0.0F), Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
|
| 58 | this.emptyBuffer = device.createBuffer(() -> "Empty fog", 128, buffer.flip());
|
| 59 | }
|
| 60 |
|
| 61 | RenderSystem.setShaderFog(this.getBuffer(FogRenderer.FogMode.NONE));
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public void close() {
|
| 66 | this.emptyBuffer.close();
|
| 67 | this.regularBuffer.close();
|
| 68 | }
|
| 69 |
|
| 70 | public void endFrame() {
|
| 71 | this.regularBuffer.rotate();
|
| 72 | }
|
| 73 |
|
| 74 | public GpuBufferSlice getBuffer(final FogRenderer.FogMode mode) {
|
| 75 | if (!fogEnabled) {
|
| 76 | return this.emptyBuffer.slice(0L, FOG_UBO_SIZE);
|
| 77 | }
|
| 78 |
|
| 79 | return switch (mode) {
|
| 80 | case NONE -> this.emptyBuffer.slice(0L, FOG_UBO_SIZE);
|
| 81 | case WORLD -> this.regularBuffer.currentBuffer().slice(0L, FOG_UBO_SIZE);
|
| 82 | };
|
| 83 | }
|
| 84 |
|
| 85 | private void computeFogColor(
|
| 86 | final Camera camera, final float partialTicks, final ClientLevel level, final int renderDistance, final float darkenWorldAmount, final Vector4f dest
|
| 87 | ) {
|
| 88 | FogType fogType = this.getFogType(camera);
|
| 89 | Entity entity = camera.entity();
|
| 90 | FogEnvironment colorSourceEnvironment = null;
|
| 91 | FogEnvironment darknessModifyingEnvironment = null;
|
| 92 |
|
| 93 | for (FogEnvironment fogEnvironment : FOG_ENVIRONMENTS) {
|
| 94 | if (fogEnvironment.isApplicable(fogType, entity)) {
|
| 95 | if (colorSourceEnvironment == null && fogEnvironment.providesColor()) {
|
| 96 | colorSourceEnvironment = fogEnvironment;
|
| 97 | }
|
| 98 |
|
| 99 | if (darknessModifyingEnvironment == null && fogEnvironment.modifiesDarkness()) {
|
| 100 | darknessModifyingEnvironment = fogEnvironment;
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | if (colorSourceEnvironment == null) {
|
| 106 | throw new IllegalStateException("No color source environment found");
|
| 107 | }
|
| 108 |
|
| 109 | Vector3fc color = colorSourceEnvironment.getBaseColor(level, camera, renderDistance, partialTicks);
|
| 110 | float voidDarknessOnsetRange = level.getLevelData().voidDarknessOnsetRange();
|
| 111 | float darkness = Mth.clamp((voidDarknessOnsetRange + level.getMinY() - (float)camera.position().y) / voidDarknessOnsetRange, 0.0F, 1.0F);
|
| 112 | if (darknessModifyingEnvironment != null) {
|
| 113 | LivingEntity livingEntity = (LivingEntity)entity;
|
| 114 | darkness = darknessModifyingEnvironment.getModifiedDarkness(livingEntity, darkness, partialTicks);
|
| 115 | }
|
| 116 |
|
| 117 | float fogRed = color.x();
|
| 118 | float fogGreen = color.y();
|
| 119 | float fogBlue = color.z();
|
| 120 | if (darkness > 0.0F && fogType != FogType.LAVA && fogType != FogType.POWDER_SNOW) {
|
| 121 | float brightness = Mth.square(1.0F - darkness);
|
| 122 | fogRed *= brightness;
|
| 123 | fogGreen *= brightness;
|
| 124 | fogBlue *= brightness;
|
| 125 | }
|
| 126 |
|
| 127 | if (darkenWorldAmount > 0.0F) {
|
| 128 | fogRed = Mth.lerp(darkenWorldAmount, fogRed, fogRed * 0.7F);
|
| 129 | fogGreen = Mth.lerp(darkenWorldAmount, fogGreen, fogGreen * 0.6F);
|
| 130 | fogBlue = Mth.lerp(darkenWorldAmount, fogBlue, fogBlue * 0.6F);
|
| 131 | }
|
| 132 |
|
| 133 | float brightenFactor;
|
| 134 | if (fogType == FogType.WATER) {
|
| 135 | if (entity instanceof LocalPlayer localPlayer) {
|
| 136 | brightenFactor = localPlayer.getWaterVision();
|
| 137 | } else {
|
| 138 | brightenFactor = 1.0F;
|
| 139 | }
|
| 140 | } else if (entity instanceof LivingEntity livingEntity && livingEntity.hasEffect(MobEffects.NIGHT_VISION) && !livingEntity.hasEffect(MobEffects.DARKNESS)) {
|
| 141 | brightenFactor = GameRenderer.nightVisionScale(livingEntity, partialTicks);
|
| 142 | } else {
|
| 143 | brightenFactor = 0.0F;
|
| 144 | }
|
| 145 |
|
| 146 | if (fogRed != 0.0F && fogGreen != 0.0F && fogBlue != 0.0F) {
|
| 147 | float targetScale = 1.0F / Math.max(fogRed, Math.max(fogGreen, fogBlue));
|
| 148 | fogRed = Mth.lerp(brightenFactor, fogRed, fogRed * targetScale);
|
| 149 | fogGreen = Mth.lerp(brightenFactor, fogGreen, fogGreen * targetScale);
|
| 150 | fogBlue = Mth.lerp(brightenFactor, fogBlue, fogBlue * targetScale);
|
| 151 | }
|
| 152 |
|
| 153 | dest.set(fogRed, fogGreen, fogBlue, 1.0F);
|
| 154 | }
|
| 155 |
|
| 156 | public static boolean toggleFog() {
|
| 157 | return fogEnabled = !fogEnabled;
|
| 158 | }
|
| 159 |
|
| 160 | public FogData setupFog(
|
| 161 | final Camera camera, final int renderDistanceInChunks, final DeltaTracker deltaTracker, final float darkenWorldAmount, final ClientLevel level
|
| 162 | ) {
|
| 163 | float partialTickTime = deltaTracker.getGameTimeDeltaPartialTick(false);
|
| 164 | float renderDistanceInBlocks = renderDistanceInChunks * 16;
|
| 165 | FogType fogType = this.getFogType(camera);
|
| 166 | Entity entity = camera.entity();
|
| 167 | FogData fog = new FogData();
|
| 168 | this.computeFogColor(camera, partialTickTime, level, renderDistanceInChunks, darkenWorldAmount, fog.color);
|
| 169 |
|
| 170 | for (FogEnvironment fogEnvironment : FOG_ENVIRONMENTS) {
|
| 171 | if (fogEnvironment.isApplicable(fogType, entity)) {
|
| 172 | fogEnvironment.setupFog(fog, camera, level, renderDistanceInBlocks, deltaTracker);
|
| 173 | break;
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | float renderDistanceFogSpan = Mth.clamp(renderDistanceInBlocks / 10.0F, 4.0F, 64.0F);
|
| 178 | fog.renderDistanceStart = renderDistanceInBlocks - renderDistanceFogSpan;
|
| 179 | fog.renderDistanceEnd = renderDistanceInBlocks;
|
| 180 | return fog;
|
| 181 | }
|
| 182 |
|
| 183 | public void updateBuffer(final FogData fog) {
|
| 184 | try (GpuBufferSlice.MappedView view = this.regularBuffer.currentBuffer().map(false, true)) {
|
| 185 | this.updateBuffer(
|
| 186 | view.data(), 0, fog.color, fog.environmentalStart, fog.environmentalEnd, fog.renderDistanceStart, fog.renderDistanceEnd, fog.skyEnd, fog.cloudEnd
|
| 187 | );
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | private FogType getFogType(final Camera camera) {
|
| 192 | FogType blockFogType = camera.getFluidInCamera();
|
| 193 | return blockFogType == FogType.NONE ? FogType.ATMOSPHERIC : blockFogType;
|
| 194 | }
|
| 195 |
|
| 196 | private void updateBuffer(
|
| 197 | final ByteBuffer byteBuffer,
|
| 198 | final int offset,
|
| 199 | final Vector4f fogColor,
|
| 200 | final float environmentalStart,
|
| 201 | final float environmentalEnd,
|
| 202 | final float renderDistanceStart,
|
| 203 | final float renderDistanceEnd,
|
| 204 | final float skyEnd,
|
| 205 | final float endClouds
|
| 206 | ) {
|
| 207 | byteBuffer.position(offset);
|
| 208 | Std140Builder.intoBuffer(byteBuffer)
|
| 209 | .putVec4(fogColor)
|
| 210 | .putFloat(environmentalStart)
|
| 211 | .putFloat(environmentalEnd)
|
| 212 | .putFloat(renderDistanceStart)
|
| 213 | .putFloat(renderDistanceEnd)
|
| 214 | .putFloat(skyEnd)
|
| 215 | .putFloat(endClouds);
|
| 216 | }
|
| 217 |
|
| 218 | @Environment(EnvType.CLIENT)
|
| 219 | public enum FogMode {
|
| 220 | NONE,
|
| 221 | WORLD;
|
| 222 | }
|
| 223 | }
|
| 224 |
|