Skip to content

Commit

Permalink
Fix #851: Improve meshing performance of fluid pipes (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored Sep 9, 2024
1 parent 45e7a30 commit 686c73a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Material> getSpriteDependencies();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -137,7 +134,7 @@ public PipeMeshCache(Function<Material, TextureAtlasSprite> 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];
Expand All @@ -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 -> {
Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,16 @@ public Packet<ClientGamePacketListener> 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<PipeNetworkType, PipeEndpointType[]> 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()
Expand Down Expand Up @@ -538,7 +542,7 @@ record RenderAttachment(
@Nullable BlockState camouflage,
PipeNetworkType[] types,
PipeEndpointType[][] renderedConnections,
CompoundTag[] customData) {
@Nullable Object[] customData) {
public static final ModelProperty<RenderAttachment> KEY = new ModelProperty<>();
}

Expand Down

0 comments on commit 686c73a

Please sign in to comment.