我的实体的模型没有正确地被渲染/显示

USS.Shenzhou


我使用的IDE:IDEA
我使用的IDE版本:2020.1
Forge版本: 28.2.3
Minecraft版本: 1.14.4
Mapping 文件版本: 20200119-1.14.4

我创建了一个ThrowAbleEntity,并尝试着为它添加一个模型。不幸的是,我尝试了超过一个星期仍然没有成功:这个实体显示为一个(符合我的size所定义的大小的)白色长方体。我感觉是我的renderer文件内容或者是它的注册出现了问题。
我的model文件:

@OnlyIn(Dist.CLIENT)
public class c4model extends EntityModel<c4entity> {
    public RendererModel boom;
    public RendererModel phone;

    public c4model(){
        this.textureHeight=14;
        this.textureWidth=14;
        boom = new RendererModel(this,0,0);
        boom.addBox(-0.4f,-1.9f,-0.4f,2,2,5,-0.6f);
        phone = new RendererModel(this,0,5);
        phone.addBox(-1.2f, -0.8f, 0f, 2, 1, 3, -0.4f);
        boom.addChild(phone);
    }

    @Override
    public void render(c4entity entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scale) {
        setRotationAngles(entityIn, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scale);
        boom.render(scale);
        super.render(entityIn, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scale);

    }

    @Override
    public void setRotationAngles(c4entity entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scaleFactor) {
        super.setRotationAngles(entityIn, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scaleFactor);
    }
}

我的renderer文件:

@OnlyIn(Dist.CLIENT)
public class c4renderer extends DefaultRenderer {
    public static final ResourceLocation TEXTURE_LOCATION = new ResourceLocation("rainbow6", "textures/entity/c4");
    private static final c4model c4model = new c4model();

    public c4renderer(EntityRendererManager renderManagerIn) {
        super(renderManagerIn);
    }

    public void doRender(c4entity entity, double x, double y, double z, float entityYaw, float partialTicks) {
        Tessellator tessellator = Tessellator.getInstance();
        GlStateManager.pushMatrix();
        GlStateManager.translated(x,y,z);
        GlStateManager.rotatef(entity.rotationYaw,0.0f,1.0f,0.0f);
        this.bindEntityTexture(entity);
        this.c4model.render(entity,0.0f,0.0f,0.0f,0.0f,0.0f,0.0625f);
        GlStateManager.popMatrix();
        super.doRender(entity, x, y, z, entityYaw, partialTicks);
        tessellator.draw();
    }

    
    @Nullable
    @Override
    protected ResourceLocation getEntityTexture(Entity entity) {
        return TEXTURE_LOCATION;
    }
}

注册renderer:

    @OnlyIn(Dist.CLIENT)
    public class ModRenderRegistry {
        public void registryEntityRenders(final FMLClientSetupEvent event){
            RenderingRegistry.registerEntityRenderingHandler(c4entity.class, c4renderer::new);

    }
}

FledgeXu


这个实体显示为一个(符合我的size所定义的大小的)白色长方体

这个具体是什么意思?
和你在这里定义的两个Box大小一致吗?
建议补充图片。

  boom = new RendererModel(this,0,0);
  boom.addBox(-0.4f,-1.9f,-0.4f,2,2,5,-0.6f);
  phone = new RendererModel(this,0,5);
  phone.addBox(-1.2f, -0.8f, 0f, 2, 1, 3, -0.4f);
  boom.addChild(phone);

FledgeXu


另外请把注册renderer部分的整个类的代码补充完整。


USS.Shenzhou


这个size指游戏中F3+B所看到的碰撞框(白线框),box指模型的各个部分(头、手等)


USS.Shenzhou


这是我的ModRendererRegistry.class:

package com.uss_shenzhou.rainbow6.entities;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.entity.SpriteRenderer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

@OnlyIn(Dist.CLIENT)
public class ModRenderRegistry {
   public void registryEntityRenders(final FMLClientSetupEvent event){
       RenderingRegistry.registerEntityRenderingHandler(c4entity.class, c4renderer::new);

   }
}

FledgeXu


看起来你的public void registryEntityRenders(final FMLClientSetupEvent event)根本没有调用到。


FledgeXu


FMLClientSetupEvent 是个事件。
如何注册使用事件处理器请看Forge文档。
https://mcforge-cn.readthedocs.io/zh/latest/events/intro/


FledgeXu


你应该请改成类似这样。

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class ModRenderRegistry {
    @SubscribeEvent
    public static void registryEntityRenders(final FMLClientSetupEvent event) {
        RenderingRegistry.registerEntityRenderingHandler(c4entity.class, c4renderer::new);
    }
}

USS.Shenzhou


(帖子被作者删除,如无标记将在 24 小时后自动删除)


USS.Shenzhou


我在发帖前已经尝试过此种方法,并在刚才又尝试了一遍,也没有起作用。在latest.log和debug.log中均没有任何警告或错误。断点结果也显示注册语句被执行了。


FledgeXu


那我觉得就是你DefaultRenderer的实现有问题,用原版实现好的类似于MobRenderer试试。


USS.Shenzhou


谢谢提醒!我把DeffaultRenderer改成了EntityRenderer<c4entity>,并在renderer文件去掉了
Tessellator tessellator = Tessellator.getInstance(); tessellator.draw();这两行。现在它的模型能被正确的显示,但是贴图仍然不正确:一些原版中的材质贴图被看起来很胡乱地画在了模型上,而不是我自己指定的图片。除上述之外,我的model文件没有改动,


FledgeXu


应该是你贴图和模型没有对应上,UV展开有问题。
这个就要自己慢慢调了,要你自己看情况修改贴图了。


USS.Shenzhou


如果是uv展开的话,至少会显示图片中的部分内容吧?但是现在显示的是一些原版的不相干的图片。为方便,图片发在群里。


USS.Shenzhou


补充


FledgeXu


这个图叫做atlas texture,看起来是你材质绑定错误了。


FledgeXu


你在doRender最后加上 super.doRender(entity, x, y, z, entityYaw, partialTicks);试试。


USS.Shenzhou


我刚刚尝试在 @Nullable @Override protected ResourceLocation getEntityTexture(c4entity entity) { return TEXTURE_LOCATION; }改成了return null,出现了同样的结果,似乎这个getEntityTexture并没有起作用。
另外,如何查看在什么情况下材质会变成atlas texture?


FledgeXu


你先下个断点试试,看看确实调用到了吗?


USS.Shenzhou


看起来没有 :sweat:


FledgeXu


你把修改过的Render代码粘贴上来。


USS.Shenzhou


package com.uss_shenzhou.rainbow6.entities;

import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.DefaultRenderer;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import javax.annotation.Nullable;

@OnlyIn(Dist.CLIENT)
public class c4renderer extends EntityRenderer<c4entity> {
    public static final ResourceLocation TEXTURE_LOCATION = new ResourceLocation("rainbow6:textures/entity/c4.png");
    private static final c4model c4model = new c4model();

    public c4renderer(EntityRendererManager renderManagerIn) {
        super(renderManagerIn);
    }

    public void doRender(c4entity entity, double x, double y, double z, float entityYaw, float partialTicks) {
        GlStateManager.pushMatrix();
        GlStateManager.translated(x,y,z);
        GlStateManager.rotatef(entity.rotationYaw,0.0f,1.0f,0.0f);
        c4model.render(entity,0.0f,0.0f,0.0f,0.0f,0.0f,0.0625f);
        GlStateManager.popMatrix();
        super.doRender(entity, x, y, z, entityYaw, partialTicks);
    }
    @Nullable
    @Override
    protected ResourceLocation getEntityTexture(c4entity entity) {
        return TEXTURE_LOCATION;
    }

}

FledgeXu


这行前面你忘了绑定材质了……

c4model.render(entity,0.0f,0.0f,0.0f,0.0f,0.0f,0.0625f);

这行前面加上

this.bindEntityTexture(entity);

USS.Shenzhou


感谢!虽然仍是错位的,不过终于能显示我自己的图片了。


system


该主题在最后一个回复创建后7天后自动关闭。不再允许新的回复。