Skip to content

Commit

Permalink
Some model fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Nov 12, 2023
1 parent d6daf00 commit f5272e3
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ minecraft_range=[1.18.2,1.20)

# check these on https://fabricmc.net/versions.html
loader_version=0.14.22
fabric_version=0.87.0+1.20.1
fabric_version=0.90.7+1.20.1
parchment_version=2021.12.19

# Build dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DefaultRetexturedBlockEntity(BlockEntityType<?> type, BlockPos pos, Block

@Nonnull
@Override
public IModelData getRenderAttachmentData() {
public IModelData getRenderData() {
return this.data.get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package slimeknights.mantle.block.entity;

import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity;
import net.fabricmc.fabric.api.blockview.v2.RenderDataBlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand All @@ -17,7 +17,7 @@
*
* Use alongside {@link RetexturedBlock} and {@link slimeknights.mantle.item.RetexturedBlockItem}. See {@link DefaultRetexturedBlockEntity} for implementation.
*/
public interface IRetexturedBlockEntity extends RenderAttachmentBlockEntity {
public interface IRetexturedBlockEntity extends RenderDataBlockEntity {
/* Gets the Forge tile data for the tile entity */
default CompoundTag getTileData() {
return ((BlockEntity)this).getCustomData();
Expand Down Expand Up @@ -62,5 +62,5 @@ default IModelData getRetexturedModelData() {
}

@Override
@Nullable IModelData getRenderAttachmentData();
@Nullable IModelData getRenderData();
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package slimeknights.mantle.block.entity;

import io.github.fabricators_of_create.porting_lib.common.util.Lazy;
import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import slimeknights.mantle.client.model.data.IModelData;
import slimeknights.mantle.client.model.data.SinglePropertyData;
import slimeknights.mantle.util.RetexturedHelper;

import javax.annotation.Nonnull;
Expand All @@ -20,7 +17,7 @@
* @deprecated use {@link DefaultRetexturedBlockEntity}
*/
@Deprecated
public class RetexturedBlockEntity extends MantleBlockEntity implements IRetexturedBlockEntity, RenderAttachmentBlockEntity {
public class RetexturedBlockEntity extends MantleBlockEntity implements IRetexturedBlockEntity {
/** Lazy value of model data as it will not change after first fetch */
private final Lazy<IModelData> data = Lazy.of(this::getRetexturedModelData);
public RetexturedBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
Expand All @@ -29,7 +26,7 @@ public RetexturedBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState s

@Nonnull
@Override
public IModelData getRenderAttachmentData() {
public IModelData getRenderData() {
return data.get();
}

Expand Down
42 changes: 22 additions & 20 deletions src/main/java/slimeknights/mantle/client/model/RetexturedModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.datafixers.util.Either;
import io.github.fabricators_of_create.porting_lib.models.CustomParticleIconModel;
import io.github.fabricators_of_create.porting_lib.models.geometry.IGeometryLoader;
import io.github.fabricators_of_create.porting_lib.models.geometry.IUnbakedGeometry;
import lombok.AccessLevel;
Expand Down Expand Up @@ -37,6 +38,7 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import slimeknights.mantle.client.model.data.IModelData;
import slimeknights.mantle.client.model.data.SinglePropertyData;
import slimeknights.mantle.client.model.util.ColoredBlockModel;
import slimeknights.mantle.client.model.util.DynamicBakedWrapper;
Expand Down Expand Up @@ -151,7 +153,7 @@ public static Set<String> getRetextured(JsonObject json) {
}

/** Baked variant of the model, used to swap out quads based on the texture */
public static class Baked extends DynamicBakedWrapper<BakedModel> {
public static class Baked extends DynamicBakedWrapper<BakedModel> implements CustomParticleIconModel {
/** Cache of texture name to baked model */
private final Map<ResourceLocation,BakedModel> cache = new ConcurrentHashMap<>();
/* Properties for rebaking */
Expand Down Expand Up @@ -191,28 +193,30 @@ private BakedModel getCachedModel(Block block) {
return cache.computeIfAbsent(ModelHelper.getParticleTexture(block), this::getRetexturedModel);
}

// TODO: PORT
// @Override
// public TextureAtlasSprite getParticleIcon(IModelData data) {
// // if particle is retextured, fetch particle from the cached model
// if (retextured.contains("particle")) {
// Block block = data.getData(RetexturedHelper.BLOCK_PROPERTY);
// if (block != null) {
// return getCachedModel(block).getParticleIcon(data);
// }
// }
// return wrapped.getParticleIcon(data);
// }
@Override
public TextureAtlasSprite getParticleIcon(Object obj) {
// if particle is retextured, fetch particle from the cached model
if (retextured.contains("particle") && obj instanceof IModelData data) {
Block block = data.getData(RetexturedHelper.BLOCK_PROPERTY);
if (block != null) {
var cached = getCachedModel(block);
return cached instanceof CustomParticleIconModel cachedParticle ? cachedParticle.getParticleIcon(data) : cached.getParticleIcon();
}
}
if (wrapped instanceof CustomParticleIconModel customParticleIconModel)
return customParticleIconModel.getParticleIcon(obj);
return wrapped.getParticleIcon();
}

@Override
public void emitBlockQuads(BlockAndTintGetter blockView, BlockState state, BlockPos pos, Supplier<RandomSource> randomSupplier, RenderContext context) {
if(blockView instanceof RenderAttachedBlockView renderAttachedBlockView && renderAttachedBlockView.getBlockEntityRenderAttachment(pos) instanceof SinglePropertyData data) {
Block block = (Block) data.getData(RetexturedHelper.BLOCK_PROPERTY);
if(blockView.getBlockEntityRenderData(pos) instanceof IModelData data) {
Block block = data.getData(RetexturedHelper.BLOCK_PROPERTY);
if (block == null) {
super.emitBlockQuads(blockView, state, pos, randomSupplier, context);
return;
}
((FabricBakedModel)getCachedModel(block)).emitBlockQuads(blockView, state, pos, randomSupplier, context);
getCachedModel(block).emitBlockQuads(blockView, state, pos, randomSupplier, context);
} else {
super.emitBlockQuads(blockView, state, pos, randomSupplier, context);
}
Expand All @@ -233,7 +237,7 @@ public void emitItemQuads(ItemStack stack, Supplier<RandomSource> randomSupplier
}

// if valid, use the block
((FabricBakedModel)getCachedModel(block)).emitItemQuads(stack, randomSupplier, context);
getCachedModel(block).emitItemQuads(stack, randomSupplier, context);
}

@Override
Expand Down Expand Up @@ -298,9 +302,7 @@ public BakedModel resolve(BakedModel originalModel, ItemStack stack, @Nullable C
}

// if valid, use the block
if (originalModel instanceof Baked model)
return model.getCachedModel(block);
return ((Baked) ((WrapperBakedModel) originalModel).getWrappedModel()).getCachedModel(block);
return ModelHelper.unwrap(originalModel, Baked.class).getCachedModel(block);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.mojang.math.Transformation;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.fabricmc.fabric.api.renderer.v1.model.WrapperBakedModel;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BakedQuad;
Expand Down Expand Up @@ -68,7 +69,7 @@ public static <T extends BakedModel> T getBakedModel(BlockState state, Class<T>
if (minecraft == null) {
return null;
}
BakedModel baked = minecraft.getModelManager().getBlockModelShaper().getBlockModel(state);
BakedModel baked = unwrap(minecraft.getModelManager().getBlockModelShaper().getBlockModel(state), clazz);
// map multipart and weighted random into the first variant
if (baked instanceof MultiPartBakedModel) {
baked = ((MultiPartBakedModel)baked).selectors.get(0).getRight();
Expand Down Expand Up @@ -97,7 +98,7 @@ public static <T extends BakedModel> T getBakedModel(ItemLike item, Class<T> cla
if (minecraft == null) {
return null;
}
BakedModel baked = minecraft.getItemRenderer().getItemModelShaper().getItemModel(item.asItem());
BakedModel baked = unwrap(minecraft.getItemRenderer().getItemModelShaper().getItemModel(item.asItem()), clazz);
if (clazz.isInstance(baked)) {
return clazz.cast(baked);
}
Expand Down Expand Up @@ -218,4 +219,27 @@ public static int getRotation(JsonObject json, String key) {
// return ((BakedQuadBuilder) this.parent).build();
// }
// }

/**
* Fully unwrap a model, i.e. return the innermost model.
*/
public static <T extends BakedModel> T unwrap(BakedModel model, Class<T> modelClass) {
while (model instanceof WrapperBakedModel wrapper) {
if (modelClass.isAssignableFrom(model.getClass()))
return (T) model;
BakedModel wrapped = wrapper.getWrappedModel();

if (wrapped == null) {
return (T) model;
} else if (wrapped == model) {
throw new IllegalArgumentException("Model " + model + " is wrapping itself!");
} else {
model = wrapped;
}
}
if (!modelClass.isAssignableFrom(model.getClass()))
throw new RuntimeException("Trying to unwrap " + model + " that isn't assignable to " + modelClass);

return (T) model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.math.Transformation;
import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import org.joml.Quaternionf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static <T extends BlockEntity & IRetexturedBlockEntity> void onTextureUpd
if (level != null && level.isClientSide) {
Block texture = self.getTexture();
texture = texture == Blocks.AIR ? null : texture;
IModelData data = self.getRetexturedModelData();
IModelData data = self.getRenderData();
if (data.getData(BLOCK_PROPERTY) != texture) {
data.setData(BLOCK_PROPERTY, texture);
BlockState state = self.getBlockState();
Expand Down

0 comments on commit f5272e3

Please sign in to comment.