| 1 | package net.minecraft.client.renderer.block;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.renderer.chunk.ChunkSectionLayer;
|
| 7 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 8 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.Direction.Axis;
|
| 12 | import net.minecraft.core.Direction.Plane;
|
| 13 | import net.minecraft.util.ARGB;
|
| 14 | import net.minecraft.util.LightCoordsUtil;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 | import net.minecraft.world.level.CardinalLighting;
|
| 17 | import net.minecraft.world.level.block.Block;
|
| 18 | import net.minecraft.world.level.block.HalfTransparentBlock;
|
| 19 | import net.minecraft.world.level.block.LeavesBlock;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.material.Fluid;
|
| 22 | import net.minecraft.world.level.material.FluidState;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 | import net.minecraft.world.phys.shapes.Shapes;
|
| 25 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 26 |
|
| 27 | /**
|
| 28 | * Access widened by fabric-rendering-fluids-v1 to accessible
|
| 29 | */
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class FluidRenderer {
|
| 32 | private static final float MAX_FLUID_HEIGHT = 0.8888889F;
|
| 33 | /**
|
| 34 | * Access widened by fabric-rendering-fluids-v1 to accessible
|
| 35 | */
|
| 36 | public final FluidStateModelSet fluidModels;
|
| 37 |
|
| 38 | public FluidRenderer(final FluidStateModelSet fluidModels) {
|
| 39 | this.fluidModels = fluidModels;
|
| 40 | }
|
| 41 |
|
| 42 | private static boolean isNeighborSameFluid(final FluidState fluidState, final FluidState neighborFluidState) {
|
| 43 | return neighborFluidState.getType().isSame(fluidState.getType());
|
| 44 | }
|
| 45 |
|
| 46 | private static boolean isFaceOccludedByState(final Direction direction, final float height, final BlockState state) {
|
| 47 | VoxelShape occluder = state.getFaceOcclusionShape(direction.getOpposite());
|
| 48 | if (occluder == Shapes.empty()) {
|
| 49 | return false;
|
| 50 | } else if (occluder == Shapes.block()) {
|
| 51 | boolean fullBlock = height == 1.0F;
|
| 52 | return direction != Direction.UP || fullBlock;
|
| 53 | } else {
|
| 54 | VoxelShape shape = Shapes.box(0.0, 0.0, 0.0, 1.0, height, 1.0);
|
| 55 | return Shapes.blockOccludes(shape, occluder, direction);
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | private static boolean isFaceOccludedByNeighbor(final Direction direction, final float height, final BlockState neighborState) {
|
| 60 | return isFaceOccludedByState(direction, height, neighborState);
|
| 61 | }
|
| 62 |
|
| 63 | private static boolean isFaceOccludedBySelf(final BlockState state, final Direction direction) {
|
| 64 | return isFaceOccludedByState(direction.getOpposite(), 1.0F, state);
|
| 65 | }
|
| 66 |
|
| 67 | public static boolean shouldRenderFace(
|
| 68 | final FluidState fluidState, final BlockState blockState, final Direction direction, final FluidState neighborFluidState
|
| 69 | ) {
|
| 70 | return !isNeighborSameFluid(fluidState, neighborFluidState) && !isFaceOccludedBySelf(blockState, direction);
|
| 71 | }
|
| 72 |
|
| 73 | public void tesselate(
|
| 74 | final BlockAndTintGetter level, final BlockPos pos, final FluidRenderer.Output output, final BlockState blockState, final FluidState fluidState
|
| 75 | ) {
|
| 76 | BlockState blockStateDown = level.getBlockState(pos.relative(Direction.DOWN));
|
| 77 | FluidState fluidStateDown = blockStateDown.getFluidState();
|
| 78 | BlockState blockStateUp = level.getBlockState(pos.relative(Direction.UP));
|
| 79 | FluidState fluidStateUp = blockStateUp.getFluidState();
|
| 80 | BlockState blockStateNorth = level.getBlockState(pos.relative(Direction.NORTH));
|
| 81 | FluidState fluidStateNorth = blockStateNorth.getFluidState();
|
| 82 | BlockState blockStateSouth = level.getBlockState(pos.relative(Direction.SOUTH));
|
| 83 | FluidState fluidStateSouth = blockStateSouth.getFluidState();
|
| 84 | BlockState blockStateWest = level.getBlockState(pos.relative(Direction.WEST));
|
| 85 | FluidState fluidStateWest = blockStateWest.getFluidState();
|
| 86 | BlockState blockStateEast = level.getBlockState(pos.relative(Direction.EAST));
|
| 87 | FluidState fluidStateEast = blockStateEast.getFluidState();
|
| 88 | boolean renderUp = !isNeighborSameFluid(fluidState, fluidStateUp);
|
| 89 | boolean renderDown = shouldRenderFace(fluidState, blockState, Direction.DOWN, fluidStateDown)
|
| 90 | && !isFaceOccludedByNeighbor(Direction.DOWN, 0.8888889F, blockStateDown);
|
| 91 | boolean renderNorth = shouldRenderFace(fluidState, blockState, Direction.NORTH, fluidStateNorth);
|
| 92 | boolean renderSouth = shouldRenderFace(fluidState, blockState, Direction.SOUTH, fluidStateSouth);
|
| 93 | boolean renderWest = shouldRenderFace(fluidState, blockState, Direction.WEST, fluidStateWest);
|
| 94 | boolean renderEast = shouldRenderFace(fluidState, blockState, Direction.EAST, fluidStateEast);
|
| 95 | if (renderUp || renderDown || renderEast || renderWest || renderNorth || renderSouth) {
|
| 96 | FluidModel model = this.fluidModels.get(fluidState);
|
| 97 | VertexConsumer builder = output.getBuilder(model.layer());
|
| 98 | int tintColor = model.tintSource() != null ? model.tintSource().colorInWorld(blockState, level, pos) : -1;
|
| 99 | CardinalLighting cardinalLighting = level.cardinalLighting();
|
| 100 | Fluid type = fluidState.getType();
|
| 101 | float heightSelf = this.getHeight(level, type, pos, blockState, fluidState);
|
| 102 | float heightNorthEast;
|
| 103 | float heightNorthWest;
|
| 104 | float heightSouthEast;
|
| 105 | float heightSouthWest;
|
| 106 | if (heightSelf >= 1.0F) {
|
| 107 | heightNorthEast = 1.0F;
|
| 108 | heightNorthWest = 1.0F;
|
| 109 | heightSouthEast = 1.0F;
|
| 110 | heightSouthWest = 1.0F;
|
| 111 | } else {
|
| 112 | float heightNorth = this.getHeight(level, type, pos.north(), blockStateNorth, fluidStateNorth);
|
| 113 | float heightSouth = this.getHeight(level, type, pos.south(), blockStateSouth, fluidStateSouth);
|
| 114 | float heightEast = this.getHeight(level, type, pos.east(), blockStateEast, fluidStateEast);
|
| 115 | float heightWest = this.getHeight(level, type, pos.west(), blockStateWest, fluidStateWest);
|
| 116 | heightNorthEast = this.calculateAverageHeight(level, type, heightSelf, heightNorth, heightEast, pos.relative(Direction.NORTH).relative(Direction.EAST));
|
| 117 | heightNorthWest = this.calculateAverageHeight(level, type, heightSelf, heightNorth, heightWest, pos.relative(Direction.NORTH).relative(Direction.WEST));
|
| 118 | heightSouthEast = this.calculateAverageHeight(level, type, heightSelf, heightSouth, heightEast, pos.relative(Direction.SOUTH).relative(Direction.EAST));
|
| 119 | heightSouthWest = this.calculateAverageHeight(level, type, heightSelf, heightSouth, heightWest, pos.relative(Direction.SOUTH).relative(Direction.WEST));
|
| 120 | }
|
| 121 |
|
| 122 | float x = pos.getX() & 15;
|
| 123 | float y = pos.getY() & 15;
|
| 124 | float z = pos.getZ() & 15;
|
| 125 | float offs = 0.001F;
|
| 126 | float bottomOffs = renderDown ? 0.001F : 0.0F;
|
| 127 | if (renderUp
|
| 128 | && !isFaceOccludedByNeighbor(Direction.UP, Math.min(Math.min(heightNorthWest, heightSouthWest), Math.min(heightSouthEast, heightNorthEast)), blockStateUp)) {
|
| 129 | heightNorthWest -= 0.001F;
|
| 130 | heightSouthWest -= 0.001F;
|
| 131 | heightSouthEast -= 0.001F;
|
| 132 | heightNorthEast -= 0.001F;
|
| 133 | Vec3 flow = fluidState.getFlow(level, pos);
|
| 134 | float u00;
|
| 135 | float u01;
|
| 136 | float u10;
|
| 137 | float u11;
|
| 138 | float v00;
|
| 139 | float v01;
|
| 140 | float v10;
|
| 141 | float v11;
|
| 142 | if (flow.x == 0.0 && flow.z == 0.0) {
|
| 143 | TextureAtlasSprite stillSprite = model.stillMaterial().sprite();
|
| 144 | u00 = stillSprite.getU0();
|
| 145 | v00 = stillSprite.getV0();
|
| 146 | u01 = u00;
|
| 147 | v01 = stillSprite.getV1();
|
| 148 | u10 = stillSprite.getU1();
|
| 149 | v10 = v01;
|
| 150 | u11 = u10;
|
| 151 | v11 = v00;
|
| 152 | } else {
|
| 153 | float angle = (float)Mth.atan2(flow.z, flow.x) - (float) (Math.PI / 2);
|
| 154 | float s = Mth.sin(angle) * 0.25F;
|
| 155 | float c = Mth.cos(angle) * 0.25F;
|
| 156 | float cc = 0.5F;
|
| 157 | TextureAtlasSprite flowingSprite = model.flowingMaterial().sprite();
|
| 158 | u00 = flowingSprite.getU(0.5F + (-c - s));
|
| 159 | v00 = flowingSprite.getV(0.5F + (-c + s));
|
| 160 | u01 = flowingSprite.getU(0.5F + (-c + s));
|
| 161 | v01 = flowingSprite.getV(0.5F + (c + s));
|
| 162 | u10 = flowingSprite.getU(0.5F + (c + s));
|
| 163 | v10 = flowingSprite.getV(0.5F + (c - s));
|
| 164 | u11 = flowingSprite.getU(0.5F + (c - s));
|
| 165 | v11 = flowingSprite.getV(0.5F + (-c - s));
|
| 166 | }
|
| 167 |
|
| 168 | int topLightCoords = this.getLightCoords(level, pos);
|
| 169 | int topColor = ARGB.scaleRGB(tintColor, cardinalLighting.up());
|
| 170 | this.addFace(
|
| 171 | builder,
|
| 172 | x + 0.0F,
|
| 173 | y + heightNorthWest,
|
| 174 | z + 0.0F,
|
| 175 | u00,
|
| 176 | v00,
|
| 177 | x + 0.0F,
|
| 178 | y + heightSouthWest,
|
| 179 | z + 1.0F,
|
| 180 | u01,
|
| 181 | v01,
|
| 182 | x + 1.0F,
|
| 183 | y + heightSouthEast,
|
| 184 | z + 1.0F,
|
| 185 | u10,
|
| 186 | v10,
|
| 187 | x + 1.0F,
|
| 188 | y + heightNorthEast,
|
| 189 | z + 0.0F,
|
| 190 | u11,
|
| 191 | v11,
|
| 192 | topColor,
|
| 193 | topLightCoords,
|
| 194 | fluidState.shouldRenderBackwardUpFace(level, pos.above())
|
| 195 | );
|
| 196 | }
|
| 197 |
|
| 198 | if (renderDown) {
|
| 199 | TextureAtlasSprite stillSprite = model.stillMaterial().sprite();
|
| 200 | float u0 = stillSprite.getU0();
|
| 201 | float u1 = stillSprite.getU1();
|
| 202 | float v0 = stillSprite.getV0();
|
| 203 | float v1 = stillSprite.getV1();
|
| 204 | int belowLightCoords = this.getLightCoords(level, pos.below());
|
| 205 | int belowColor = ARGB.scaleRGB(tintColor, cardinalLighting.down());
|
| 206 | this.addFace(
|
| 207 | builder,
|
| 208 | x,
|
| 209 | y + bottomOffs,
|
| 210 | z,
|
| 211 | u0,
|
| 212 | v0,
|
| 213 | x + 1.0F,
|
| 214 | y + bottomOffs,
|
| 215 | z,
|
| 216 | u1,
|
| 217 | v0,
|
| 218 | x + 1.0F,
|
| 219 | y + bottomOffs,
|
| 220 | z + 1.0F,
|
| 221 | u1,
|
| 222 | v1,
|
| 223 | x,
|
| 224 | y + bottomOffs,
|
| 225 | z + 1.0F,
|
| 226 | u0,
|
| 227 | v1,
|
| 228 | belowColor,
|
| 229 | belowLightCoords,
|
| 230 | false
|
| 231 | );
|
| 232 | }
|
| 233 |
|
| 234 | int sideLightCoords = this.getLightCoords(level, pos);
|
| 235 |
|
| 236 | for (Direction faceDir : Plane.HORIZONTAL) {
|
| 237 | float hh0;
|
| 238 | float hh1;
|
| 239 | float x0;
|
| 240 | float z0;
|
| 241 | float x1;
|
| 242 | float z1;
|
| 243 | boolean renderCondition;
|
| 244 | BlockState faceState;
|
| 245 | switch (faceDir) {
|
| 246 | case NORTH:
|
| 247 | hh0 = heightNorthWest;
|
| 248 | hh1 = heightNorthEast;
|
| 249 | x0 = x;
|
| 250 | x1 = x + 1.0F;
|
| 251 | z0 = z + 0.001F;
|
| 252 | z1 = z + 0.001F;
|
| 253 | renderCondition = renderNorth;
|
| 254 | faceState = blockStateNorth;
|
| 255 | break;
|
| 256 | case SOUTH:
|
| 257 | hh0 = heightSouthEast;
|
| 258 | hh1 = heightSouthWest;
|
| 259 | x0 = x + 1.0F;
|
| 260 | x1 = x;
|
| 261 | z0 = z + 1.0F - 0.001F;
|
| 262 | z1 = z + 1.0F - 0.001F;
|
| 263 | renderCondition = renderSouth;
|
| 264 | faceState = blockStateSouth;
|
| 265 | break;
|
| 266 | case WEST:
|
| 267 | hh0 = heightSouthWest;
|
| 268 | hh1 = heightNorthWest;
|
| 269 | x0 = x + 0.001F;
|
| 270 | x1 = x + 0.001F;
|
| 271 | z0 = z + 1.0F;
|
| 272 | z1 = z;
|
| 273 | renderCondition = renderWest;
|
| 274 | faceState = blockStateWest;
|
| 275 | break;
|
| 276 | case EAST:
|
| 277 | hh0 = heightNorthEast;
|
| 278 | hh1 = heightSouthEast;
|
| 279 | x0 = x + 1.0F - 0.001F;
|
| 280 | x1 = x + 1.0F - 0.001F;
|
| 281 | z0 = z;
|
| 282 | z1 = z + 1.0F;
|
| 283 | renderCondition = renderEast;
|
| 284 | faceState = blockStateEast;
|
| 285 | break;
|
| 286 | default:
|
| 287 | throw new UnsupportedOperationException();
|
| 288 | }
|
| 289 |
|
| 290 | if (renderCondition && !isFaceOccludedByNeighbor(faceDir, Math.max(hh0, hh1), faceState)) {
|
| 291 | TextureAtlasSprite sprite = model.flowingMaterial().sprite();
|
| 292 | boolean isOverlay = false;
|
| 293 | if (model.overlayMaterial() != null) {
|
| 294 | Block relativeBlock = faceState.getBlock();
|
| 295 | if (relativeBlock instanceof HalfTransparentBlock || relativeBlock instanceof LeavesBlock) {
|
| 296 | sprite = model.overlayMaterial().sprite();
|
| 297 | isOverlay = true;
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | float u0 = sprite.getU(0.0F);
|
| 302 | float u1 = sprite.getU(0.5F);
|
| 303 | float v01 = sprite.getV((1.0F - hh0) * 0.5F);
|
| 304 | float v02 = sprite.getV((1.0F - hh1) * 0.5F);
|
| 305 | float v1 = sprite.getV(0.5F);
|
| 306 | float shadeSide = faceDir.getAxis() == Axis.Z ? cardinalLighting.north() : cardinalLighting.west();
|
| 307 | int faceColor = ARGB.scaleRGB(tintColor, cardinalLighting.up() * shadeSide);
|
| 308 | this.addFace(
|
| 309 | builder,
|
| 310 | x0,
|
| 311 | y + hh0,
|
| 312 | z0,
|
| 313 | u0,
|
| 314 | v01,
|
| 315 | x1,
|
| 316 | y + hh1,
|
| 317 | z1,
|
| 318 | u1,
|
| 319 | v02,
|
| 320 | x1,
|
| 321 | y + bottomOffs,
|
| 322 | z1,
|
| 323 | u1,
|
| 324 | v1,
|
| 325 | x0,
|
| 326 | y + bottomOffs,
|
| 327 | z0,
|
| 328 | u0,
|
| 329 | v1,
|
| 330 | faceColor,
|
| 331 | sideLightCoords,
|
| 332 | !isOverlay
|
| 333 | );
|
| 334 | }
|
| 335 | }
|
| 336 | }
|
| 337 | }
|
| 338 |
|
| 339 | private void addFace(
|
| 340 | final VertexConsumer builder,
|
| 341 | final float x0,
|
| 342 | final float y0,
|
| 343 | final float z0,
|
| 344 | final float u0,
|
| 345 | final float v0,
|
| 346 | final float x1,
|
| 347 | final float y1,
|
| 348 | final float z1,
|
| 349 | final float u1,
|
| 350 | final float v1,
|
| 351 | final float x2,
|
| 352 | final float y2,
|
| 353 | final float z2,
|
| 354 | final float u2,
|
| 355 | final float v2,
|
| 356 | final float x3,
|
| 357 | final float y3,
|
| 358 | final float z3,
|
| 359 | final float u3,
|
| 360 | final float v3,
|
| 361 | final int color,
|
| 362 | final int lightCoords,
|
| 363 | final boolean addBackFace
|
| 364 | ) {
|
| 365 | this.vertex(builder, x0, y0, z0, color, u0, v0, lightCoords);
|
| 366 | this.vertex(builder, x1, y1, z1, color, u1, v1, lightCoords);
|
| 367 | this.vertex(builder, x2, y2, z2, color, u2, v2, lightCoords);
|
| 368 | this.vertex(builder, x3, y3, z3, color, u3, v3, lightCoords);
|
| 369 | if (addBackFace) {
|
| 370 | this.vertex(builder, x0, y0, z0, color, u0, v0, lightCoords);
|
| 371 | this.vertex(builder, x3, y3, z3, color, u3, v3, lightCoords);
|
| 372 | this.vertex(builder, x2, y2, z2, color, u2, v2, lightCoords);
|
| 373 | this.vertex(builder, x1, y1, z1, color, u1, v1, lightCoords);
|
| 374 | }
|
| 375 | }
|
| 376 |
|
| 377 | private float calculateAverageHeight(
|
| 378 | final BlockAndTintGetter level, final Fluid type, final float heightSelf, final float height2, final float height1, final BlockPos cornerPos
|
| 379 | ) {
|
| 380 | if (!(height1 >= 1.0F) && !(height2 >= 1.0F)) {
|
| 381 | float[] weightedHeight = new float[2];
|
| 382 | if (height1 > 0.0F || height2 > 0.0F) {
|
| 383 | float heightCorner = this.getHeight(level, type, cornerPos);
|
| 384 | if (heightCorner >= 1.0F) {
|
| 385 | return 1.0F;
|
| 386 | }
|
| 387 |
|
| 388 | this.addWeightedHeight(weightedHeight, heightCorner);
|
| 389 | }
|
| 390 |
|
| 391 | this.addWeightedHeight(weightedHeight, heightSelf);
|
| 392 | this.addWeightedHeight(weightedHeight, height1);
|
| 393 | this.addWeightedHeight(weightedHeight, height2);
|
| 394 | return weightedHeight[0] / weightedHeight[1];
|
| 395 | } else {
|
| 396 | return 1.0F;
|
| 397 | }
|
| 398 | }
|
| 399 |
|
| 400 | private void addWeightedHeight(final float[] weightedHeight, final float height) {
|
| 401 | if (height >= 0.8F) {
|
| 402 | weightedHeight[0] += height * 10.0F;
|
| 403 | weightedHeight[1] += 10.0F;
|
| 404 | } else if (height >= 0.0F) {
|
| 405 | weightedHeight[0] += height;
|
| 406 | weightedHeight[1]++;
|
| 407 | }
|
| 408 | }
|
| 409 |
|
| 410 | private float getHeight(final BlockAndTintGetter level, final Fluid fluidType, final BlockPos pos) {
|
| 411 | BlockState state = level.getBlockState(pos);
|
| 412 | return this.getHeight(level, fluidType, pos, state, state.getFluidState());
|
| 413 | }
|
| 414 |
|
| 415 | private float getHeight(final BlockAndTintGetter level, final Fluid fluidType, final BlockPos pos, final BlockState state, final FluidState fluidState) {
|
| 416 | if (fluidType.isSame(fluidState.getType())) {
|
| 417 | BlockState aboveState = level.getBlockState(pos.above());
|
| 418 | return fluidType.isSame(aboveState.getFluidState().getType()) ? 1.0F : fluidState.getOwnHeight();
|
| 419 | } else {
|
| 420 | return !state.isSolid() ? 0.0F : -1.0F;
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | private void vertex(
|
| 425 | final VertexConsumer builder, final float x, final float y, final float z, final int color, final float u, final float v, final int lightCoords
|
| 426 | ) {
|
| 427 | builder.addVertex(x, y, z, color, u, v, OverlayTexture.NO_OVERLAY, lightCoords, 0.0F, 1.0F, 0.0F);
|
| 428 | }
|
| 429 |
|
| 430 | private int getLightCoords(final BlockAndTintGetter level, final BlockPos pos) {
|
| 431 | return LightCoordsUtil.max(LightCoordsUtil.getLightCoords(level, pos), LightCoordsUtil.getLightCoords(level, pos.above()));
|
| 432 | }
|
| 433 |
|
| 434 | @FunctionalInterface
|
| 435 | @Environment(EnvType.CLIENT)
|
| 436 | public interface Output {
|
| 437 | VertexConsumer getBuilder(ChunkSectionLayer layer);
|
| 438 | }
|
| 439 | }
|
| 440 |
|