Jump to content

[1.11.2]Load an obj model and render it manually


Maccaronne

Recommended Posts

Hi there! I'm hosting a spigot 1.11 server, and I'm making a client mod which changes the model of entities with certain names.

Since it is a client-side mod, I can't bind the model on a custom Item class like most tutorials. :( 

I've figured a way to load and bake the model, cache it, then render it in RenderLivingEvent.Pre event. Here's the result, a horse "disguised" as the model:

fe1dd69f1bcad4e9.png

The only problem here is, obviously, the model won't be loaded, everything else works like a charm.

 

I have uploaded all relevant code and assets to a repository: https://github.com/SmilingTrickster/rendercloudmodel

 

Here's where the model is loaded and cached: https://github.com/SmilingTrickster/rendercloudmodel/blob/master/java/com/mcorg/maccaronne/client/resources/model/ModelResource.java#L44

Here's the rendering code entry: https://github.com/SmilingTrickster/rendercloudmodel/blob/master/java/com/mcorg/maccaronne/client/equip/MountModelRenderer.java#L60

Any help is greatly appreciated :)

Edited by Maccaronne
Link to comment
Share on other sites

Don't use Minecraft::getItemModelMesher. Prone to issues. Forge has since several versions now included the ModelLoader as a more reliable Model handler.

 

This is how I manually load a (normal) IBakedModel.

To load a .OBJ model, you need to call OBjLoader::loadModel instead of ModelLoaderRegistry::getModel.

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

7 hours ago, Matryoshika said:

Don't use Minecraft::getItemModelMesher. Prone to issues. Forge has since several versions now included the ModelLoader as a more reliable Model handler.

 

This is how I manually load a (normal) IBakedModel.

To load a .OBJ model, you need to call OBjLoader::loadModel instead of ModelLoaderRegistry::getModel.

After five hours of trial and errors, now at least something is rendered!

Spoiler

 



public class ModelResource {
    private static BlockColors blockColors;

    public static ModelResource CLOUD;
    public static ModelResource PIG_FANCY;

    static {
        OBJLoader.INSTANCE.addDomain("eodmod");
        CLOUD = new ModelResource("cloud");
        PIG_FANCY = new ModelResource("pig_fancy");
        blockColors = ReflectionHelper.getPrivateValue(BlockModelRenderer.class, Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer(), 0);
    }


    public ModelResource(String resourceLocation) {
        this.resourceLocation = resourceLocation;
        try {
            getBakedModel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String resourceLocation;
    private IModel model;
    private IBakedModel bakedModel;

    private IBakedModel getBakedModel() {
        if (bakedModel == null) {
            try {
                model = OBJLoader.INSTANCE.loadModel(new ResourceLocation("eodmod", "models/" + resourceLocation + ".obj"));
                bakedModel = model.bake(TRSRTransformation.identity(), DefaultVertexFormats.ITEM,
                        DefaultTextureGetter.INSTANCE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bakedModel;
    }

    private enum DefaultTextureGetter implements Function<ResourceLocation, TextureAtlasSprite> {
        INSTANCE;

        public TextureAtlasSprite apply(ResourceLocation location) {
//            return Minecraft.getMinecraft().getTextureMapBlocks().getMissingSprite();
            return Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(location.toString());
        }
    }

    boolean message = false;

    public void renderModel(Entity entity, double offsetX, double offsetY, double offsetZ, double pitch, double yaw) {
        GlStateManager.pushMatrix();
        GlStateManager.pushAttrib();
        GlStateManager.color(1f, 1f, 1f, 1f);
        GlStateManager.rotate((float) -yaw, 0.0F, 1.0F, 0.0F);
        GlStateManager.rotate((float) pitch, 0.0F, 1.0F, 0.0F);

        World world = entity.world;
        BlockPos pos = new BlockPos(0, 0, 0);

        Tessellator tessellator = Tessellator.getInstance();
        VertexBuffer buffer = tessellator.getBuffer();
        BlockPos blockPos = new BlockPos(offsetX, offsetY, offsetZ);
        IBlockState blockState = world.getBlockState(blockPos);
        List<BakedQuad> quads = bakedModel.getQuads(blockState, null, 0);
        if (quads.isEmpty()) return;
        VertexFormat format = quads.get(0).getFormat();

        tessellator.getBuffer().begin(GL11.GL_QUADS, format);
        Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelRenderer().renderModelSmooth(
                world,
                getBakedModel(),
                blockState,
                pos,
                buffer, false, 0);
        tessellator.draw();

        GlStateManager.color(1f, 1f, 1f, 1f);
        GlStateManager.popAttrib();
        GlStateManager.popMatrix();
    }
}

 

 

However, some faces of the model are missing, and the texture is missing.

I wonder how I suppose to use 'Function<ResourceLocation, TextureAtlasSprite>' in IModel::bake? Where should the png file be?

 

Some debug message from OBJLoader pops up when the model is loaded:

Spoiler

[17:24:16] [Client thread/INFO] [FML]: OBJLoader: Domain eodmod has been added.
[17:24:16] [Client thread/INFO] [FML]: OBJLoader.MaterialLibrary: key '' (model: 'eodmod:models/block/cloud.mtl') is not currently supported, skipping
[17:24:16] [Client thread/INFO] [FML]: OBJLoader.Parser: command 's' (model: 'eodmod:models/block/cloud.obj') is not currently supported, skipping. Line: 1357 's 1'
[17:24:16] [Client thread/INFO]: [STDOUT]: Quads size: 864

 

The obj model is exported from 3dsmax. Here's how it's supposed to look like

Edited by Maccaronne
Link to comment
Share on other sites

  • 10 months later...

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.