| 1 | package net.minecraft.client.renderer.block.dispatch;
|
| 2 |
|
| 3 | import com.mojang.math.OctahedralGroup;
|
| 4 | import com.mojang.math.Transformation;
|
| 5 | import java.util.EnumMap;
|
| 6 | import java.util.Map;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.core.BlockMath;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.util.Util;
|
| 12 | import org.joml.Matrix4f;
|
| 13 | import org.joml.Matrix4fc;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class BlockModelRotation implements ModelState {
|
| 17 | private static final Map<OctahedralGroup, BlockModelRotation> BY_GROUP_ORDINAL = Util.makeEnumMap(OctahedralGroup.class, BlockModelRotation::new);
|
| 18 | public static final BlockModelRotation IDENTITY = get(OctahedralGroup.IDENTITY);
|
| 19 | private final OctahedralGroup orientation;
|
| 20 | private final Transformation transformation;
|
| 21 | private final Map<Direction, Matrix4fc> faceMapping = new EnumMap<>(Direction.class);
|
| 22 | private final Map<Direction, Matrix4fc> inverseFaceMapping = new EnumMap<>(Direction.class);
|
| 23 | private final BlockModelRotation.WithUvLock withUvLock = new BlockModelRotation.WithUvLock(this);
|
| 24 |
|
| 25 | private BlockModelRotation(final OctahedralGroup orientation) {
|
| 26 | this.orientation = orientation;
|
| 27 | if (orientation != OctahedralGroup.IDENTITY) {
|
| 28 | this.transformation = new Transformation(new Matrix4f(orientation.transformation()));
|
| 29 | } else {
|
| 30 | this.transformation = Transformation.IDENTITY;
|
| 31 | }
|
| 32 |
|
| 33 | for (Direction face : Direction.values()) {
|
| 34 | Matrix4fc faceTransform = BlockMath.getFaceTransformation(this.transformation, face).getMatrix();
|
| 35 | this.faceMapping.put(face, faceTransform);
|
| 36 | this.inverseFaceMapping.put(face, faceTransform.invertAffine(new Matrix4f()));
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public Transformation transformation() {
|
| 42 | return this.transformation;
|
| 43 | }
|
| 44 |
|
| 45 | public static BlockModelRotation get(final OctahedralGroup group) {
|
| 46 | return BY_GROUP_ORDINAL.get(group);
|
| 47 | }
|
| 48 |
|
| 49 | public ModelState withUvLock() {
|
| 50 | return this.withUvLock;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public String toString() {
|
| 55 | return "simple[" + this.orientation.getSerializedName() + "]";
|
| 56 | }
|
| 57 |
|
| 58 | @Environment(EnvType.CLIENT)
|
| 59 | private record WithUvLock(BlockModelRotation parent) implements ModelState {
|
| 60 | @Override
|
| 61 | public Transformation transformation() {
|
| 62 | return this.parent.transformation;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public Matrix4fc faceTransformation(final Direction face) {
|
| 67 | return this.parent.faceMapping.getOrDefault(face, NO_TRANSFORM);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public Matrix4fc inverseFaceTransformation(final Direction face) {
|
| 72 | return this.parent.inverseFaceMapping.getOrDefault(face, NO_TRANSFORM);
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public String toString() {
|
| 77 | return "uvLocked[" + this.parent.orientation.getSerializedName() + "]";
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|