| 1 | package com.mojang.math;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Objects;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.util.ExtraCodecs;
|
| 8 | import net.minecraft.util.Util;
|
| 9 | import org.joml.Matrix4f;
|
| 10 | import org.joml.Matrix4fc;
|
| 11 | import org.joml.Quaternionf;
|
| 12 | import org.joml.Quaternionfc;
|
| 13 | import org.joml.Vector3f;
|
| 14 | import org.joml.Vector3fc;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public final class Transformation {
|
| 18 | private static final Vector3fc ZERO_TRANSLATION = new Vector3f();
|
| 19 | private static final Vector3fc UNIT_SCALE = new Vector3f(1.0F, 1.0F, 1.0F);
|
| 20 | private static final Quaternionfc ZERO_ROTATION = new Quaternionf();
|
| 21 | private final Matrix4fc matrix;
|
| 22 | public static final Codec<Transformation> CODEC = RecordCodecBuilder.create(
|
| 23 | i -> i.group(
|
| 24 | ExtraCodecs.VECTOR3F.fieldOf("translation").forGetter(Transformation::translation),
|
| 25 | ExtraCodecs.QUATERNIONF.fieldOf("left_rotation").forGetter(Transformation::leftRotation),
|
| 26 | ExtraCodecs.VECTOR3F.fieldOf("scale").forGetter(Transformation::scale),
|
| 27 | ExtraCodecs.QUATERNIONF.fieldOf("right_rotation").forGetter(Transformation::rightRotation)
|
| 28 | )
|
| 29 | .apply(i, Transformation::new)
|
| 30 | );
|
| 31 | public static final Codec<Transformation> EXTENDED_CODEC = Codec.withAlternative(
|
| 32 | CODEC, ExtraCodecs.MATRIX4F.xmap(Transformation::new, Transformation::getMatrix)
|
| 33 | );
|
| 34 | private boolean decomposed;
|
| 35 | private @Nullable Vector3fc translation;
|
| 36 | private @Nullable Quaternionfc leftRotation;
|
| 37 | private @Nullable Vector3fc scale;
|
| 38 | private @Nullable Quaternionfc rightRotation;
|
| 39 | public static final Transformation IDENTITY = Util.make(() -> {
|
| 40 | Transformation identity = new Transformation(new Matrix4f());
|
| 41 | identity.translation = ZERO_TRANSLATION;
|
| 42 | identity.leftRotation = ZERO_ROTATION;
|
| 43 | identity.scale = UNIT_SCALE;
|
| 44 | identity.rightRotation = ZERO_ROTATION;
|
| 45 | identity.decomposed = true;
|
| 46 | return identity;
|
| 47 | });
|
| 48 |
|
| 49 | public Transformation(final Matrix4fc matrix) {
|
| 50 | this.matrix = matrix;
|
| 51 | }
|
| 52 |
|
| 53 | public Transformation(
|
| 54 | final @Nullable Vector3fc translation, final @Nullable Quaternionfc leftRotation, final @Nullable Vector3fc scale, final @Nullable Quaternionfc rightRotation
|
| 55 | ) {
|
| 56 | this.matrix = compose(translation, leftRotation, scale, rightRotation);
|
| 57 | this.translation = Objects.requireNonNullElse(translation, ZERO_TRANSLATION);
|
| 58 | this.leftRotation = Objects.requireNonNullElse(leftRotation, ZERO_ROTATION);
|
| 59 | this.scale = Objects.requireNonNullElse(scale, UNIT_SCALE);
|
| 60 | this.rightRotation = Objects.requireNonNullElse(rightRotation, ZERO_ROTATION);
|
| 61 | this.decomposed = true;
|
| 62 | }
|
| 63 |
|
| 64 | public Transformation compose(final Transformation that) {
|
| 65 | Matrix4f matrix = this.getMatrixCopy();
|
| 66 | matrix.mul(that.getMatrix());
|
| 67 | return new Transformation(matrix);
|
| 68 | }
|
| 69 |
|
| 70 | public @Nullable Transformation inverse() {
|
| 71 | if (this == IDENTITY) {
|
| 72 | return this;
|
| 73 | }
|
| 74 |
|
| 75 | Matrix4f matrix = this.getMatrixCopy().invertAffine();
|
| 76 | return matrix.isFinite() ? new Transformation(matrix) : null;
|
| 77 | }
|
| 78 |
|
| 79 | private void ensureDecomposed() {
|
| 80 | if (!this.decomposed) {
|
| 81 | Vector3f translation = new Vector3f();
|
| 82 | Quaternionf leftRotation = new Quaternionf();
|
| 83 | Vector3f scale = new Vector3f();
|
| 84 | Quaternionf rightRotation = new Quaternionf();
|
| 85 | MatrixUtil.svdDecompose(this.matrix, translation, leftRotation, scale, rightRotation);
|
| 86 | this.translation = translation;
|
| 87 | this.leftRotation = leftRotation;
|
| 88 | this.scale = scale;
|
| 89 | this.rightRotation = rightRotation;
|
| 90 | this.decomposed = true;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | private static Matrix4f compose(
|
| 95 | final @Nullable Vector3fc translation, final @Nullable Quaternionfc leftRotation, final @Nullable Vector3fc scale, final @Nullable Quaternionfc rightRotation
|
| 96 | ) {
|
| 97 | Matrix4f result = new Matrix4f();
|
| 98 | if (translation != null) {
|
| 99 | result.translation(translation);
|
| 100 | }
|
| 101 |
|
| 102 | if (leftRotation != null) {
|
| 103 | result.rotate(leftRotation);
|
| 104 | }
|
| 105 |
|
| 106 | if (scale != null) {
|
| 107 | result.scale(scale);
|
| 108 | }
|
| 109 |
|
| 110 | if (rightRotation != null) {
|
| 111 | result.rotate(rightRotation);
|
| 112 | }
|
| 113 |
|
| 114 | return result;
|
| 115 | }
|
| 116 |
|
| 117 | public Matrix4fc getMatrix() {
|
| 118 | return this.matrix;
|
| 119 | }
|
| 120 |
|
| 121 | public Matrix4f getMatrixCopy() {
|
| 122 | return new Matrix4f(this.matrix);
|
| 123 | }
|
| 124 |
|
| 125 | public Vector3fc translation() {
|
| 126 | this.ensureDecomposed();
|
| 127 | return this.translation;
|
| 128 | }
|
| 129 |
|
| 130 | public Quaternionfc leftRotation() {
|
| 131 | this.ensureDecomposed();
|
| 132 | return this.leftRotation;
|
| 133 | }
|
| 134 |
|
| 135 | public Vector3fc scale() {
|
| 136 | this.ensureDecomposed();
|
| 137 | return this.scale;
|
| 138 | }
|
| 139 |
|
| 140 | public Quaternionfc rightRotation() {
|
| 141 | this.ensureDecomposed();
|
| 142 | return this.rightRotation;
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public boolean equals(final Object o) {
|
| 147 | if (this == o) {
|
| 148 | return true;
|
| 149 | } else if (o != null && this.getClass() == o.getClass()) {
|
| 150 | Transformation that = (Transformation)o;
|
| 151 | return Objects.equals(this.matrix, that.matrix);
|
| 152 | } else {
|
| 153 | return false;
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public int hashCode() {
|
| 159 | return Objects.hash(this.matrix);
|
| 160 | }
|
| 161 |
|
| 162 | public Transformation slerp(final Transformation that, final float progress) {
|
| 163 | return new Transformation(
|
| 164 | this.translation().lerp(that.translation(), progress, new Vector3f()),
|
| 165 | this.leftRotation().slerp(that.leftRotation(), progress, new Quaternionf()),
|
| 166 | this.scale().lerp(that.scale(), progress, new Vector3f()),
|
| 167 | this.rightRotation().slerp(that.rightRotation(), progress, new Quaternionf())
|
| 168 | );
|
| 169 | }
|
| 170 |
|
| 171 | public static Matrix4fc compose(final Matrix4fc parent, final Optional<Transformation> transform) {
|
| 172 | return transform.isPresent() ? parent.mul(transform.get().getMatrix(), new Matrix4f()) : parent;
|
| 173 | }
|
| 174 | }
|
| 175 |
|