mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-28 07:35:51 +00:00
Split the quads from the JSON metadata
This commit is contained in:
1 parent
01c902b6b9
commit
e7c62bc0ed
2 files changed
+94
-44
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package com.gtnewhorizon.gtnhlib.client.model;
|
||||
|
||||
import static net.minecraftforge.common.util.ForgeDirection.DOWN;
|
||||
import static net.minecraftforge.common.util.ForgeDirection.EAST;
|
||||
import static net.minecraftforge.common.util.ForgeDirection.NORTH;
|
||||
import static net.minecraftforge.common.util.ForgeDirection.SOUTH;
|
||||
import static net.minecraftforge.common.util.ForgeDirection.UNKNOWN;
|
||||
import static net.minecraftforge.common.util.ForgeDirection.UP;
|
||||
import static net.minecraftforge.common.util.ForgeDirection.WEST;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadView;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public record PileOfQuads(SidedQuadStore sidedQuadStore) implements BakedModel {
|
||||
|
||||
public PileOfQuads(Map<ForgeDirection, ArrayList<QuadView>> sidedQuadStore) {
|
||||
this(new SidedQuadStore(sidedQuadStore));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QuadView> getQuads(@Nullable IBlockAccess world, int x, int y, int z, Block block, int meta, ForgeDirection dir, Random random, int color, @Nullable Supplier<QuadView> quadPool) {
|
||||
return sidedQuadStore.getQuads(dir);
|
||||
}
|
||||
|
||||
public static class SidedQuadStore {
|
||||
private static final ObjectImmutableList<QuadView> EMPTY = ObjectImmutableList.of();
|
||||
|
||||
private final ObjectImmutableList<QuadView> up;
|
||||
private final ObjectImmutableList<QuadView> down;
|
||||
private final ObjectImmutableList<QuadView> north;
|
||||
private final ObjectImmutableList<QuadView> south;
|
||||
private final ObjectImmutableList<QuadView> west;
|
||||
private final ObjectImmutableList<QuadView> east;
|
||||
private final ObjectImmutableList<QuadView> unknown;
|
||||
|
||||
public SidedQuadStore(Map<ForgeDirection, ArrayList<QuadView>> sidedQuadStore) {
|
||||
up = lockList(sidedQuadStore.get(UP));
|
||||
down = lockList(sidedQuadStore.get(DOWN));
|
||||
north = lockList(sidedQuadStore.get(NORTH));
|
||||
south = lockList(sidedQuadStore.get(SOUTH));
|
||||
west = lockList(sidedQuadStore.get(WEST));
|
||||
east = lockList(sidedQuadStore.get(EAST));
|
||||
unknown = lockList(sidedQuadStore.get(UNKNOWN));
|
||||
}
|
||||
|
||||
public List<QuadView> getQuads(ForgeDirection dir) {
|
||||
return switch (dir) {
|
||||
case UP -> up;
|
||||
case DOWN -> down;
|
||||
case NORTH -> north;
|
||||
case SOUTH -> south;
|
||||
case WEST -> west;
|
||||
case EAST -> east;
|
||||
case UNKNOWN -> unknown;
|
||||
};
|
||||
}
|
||||
|
||||
private ObjectImmutableList<QuadView> lockList(List<QuadView> list) {
|
||||
if (list.isEmpty()) return EMPTY;
|
||||
return new ObjectImmutableList<>(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,32 @@
|
||||
package com.gtnewhorizon.gtnhlib.client.model.json;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.client.renderer.util.DirectionUtil.ALL_DIRECTIONS;
|
||||
import static java.lang.Math.max;
|
||||
import static java.lang.Math.min;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.model.BakeData;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.BakedModel;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.PileOfQuads;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.UnbakedModel;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.impl.NdQuadBuilder;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.Quad;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadBuilder;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadView;
|
||||
import it.unimi.dsi.fastutil.Function;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import lombok.Getter;
|
||||
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;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.client.model.JSONVariant;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.impl.NdQuadBuilder;
|
||||
import com.gtnewhorizon.gtnhlib.client.model.BakedModel;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.Quad;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadBuilder;
|
||||
import com.gtnewhorizon.gtnhlib.client.renderer.quad.QuadView;
|
||||
|
||||
import it.unimi.dsi.fastutil.Function;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
|
||||
import lombok.Getter;
|
||||
|
||||
public class JSONModel implements BakedModel {
|
||||
public class JSONModel implements UnbakedModel {
|
||||
|
||||
@Nullable
|
||||
private final ResourceLocation parentId;
|
||||
@@ -45,9 +38,6 @@ public class JSONModel implements BakedModel {
|
||||
private final Map<ModelDisplay.Position, ModelDisplay> display;
|
||||
private final Map<String, String> textures;
|
||||
private List<ModelElement> elements;
|
||||
private List<QuadView> allQuadStore = new ObjectArrayList<>();
|
||||
private final Map<ForgeDirection, List<QuadView>> sidedQuadStore = new Object2ObjectOpenHashMap<>();
|
||||
private static final List<QuadView> EMPTY = ObjectImmutableList.of();
|
||||
|
||||
public JSONModel(@Nullable ResourceLocation parentId, boolean useAO,
|
||||
Map<ModelDisplay.Position, ModelDisplay> display, Map<String, String> textures,
|
||||
@@ -72,10 +62,12 @@ public class JSONModel implements BakedModel {
|
||||
this.elements = og.elements;
|
||||
}
|
||||
|
||||
public void bake(JSONVariant v) {
|
||||
@Override
|
||||
public BakedModel bake(BakeData data) {
|
||||
|
||||
final Matrix4f vRot = v.getAffineMatrix();
|
||||
final NdQuadBuilder builder = new NdQuadBuilder();
|
||||
final Matrix4f vRot = data.getAffineMatrix();
|
||||
final var builder = new NdQuadBuilder();
|
||||
final var sidedQuadStore = new HashMap<ForgeDirection, ArrayList<QuadView>>(7);
|
||||
|
||||
// Append faces from each element
|
||||
for (ModelElement e : this.elements) {
|
||||
@@ -146,18 +138,12 @@ public class JSONModel implements BakedModel {
|
||||
|
||||
// Bake and add it
|
||||
final QuadView q = builder.build(new Quad());
|
||||
this.allQuadStore.add(q);
|
||||
this.sidedQuadStore.computeIfAbsent(q.getCullFace(), o -> new ObjectArrayList<>()).add(q);
|
||||
sidedQuadStore.computeIfAbsent(q.getCullFace(), d -> new ArrayList<>()).add(q);
|
||||
}
|
||||
}
|
||||
|
||||
// Lock the lists.
|
||||
this.allQuadStore = new ObjectImmutableList<>(this.allQuadStore);
|
||||
for (ForgeDirection f : ALL_DIRECTIONS) {
|
||||
|
||||
List<QuadView> l = this.sidedQuadStore.computeIfAbsent(f, o -> EMPTY);
|
||||
if (!l.isEmpty()) this.sidedQuadStore.put(f, new ObjectImmutableList<>(l));
|
||||
}
|
||||
// Add them to the model
|
||||
return new PileOfQuads(sidedQuadStore);
|
||||
}
|
||||
|
||||
public List<ResourceLocation> getParents() {
|
||||
@@ -165,13 +151,6 @@ public class JSONModel implements BakedModel {
|
||||
return Arrays.asList(parentId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QuadView> getQuads(IBlockAccess world, int x, int y, int z, Block block, int meta, ForgeDirection dir,
|
||||
Random random, int color, Supplier<QuadView> quadPool) {
|
||||
|
||||
return this.sidedQuadStore.getOrDefault(dir, EMPTY);
|
||||
}
|
||||
|
||||
public void resolveParents(Function<ResourceLocation, JSONModel> modelLoader) {
|
||||
|
||||
if (this.parentId != null && this.parent == null) {
|
||||
@@ -213,5 +192,4 @@ public class JSONModel implements BakedModel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user