| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.buffers.Std140Builder;
|
| 4 | import com.mojang.blaze3d.buffers.Std140SizeCalculator;
|
| 5 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 6 | import com.mojang.renderpearl.api.GpuFormat;
|
| 7 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 8 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 9 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 10 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 11 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 12 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 13 | import java.util.Optional;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.renderer.state.LightmapRenderState;
|
| 17 | import net.minecraft.util.ARGB;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.util.profiling.Profiler;
|
| 20 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 21 | import net.minecraft.world.level.dimension.DimensionType;
|
| 22 | import org.joml.Vector4fc;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class Lightmap implements AutoCloseable {
|
| 26 | public static final int TEXTURE_SIZE = 16;
|
| 27 | private static final Vector4fc CLEAR_COLOR = ARGB.vector4fFromARGB32(-1);
|
| 28 | private static final int LIGHTMAP_UBO_SIZE = new Std140SizeCalculator()
|
| 29 | .putFloat()
|
| 30 | .putFloat()
|
| 31 | .putFloat()
|
| 32 | .putFloat()
|
| 33 | .putFloat()
|
| 34 | .putFloat()
|
| 35 | .putVec3()
|
| 36 | .putVec3()
|
| 37 | .putVec3()
|
| 38 | .putVec3()
|
| 39 | .get();
|
| 40 | private final GpuTexture texture;
|
| 41 | private final GpuTextureView textureView;
|
| 42 | private final MappableRingBuffer ubo;
|
| 43 |
|
| 44 | public Lightmap() {
|
| 45 | GpuDevice device = RenderSystem.getDevice();
|
| 46 | this.texture = device.createTexture("Lightmap", 13, GpuFormat.RGBA8_UNORM, 16, 16, 1, 1);
|
| 47 | this.textureView = device.createTextureView(this.texture);
|
| 48 | device.createCommandEncoder().clearColorTexture(this.texture, CLEAR_COLOR);
|
| 49 | this.ubo = new MappableRingBuffer(() -> "Lightmap UBO", 130, LIGHTMAP_UBO_SIZE);
|
| 50 | }
|
| 51 |
|
| 52 | public GpuTextureView getTextureView() {
|
| 53 | return this.textureView;
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public void close() {
|
| 58 | this.texture.close();
|
| 59 | this.textureView.close();
|
| 60 | this.ubo.close();
|
| 61 | }
|
| 62 |
|
| 63 | public void render(final LightmapRenderState renderState) {
|
| 64 | if (renderState.needsUpdate) {
|
| 65 | ProfilerFiller profiler = Profiler.get();
|
| 66 | profiler.push("lightmap");
|
| 67 | CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder();
|
| 68 |
|
| 69 | try (GpuBufferSlice.MappedView view = this.ubo.currentBuffer().map(false, true)) {
|
| 70 | Std140Builder.intoBuffer(view.data())
|
| 71 | .putFloat(renderState.skyFactor)
|
| 72 | .putFloat(renderState.blockFactor)
|
| 73 | .putFloat(renderState.nightVisionEffectIntensity)
|
| 74 | .putFloat(renderState.darknessEffectScale)
|
| 75 | .putFloat(renderState.bossOverlayWorldDarkening)
|
| 76 | .putFloat(renderState.brightness)
|
| 77 | .putVec3(renderState.blockLightTint)
|
| 78 | .putVec3(renderState.skyLightColor)
|
| 79 | .putVec3(renderState.ambientColor)
|
| 80 | .putVec3(renderState.nightVisionColor);
|
| 81 | }
|
| 82 |
|
| 83 | try (RenderPass renderPass = commandEncoder.createRenderPass(() -> "Update light", this.textureView, Optional.empty())) {
|
| 84 | renderPass.setPipeline(RenderSystem.getCompiledPipeline(RenderPipelines.LIGHTMAP));
|
| 85 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 86 | renderPass.setUniform("LightmapInfo", this.ubo.currentBuffer());
|
| 87 | renderPass.draw(3, 1, 0, 0);
|
| 88 | }
|
| 89 |
|
| 90 | this.ubo.rotate();
|
| 91 | profiler.pop();
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | public static float getBrightness(final DimensionType dimensionType, final int level) {
|
| 96 | float v = level / 15.0F;
|
| 97 | float curvedV = v / (4.0F - 3.0F * v);
|
| 98 | return Mth.lerp(dimensionType.ambientLight(), curvedV, 1.0F);
|
| 99 | }
|
| 100 | }
|
| 101 |
|