| 1 | package net.minecraft.client.resources.model.cuboid;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.blaze3d.platform.Transparency;
|
| 5 | import com.mojang.math.MatrixUtil;
|
| 6 | import com.mojang.math.Quadrant;
|
| 7 | import com.mojang.math.Transformation;
|
| 8 | import java.util.Objects;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.model.geom.builders.UVPair;
|
| 12 | import net.minecraft.client.renderer.FaceInfo;
|
| 13 | import net.minecraft.client.renderer.block.dispatch.ModelState;
|
| 14 | import net.minecraft.client.resources.model.ModelBaker;
|
| 15 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 16 | import net.minecraft.client.resources.model.sprite.Material;
|
| 17 | import net.minecraft.core.Direction;
|
| 18 | import org.joml.GeometryUtils;
|
| 19 | import org.joml.Matrix4fc;
|
| 20 | import org.joml.Vector3f;
|
| 21 | import org.joml.Vector3fc;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class FaceBakery {
|
| 26 | private static final Vector3fc BLOCK_MIDDLE = new Vector3f(0.5F, 0.5F, 0.5F);
|
| 27 |
|
| 28 | @VisibleForTesting
|
| 29 | static CuboidFace.UVs defaultFaceUV(final Vector3fc from, final Vector3fc to, final Direction facing) {
|
| 30 | return switch (facing) {
|
| 31 | case DOWN -> new CuboidFace.UVs(from.x(), 16.0F - to.z(), to.x(), 16.0F - from.z());
|
| 32 | case UP -> new CuboidFace.UVs(from.x(), from.z(), to.x(), to.z());
|
| 33 | case NORTH -> new CuboidFace.UVs(16.0F - to.x(), 16.0F - to.y(), 16.0F - from.x(), 16.0F - from.y());
|
| 34 | case SOUTH -> new CuboidFace.UVs(from.x(), 16.0F - to.y(), to.x(), 16.0F - from.y());
|
| 35 | case WEST -> new CuboidFace.UVs(from.z(), 16.0F - to.y(), to.z(), 16.0F - from.y());
|
| 36 | case EAST -> new CuboidFace.UVs(16.0F - to.z(), 16.0F - to.y(), 16.0F - from.z(), 16.0F - from.y());
|
| 37 | };
|
| 38 | }
|
| 39 |
|
| 40 | private static Transparency computeMaterialTransparency(final Material.Baked material, final CuboidFace.UVs uvs) {
|
| 41 | return material.forceTranslucent()
|
| 42 | ? Transparency.TRANSLUCENT
|
| 43 | : material.sprite()
|
| 44 | .contents()
|
| 45 | .computeTransparency(
|
| 46 | Math.min(uvs.minU(), uvs.maxU()) / 16.0F,
|
| 47 | Math.min(uvs.minV(), uvs.maxV()) / 16.0F,
|
| 48 | Math.max(uvs.minU(), uvs.maxU()) / 16.0F,
|
| 49 | Math.max(uvs.minV(), uvs.maxV()) / 16.0F
|
| 50 | );
|
| 51 | }
|
| 52 |
|
| 53 | public static BakedQuad bakeQuad(
|
| 54 | final ModelBaker modelBaker,
|
| 55 | final Vector3fc from,
|
| 56 | final Vector3fc to,
|
| 57 | final CuboidFace face,
|
| 58 | final Material.Baked material,
|
| 59 | final Direction facing,
|
| 60 | final ModelState modelState,
|
| 61 | final @Nullable CuboidRotation elementRotation,
|
| 62 | final @Nullable Direction shadeDirectionOverride,
|
| 63 | final int lightEmission
|
| 64 | ) {
|
| 65 | CuboidFace.UVs uvs = face.uvs();
|
| 66 | if (uvs == null) {
|
| 67 | uvs = defaultFaceUV(from, to, facing);
|
| 68 | }
|
| 69 |
|
| 70 | Transparency transparency = computeMaterialTransparency(material, uvs);
|
| 71 | ModelBaker.Interner interner = modelBaker.interner();
|
| 72 | BakedQuad.MaterialInfo materialInfo = interner.materialInfo(
|
| 73 | BakedQuad.MaterialInfo.of(material, transparency, face.tintIndex(), shadeDirectionOverride, lightEmission)
|
| 74 | );
|
| 75 | return bakeQuad(interner, from, to, uvs, face.rotation(), materialInfo, facing, modelState, elementRotation);
|
| 76 | }
|
| 77 |
|
| 78 | public static BakedQuad bakeQuad(
|
| 79 | final ModelBaker.Interner interner,
|
| 80 | final Vector3fc from,
|
| 81 | final Vector3fc to,
|
| 82 | final CuboidFace.UVs uvs,
|
| 83 | final Quadrant uvRotation,
|
| 84 | final BakedQuad.MaterialInfo materialInfo,
|
| 85 | final Direction facing,
|
| 86 | final ModelState modelState,
|
| 87 | final @Nullable CuboidRotation elementRotation
|
| 88 | ) {
|
| 89 | Matrix4fc uvTransform = modelState.inverseFaceTransformation(facing);
|
| 90 | Vector3fc[] vertexPositions = new Vector3fc[4];
|
| 91 | long[] vertexPackedUvs = new long[4];
|
| 92 | FaceInfo faceInfo = FaceInfo.fromFacing(facing);
|
| 93 |
|
| 94 | for (int i = 0; i < 4; i++) {
|
| 95 | bakeVertex(
|
| 96 | i, faceInfo, uvs, uvRotation, uvTransform, from, to, materialInfo, modelState.transformation(), elementRotation, vertexPositions, vertexPackedUvs, interner
|
| 97 | );
|
| 98 | }
|
| 99 |
|
| 100 | Direction finalDirection = calculateFacing(vertexPositions);
|
| 101 | if (elementRotation == null && finalDirection != null) {
|
| 102 | recalculateWinding(vertexPositions, vertexPackedUvs, finalDirection);
|
| 103 | }
|
| 104 |
|
| 105 | return new BakedQuad(
|
| 106 | vertexPositions[0],
|
| 107 | vertexPositions[1],
|
| 108 | vertexPositions[2],
|
| 109 | vertexPositions[3],
|
| 110 | vertexPackedUvs[0],
|
| 111 | vertexPackedUvs[1],
|
| 112 | vertexPackedUvs[2],
|
| 113 | vertexPackedUvs[3],
|
| 114 | Objects.requireNonNullElse(finalDirection, Direction.UP),
|
| 115 | materialInfo
|
| 116 | );
|
| 117 | }
|
| 118 |
|
| 119 | private static void bakeVertex(
|
| 120 | final int index,
|
| 121 | final FaceInfo faceInfo,
|
| 122 | final CuboidFace.UVs uvs,
|
| 123 | final Quadrant uvRotation,
|
| 124 | final Matrix4fc uvTransform,
|
| 125 | final Vector3fc from,
|
| 126 | final Vector3fc to,
|
| 127 | final BakedQuad.MaterialInfo materialInfo,
|
| 128 | final Transformation rotation,
|
| 129 | final @Nullable CuboidRotation elementRotation,
|
| 130 | final Vector3fc[] positionOutput,
|
| 131 | final long[] uvOutput,
|
| 132 | final ModelBaker.Interner interner
|
| 133 | ) {
|
| 134 | FaceInfo.VertexInfo vertexInfo = faceInfo.getVertexInfo(index);
|
| 135 | Vector3f vertex = vertexInfo.select(from, to).div(16.0F);
|
| 136 | if (elementRotation != null) {
|
| 137 | rotateVertexBy(vertex, elementRotation.origin(), elementRotation.transform());
|
| 138 | }
|
| 139 |
|
| 140 | if (rotation != Transformation.IDENTITY) {
|
| 141 | rotateVertexBy(vertex, BLOCK_MIDDLE, rotation.getMatrix());
|
| 142 | }
|
| 143 |
|
| 144 | float rawU = CuboidFace.getU(uvs, uvRotation, index);
|
| 145 | float rawV = CuboidFace.getV(uvs, uvRotation, index);
|
| 146 | float transformedV;
|
| 147 | float transformedU;
|
| 148 | if (MatrixUtil.isIdentity(uvTransform)) {
|
| 149 | transformedU = rawU;
|
| 150 | transformedV = rawV;
|
| 151 | } else {
|
| 152 | Vector3f transformedUV = uvTransform.transformPosition(new Vector3f(cornerToCenter(rawU), cornerToCenter(rawV), 0.0F));
|
| 153 | transformedU = centerToCorner(transformedUV.x);
|
| 154 | transformedV = centerToCorner(transformedUV.y);
|
| 155 | }
|
| 156 |
|
| 157 | positionOutput[index] = interner.vector(vertex);
|
| 158 | uvOutput[index] = UVPair.pack(materialInfo.sprite().getU(transformedU), materialInfo.sprite().getV(transformedV));
|
| 159 | }
|
| 160 |
|
| 161 | private static float cornerToCenter(final float value) {
|
| 162 | return value - 0.5F;
|
| 163 | }
|
| 164 |
|
| 165 | private static float centerToCorner(final float value) {
|
| 166 | return value + 0.5F;
|
| 167 | }
|
| 168 |
|
| 169 | private static void rotateVertexBy(final Vector3f vertex, final Vector3fc origin, final Matrix4fc transformation) {
|
| 170 | vertex.sub(origin);
|
| 171 | transformation.transformPosition(vertex);
|
| 172 | vertex.add(origin);
|
| 173 | }
|
| 174 |
|
| 175 | private static @Nullable Direction calculateFacing(final Vector3fc[] positions) {
|
| 176 | Vector3f normal = new Vector3f();
|
| 177 | GeometryUtils.normal(positions[0], positions[1], positions[2], normal);
|
| 178 | return findClosestDirection(normal);
|
| 179 | }
|
| 180 |
|
| 181 | private static @Nullable Direction findClosestDirection(final Vector3f direction) {
|
| 182 | if (!direction.isFinite()) {
|
| 183 | return null;
|
| 184 | }
|
| 185 |
|
| 186 | Direction best = null;
|
| 187 | float closestProduct = 0.0F;
|
| 188 |
|
| 189 | for (Direction candidate : Direction.values()) {
|
| 190 | float product = direction.dot(candidate.getUnitVec3f());
|
| 191 | if (product >= 0.0F && product > closestProduct) {
|
| 192 | closestProduct = product;
|
| 193 | best = candidate;
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | return best;
|
| 198 | }
|
| 199 |
|
| 200 | private static void recalculateWinding(final Vector3fc[] positions, final long[] uvs, final Direction direction) {
|
| 201 | float minX = 999.0F;
|
| 202 | float minY = 999.0F;
|
| 203 | float minZ = 999.0F;
|
| 204 | float maxX = -999.0F;
|
| 205 | float maxY = -999.0F;
|
| 206 | float maxZ = -999.0F;
|
| 207 |
|
| 208 | for (int i = 0; i < 4; i++) {
|
| 209 | Vector3fc position = positions[i];
|
| 210 | float x = position.x();
|
| 211 | float y = position.y();
|
| 212 | float z = position.z();
|
| 213 | if (x < minX) {
|
| 214 | minX = x;
|
| 215 | }
|
| 216 |
|
| 217 | if (y < minY) {
|
| 218 | minY = y;
|
| 219 | }
|
| 220 |
|
| 221 | if (z < minZ) {
|
| 222 | minZ = z;
|
| 223 | }
|
| 224 |
|
| 225 | if (x > maxX) {
|
| 226 | maxX = x;
|
| 227 | }
|
| 228 |
|
| 229 | if (y > maxY) {
|
| 230 | maxY = y;
|
| 231 | }
|
| 232 |
|
| 233 | if (z > maxZ) {
|
| 234 | maxZ = z;
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | FaceInfo info = FaceInfo.fromFacing(direction);
|
| 239 |
|
| 240 | for (int vertex = 0; vertex < 4; vertex++) {
|
| 241 | FaceInfo.VertexInfo vertInfo = info.getVertexInfo(vertex);
|
| 242 | float newX = vertInfo.xFace().select(minX, minY, minZ, maxX, maxY, maxZ);
|
| 243 | float newY = vertInfo.yFace().select(minX, minY, minZ, maxX, maxY, maxZ);
|
| 244 | float newZ = vertInfo.zFace().select(minX, minY, minZ, maxX, maxY, maxZ);
|
| 245 | int vertexToSwap = findVertex(positions, vertex, newX, newY, newZ);
|
| 246 | if (vertexToSwap == -1) {
|
| 247 | throw new IllegalStateException("Can't find vertex to swap");
|
| 248 | }
|
| 249 |
|
| 250 | if (vertexToSwap != vertex) {
|
| 251 | swap(positions, vertexToSwap, vertex);
|
| 252 | swap(uvs, vertexToSwap, vertex);
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | private static int findVertex(final Vector3fc[] positions, final int start, final float x, final float y, final float z) {
|
| 258 | for (int i = start; i < 4; i++) {
|
| 259 | Vector3fc position = positions[i];
|
| 260 | if (x == position.x() && y == position.y() && z == position.z()) {
|
| 261 | return i;
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | return -1;
|
| 266 | }
|
| 267 |
|
| 268 | private static void swap(final Vector3fc[] array, final int indexA, final int indexB) {
|
| 269 | Vector3fc tmp = array[indexA];
|
| 270 | array[indexA] = array[indexB];
|
| 271 | array[indexB] = tmp;
|
| 272 | }
|
| 273 |
|
| 274 | private static void swap(final long[] array, final int indexA, final int indexB) {
|
| 275 | long tmp = array[indexA];
|
| 276 | array[indexA] = array[indexB];
|
| 277 | array[indexB] = tmp;
|
| 278 | }
|
| 279 | }
|
| 280 |
|