实现LOD模型渲染

This commit is contained in:
Light_Quanta 2025-06-02 02:12:20 +08:00
parent bcbf5d7e87
commit bca2714a78
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
3 changed files with 50 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import com.atsuishio.superbwarfare.client.molang.MolangVariable;
import com.atsuishio.superbwarfare.data.gun.GunData; import com.atsuishio.superbwarfare.data.gun.GunData;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemDisplayContext; import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import software.bernie.geckolib.animatable.GeoAnimatable; import software.bernie.geckolib.animatable.GeoAnimatable;
@ -18,6 +19,14 @@ import java.util.function.DoubleSupplier;
public abstract class CustomGunModel<T extends GunItem & GeoAnimatable> extends GeoModel<T> { public abstract class CustomGunModel<T extends GunItem & GeoAnimatable> extends GeoModel<T> {
public ResourceLocation getLODModelResource(T animatable) {
return this.getModelResource(animatable);
}
public ResourceLocation getLODTextureResource(T animatable) {
return this.getTextureResource(animatable);
}
@Override @Override
public void applyMolangQueries(AnimationState<T> animationState, double animTime) { public void applyMolangQueries(AnimationState<T> animationState, double animTime) {
Minecraft mc = Minecraft.getInstance(); Minecraft mc = Minecraft.getInstance();

View file

@ -30,11 +30,21 @@ public class Mp5ItemModel extends CustomGunModel<Mp5Item> {
return Mod.loc("geo/mp_5.geo.json"); return Mod.loc("geo/mp_5.geo.json");
} }
@Override
public ResourceLocation getLODModelResource(Mp5Item animatable) {
return Mod.loc("geo/lod/mp_5.geo.json");
}
@Override @Override
public ResourceLocation getTextureResource(Mp5Item animatable) { public ResourceLocation getTextureResource(Mp5Item animatable) {
return Mod.loc("textures/item/mp_5.png"); return Mod.loc("textures/item/mp_5.png");
} }
@Override
public ResourceLocation getLODTextureResource(Mp5Item animatable) {
return Mod.loc("textures/item/lod/mp_5.png");
}
@Override @Override
public void setCustomAnimations(Mp5Item animatable, long instanceId, AnimationState<Mp5Item> animationState) { public void setCustomAnimations(Mp5Item animatable, long instanceId, AnimationState<Mp5Item> animationState) {
Player player = Minecraft.getInstance().player; Player player = Minecraft.getInstance().player;

View file

@ -1,12 +1,14 @@
package com.atsuishio.superbwarfare.client.renderer; package com.atsuishio.superbwarfare.client.renderer;
import com.atsuishio.superbwarfare.client.model.item.CustomGunModel;
import com.atsuishio.superbwarfare.config.client.DisplayConfig;
import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemDisplayContext; import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -19,7 +21,7 @@ import software.bernie.geckolib.renderer.GeoItemRenderer;
import software.bernie.geckolib.util.Color; import software.bernie.geckolib.util.Color;
import software.bernie.geckolib.util.RenderUtil; import software.bernie.geckolib.util.RenderUtil;
public class CustomGunRenderer<T extends Item & GeoAnimatable> extends GeoItemRenderer<T> { public class CustomGunRenderer<T extends GunItem & GeoAnimatable> extends GeoItemRenderer<T> {
public static final float SCALE_RECIPROCAL = 1.0f / 16.0f; public static final float SCALE_RECIPROCAL = 1.0f / 16.0f;
@ -56,13 +58,39 @@ public class CustomGunRenderer<T extends Item & GeoAnimatable> extends GeoItemRe
return RenderType.entityTranslucent(getTextureLocation(animatable)); return RenderType.entityTranslucent(getTextureLocation(animatable));
} }
@Override
public ResourceLocation getTextureLocation(T animatable) {
var geoModel = getGeoModel();
if (renderPerspective != ItemDisplayContext.FIRST_PERSON_RIGHT_HAND
&& DisplayConfig.ENABLE_GUN_LOD.get()
&& geoModel instanceof CustomGunModel<T> gunModel
) {
return gunModel.getLODTextureResource(animatable);
}
return geoModel.getTextureResource(animatable);
}
@Override @Override
public void defaultRender(PoseStack poseStack, T animatable, MultiBufferSource bufferSource, @Nullable RenderType renderType, @Nullable VertexConsumer buffer, float yaw, float partialTick, int packedLight) { public void defaultRender(PoseStack poseStack, T animatable, MultiBufferSource bufferSource, @Nullable RenderType renderType, @Nullable VertexConsumer buffer, float yaw, float partialTick, int packedLight) {
poseStack.pushPose(); poseStack.pushPose();
Color renderColor = getRenderColor(animatable, partialTick, packedLight); Color renderColor = getRenderColor(animatable, partialTick, packedLight);
int packedOverlay = getPackedOverlay(animatable, 0, partialTick); int packedOverlay = getPackedOverlay(animatable, 0, partialTick);
BakedGeoModel model = getGeoModel().getBakedModel(getGeoModel().getModelResource(animatable));
ResourceLocation modelLocation;
var geoModel = getGeoModel();
if (renderPerspective != ItemDisplayContext.FIRST_PERSON_RIGHT_HAND
&& DisplayConfig.ENABLE_GUN_LOD.get()
&& geoModel instanceof CustomGunModel<T> gunModel
) {
modelLocation = gunModel.getLODModelResource(animatable);
} else {
modelLocation = geoModel.getModelResource(animatable);
}
BakedGeoModel model = geoModel.getBakedModel(modelLocation);
if (renderType == null) if (renderType == null)
renderType = getRenderType(animatable, getTextureLocation(animatable), bufferSource, partialTick); renderType = getRenderType(animatable, getTextureLocation(animatable), bufferSource, partialTick);