| 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.platform.NativeImage;
|
| 6 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 9 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 10 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 11 | import com.mojang.renderpearl.api.pipeline.PrimitiveTopology;
|
| 12 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 13 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 14 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 15 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 16 | import java.io.IOException;
|
| 17 | import java.io.InputStream;
|
| 18 | import java.nio.ByteBuffer;
|
| 19 | import java.util.Optional;
|
| 20 | import net.fabricmc.api.EnvType;
|
| 21 | import net.fabricmc.api.Environment;
|
| 22 | import net.minecraft.client.CloudStatus;
|
| 23 | import net.minecraft.client.renderer.oit.OitRenderPassProvider;
|
| 24 | import net.minecraft.client.renderer.oit.OitStage;
|
| 25 | import net.minecraft.core.Direction;
|
| 26 | import net.minecraft.resources.Identifier;
|
| 27 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 28 | import net.minecraft.server.packs.resources.SimplePreparableReloadListener;
|
| 29 | import net.minecraft.util.ARGB;
|
| 30 | import net.minecraft.util.Mth;
|
| 31 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 32 | import net.minecraft.world.phys.Vec3;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 | import org.slf4j.Logger;
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | public class CloudRenderer extends SimplePreparableReloadListener<Optional<CloudRenderer.TextureData>> implements AutoCloseable {
|
| 38 | private static final int FLAG_INSIDE_FACE = 16;
|
| 39 | private static final int FLAG_USE_TOP_COLOR = 32;
|
| 40 | private static final float CELL_SIZE_IN_BLOCKS = 12.0F;
|
| 41 | private static final int TICKS_PER_CELL = 400;
|
| 42 | private static final float BLOCKS_PER_SECOND = 0.6F;
|
| 43 | private static final int UBO_SIZE = new Std140SizeCalculator().putVec4().putVec3().putVec3().get();
|
| 44 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 45 | private static final Identifier TEXTURE_LOCATION = Identifier.withDefaultNamespace("textures/environment/clouds.png");
|
| 46 | private static final long EMPTY_CELL = 0L;
|
| 47 | private static final int COLOR_OFFSET = 4;
|
| 48 | private static final int NORTH_OFFSET = 3;
|
| 49 | private static final int EAST_OFFSET = 2;
|
| 50 | private static final int SOUTH_OFFSET = 1;
|
| 51 | private static final int WEST_OFFSET = 0;
|
| 52 | private boolean needsRebuild = true;
|
| 53 | private int prevCellX = Integer.MIN_VALUE;
|
| 54 | private int prevCellZ = Integer.MIN_VALUE;
|
| 55 | private CloudRenderer.RelativeCameraPos prevRelativeCameraPos = CloudRenderer.RelativeCameraPos.INSIDE_CLOUDS;
|
| 56 | private @Nullable CloudStatus prevCloudStatus;
|
| 57 | private CloudRenderer.@Nullable TextureData texture;
|
| 58 | private int quadCount = 0;
|
| 59 | private final MappableRingBuffer ubo = new MappableRingBuffer(() -> "Cloud UBO", 130, UBO_SIZE);
|
| 60 | private @Nullable MappableRingBuffer utb;
|
| 61 |
|
| 62 | protected Optional<CloudRenderer.TextureData> prepare(final ResourceManager manager, final ProfilerFiller profiler) {
|
| 63 | try (
|
| 64 | InputStream input = manager.open(TEXTURE_LOCATION);
|
| 65 | NativeImage texture = NativeImage.read(input);
|
| 66 | ) {
|
| 67 | int width = texture.getWidth();
|
| 68 | int height = texture.getHeight();
|
| 69 | long[] cells = new long[width * height];
|
| 70 |
|
| 71 | for (int y = 0; y < height; y++) {
|
| 72 | for (int x = 0; x < width; x++) {
|
| 73 | int color = texture.getPixel(x, y);
|
| 74 | if (isCellEmpty(color)) {
|
| 75 | cells[x + y * width] = 0L;
|
| 76 | } else {
|
| 77 | boolean north = isCellEmpty(texture.getPixel(x, Math.floorMod(y - 1, height)));
|
| 78 | boolean east = isCellEmpty(texture.getPixel(Math.floorMod(x + 1, height), y));
|
| 79 | boolean south = isCellEmpty(texture.getPixel(x, Math.floorMod(y + 1, height)));
|
| 80 | boolean west = isCellEmpty(texture.getPixel(Math.floorMod(x - 1, height), y));
|
| 81 | cells[x + y * width] = packCellData(color, north, east, south, west);
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | return Optional.of(new CloudRenderer.TextureData(cells, width, height));
|
| 87 | } catch (IOException e) {
|
| 88 | LOGGER.error("Failed to load cloud texture", e);
|
| 89 | return Optional.empty();
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | private static int getSizeForCloudDistance(final int radiusCells) {
|
| 94 | int maxFacesPerCell = 4;
|
| 95 | int maxCells = (radiusCells + 1) * 2 * (radiusCells + 1) * 2 / 2;
|
| 96 | int maxFaces = maxCells * 4 + 54;
|
| 97 | return maxFaces * 3;
|
| 98 | }
|
| 99 |
|
| 100 | protected void apply(final Optional<CloudRenderer.TextureData> preparations, final ResourceManager manager, final ProfilerFiller profiler) {
|
| 101 | this.texture = preparations.orElse(null);
|
| 102 | this.needsRebuild = true;
|
| 103 | }
|
| 104 |
|
| 105 | private static boolean isCellEmpty(final int color) {
|
| 106 | return ARGB.alpha(color) < 10;
|
| 107 | }
|
| 108 |
|
| 109 | private static long packCellData(final int color, final boolean north, final boolean east, final boolean south, final boolean west) {
|
| 110 | return (long)color << 4 | (north ? 1 : 0) << 3 | (east ? 1 : 0) << 2 | (south ? 1 : 0) << 1 | (west ? 1 : 0) << 0;
|
| 111 | }
|
| 112 |
|
| 113 | private static boolean isNorthEmpty(final long cellData) {
|
| 114 | return (cellData >> 3 & 1L) != 0L;
|
| 115 | }
|
| 116 |
|
| 117 | private static boolean isEastEmpty(final long cellData) {
|
| 118 | return (cellData >> 2 & 1L) != 0L;
|
| 119 | }
|
| 120 |
|
| 121 | private static boolean isSouthEmpty(final long cellData) {
|
| 122 | return (cellData >> 1 & 1L) != 0L;
|
| 123 | }
|
| 124 |
|
| 125 | private static boolean isWestEmpty(final long cellData) {
|
| 126 | return (cellData >> 0 & 1L) != 0L;
|
| 127 | }
|
| 128 |
|
| 129 | public void prepare(
|
| 130 | final int color,
|
| 131 | final CloudStatus cloudStatus,
|
| 132 | final float bottomY,
|
| 133 | final int range,
|
| 134 | final Vec3 cameraPosition,
|
| 135 | final long gameTime,
|
| 136 | final float partialTicks
|
| 137 | ) {
|
| 138 | if (this.texture != null) {
|
| 139 | int radiusBlocks = range * 16;
|
| 140 | int radiusCells = Mth.ceil(radiusBlocks / 12.0F);
|
| 141 | int utbSize = getSizeForCloudDistance(radiusCells);
|
| 142 | if (this.utb == null || this.utb.currentBuffer().size() != utbSize) {
|
| 143 | if (this.utb != null) {
|
| 144 | this.utb.close();
|
| 145 | }
|
| 146 |
|
| 147 | this.utb = new MappableRingBuffer(() -> "Cloud UTB", 258, utbSize);
|
| 148 | }
|
| 149 |
|
| 150 | float relativeBottomY = (float)(bottomY - cameraPosition.y);
|
| 151 | float relativeTopY = relativeBottomY + 4.0F;
|
| 152 | CloudRenderer.RelativeCameraPos relativeCameraPos;
|
| 153 | if (relativeTopY < 0.0F) {
|
| 154 | relativeCameraPos = CloudRenderer.RelativeCameraPos.ABOVE_CLOUDS;
|
| 155 | } else if (relativeBottomY > 0.0F) {
|
| 156 | relativeCameraPos = CloudRenderer.RelativeCameraPos.BELOW_CLOUDS;
|
| 157 | } else {
|
| 158 | relativeCameraPos = CloudRenderer.RelativeCameraPos.INSIDE_CLOUDS;
|
| 159 | }
|
| 160 |
|
| 161 | float cloudOffset = (float)(gameTime % (this.texture.width * 400L)) + partialTicks;
|
| 162 | double cloudX = cameraPosition.x + cloudOffset * 0.030000001F;
|
| 163 | double cloudZ = cameraPosition.z + 3.96F;
|
| 164 | double textureWidthBlocks = this.texture.width * 12.0;
|
| 165 | double textureHeightBlocks = this.texture.height * 12.0;
|
| 166 | cloudX -= Mth.floor(cloudX / textureWidthBlocks) * textureWidthBlocks;
|
| 167 | cloudZ -= Mth.floor(cloudZ / textureHeightBlocks) * textureHeightBlocks;
|
| 168 | int cellX = Mth.floor(cloudX / 12.0);
|
| 169 | int cellZ = Mth.floor(cloudZ / 12.0);
|
| 170 | float xInCell = (float)(cloudX - cellX * 12.0F);
|
| 171 | float zInCell = (float)(cloudZ - cellZ * 12.0F);
|
| 172 | boolean fancyClouds = cloudStatus == CloudStatus.FANCY;
|
| 173 | if (this.needsRebuild
|
| 174 | || cellX != this.prevCellX
|
| 175 | || cellZ != this.prevCellZ
|
| 176 | || relativeCameraPos != this.prevRelativeCameraPos
|
| 177 | || cloudStatus != this.prevCloudStatus) {
|
| 178 | this.needsRebuild = false;
|
| 179 | this.prevCellX = cellX;
|
| 180 | this.prevCellZ = cellZ;
|
| 181 | this.prevRelativeCameraPos = relativeCameraPos;
|
| 182 | this.prevCloudStatus = cloudStatus;
|
| 183 | this.utb.rotate();
|
| 184 |
|
| 185 | try (GpuBufferSlice.MappedView view = this.utb.currentBuffer().map(false, true)) {
|
| 186 | this.buildMesh(relativeCameraPos, view.data(), cellX, cellZ, fancyClouds, radiusCells);
|
| 187 | this.quadCount = view.data().position() / 3;
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | if (this.quadCount != 0) {
|
| 192 | RenderSystem.AutoStorageIndexBuffer indices = RenderSystem.getSequentialBuffer(PrimitiveTopology.QUADS);
|
| 193 | indices.requestIndexCount(6 * this.quadCount);
|
| 194 |
|
| 195 | try (GpuBufferSlice.MappedView view = this.ubo.currentBuffer().map(false, true)) {
|
| 196 | Std140Builder.intoBuffer(view.data()).putVec4(ARGB.vector4fFromARGB32(color)).putVec3(-xInCell, relativeBottomY, -zInCell).putVec3(12.0F, 4.0F, 12.0F);
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | public void render(final CloudStatus cloudStatus, final RenderPass renderPass) {
|
| 203 | if (this.texture != null && this.quadCount != 0) {
|
| 204 | RenderPipeline renderPipeline = cloudStatus == CloudStatus.FANCY ? RenderPipelines.CLOUDS : RenderPipelines.FLAT_CLOUDS;
|
| 205 | renderPass.pushDebugGroup(() -> "Clouds");
|
| 206 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 207 | this.render(renderPass, renderPipeline);
|
| 208 | renderPass.popDebugGroup();
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | public void renderOit(
|
| 213 | final CloudStatus cloudStatus, final OitStage stage, final GpuTextureView mainDepthTextureView, final OitRenderPassProvider.Parameters params
|
| 214 | ) {
|
| 215 | if (this.texture != null && this.quadCount != 0) {
|
| 216 | GpuSampler nearestSampler = RenderSystem.getSamplerCache().getClampToEdge(FilterMode.NEAREST);
|
| 217 | RenderPipeline renderPipeline = (cloudStatus == CloudStatus.FANCY ? RenderPipelines.OIT_CLOUDS : RenderPipelines.OIT_FLAT_CLOUDS).getPipeline(stage);
|
| 218 |
|
| 219 | try (RenderPass renderPass = OitRenderPassProvider.createRenderPass(stage, () -> "Clouds", params)) {
|
| 220 | if (stage == OitStage.DEPTH_BOUNDS) {
|
| 221 | renderPass.setPipeline(RenderSystem.getCompiledPipeline(RenderPipelines.BLIT_DEPTH_DURING_DEPTH_BOUNDS));
|
| 222 | renderPass.setUniform("InSampler", mainDepthTextureView, nearestSampler);
|
| 223 | renderPass.draw(3, 1, 0, 0);
|
| 224 | }
|
| 225 |
|
| 226 | this.render(renderPass, renderPipeline);
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | private void render(final RenderPass renderPass, final RenderPipeline renderPipeline) {
|
| 232 | GpuBufferSlice dynamicTransforms = RenderSystem.getDynamicUniforms().writeTransform(RenderSystem.getModelViewMatrixCopy());
|
| 233 | RenderSystem.AutoStorageIndexBuffer indices = RenderSystem.getSequentialBuffer(PrimitiveTopology.QUADS);
|
| 234 | GpuBuffer indexBuffer = indices.getBuffer();
|
| 235 | renderPass.setPipeline(RenderSystem.getCompiledPipeline(renderPipeline));
|
| 236 | renderPass.setUniform("DynamicTransforms", dynamicTransforms);
|
| 237 | renderPass.setIndexBuffer(indexBuffer, indices.type());
|
| 238 | renderPass.setVertexBuffer(0, null);
|
| 239 | renderPass.setUniform("CloudInfo", this.ubo.currentBuffer());
|
| 240 | renderPass.setUniform("CloudFaces", this.utb.currentBuffer());
|
| 241 | renderPass.drawIndexed(6 * this.quadCount, 1, 0, 0, 0);
|
| 242 | }
|
| 243 |
|
| 244 | private void buildMesh(
|
| 245 | final CloudRenderer.RelativeCameraPos relativePos,
|
| 246 | final ByteBuffer faceBuffer,
|
| 247 | final int centerCellX,
|
| 248 | final int centerCellZ,
|
| 249 | final boolean extrude,
|
| 250 | final int radiusCells
|
| 251 | ) {
|
| 252 | if (this.texture != null) {
|
| 253 | long[] cells = this.texture.cells;
|
| 254 | int textureWidth = this.texture.width;
|
| 255 | int textureHeight = this.texture.height;
|
| 256 |
|
| 257 | for (int ring = 0; ring <= 2 * radiusCells; ring++) {
|
| 258 | for (int relativeCellX = -ring; relativeCellX <= ring; relativeCellX++) {
|
| 259 | int relativeCellZ = ring - Math.abs(relativeCellX);
|
| 260 | if (relativeCellZ >= 0 && relativeCellZ <= radiusCells && relativeCellX * relativeCellX + relativeCellZ * relativeCellZ <= radiusCells * radiusCells) {
|
| 261 | if (relativeCellZ != 0) {
|
| 262 | this.tryBuildCell(relativePos, faceBuffer, centerCellX, centerCellZ, extrude, relativeCellX, textureWidth, -relativeCellZ, textureHeight, cells);
|
| 263 | }
|
| 264 |
|
| 265 | this.tryBuildCell(relativePos, faceBuffer, centerCellX, centerCellZ, extrude, relativeCellX, textureWidth, relativeCellZ, textureHeight, cells);
|
| 266 | }
|
| 267 | }
|
| 268 | }
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | private void tryBuildCell(
|
| 273 | final CloudRenderer.RelativeCameraPos relativePos,
|
| 274 | final ByteBuffer faceBuffer,
|
| 275 | final int cellX,
|
| 276 | final int cellZ,
|
| 277 | final boolean extrude,
|
| 278 | final int relativeCellX,
|
| 279 | final int textureWidth,
|
| 280 | final int relativeCellZ,
|
| 281 | final int textureHeight,
|
| 282 | final long[] cells
|
| 283 | ) {
|
| 284 | int indexX = Math.floorMod(cellX + relativeCellX, textureWidth);
|
| 285 | int indexY = Math.floorMod(cellZ + relativeCellZ, textureHeight);
|
| 286 | long cellData = cells[indexX + indexY * textureWidth];
|
| 287 | if (cellData != 0L) {
|
| 288 | if (extrude) {
|
| 289 | this.buildExtrudedCell(relativePos, faceBuffer, relativeCellX, relativeCellZ, cellData);
|
| 290 | } else {
|
| 291 | this.buildFlatCell(faceBuffer, relativeCellX, relativeCellZ);
|
| 292 | }
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | private void buildFlatCell(final ByteBuffer faceBuffer, final int x, final int z) {
|
| 297 | this.encodeFace(faceBuffer, x, z, Direction.DOWN, 32);
|
| 298 | }
|
| 299 |
|
| 300 | private void encodeFace(final ByteBuffer faceBuffer, final int x, final int z, final Direction direction, final int flags) {
|
| 301 | int dirAndFlags = direction.get3DDataValue() | flags;
|
| 302 | dirAndFlags |= (x & 1) << 7;
|
| 303 | dirAndFlags |= (z & 1) << 6;
|
| 304 | faceBuffer.put((byte)(x >> 1)).put((byte)(z >> 1)).put((byte)dirAndFlags);
|
| 305 | }
|
| 306 |
|
| 307 | private void buildExtrudedCell(final CloudRenderer.RelativeCameraPos relativePos, final ByteBuffer faceBuffer, final int x, final int z, final long cellData) {
|
| 308 | if (relativePos != CloudRenderer.RelativeCameraPos.BELOW_CLOUDS) {
|
| 309 | this.encodeFace(faceBuffer, x, z, Direction.UP, 0);
|
| 310 | }
|
| 311 |
|
| 312 | if (relativePos != CloudRenderer.RelativeCameraPos.ABOVE_CLOUDS) {
|
| 313 | this.encodeFace(faceBuffer, x, z, Direction.DOWN, 0);
|
| 314 | }
|
| 315 |
|
| 316 | if (isNorthEmpty(cellData) && z > 0) {
|
| 317 | this.encodeFace(faceBuffer, x, z, Direction.NORTH, 0);
|
| 318 | }
|
| 319 |
|
| 320 | if (isSouthEmpty(cellData) && z < 0) {
|
| 321 | this.encodeFace(faceBuffer, x, z, Direction.SOUTH, 0);
|
| 322 | }
|
| 323 |
|
| 324 | if (isWestEmpty(cellData) && x > 0) {
|
| 325 | this.encodeFace(faceBuffer, x, z, Direction.WEST, 0);
|
| 326 | }
|
| 327 |
|
| 328 | if (isEastEmpty(cellData) && x < 0) {
|
| 329 | this.encodeFace(faceBuffer, x, z, Direction.EAST, 0);
|
| 330 | }
|
| 331 |
|
| 332 | boolean addInteriorFaces = Math.abs(x) <= 1 && Math.abs(z) <= 1;
|
| 333 | if (addInteriorFaces) {
|
| 334 | for (Direction direction : Direction.values()) {
|
| 335 | this.encodeFace(faceBuffer, x, z, direction, 16);
|
| 336 | }
|
| 337 | }
|
| 338 | }
|
| 339 |
|
| 340 | public void markForRebuild() {
|
| 341 | this.needsRebuild = true;
|
| 342 | }
|
| 343 |
|
| 344 | public void endFrame() {
|
| 345 | this.ubo.rotate();
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | public void close() {
|
| 350 | this.ubo.close();
|
| 351 | if (this.utb != null) {
|
| 352 | this.utb.close();
|
| 353 | }
|
| 354 | }
|
| 355 |
|
| 356 | @Environment(EnvType.CLIENT)
|
| 357 | private enum RelativeCameraPos {
|
| 358 | ABOVE_CLOUDS,
|
| 359 | INSIDE_CLOUDS,
|
| 360 | BELOW_CLOUDS;
|
| 361 | }
|
| 362 |
|
| 363 | @Environment(EnvType.CLIENT)
|
| 364 | public record TextureData(long[] cells, int width, int height) {
|
| 365 | }
|
| 366 | }
|
| 367 |
|