Variant handling

This commit is contained in:
ah-OOG-ah committed 2024-02-29 21:17:46 -05:00
1 parent 02b0d1b728
commit 1e1e95f249
8 files changed
+136 -61

No files matched your search

@@ -5,6 +5,7 @@ import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.models.json.JsonModel;
import com.gtnewhorizons.angelica.models.json.Loader;
import com.gtnewhorizons.angelica.models.json.Variant;
import com.gtnewhorizons.angelica.utils.ObjectPooler;
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
import java.util.List;
@@ -20,14 +21,45 @@ import org.jetbrains.annotations.NotNull;
public class BlockTest extends Block implements QuadProvider {
public static final ResourceLocation modelId = new ResourceLocation("blocks/lectern");
public static final Variant[] modelId = {
new Variant(
new ResourceLocation("blocks/lectern"),
0,
0,
false
),
new Variant(
new ResourceLocation("blocks/lectern"),
180,
0,
false
),
new Variant(
new ResourceLocation("blocks/lectern"),
90,
0,
false
),
new Variant(
new ResourceLocation("blocks/lectern"),
270,
0,
false
),
};
public static final List<Quad> EMPTY = ObjectImmutableList.of();
private static final JsonModel[] model = new JsonModel[4];
public BlockTest() {
super(Material.rock);
}
public static void loadModel() {
for (int i = 0; i < 4; ++i)
model[i] = Loader.getModel(modelId[i]);
}
@Override
public boolean isOpaqueCube() {
return false;
@@ -47,8 +79,9 @@ public class BlockTest extends Block implements QuadProvider {
@Override
public List<Quad> getQuads(IBlockAccess world, BlockPos pos, Block block, int meta, ForgeDirection dir, Random random, ObjectPooler<Quad> quadPool) {
final JsonModel m = Loader.getModel(modelId);
return (m != null) ? m.getQuads(world, pos, block, meta, dir, random, quadPool) : EMPTY;
if (meta < 2 || meta > 5) meta = 2;
return (model[meta - 2] != null) ? model[meta - 2].getQuads(world, pos, block, meta, dir, random, quadPool) : EMPTY;
}
/**
@@ -27,6 +27,7 @@ import net.minecraft.util.ResourceLocation;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.joml.Vector4f;
@@ -44,7 +45,6 @@ public class JsonModel implements QuadProvider {
private final boolean useAO;
private final Map<ModelDisplay.Position, ModelDisplay> display;
private final Map<String, String> textures;
private final Map<String, String> flatTextures = new Object2ObjectOpenHashMap<>();
private List<ModelElement> elements;
private List<Quad> bakedQuadStore = new ObjectArrayList<>();
@@ -56,14 +56,29 @@ public class JsonModel implements QuadProvider {
this.elements = elements;
}
public void bake() {
/**
* Makes a shallow copy of og. This allows you to bake the same model multiple times with various transformations.
*/
JsonModel(JsonModel og) {
this.parentId = og.parentId;
this.useAO = og.useAO;
this.display = og.display;
this.textures = og.textures;
this.elements = og.elements;
}
public void bake(Variant v) {
final Matrix4f vRot = v.getAffineMatrix();
final NdQuadBuilder builder = new NdQuadBuilder();
// Append faces from each element
for (ModelElement e : this.elements) {
final ModelElement.Rotation rot = (e.getRotation() == null) ? ModelElement.Rotation.NOOP : e.getRotation();
final Matrix4f rot = (e.getRotation() == null) ? ModelElement.Rotation.NOOP.getAffineMatrix() : e.getRotation().getAffineMatrix();
//rot.mulLocal(vRot);
final Vector3f from = e.getFrom();
final Vector3f to = e.getTo();
@@ -73,7 +88,8 @@ public class JsonModel implements QuadProvider {
// Assign vertexes
for (int i = 0; i < 4; ++i) {
final Vector3f vert = rot.applyTo(NdQuadBuilder.mapSideToVertex(from, to, i, f.getName()));
final Vector3f vert = rot.transformPosition(NdQuadBuilder.mapSideToVertex(from, to, i, f.getName()));
vRot.transformPosition(vert);
builder.pos(i, vert.x, vert.y, vert.z);
}
@@ -263,7 +279,7 @@ public class JsonModel implements QuadProvider {
final Vector3f origin = loadVec3(json, "origin").div(16);
final Axis axis = Axis.fromName(loadStr(json, "axis"));
final float angle = -loadFloat(json, "angle");
final float angle = loadFloat(json, "angle");
final boolean rescale = loadBool(json, "rescale", false);
return new ModelElement.Rotation(origin, axis, angle, rescale);
@@ -3,6 +3,7 @@ package com.gtnewhorizons.angelica.models.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.gtnewhorizons.angelica.loading.AngelicaTweaker;
import com.gtnewhorizons.angelica.utils.Callback;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.io.IOException;
@@ -29,11 +30,17 @@ public class Loader {
.registerTypeAdapter(JsonModel.class, new JsonModel.Deserializer()).create();
private static final List<ResourceLocation> unloadedModels = new ObjectArrayList<>();
private static final Map<ResourceLocation, JsonModel> loadedModels = new Object2ObjectOpenHashMap<>();
private static final List<ResourceLocation> modelsToBake = new ObjectArrayList<>();
private static final Map<Variant, JsonModel> modelsToBake = new Object2ObjectOpenHashMap<>();
private static final List<Callback> postBakeCallbacks = new ObjectArrayList<>();
public static void registerModel(ResourceLocation loc) {
unloadedModels.add(loc);
modelsToBake.add(loc);
/**
* Pass the variant you want to load, and if needed a callback which puts the models in a useful place, e.g. saving
* them to a static field after baking.
*/
public static void registerModel(Variant loc, Callback loader) {
unloadedModels.add(loc.getModel());
modelsToBake.put(loc, null);
postBakeCallbacks.add(loader);
}
public static void loadModels() {
@@ -64,19 +71,24 @@ public class Loader {
public static void bakeModels() {
for (ResourceLocation l : modelsToBake) {
for (Map.Entry<Variant, JsonModel> l : modelsToBake.entrySet()) {
final JsonModel dough = loadedModels.get(l);
final JsonModel dough = new JsonModel(loadedModels.get(l.getKey().getModel()));
// Resolve the parent chain
dough.resolveParents(loadedModels::get);
// Bake
dough.bake();
dough.bake(l.getKey());
l.setValue(dough);
}
for (Callback c : postBakeCallbacks)
c.run();
}
public static JsonModel getModel(ResourceLocation loc) {
return loadedModels.get(loc);
public static JsonModel getModel(Variant loc) {
return modelsToBake.get(loc);
}
}
@@ -5,13 +5,10 @@ import java.util.List;
import lombok.Getter;
import net.minecraftforge.common.util.ForgeDirection;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix3d;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.joml.Vector4f;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
public class ModelElement {
@Getter
@@ -80,29 +77,20 @@ public class ModelElement {
this.rescale = rescale;
}
Vector3f applyTo(final Vector3f in) {
Matrix4f getAffineMatrix() {
final Vector3f ret = in.sub(origin, new Vector3f());
// Subtract origin
final Matrix4f ret = new Matrix4f().translation(-this.origin.x, -this.origin.y, -this.origin.z);
final Matrix3d rotMat = switch (this.axis) {
case X -> new Matrix3d(
1, 0, 0,
0, cos(angle), -sin(angle),
0, sin(angle), cos(angle)
);
case Y -> new Matrix3d(
cos(angle), 0, sin(angle),
0, 1, 0,
-sin(angle), 0, cos(angle)
);
case Z -> new Matrix3d(
cos(angle), -sin(angle), 0,
sin(angle), cos(angle), 0,
0, 0, 1
);
};
// Rotate
switch (this.axis) {
case X -> ret.rotateLocalX(angle);
case Y -> ret.rotateLocalY(angle);
case Z -> ret.rotateLocalZ(angle);
}
return ret.mul(rotMat, new Vector3f()).add(origin);
// Add the origin back in
return ret.translateLocal(this.origin.x, this.origin.y, this.origin.z);
}
}
}
@@ -0,0 +1,29 @@
package com.gtnewhorizons.angelica.models.json;
import lombok.Getter;
import net.minecraft.util.ResourceLocation;
import org.joml.Matrix4f;
import static java.lang.Math.toRadians;
public class Variant {
@Getter
private final ResourceLocation model;
private final float x;
private final float y;
private final boolean uvLock;
public Variant(ResourceLocation model, int x, int y, boolean uvLock) {
this.model = model;
this.x = (float) toRadians(x);
this.y = (float) toRadians(y);
this.uvLock = uvLock;
}
Matrix4f getAffineMatrix() {
return new Matrix4f()
.translation(-.5f, -.5f, -.5f).rotateLocalY(x).rotateLocalX(y).translateLocal(.5f, .5f, .5f);
}
}
@@ -58,7 +58,10 @@ public class ClientProxy extends CommonProxy {
FMLCommonHandler.instance().bus().register(this);
MinecraftForge.EVENT_BUS.register(this);
Loader.registerModel(BlockTest.modelId);
Loader.registerModel(BlockTest.modelId[0], BlockTest::loadModel);
Loader.registerModel(BlockTest.modelId[1], () -> {});
Loader.registerModel(BlockTest.modelId[2], () -> {});
Loader.registerModel(BlockTest.modelId[3], () -> {});
}
@@ -0,0 +1,13 @@
package com.gtnewhorizons.angelica.utils;
import java.util.concurrent.Callable;
/**
* Both {@link Runnable} and {@link Callable} are meant for off-thread/concurrent execution. This is just a callback, no
* more, no less.
*/
@FunctionalInterface
public interface Callback {
void run();
}