Skip to content

Commit

Permalink
Port apparition renderer and model
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 committed Oct 19, 2024
1 parent aa33655 commit 4eb65b4
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,13 @@ protected SoundEvent getAmbientSound() {
}

@Override
@NotNull
protected SoundEvent getHurtSound(DamageSource source) {
return TTSounds.APPARITION_HURT;
}

@Override
@NotNull
protected SoundEvent getDeathSound() {
return TTSounds.APPARITION_DEATH;
}
Expand Down Expand Up @@ -616,7 +618,6 @@ protected void sendDebugPackets() {
DebugPackets.sendEntityBrain(this);
}

@Contract("null->false")
public boolean canTargetEntity(@Nullable Entity entity, ServerLevel level) {
return entity instanceof LivingEntity livingEntity
&& this.level() == livingEntity.level()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

public class ApparitionOverlayLayer<T extends ApparitionRenderState> extends RenderLayer<T, ApparitionModel<T>> {
public class ApparitionOverlayLayer extends RenderLayer<ApparitionRenderState, ApparitionModel> {
private final RenderType renderType;
private final ApparitionModel<T> model;
private final ApparitionModel model;

public ApparitionOverlayLayer(
EntityRendererProvider.@NotNull Context context,
RenderLayerParent<T, ApparitionModel<T>> renderLayerParent,
ApparitionModel.AlphaFunction<T> innerAlphaFunction,
ApparitionModel.AlphaFunction<T> outlineAlphaFunction,
ApparitionModel.AlphaFunction<T> outerAlphaFunction,
ApparitionModel.DrawSelector<T, ApparitionModel<T>> drawSelector,
RenderLayerParent<ApparitionRenderState, ApparitionModel> renderLayerParent,
ApparitionModel.AlphaFunction<ApparitionRenderState> innerAlphaFunction,
ApparitionModel.AlphaFunction<ApparitionRenderState> outlineAlphaFunction,
ApparitionModel.AlphaFunction<ApparitionRenderState> outerAlphaFunction,
ApparitionModel.DrawSelector<ApparitionRenderState, ApparitionModel> drawSelector,
ResourceLocation texture,
boolean cull
) {
super(renderLayerParent);
this.model = new ApparitionModel<>(
this.model = new ApparitionModel(
cull ? FrozenRenderType::apparitionOuterCull : FrozenRenderType::apparitionOuter,
context.bakeLayer(TrailierTalesClient.APPARITION_OVERLAY),
innerAlphaFunction,
Expand All @@ -47,17 +47,16 @@ public void render(
PoseStack matrices,
@NotNull MultiBufferSource vertexConsumers,
int light,
T renderState,
ApparitionRenderState renderState,
float partialTick,
float color
) {
this.model.prepareMobModel(renderState, renderState.walkAnimationPos, renderState.walkAnimationSpeed, partialTick);
this.model.setupAnim(renderState);
VertexConsumer vertexConsumer = vertexConsumers.getBuffer(this.renderType);
this.model.renderToBuffer(matrices, vertexConsumer, 15728640, this.getOverlay(renderState, 0F));
}

private int getOverlay(@NotNull T renderState, float whiteOverlayProgress) {
private int getOverlay(@NotNull ApparitionRenderState renderState, float whiteOverlayProgress) {
return OverlayTexture.pack(OverlayTexture.u(whiteOverlayProgress), OverlayTexture.v(renderState.hurtTime > 0 || renderState.deathTime > 0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class ApparitionModel<T extends ApparitionRenderState> extends EntityModel<T> {
public class ApparitionModel extends EntityModel<ApparitionRenderState> {
private final ModelPart root;
public final ModelPart core;
public final ModelPart inner;
public final ModelPart outline;
public final ModelPart outer;
private final AlphaFunction<T> innerAlphaFunction;
private final AlphaFunction<T> outlineAlphaFunction;
private final AlphaFunction<T> outerAlphaFunction;
private final DrawSelector<T, ApparitionModel<T>> drawSelector;
private final AlphaFunction<ApparitionRenderState> innerAlphaFunction;
private final AlphaFunction<ApparitionRenderState> outlineAlphaFunction;
private final AlphaFunction<ApparitionRenderState> outerAlphaFunction;
private final DrawSelector<ApparitionRenderState, ApparitionModel> drawSelector;
private final List<ModelPart> modelParts;
private final List<ModelPart> coreParts;
private final List<ModelPart> outerParts;
Expand All @@ -47,22 +47,22 @@ public ApparitionModel(@NotNull ModelPart root) {
this(
FrozenRenderType::apparitionOuterCull,
root,
Apparition::getInnerTransparency,
Apparition::getOutlineTransparency,
Apparition::getOuterTransparency,
renderState -> renderState.innerTransparency,
renderState -> renderState.outlineTransparency,
renderState -> renderState.outlineTransparency,
ApparitionModel::getParts
);
}

public ApparitionModel(
Function<ResourceLocation, RenderType> function,
@NotNull ModelPart root,
AlphaFunction<T> innerAlpha,
AlphaFunction<T> outlineAlpha,
AlphaFunction<T> outerAlpha,
DrawSelector<T, ApparitionModel<T>> drawSelector
AlphaFunction<ApparitionRenderState> innerAlpha,
AlphaFunction<ApparitionRenderState> outlineAlpha,
AlphaFunction<ApparitionRenderState> outerAlpha,
DrawSelector<ApparitionRenderState, ApparitionModel> drawSelector
) {
super(function);
super(root, function);
this.root = root;
this.core = root.getChild("core");
this.inner = this.core.getChild("inner");
Expand Down Expand Up @@ -110,14 +110,19 @@ public static LayerDefinition createBodyLayer() {
}

@Override
@NotNull
public ModelPart root() {
return this.root;
}

@Override
public void setupAnim(T entity, float limbAngle, float limbDistance, float animationProgress, float headYaw, float headPitch) {
animationProgress = animationProgress + (limbAngle * 3.5F);
public void setupAnim(ApparitionRenderState renderState) {
float limbAngle = renderState.walkAnimationPos;
float limbDistance = renderState.walkAnimationSpeed;
float headYaw = renderState.yRot;
float headPitch = renderState.xRot;
this.innerTransparency = this.innerAlphaFunction.apply(renderState);
this.outlineTransparency = this.outlineAlphaFunction.apply(renderState);
this.outerTransparency = this.outerAlphaFunction.apply(renderState);
this.flicker = renderState.flicker;
this.outer.yRot = renderState.itemYRot;
this.outer.zRot = renderState.itemZRot;

float animationProgress = renderState.ageInTicks + (limbAngle * 3.5F);
this.core.yRot = headYaw * ((float) Math.PI / 180F);
this.core.xRot = headPitch * ((float) Math.PI / 180F);

Expand All @@ -134,16 +139,6 @@ public void setupAnim(T entity, float limbAngle, float limbDistance, float anima
this.outer.yScale = -sinIdle + 1;
}

@Override
public void prepareMobModel(@NotNull T renderState, float limbAngle, float limbDistance, float tickDelta) {
this.innerTransparency = this.innerAlphaFunction.apply(renderState, tickDelta);
this.outlineTransparency = this.outlineAlphaFunction.apply(renderState, tickDelta);
this.outerTransparency = this.outerAlphaFunction.apply(renderState, tickDelta);
this.flicker = renderState.getFlicker(tickDelta);
this.outer.yRot = renderState.getItemYRot(tickDelta);
this.outer.zRot = renderState.getItemZRot(tickDelta);
}

@Override
public void renderToBuffer(@NotNull PoseStack poseStack, @NotNull VertexConsumer buffer, int packedLight, int packedOverlay, int colorBad) {
poseStack.pushPose();
Expand Down Expand Up @@ -177,7 +172,7 @@ private void resetDrawForAllParts() {

@Environment(EnvType.CLIENT)
public interface AlphaFunction<T extends ApparitionRenderState> {
float apply(T apparition, float tickDelta);
float apply(T apparition);
}

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
public class ApparitionRenderer extends MobRenderer<Apparition, ApparitionRenderState, ApparitionModel<Apparition>> {
public class ApparitionRenderer extends MobRenderer<Apparition, ApparitionRenderState, ApparitionModel> {
private final ItemRenderer itemRenderer;
private float itemYaw;

private static final ResourceLocation TEXTURE = TTConstants.id("textures/entity/apparition/apparition.png");

public ApparitionRenderer(EntityRendererProvider.Context context) {
super(context, new ApparitionModel<>(context.bakeLayer(TrailierTalesClient.APPARITION)), 0.5F);
this.addLayer(new ApparitionOverlayLayer<>(
super(context, new ApparitionModel(context.bakeLayer(TrailierTalesClient.APPARITION)), 0.5F);
this.addLayer(new ApparitionOverlayLayer(
context,
this,
(apparition, tickDelta) -> apparition.getAidAnimProgress(tickDelta) * 0.8F,
(apparition, tickDelta) -> apparition.getAidAnimProgress(tickDelta) * 0.8F,
(apparition, tickDelta) -> apparition.getAidAnimProgress(tickDelta) * 0.8F,
renderState -> renderState.aidAnimProgress * 0.8F,
renderState -> renderState.aidAnimProgress * 0.8F,
renderState -> renderState.aidAnimProgress * 0.8F,
ApparitionModel::getParts,
TTConstants.id("textures/entity/apparition/apparition_hypnotizing.png"),
true
));
this.addLayer(new ApparitionOverlayLayer<>(
this.addLayer(new ApparitionOverlayLayer(
context,
this,
(apparition, tickDelta) -> apparition.getPoltergeistAnimProgress(tickDelta) * 0.8F,
(apparition, tickDelta) -> apparition.getPoltergeistAnimProgress(tickDelta) * 0.8F,
(apparition, tickDelta) -> apparition.getPoltergeistAnimProgress(tickDelta) * 0.8F,
renderState -> renderState.poltergeistAnimProgress * 0.8F,
renderState -> renderState.poltergeistAnimProgress * 0.8F,
renderState -> renderState.poltergeistAnimProgress * 0.8F,
ApparitionModel::getParts,
TTConstants.id("textures/entity/apparition/apparition_shooting.png"),
true
Expand All @@ -54,15 +54,15 @@ public ApparitionRenderer(EntityRendererProvider.Context context) {
}

@Override
public void render(@NotNull Apparition entity, float entityYaw, float partialTick, @NotNull PoseStack poseStack, @NotNull MultiBufferSource buffer, int packedLight) {
super.render(entity, entityYaw, partialTick, poseStack, buffer, packedLight);
ItemStack stack = entity.getVisibleItem();
public void render(@NotNull ApparitionRenderState renderState, @NotNull PoseStack poseStack, @NotNull MultiBufferSource buffer, int packedLight) {
super.render(renderState, poseStack, buffer, packedLight);
ItemStack stack = renderState.visibleItem;
if (!stack.isEmpty()) {
poseStack.pushPose();
poseStack.translate(0F, 0.425F, 0F);
poseStack.mulPose(Axis.YP.rotationDegrees(180F - this.itemYaw));
poseStack.mulPose(Axis.YN.rotation(entity.getItemYRot(partialTick)));
poseStack.mulPose(Axis.ZN.rotation(entity.getItemZRot(partialTick)));
poseStack.mulPose(Axis.YN.rotation(renderState.itemYRot));
poseStack.mulPose(Axis.ZN.rotation(renderState.itemZRot));
poseStack.pushPose();
this.itemRenderer.renderStatic(stack, ItemDisplayContext.GROUND, packedLight, OverlayTexture.NO_OVERLAY, poseStack, buffer, null, 1);
poseStack.popPose();
Expand All @@ -71,20 +71,44 @@ public void render(@NotNull Apparition entity, float entityYaw, float partialTic
}

@Override
protected void setupRotations(Apparition entity, PoseStack matrices, float animationProgress, float bodyYaw, float tickDelta, float f) {
super.setupRotations(entity, matrices, animationProgress, bodyYaw, tickDelta, f);
protected void setupRotations(ApparitionRenderState renderState, PoseStack matrices, float bodyYaw, float scale) {
super.setupRotations(renderState, matrices, bodyYaw, scale);
this.itemYaw = bodyYaw;
this.shadowStrength = entity.totalTransparency(tickDelta);
this.shadowStrength = renderState.totalTransparency;
}

@Override
@NotNull
public ResourceLocation getTextureLocation(Apparition entity) {
public ResourceLocation getTextureLocation(ApparitionRenderState renderState) {
return TEXTURE;
}

@Override
protected int getBlockLightLevel(Apparition entity, BlockPos pos) {
return 15;
}

@Override
@NotNull
public ApparitionRenderState createRenderState() {
return new ApparitionRenderState();
}

@Override
public void extractRenderState(Apparition apparition, ApparitionRenderState renderState, float partialTick) {
super.extractRenderState(apparition, renderState, partialTick);

renderState.hurtTime = apparition.hurtTime;
renderState.itemYRot = apparition.getItemYRot(partialTick);
renderState.itemZRot = apparition.getItemZRot(partialTick);
renderState.totalTransparency = apparition.totalTransparency(partialTick);
renderState.innerTransparency = apparition.getInnerTransparency(partialTick);
renderState.outlineTransparency = apparition.getOutlineTransparency(partialTick);
renderState.outerTransparency = apparition.getOuterTransparency(partialTick);
renderState.flicker = apparition.getFlicker(partialTick);

renderState.visibleItem = apparition.getVisibleItem();
renderState.aidAnimProgress = apparition.getAidAnimProgress(partialTick);
renderState.poltergeistAnimProgress = apparition.getPoltergeistAnimProgress(partialTick);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.entity.state.LivingEntityRenderState;
import net.minecraft.world.item.ItemStack;

@Environment(EnvType.CLIENT)
public class ApparitionRenderState extends LivingEntityRenderState {
Expand All @@ -11,5 +12,12 @@ public class ApparitionRenderState extends LivingEntityRenderState {
public float itemYRot;
public float itemZRot;
public float totalTransparency;
public float innerTransparency;
public float outlineTransparency;
public float outerTransparency;
public float flicker;

public ItemStack visibleItem;
public float aidAnimProgress;
public float poltergeistAnimProgress;
}

0 comments on commit 4eb65b4

Please sign in to comment.