| 1 | package net.minecraft.client.renderer.chunk;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.MeshData;
|
| 4 | import java.util.EnumMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.util.Util;
|
| 12 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class CompiledSectionMesh implements SectionMesh {
|
| 17 | public static final SectionMesh UNCOMPILED = new SectionMesh() {
|
| 18 | @Override
|
| 19 | public boolean facesCanSeeEachother(final Direction direction1, final Direction direction2) {
|
| 20 | return false;
|
| 21 | }
|
| 22 | };
|
| 23 | public static final SectionMesh EMPTY = new SectionMesh() {
|
| 24 | @Override
|
| 25 | public boolean facesCanSeeEachother(final Direction direction1, final Direction direction2) {
|
| 26 | return true;
|
| 27 | }
|
| 28 | };
|
| 29 | private final List<BlockEntity> renderableBlockEntities;
|
| 30 | private final VisibilitySet visibilitySet;
|
| 31 | private final MeshData.@Nullable SortState transparencyState;
|
| 32 | private @Nullable TranslucencyPointOfView translucencyPointOfView;
|
| 33 | private final Map<ChunkSectionLayer, SectionMesh.SectionDraw> draws = new EnumMap<>(ChunkSectionLayer.class);
|
| 34 | private final Map<ChunkSectionLayer, AtomicBoolean> vertexBufferUploaded = Util.makeEnumMap(ChunkSectionLayer.class, layer -> new AtomicBoolean());
|
| 35 | private final Map<ChunkSectionLayer, AtomicBoolean> indexBufferUploaded = Util.makeEnumMap(ChunkSectionLayer.class, layer -> new AtomicBoolean());
|
| 36 | private final long compileTaskStartTimeNs;
|
| 37 |
|
| 38 | public CompiledSectionMesh(final TranslucencyPointOfView translucencyPointOfView, final SectionCompiler.Results results, final long compileTaskStartTimeNs) {
|
| 39 | this.translucencyPointOfView = translucencyPointOfView;
|
| 40 | this.visibilitySet = results.visibilitySet;
|
| 41 | this.renderableBlockEntities = results.blockEntities;
|
| 42 | this.transparencyState = results.transparencyState;
|
| 43 | results.renderedLayers
|
| 44 | .forEach(
|
| 45 | (layer, mesh) -> this.draws
|
| 46 | .put(layer, new SectionMesh.SectionDraw(mesh.drawState().indexCount(), mesh.drawState().indexType(), mesh.indexBuffer() != null))
|
| 47 | );
|
| 48 | this.compileTaskStartTimeNs = compileTaskStartTimeNs;
|
| 49 | }
|
| 50 |
|
| 51 | public void setTranslucencyPointOfView(final TranslucencyPointOfView translucencyPointOfView) {
|
| 52 | this.translucencyPointOfView = translucencyPointOfView;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public boolean isDifferentPointOfView(final TranslucencyPointOfView pointOfView) {
|
| 57 | return !pointOfView.equals(this.translucencyPointOfView);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public boolean hasRenderableLayers() {
|
| 62 | return !this.draws.isEmpty();
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public boolean isEmpty(final ChunkSectionLayer layer) {
|
| 67 | return !this.draws.containsKey(layer);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public List<BlockEntity> getRenderableBlockEntities() {
|
| 72 | return this.renderableBlockEntities;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public boolean facesCanSeeEachother(final Direction direction1, final Direction direction2) {
|
| 77 | return this.visibilitySet.visibilityBetween(direction1, direction2);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public SectionMesh.@Nullable SectionDraw getSectionDraw(final ChunkSectionLayer layer) {
|
| 82 | return this.draws.get(layer);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public long getCompileTaskStartTime() {
|
| 87 | return this.compileTaskStartTimeNs;
|
| 88 | }
|
| 89 |
|
| 90 | public boolean isVertexBufferUploaded(final ChunkSectionLayer layer) {
|
| 91 | return this.vertexBufferUploaded.get(layer).get();
|
| 92 | }
|
| 93 |
|
| 94 | public boolean isIndexBufferUploaded(final ChunkSectionLayer layer) {
|
| 95 | return this.indexBufferUploaded.get(layer).get();
|
| 96 | }
|
| 97 |
|
| 98 | public void setVertexBufferUploaded(final ChunkSectionLayer layer) {
|
| 99 | this.vertexBufferUploaded.get(layer).set(true);
|
| 100 | }
|
| 101 |
|
| 102 | public void setIndexBufferUploaded(final ChunkSectionLayer layer) {
|
| 103 | this.indexBufferUploaded.get(layer).set(true);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public boolean hasTranslucentGeometry() {
|
| 108 | return this.draws.containsKey(ChunkSectionLayer.TRANSLUCENT);
|
| 109 | }
|
| 110 |
|
| 111 | public MeshData.@Nullable SortState getTransparencyState() {
|
| 112 | return this.transparencyState;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void close() {
|
| 117 | this.draws.clear();
|
| 118 | this.vertexBufferUploaded.clear();
|
| 119 | this.indexBufferUploaded.clear();
|
| 120 | }
|
| 121 | }
|
| 122 |
|