From 918c19a489abcce046db897898de3f013874ca25 Mon Sep 17 00:00:00 2001 From: Zepalesque <60141811+Zepalesque@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:56:32 -0500 Subject: [PATCH] feat: Render Caching stuff --- .../client/event/listener/RenderListener.java | 4 ++ .../client/renderer/api/ICachedRerender.java | 63 +++++++++++++++++++ .../entity/ReduxWhirlwindRenderer.java | 19 +++++- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/main/java/net/zepalesque/redux/client/event/listener/RenderListener.java create mode 100644 src/main/java/net/zepalesque/redux/client/renderer/api/ICachedRerender.java diff --git a/src/main/java/net/zepalesque/redux/client/event/listener/RenderListener.java b/src/main/java/net/zepalesque/redux/client/event/listener/RenderListener.java new file mode 100644 index 000000000..a910b8b2a --- /dev/null +++ b/src/main/java/net/zepalesque/redux/client/event/listener/RenderListener.java @@ -0,0 +1,4 @@ +package net.zepalesque.redux.client.event.listener; + +public class RenderListener { +} diff --git a/src/main/java/net/zepalesque/redux/client/renderer/api/ICachedRerender.java b/src/main/java/net/zepalesque/redux/client/renderer/api/ICachedRerender.java new file mode 100644 index 000000000..d0ebfb4c0 --- /dev/null +++ b/src/main/java/net/zepalesque/redux/client/renderer/api/ICachedRerender.java @@ -0,0 +1,63 @@ +package net.zepalesque.redux.client.renderer.api; + +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.world.entity.Entity; +import net.zepalesque.zenith.util.lambda.Consumers; +import org.jetbrains.annotations.NotNull; + +public interface ICachedRerender { + + default boolean actuallyRender() { + Cache cache = this.getCache(); + boolean success = cache.execute(this::internalRender); + if (success) { + cache.clear(); + return true; + } + return false; + } + + Cache getCache(); + + void internalRender(@NotNull T entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight); + + class Cache { + + private T entity; + private float entityYaw, partialTicks; + private PoseStack poseStack; + private MultiBufferSource buffer; + private int packedLight; + private boolean cached = false; + + public Cache() {} + + public void cache(@NotNull T entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight) { + this.entity = entity; + this.entityYaw = entityYaw; + this.partialTicks = partialTicks; + this.poseStack = poseStack; + this.buffer = buffer; + this.packedLight = packedLight; + this.cached = true; + } + + public void clear() { + this.entity = null; + this.entityYaw = Float.NaN; + this.partialTicks = Float.NaN; + this.poseStack = null; + this.buffer = null; + this.packedLight = Integer.MIN_VALUE; + this.cached = false; + } + + public boolean execute(Consumers.C6 method) { + if (this.cached) { + method.accept(this.entity, this.entityYaw, this.partialTicks, this.poseStack, this.buffer, this.packedLight); + return true; + } else return false; + } + } +} diff --git a/src/main/java/net/zepalesque/redux/client/renderer/entity/ReduxWhirlwindRenderer.java b/src/main/java/net/zepalesque/redux/client/renderer/entity/ReduxWhirlwindRenderer.java index c6e9db674..5946cfd26 100644 --- a/src/main/java/net/zepalesque/redux/client/renderer/entity/ReduxWhirlwindRenderer.java +++ b/src/main/java/net/zepalesque/redux/client/renderer/entity/ReduxWhirlwindRenderer.java @@ -14,17 +14,20 @@ import net.minecraft.resources.ResourceLocation; import net.zepalesque.redux.client.renderer.ReduxRenderTypes; import net.zepalesque.redux.client.renderer.ReduxRenderers; +import net.zepalesque.redux.client.renderer.api.ICachedRerender; import net.zepalesque.redux.client.renderer.entity.model.WhirlwindModel; import net.zepalesque.redux.config.ReduxConfig; import org.jetbrains.annotations.NotNull; -public class ReduxWhirlwindRenderer extends LivingEntityRenderer> { +public class ReduxWhirlwindRenderer extends LivingEntityRenderer> implements ICachedRerender { private static final ResourceLocation WHIRLWIND = ResourceLocation.fromNamespaceAndPath(Aether.MODID, "textures/entity/whirlwind/whirlwind.png"); private static final ResourceLocation EVIL_WHIRLWIND = ResourceLocation.fromNamespaceAndPath(Aether.MODID, "textures/entity/whirlwind/evil_whirlwind.png"); + private final Cache cache; public ReduxWhirlwindRenderer(EntityRendererProvider.Context context) { super(context, new WhirlwindModel<>(context.bakeLayer(ReduxRenderers.ModelLayers.WHIRLWIND)), 0.0F); + this.cache = new Cache<>(); } @Override @@ -36,8 +39,16 @@ public ResourceLocation getTextureLocation(@NotNull T whirlwind) { @Override public void render(@NotNull T entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight) { - if (ReduxConfig.CLIENT.improved_whirlwinds.get()) { + this.cache.cache(entity, entityYaw, partialTicks, poseStack, buffer, packedLight); + } + + @Override + public Cache getCache() { + return this.cache; + } + public void internalRender(@NotNull T entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight) { + if (ReduxConfig.CLIENT.improved_whirlwinds.get()) { boolean isEvil = entity.getType() == AetherEntityTypes.EVIL_WHIRLWIND.get(); float age = this.getBob(entity, partialTicks); VertexConsumer vertexconsumer = buffer.getBuffer(ReduxRenderTypes.whirlwind(getTextureLocation(entity), this.xOffset(age, isEvil) % 1.0F, 0.0F)); @@ -49,6 +60,7 @@ public void render(@NotNull T entity, float entityYaw, float partialTicks, PoseS this.model.renderToBuffer(poseStack, vertexconsumer, packedLight, OverlayTexture.NO_OVERLAY); poseStack.popPose(); } + } @@ -58,4 +70,7 @@ private float xOffset(float tickCount, boolean isEvil) { @Override protected void setupRotations(@NotNull AbstractWhirlwind entity, @NotNull PoseStack poseStack, float bob, float yBodyRot, float partialTick, float scale) { } + + + }