Skip to content

Commit

Permalink
Cache item quads on forge
Browse files Browse the repository at this point in the history
  • Loading branch information
UnlikePaladin committed Feb 21, 2024
1 parent 5fd7c80 commit 07126c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.unlikepaladin.pfm.client.forge;

import com.mojang.datafixers.util.Pair;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.texture.Sprite;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.Direction;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public interface PFMBakedModelGetQuadsExtension {
List<BakedQuad> getQuads(ItemStack stack, @Nullable BlockState state, @Nullable Direction face, Random random);

Map<Pair<ItemStack, Direction>, List<BakedQuad>> cache = new HashMap<>();
default List<BakedQuad> getQuadsCached(ItemStack stack, @Nullable BlockState state, @Nullable Direction face, Random random) {
Pair<ItemStack, Direction> directionPair = new Pair<>(stack, face);
if (cache.containsKey(directionPair))
return cache.get(directionPair);

List<BakedQuad> quads = getQuads(stack, state, face, random);
cache.put(directionPair, quads);
return quads;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ private void renderPFMItem(ItemStack stack, ModelTransformation.Mode renderMode,
long randomSeed = 42L;
for (Direction direction : Direction.values()) {
random.setSeed(randomSeed);
this.renderBakedItemQuads(matrices, vertexConsumer, ((PFMBakedModelGetQuadsExtension) model).getQuads(stack, state, direction, random), stack, light, overlay);
this.renderBakedItemQuads(matrices, vertexConsumer, ((PFMBakedModelGetQuadsExtension) model).getQuadsCached(stack, state, direction, random), stack, light, overlay);
}
random.setSeed(randomSeed);
this.renderBakedItemQuads(matrices, vertexConsumer, ((PFMBakedModelGetQuadsExtension)model).getQuads(stack, state, null, random), stack, light, overlay);
this.renderBakedItemQuads(matrices, vertexConsumer, ((PFMBakedModelGetQuadsExtension)model).getQuadsCached(stack, state, null, random), stack, light, overlay);

// Conditionally pop because of handlePerspective weirdness
if (matrices.peek() != pose) {
Expand Down

0 comments on commit 07126c0

Please sign in to comment.