| 1 | package net.minecraft.client.resources.model.geometry;
|
| 2 |
|
| 3 | import com.google.common.collect.ArrayListMultimap;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.Multimap;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class QuadCollection {
|
| 15 | public static final QuadCollection EMPTY = new QuadCollection(List.of(), List.of(), List.of(), List.of(), List.of(), List.of(), List.of(), List.of());
|
| 16 | private static final int FLAGS_NOT_COMPUTED = -1;
|
| 17 | private final List<BakedQuad> all;
|
| 18 | private final List<BakedQuad> unculled;
|
| 19 | private final List<BakedQuad> north;
|
| 20 | private final List<BakedQuad> south;
|
| 21 | private final List<BakedQuad> east;
|
| 22 | private final List<BakedQuad> west;
|
| 23 | private final List<BakedQuad> up;
|
| 24 | private final List<BakedQuad> down;
|
| 25 | private int materialFlags = -1;
|
| 26 |
|
| 27 | private QuadCollection(
|
| 28 | final List<BakedQuad> all,
|
| 29 | final List<BakedQuad> unculled,
|
| 30 | final List<BakedQuad> north,
|
| 31 | final List<BakedQuad> south,
|
| 32 | final List<BakedQuad> east,
|
| 33 | final List<BakedQuad> west,
|
| 34 | final List<BakedQuad> up,
|
| 35 | final List<BakedQuad> down
|
| 36 | ) {
|
| 37 | this.all = all;
|
| 38 | this.unculled = unculled;
|
| 39 | this.north = north;
|
| 40 | this.south = south;
|
| 41 | this.east = east;
|
| 42 | this.west = west;
|
| 43 | this.up = up;
|
| 44 | this.down = down;
|
| 45 | }
|
| 46 |
|
| 47 | private static @BakedQuad.MaterialFlags int computeMaterialFlags(final List<BakedQuad> quads) {
|
| 48 | int flags = 0;
|
| 49 |
|
| 50 | for (BakedQuad quad : quads) {
|
| 51 | flags |= quad.materialInfo().flags();
|
| 52 | }
|
| 53 |
|
| 54 | return flags;
|
| 55 | }
|
| 56 |
|
| 57 | public List<BakedQuad> getQuads(final @Nullable Direction direction) {
|
| 58 | return switch (direction) {
|
| 59 | case null -> this.unculled;
|
| 60 | case NORTH -> this.north;
|
| 61 | case SOUTH -> this.south;
|
| 62 | case EAST -> this.east;
|
| 63 | case WEST -> this.west;
|
| 64 | case UP -> this.up;
|
| 65 | case DOWN -> this.down;
|
| 66 | };
|
| 67 | }
|
| 68 |
|
| 69 | public List<BakedQuad> getAll() {
|
| 70 | return this.all;
|
| 71 | }
|
| 72 |
|
| 73 | public @BakedQuad.MaterialFlags int materialFlags() {
|
| 74 | if (this.materialFlags == -1) {
|
| 75 | this.materialFlags = computeMaterialFlags(this.all);
|
| 76 | }
|
| 77 |
|
| 78 | return this.materialFlags;
|
| 79 | }
|
| 80 |
|
| 81 | public boolean hasMaterialFlag(final @BakedQuad.MaterialFlags int flag) {
|
| 82 | return (this.materialFlags() & flag) != 0;
|
| 83 | }
|
| 84 |
|
| 85 | @Environment(EnvType.CLIENT)
|
| 86 | public static class Builder {
|
| 87 | private final ImmutableList.Builder<BakedQuad> unculledFaces = ImmutableList.builder();
|
| 88 | private final Multimap<Direction, BakedQuad> culledFaces = ArrayListMultimap.create();
|
| 89 |
|
| 90 | public QuadCollection.Builder addCulledFace(final Direction direction, final BakedQuad quad) {
|
| 91 | this.culledFaces.put(direction, quad);
|
| 92 | return this;
|
| 93 | }
|
| 94 |
|
| 95 | public QuadCollection.Builder addUnculledFace(final BakedQuad quad) {
|
| 96 | this.unculledFaces.add(quad);
|
| 97 | return this;
|
| 98 | }
|
| 99 |
|
| 100 | public QuadCollection.Builder addAll(final QuadCollection quadCollection) {
|
| 101 | this.culledFaces.putAll(Direction.UP, quadCollection.up);
|
| 102 | this.culledFaces.putAll(Direction.DOWN, quadCollection.down);
|
| 103 | this.culledFaces.putAll(Direction.NORTH, quadCollection.north);
|
| 104 | this.culledFaces.putAll(Direction.SOUTH, quadCollection.south);
|
| 105 | this.culledFaces.putAll(Direction.EAST, quadCollection.east);
|
| 106 | this.culledFaces.putAll(Direction.WEST, quadCollection.west);
|
| 107 | this.unculledFaces.addAll(quadCollection.unculled);
|
| 108 | return this;
|
| 109 | }
|
| 110 |
|
| 111 | private static QuadCollection createFromSublists(
|
| 112 | final List<BakedQuad> all,
|
| 113 | final int unculledCount,
|
| 114 | final int northCount,
|
| 115 | final int southCount,
|
| 116 | final int eastCount,
|
| 117 | final int westCount,
|
| 118 | final int upCount,
|
| 119 | final int downCount
|
| 120 | ) {
|
| 121 | int index = 0;
|
| 122 | int var16;
|
| 123 | List<BakedQuad> unculled = all.subList(index, var16 = index + unculledCount);
|
| 124 | List<BakedQuad> north = all.subList(var16, index = var16 + northCount);
|
| 125 | int var18;
|
| 126 | List<BakedQuad> south = all.subList(index, var18 = index + southCount);
|
| 127 | List<BakedQuad> east = all.subList(var18, index = var18 + eastCount);
|
| 128 | int var20;
|
| 129 | List<BakedQuad> west = all.subList(index, var20 = index + westCount);
|
| 130 | List<BakedQuad> up = all.subList(var20, index = var20 + upCount);
|
| 131 | List<BakedQuad> down = all.subList(index, index + downCount);
|
| 132 | return new QuadCollection(all, unculled, north, south, east, west, up, down);
|
| 133 | }
|
| 134 |
|
| 135 | public QuadCollection build() {
|
| 136 | ImmutableList<BakedQuad> unculledFaces = this.unculledFaces.build();
|
| 137 | if (this.culledFaces.isEmpty()) {
|
| 138 | return unculledFaces.isEmpty()
|
| 139 | ? QuadCollection.EMPTY
|
| 140 | : new QuadCollection(unculledFaces, unculledFaces, List.of(), List.of(), List.of(), List.of(), List.of(), List.of());
|
| 141 | }
|
| 142 |
|
| 143 | ImmutableList.Builder<BakedQuad> quads = ImmutableList.builder();
|
| 144 | quads.addAll(unculledFaces);
|
| 145 | Collection<BakedQuad> north = this.culledFaces.get(Direction.NORTH);
|
| 146 | quads.addAll(north);
|
| 147 | Collection<BakedQuad> south = this.culledFaces.get(Direction.SOUTH);
|
| 148 | quads.addAll(south);
|
| 149 | Collection<BakedQuad> east = this.culledFaces.get(Direction.EAST);
|
| 150 | quads.addAll(east);
|
| 151 | Collection<BakedQuad> west = this.culledFaces.get(Direction.WEST);
|
| 152 | quads.addAll(west);
|
| 153 | Collection<BakedQuad> up = this.culledFaces.get(Direction.UP);
|
| 154 | quads.addAll(up);
|
| 155 | Collection<BakedQuad> down = this.culledFaces.get(Direction.DOWN);
|
| 156 | quads.addAll(down);
|
| 157 | return createFromSublists(quads.build(), unculledFaces.size(), north.size(), south.size(), east.size(), west.size(), up.size(), down.size());
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 |
|