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.


×
×
  • Create New...

Important Information

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