[1.19.4] Move root transform builder to ModelBuilder to allow use in ItemModelBuilder (#9456)

This commit is contained in:
Dennis C authored and GitHub committed 2023-04-20 09:27:30 +02:00
1 parent f2aeeeb1fe
commit b398f52a7f
3 files changed
+404 -102

No files matched your search

@@ -5,8 +5,6 @@
package net.minecraftforge.client.model.generators;
import com.google.common.base.Preconditions;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.math.Transformation;
import net.minecraft.resources.ResourceLocation;
@@ -28,13 +26,18 @@ import org.joml.Vector3f;
*/
public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
{
private final RootTransformBuilder rootTransform = new RootTransformBuilder();
private final RootTransformBuilder rootTransform;
public BlockModelBuilder(ResourceLocation outputLocation, ExistingFileHelper existingFileHelper)
{
super(outputLocation, existingFileHelper);
this.rootTransform = new RootTransformBuilder(super.rootTransforms());
}
/**
* @deprecated Use {@link ModelBuilder#rootTransforms()} instead
*/
@Deprecated(forRemoval = true, since = "1.19.4")
public RootTransformBuilder rootTransform()
{
return rootTransform;
@@ -43,32 +46,20 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
@Override
public JsonObject toJson()
{
JsonObject json = super.toJson();
// If there were any transform properties set, add them to the output.
JsonObject transform = rootTransform.toJson();
if (transform.size() > 0)
{
json.add("transform", transform);
}
return json;
return super.toJson();
}
/**
* @deprecated Use {@link ModelBuilder.RootTransformsBuilder} instead via {@link ModelBuilder#rootTransforms()}
*/
@Deprecated(forRemoval = true, since = "1.19.4")
public class RootTransformBuilder
{
private static final Vector3f ONE = new Vector3f(1, 1, 1);
private final ModelBuilder<BlockModelBuilder>.RootTransformsBuilder rootTransforms;
private Vector3f translation = new Vector3f();
private Quaternionf leftRotation = new Quaternionf();
private Quaternionf rightRotation = new Quaternionf();
private Vector3f scale = ONE;
private @Nullable TransformOrigin origin;
private @Nullable Vector3f originVec;
private RootTransformBuilder()
private RootTransformBuilder(ModelBuilder<BlockModelBuilder>.RootTransformsBuilder rootTransforms)
{
this.rootTransforms = rootTransforms;
}
/**
@@ -80,7 +71,7 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*/
public RootTransformBuilder translation(Vector3f translation)
{
this.translation = Preconditions.checkNotNull(translation, "Translation must not be null");
rootTransforms.translation(translation);
return this;
}
@@ -106,7 +97,7 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*/
public RootTransformBuilder rotation(Quaternionf rotation)
{
this.leftRotation = Preconditions.checkNotNull(rotation, "Rotation must not be null");
rootTransforms.rotation(rotation);
return this;
}
@@ -159,7 +150,7 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*/
public RootTransformBuilder rightRotation(Quaternionf rightRotation)
{
this.rightRotation = Preconditions.checkNotNull(rightRotation, "Rotation must not be null");
rootTransforms.rightRotation(rightRotation);
return this;
}
@@ -208,7 +199,6 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*
* @param scale the scale
* @return this builder
* @throws NullPointerException if {@code scale} is {@code null}
*/
public RootTransformBuilder scale(float scale)
{
@@ -237,7 +227,7 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*/
public RootTransformBuilder scale(Vector3f scale)
{
this.scale = Preconditions.checkNotNull(scale, "Scale must not be null");
rootTransforms.scale(scale);
return this;
}
@@ -250,11 +240,7 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*/
public RootTransformBuilder transform(Transformation transformation)
{
Preconditions.checkNotNull(transformation, "Transformation must not be null");
this.translation = transformation.getTranslation();
this.leftRotation = transformation.getLeftRotation();
this.rightRotation = transformation.getRightRotation();
this.scale = transformation.getScale();
rootTransforms.transform(transformation);
return this;
}
@@ -267,8 +253,7 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*/
public RootTransformBuilder origin(Vector3f origin)
{
this.originVec = Preconditions.checkNotNull(origin, "Origin must not be null");
this.origin = null;
rootTransforms.origin(origin);
return this;
}
@@ -282,8 +267,7 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
*/
public RootTransformBuilder origin(TransformOrigin origin)
{
this.origin = Preconditions.checkNotNull(origin, "Origin must not be null");
this.originVec = null;
rootTransforms.origin(origin.toNewOrigin());
return this;
}
@@ -296,67 +280,43 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
return BlockModelBuilder.this;
}
public JsonObject toJson() {
// Write the transform to an object
JsonObject transform = new JsonObject();
if (!translation.equals(0, 0, 0))
{
transform.add("translation", writeVec3(translation));
}
if (!scale.equals(ONE))
{
transform.add("scale", writeVec3(scale));
}
if (!leftRotation.equals(0, 0, 0, 1))
{
transform.add("rotation", writeQuaternion(leftRotation));
}
if (!rightRotation.equals(0, 0, 0, 1))
{
transform.add("post_rotation", writeQuaternion(rightRotation));
}
if (origin != null)
{
transform.addProperty("origin", origin.getSerializedName());
}
else if (originVec != null && !originVec.equals(0, 0, 0))
{
transform.add("origin", writeVec3(originVec));
}
return transform;
public JsonObject toJson()
{
return rootTransforms.toJson();
}
/**
* @deprecated Use {@link TransformationHelper.TransformOrigin} instead
*/
@Deprecated(forRemoval = true, since = "1.19.4")
public enum TransformOrigin implements StringRepresentable
{
CENTER(new Vector3f(.5f, .5f, .5f), "center"),
CORNER(new Vector3f(), "corner"),
OPPOSING_CORNER(ONE, "opposing-corner");
CENTER(TransformationHelper.TransformOrigin.CENTER),
CORNER(TransformationHelper.TransformOrigin.CORNER),
OPPOSING_CORNER(TransformationHelper.TransformOrigin.OPPOSING_CORNER);
private final Vector3f vec;
private final String name;
private final TransformationHelper.TransformOrigin newOrigin;
TransformOrigin(Vector3f vec, String name)
TransformOrigin(TransformationHelper.TransformOrigin newOrigin)
{
this.vec = vec;
this.name = name;
this.newOrigin = newOrigin;
}
public Vector3f getVector()
{
return vec;
return newOrigin.getVector();
}
@Override
@NotNull
public String getSerializedName()
{
return name;
return newOrigin.getSerializedName();
}
private TransformationHelper.TransformOrigin toNewOrigin()
{
return newOrigin;
}
public static @Nullable TransformOrigin fromString(String originName)
@@ -376,24 +336,5 @@ public class BlockModelBuilder extends ModelBuilder<BlockModelBuilder>
return null;
}
}
private JsonArray writeVec3(Vector3f vector)
{
JsonArray array = new JsonArray();
array.add(vector.x());
array.add(vector.y());
array.add(vector.z());
return array;
}
private JsonArray writeQuaternion(Quaternionf quaternion)
{
JsonArray array = new JsonArray();
array.add(quaternion.x());
array.add(quaternion.y());
array.add(quaternion.z());
array.add(quaternion.w());
return array;
}
}
}
@@ -20,6 +20,7 @@ import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.math.Transformation;
import com.mojang.serialization.JsonOps;
import net.minecraft.client.renderer.block.model.BlockFaceUV;
@@ -35,7 +36,9 @@ import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraftforge.client.model.ForgeFaceData;
import net.minecraftforge.common.data.ExistingFileHelper;
import net.minecraftforge.common.util.TransformationHelper;
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;
import org.joml.Vector3f;
/**
@@ -64,7 +67,10 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
protected CustomLoaderBuilder<T> customLoader = null;
protected ModelBuilder(ResourceLocation outputLocation, ExistingFileHelper existingFileHelper) {
private final RootTransformsBuilder rootTransforms = new RootTransformsBuilder();
protected ModelBuilder(ResourceLocation outputLocation, ExistingFileHelper existingFileHelper)
{
super(outputLocation);
this.existingFileHelper = existingFileHelper;
}
@@ -225,6 +231,11 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
return customLoader;
}
public RootTransformsBuilder rootTransforms()
{
return rootTransforms;
}
@VisibleForTesting
public JsonObject toJson() {
JsonObject root = new JsonObject();
@@ -332,6 +343,13 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
root.add("elements", elements);
}
// If there were any transform properties set, add them to the output.
JsonObject transform = rootTransforms.toJson();
if (transform.size() > 0)
{
root.add("transform", transform);
}
if (customLoader != null)
return customLoader.toJson(root);
@@ -783,4 +801,300 @@ public class ModelBuilder<T extends ModelBuilder<T>> extends ModelFile {
public TransformsBuilder end() { return TransformsBuilder.this; }
}
}
public class RootTransformsBuilder
{
private static final Vector3f ONE = new Vector3f(1, 1, 1);
private Vector3f translation = new Vector3f();
private Quaternionf leftRotation = new Quaternionf();
private Quaternionf rightRotation = new Quaternionf();
private Vector3f scale = ONE;
private @Nullable TransformationHelper.TransformOrigin origin;
private @Nullable Vector3f originVec;
RootTransformsBuilder()
{
}
/**
* Sets the translation of the root transform.
*
* @param translation the translation
* @return this builder
* @throws NullPointerException if {@code translation} is {@code null}
*/
public RootTransformsBuilder translation(Vector3f translation)
{
this.translation = Preconditions.checkNotNull(translation, "Translation must not be null");
return this;
}
/**
* Sets the translation of the root transform.
*
* @param x x translation
* @param y y translation
* @param z z translation
* @return this builder
*/
public RootTransformsBuilder translation(float x, float y, float z)
{
return translation(new Vector3f(x, y, z));
}
/**
* Sets the left rotation of the root transform.
*
* @param rotation the left rotation
* @return this builder
* @throws NullPointerException if {@code rotation} is {@code null}
*/
public RootTransformsBuilder rotation(Quaternionf rotation)
{
this.leftRotation = Preconditions.checkNotNull(rotation, "Rotation must not be null");
return this;
}
/**
* Sets the left rotation of the root transform.
*
* @param x x rotation
* @param y y rotation
* @param z z rotation
* @param isDegrees whether the rotation is in degrees or radians
* @return this builder
*/
public RootTransformsBuilder rotation(float x, float y, float z, boolean isDegrees)
{
return rotation(TransformationHelper.quatFromXYZ(x, y, z, isDegrees));
}
/**
* Sets the left rotation of the root transform.
*
* @param leftRotation the left rotation
* @return this builder
* @throws NullPointerException if {@code leftRotation} is {@code null}
*/
public RootTransformsBuilder leftRotation(Quaternionf leftRotation)
{
return rotation(leftRotation);
}
/**
* Sets the left rotation of the root transform.
*
* @param x x rotation
* @param y y rotation
* @param z z rotation
* @param isDegrees whether the rotation is in degrees or radians
* @return this builder
*/
public RootTransformsBuilder leftRotation(float x, float y, float z, boolean isDegrees)
{
return leftRotation(TransformationHelper.quatFromXYZ(x, y, z, isDegrees));
}
/**
* Sets the right rotation of the root transform.
*
* @param rightRotation the right rotation
* @return this builder
* @throws NullPointerException if {@code rightRotation} is {@code null}
*/
public RootTransformsBuilder rightRotation(Quaternionf rightRotation)
{
this.rightRotation = Preconditions.checkNotNull(rightRotation, "Rotation must not be null");
return this;
}
/**
* Sets the right rotation of the root transform.
*
* @param x x rotation
* @param y y rotation
* @param z z rotation
* @param isDegrees whether the rotation is in degrees or radians
* @return this builder
*/
public RootTransformsBuilder rightRotation(float x, float y, float z, boolean isDegrees)
{
return rightRotation(TransformationHelper.quatFromXYZ(x, y, z, isDegrees));
}
/**
* Sets the right rotation of the root transform.
*
* @param postRotation the right rotation
* @return this builder
* @throws NullPointerException if {@code rightRotation} is {@code null}
*/
public RootTransformsBuilder postRotation(Quaternionf postRotation)
{
return rightRotation(postRotation);
}
/**
* Sets the right rotation of the root transform.
*
* @param x x rotation
* @param y y rotation
* @param z z rotation
* @param isDegrees whether the rotation is in degrees or radians
* @return this builder
*/
public RootTransformsBuilder postRotation(float x, float y, float z, boolean isDegrees)
{
return postRotation(TransformationHelper.quatFromXYZ(x, y, z, isDegrees));
}
/**
* Sets the scale of the root transform.
*
* @param scale the scale
* @return this builder
*/
public RootTransformsBuilder scale(float scale)
{
return scale(new Vector3f(scale, scale, scale));
}
/**
* Sets the scale of the root transform.
*
* @param xScale x scale
* @param yScale y scale
* @param zScale z scale
* @return this builder
*/
public RootTransformsBuilder scale(float xScale, float yScale, float zScale)
{
return scale(new Vector3f(xScale, yScale, zScale));
}
/**
* Sets the scale of the root transform.
*
* @param scale the scale vector
* @return this builder
* @throws NullPointerException if {@code scale} is {@code null}
*/
public RootTransformsBuilder scale(Vector3f scale)
{
this.scale = Preconditions.checkNotNull(scale, "Scale must not be null");
return this;
}
/**
* Sets the root transform.
*
* @param transformation the transformation to use
* @return this builder
* @throws NullPointerException if {@code transformation} is {@code null}
*/
public RootTransformsBuilder transform(Transformation transformation)
{
Preconditions.checkNotNull(transformation, "Transformation must not be null");
this.translation = transformation.getTranslation();
this.leftRotation = transformation.getLeftRotation();
this.rightRotation = transformation.getRightRotation();
this.scale = transformation.getScale();
return this;
}
/**
* Sets the origin of the root transform.
*
* @param origin the origin vector
* @return this builder
* @throws NullPointerException if {@code origin} is {@code null}
*/
public RootTransformsBuilder origin(Vector3f origin)
{
this.originVec = Preconditions.checkNotNull(origin, "Origin must not be null");
this.origin = null;
return this;
}
/**
* Sets the origin of the root transform.
*
* @param origin the origin name
* @return this builder
* @throws NullPointerException if {@code origin} is {@code null}
* @throws IllegalArgumentException if {@code origin} is not {@code center}, {@code corner} or {@code opposing-corner}
*/
public RootTransformsBuilder origin(TransformationHelper.TransformOrigin origin)
{
this.origin = Preconditions.checkNotNull(origin, "Origin must not be null");
this.originVec = null;
return this;
}
/**
* Finish configuring the parent builder
* @return the parent block model builder
*/
public ModelBuilder<T> end()
{
return ModelBuilder.this;
}
public JsonObject toJson() {
// Write the transform to an object
JsonObject transform = new JsonObject();
if (!translation.equals(0, 0, 0))
{
transform.add("translation", writeVec3(translation));
}
if (!scale.equals(ONE))
{
transform.add("scale", writeVec3(scale));
}
if (!leftRotation.equals(0, 0, 0, 1))
{
transform.add("rotation", writeQuaternion(leftRotation));
}
if (!rightRotation.equals(0, 0, 0, 1))
{
transform.add("post_rotation", writeQuaternion(rightRotation));
}
if (origin != null)
{
transform.addProperty("origin", origin.getSerializedName());
}
else if (originVec != null && !originVec.equals(0, 0, 0))
{
transform.add("origin", writeVec3(originVec));
}
return transform;
}
private static JsonArray writeVec3(Vector3f vector)
{
JsonArray array = new JsonArray();
array.add(vector.x());
array.add(vector.y());
array.add(vector.z());
return array;
}
private static JsonArray writeQuaternion(Quaternionf quaternion)
{
JsonArray array = new JsonArray();
array.add(quaternion.x());
array.add(quaternion.y());
array.add(quaternion.z());
array.add(quaternion.w());
return array;
}
}
}
@@ -18,7 +18,9 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.minecraft.util.Mth;
import net.minecraftforge.client.model.generators.BlockModelBuilder.RootTransformBuilder.TransformOrigin;
import net.minecraft.util.StringRepresentable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Quaternionfc;
@@ -349,4 +351,49 @@ public final class TransformationHelper
else throw new JsonParseException("Rotation: expected array or object, got: " + e);
}
}
public enum TransformOrigin implements StringRepresentable
{
CENTER(new Vector3f(.5f, .5f, .5f), "center"),
CORNER(new Vector3f(), "corner"),
OPPOSING_CORNER(new Vector3f(1, 1, 1), "opposing-corner");
private final Vector3f vec;
private final String name;
TransformOrigin(Vector3f vec, String name)
{
this.vec = vec;
this.name = name;
}
public Vector3f getVector()
{
return vec;
}
@Override
@NotNull
public String getSerializedName()
{
return name;
}
public static @Nullable TransformOrigin fromString(String originName)
{
if (CENTER.getSerializedName().equals(originName))
{
return CENTER;
}
if (CORNER.getSerializedName().equals(originName))
{
return CORNER;
}
if (OPPOSING_CORNER.getSerializedName().equals(originName))
{
return OPPOSING_CORNER;
}
return null;
}
}
}