Jump to content

[1.19] Baked Model working in inventory but not in world


JCox

Recommended Posts

Hi, I have been closely following along with McJty's 1.18 tutorials. I am trying to make my own baked model in which a blockstate can be rendered inside. So far I have been able to render the actual model and textures but when I want to render a second blockstate inside the model it only appears in the hotbar, inventory and as a dropped item, when placing it on the ground as a block, it doesn't render.

(For debugging purposes, I have set the blockstate to render as an iron block)

I will include some pictures to better explain:8Fx9JF4.jpg

As you can see, the iron block is only visible when the item is dropped (or in the inventory) and not when it is placed as a block.

Files and Git Repo
https://github.com/JCox06/AdvancedFarming

Registration is done under the setup/Registration
The PlantVessel block and tile entity ascioated with the baked model are in block/PlantVessel and be/PlantVesselBE
The following file below is the bakedmodel (for more context this file is under the cient/model package)

package uk.co.jcox.advancedfarming.client.model;

import com.mojang.math.Matrix4f;
import com.mojang.math.Transformation;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.client.model.IDynamicBakedModel;
import net.minecraftforge.client.model.IQuadTransformer;
import net.minecraftforge.client.model.QuadTransformers;
import net.minecraftforge.client.model.data.ModelData;
import uk.co.jcox.advancedfarming.block.PlantVessel;
import uk.co.jcox.advancedfarming.util.ClientTools;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import static uk.co.jcox.advancedfarming.util.ClientTools.v;

public class PlantVesselBakedModel implements IDynamicBakedModel {

    private final ModelState modelState;
    private final Function<Material, TextureAtlasSprite> spriteGetter;
    private List<BakedQuad> quads = new ArrayList<>();
    private final ItemOverrides overrides;
    private final ItemTransforms itemTransforms;


    public PlantVesselBakedModel(ModelState modelState, Function<Material, TextureAtlasSprite> spriteGetter, ItemOverrides overrides, ItemTransforms itemTransforms) {
        this.modelState = modelState;
        this.spriteGetter = spriteGetter;
        this.overrides = overrides;
        this.itemTransforms = itemTransforms;

        generateQuadCache();
    }


    @Override
    public List<BakedQuad> getQuads(@Nullable BlockState state, @Nullable Direction side, @Nonnull RandomSource rand, @Nonnull ModelData extraData, @Nullable RenderType layer) {


        if (side != null || (layer != null && layer.equals(RenderType.solid()))) {
            return Collections.emptyList();
        }

        List<BakedQuad> combined = new ArrayList<>(quads);
        combined.addAll(getQuadsOfIncubatingBlock(state, rand, extraData, layer));
        return combined;
    }

    private void generateQuadCache() {
        Transformation rotation = modelState.getRotation();

        TextureAtlasSprite textureSide = spriteGetter.apply(PlantVesselModelLoader.MATERIAL_SIDE);
        TextureAtlasSprite textureBase = spriteGetter.apply(PlantVesselModelLoader.MATERIAL_BOTTOM);
        TextureAtlasSprite textureOutsideBase = spriteGetter.apply(PlantVesselModelLoader.MATERIAL_BASE);
        TextureAtlasSprite textureBottomBase = spriteGetter.apply(PlantVesselModelLoader.MATERIAL_BOTTOM_BASE);

        float l = 2f / 16f;
        float r = 14f / 16f;

        float a = 1;
        float b = 0;

        //Base
        var quads = new ArrayList<BakedQuad>();
        quads.add(ClientTools.createQuad(v(r, r, r), v(r, r, l), v(l, r, l), v(l, r, r), rotation, textureSide));      // Top side
        quads.add(ClientTools.createQuad(v(a, l, a), v(a, l, b), v(b, l, b), v(b, l, a), rotation, textureBase));      // Inside texture
        quads.add(ClientTools.createQuad(v(r, r, r), v(r, l, r), v(r, l, l), v(r, r, l), rotation, textureSide));
        quads.add(ClientTools.createQuad(v(l, r, l), v(l, l, l), v(l, l, r), v(l, r, r), rotation, textureSide));
        quads.add(ClientTools.createQuad(v(r, r, l), v(r, l, l), v(l, l, l), v(l, r, l), rotation, textureSide));
        quads.add(ClientTools.createQuad(v(l, r, r), v(l, l, r), v(r, l, r), v(r, r, r), rotation, textureSide));


        quads.add(ClientTools.createQuad(v(b, b, b), v(a, b, b), v(a, b, a), v(b, b, a), rotation, textureOutsideBase));       //Outside

        quads.add(ClientTools.createQuad(v(b, l, b), v(b, b, b), v(b, b, a), v(b, l, a), rotation, textureBottomBase));
        quads.add(ClientTools.createQuad(v(a, l, b), v(a, b, b), v(b, b, b), v(b, l, b), rotation, textureBottomBase));
        quads.add(ClientTools.createQuad(v(b, l, a), v(b, b, a), v(a, b, a), v(a, l, a), rotation, textureBottomBase));
        quads.add(ClientTools.createQuad(v(a, l, a), v(a, b, a), v(a, b, b), v(a, l, b), rotation, textureBottomBase));

        this.quads = quads;
    }


    private List<BakedQuad> getQuadsOfIncubatingBlock(@Nullable BlockState state, @Nullable RandomSource rand, @Nullable ModelData extraData, RenderType layer) {
        List<BakedQuad> incubatingBlockQuads = new ArrayList<>();

        BlockState crop = Blocks.IRON_BLOCK.defaultBlockState();

        if (crop != null && !(crop.getBlock() instanceof PlantVessel)) {
            if (layer == null || getRenderTypes(crop, rand, extraData).contains(layer)) {
                BakedModel model = Minecraft.getInstance().getBlockRenderer().getBlockModelShaper().getBlockModel(crop);

                try {
                    Transformation translate = new Transformation(Matrix4f.createScaleMatrix(0.5f, 0.5f, 0.5f));
                    IQuadTransformer transformer = QuadTransformers.applying(translate);

                    //Get the quad of every side, transform it, and add it to the list of quads to render
                    for (Direction s : Direction.values()) {
                        List<BakedQuad> modelQuads = model.getQuads(crop, s, rand, ModelData.EMPTY, layer);
                        for (BakedQuad quad : modelQuads) {
                            incubatingBlockQuads.add(transformer.process(quad));
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return incubatingBlockQuads;
    }

    @Override
    public boolean usesBlockLight() {
        return false;
    }

    @Override
    public boolean useAmbientOcclusion() {
        return false;
    }

    @Override
    public boolean isGui3d() {
        return false;
    }

    @Override
    public boolean isCustomRenderer() {
        return false;
    }

    @Override
    public TextureAtlasSprite getParticleIcon() {
        return this.spriteGetter.apply(PlantVesselModelLoader.MATERIAL_SIDE);
    }

    @Override
    public ItemOverrides getOverrides() {
        return this.overrides;
    }

    @Override
    public ItemTransforms getTransforms() {
        return this.itemTransforms;
    }
}


If anyone has an idea why this is happening that would be greatly appreciated. Thank you.

Edited by JCox
Add mc version
Link to comment
Share on other sites

  • JCox changed the title to [1.19] Baked Model working in inventory but not in world

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.