Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: obj loader not rendering parts with the same name [1.21.4] #1759

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
package net.neoforged.neoforge.client.model.obj;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.mojang.math.Transformation;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -55,7 +56,7 @@ public class ObjModel extends AbstractUnbakedModel {
new Vec2(1, 0),
};

private final Map<String, ModelGroup> parts = Maps.newLinkedHashMap();
private final Multimap<String, ModelGroup> parts = MultimapBuilder.linkedHashKeys().arrayListValues().build();

private final List<Vector3f> positions = Lists.newArrayList();
private final List<Vec2> texCoords = Lists.newArrayList();
Expand Down Expand Up @@ -441,7 +442,7 @@ protected void addNamesRecursively(Set<String> names) {
}

public class ModelGroup extends ModelObject {
final Map<String, ModelObject> parts = Maps.newLinkedHashMap();
final Multimap<String, ModelObject> parts = MultimapBuilder.linkedHashKeys().arrayListValues().build();

ModelGroup(String name) {
super(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"variants": {
"facing=east": {
"model": "new_model_loader_test:block/obj_block_same_part_names",
"y": 90
},
"facing=north": {
"model": "new_model_loader_test:block/obj_block_same_part_names"
},
"facing=south": {
"model": "new_model_loader_test:block/obj_block_same_part_names",
"y": 180
},
"facing=west": {
"model": "new_model_loader_test:block/obj_block_same_part_names",
"y": 270
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"flip_v": true,
"loader": "neoforge:obj",
"model": "new_model_loader_test:models/item/sugar_glider_same_part_names.obj",
"textures": {
"particle": "#qr",
"qr": "minecraft:block/oak_planks"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,11 @@ public class NewModelLoaderTest {
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(MODID);
public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(MODID);

public static DeferredBlock<Block> obj_block = BLOCKS.registerBlock("obj_block", props -> new Block(props) {
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(BlockStateProperties.HORIZONTAL_FACING);
}
public static DeferredBlock<Block> obj_block = BLOCKS.registerBlock("obj_block", TestBlock::new, Block.Properties.of().mapColor(MapColor.WOOD).strength(10));

@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
return defaultBlockState().setValue(
BlockStateProperties.HORIZONTAL_FACING, context.getHorizontalDirection());
}

@Override
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return Block.box(2, 2, 2, 14, 14, 14);
}
}, Block.Properties.of().mapColor(MapColor.WOOD).strength(10));
// Same at obj_block except all the parts in the obj model have the same name,
// this is a test for neoforged/NeoForge#1755 that was fixed by neoforged/NeoForge#1759
public static DeferredBlock<Block> obj_block_same_part_names = BLOCKS.registerBlock("obj_block_same_part_names", TestBlock::new, Block.Properties.of().mapColor(MapColor.WOOD).strength(10));

public static DeferredItem<Item> obj_item = ITEMS.registerItem("obj_block", props -> new BlockItem(obj_block.get(), props.useBlockDescriptionPrefix()) {
@Override
Expand All @@ -94,6 +81,8 @@ public boolean canEquip(ItemStack stack, EquipmentSlot armorType, LivingEntity e
}
});

public static DeferredItem<Item> obj_item_same_part_names = ITEMS.registerItem("obj_block_same_part_names", props -> new BlockItem(obj_block_same_part_names.get(), props));

public static DeferredItem<Item> custom_transforms = ITEMS.registerSimpleItem("custom_transforms");

public static DeferredItem<Item> custom_vanilla_loader = ITEMS.registerSimpleItem("custom_vanilla_loader");
Expand All @@ -117,6 +106,7 @@ private void addCreative(BuildCreativeModeTabContentsEvent event) {
if (event.getTabKey() == CreativeModeTabs.INGREDIENTS) {
Arrays.asList(
obj_item,
obj_item_same_part_names,
custom_transforms,
custom_vanilla_loader,
custom_loader,
Expand All @@ -129,6 +119,29 @@ public void modelRegistry(ModelEvent.RegisterLoaders event) {
event.register(ResourceLocation.fromNamespaceAndPath(MODID, "custom_loader"), new TestLoader());
}

static class TestBlock extends Block {
public TestBlock(Properties properties) {
super(properties);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(BlockStateProperties.HORIZONTAL_FACING);
}

@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
return defaultBlockState().setValue(
BlockStateProperties.HORIZONTAL_FACING, context.getHorizontalDirection());
}

@Override
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return Block.box(2, 2, 2, 14, 14, 14);
}
}

static class TestLoader implements UnbakedModelLoader<TestModel> {
@Override
public TestModel read(JsonObject jsonObject, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
Expand Down Expand Up @@ -203,15 +216,20 @@ public BlockStates(PackOutput output, ExistingFileHelper exFileHelper) {

@Override
protected void registerStatesAndModels() {
createModelAndBlockState(obj_block, "sugar_glider");
createModelAndBlockState(obj_block_same_part_names, "sugar_glider_same_part_names");
}

private void createModelAndBlockState(DeferredBlock<Block> block, String objModel) {
BlockModelBuilder model = models()
.getBuilder(NewModelLoaderTest.obj_block.getId().getPath())
.getBuilder(block.getId().getPath())
.customLoader(ObjModelBuilder::begin)
.modelLocation(ResourceLocation.fromNamespaceAndPath("new_model_loader_test", "models/item/sugar_glider.obj"))
.modelLocation(ResourceLocation.fromNamespaceAndPath("new_model_loader_test", "models/item/" + objModel + ".obj"))
.flipV(true)
.end()
.texture("qr", "minecraft:block/oak_planks")
.texture("particle", "#qr");
getVariantBuilder(NewModelLoaderTest.obj_block.get())
getVariantBuilder(block.get())
.partialState()
.with(BlockStateProperties.HORIZONTAL_FACING, Direction.EAST)
.addModels(new ConfiguredModel(model, 0, 90, false))
Expand Down
Loading
Loading