diff --git a/build.gradle b/build.gradle index c7e81a148..3ad7f4c4d 100644 --- a/build.gradle +++ b/build.gradle @@ -98,7 +98,7 @@ mixin { config 'flywheel.backend.mixins.json' config 'flywheel.impl.mixins.json' config 'flywheel.impl.sodium.mixins.json' - config 'flywheel.vanilla.mixins.json' + config 'flywheel.lib.mixins.json' debug.verbose = true debug.export = true diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 0982c6ca9..234e01d9f 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -27,11 +27,11 @@ import com.jozufozu.flywheel.lib.memory.FlwMemoryTracker; import com.jozufozu.flywheel.lib.model.ModelCache; import com.jozufozu.flywheel.lib.model.ModelHolder; import com.jozufozu.flywheel.lib.model.baked.PartialModel; +import com.jozufozu.flywheel.lib.model.part.MeshTree; import com.jozufozu.flywheel.lib.util.LevelAttached; import com.jozufozu.flywheel.lib.util.ShadersModHandler; import com.jozufozu.flywheel.lib.util.StringUtil; import com.jozufozu.flywheel.vanilla.VanillaVisuals; -import com.jozufozu.flywheel.vanilla.model.MeshTreeCache; import net.minecraft.client.Minecraft; import net.minecraft.commands.synchronization.ArgumentTypeInfos; @@ -107,7 +107,7 @@ public class Flywheel { modEventBus.addListener(BackendManagerImpl::onEndClientResourceReload); modEventBus.addListener((EndClientResourceReloadEvent e) -> ModelCache.onEndClientResourceReload(e)); - modEventBus.addListener(MeshTreeCache::onEndClientResourceReload); + modEventBus.addListener(MeshTree::onEndClientResourceReload); modEventBus.addListener(ModelHolder::onEndClientResourceReload); modEventBus.addListener(PartialModel::onModelRegistry); diff --git a/src/main/java/com/jozufozu/flywheel/lib/mixin/ModelPartAccessor.java b/src/main/java/com/jozufozu/flywheel/lib/mixin/ModelPartAccessor.java new file mode 100644 index 000000000..158249f35 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/mixin/ModelPartAccessor.java @@ -0,0 +1,21 @@ +package com.jozufozu.flywheel.lib.mixin; + +import java.util.Map; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; + +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; + +import net.minecraft.client.model.geom.ModelPart; + +@Mixin(ModelPart.class) +public interface ModelPartAccessor { + @Accessor("children") + Map flywheel$children(); + + @Invoker("compile") + void flywheel$compile(PoseStack.Pose pPose, VertexConsumer pVertexConsumer, int pPackedLight, int pPackedOverlay, float pRed, float pGreen, float pBlue, float pAlpha); +} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/VertexMultiConsumerDoubleMixin.java b/src/main/java/com/jozufozu/flywheel/lib/mixin/VertexMultiConsumerDoubleMixin.java similarity index 97% rename from src/main/java/com/jozufozu/flywheel/vanilla/mixin/VertexMultiConsumerDoubleMixin.java rename to src/main/java/com/jozufozu/flywheel/lib/mixin/VertexMultiConsumerDoubleMixin.java index 3bee7eaed..b1ef0ab49 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/VertexMultiConsumerDoubleMixin.java +++ b/src/main/java/com/jozufozu/flywheel/lib/mixin/VertexMultiConsumerDoubleMixin.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.vanilla.mixin; +package com.jozufozu.flywheel.lib.mixin; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/model/RetexturedMesh.java b/src/main/java/com/jozufozu/flywheel/lib/model/RetexturedMesh.java similarity index 94% rename from src/main/java/com/jozufozu/flywheel/vanilla/model/RetexturedMesh.java rename to src/main/java/com/jozufozu/flywheel/lib/model/RetexturedMesh.java index 8a751d0e2..bf8677185 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/model/RetexturedMesh.java +++ b/src/main/java/com/jozufozu/flywheel/lib/model/RetexturedMesh.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.vanilla.model; +package com.jozufozu.flywheel.lib.model; import org.joml.Vector4fc; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/model/RetexturingVertexList.java b/src/main/java/com/jozufozu/flywheel/lib/model/RetexturingVertexList.java similarity index 94% rename from src/main/java/com/jozufozu/flywheel/vanilla/model/RetexturingVertexList.java rename to src/main/java/com/jozufozu/flywheel/lib/model/RetexturingVertexList.java index 7242ae119..011f74233 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/model/RetexturingVertexList.java +++ b/src/main/java/com/jozufozu/flywheel/lib/model/RetexturingVertexList.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.vanilla.model; +package com.jozufozu.flywheel.lib.model; import com.jozufozu.flywheel.api.vertex.MutableVertexList; import com.jozufozu.flywheel.lib.vertex.WrappedVertexList; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/model/InstanceTree.java b/src/main/java/com/jozufozu/flywheel/lib/model/part/InstanceTree.java similarity index 99% rename from src/main/java/com/jozufozu/flywheel/vanilla/model/InstanceTree.java rename to src/main/java/com/jozufozu/flywheel/lib/model/part/InstanceTree.java index ffb50a31a..40486fd98 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/model/InstanceTree.java +++ b/src/main/java/com/jozufozu/flywheel/lib/model/part/InstanceTree.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.vanilla.model; +package com.jozufozu.flywheel.lib.model.part; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/com/jozufozu/flywheel/lib/model/part/MeshTree.java b/src/main/java/com/jozufozu/flywheel/lib/model/part/MeshTree.java new file mode 100644 index 000000000..4abf40a9c --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/model/part/MeshTree.java @@ -0,0 +1,114 @@ +package com.jozufozu.flywheel.lib.model.part; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import com.jozufozu.flywheel.api.event.EndClientResourceReloadEvent; +import com.jozufozu.flywheel.api.model.Mesh; +import com.jozufozu.flywheel.lib.mixin.ModelPartAccessor; +import com.jozufozu.flywheel.lib.model.SimpleMesh; +import com.jozufozu.flywheel.lib.vertex.PosTexNormalVertexView; +import com.mojang.blaze3d.vertex.PoseStack; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.model.geom.EntityModelSet; +import net.minecraft.client.model.geom.ModelLayerLocation; +import net.minecraft.client.model.geom.ModelPart; +import net.minecraft.client.model.geom.PartPose; + +public class MeshTree { + private static final ThreadLocal THREAD_LOCAL_OBJECTS = ThreadLocal.withInitial(ThreadLocalObjects::new); + private static final PoseStack.Pose IDENTITY = new PoseStack().last(); + private static final Map MESH_TREES = new ConcurrentHashMap<>(); + + private final PartPose initialPose; + @Nullable + private final Mesh mesh; + private final Map children; + + private MeshTree(PartPose initialPose, @Nullable Mesh mesh, Map children) { + this.initialPose = initialPose; + this.mesh = mesh; + this.children = children; + } + + public static MeshTree get(ModelLayerLocation key) { + return MESH_TREES.computeIfAbsent(key, MeshTree::convert); + } + + @ApiStatus.Internal + public static void onEndClientResourceReload(EndClientResourceReloadEvent event) { + MESH_TREES.values() + .forEach(MeshTree::delete); + MESH_TREES.clear(); + } + + public PartPose initialPose() { + return initialPose; + } + + @Nullable + public Mesh mesh() { + return mesh; + } + + public Map children() { + return children; + } + + @Nullable + public MeshTree child(String name) { + return children.get(name); + } + + public void delete() { + if (mesh != null) { + mesh.delete(); + } + children.values() + .forEach(MeshTree::delete); + } + + public static MeshTree convert(ModelLayerLocation location) { + EntityModelSet entityModels = Minecraft.getInstance() + .getEntityModels(); + ModelPart modelPart = entityModels.bakeLayer(location); + + return convert(modelPart, THREAD_LOCAL_OBJECTS.get()); + } + + private static MeshTree convert(ModelPart modelPart, ThreadLocalObjects objects) { + var accessor = cast(modelPart); + var childModelParts = accessor.flywheel$children(); + + Map children = new HashMap<>(); + + for (Map.Entry entry : childModelParts.entrySet()) { + children.put(entry.getKey(), convert(entry.getValue(), objects)); + } + + return new MeshTree(modelPart.getInitialPose(), compile(accessor, objects), children); + } + + private static Mesh compile(ModelPartAccessor accessor, ThreadLocalObjects objects) { + var vertexWriter = objects.vertexWriter; + + accessor.flywheel$compile(IDENTITY, vertexWriter, 0, 0, 1.0F, 1.0F, 1.0F, 1.0F); + + var data = vertexWriter.copyDataAndReset(); + + return new SimpleMesh(new PosTexNormalVertexView(), data, "source=ModelPartConverter"); + } + + private static ModelPartAccessor cast(ModelPart cube) { + return (ModelPartAccessor) (Object) cube; + } + + private static class ThreadLocalObjects { + public final VertexWriter vertexWriter = new VertexWriter(); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/AgeableListComponent.java b/src/main/java/com/jozufozu/flywheel/vanilla/AgeableListComponent.java index 39a1dda1c..c7ce63728 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/AgeableListComponent.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/AgeableListComponent.java @@ -1,6 +1,6 @@ package com.jozufozu.flywheel.vanilla; -import com.jozufozu.flywheel.vanilla.model.InstanceTree; +import com.jozufozu.flywheel.lib.model.part.InstanceTree; import com.mojang.blaze3d.vertex.PoseStack; public abstract class AgeableListComponent { diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java index 16a1d07f9..6d9fe2ad8 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestVisual.java @@ -15,12 +15,12 @@ import com.jozufozu.flywheel.lib.instance.OrientedInstance; import com.jozufozu.flywheel.lib.instance.TransformedInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.ModelCache; +import com.jozufozu.flywheel.lib.model.RetexturedMesh; import com.jozufozu.flywheel.lib.model.SingleMeshModel; +import com.jozufozu.flywheel.lib.model.part.MeshTree; import com.jozufozu.flywheel.lib.util.Pair; import com.jozufozu.flywheel.lib.visual.AbstractBlockEntityVisual; import com.jozufozu.flywheel.lib.visual.SimpleDynamicVisual; -import com.jozufozu.flywheel.vanilla.model.MeshTreeCache; -import com.jozufozu.flywheel.vanilla.model.RetexturedMesh; import it.unimi.dsi.fastutil.floats.Float2FloatFunction; import net.minecraft.client.model.geom.ModelLayerLocation; @@ -173,7 +173,7 @@ public class ChestVisual extends Abstrac } public static Model chestModel(Pair key, String child) { - var mesh = MeshTreeCache.get(LAYER_LOCATIONS.get(key.first())) + var mesh = MeshTree.get(LAYER_LOCATIONS.get(key.first())) .child(child) .mesh(); var sprite = key.second() diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/QuadrupedComponent.java b/src/main/java/com/jozufozu/flywheel/vanilla/QuadrupedComponent.java index 7a280ff5c..1cc9ac560 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/QuadrupedComponent.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/QuadrupedComponent.java @@ -4,8 +4,8 @@ import java.util.List; import com.jozufozu.flywheel.api.instance.InstancerProvider; import com.jozufozu.flywheel.api.material.Material; -import com.jozufozu.flywheel.vanilla.model.InstanceTree; -import com.jozufozu.flywheel.vanilla.model.MeshTreeCache; +import com.jozufozu.flywheel.lib.model.part.InstanceTree; +import com.jozufozu.flywheel.lib.model.part.MeshTree; import net.minecraft.client.model.geom.ModelLayerLocation; import net.minecraft.util.Mth; @@ -23,7 +23,7 @@ public class QuadrupedComponent extends AgeableListComponent { public QuadrupedComponent(InstancerProvider instancerProvider, ModelLayerLocation layer, Material material, Config config) { super(config); - var meshTree = MeshTreeCache.get(layer); + var meshTree = MeshTree.get(layer); this.root = InstanceTree.create(instancerProvider, meshTree, material); diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java index b00635597..3672c5a4a 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ShulkerBoxVisual.java @@ -11,12 +11,12 @@ import com.jozufozu.flywheel.lib.instance.InstanceTypes; import com.jozufozu.flywheel.lib.instance.TransformedInstance; import com.jozufozu.flywheel.lib.material.Materials; import com.jozufozu.flywheel.lib.model.ModelCache; +import com.jozufozu.flywheel.lib.model.RetexturedMesh; import com.jozufozu.flywheel.lib.model.SingleMeshModel; +import com.jozufozu.flywheel.lib.model.part.MeshTree; import com.jozufozu.flywheel.lib.transform.TransformStack; import com.jozufozu.flywheel.lib.visual.AbstractBlockEntityVisual; import com.jozufozu.flywheel.lib.visual.SimpleDynamicVisual; -import com.jozufozu.flywheel.vanilla.model.MeshTreeCache; -import com.jozufozu.flywheel.vanilla.model.RetexturedMesh; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Axis; @@ -138,7 +138,7 @@ public class ShulkerBoxVisual extends AbstractBlockEntityVisual vanillin$visibleFaces(); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/CubeDeformationAccessor.java b/src/main/java/com/jozufozu/flywheel/vanilla/mixin/CubeDeformationAccessor.java deleted file mode 100644 index 775324fde..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/CubeDeformationAccessor.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.jozufozu.flywheel.vanilla.mixin; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.model.geom.builders.CubeDeformation; - -@Mixin(CubeDeformation.class) -public interface CubeDeformationAccessor { - @Accessor("growX") - float vanillin$growX(); - - @Accessor("growY") - float vanillin$growY(); - - @Accessor("growZ") - float vanillin$growZ(); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/EntityModelSetAccessor.java b/src/main/java/com/jozufozu/flywheel/vanilla/mixin/EntityModelSetAccessor.java deleted file mode 100644 index 9010f084a..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/EntityModelSetAccessor.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.jozufozu.flywheel.vanilla.mixin; - -import java.util.Map; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.model.geom.EntityModelSet; -import net.minecraft.client.model.geom.ModelLayerLocation; -import net.minecraft.client.model.geom.builders.LayerDefinition; - -@Mixin(EntityModelSet.class) -public interface EntityModelSetAccessor { - @Accessor("roots") - Map vanillin$roots(); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/LayerDefinitionAccessor.java b/src/main/java/com/jozufozu/flywheel/vanilla/mixin/LayerDefinitionAccessor.java deleted file mode 100644 index 367266270..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/LayerDefinitionAccessor.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.jozufozu.flywheel.vanilla.mixin; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.model.geom.builders.LayerDefinition; -import net.minecraft.client.model.geom.builders.MaterialDefinition; -import net.minecraft.client.model.geom.builders.MeshDefinition; - -@Mixin(LayerDefinition.class) -public interface LayerDefinitionAccessor { - @Accessor("mesh") - MeshDefinition vanillin$mesh(); - - @Accessor("material") - MaterialDefinition vanillin$material(); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/MaterialDefinitionAccessor.java b/src/main/java/com/jozufozu/flywheel/vanilla/mixin/MaterialDefinitionAccessor.java deleted file mode 100644 index e0140a777..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/MaterialDefinitionAccessor.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jozufozu.flywheel.vanilla.mixin; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.model.geom.builders.MaterialDefinition; - -@Mixin(MaterialDefinition.class) -public interface MaterialDefinitionAccessor { - @Accessor("xTexSize") - int vanillin$xTexSize(); - - @Accessor("yTexSize") - int vanillin$yTexSize(); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/PartDefinitionAccessor.java b/src/main/java/com/jozufozu/flywheel/vanilla/mixin/PartDefinitionAccessor.java deleted file mode 100644 index 72ab1469d..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/mixin/PartDefinitionAccessor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.jozufozu.flywheel.vanilla.mixin; - -import java.util.List; -import java.util.Map; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import net.minecraft.client.model.geom.PartPose; -import net.minecraft.client.model.geom.builders.CubeDefinition; -import net.minecraft.client.model.geom.builders.PartDefinition; - -@Mixin(PartDefinition.class) -public interface PartDefinitionAccessor { - @Accessor("cubes") - List vanillin$cubes(); - - @Accessor("partPose") - PartPose vanillin$partPose(); - - @Accessor("children") - Map vanillin$children(); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/model/MeshTree.java b/src/main/java/com/jozufozu/flywheel/vanilla/model/MeshTree.java deleted file mode 100644 index 3968b881f..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/model/MeshTree.java +++ /dev/null @@ -1,281 +0,0 @@ -package com.jozufozu.flywheel.vanilla.model; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import com.jozufozu.flywheel.api.model.Mesh; -import com.jozufozu.flywheel.lib.memory.MemoryBlock; -import com.jozufozu.flywheel.lib.model.SimpleMesh; -import com.jozufozu.flywheel.lib.vertex.PosTexNormalVertexView; -import com.jozufozu.flywheel.vanilla.mixin.CubeDefinitionAccessor; -import com.jozufozu.flywheel.vanilla.mixin.CubeDeformationAccessor; -import com.jozufozu.flywheel.vanilla.mixin.EntityModelSetAccessor; -import com.jozufozu.flywheel.vanilla.mixin.LayerDefinitionAccessor; -import com.jozufozu.flywheel.vanilla.mixin.MaterialDefinitionAccessor; -import com.jozufozu.flywheel.vanilla.mixin.PartDefinitionAccessor; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.model.geom.ModelLayerLocation; -import net.minecraft.client.model.geom.PartPose; -import net.minecraft.client.model.geom.builders.CubeDefinition; -import net.minecraft.client.model.geom.builders.LayerDefinition; -import net.minecraft.client.model.geom.builders.PartDefinition; -import net.minecraft.core.Direction; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; - -public class MeshTree { - private final PartPose initialPose; - @Nullable - private final Mesh mesh; - private final Map children; - - private MeshTree(PartPose initialPose, @Nullable Mesh mesh, Map children) { - this.initialPose = initialPose; - this.mesh = mesh; - this.children = children; - } - - public PartPose initialPose() { - return initialPose; - } - - @Nullable - public Mesh mesh() { - return mesh; - } - - public Map children() { - return children; - } - - @Nullable - public MeshTree child(String name) { - return children.get(name); - } - - public void delete() { - if (mesh != null) { - mesh.delete(); - } - children.values() - .forEach(MeshTree::delete); - } - - public static MeshTree convert(ModelLayerLocation location) { - var entityModels = (EntityModelSetAccessor) Minecraft.getInstance() - .getEntityModels(); - - return convert(entityModels.vanillin$roots() - .get(location)); - } - - public static MeshTree convert(LayerDefinition layerDefinition) { - var accessor = (LayerDefinitionAccessor) layerDefinition; - var root = accessor.vanillin$mesh() - .getRoot(); - - var material = (MaterialDefinitionAccessor) accessor.vanillin$material(); - - int xTexSize = material.vanillin$xTexSize(); - int yTexSize = material.vanillin$yTexSize(); - - return convert(root, xTexSize, yTexSize); - } - - private static MeshTree convert(PartDefinition part, int xTexSize, int yTexSize) { - var accessor = (PartDefinitionAccessor) part; - - var cubes = accessor.vanillin$cubes(); - var initialPose = accessor.vanillin$partPose(); - var childDefinitions = accessor.vanillin$children(); - - Map children = new HashMap<>(); - - for (Map.Entry entry : childDefinitions.entrySet()) { - children.put(entry.getKey(), convert(entry.getValue(), xTexSize, yTexSize)); - } - - return new MeshTree(initialPose, convertCubes(cubes, xTexSize, yTexSize), children); - } - - private static Mesh convertCubes(List cubes, int xTexSize, int yTexSize) { - if (cubes.isEmpty()) { - return null; - } - var totalVisibleFaces = countVisibleFaces(cubes); - - if (totalVisibleFaces == 0) { - return null; - } - - var totalVertices = totalVisibleFaces * 4; - - var block = MemoryBlock.malloc(totalVertices * PosTexNormalVertexView.STRIDE); - var view = new PosTexNormalVertexView(); - - view.ptr(block.ptr()); - view.vertexCount(totalVertices); - - int vertexIndex = 0; - for (CubeDefinition cube : cubes) { - CubeDefinitionAccessor accessor = cast(cube); - - var origin = accessor.vanillin$origin(); - var dimensions = accessor.vanillin$dimensions(); - var grow = (CubeDeformationAccessor) accessor.vanillin$grow(); - var pMirror = accessor.vanillin$mirror(); - var texCoord = accessor.vanillin$texCoord(); - var texScale = accessor.vanillin$texScale(); - - var visibleFaces = accessor.vanillin$visibleFaces(); - - float pOriginX = origin.x(); - float pOriginY = origin.y(); - float pOriginZ = origin.z(); - - float pDimensionX = dimensions.x(); - float pDimensionY = dimensions.y(); - float pDimensionZ = dimensions.z(); - - float pGrowX = grow.vanillin$growX(); - float pGrowY = grow.vanillin$growY(); - float pGrowZ = grow.vanillin$growZ(); - - float pTexCoordU = texCoord.u(); - float pTexCoordV = texCoord.v(); - - float pTexScaleU = xTexSize * texScale.u(); - float pTexScaleV = yTexSize * texScale.v(); - - float f = pOriginX + pDimensionX; - float f1 = pOriginY + pDimensionY; - float f2 = pOriginZ + pDimensionZ; - pOriginX -= pGrowX; - pOriginY -= pGrowY; - pOriginZ -= pGrowZ; - f += pGrowX; - f1 += pGrowY; - f2 += pGrowZ; - if (pMirror) { - float f3 = f; - f = pOriginX; - pOriginX = f3; - } - - Vertex modelpart$vertex7 = new Vertex(pOriginX, pOriginY, pOriginZ, 0.0F, 0.0F); - Vertex modelpart$vertex = new Vertex(f, pOriginY, pOriginZ, 0.0F, 8.0F); - Vertex modelpart$vertex1 = new Vertex(f, f1, pOriginZ, 8.0F, 8.0F); - Vertex modelpart$vertex2 = new Vertex(pOriginX, f1, pOriginZ, 8.0F, 0.0F); - Vertex modelpart$vertex3 = new Vertex(pOriginX, pOriginY, f2, 0.0F, 0.0F); - Vertex modelpart$vertex4 = new Vertex(f, pOriginY, f2, 0.0F, 8.0F); - Vertex modelpart$vertex5 = new Vertex(f, f1, f2, 8.0F, 8.0F); - Vertex modelpart$vertex6 = new Vertex(pOriginX, f1, f2, 8.0F, 0.0F); - float f4 = pTexCoordU; - float f5 = pTexCoordU + pDimensionZ; - float f6 = pTexCoordU + pDimensionZ + pDimensionX; - float f7 = pTexCoordU + pDimensionZ + pDimensionX + pDimensionX; - float f8 = pTexCoordU + pDimensionZ + pDimensionX + pDimensionZ; - float f9 = pTexCoordU + pDimensionZ + pDimensionX + pDimensionZ + pDimensionX; - float f10 = pTexCoordV; - float f11 = pTexCoordV + pDimensionZ; - float f12 = pTexCoordV + pDimensionZ + pDimensionY; - if (visibleFaces.contains(Direction.DOWN)) { - addFace(view, vertexIndex, new Vertex[]{modelpart$vertex4, modelpart$vertex3, modelpart$vertex7, modelpart$vertex}, f5, f10, f6, f11, pTexScaleU, pTexScaleV, pMirror, Direction.DOWN); - vertexIndex += 4; - } - - if (visibleFaces.contains(Direction.UP)) { - addFace(view, vertexIndex, new Vertex[]{modelpart$vertex1, modelpart$vertex2, modelpart$vertex6, modelpart$vertex5}, f6, f11, f7, f10, pTexScaleU, pTexScaleV, pMirror, Direction.UP); - vertexIndex += 4; - } - - if (visibleFaces.contains(Direction.WEST)) { - addFace(view, vertexIndex, new Vertex[]{modelpart$vertex7, modelpart$vertex3, modelpart$vertex6, modelpart$vertex2}, f4, f11, f5, f12, pTexScaleU, pTexScaleV, pMirror, Direction.WEST); - vertexIndex += 4; - } - - if (visibleFaces.contains(Direction.NORTH)) { - addFace(view, vertexIndex, new Vertex[]{modelpart$vertex, modelpart$vertex7, modelpart$vertex2, modelpart$vertex1}, f5, f11, f6, f12, pTexScaleU, pTexScaleV, pMirror, Direction.NORTH); - vertexIndex += 4; - } - - if (visibleFaces.contains(Direction.EAST)) { - addFace(view, vertexIndex, new Vertex[]{modelpart$vertex4, modelpart$vertex, modelpart$vertex1, modelpart$vertex5}, f6, f11, f8, f12, pTexScaleU, pTexScaleV, pMirror, Direction.EAST); - vertexIndex += 4; - } - - if (visibleFaces.contains(Direction.SOUTH)) { - addFace(view, vertexIndex, new Vertex[]{modelpart$vertex3, modelpart$vertex4, modelpart$vertex5, modelpart$vertex6}, f8, f11, f9, f12, pTexScaleU, pTexScaleV, pMirror, Direction.SOUTH); - vertexIndex += 4; - } - } - - return new SimpleMesh(view, block); - } - - private static void addFace(PosTexNormalVertexView view, int index, Vertex[] pVertices, float pU1, float pV1, float pU2, float pV2, float pTextureWidth, float pTextureHeight, boolean pMirror, Direction pDirection) { - float f = 0.0F / pTextureWidth; - float f1 = 0.0F / pTextureHeight; - pVertices[0] = pVertices[0].remap(pU2 / pTextureWidth - f, pV1 / pTextureHeight + f1); - pVertices[1] = pVertices[1].remap(pU1 / pTextureWidth + f, pV1 / pTextureHeight + f1); - pVertices[2] = pVertices[2].remap(pU1 / pTextureWidth + f, pV2 / pTextureHeight - f1); - pVertices[3] = pVertices[3].remap(pU2 / pTextureWidth - f, pV2 / pTextureHeight - f1); - if (pMirror) { - int i = pVertices.length; - - for (int j = 0; j < i / 2; ++j) { - Vertex modelpart$vertex = pVertices[j]; - pVertices[j] = pVertices[i - 1 - j]; - pVertices[i - 1 - j] = modelpart$vertex; - } - } - - var normal = pDirection.step(); - if (pMirror) { - normal.mul(-1.0F, 1.0F, 1.0F); - } - - int i = index; - for (Vertex modelpart$vertex : pVertices) { - float f3 = modelpart$vertex.x() / 16.0F; - float f4 = modelpart$vertex.y() / 16.0F; - float f5 = modelpart$vertex.z() / 16.0F; - view.x(i, f3); - view.y(i, f4); - view.z(i, f5); - view.u(i, modelpart$vertex.u()); - view.v(i, modelpart$vertex.v()); - view.normalX(i, normal.x()); - view.normalY(i, normal.y()); - view.normalZ(i, normal.z()); - - ++i; - } - } - - private static int countVisibleFaces(List cubes) { - int totalVisibleFaces = 0; - for (CubeDefinition cube : cubes) { - totalVisibleFaces += cast(cube).vanillin$visibleFaces() - .size(); - } - return totalVisibleFaces; - } - - @NotNull - private static CubeDefinitionAccessor cast(CubeDefinition cube) { - return (CubeDefinitionAccessor) (Object) cube; - } - - @OnlyIn(Dist.CLIENT) - record Vertex(float x, float y, float z, float u, float v) { - public Vertex remap(float pU, float pV) { - return new Vertex(x, y, z, pU, pV); - } - } -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/model/MeshTreeCache.java b/src/main/java/com/jozufozu/flywheel/vanilla/model/MeshTreeCache.java deleted file mode 100644 index cb95b3966..000000000 --- a/src/main/java/com/jozufozu/flywheel/vanilla/model/MeshTreeCache.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.jozufozu.flywheel.vanilla.model; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.jetbrains.annotations.ApiStatus; - -import com.jozufozu.flywheel.api.event.EndClientResourceReloadEvent; - -import net.minecraft.client.model.geom.ModelLayerLocation; - -public class MeshTreeCache { - private static final Map MESH_TREES = new ConcurrentHashMap<>(); - - public static MeshTree get(ModelLayerLocation key) { - return MESH_TREES.computeIfAbsent(key, MeshTree::convert); - } - - @ApiStatus.Internal - public static void onEndClientResourceReload(EndClientResourceReloadEvent event) { - MESH_TREES.values() - .forEach(MeshTree::delete); - MESH_TREES.clear(); - } - - private MeshTreeCache() { - } -} diff --git a/src/main/resources/flywheel.lib.mixins.json b/src/main/resources/flywheel.lib.mixins.json new file mode 100644 index 000000000..e256b9b96 --- /dev/null +++ b/src/main/resources/flywheel.lib.mixins.json @@ -0,0 +1,14 @@ +{ + "required" : true, + "minVersion" : "0.8", + "package" : "com.jozufozu.flywheel.lib.mixin", + "compatibilityLevel" : "JAVA_17", + "refmap" : "flywheel.refmap.json", + "client" : [ + "ModelPartAccessor", + "VertexMultiConsumerDoubleMixin" + ], + "injectors" : { + "defaultRequire" : 1 + } +} diff --git a/src/main/resources/flywheel.vanilla.mixins.json b/src/main/resources/flywheel.vanilla.mixins.json deleted file mode 100644 index cf90f7ddf..000000000 --- a/src/main/resources/flywheel.vanilla.mixins.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "required" : true, - "minVersion" : "0.8", - "package" : "com.jozufozu.flywheel.vanilla.mixin", - "compatibilityLevel" : "JAVA_17", - "refmap" : "flywheel.refmap.json", - "client" : [ - "CubeDefinitionAccessor", - "CubeDeformationAccessor", - "EntityModelSetAccessor", - "LayerDefinitionAccessor", - "MaterialDefinitionAccessor", - "PartDefinitionAccessor", - "VertexMultiConsumerDoubleMixin" - ], - "injectors" : { - "defaultRequire" : 1 - } -}