Reduce memory usage of model transforms (#4753)

This commit is contained in:
Ben Staddon authored and LexManos committed 2018-04-01 00:24:37 -07:00
1 parent fb61505d35
commit 0b5a6a3b03
12 files changed
+183 -99

No files matched your search

@@ -20,6 +20,9 @@
package net.minecraftforge.common.model;
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.vecmath.AxisAngle4f;
@@ -32,9 +35,11 @@ import javax.vecmath.Tuple4f;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4f;
import net.minecraft.client.renderer.block.model.ItemTransformVec3f;
import net.minecraft.client.renderer.block.model.ModelRotation;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.Vec3i;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -42,7 +47,6 @@ import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import com.google.common.base.MoreObjects;
import java.util.Optional;
import com.google.common.collect.Maps;
/*
@@ -67,7 +71,7 @@ public final class TRSRTransformation implements IModelState, ITransformation
private Vector3f scale;
private Quat4f rightRot;
public TRSRTransformation(Matrix4f matrix)
public TRSRTransformation(@Nullable Matrix4f matrix)
{
if(matrix == null)
{
@@ -89,38 +93,68 @@ public final class TRSRTransformation implements IModelState, ITransformation
full = true;
}
@Deprecated
/** @deprecated use {@link #from(ItemTransformVec3f)} */
@Deprecated // TODO: remove / make private
@SideOnly(Side.CLIENT)
public TRSRTransformation(net.minecraft.client.renderer.block.model.ItemTransformVec3f transform)
public TRSRTransformation(ItemTransformVec3f transform)
{
this(toVecmath(transform.translation), quatFromXYZDegrees(toVecmath(transform.rotation)), toVecmath(transform.scale), null);
}
/** @deprecated use {@link #from(ModelRotation)} */
@Deprecated // TODO: remove
@SideOnly(Side.CLIENT)
public TRSRTransformation(ModelRotation rotation)
{
this(rotation.getMatrix());
}
/** @deprecated use {@link #from(EnumFacing)} */
@Deprecated // TODO: remove
@SideOnly(Side.CLIENT)
public TRSRTransformation(EnumFacing facing)
{
this(getMatrix(facing));
}
@Deprecated
@SideOnly(Side.CLIENT)
public static TRSRTransformation from(ItemTransformVec3f transform)
{
return transform.equals(ItemTransformVec3f.DEFAULT) ? identity : new TRSRTransformation(transform);
}
@SideOnly(Side.CLIENT)
public static TRSRTransformation from(ModelRotation rotation)
{
return Cache.get(rotation);
}
@SideOnly(Side.CLIENT)
public static TRSRTransformation from(EnumFacing facing)
{
return Cache.get(getRotation(facing));
}
@SideOnly(Side.CLIENT)
public static Matrix4f getMatrix(EnumFacing facing)
{
switch(facing)
return getRotation(facing).getMatrix();
}
@SideOnly(Side.CLIENT)
public static ModelRotation getRotation(EnumFacing facing)
{
switch (facing)
{
case DOWN: return ModelRotation.X90_Y0.getMatrix();
case UP: return ModelRotation.X270_Y0.getMatrix();
case NORTH: return TRSRTransformation.identity.matrix;
case SOUTH: return ModelRotation.X0_Y180.getMatrix();
case WEST: return ModelRotation.X0_Y270.getMatrix();
case EAST: return ModelRotation.X0_Y90.getMatrix();
default: return new Matrix4f();
case DOWN: return ModelRotation.X90_Y0;
case UP: return ModelRotation.X270_Y0;
case NORTH: return ModelRotation.X0_Y0;
case SOUTH: return ModelRotation.X0_Y180;
case WEST: return ModelRotation.X0_Y270;
case EAST: return ModelRotation.X0_Y90;
}
throw new IllegalArgumentException(String.valueOf(facing));
}
private static final TRSRTransformation identity;
@@ -140,6 +174,8 @@ public final class TRSRTransformation implements IModelState, ITransformation
public TRSRTransformation compose(TRSRTransformation b)
{
if (this.isIdentity()) return b;
if (b.isIdentity()) return this;
Matrix4f m = getMatrix();
m.mul(b.getMatrix());
return new TRSRTransformation(m);
@@ -147,7 +183,7 @@ public final class TRSRTransformation implements IModelState, ITransformation
public TRSRTransformation inverse()
{
if(this == identity) return this;
if (this.isIdentity()) return this;
Matrix4f m = getMatrix();
m.invert();
return new TRSRTransformation(m);
@@ -523,9 +559,14 @@ public final class TRSRTransformation implements IModelState, ITransformation
*/
@Deprecated
@SideOnly(Side.CLIENT)
public net.minecraft.client.renderer.block.model.ItemTransformVec3f toItemTransform()
public ItemTransformVec3f toItemTransform()
{
return new net.minecraft.client.renderer.block.model.ItemTransformVec3f(toLwjgl(toXYZDegrees(getLeftRot())), toLwjgl(getTranslation()), toLwjgl(getScale()));
return new ItemTransformVec3f(toLwjgl(toXYZDegrees(getLeftRot())), toLwjgl(getTranslation()), toLwjgl(getScale()));
}
public boolean isIdentity()
{
return this.equals(identity);
}
@Override
@@ -625,6 +666,8 @@ public final class TRSRTransformation implements IModelState, ITransformation
*/
public static TRSRTransformation blockCenterToCorner(TRSRTransformation transform)
{
if (transform.isIdentity()) return transform;
Matrix4f ret = new Matrix4f(transform.getMatrix()), tmp = new Matrix4f();
tmp.setIdentity();
tmp.m03 = tmp.m13 = tmp.m23 = .5f;
@@ -639,6 +682,8 @@ public final class TRSRTransformation implements IModelState, ITransformation
*/
public static TRSRTransformation blockCornerToCenter(TRSRTransformation transform)
{
if (transform.isIdentity()) return transform;
Matrix4f ret = new Matrix4f(transform.getMatrix()), tmp = new Matrix4f();
tmp.setIdentity();
tmp.m03 = tmp.m13 = tmp.m23 = -.5f;
@@ -653,7 +698,7 @@ public final class TRSRTransformation implements IModelState, ITransformation
{
final int prime = 31;
int result = 1;
result = prime * result + ((matrix == null) ? 0 : matrix.hashCode());
result = prime * result + Objects.hashCode(matrix);
return result;
}
@@ -664,12 +709,7 @@ public final class TRSRTransformation implements IModelState, ITransformation
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TRSRTransformation other = (TRSRTransformation) obj;
if (matrix == null)
{
if (other.matrix != null) return false;
}
else if (!matrix.equals(other.matrix)) return false;
return true;
return Objects.equals(matrix, other.matrix);
}
@SideOnly(Side.CLIENT)
@@ -806,4 +846,20 @@ public final class TRSRTransformation implements IModelState, ITransformation
return new TRSRTransformation(null, null, new Vector3f(0, 0, 0), null);
}
}
@SideOnly(Side.CLIENT)
private static final class Cache
{
private static final Map<ModelRotation, TRSRTransformation> rotations = new EnumMap<>(ModelRotation.class);
static
{
rotations.put(ModelRotation.X0_Y0, identity());
}
static TRSRTransformation get(ModelRotation rotation)
{
return rotations.computeIfAbsent(rotation, r -> new TRSRTransformation(ForgeHooksClient.getMatrix(r)));
}
}
}