MineLittlePony/src/main/java/com/minelittlepony/client/render/RenderPony.java

190 lines
5.7 KiB
Java
Raw Normal View History

2019-03-23 20:49:34 +01:00
package com.minelittlepony.client.render;
import com.minelittlepony.client.MineLittlePony;
2019-03-23 20:49:34 +01:00
import com.minelittlepony.client.PonyRenderManager;
import com.minelittlepony.client.model.IPonyModel;
2019-03-23 20:49:34 +01:00
import com.minelittlepony.client.model.ModelWrapper;
import com.minelittlepony.client.transform.PonyPosture;
import com.minelittlepony.pony.IPony;
import com.minelittlepony.util.math.MathUtil;
2019-11-23 18:28:42 +01:00
import com.mojang.blaze3d.platform.GlStateManager.DestFactor;
import com.mojang.blaze3d.platform.GlStateManager.SourceFactor;
import com.mojang.blaze3d.systems.RenderSystem;
2019-05-28 01:50:45 +02:00
import javax.annotation.Nonnull;
2019-03-24 18:55:15 +01:00
2019-11-23 18:28:42 +01:00
import net.minecraft.client.render.Frustum;
2019-05-27 17:59:15 +02:00
import net.minecraft.client.render.entity.model.EntityModel;
2019-11-23 18:28:42 +01:00
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
2019-05-27 17:59:15 +02:00
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.Identifier;
2019-05-27 17:59:15 +02:00
public class RenderPony<T extends LivingEntity, M extends EntityModel<T> & IPonyModel<T>> {
2019-05-27 17:59:15 +02:00
public ModelWrapper<T, M> playerModel;
private IPony pony;
2019-05-28 01:50:45 +02:00
private final IPonyRender<T, M> renderer;
private boolean skipBlend;
2019-05-28 01:50:45 +02:00
private final FrustrumCheck<T> frustrum = new FrustrumCheck<>(this);
public static void enableModelRenderProfile(boolean skipBlend) {
2019-11-23 18:28:42 +01:00
RenderSystem.enableBlend();
if (!skipBlend) {
2019-11-23 18:28:42 +01:00
RenderSystem.blendFunc(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA);
}
2019-11-23 18:28:42 +01:00
RenderSystem.alphaFunc(516, 0.003921569F);
2018-08-20 21:25:11 +02:00
}
public static void disableModelRenderProfile() {
2019-11-23 18:28:42 +01:00
RenderSystem.disableBlend();
2018-08-20 21:25:11 +02:00
}
2019-05-27 17:59:15 +02:00
public RenderPony(IPonyRender<T, M> renderer) {
this.renderer = renderer;
}
public void setSkipBlend() {
skipBlend = true;
}
2019-11-23 18:28:42 +01:00
public Frustum getFrustrum(T entity, Frustum vanilla) {
if (entity.isSleeping() || !MineLittlePony.getInstance().getConfig().frustrum.get()) {
return vanilla;
}
return frustrum.withCamera(entity, vanilla);
}
2019-11-23 18:28:42 +01:00
public void preRenderCallback(T entity, MatrixStack stack, float ticks) {
updateModel(entity);
float s = getScaleFactor();
2019-11-23 18:28:42 +01:00
stack.scale(s, s, s);
enableModelRenderProfile(skipBlend);
2019-11-23 18:28:42 +01:00
translateRider(entity, stack, ticks);
}
public float getRenderYaw(T entity, float rotationYaw, float partialTicks) {
2019-05-27 17:59:15 +02:00
if (entity.hasVehicle()) {
Entity mount = entity.getVehicle();
if (mount instanceof LivingEntity) {
2019-11-22 18:24:22 +01:00
return MathUtil.interpolateDegress(((LivingEntity) mount).prevBodyYaw, ((LivingEntity) mount).bodyYaw, partialTicks);
}
}
return rotationYaw;
}
2019-11-23 18:28:42 +01:00
protected void translateRider(T entity, MatrixStack stack, float ticks) {
if (entity.hasVehicle() && entity.getVehicle() instanceof LivingEntity) {
LivingEntity ridingEntity = (LivingEntity) entity.getVehicle();
IPonyRender<LivingEntity, ?> renderer = PonyRenderManager.getInstance().getPonyRenderer(ridingEntity);
if (renderer != null) {
// negate vanilla translations so the rider begins at the ridees feet.
2019-11-23 18:28:42 +01:00
stack.translate(0, -ridingEntity.getHeight(), 0);
IPony riderPony = renderer.getEntityPony(ridingEntity);
2019-11-23 18:28:42 +01:00
renderer.translateRider(ridingEntity, riderPony, entity, pony, stack, ticks);
}
}
}
@SuppressWarnings("unchecked")
2019-11-23 18:28:42 +01:00
public void applyPostureTransform(T player, MatrixStack stack, float yaw, float ticks) {
((PonyPosture<T>) getPosture(player)).apply(player, getModel(), stack, yaw, ticks, 1);
}
@SuppressWarnings("unchecked")
2019-11-23 18:28:42 +01:00
public void applyPostureRiding(T player, MatrixStack stack, float yaw, float ticks) {
((PonyPosture<T>) getPosture(player)).apply(player, getModel(), stack, yaw, ticks, -1);
}
2019-05-28 01:50:45 +02:00
@Nonnull
private PonyPosture<?> getPosture(T entity) {
2019-05-27 17:59:15 +02:00
if (entity.isFallFlying()) {
return PonyPosture.ELYTRA;
}
2019-05-27 17:59:15 +02:00
if (entity.isAlive() && entity.isSleeping()) {
2019-05-28 01:50:45 +02:00
return PonyPosture.DEFAULT;
2019-03-24 18:55:15 +01:00
}
if (getModel().getAttributes().isSwimming) {
return PonyPosture.SWIMMING;
}
if (getModel().getAttributes().isGoingFast) {
return PonyPosture.FLIGHT;
}
return PonyPosture.FALLING;
}
2019-05-28 01:50:45 +02:00
public M getModel() {
return playerModel.getBody();
}
2019-05-27 17:59:15 +02:00
public M setPonyModel(ModelWrapper<T, M> model) {
playerModel = model;
return getModel();
}
public void updateMetadata(Identifier texture) {
pony = MineLittlePony.getInstance().getManager().getPony(texture);
playerModel.apply(pony.getMetadata());
}
public void updateModel(T entity) {
pony = renderer.getEntityPony(entity);
playerModel.apply(pony.getMetadata());
pony.updateForEntity(entity);
getModel().updateLivingState(entity, pony);
}
public IPony getPony(T entity) {
updateModel(entity);
return pony;
}
public float getShadowScale() {
2019-05-28 01:50:45 +02:00
return getModel().getSize().getShadowSize();
}
public float getScaleFactor() {
2019-05-28 01:50:45 +02:00
return getModel().getSize().getScaleFactor();
}
2019-11-23 18:28:42 +01:00
public double getNamePlateYOffset(T entity) {
// We start by negating the height calculation done by mahjong.
float y = -(entity.getHeight() + 0.5F - (entity.isInSneakingPose() ? 0.25F : 0));
// Then we add our own offsets.
y += getModel().getAttributes().visualHeight * getScaleFactor() + 0.25F;
if (entity.isSneaking()) {
2018-09-05 13:56:07 +02:00
y -= 0.25F;
}
2019-05-27 17:59:15 +02:00
if (entity.hasVehicle()) {
y += entity.getVehicle().getEyeHeight(entity.getPose());
}
2019-05-27 17:59:15 +02:00
if (entity.isSleeping()) {
y /= 2;
}
2019-11-23 18:28:42 +01:00
return y;
}
}