Skip to content

Commit

Permalink
feat: Render Caching stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepalesque committed Dec 27, 2024
1 parent d362070 commit 918c19a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.zepalesque.redux.client.event.listener;

public class RenderListener {
}
Original file line number Diff line number Diff line change
@@ -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<T extends Entity> {

default boolean actuallyRender() {
Cache<T> cache = this.getCache();
boolean success = cache.execute(this::internalRender);
if (success) {
cache.clear();
return true;
}
return false;
}

Cache<T> getCache();

void internalRender(@NotNull T entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight);

class Cache<T extends Entity> {

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<T, Float, Float, PoseStack, MultiBufferSource, Integer> method) {
if (this.cached) {
method.accept(this.entity, this.entityYaw, this.partialTicks, this.poseStack, this.buffer, this.packedLight);
return true;
} else return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends AbstractWhirlwind> extends LivingEntityRenderer<T, EntityModel<T>> {
public class ReduxWhirlwindRenderer<T extends AbstractWhirlwind> extends LivingEntityRenderer<T, EntityModel<T>> implements ICachedRerender<T> {

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<T> cache;
public ReduxWhirlwindRenderer(EntityRendererProvider.Context context) {
super(context, new WhirlwindModel<>(context.bakeLayer(ReduxRenderers.ModelLayers.WHIRLWIND)), 0.0F);
this.cache = new Cache<>();
}

@Override
Expand All @@ -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<T> 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));
Expand All @@ -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();
}

}


Expand All @@ -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) { }



}

0 comments on commit 918c19a

Please sign in to comment.