From 686c73a3b128c2012e523986e01d8eb6e67d5100 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Mon, 9 Sep 2024 23:28:35 +0200 Subject: [PATCH] Fix #851: Improve meshing performance of fluid pipes (#874) --- .../pipes/api/PipeRenderer.java | 3 +-- .../pipes/impl/PipeMeshCache.java | 10 +++------- .../pipes/impl/PipeBlockEntity.java | 10 +++++++--- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/client/java/aztech/modern_industrialization/pipes/api/PipeRenderer.java b/src/client/java/aztech/modern_industrialization/pipes/api/PipeRenderer.java index 1610cd054..63c6a4b2c 100644 --- a/src/client/java/aztech/modern_industrialization/pipes/api/PipeRenderer.java +++ b/src/client/java/aztech/modern_industrialization/pipes/api/PipeRenderer.java @@ -29,7 +29,6 @@ import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.resources.model.Material; import net.minecraft.core.BlockPos; -import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.BlockAndTintGetter; import org.jetbrains.annotations.Nullable; @@ -52,7 +51,7 @@ static PipeRenderer.Factory get(PipeNetworkType type) { * connection type or null for no connection. */ void draw(@Nullable BlockAndTintGetter view, @Nullable BlockPos pos, PipeRenderContext ctx, int logicalSlot, PipeEndpointType[][] connections, - CompoundTag customData); + @Nullable Object customData); interface Factory { Collection getSpriteDependencies(); diff --git a/src/client/java/aztech/modern_industrialization/pipes/impl/PipeMeshCache.java b/src/client/java/aztech/modern_industrialization/pipes/impl/PipeMeshCache.java index bfb213607..3acd3dfc5 100644 --- a/src/client/java/aztech/modern_industrialization/pipes/impl/PipeMeshCache.java +++ b/src/client/java/aztech/modern_industrialization/pipes/impl/PipeMeshCache.java @@ -31,16 +31,13 @@ import aztech.modern_industrialization.thirdparty.fabricrendering.MutableQuadView; import aztech.modern_industrialization.thirdparty.fabrictransfer.api.client.fluid.FluidVariantRendering; import aztech.modern_industrialization.thirdparty.fabrictransfer.api.fluid.FluidVariant; -import aztech.modern_industrialization.util.NbtHelper; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.function.Function; -import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.resources.model.Material; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; -import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.BlockAndTintGetter; import org.jetbrains.annotations.Nullable; @@ -137,7 +134,7 @@ public PipeMeshCache(Function textureGetter, Mater */ public void draw(@Nullable BlockAndTintGetter view, @Nullable BlockPos pos, PipeRenderContext ctx, int logicalSlot, PipeEndpointType[][] connections, - CompoundTag customData) { + @Nullable Object customData) { // The render type of the connections (0 for no connection, 1 for straight pipe, // 2 for short bend, etc...) int[] renderTypes = new int[6]; @@ -160,8 +157,7 @@ public void draw(@Nullable BlockAndTintGetter view, @Nullable BlockPos pos, Pipe } // Fluid handling logic - if (customData.contains("fluid")) { - FluidVariant fluid = NbtHelper.getFluidCompatible(customData, "fluid", Minecraft.getInstance().player.registryAccess()); + if (customData instanceof FluidVariant fluid) { TextureAtlasSprite still = FluidVariantRendering.getSprite(fluid); int color = FluidVariantRendering.getColor(fluid, view, pos); ctx.pushTransform(quad -> { @@ -197,7 +193,7 @@ public void draw(@Nullable BlockAndTintGetter view, @Nullable BlockPos pos, Pipe centerMeshes.computeIfAbsent(new CenterMeshKey(logicalSlot, directionsMask), centerMeshBuilder).outputTo(ctx.getEmitter()); // Fluid handling logic - if (customData.contains("fluid")) { + if (customData instanceof FluidVariant) { ctx.popTransform(); } } diff --git a/src/main/java/aztech/modern_industrialization/pipes/impl/PipeBlockEntity.java b/src/main/java/aztech/modern_industrialization/pipes/impl/PipeBlockEntity.java index 592d4379f..5eedc3ee9 100644 --- a/src/main/java/aztech/modern_industrialization/pipes/impl/PipeBlockEntity.java +++ b/src/main/java/aztech/modern_industrialization/pipes/impl/PipeBlockEntity.java @@ -491,12 +491,16 @@ public Packet getUpdatePacket() { public ModelData getModelData() { PipeNetworkType[] types = new PipeNetworkType[connections.size()]; PipeEndpointType[][] renderedConnections = new PipeEndpointType[connections.size()][]; - CompoundTag[] customData = new CompoundTag[connections.size()]; + @Nullable + Object[] customData = new Object[connections.size()]; int i = 0; for (Map.Entry entry : connections.entrySet()) { types[i] = entry.getKey(); renderedConnections[i] = Arrays.copyOf(entry.getValue(), 6); - customData[i] = this.customData.get(entry.getKey()); + var data = this.customData.get(entry.getKey()); + if (data.contains("fluid")) { + customData[i] = NbtHelper.getFluidCompatible(data, "fluid", level.registryAccess()); + } i++; } return ModelData.builder() @@ -538,7 +542,7 @@ record RenderAttachment( @Nullable BlockState camouflage, PipeNetworkType[] types, PipeEndpointType[][] renderedConnections, - CompoundTag[] customData) { + @Nullable Object[] customData) { public static final ModelProperty KEY = new ModelProperty<>(); }