| 1 | package net.minecraft.client.renderer.extract;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import it.unimi.dsi.fastutil.longs.LongCollection;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry;
|
| 6 | import java.util.ArrayDeque;
|
| 7 | import java.util.Deque;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Locale;
|
| 11 | import java.util.SortedSet;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.SharedConstants;
|
| 15 | import net.minecraft.client.Camera;
|
| 16 | import net.minecraft.client.DeltaTracker;
|
| 17 | import net.minecraft.client.Minecraft;
|
| 18 | import net.minecraft.client.Options;
|
| 19 | import net.minecraft.client.SectionUpdateTracker;
|
| 20 | import net.minecraft.client.gui.components.debug.DebugScreenEntries;
|
| 21 | import net.minecraft.client.multiplayer.ClientChunkCache;
|
| 22 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 23 | import net.minecraft.client.multiplayer.ClientPacketListener;
|
| 24 | import net.minecraft.client.player.ItemActivation;
|
| 25 | import net.minecraft.client.player.LocalPlayer;
|
| 26 | import net.minecraft.client.renderer.LevelRenderer;
|
| 27 | import net.minecraft.client.renderer.Lightmap;
|
| 28 | import net.minecraft.client.renderer.SkyRenderer;
|
| 29 | import net.minecraft.client.renderer.ViewArea;
|
| 30 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
|
| 31 | import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState;
|
| 32 | import net.minecraft.client.renderer.chunk.CompiledSectionMesh;
|
| 33 | import net.minecraft.client.renderer.chunk.RenderRegionCache;
|
| 34 | import net.minecraft.client.renderer.chunk.SectionRenderDispatcher;
|
| 35 | import net.minecraft.client.renderer.culling.Frustum;
|
| 36 | import net.minecraft.client.renderer.debug.DebugRenderer;
|
| 37 | import net.minecraft.client.renderer.debug.GameTestBlockHighlightRenderer;
|
| 38 | import net.minecraft.client.renderer.entity.state.AvatarRenderState;
|
| 39 | import net.minecraft.client.renderer.entity.state.EntityRenderState;
|
| 40 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 41 | import net.minecraft.client.renderer.state.level.BlockBreakingRenderState;
|
| 42 | import net.minecraft.client.renderer.state.level.BlockOutlineRenderState;
|
| 43 | import net.minecraft.client.renderer.state.level.LevelRenderState;
|
| 44 | import net.minecraft.client.renderer.state.level.PlayerRenderState;
|
| 45 | import net.minecraft.client.renderer.state.level.SectionUpdateRenderState;
|
| 46 | import net.minecraft.client.renderer.state.level.TransientBlockRenderState;
|
| 47 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 48 | import net.minecraft.client.server.IntegratedServer;
|
| 49 | import net.minecraft.core.BlockPos;
|
| 50 | import net.minecraft.core.SectionPos;
|
| 51 | import net.minecraft.core.BlockPos.MutableBlockPos;
|
| 52 | import net.minecraft.gizmos.Gizmos;
|
| 53 | import net.minecraft.gizmos.SimpleGizmoCollector;
|
| 54 | import net.minecraft.gizmos.Gizmos.TemporaryCollection;
|
| 55 | import net.minecraft.server.level.BlockDestructionProgress;
|
| 56 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 57 | import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
|
| 58 | import net.minecraft.tags.FluidTags;
|
| 59 | import net.minecraft.util.ARGB;
|
| 60 | import net.minecraft.util.Mth;
|
| 61 | import net.minecraft.util.Util;
|
| 62 | import net.minecraft.util.VisibleForDebug;
|
| 63 | import net.minecraft.util.profiling.Profiler;
|
| 64 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 65 | import net.minecraft.world.TickRateManager;
|
| 66 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 67 | import net.minecraft.world.effect.MobEffects;
|
| 68 | import net.minecraft.world.entity.Entity;
|
| 69 | import net.minecraft.world.entity.LivingEntity;
|
| 70 | import net.minecraft.world.item.ItemDisplayContext;
|
| 71 | import net.minecraft.world.level.ChunkPos;
|
| 72 | import net.minecraft.world.level.block.RenderShape;
|
| 73 | import net.minecraft.world.level.block.Block.UpdateFlags;
|
| 74 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 75 | import net.minecraft.world.level.block.state.BlockState;
|
| 76 | import net.minecraft.world.phys.AABB;
|
| 77 | import net.minecraft.world.phys.BlockHitResult;
|
| 78 | import net.minecraft.world.phys.Vec3;
|
| 79 | import net.minecraft.world.phys.HitResult.Type;
|
| 80 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 81 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 82 | import org.joml.Vector4fc;
|
| 83 | import org.jspecify.annotations.Nullable;
|
| 84 |
|
| 85 | @Environment(EnvType.CLIENT)
|
| 86 | public class LevelExtractor implements ResourceManagerReloadListener {
|
| 87 | private static final float CHUNK_VISIBILITY_THRESHOLD = 0.3F;
|
| 88 | private final Minecraft minecraft;
|
| 89 | private final LevelRenderer levelRenderer;
|
| 90 | private @Nullable ClientLevel level;
|
| 91 | private @Nullable SectionUpdateTracker sectionUpdateTracker;
|
| 92 | private final LevelRenderState levelRenderState;
|
| 93 | public final DebugRenderer debugRenderer = new DebugRenderer();
|
| 94 | public final GameTestBlockHighlightRenderer gameTestBlockHighlightRenderer = new GameTestBlockHighlightRenderer();
|
| 95 | private final Deque<TransientBlock> transientBlockQueue = new ArrayDeque<>();
|
| 96 | private final SimpleGizmoCollector mainThreadGizmos = new SimpleGizmoCollector();
|
| 97 | private double prevCamRotX = Double.MIN_VALUE;
|
| 98 | private double prevCamRotY = Double.MIN_VALUE;
|
| 99 | private int lastViewDistance = -1;
|
| 100 | private boolean shouldInvalidateCompiledGeometry;
|
| 101 | private boolean shouldResetLevelRenderData;
|
| 102 | private boolean shouldResetChunkLayerSampler;
|
| 103 | private boolean shouldResetSkyRenderer;
|
| 104 |
|
| 105 | public LevelExtractor(final Minecraft minecraft, final LevelRenderState levelRenderState, final LevelRenderer levelRenderer) {
|
| 106 | this.minecraft = minecraft;
|
| 107 | this.levelRenderer = levelRenderer;
|
| 108 | this.levelRenderState = levelRenderState;
|
| 109 | }
|
| 110 |
|
| 111 | public void extract(final DeltaTracker deltaTracker, final Camera camera, final float worldPartialTicks) {
|
| 112 | this.levelRenderState.worldPartialTicks = worldPartialTicks;
|
| 113 | if (this.minecraft.options.getEffectiveRenderDistance() != this.lastViewDistance) {
|
| 114 | this.allChanged();
|
| 115 | }
|
| 116 |
|
| 117 | Vec3 cameraPos = camera.position();
|
| 118 | if (this.sectionUpdateTracker != null) {
|
| 119 | this.sectionUpdateTracker.repositionCamera(SectionPos.of(cameraPos));
|
| 120 | }
|
| 121 |
|
| 122 | if (this.shouldResetLevelRenderData) {
|
| 123 | this.levelRenderer.resetLevelRenderData();
|
| 124 | this.shouldResetLevelRenderData = false;
|
| 125 | }
|
| 126 |
|
| 127 | this.levelRenderState.reset();
|
| 128 | ProfilerFiller profiler = Profiler.get();
|
| 129 | profiler.push("level");
|
| 130 | this.levelRenderState.shouldResetChunkLayerSampler = this.shouldResetChunkLayerSampler;
|
| 131 | this.shouldResetChunkLayerSampler = false;
|
| 132 | this.levelRenderState.shouldResetSkyRenderer = this.shouldResetSkyRenderer;
|
| 133 | this.shouldResetSkyRenderer = false;
|
| 134 | this.levelRenderState.gameTime = this.level.getGameTime();
|
| 135 | this.extractPlayerState(camera, deltaTracker, worldPartialTicks, this.levelRenderState.playerRenderState);
|
| 136 | Frustum cullFrustum = camera.getCullFrustum();
|
| 137 | profiler.push("prepareDispatchers");
|
| 138 | this.levelRenderer.blockEntityRenderDispatcher().prepare(cameraPos);
|
| 139 | this.levelRenderer.entityRenderDispatcher().prepare(camera, this.minecraft.crosshairPickEntity);
|
| 140 | if (this.shouldInvalidateCompiledGeometry) {
|
| 141 | this.levelRenderer.invalidateCompiledGeometry(this.level, this.minecraft.options, camera, this.minecraft.getBlockColors());
|
| 142 | this.shouldInvalidateCompiledGeometry = false;
|
| 143 | } else if (camera.getCapturedFrustum() == null) {
|
| 144 | double camRotX = Math.floor(camera.xRot() / 2.0F);
|
| 145 | double camRotY = Math.floor(camera.yRot() / 2.0F);
|
| 146 | if (this.levelRenderer.sectionOcclusionGraph().consumeFrustumUpdate() || camRotX != this.prevCamRotX || camRotY != this.prevCamRotY) {
|
| 147 | profiler.popPush("applyFrustum");
|
| 148 | this.applyFrustum(cullFrustum);
|
| 149 | this.prevCamRotX = camRotX;
|
| 150 | this.prevCamRotY = camRotY;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | if (this.sectionUpdateTracker != null && this.level != null) {
|
| 155 | ClientChunkCache chunkCache = this.level.getChunkSource();
|
| 156 | this.levelRenderState.chunkLoadingRenderState.addedEmptySections = chunkCache.addedEmptySections();
|
| 157 | this.levelRenderState.chunkLoadingRenderState.removedEmptySections = chunkCache.removedEmptySections();
|
| 158 | this.levelRenderState.chunkLoadingRenderState.addedLoadedChunks = chunkCache.addedLoadedChunks();
|
| 159 | this.levelRenderState.chunkLoadingRenderState.removedLoadedChunks = chunkCache.removedLoadedChunks();
|
| 160 | chunkCache.flipUpdateTrackingSets();
|
| 161 | LongCollection expectedChunks = this.levelRenderer.expectedChunks();
|
| 162 | expectedChunks.forEach(expectedChunk -> {
|
| 163 | if (chunkCache.hasChunk(ChunkPos.getX(expectedChunk), ChunkPos.getZ(expectedChunk))) {
|
| 164 | this.levelRenderState.chunkLoadingRenderState.loadedExpectedChunks.add(expectedChunk);
|
| 165 | }
|
| 166 | });
|
| 167 | profiler.popPush("sectionUpdates");
|
| 168 | RenderRegionCache cache = new RenderRegionCache();
|
| 169 |
|
| 170 | for (SectionRenderDispatcher.RenderSection section : this.levelRenderer.visibleSections()) {
|
| 171 | SectionUpdateTracker.SectionDirtyState dirtyState = this.sectionUpdateTracker.getDirtyState(section.getSectionNode());
|
| 172 | if (dirtyState != null
|
| 173 | && dirtyState.isDirty()
|
| 174 | && (section.sectionMesh.get() != CompiledSectionMesh.UNCOMPILED || this.sectionUpdateTracker.hasAllNeighbors(this.level, section.getSectionNode()))) {
|
| 175 | this.levelRenderState
|
| 176 | .sectionUpdateRenderStates
|
| 177 | .add(new SectionUpdateRenderState(section.getSectionNode(), dirtyState.isDirtyFromPlayer(), cache.createRegion(this.level, section.getSectionNode())));
|
| 178 | dirtyState.setNotDirty();
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | profiler.popPush("entities");
|
| 184 | this.extractVisibleEntities(camera, cullFrustum, deltaTracker, this.levelRenderState);
|
| 185 | profiler.popPush("blockEntities");
|
| 186 | this.extractVisibleBlockEntities(camera, worldPartialTicks, this.levelRenderState);
|
| 187 | profiler.popPush("blockOutline");
|
| 188 | this.extractBlockOutline(camera, this.levelRenderState);
|
| 189 | profiler.popPush("blockBreaking");
|
| 190 | this.extractBlockDestroyAnimation(camera, this.levelRenderState);
|
| 191 | profiler.popPush("transientBlocks");
|
| 192 | this.drainTransientBlockQueue();
|
| 193 | profiler.popPush("weather");
|
| 194 | this.levelRenderer.weatherEffectRenderer().extractRenderState(this.level, worldPartialTicks, cameraPos, this.levelRenderState.weatherRenderState);
|
| 195 | SkyRenderer skyRenderer = this.levelRenderer.skyRenderer();
|
| 196 | if (skyRenderer != null) {
|
| 197 | profiler.popPush("sky");
|
| 198 | skyRenderer.extractRenderState(this.level, worldPartialTicks, camera, this.levelRenderState.skyRenderState);
|
| 199 | }
|
| 200 |
|
| 201 | profiler.popPush("border");
|
| 202 | this.levelRenderer
|
| 203 | .worldBorderRenderer()
|
| 204 | .extract(
|
| 205 | this.level.getWorldBorder(),
|
| 206 | worldPartialTicks,
|
| 207 | cameraPos,
|
| 208 | this.minecraft.options.getEffectiveRenderDistance() * 16,
|
| 209 | this.levelRenderState.worldBorderRenderState
|
| 210 | );
|
| 211 | profiler.popPush("particles");
|
| 212 | this.minecraft.particleEngine.extract(this.levelRenderState.particlesRenderState, new Frustum(cullFrustum).offset(-3.0F), camera, worldPartialTicks);
|
| 213 | profiler.popPush("cloud");
|
| 214 | Vector4fc cloudColor = camera.attributeProbe().getValue(EnvironmentAttributes.CLOUD_COLOR, worldPartialTicks);
|
| 215 | this.levelRenderState.cloudColor = ARGB.colorFromVector4f(cloudColor);
|
| 216 | if (ARGB.alpha(this.levelRenderState.cloudColor) > 0) {
|
| 217 | this.levelRenderState.cloudHeight = camera.attributeProbe().getValue(EnvironmentAttributes.CLOUD_HEIGHT, worldPartialTicks);
|
| 218 | }
|
| 219 |
|
| 220 | profiler.popPush("debug");
|
| 221 | this.debugRenderer.emitGizmos(cullFrustum, cameraPos.x, cameraPos.y, cameraPos.z, deltaTracker.getGameTimeDeltaPartialTick(false));
|
| 222 | this.gameTestBlockHighlightRenderer.emitGizmos();
|
| 223 | this.levelRenderState.render3dCrosshair = this.minecraft.debugEntries.isCurrentlyEnabled(DebugScreenEntries.THREE_DIMENSIONAL_CROSSHAIR);
|
| 224 | this.levelRenderState.renderWireframeTerrain = this.minecraft.wireframe;
|
| 225 | this.levelRenderState.shouldUseMultiDrawIndirectForTerrain = this.minecraft.multiDrawIndirect;
|
| 226 | ClientPacketListener connection = this.minecraft.getConnection();
|
| 227 | if (connection != null) {
|
| 228 | this.levelRenderState.playerCompiledSectionCallback = connection.getPlayerCompiledSectionCallback();
|
| 229 | }
|
| 230 |
|
| 231 | this.levelRenderState.shouldShowEntityOutlines = shouldShowEntityOutlines(camera, this.levelRenderState.playerRenderState);
|
| 232 | this.extractGizmos();
|
| 233 | profiler.pop();
|
| 234 | profiler.pop();
|
| 235 | }
|
| 236 |
|
| 237 | private void drainTransientBlockQueue() {
|
| 238 | long currentTimeMs = Util.getMillis();
|
| 239 |
|
| 240 | TransientBlock transientBlock;
|
| 241 | while ((transientBlock = this.transientBlockQueue.poll()) != null) {
|
| 242 | TransientBlockRenderState state = new TransientBlockRenderState();
|
| 243 | state.movingBlockRenderState.randomSeedPos = transientBlock.pos();
|
| 244 | state.movingBlockRenderState.blockPos = transientBlock.pos();
|
| 245 | state.movingBlockRenderState.blockState = transientBlock.state();
|
| 246 | state.movingBlockRenderState.biome = this.level.getBiome(transientBlock.pos());
|
| 247 | state.movingBlockRenderState.cardinalLighting = this.level.cardinalLighting();
|
| 248 | state.movingBlockRenderState.lightEngine = this.level.getLightEngine();
|
| 249 | state.createTimeNs = transientBlock.createTimeNs();
|
| 250 | state.liveUntilMs = currentTimeMs + (long)(transientBlock.timeToLive() * 1000.0);
|
| 251 | this.levelRenderer.addTransientBlock(transientBlock.pos().asLong(), state);
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | private void extractVisibleEntities(final Camera camera, final Frustum frustum, final DeltaTracker deltaTracker, final LevelRenderState output) {
|
| 256 | Vec3 cameraPos = camera.position();
|
| 257 | double camX = cameraPos.x();
|
| 258 | double camY = cameraPos.y();
|
| 259 | double camZ = cameraPos.z();
|
| 260 | TickRateManager tickRateManager = this.minecraft.level.tickRateManager();
|
| 261 | Entity.setViewScale(Mth.clamp(this.minecraft.options.getEffectiveRenderDistance() / 8.0, 1.0, 2.5) * this.minecraft.options.entityDistanceScaling().get());
|
| 262 | long chunkFadeDuration = Util.toMillis(this.minecraft.options.chunkSectionFadeInTime().get());
|
| 263 |
|
| 264 | for (Entity entity : this.level.entitiesForRendering()) {
|
| 265 | float entityPartialTicks = deltaTracker.getGameTimeDeltaPartialTick(!tickRateManager.isEntityFrozen(entity));
|
| 266 | if (this.isEntityVisible(entity, frustum, camX, camY, camZ, entityPartialTicks, chunkFadeDuration)
|
| 267 | && (entity != camera.entity() || camera.isDetached() || camera.entity() instanceof LivingEntity && ((LivingEntity)camera.entity()).isSleeping())
|
| 268 | && (!(entity instanceof LocalPlayer) || camera.entity() == entity)) {
|
| 269 | if (entity.tickCount == 0) {
|
| 270 | entity.xOld = entity.getX();
|
| 271 | entity.yOld = entity.getY();
|
| 272 | entity.zOld = entity.getZ();
|
| 273 | }
|
| 274 |
|
| 275 | EntityRenderState state = this.extractEntity(entity, entityPartialTicks);
|
| 276 | output.entityRenderStates.add(state);
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | output.lastEntityRenderStateCount = output.entityRenderStates.size();
|
| 281 | }
|
| 282 |
|
| 283 | public boolean isEntityVisible(
|
| 284 | final Entity entity, final Frustum frustum, final double camX, final double camY, final double camZ, final float partialTicks, final long chunkFadeDuration
|
| 285 | ) {
|
| 286 | if (this.level == null) {
|
| 287 | return false;
|
| 288 | } else if (this.levelRenderer.entityRenderDispatcher().shouldRender(entity, frustum, camX, camY, camZ, partialTicks)
|
| 289 | || this.minecraft.player != null && entity.hasIndirectPassenger(this.minecraft.player)) {
|
| 290 | BlockPos blockPos = entity.blockPosition();
|
| 291 | return this.level.isOutsideBuildHeight(blockPos.getY()) || this.levelRenderer.isSectionCompiledAndVisible(blockPos, chunkFadeDuration);
|
| 292 | } else {
|
| 293 | return false;
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | private EntityRenderState extractEntity(final Entity entity, final float partialTickTime) {
|
| 298 | return this.levelRenderer.entityRenderDispatcher().extractEntity(entity, partialTickTime);
|
| 299 | }
|
| 300 |
|
| 301 | private void extractVisibleBlockEntities(final Camera camera, final float deltaPartialTick, final LevelRenderState levelRenderState) {
|
| 302 | Vec3 cameraPos = camera.position();
|
| 303 | double camX = cameraPos.x();
|
| 304 | double camY = cameraPos.y();
|
| 305 | double camZ = cameraPos.z();
|
| 306 | PoseStack poseStack = new PoseStack();
|
| 307 | long chunkFadeDuration = Util.toMillis(this.minecraft.options.chunkSectionFadeInTime().get());
|
| 308 |
|
| 309 | for (SectionRenderDispatcher.RenderSection section : this.levelRenderer.visibleSections()) {
|
| 310 | List<BlockEntity> renderableBlockEntities = section.getSectionMesh().getRenderableBlockEntities();
|
| 311 | if (!renderableBlockEntities.isEmpty() && !(section.getVisibility(Util.getMillis(), chunkFadeDuration) < 0.3F)) {
|
| 312 | for (BlockEntity blockEntity : renderableBlockEntities) {
|
| 313 | BlockPos blockPos = blockEntity.getBlockPos();
|
| 314 | SortedSet<BlockDestructionProgress> progresses = this.level.destructionProgress().get(blockPos.asLong());
|
| 315 | ModelFeatureRenderer.CrumblingOverlay breakProgress;
|
| 316 | if (progresses != null && !progresses.isEmpty()) {
|
| 317 | poseStack.pushPose();
|
| 318 | poseStack.translate(blockPos.getX() - camX, blockPos.getY() - camY, blockPos.getZ() - camZ);
|
| 319 | breakProgress = new ModelFeatureRenderer.CrumblingOverlay(progresses.last().getProgress(), poseStack.last());
|
| 320 | poseStack.popPose();
|
| 321 | } else {
|
| 322 | breakProgress = null;
|
| 323 | }
|
| 324 |
|
| 325 | BlockEntityRenderState state = this.levelRenderer.blockEntityRenderDispatcher().tryExtractRenderState(blockEntity, deltaPartialTick, breakProgress, false);
|
| 326 | if (state != null) {
|
| 327 | levelRenderState.blockEntityRenderStates.add(state);
|
| 328 | }
|
| 329 | }
|
| 330 | }
|
| 331 | }
|
| 332 |
|
| 333 | Iterator<BlockEntity> iterator = this.level.getGloballyRenderedBlockEntities().iterator();
|
| 334 |
|
| 335 | while (iterator.hasNext()) {
|
| 336 | BlockEntity blockEntity = iterator.next();
|
| 337 | if (blockEntity.isRemoved()) {
|
| 338 | iterator.remove();
|
| 339 | } else {
|
| 340 | BlockEntityRenderState state = this.levelRenderer.blockEntityRenderDispatcher().tryExtractRenderState(blockEntity, deltaPartialTick, null, true);
|
| 341 | if (state != null) {
|
| 342 | levelRenderState.blockEntityRenderStates.add(state);
|
| 343 | }
|
| 344 | }
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | private void extractBlockDestroyAnimation(final Camera camera, final LevelRenderState levelRenderState) {
|
| 349 | Vec3 cameraPos = camera.position();
|
| 350 | double camX = cameraPos.x();
|
| 351 | double camY = cameraPos.y();
|
| 352 | double camZ = cameraPos.z();
|
| 353 | levelRenderState.blockBreakingRenderStates.clear();
|
| 354 |
|
| 355 | for (Entry<SortedSet<BlockDestructionProgress>> entry : this.level.destructionProgress().long2ObjectEntrySet()) {
|
| 356 | BlockPos pos = BlockPos.of(entry.getLongKey());
|
| 357 | if (!(pos.distToCenterSqr(camX, camY, camZ) > 1024.0)) {
|
| 358 | SortedSet<BlockDestructionProgress> progresses = entry.getValue();
|
| 359 | if (progresses != null && !progresses.isEmpty()) {
|
| 360 | int progress = progresses.last().getProgress();
|
| 361 | levelRenderState.blockBreakingRenderStates.add(new BlockBreakingRenderState(pos, this.level.getBlockState(pos), progress));
|
| 362 | }
|
| 363 | }
|
| 364 | }
|
| 365 | }
|
| 366 |
|
| 367 | private void extractBlockOutline(final Camera camera, final LevelRenderState levelRenderState) {
|
| 368 | levelRenderState.blockOutlineRenderState = null;
|
| 369 | if (this.minecraft.hitResult instanceof BlockHitResult blockHitResult) {
|
| 370 | if (blockHitResult.getType() != Type.MISS) {
|
| 371 | BlockPos pos = blockHitResult.getBlockPos();
|
| 372 | BlockState state = this.level.getBlockState(pos);
|
| 373 | if (!state.isAir() && this.level.getWorldBorder().isWithinBounds(pos)) {
|
| 374 | BlockStateModel blockStateModel = this.minecraft.getModelManager().getBlockStateModelSet().get(state);
|
| 375 | boolean isBlockTranslucent = blockStateModel.hasMaterialFlag(1);
|
| 376 | boolean highContrast = this.minecraft.options.highContrastBlockOutline().get();
|
| 377 | CollisionContext context = CollisionContext.of(camera.entity());
|
| 378 | VoxelShape shape = state.getShape(this.level, pos, context);
|
| 379 | if (SharedConstants.DEBUG_SHAPES) {
|
| 380 | VoxelShape collisionShape = state.getCollisionShape(this.level, pos, context);
|
| 381 | VoxelShape occlusionShape = state.getOcclusionShape();
|
| 382 | VoxelShape interactionShape = state.getInteractionShape(this.level, pos);
|
| 383 | levelRenderState.blockOutlineRenderState = new BlockOutlineRenderState(
|
| 384 | pos, isBlockTranslucent, highContrast, shape, collisionShape, occlusionShape, interactionShape
|
| 385 | );
|
| 386 | } else {
|
| 387 | levelRenderState.blockOutlineRenderState = new BlockOutlineRenderState(pos, isBlockTranslucent, highContrast, shape);
|
| 388 | }
|
| 389 | }
|
| 390 | }
|
| 391 | }
|
| 392 | }
|
| 393 |
|
| 394 | private void extractGizmos() {
|
| 395 | this.mainThreadGizmos.addTemporaryGizmos(Minecraft.getInstance().getPerTickGizmos());
|
| 396 | IntegratedServer server = Minecraft.getInstance().getSingleplayerServer();
|
| 397 | if (server != null) {
|
| 398 | this.mainThreadGizmos.addTemporaryGizmos(server.getPerTickGizmos());
|
| 399 | }
|
| 400 |
|
| 401 | this.levelRenderer.addMainThreadGizmos(this.mainThreadGizmos.drainGizmos());
|
| 402 | }
|
| 403 |
|
| 404 | private void extractPlayerState(final Camera camera, final DeltaTracker deltaTracker, final float worldPartialTicks, final PlayerRenderState state) {
|
| 405 | state.reset();
|
| 406 | LocalPlayer player = this.minecraft.player;
|
| 407 | if (player != null && this.level != null) {
|
| 408 | state.hasPlayer = true;
|
| 409 | float playerPartialTick = deltaTracker.getGameTimeDeltaPartialTick(!this.level.tickRateManager().isEntityFrozen(player));
|
| 410 | if (this.extractEntity(player, playerPartialTick) instanceof AvatarRenderState avatarRenderState) {
|
| 411 | state.avatarRenderState = avatarRenderState;
|
| 412 | player.firstPersonHandsAndItems().extractRenderState(player, playerPartialTick, state.firstPersonHandsAndItems);
|
| 413 | state.portalEffectIntensity = Mth.lerp(worldPartialTicks, player.oPortalEffectIntensity, player.portalEffectIntensity);
|
| 414 | state.nauseaEffectIntensity = player.getEffectBlendFactor(MobEffects.NAUSEA, worldPartialTicks);
|
| 415 | state.spinningEffectAngle = player.getSpinningEffectAngle(worldPartialTicks);
|
| 416 | state.isUnderWater = player.isUnderWater();
|
| 417 | state.eyePositionY = player.getEyePosition(worldPartialTicks).y;
|
| 418 | if (player.itemActivation().isActive()) {
|
| 419 | ItemActivation activation = player.itemActivation();
|
| 420 | PlayerRenderState.ItemActivationRenderState activationState = new PlayerRenderState.ItemActivationRenderState(
|
| 421 | activation.item().copy(), activation.ticks(), activation.offX(), activation.offY()
|
| 422 | );
|
| 423 | this.minecraft.getItemModelResolver().updateForTopItem(activationState.itemState, activationState.item, ItemDisplayContext.FIXED, this.level, null, 0);
|
| 424 | state.itemActivation = activationState;
|
| 425 | }
|
| 426 |
|
| 427 | if (!(camera.entity() instanceof LivingEntity livingEntity && livingEntity.isSleeping())) {
|
| 428 | BlockState viewBlockingState = getViewBlockingState(player, camera.getCullFrustum());
|
| 429 | if (viewBlockingState != null) {
|
| 430 | TextureAtlasSprite sprite = this.minecraft.getModelManager().getBlockStateModelSet().getParticleMaterial(viewBlockingState).sprite();
|
| 431 | state.blockOverlay = new PlayerRenderState.BlockOverlay(sprite.atlasLocation(), sprite.getU0(), sprite.getV0(), sprite.getU1(), sprite.getV1());
|
| 432 | }
|
| 433 |
|
| 434 | if (this.minecraft.options.getCameraType().isFirstPerson()) {
|
| 435 | state.isEyeInWater = player.isEyeInFluid(FluidTags.WATER);
|
| 436 | state.isOnFire = player.isOnFire();
|
| 437 | if (state.isEyeInWater) {
|
| 438 | BlockPos eyePos = BlockPos.containing(player.getEyePosition());
|
| 439 | float brightness = Lightmap.getBrightness(player.level().dimensionType(), player.level().getMaxLocalRawBrightness(eyePos));
|
| 440 | state.waterOverlay = new PlayerRenderState.WaterOverlay(
|
| 441 | ARGB.colorFromFloat(0.1F, brightness, brightness, brightness), -player.getYRot() / 64.0F, player.getXRot() / 64.0F
|
| 442 | );
|
| 443 | }
|
| 444 | }
|
| 445 | }
|
| 446 | } else {
|
| 447 | throw new IllegalStateException("Expected an AvatarRenderState for the local player");
|
| 448 | }
|
| 449 | }
|
| 450 | }
|
| 451 |
|
| 452 | private static @Nullable BlockState getViewBlockingState(final LocalPlayer player, final Frustum frustum) {
|
| 453 | if (player.noPhysics) {
|
| 454 | return null;
|
| 455 | }
|
| 456 |
|
| 457 | AABB nearPlaneBB = frustum.getNearPlaneBounds().move(player.getEyePosition());
|
| 458 | MutableBlockPos testPos = new MutableBlockPos();
|
| 459 |
|
| 460 | for (int i = 0; i < 8; i++) {
|
| 461 | testPos.set(
|
| 462 | player.getX() + ((i >> 0) % 2 - 0.5F) * player.getBbWidth() * 0.8F,
|
| 463 | player.getEyeY() + ((i >> 1) % 2 - 0.5F) * 0.1F * player.getScale(),
|
| 464 | player.getZ() + ((i >> 2) % 2 - 0.5F) * player.getBbWidth() * 0.8F
|
| 465 | );
|
| 466 | BlockState blockState = player.level().getBlockState(testPos);
|
| 467 | if (blockState.getRenderShape() != RenderShape.INVISIBLE && blockState.isViewBlocking(player.level(), testPos, nearPlaneBB)) {
|
| 468 | return blockState;
|
| 469 | }
|
| 470 | }
|
| 471 |
|
| 472 | return null;
|
| 473 | }
|
| 474 |
|
| 475 | private void applyFrustum(final Frustum frustum) {
|
| 476 | if (!Minecraft.getInstance().isSameThread()) {
|
| 477 | throw new IllegalStateException("applyFrustum called from wrong thread: " + Thread.currentThread().getName());
|
| 478 | }
|
| 479 |
|
| 480 | this.levelRenderer.clearVisibleSections();
|
| 481 | this.levelRenderer.sectionOcclusionGraph().addSectionsInFrustum(frustum, this.levelRenderer.visibleSections(), this.levelRenderer.nearbyVisibleSections());
|
| 482 | }
|
| 483 |
|
| 484 | private static boolean shouldShowEntityOutlines(final Camera camera, final PlayerRenderState playerRenderState) {
|
| 485 | return !camera.isPanoramicMode() && playerRenderState.hasPlayer;
|
| 486 | }
|
| 487 |
|
| 488 | @Override
|
| 489 | public void onResourceManagerReload(final ResourceManager resourceManager) {
|
| 490 | this.shouldResetSkyRenderer = true;
|
| 491 | }
|
| 492 |
|
| 493 | public void setLevel(final @Nullable ClientLevel level) {
|
| 494 | this.level = level;
|
| 495 | if (level != null) {
|
| 496 | this.allChanged();
|
| 497 | } else {
|
| 498 | this.levelRenderer.entityRenderDispatcher().resetCamera();
|
| 499 | this.sectionUpdateTracker = null;
|
| 500 | }
|
| 501 |
|
| 502 | this.shouldResetLevelRenderData = true;
|
| 503 | this.gameTestBlockHighlightRenderer.clear();
|
| 504 | }
|
| 505 |
|
| 506 | public void allChanged() {
|
| 507 | if (this.level != null) {
|
| 508 | this.level.clearTintCaches();
|
| 509 | Options options = this.minecraft.options;
|
| 510 | this.lastViewDistance = options.getEffectiveRenderDistance();
|
| 511 | this.sectionUpdateTracker = new SectionUpdateTracker(this.level, this.lastViewDistance);
|
| 512 | Camera camera = this.minecraft.gameRenderer.mainCamera();
|
| 513 | SectionPos cameraSectionPos = SectionPos.of(camera.position());
|
| 514 | this.sectionUpdateTracker.repositionCamera(cameraSectionPos);
|
| 515 | this.shouldInvalidateCompiledGeometry = true;
|
| 516 | }
|
| 517 | }
|
| 518 |
|
| 519 | public void resetSampler() {
|
| 520 | this.shouldResetChunkLayerSampler = true;
|
| 521 | }
|
| 522 |
|
| 523 | public void blockChanged(final BlockPos pos, final @UpdateFlags int updateFlags) {
|
| 524 | this.setBlockDirty(pos, (updateFlags & 8) != 0);
|
| 525 | }
|
| 526 |
|
| 527 | private void setBlockDirty(final BlockPos pos, final boolean playerChanged) {
|
| 528 | for (int z = pos.getZ() - 1; z <= pos.getZ() + 1; z++) {
|
| 529 | for (int x = pos.getX() - 1; x <= pos.getX() + 1; x++) {
|
| 530 | for (int y = pos.getY() - 1; y <= pos.getY() + 1; y++) {
|
| 531 | this.setSectionDirty(SectionPos.blockToSectionCoord(x), SectionPos.blockToSectionCoord(y), SectionPos.blockToSectionCoord(z), playerChanged);
|
| 532 | }
|
| 533 | }
|
| 534 | }
|
| 535 | }
|
| 536 |
|
| 537 | public void setBlocksDirty(final int x0, final int y0, final int z0, final int x1, final int y1, final int z1) {
|
| 538 | for (int z = z0 - 1; z <= z1 + 1; z++) {
|
| 539 | for (int x = x0 - 1; x <= x1 + 1; x++) {
|
| 540 | for (int y = y0 - 1; y <= y1 + 1; y++) {
|
| 541 | this.setSectionDirty(SectionPos.blockToSectionCoord(x), SectionPos.blockToSectionCoord(y), SectionPos.blockToSectionCoord(z));
|
| 542 | }
|
| 543 | }
|
| 544 | }
|
| 545 | }
|
| 546 |
|
| 547 | public void setBlockDirty(final BlockPos pos, final BlockState oldState, final BlockState newState) {
|
| 548 | if (this.minecraft.getModelManager().requiresRender(oldState, newState)) {
|
| 549 | this.setBlocksDirty(pos.getX(), pos.getY(), pos.getZ(), pos.getX(), pos.getY(), pos.getZ());
|
| 550 | }
|
| 551 | }
|
| 552 |
|
| 553 | public void setSectionDirtyWithNeighbors(final int sectionX, final int sectionY, final int sectionZ) {
|
| 554 | this.setSectionRangeDirty(sectionX - 1, sectionY - 1, sectionZ - 1, sectionX + 1, sectionY + 1, sectionZ + 1);
|
| 555 | }
|
| 556 |
|
| 557 | public void setSectionRangeDirty(
|
| 558 | final int minSectionX, final int minSectionY, final int minSectionZ, final int maxSectionX, final int maxSectionY, final int maxSectionZ
|
| 559 | ) {
|
| 560 | for (int z = minSectionZ; z <= maxSectionZ; z++) {
|
| 561 | for (int x = minSectionX; x <= maxSectionX; x++) {
|
| 562 | for (int y = minSectionY; y <= maxSectionY; y++) {
|
| 563 | this.setSectionDirty(x, y, z);
|
| 564 | }
|
| 565 | }
|
| 566 | }
|
| 567 | }
|
| 568 |
|
| 569 | public void setSectionDirty(final int sectionX, final int sectionY, final int sectionZ) {
|
| 570 | this.setSectionDirty(sectionX, sectionY, sectionZ, false);
|
| 571 | }
|
| 572 |
|
| 573 | private void setSectionDirty(final int sectionX, final int sectionY, final int sectionZ, final boolean playerChanged) {
|
| 574 | this.sectionUpdateTracker.setDirty(sectionX, sectionY, sectionZ, playerChanged);
|
| 575 | }
|
| 576 |
|
| 577 | public void queueTransientBlock(final TransientBlock transientBlock) {
|
| 578 | this.transientBlockQueue.add(transientBlock);
|
| 579 | }
|
| 580 |
|
| 581 | public TemporaryCollection collectPerFrameMainThreadGizmos() {
|
| 582 | return Gizmos.withCollector(this.mainThreadGizmos);
|
| 583 | }
|
| 584 |
|
| 585 | public int countRenderedSections() {
|
| 586 | int rendered = 0;
|
| 587 |
|
| 588 | for (SectionRenderDispatcher.RenderSection section : this.levelRenderer.visibleSections()) {
|
| 589 | if (section.getSectionMesh().hasRenderableLayers()) {
|
| 590 | rendered++;
|
| 591 | }
|
| 592 | }
|
| 593 |
|
| 594 | return rendered;
|
| 595 | }
|
| 596 |
|
| 597 | @VisibleForDebug
|
| 598 | public @Nullable String sectionStatistics() {
|
| 599 | ViewArea viewArea = this.levelRenderer.viewArea();
|
| 600 | if (viewArea == null) {
|
| 601 | return null;
|
| 602 | }
|
| 603 |
|
| 604 | int totalSections = viewArea.size();
|
| 605 | int rendered = this.countRenderedSections();
|
| 606 | SectionRenderDispatcher sectionRenderDispatcher = this.levelRenderer.sectionRenderDispatcher();
|
| 607 | return String.format(
|
| 608 | Locale.ROOT,
|
| 609 | "C: %d/%d %sD: %d, %s",
|
| 610 | rendered,
|
| 611 | totalSections,
|
| 612 | this.minecraft.smartCull ? "(s) " : "",
|
| 613 | this.lastViewDistance,
|
| 614 | sectionRenderDispatcher == null ? "null" : sectionRenderDispatcher.getStats()
|
| 615 | );
|
| 616 | }
|
| 617 |
|
| 618 | @VisibleForDebug
|
| 619 | public @Nullable String entityStatistics() {
|
| 620 | return this.level == null
|
| 621 | ? null
|
| 622 | : "E: " + this.levelRenderState.lastEntityRenderStateCount + "/" + this.level.getEntityCount() + ", SD: " + this.level.getServerSimulationDistance();
|
| 623 | }
|
| 624 |
|
| 625 | @VisibleForDebug
|
| 626 | public double totalSections() {
|
| 627 | return this.sectionUpdateTracker == null ? 0.0 : this.sectionUpdateTracker.size();
|
| 628 | }
|
| 629 |
|
| 630 | @VisibleForDebug
|
| 631 | public double lastViewDistance() {
|
| 632 | return this.lastViewDistance;
|
| 633 | }
|
| 634 | }
|
| 635 |
|