Skip to content

Commit

Permalink
fix dragon render lag
Browse files Browse the repository at this point in the history
  • Loading branch information
IAFEnvoy committed Dec 4, 2024
1 parent 5454579 commit d32193c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.iafenvoy.uranus.client.model.util.TabulaModelHandlerHelper;
import com.iafenvoy.uranus.client.render.DynamicItemRenderer;
import com.iafenvoy.uranus.client.render.armor.IArmorRendererBase;
import com.iafenvoy.uranus.util.function.MemorizeSupplier;
import dev.architectury.registry.client.level.entity.EntityRendererRegistry;
import dev.architectury.registry.client.particle.ParticleProviderRegistry;
import dev.architectury.registry.client.rendering.BlockEntityRendererRegistry;
Expand All @@ -42,9 +43,9 @@ public final class IafRenderers {
public static final Identifier SEA_SERPENT = new Identifier(IceAndFire.MOD_ID, "seaserpent/seaserpent_base");

public static void registerEntityRenderers() {
EntityRendererRegistry.register(IafEntities.FIRE_DRAGON, x -> new RenderDragonBase(x, TabulaModelHandlerHelper.getModel(FIRE_DRAGON, FireDragonTabulaModelAnimator::new), 0));
EntityRendererRegistry.register(IafEntities.ICE_DRAGON, manager -> new RenderDragonBase(manager, TabulaModelHandlerHelper.getModel(ICE_DRAGON, IceDragonTabulaModelAnimator::new), 1));
EntityRendererRegistry.register(IafEntities.LIGHTNING_DRAGON, manager -> new RenderLightningDragon(manager, TabulaModelHandlerHelper.getModel(LIGHTNING_DRAGON, LightningTabulaDragonAnimator::new), 2));
EntityRendererRegistry.register(IafEntities.FIRE_DRAGON, x -> new RenderDragonBase(x, TabulaModelHandlerHelper.getModel(FIRE_DRAGON, new MemorizeSupplier<>(FireDragonTabulaModelAnimator::new)), 0));
EntityRendererRegistry.register(IafEntities.ICE_DRAGON, manager -> new RenderDragonBase(manager, TabulaModelHandlerHelper.getModel(ICE_DRAGON, new MemorizeSupplier<>(IceDragonTabulaModelAnimator::new)), 1));
EntityRendererRegistry.register(IafEntities.LIGHTNING_DRAGON, manager -> new RenderLightningDragon(manager, TabulaModelHandlerHelper.getModel(LIGHTNING_DRAGON, new MemorizeSupplier<>(LightningTabulaDragonAnimator::new)), 2));
EntityRendererRegistry.register(IafEntities.DRAGON_EGG, RenderDragonEgg::new);
EntityRendererRegistry.register(IafEntities.DRAGON_ARROW, RenderDragonArrow::new);
EntityRendererRegistry.register(IafEntities.DRAGON_SKULL, RenderDragonSkull::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.iafenvoy.uranus.client.model.TabulaModel;
import com.iafenvoy.uranus.client.model.basic.BasicModelPart;
import com.iafenvoy.uranus.client.model.util.TabulaModelHandlerHelper;
import com.iafenvoy.uranus.util.function.MemorizeSupplier;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
Expand All @@ -26,17 +27,17 @@
import java.util.function.Supplier;

public class RenderDragonSkull extends EntityRenderer<EntityDragonSkull> {
public static final Map<DragonType, Pair<Identifier, Supplier<ITabulaModelAnimator<?>>>> MODELS = new HashMap<>();
public static final Map<DragonType, Pair<Identifier, MemorizeSupplier<ITabulaModelAnimator<?>>>> MODELS = new HashMap<>();
public static final float[] growth_stage_1 = new float[]{1F, 3F};
public static final float[] growth_stage_2 = new float[]{3F, 7F};
public static final float[] growth_stage_3 = new float[]{7F, 12.5F};
public static final float[] growth_stage_4 = new float[]{12.5F, 20F};
public static final float[] growth_stage_5 = new float[]{20F, 30F};

static {
MODELS.put(DragonType.FIRE, Pair.of(IafRenderers.FIRE_DRAGON, FireDragonTabulaModelAnimator::new));
MODELS.put(DragonType.ICE, Pair.of(IafRenderers.ICE_DRAGON, IceDragonTabulaModelAnimator::new));
MODELS.put(DragonType.LIGHTNING, Pair.of(IafRenderers.LIGHTNING_DRAGON, LightningTabulaDragonAnimator::new));
MODELS.put(DragonType.FIRE, Pair.of(IafRenderers.FIRE_DRAGON, new MemorizeSupplier<>(FireDragonTabulaModelAnimator::new)));
MODELS.put(DragonType.ICE, Pair.of(IafRenderers.ICE_DRAGON, new MemorizeSupplier<>(IceDragonTabulaModelAnimator::new)));
MODELS.put(DragonType.LIGHTNING, Pair.of(IafRenderers.LIGHTNING_DRAGON, new MemorizeSupplier<>(LightningTabulaDragonAnimator::new)));
}

public final float[][] growth_stages;
Expand All @@ -54,7 +55,7 @@ private static void setRotationAngles(BasicModelPart cube, float rotX) {

@Override
public void render(EntityDragonSkull entity, float entityYaw, float partialTicks, MatrixStack matrixStackIn, VertexConsumerProvider bufferIn, int packedLightIn) {
Pair<Identifier, Supplier<ITabulaModelAnimator<?>>> p = MODELS.get(DragonType.getTypeById(entity.getDragonType()));
Pair<Identifier, MemorizeSupplier<ITabulaModelAnimator<?>>> p = MODELS.get(DragonType.getTypeById(entity.getDragonType()));
TabulaModel model = TabulaModelHandlerHelper.getModel(p.getFirst(), p.getSecond()::get);
if (model == null) return;
VertexConsumer ivertexbuilder = bufferIn.getBuffer(RenderLayer.getEntityTranslucent(this.getTexture(entity)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.math.MathHelper;

import java.util.LinkedHashMap;
import java.util.Map;

public abstract class DragonTabulaModelAnimator<T extends EntityDragonBase> extends IceAndFireTabulaModelAnimator implements ITabulaModelAnimator<T> {
//FIXME::Will not reload
private final Map<EnumDragonPoses, TabulaModel> CACHES = new LinkedHashMap<>();
protected TabulaModel[] walkPoses;
protected TabulaModel[] flyPoses;
protected TabulaModel[] swimPoses;
Expand Down Expand Up @@ -240,7 +245,14 @@ protected void genderMob(T entity, AdvancedModelBox cube) {
}
}

protected abstract TabulaModel getModel(EnumDragonPoses pose);
protected TabulaModel getModel(EnumDragonPoses pose) {
if (CACHES.containsKey(pose)) return CACHES.get(pose);
TabulaModel model = this.getModelInternal(pose);
CACHES.put(pose, model);
return model;
}

protected abstract TabulaModel getModelInternal(EnumDragonPoses pose);

protected void animate(TabulaModel model, T entity, float limbSwing, float limbSwingAmount, float ageInTicks, float rotationYaw, float rotationPitch, float scale) {
AdvancedModelBox modelCubeJaw = model.getCube("Jaw");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FireDragonTabulaModelAnimator() {
}

@Override
protected TabulaModel getModel(EnumDragonPoses pose) {
protected TabulaModel getModelInternal(EnumDragonPoses pose) {
return DragonAnimationsLibrary.getModel(pose, EnumDragonModelTypes.FIRE_DRAGON_MODEL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public IceDragonTabulaModelAnimator() {
}

@Override
protected TabulaModel getModel(EnumDragonPoses pose) {
protected TabulaModel getModelInternal(EnumDragonPoses pose) {
return DragonAnimationsLibrary.getModel(pose, EnumDragonModelTypes.ICE_DRAGON_MODEL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public LightningTabulaDragonAnimator() {
}

@Override
protected TabulaModel getModel(EnumDragonPoses pose) {
protected TabulaModel getModelInternal(EnumDragonPoses pose) {
return DragonAnimationsLibrary.getModel(pose, EnumDragonModelTypes.LIGHTNING_DRAGON_MODEL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
import com.iafenvoy.uranus.client.model.util.TabulaModelHandlerHelper;
import net.minecraft.util.Identifier;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* A library containing all animations for all dragons. Contains methods for registering and retrieving models
*/
public class DragonAnimationsLibrary {
private static final Map<Identifier, TabulaModel> CACHES = new LinkedHashMap<>();

public static TabulaModel getModel(IEnumDragonPoses pose, IEnumDragonModelTypes modelType) {
Identifier id = new Identifier(IceAndFire.MOD_ID, modelType.getModelType() + "dragon/" + modelType.getModelType() + "dragon_" + pose.getPose());
if (CACHES.containsKey(id)) return CACHES.get(id);
TabulaModel result = TabulaModelHandlerHelper.getModel(id);
if (result == null) IceAndFire.LOGGER.error("No model defined for {} have you registered your animations?", id);
CACHES.put(id, result);
return result;
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx2G
org.gradle.parallel=true

# Mod properties
mod_version = 0.8.1
mod_version = 0.8.2
maven_group = com.iafenvoy.iceandfire
archives_name = IceAndFireCE
enabled_platforms = fabric,forge
Expand Down

0 comments on commit d32193c

Please sign in to comment.