Changing model loading (WIP)

This commit is contained in:
Aleksey 2021-05-21 23:45:13 +03:00
parent d9c5f8e89c
commit 4d2e3d7be6
8 changed files with 120 additions and 98 deletions

View file

@ -100,6 +100,6 @@ public class EndAnvilBlock extends AnvilBlock implements BlockModelProvider {
String modelId = "block/" + resourceLocation.getPath() + "_top_" + destruction; String modelId = "block/" + resourceLocation.getPath() + "_top_" + destruction;
ResourceLocation modelLocation = new ResourceLocation(modId, modelId); ResourceLocation modelLocation = new ResourceLocation(modId, modelId);
registerBlockModel(resourceLocation, modelLocation, blockState, modelCache); registerBlockModel(resourceLocation, modelLocation, blockState, modelCache);
return ModelsHelper.createFacingModel(modelLocation, blockState.getValue(FACING).getOpposite()); return ModelsHelper.createFacingModel(modelLocation, blockState.getValue(FACING), false, false);
} }
} }

View file

@ -7,8 +7,10 @@ import java.util.Random;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.BlockModelRotation;
import net.minecraft.client.resources.model.UnbakedModel; import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
@ -125,6 +127,15 @@ public class EndBarrelBlock extends BarrelBlock implements BlockModelProvider {
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(), ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
"block/" + resourceLocation.getPath() + open); "block/" + resourceLocation.getPath() + open);
registerBlockModel(resourceLocation, modelId, blockState, modelCache); registerBlockModel(resourceLocation, modelId, blockState, modelCache);
return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING)); Direction facing = blockState.getValue(FACING);
BlockModelRotation rotation = BlockModelRotation.X0_Y0;
switch (facing) {
case NORTH: rotation = BlockModelRotation.X90_Y0; break;
case EAST: rotation = BlockModelRotation.X90_Y90; break;
case SOUTH: rotation = BlockModelRotation.X90_Y180; break;
case WEST: rotation = BlockModelRotation.X90_Y270; break;
case DOWN: rotation = BlockModelRotation.X180_Y0; break;
}
return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false);
} }
} }

View file

@ -24,6 +24,7 @@ import net.minecraft.world.level.block.state.properties.DoorHingeSide;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf; import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.client.render.ERenderLayer; import ru.betterend.client.render.ERenderLayer;
import ru.betterend.interfaces.IRenderTypeable; import ru.betterend.interfaces.IRenderTypeable;
import ru.betterend.client.models.BlockModelProvider; import ru.betterend.client.models.BlockModelProvider;
@ -133,8 +134,7 @@ public class EndDoorBlock extends DoorBlock implements IRenderTypeable, BlockMod
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(), ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
"block/" + resourceLocation.getPath() + "_" + doorType); "block/" + resourceLocation.getPath() + "_" + doorType);
registerBlockModel(resourceLocation, modelId, blockState, modelCache); registerBlockModel(resourceLocation, modelId, blockState, modelCache);
Variant variant = new Variant(modelId, rotation.getRotation(), false, 1); return ModelsHelper.createMultiVariant(modelId, rotation.getRotation(), false);
return new MultiVariant(Lists.newArrayList(variant));
} }
protected DoorType getDoorType(BlockState blockState) { protected DoorType getDoorType(BlockState blockState) {

View file

@ -1,19 +1,7 @@
package ru.betterend.blocks.basis; package ru.betterend.blocks.basis;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.google.common.collect.Lists;
import com.mojang.math.Transformation;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.MultiVariant;
import net.minecraft.client.renderer.block.model.Variant;
import net.minecraft.client.renderer.block.model.multipart.Condition;
import net.minecraft.client.renderer.block.model.multipart.MultiPart;
import net.minecraft.client.renderer.block.model.multipart.Selector;
import net.minecraft.client.resources.model.BlockModelRotation; import net.minecraft.client.resources.model.BlockModelRotation;
import net.minecraft.client.resources.model.UnbakedModel; import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
@ -22,14 +10,16 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.FenceBlock; import net.minecraft.world.level.block.FenceBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import ru.betterend.BetterEnd;
import ru.betterend.client.models.BlockModelProvider; import ru.betterend.client.models.BlockModelProvider;
import ru.betterend.client.models.ModelsHelper.MultiPartBuilder;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
import static net.minecraft.client.resources.model.ModelBakery.MISSING_MODEL_LOCATION; import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class EndFenceBlock extends FenceBlock implements BlockModelProvider { public class EndFenceBlock extends FenceBlock implements BlockModelProvider {
private final Block parent; private final Block parent;
@ -92,23 +82,16 @@ public class EndFenceBlock extends FenceBlock implements BlockModelProvider {
registerBlockModel(postId, postId, blockState, modelCache); registerBlockModel(postId, postId, blockState, modelCache);
registerBlockModel(sideId, sideId, blockState, modelCache); registerBlockModel(sideId, sideId, blockState, modelCache);
MultiVariant postVariant = new MultiVariant(Lists.newArrayList( MultiPartBuilder builder = MultiPartBuilder.create(stateDefinition);
new Variant(postId, Transformation.identity(), false, 1))); builder.part(sideId).setCondition(state -> state.getValue(NORTH)).setUVLock(true).save();
MultiVariant sideNorth = new MultiVariant(Lists.newArrayList( builder.part(sideId).setCondition(state -> state.getValue(EAST))
new Variant(sideId, Transformation.identity(), true, 1))); .setTransformation(BlockModelRotation.X0_Y90.getRotation()).setUVLock(true).save();
MultiVariant sideEast = new MultiVariant(Lists.newArrayList( builder.part(sideId).setCondition(state -> state.getValue(SOUTH))
new Variant(sideId, BlockModelRotation.X0_Y90.getRotation(), true, 1))); .setTransformation(BlockModelRotation.X0_Y180.getRotation()).setUVLock(true).save();
MultiVariant sideSouth = new MultiVariant(Lists.newArrayList( builder.part(sideId).setCondition(state -> state.getValue(WEST))
new Variant(sideId, BlockModelRotation.X0_Y180.getRotation(), true, 1))); .setTransformation(BlockModelRotation.X0_Y270.getRotation()).setUVLock(true).save();
MultiVariant sideWest = new MultiVariant(Lists.newArrayList( builder.part(postId).save();
new Variant(sideId, BlockModelRotation.X0_Y270.getRotation(), true, 1)));
StateDefinition<Block, BlockState> blockStateDefinition = getStateDefinition(); return builder.build();
return new MultiPart(blockStateDefinition, Lists.newArrayList(
new Selector(Condition.TRUE, postVariant),
new Selector(stateDefinition -> state -> state.getValue(NORTH), sideNorth),
new Selector(stateDefinition -> state -> state.getValue(EAST), sideEast),
new Selector(stateDefinition -> state -> state.getValue(SOUTH), sideSouth),
new Selector(stateDefinition -> state -> state.getValue(WEST), sideWest)
));
} }
} }

View file

@ -98,7 +98,7 @@ public class EndFurnaceBlock extends FurnaceBlock implements BlockModelProvider,
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(), ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
"block/" + resourceLocation.getPath() + lit); "block/" + resourceLocation.getPath() + lit);
registerBlockModel(resourceLocation, modelId, blockState, modelCache); registerBlockModel(resourceLocation, modelId, blockState, modelCache);
return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING)); return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), false, true);
} }
@Override @Override

View file

@ -2,9 +2,12 @@ package ru.betterend.blocks.basis;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
@ -12,7 +15,9 @@ import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.FenceGateBlock; import net.minecraft.world.level.block.FenceGateBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.LootContext;
import org.jetbrains.annotations.Nullable;
import ru.betterend.client.models.BlockModelProvider; import ru.betterend.client.models.BlockModelProvider;
import ru.betterend.client.models.ModelsHelper;
import ru.betterend.client.models.Patterns; import ru.betterend.client.models.Patterns;
public class EndGateBlock extends FenceGateBlock implements BlockModelProvider { public class EndGateBlock extends FenceGateBlock implements BlockModelProvider {
@ -45,4 +50,30 @@ public class EndGateBlock extends FenceGateBlock implements BlockModelProvider {
return Patterns.createJson(Patterns.BLOCK_GATE_CLOSED, parentId.getPath(), blockId.getPath()); return Patterns.createJson(Patterns.BLOCK_GATE_CLOSED, parentId.getPath(), blockId.getPath());
} }
@Override
public @Nullable UnbakedModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
boolean inWall = blockState.getValue(IN_WALL);
boolean isOpen = blockState.getValue(OPEN);
ResourceLocation parentId = Registry.BLOCK.getKey(parent);
Optional<String> pattern;
if (inWall) {
pattern = isOpen ? Patterns.createJson(Patterns.BLOCK_GATE_OPEN_WALL, parentId.getPath(), blockId.getPath()) :
Patterns.createJson(Patterns.BLOCK_GATE_CLOSED_WALL, parentId.getPath(), blockId.getPath());
} else {
pattern = isOpen ? Patterns.createJson(Patterns.BLOCK_GATE_OPEN, parentId.getPath(), blockId.getPath()) :
Patterns.createJson(Patterns.BLOCK_GATE_CLOSED, parentId.getPath(), blockId.getPath());
}
return pattern.map(BlockModel::fromString).orElse(null);
}
@Override
public UnbakedModel getModelVariant(ResourceLocation resourceLocation, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
boolean inWall = blockState.getValue(IN_WALL);
boolean isOpen = blockState.getValue(OPEN);
String state = "" + (inWall ? "_wall" : "") + (isOpen ? "_open" : "_closed");
ResourceLocation modelId = new ResourceLocation(resourceLocation.getNamespace(),
"block/" + resourceLocation.getPath() + state);
registerBlockModel(resourceLocation, modelId, blockState, modelCache);
return ModelsHelper.createFacingModel(modelId, blockState.getValue(FACING), true, false);
}
} }

View file

@ -59,9 +59,7 @@ public class EndSlabBlock extends SlabBlock implements BlockModelProvider {
"block/" + resourceLocation.getPath() + "_" + type); "block/" + resourceLocation.getPath() + "_" + type);
registerBlockModel(resourceLocation, modelId, blockState, modelCache); registerBlockModel(resourceLocation, modelId, blockState, modelCache);
if (type == SlabType.TOP) { if (type == SlabType.TOP) {
BlockModelRotation rotation = BlockModelRotation.by(180, 0); return ModelsHelper.createMultiVariant(modelId, BlockModelRotation.X180_Y0.getRotation(), true);
Variant variant = new Variant(modelId, rotation.getRotation(), true, 1);
return new MultiVariant(Lists.newArrayList(variant));
} }
return ModelsHelper.createBlockSimple(modelId); return ModelsHelper.createBlockSimple(modelId);
} }

View file

@ -19,6 +19,7 @@ import net.minecraft.world.level.block.state.StateDefinition;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class ModelsHelper { public class ModelsHelper {
@ -32,86 +33,65 @@ public class ModelsHelper {
return pattern.map(BlockModel::fromString).orElse(null); return pattern.map(BlockModel::fromString).orElse(null);
} }
public static MultiVariant createBlockSimple(ResourceLocation resourceLocation) { public static MultiVariant createMultiVariant(ResourceLocation resourceLocation, Transformation transform, boolean uvLock) {
Variant variant = new Variant(resourceLocation, Transformation.identity(), false, 1); Variant variant = new Variant(resourceLocation, transform, uvLock, 1);
return new MultiVariant(Lists.newArrayList(variant)); return new MultiVariant(Lists.newArrayList(variant));
} }
public static MultiVariant createFacingModel(ResourceLocation resourceLocation, Direction facing) { public static MultiVariant createBlockSimple(ResourceLocation resourceLocation) {
BlockModelRotation rotation = BlockModelRotation.by(0, (int) facing.getOpposite().toYRot()); return createMultiVariant(resourceLocation, Transformation.identity(), false);
Variant variant = new Variant(resourceLocation, rotation.getRotation(), false, 1); }
return new MultiVariant(Lists.newArrayList(variant));
public static MultiVariant createFacingModel(ResourceLocation resourceLocation, Direction facing, boolean uvLock, boolean inverted) {
if (inverted) {
facing = facing.getOpposite();
}
BlockModelRotation rotation = BlockModelRotation.by(0, (int) facing.toYRot());
return createMultiVariant(resourceLocation, rotation.getRotation(), uvLock);
} }
public static MultiVariant createRotatedModel(ResourceLocation resourceLocation, Direction.Axis axis) { public static MultiVariant createRotatedModel(ResourceLocation resourceLocation, Direction.Axis axis) {
BlockModelRotation rotation = BlockModelRotation.X0_Y0; BlockModelRotation rotation = BlockModelRotation.X0_Y0;
switch (axis) { switch (axis) {
case X: { case X: rotation = BlockModelRotation.X90_Y90; break;
rotation = BlockModelRotation.by(90, 90); case Z: rotation = BlockModelRotation.X90_Y0; break;
break;
}
case Z: {
rotation = BlockModelRotation.by(90, 0);
break;
}
} }
Variant variant = new Variant(resourceLocation, rotation.getRotation(), false, 1); return createMultiVariant(resourceLocation, rotation.getRotation(), false);
return new MultiVariant(Lists.newArrayList(variant));
} }
public static class MultiPartBuilder { public static class MultiPartBuilder {
private final List<ModelPart> modelParts = Lists.newArrayList(); private final static MultiPartBuilder BUILDER = new MultiPartBuilder();
public static MultiPartBuilder create(StateDefinition<Block, BlockState> stateDefinition) {
BUILDER.stateDefinition = stateDefinition;
BUILDER.modelParts.clear();
return BUILDER;
}
private final List<ModelPart> modelParts = Lists.newArrayList();
private StateDefinition<Block, BlockState> stateDefinition; private StateDefinition<Block, BlockState> stateDefinition;
private ModelPart activePart;
private MultiPartBuilder() {} private MultiPartBuilder() {}
public MultiPartBuilder create(StateDefinition<Block, BlockState> stateDefinition) { public ModelPart part(ResourceLocation modelId) {
this.stateDefinition = stateDefinition; return new ModelPart(modelId);
return this;
}
public MultiPartBuilder createPart(ResourceLocation modelId) {
activePart = new ModelPart(modelId);
return this;
}
public MultiPartBuilder setCondition(Condition condition) {
activePart.condition = condition;
return this;
}
public MultiPartBuilder setTransformation(Transformation transform) {
activePart.transform = transform;
return this;
}
public MultiPartBuilder setUVLock(boolean value) {
activePart.uvLock = value;
return this;
}
public MultiPartBuilder savePart() {
if (activePart != null) {
modelParts.add(activePart);
}
return this;
} }
public MultiPart build() { public MultiPart build() {
List<Selector> selectors = Lists.newArrayList(); if (modelParts.size() > 0) {
modelParts.forEach(modelPart -> { List<Selector> selectors = Lists.newArrayList();
MultiVariant variant = new MultiVariant(Lists.newArrayList( modelParts.forEach(modelPart -> {
new Variant(modelPart.modelId, modelPart.transform, modelPart.uvLock, 1))); MultiVariant variant = createMultiVariant(modelPart.modelId, modelPart.transform, modelPart.uvLock);
selectors.add(new Selector(modelPart.condition, variant)); selectors.add(new Selector(modelPart.condition, variant));
}); });
modelParts.clear(); modelParts.clear();
return new MultiPart(stateDefinition, selectors); return new MultiPart(stateDefinition, selectors);
}
throw new IllegalStateException("At least one model part need to be created.");
} }
private static class ModelPart { public class ModelPart {
private final ResourceLocation modelId; private final ResourceLocation modelId;
private Transformation transform = Transformation.identity(); private Transformation transform = Transformation.identity();
private Condition condition = Condition.TRUE; private Condition condition = Condition.TRUE;
@ -120,6 +100,25 @@ public class ModelsHelper {
private ModelPart(ResourceLocation modelId) { private ModelPart(ResourceLocation modelId) {
this.modelId = modelId; this.modelId = modelId;
} }
public ModelPart setCondition(Function<BlockState, Boolean> condition) {
this.condition = stateDefinition -> condition::apply;
return this;
}
public ModelPart setTransformation(Transformation transform) {
this.transform = transform;
return this;
}
public ModelPart setUVLock(boolean value) {
this.uvLock = value;
return this;
}
public void save() {
modelParts.add(this);
}
} }
} }
} }