| 1 | package net.minecraft.client.resources.model.cuboid;
|
| 2 |
|
| 3 | import com.mojang.math.MatrixUtil;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.core.Direction.Axis;
|
| 7 | import org.joml.Math;
|
| 8 | import org.joml.Matrix4f;
|
| 9 | import org.joml.Matrix4fc;
|
| 10 | import org.joml.Vector3f;
|
| 11 | import org.joml.Vector3fc;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public record CuboidRotation(Vector3fc origin, CuboidRotation.RotationValue value, boolean rescale, Matrix4fc transform) {
|
| 15 | public CuboidRotation(final Vector3fc origin, final CuboidRotation.RotationValue value, final boolean rescale) {
|
| 16 | this(origin, value, rescale, computeTransform(value, rescale));
|
| 17 | }
|
| 18 |
|
| 19 | private static Matrix4f computeTransform(final CuboidRotation.RotationValue value, final boolean rescale) {
|
| 20 | Matrix4f result = value.transformation();
|
| 21 | if (rescale && !MatrixUtil.isIdentity(result)) {
|
| 22 | Vector3fc scale = computeRescale(result);
|
| 23 | result.scale(scale);
|
| 24 | }
|
| 25 |
|
| 26 | return result;
|
| 27 | }
|
| 28 |
|
| 29 | private static Vector3fc computeRescale(final Matrix4fc rotation) {
|
| 30 | Vector3f scratch = new Vector3f();
|
| 31 | float scaleX = scaleFactorForAxis(rotation, Axis.X, scratch);
|
| 32 | float scaleY = scaleFactorForAxis(rotation, Axis.Y, scratch);
|
| 33 | float scaleZ = scaleFactorForAxis(rotation, Axis.Z, scratch);
|
| 34 | return scratch.set(scaleX, scaleY, scaleZ);
|
| 35 | }
|
| 36 |
|
| 37 | private static float scaleFactorForAxis(final Matrix4fc rotation, final Axis axis, final Vector3f scratch) {
|
| 38 | Vector3f axisUnit = scratch.set(axis.getPositive().getUnitVec3f());
|
| 39 | Vector3f transformedAxisUnit = rotation.transformDirection(axisUnit);
|
| 40 | float absX = Math.abs(transformedAxisUnit.x);
|
| 41 | float absY = Math.abs(transformedAxisUnit.y);
|
| 42 | float absZ = Math.abs(transformedAxisUnit.z);
|
| 43 | float maxComponent = Math.max(Math.max(absX, absY), absZ);
|
| 44 | return 1.0F / maxComponent;
|
| 45 | }
|
| 46 |
|
| 47 | @Environment(EnvType.CLIENT)
|
| 48 | public record EulerXYZRotation(float x, float y, float z) implements CuboidRotation.RotationValue {
|
| 49 | @Override
|
| 50 | public Matrix4f transformation() {
|
| 51 | return new Matrix4f()
|
| 52 | .rotationZYX(this.z * (float) (java.lang.Math.PI / 180.0), this.y * (float) (java.lang.Math.PI / 180.0), this.x * (float) (java.lang.Math.PI / 180.0));
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @Environment(EnvType.CLIENT)
|
| 57 | public interface RotationValue {
|
| 58 | Matrix4f transformation();
|
| 59 | }
|
| 60 |
|
| 61 | @Environment(EnvType.CLIENT)
|
| 62 | public record SingleAxisRotation(Axis axis, float angle) implements CuboidRotation.RotationValue {
|
| 63 | @Override
|
| 64 | public Matrix4f transformation() {
|
| 65 | Matrix4f result = new Matrix4f();
|
| 66 | if (this.angle == 0.0F) {
|
| 67 | return result;
|
| 68 | }
|
| 69 |
|
| 70 | Vector3fc rotateAround = this.axis.getPositive().getUnitVec3f();
|
| 71 | result.rotation(this.angle * (float) (java.lang.Math.PI / 180.0), rotateAround);
|
| 72 | return result;
|
| 73 | }
|
| 74 | }
|
| 75 | }
|
| 76 |
|