Skip to content

Commit

Permalink
Add friend entity model to test manual model loading and fix texture …
Browse files Browse the repository at this point in the history
…loading
  • Loading branch information
ashpieboop committed Feb 27, 2020
1 parent 4a752a5 commit 71807bb
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 11 deletions.
29 changes: 18 additions & 11 deletions foml-mod/src/main/java/nerdhub/foml/mixins/ModelLoaderMixin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package nerdhub.foml.mixins;

import nerdhub.foml.ManualModelLoaderRegistry;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.profiler.Profiler;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -15,15 +13,24 @@
@Mixin(ModelLoader.class)
public abstract class ModelLoaderMixin {

@Shadow protected abstract void addModel(ModelIdentifier modelId);
@Shadow
protected abstract void addModel(ModelIdentifier modelId);

@Inject(method = "<init>", at= @At(value = "RETURN"))
public void init(ResourceManager resourceManager, BlockColors blockColors, Profiler profiler, int i, CallbackInfo ci) {
profiler.push("manual_obj_models");
for (ModelIdentifier id : ManualModelLoaderRegistry.INSTANCE.getModels()) {
this.addModel(id);
@Shadow
@Final
public static ModelIdentifier MISSING;

/**
* This method is only called in the constructor of ModelLoader, this allows to inject models earlier than on the
* constructor return.
*/
@Inject(method = "addModel", at = @At("RETURN"))
public void injectModels(ModelIdentifier modelId, CallbackInfo ci) {
// Only inject models once
if (modelId == MISSING) {
for (ModelIdentifier id : ManualModelLoaderRegistry.INSTANCE.getModels()) {
this.addModel(id);
}
}
profiler.pop();
}

}
15 changes: 15 additions & 0 deletions foml-test/src/main/java/nerdhub/fomltest/FOMLTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package nerdhub.fomltest;

import nerdhub.foml.ManualModelLoaderRegistry;
import nerdhub.foml.obj.OBJLoader;
import nerdhub.fomltest.entity.FriendEntity;
import nerdhub.fomltest.entity.FriendEntityModel;
import nerdhub.fomltest.entity.FriendEntityRenderer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.block.FabricBlockSettings;
import net.fabricmc.fabric.api.client.rendereregistry.v1.EntityRendererRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.Material;
import net.minecraft.entity.EntityCategory;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
Expand All @@ -16,10 +24,17 @@ public class FOMLTest implements ModInitializer {
public static final String MODID = "fomltest";
public static Block TEST_BLOCK = new Block(FabricBlockSettings.of(Material.STONE).hardness(1.0f).build());

public static final Identifier FRIEND_ENTITY_ID = new Identifier(MODID, "friend");
public static final EntityType<FriendEntity> FRIEND_ENTITY_TYPE = new EntityType<>(FriendEntity::new, EntityCategory.MONSTER, true, true, false, true, EntityDimensions.fixed(0.5f, 1.8f));

@Override
public void onInitialize() {
OBJLoader.INSTANCE.registerDomain(MODID);
Registry.register(Registry.BLOCK, new Identifier(MODID, "test"), TEST_BLOCK);
Registry.register(Registry.ITEM, new Identifier(MODID, "test"), new BlockItem(TEST_BLOCK, new Item.Settings().group(ItemGroup.REDSTONE)));

ManualModelLoaderRegistry.INSTANCE.register(FriendEntityModel.MODEL);
Registry.register(Registry.ENTITY_TYPE, FRIEND_ENTITY_ID, FRIEND_ENTITY_TYPE);
EntityRendererRegistry.INSTANCE.register(FRIEND_ENTITY_TYPE, (dispatcher, context) -> new FriendEntityRenderer<>(dispatcher, new FriendEntityModel<>()));
}
}
11 changes: 11 additions & 0 deletions foml-test/src/main/java/nerdhub/fomltest/entity/FriendEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nerdhub.fomltest.entity;

import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.CreeperEntity;
import net.minecraft.world.World;

public class FriendEntity extends CreeperEntity {
public FriendEntity(EntityType<? extends FriendEntity> entityType, World world) {
super(entityType, world);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nerdhub.fomltest.entity;

import nerdhub.fomltest.FOMLTest;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.entity.model.EntityModel;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;

public class FriendEntityModel<T extends FriendEntity> extends EntityModel<T> {
public static final ModelIdentifier MODEL = new ModelIdentifier(new Identifier(FOMLTest.MODID, "entity/friend.obj"), null);

public FriendEntityModel() {
super(texture -> RenderLayer.getSolid());
}

@Override
public void setAngles(T entity, float limbAngle, float limbDistance, float customAngle, float headYaw, float headPitch) {
}

@Override
public void render(MatrixStack matrices, VertexConsumer vertexConsumer, int light, int overlay, float red, float green, float blue, float alpha) {
MatrixStack.Entry entry = matrices.peek();
BakedModel model = MinecraftClient.getInstance().getBakedModelManager().getModel(MODEL);
for (BakedQuad quad : model.getQuads(null, null, null)) {
vertexConsumer.quad(entry, quad, red, green, blue, light, overlay);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package nerdhub.fomltest.entity;

import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderDispatcher;
import net.minecraft.client.render.entity.MobEntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;

public class FriendEntityRenderer<T extends FriendEntity, M extends FriendEntityModel<T>> extends MobEntityRenderer<T, M> {
public FriendEntityRenderer(EntityRenderDispatcher dispatcher, M model) {
super(dispatcher, model, 1);
}

@Override
public void render(T mobEntity, float yaw, float tickDelta, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int overlay) {
matrixStack.push();
matrixStack.scale(-1.0F, -1.0F, 1.0F);
this.scale(mobEntity, matrixStack, tickDelta);
matrixStack.translate(0.0D, -1.5010000467300415D, 0.0D);
super.render(mobEntity, yaw, tickDelta, matrixStack, vertexConsumerProvider, overlay);
matrixStack.pop();
}

@Override
public Identifier getTexture(T entity) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Blender MTL File: 'friend.blend'
# Material Count: 1

newmtl Material
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Kd fomltest:entities/friend
150 changes: 150 additions & 0 deletions foml-test/src/main/resources/assets/fomltest/models/entity/friend.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Blender v2.82 (sub 7) OBJ File: 'friend.blend'
# www.blender.org
mtllib entity/friend.mtl
o Cube
v 0.250000 1.800000 -0.250000
v 0.250000 0.000000 -0.250000
v 0.250000 1.800000 0.250000
v 0.250000 0.000000 0.250000
v -0.250000 1.800000 -0.250000
v -0.250000 0.000000 -0.250000
v -0.250000 1.800000 0.250000
v -0.250000 0.000000 0.250000
v 0.250000 1.350000 -0.250000
v -0.250000 1.350000 0.250000
v 0.250000 1.350000 0.250000
v -0.250000 1.350000 -0.250000
v 0.250000 1.282500 -0.250000
v 0.250000 1.282500 0.250000
v -0.250000 1.282500 -0.250000
v -0.250000 1.282500 0.250000
v 0.250000 0.064125 -0.250000
v 0.250000 0.064125 0.250000
v -0.250000 0.064125 -0.250000
v -0.250000 0.064125 0.250000
v -0.225000 0.064125 0.225000
v -0.225000 1.282500 0.225000
v 0.225000 1.282500 0.225000
v -0.225000 1.282500 -0.225000
v 0.225000 1.282500 -0.225000
v 0.225000 0.064125 -0.225000
v 0.225000 0.064125 0.225000
v -0.225000 0.064125 -0.225000
vt 0.250000 0.750000
vt 0.500000 0.750000
vt 0.500000 1.000000
vt 0.250000 1.000000
vt 0.500000 0.500000
vt 0.500000 0.750000
vt 0.250000 0.750000
vt 0.250000 0.500000
vt 0.000000 0.500000
vt 0.250000 0.500000
vt 0.250000 0.750000
vt 0.000000 0.750000
vt 0.500000 0.000000
vt 0.750000 0.000000
vt 0.750000 0.250000
vt 0.500000 0.250000
vt 0.500000 0.500000
vt 0.750000 0.500000
vt 0.750000 0.750000
vt 0.500000 0.750000
vt 0.750000 0.500000
vt 1.000000 0.500000
vt 1.000000 0.750000
vt 0.750000 0.750000
vt 0.968750 0.250000
vt 1.000000 0.250000
vt 1.000000 0.500000
vt 0.968750 0.500000
vt 0.531250 0.125000
vt 0.562500 0.125000
vt 0.562500 0.375000
vt 0.531250 0.375000
vt 0.968750 0.000000
vt 1.000000 0.000000
vt 0.500000 0.125000
vt 0.531250 0.125000
vt 0.531250 0.375000
vt 0.500000 0.375000
vt 0.250000 0.343750
vt 0.500000 0.343750
vt 0.500000 0.375000
vt 0.250000 0.375000
vt 0.812500 0.093750
vt 0.812500 0.062500
vt 0.781250 0.062500
vt 0.781250 0.093750
vt 0.750000 0.343750
vt 1.000000 0.343750
vt 1.000000 0.375000
vt 0.750000 0.375000
vt 0.812500 0.125000
vt 0.781250 0.125000
vt 0.718750 0.250000
vt 0.750000 0.250000
vt 0.750000 0.500000
vt 0.718750 0.500000
vt 0.750000 0.750000
vt 0.718750 0.750000
vt 0.718750 0.000000
vt 0.750000 0.000000
vt 0.750000 1.000000
vt 0.718750 1.000000
vt 0.093750 0.125000
vt 0.843750 0.125000
vt 0.843750 0.375000
vt 0.093750 0.375000
vt 0.093750 -0.000000
vt 0.843750 -0.000000
vt 0.843750 0.250000
vt 0.093750 0.250000
vt 0.093750 -0.000000
vt 0.843750 -0.000000
vt 0.843750 0.250000
vt 0.093750 0.250000
vt 0.093750 0.125000
vt 0.843750 0.125000
vt 0.843750 0.375000
vt 0.093750 0.375000
vt -0.000000 0.343750
vt -0.000000 0.375000
vt 0.812500 0.031250
vt 0.812500 0.000000
vt 0.781250 0.000000
vt 0.781250 0.031250
vn 0.0000 1.0000 0.0000
vn 0.0000 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 -1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
usemtl Material
s off
f 1/1/1 5/2/1 7/3/1 3/4/1
f 11/5/2 3/6/2 7/7/2 10/8/2
f 10/9/3 7/10/3 5/11/3 12/12/3
f 6/13/4 2/14/4 4/15/4 8/16/4
f 9/17/5 1/18/5 3/19/5 11/20/5
f 12/21/6 5/22/6 1/23/6 9/24/6
f 15/25/6 12/26/6 9/27/6 13/28/6
f 13/29/5 9/30/5 11/31/5 14/32/5
f 16/33/3 10/34/3 12/26/3 15/25/3
f 14/35/2 11/36/2 10/37/2 16/38/2
f 15/39/4 13/40/4 25/41/4 24/42/4
f 18/43/1 17/44/1 26/45/1 27/46/1
f 14/47/4 16/48/4 22/49/4 23/50/4
f 20/51/1 18/43/1 27/46/1 21/52/1
f 6/53/6 19/54/6 17/55/6 2/56/6
f 2/56/5 17/55/5 18/57/5 4/58/5
f 8/59/3 20/60/3 19/54/3 6/53/3
f 4/58/2 18/57/2 20/61/2 8/62/2
f 27/63/2 23/64/2 22/65/2 21/66/2
f 21/67/3 22/68/3 24/69/3 28/70/3
f 26/71/5 25/72/5 23/73/5 27/74/5
f 28/75/6 24/76/6 25/77/6 26/78/6
f 16/79/4 15/39/4 24/42/4 22/80/4
f 19/81/1 20/82/1 21/83/1 28/84/1
f 17/44/1 19/81/1 28/84/1 26/45/1
f 13/40/4 14/47/4 23/50/4 25/41/4
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 71807bb

Please sign in to comment.