| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.math.MatrixUtil;
|
| 5 | import com.mojang.math.Transformation;
|
| 6 | import java.util.Map;
|
| 7 | import net.minecraft.util.Util;
|
| 8 | import org.joml.Matrix4f;
|
| 9 | import org.joml.Quaternionf;
|
| 10 | import org.joml.Vector3f;
|
| 11 |
|
| 12 | public class BlockMath {
|
| 13 | private static final Map<Direction, Transformation> VANILLA_UV_TRANSFORM_LOCAL_TO_GLOBAL = Maps.newEnumMap(
|
| 14 | Map.of(
|
| 15 | Direction.SOUTH,
|
| 16 | Transformation.IDENTITY,
|
| 17 | Direction.EAST,
|
| 18 | new Transformation(null, new Quaternionf().rotateY((float) (Math.PI / 2)), null, null),
|
| 19 | Direction.WEST,
|
| 20 | new Transformation(null, new Quaternionf().rotateY((float) (-Math.PI / 2)), null, null),
|
| 21 | Direction.NORTH,
|
| 22 | new Transformation(null, new Quaternionf().rotateY((float) Math.PI), null, null),
|
| 23 | Direction.UP,
|
| 24 | new Transformation(null, new Quaternionf().rotateX((float) (-Math.PI / 2)), null, null),
|
| 25 | Direction.DOWN,
|
| 26 | new Transformation(null, new Quaternionf().rotateX((float) (Math.PI / 2)), null, null)
|
| 27 | )
|
| 28 | );
|
| 29 | private static final Map<Direction, Transformation> VANILLA_UV_TRANSFORM_GLOBAL_TO_LOCAL = Maps.newEnumMap(
|
| 30 | Util.mapValues(VANILLA_UV_TRANSFORM_LOCAL_TO_GLOBAL, Transformation::inverse)
|
| 31 | );
|
| 32 |
|
| 33 | public static Transformation blockCenterToCorner(final Transformation transform) {
|
| 34 | Matrix4f ret = new Matrix4f().translation(0.5F, 0.5F, 0.5F);
|
| 35 | ret.mul(transform.getMatrix());
|
| 36 | ret.translate(-0.5F, -0.5F, -0.5F);
|
| 37 | return new Transformation(ret);
|
| 38 | }
|
| 39 |
|
| 40 | public static Transformation blockCornerToCenter(final Transformation transform) {
|
| 41 | Matrix4f ret = new Matrix4f().translation(-0.5F, -0.5F, -0.5F);
|
| 42 | ret.mul(transform.getMatrix());
|
| 43 | ret.translate(0.5F, 0.5F, 0.5F);
|
| 44 | return new Transformation(ret);
|
| 45 | }
|
| 46 |
|
| 47 | public static Transformation getFaceTransformation(final Transformation transformation, final Direction originalSide) {
|
| 48 | if (MatrixUtil.isIdentity(transformation.getMatrix())) {
|
| 49 | return transformation;
|
| 50 | }
|
| 51 |
|
| 52 | Transformation faceAction = VANILLA_UV_TRANSFORM_LOCAL_TO_GLOBAL.get(originalSide);
|
| 53 | faceAction = transformation.compose(faceAction);
|
| 54 | Vector3f transformedNormal = faceAction.getMatrix().transformDirection(new Vector3f(0.0F, 0.0F, 1.0F));
|
| 55 | Direction newSide = Direction.getApproximateNearest(transformedNormal.x, transformedNormal.y, transformedNormal.z);
|
| 56 | return VANILLA_UV_TRANSFORM_GLOBAL_TO_LOCAL.get(newSide).compose(faceAction);
|
| 57 | }
|
| 58 | }
|
| 59 |
|