mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-03-29 14:57:43 +01:00
198 lines
6.5 KiB
Java
198 lines
6.5 KiB
Java
package com.minelittlepony.client.render;
|
|
|
|
import com.minelittlepony.api.config.PonyConfig;
|
|
import com.minelittlepony.api.events.Channel;
|
|
import com.minelittlepony.api.events.PonyDataCallback;
|
|
import com.minelittlepony.api.model.*;
|
|
import com.minelittlepony.api.pony.Pony;
|
|
import com.minelittlepony.client.MineLittlePony;
|
|
import com.minelittlepony.client.transform.PonyPosture;
|
|
import com.minelittlepony.mson.api.ModelKey;
|
|
import com.minelittlepony.util.MathUtil;
|
|
import com.mojang.blaze3d.systems.RenderSystem;
|
|
|
|
import java.util.Objects;
|
|
|
|
import net.fabricmc.api.EnvType;
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.client.render.Frustum;
|
|
import net.minecraft.client.render.entity.model.EntityModel;
|
|
import net.minecraft.client.util.math.MatrixStack;
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.entity.LivingEntity;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class EquineRenderManager<T extends LivingEntity, M extends EntityModel<T> & PonyModel<T>> {
|
|
|
|
private ModelWrapper<T, M> playerModel;
|
|
|
|
private final PonyRenderContext<T, M> renderer;
|
|
|
|
private final FrustrumCheck<T> frustrum = new FrustrumCheck<>(this);
|
|
|
|
public static void disableModelRenderProfile() {
|
|
RenderSystem.disableBlend();
|
|
}
|
|
|
|
public EquineRenderManager(PonyRenderContext<T, M> renderer) {
|
|
this.renderer = renderer;
|
|
}
|
|
|
|
public PonyRenderContext<T, M> getContext() {
|
|
return renderer;
|
|
}
|
|
|
|
public ModelWrapper<T, M> getModelWrapper() {
|
|
return playerModel;
|
|
}
|
|
|
|
public M getModel() {
|
|
return playerModel.body();
|
|
}
|
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
public ModelWrapper<T, M> setModel(ModelKey<? super M> key) {
|
|
return setModel(new ModelWrapper(key));
|
|
}
|
|
|
|
public ModelWrapper<T, M> setModel(ModelWrapper<T, M> wrapper) {
|
|
playerModel = wrapper;
|
|
return wrapper;
|
|
}
|
|
|
|
public Frustum getFrustrum(T entity, Frustum vanilla) {
|
|
if (RenderPass.getCurrent() == RenderPass.HUD) {
|
|
return FrustrumCheck.ALWAYS_VISIBLE;
|
|
}
|
|
|
|
if (entity.isSleeping() || !PonyConfig.getInstance().frustrum.get()) {
|
|
return vanilla;
|
|
}
|
|
return frustrum.withCamera(entity, vanilla);
|
|
}
|
|
|
|
public float getRenderYaw(T entity, float rotationYaw, float partialTicks) {
|
|
if (entity.hasVehicle()) {
|
|
Entity mount = entity.getVehicle();
|
|
if (mount instanceof LivingEntity) {
|
|
return MathUtil.interpolateDegress(((LivingEntity) mount).prevBodyYaw, ((LivingEntity) mount).bodyYaw, partialTicks);
|
|
}
|
|
}
|
|
|
|
return rotationYaw;
|
|
}
|
|
|
|
public void preRenderCallback(T entity, MatrixStack stack, float ticks) {
|
|
updateModel(entity, ModelAttributes.Mode.THIRD_PERSON);
|
|
|
|
float s = getScaleFactor();
|
|
stack.scale(s, s, s);
|
|
|
|
translateRider(entity, stack, ticks);
|
|
}
|
|
|
|
private void translateRider(T entity, MatrixStack stack, float ticks) {
|
|
if (entity.hasVehicle() && entity.getVehicle() instanceof LivingEntity) {
|
|
|
|
LivingEntity ridingEntity = (LivingEntity) entity.getVehicle();
|
|
PonyRenderContext<LivingEntity, ?> renderer = MineLittlePony.getInstance().getRenderDispatcher().getPonyRenderer(ridingEntity);
|
|
|
|
if (renderer != null) {
|
|
// negate vanilla translations so the rider begins at the ridees feet.
|
|
stack.translate(0, -ridingEntity.getHeight(), 0);
|
|
|
|
Pony riderPony = renderer.getEntityPony(ridingEntity);
|
|
|
|
renderer.translateRider(ridingEntity, riderPony, entity, renderer.getEntityPony(entity), stack, ticks);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setupTransforms(T entity, MatrixStack stack, float yaw, float tickDelta) {
|
|
PonyPosture.of(getModel().getAttributes()).apply(entity, getModel(), stack, yaw, tickDelta, 1);
|
|
}
|
|
|
|
public void applyPostureRiding(T entity, MatrixStack stack, float yaw, float tickDelta) {
|
|
PonyPosture.of(getModel().getAttributes()).apply(entity, getModel(), stack, yaw, tickDelta, -1);
|
|
}
|
|
|
|
public Pony updateModel(T entity, ModelAttributes.Mode mode) {
|
|
Pony pony = renderer.getEntityPony(entity);
|
|
playerModel.applyMetadata(pony.metadata());
|
|
|
|
if (entity instanceof PlayerEntity player && entity instanceof RegistrationHandler handler) {
|
|
SyncedPony synced = handler.getSyncedPony();
|
|
boolean changed = pony.compareTo(synced.lastRenderedPony) != 0;
|
|
|
|
if (changed) {
|
|
synced.lastRenderedPony = pony;
|
|
player.calculateDimensions();
|
|
}
|
|
|
|
if (!(player instanceof PreviewModel)) {
|
|
@Nullable
|
|
PlayerEntity clientPlayer = MinecraftClient.getInstance().player;
|
|
|
|
if (pony.compareTo(synced.lastTransmittedPony) != 0) {
|
|
if (clientPlayer != null && (Objects.equals(player, clientPlayer) || Objects.equals(player.getGameProfile(), clientPlayer.getGameProfile()))) {
|
|
if (Channel.broadcastPonyData(pony.metadata())) {
|
|
synced.lastTransmittedPony = pony;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (changed) {
|
|
PonyDataCallback.EVENT.invoker().onPonyDataAvailable(player, pony.metadata(), EnvType.CLIENT);
|
|
}
|
|
}
|
|
}
|
|
|
|
getModel().updateLivingState(entity, pony, mode);
|
|
|
|
return pony;
|
|
}
|
|
|
|
public float getScaleFactor() {
|
|
return getModel().getSize().scaleFactor();
|
|
}
|
|
|
|
public float getShadowSize() {
|
|
return getModel().getSize().shadowSize();
|
|
}
|
|
|
|
public double getNamePlateYOffset(T entity) {
|
|
|
|
// We start by negating the height calculation done by mahjong.
|
|
float y = -(entity.getHeight() + 0.5F);
|
|
|
|
// Then we add our own offsets.
|
|
y += getModel().getAttributes().visualHeight * getScaleFactor() + 0.25F;
|
|
|
|
if (entity.isSneaking()) {
|
|
y -= 0.25F;
|
|
}
|
|
|
|
if (entity.hasVehicle()) {
|
|
y += entity.getVehicle().getEyeHeight(entity.getPose());
|
|
}
|
|
|
|
if (entity.isSleeping()) {
|
|
y /= 2;
|
|
}
|
|
|
|
return y;
|
|
}
|
|
|
|
public interface RegistrationHandler {
|
|
SyncedPony getSyncedPony();
|
|
}
|
|
|
|
public static class SyncedPony {
|
|
@Nullable
|
|
private Pony lastRenderedPony;
|
|
@Nullable
|
|
private Pony lastTransmittedPony;
|
|
}
|
|
}
|