Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Rendering custom obj model


Recommended Posts

Posted (edited)

I have a block with a tile entity, which has a regular cube model. I want to render a custom obj model on top of it with the specific rotation, so it can't be static. I managed to load and bake the model with my ModelLoader but I don't know how to render it, i.e. what to put in the TileEntityRenderer#render method.

Edited by BudRunBun
Link to post
Share on other sites
Posted (edited)

I don't know if it's possible to draw a quarter of a sphere via json models. I think you can only make a model out of cuboids.

Edited by BudRunBun
Link to post
Share on other sites
Posted (edited)

I tried BlockModelRenderer#renderMode(looked how PistonTileEntity does it) and that's what I got:png.png

I have several problems with it:

  • It renders only from specific angle: if I move a little bit or look somewhere else it disappears
  • It can be seen through blocks
  • I don't know how to scale it properly(I tried GlStateManagel#scalef but it didn't do anything)
  • It's very dark and has some problems with lightning
Edited by BudRunBun
Link to post
Share on other sites

This is my code:

package com.budrunbun.lavalamp.renderer;

import com.budrunbun.lavalamp.tileentity.TurretTileEntity;
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.*;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ModelResourceLocation;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelDataManager;
import net.minecraftforge.client.model.data.IModelData;

import javax.annotation.Nonnull;
import java.util.Random;

public class TurretRenderer extends TileEntityRenderer<TurretTileEntity> {
    private static final BlockRendererDispatcher blockRenderer = Minecraft.getInstance().getBlockRendererDispatcher();
    private IBakedModel sphereQuarter;

    @Override
    public void render(@Nonnull TurretTileEntity turret, double x, double y, double z, float partialTicks, int destroyStage) {
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder buffer = tessellator.getBuffer();
        BlockPos pos = turret.getPos();
        World world = this.getWorld();

        RenderHelper.disableStandardItemLighting();
        GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
        GlStateManager.enableBlend();
        GlStateManager.disableCull();
        if (Minecraft.isAmbientOcclusionEnabled())
            GlStateManager.shadeModel(7425);
        else
            GlStateManager.shadeModel(7424);

        BlockModelRenderer.enableCache();

        buffer.begin(7, DefaultVertexFormats.BLOCK);
        buffer.setTranslation(x - pos.getX(), y - pos.getY(), z - pos.getZ());

        if (sphereQuarter == null)
            sphereQuarter = Minecraft.getInstance().getModelManager().getModel(new ModelResourceLocation("lavalamp:sphere_quarter", ""));
        IModelData data = ModelDataManager.getModelData(world, pos);

        blockRenderer.getBlockModelRenderer().renderModel(
                world,
                this.sphereQuarter,
                turret.getBlockState(),
                turret.getPos(),
                buffer,
                false,
                new Random(),
                42,
                data
        );

        buffer.setTranslation(0.0D, 0.0D, 0.0D);
        tessellator.draw();

        BlockModelRenderer.disableCache();
        RenderHelper.enableStandardItemLighting();
    }
}

 

Link to post
Share on other sites
Posted (edited)
1 hour ago, BudRunBun said:

I don't know if it's possible to draw a quarter of a sphere via json models. I think you can only make a model out of cuboids.

Forge has custom model loader support through jsons. For example, there is a model loader for .obj models that takes in the location of the obj model and its material. The omnibus PR has more information on this.

 

As for the actual code, you shouldn't have any reason to refer to RenderSystem at all in a TER. The batch rendering system used by #renderModel should be perfectly capable of handling the shading and culling of your model.

Edited by ChampionAsh5357
Link to post
Share on other sites

Okay, I got rid of unnecessary calls to RenderSystem but how do I fix that strange behaviour? I have no idea how to even scale the model via buffer and I can't find any examples.

Link to post
Share on other sites
56 minutes ago, BudRunBun said:

Okay, I got rid of unnecessary calls to RenderSystem but how do I fix that strange behaviour? I have no idea how to even scale the model via buffer and I can't find any examples.

That's the point of the MatrixStack. It's a projection matrix transformation for how to draw the element to the screen. So, you can just call #scale on it to scale the entries drawn in this particular case.

Link to post
Share on other sites
Posted (edited)

I've dealt with the matrix stack before with the ItemRenderer and there glScalef worked fine but here it just ignores it. I think that I might be transforming not the matrix that BlockModelRenderer uses. Where should I get the right one?

Edited by BudRunBun
Link to post
Share on other sites

I just realized you are using a non-supported version of Forge based on the parameters of the render method as they are pre-1.15. The only versions supported are those in the blue banner at the top of the screen. Please update to one of the above versions to receive more support.

Link to post
Share on other sites
Guest
This topic is now closed to further replies.


  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • i currently try to find out how the world gen works, at the moment im working on a structure but i have some problems with the generation. i use this  as an example, i created everything for the generation, but the structure is not generating this is my structure this is the structure registry this is my world load event and this my biome loading event thanks for replies
    • I'm currently trying to make a new cauldron-type block, and I'm having difficulty implementing blockstates. Here's the relevant part of the alchemical cauldron's code:   public class AlchemicalCauldronBlock extends Block { public static final IntegerProperty LEVEL = IntegerProperty.create("level", 1, 3); private static final VoxelShape INSIDE = box(2.0D, 4.0D, 2.0D, 14.0D, 16.0D, 14.0D); protected static final VoxelShape SHAPE = VoxelShapes.join(VoxelShapes.block(), VoxelShapes.or(box(0.0D, 0.0D, 4.0D, 16.0D, 3.0D, 12.0D), box(4.0D, 0.0D, 0.0D, 12.0D, 3.0D, 16.0D), box(2.0D, 0.0D, 2.0D, 14.0D, 3.0D, 14.0D), INSIDE), IBooleanFunction.ONLY_FIRST); public AlchemicalCauldronBlock() { super(Properties.of(Material.METAL).sound(SoundType.METAL).harvestLevel(3).strength(2F,2F)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 0)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, Integer.valueOf(0))); } ... When I try to run this with either of the two attempts at registerDefaultState uncommented, I get this error: https://pastebin.com/vPf3UryC. For convenience, I believe this part is most relevant: How can I avoid this? My registry classes look like this, if it helps: public class registry { public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, AlchemyPlus.MODID); public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, AlchemyPlus.MODID); public static void register() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); BLOCKS.register(modEventBus); ITEMS.register(modEventBus); registerItems.register(); registerBlocks.register(); } } public class registerBlocks { public static final RegistryObject<Block> ALCHEMICAL_CAULDRON = register("alchemical_cauldron", AlchemicalCauldronBlock::new); static void register() {} private static <T extends Block> RegistryObject<T> registerNoItem(String name, Supplier<T> block) { return registry.BLOCKS.register(name, block); } private static <T extends Block> RegistryObject<T> register(String name, Supplier<T> block) { RegistryObject<T> ret = registerNoItem(name, block); registry.ITEMS.register(name, () -> new BlockItem(ret.get(), new Item.Properties().tab(ItemGroup.TAB_BREWING))); return ret; } } Full GitHub repository available here.   (Other minor problem(?):  I copied the methods getShape and getInteractionShape from net.minecraft.block.CauldronBlock and I'm getting these weird warnings. What's up with that? Do I need to worry about it?)  
    • Personally I use IForgeRegistryEntry#getRegistryName and then ResourceLocation#getNameSpace to get the id of the object.
    • you can get a block form a ResourceLocation, take a look at NBTUtil#readBlockState, do somthing like that
    • I am running forge version 1.16.5 36.1.31 on a server with a lot of mods and when a player refreshes the server list, it sends a long error in the server console. Additionally, the server list displays "Can't connect to server", despite refreshing and actually being able to connect to the server. here's the links to the logs: https://gist.github.com/WaffleTraits/83885b1539d5711fabff73a91924e665 https://gist.github.com/WaffleTraits/5ae78e6eaa330422bbc9f7226faf60ce Here's what's displayed in console when a player refreshes server listing: https://hastebin.com/ativexeyoy.properties server is running on ubuntu 20.04 and is using java 8 for this forge instance.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.