Model system fixes:

- Add "origin" key to determine what origin to use. This will default to the existing (broken) behaviour to avoid breaking changes.
- Fix interaction of transforms with variant transforms (transformation ordering).
- Fix OBJ loader ignoring "diffuseLighting" attribute from the json.
  This attribute will continue not being used if "ambientToFullbright" is on (default) to avoid breaking changes.
This commit is contained in:
David Quintana committed 2020-05-15 19:25:26 +02:00
1 parent 0f48e851a3
commit c23ea1e733
9 files changed
+286 -24

No files matched your search

@@ -89,37 +89,36 @@ public interface IForgeTransformationMatrix
}
/**
* convert transformation from assuming center-block system to corner-block system
* convert transformation from assuming center-block system to opposing-corner-block system
*/
default TransformationMatrix blockCenterToCorner()
{
TransformationMatrix transform = getTransformaion();
if (transform.isIdentity()) return TransformationMatrix.identity();
Matrix4f ret = transform.getMatrix();
Matrix4f tmp = Matrix4f.makeTranslate(.5f, .5f, .5f);
ret.multiplyBackward(tmp);
tmp.setIdentity();
tmp.setTranslation(-.5f, -.5f, -.5f);
ret.mul(tmp);
return new TransformationMatrix(ret);
return applyOrigin(new Vector3f(.5f, .5f, .5f));
}
/**
* convert transformation from assuming corner-block system to center-block system
* convert transformation from assuming opposing-corner-block system to center-block system
*/
default TransformationMatrix blockCornerToCenter()
{
return applyOrigin(new Vector3f(-.5f, -.5f, -.5f));
}
/**
* Apply this transformation to a different origin.
* Can be used for switching between coordinate systems.
* Parameter is relative to the current origin.
*/
default TransformationMatrix applyOrigin(Vector3f origin) {
TransformationMatrix transform = getTransformaion();
if (transform.isIdentity()) return TransformationMatrix.identity();
Matrix4f ret = transform.getMatrix();
Matrix4f tmp = Matrix4f.makeTranslate(-.5f, -.5f, -.5f);
Matrix4f tmp = Matrix4f.makeTranslate(origin.getX(), origin.getY(), origin.getZ());
ret.multiplyBackward(tmp);
tmp.setIdentity();
tmp.setTranslation(.5f, .5f, .5f);
tmp.setTranslation(-origin.getX(), -origin.getY(), -origin.getZ());
ret.mul(tmp);
return new TransformationMatrix(ret);
}
}
@@ -219,15 +219,15 @@ public class ModelLoaderRegistry
if(transform.has("rotation")) k--;
if(transform.has("scale")) k--;
if(transform.has("post-rotation")) k--;
if(transform.has("origin")) k--;
if(k > 0)
{
throw new JsonParseException("transform: allowed keys: 'thirdperson', 'firstperson', 'gui', 'head', 'matrix', 'translation', 'rotation', 'scale', 'post-rotation'");
throw new JsonParseException("transform: allowed keys: 'thirdperson', 'firstperson', 'gui', 'head', 'matrix', 'translation', 'rotation', 'scale', 'post-rotation', 'origin'");
}
TransformationMatrix base = TransformationMatrix.identity();
if(!transform.entrySet().isEmpty())
{
base = context.deserialize(transform, TransformationMatrix.class);
base = base.blockCenterToCorner();
}
IModelTransform state = new SimpleModelTransform(Maps.immutableEnumMap(transforms), base);
return Optional.of(state);
@@ -249,7 +249,7 @@ public class ModelLoaderRegistry
IModelGeometry<?> customModel = blockModel.customData.getCustomGeometry();
IModelTransform customModelState = blockModel.customData.getCustomModelState();
if (customModelState != null)
modelTransform = new ModelTransformComposition(customModelState, modelTransform, modelTransform.isUvLock());
modelTransform = new ModelTransformComposition(modelTransform, customModelState, modelTransform.isUvLock());
if (customModel != null)
model = customModel.bake(blockModel.customData, modelBakery, spriteGetter, modelTransform, blockModel.getOverrides(modelBakery, otherModel, spriteGetter), modelLocation);
@@ -373,6 +373,10 @@ public class OBJModel implements IMultipartModelGeometry<OBJModel>
uv2 = new Vec2f((fakeLight << 4) / 32767.0f, (fakeLight << 4) / 32767.0f);
builder.setApplyDiffuseLighting(fakeLight == 0);
}
else
{
builder.setApplyDiffuseLighting(diffuseLighting);
}
boolean hasTransform = !transform.isIdentity();
// The incoming transform is referenced on the center of the block, but our coords are referenced on the corner
@@ -20,17 +20,13 @@
package net.minecraftforge.common.model;
import java.lang.reflect.Type;
import java.util.EnumMap;
import java.util.Map;
import com.google.gson.*;
import net.minecraft.client.renderer.*;
import net.minecraft.util.math.MathHelper;
import com.google.common.collect.Maps;
import net.minecraft.client.renderer.model.ItemTransformVec3f;
import net.minecraft.util.Direction;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@@ -132,6 +128,10 @@ public final class TransformationHelper
public static class Deserializer implements JsonDeserializer<TransformationMatrix>
{
private static final Vector3f ORIGIN_CORNER = new Vector3f();
private static final Vector3f ORIGIN_OPPOSING_CORNER = new Vector3f(1f, 1f, 1f);
private static final Vector3f ORIGIN_CENTER = new Vector3f(.5f, .5f, .5f);
@Override
public TransformationMatrix deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
@@ -170,6 +170,9 @@ public final class TransformationHelper
Quaternion leftRot = null;
Vector3f scale = null;
Quaternion rightRot = null;
// TODO: Default origin is opposing corner, due to a mistake.
// This should probably be replaced with center in future versions.
Vector3f origin = ORIGIN_OPPOSING_CORNER;
if (obj.has("translation"))
{
translation = new Vector3f(parseFloatArray(obj.get("translation"), 3, "Translation"));
@@ -205,8 +208,59 @@ public final class TransformationHelper
rightRot = parseRotation(obj.get("post-rotation"));
obj.remove("post-rotation");
}
if (!obj.entrySet().isEmpty()) throw new JsonParseException("TRSR: can either have single 'matrix' key, or a combination of 'translation', 'rotation', 'scale', 'post-rotation'");
return new TransformationMatrix(translation, leftRot, scale, rightRot);
if (obj.has("origin"))
{
origin = parseOrigin(obj);
obj.remove("origin");
}
if (!obj.entrySet().isEmpty()) throw new JsonParseException("TRSR: can either have single 'matrix' key, or a combination of 'translation', 'rotation', 'scale', 'post-rotation', 'origin'");
TransformationMatrix matrix = new TransformationMatrix(translation, leftRot, scale, rightRot);
// Use a different origin if needed.
if (!ORIGIN_CENTER.equals(origin))
{
Vector3f originFromCenter = origin.copy();
originFromCenter.sub(ORIGIN_CENTER);
matrix = matrix.applyOrigin(originFromCenter);
}
return matrix;
}
private static Vector3f parseOrigin(JsonObject obj) {
Vector3f origin = null;
// Two types supported: string ("center", "corner") and array ([x, y, z])
JsonElement originElement = obj.get("origin");
if (originElement.isJsonArray())
{
origin = new Vector3f(parseFloatArray(originElement, 3, "Origin"));
}
else if (originElement.isJsonPrimitive())
{
String originString = originElement.getAsString();
if ("center".equals(originString))
{
origin = ORIGIN_CENTER;
}
else if ("corner".equals(originString))
{
origin = ORIGIN_CORNER;
}
else if ("opposing-corner".equals(originString))
{
// This option can be used to not break models that were written with this origin once the default is changed
origin = ORIGIN_OPPOSING_CORNER;
}
else
{
throw new JsonParseException("Origin: expected one of 'center', 'corner', 'opposing-corner'");
}
}
else
{
throw new JsonParseException("Origin: expected an array or one of 'center', 'corner', 'opposing-corner'");
}
return origin;
}
public static Matrix4f parseMatrix(JsonElement e)