diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java index 48b0fa46f..964087a93 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java @@ -8,7 +8,10 @@ import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProvider; import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProviderHolder; import com.gtnewhorizons.angelica.rendering.celeritas.iris.BlockRenderContext; import com.gtnewhorizons.angelica.rendering.celeritas.iris.ContextAwareChunkVertexEncoder; +import com.prupe.mcpatcher.mal.block.RenderBlocksUtils; import it.unimi.dsi.fastutil.objects.Reference2ReferenceMap; +import net.coderbot.iris.block_rendering.BlockMaterialMapping; +import net.coderbot.iris.block_rendering.BlockRenderingSettings; import net.coderbot.iris.vertices.ExtendedDataHelper; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; @@ -261,7 +264,13 @@ public abstract class AngelicaChunkBuilderMeshingTask extends ChunkBuilderTask createBlockMetaIdMap(Int2ObjectMap> blockPropertiesMap) { - Reference2ObjectMap blockMatches = new Reference2ObjectOpenHashMap<>(); + public static Reference2ObjectMap createBlockMetaIdMap(Int2ObjectMap> blockPropertiesMap, boolean skipFlattening) { + final Reference2ObjectMap blockMatches = new Reference2ObjectOpenHashMap<>(); + final ReferenceSet snowyBlocks = new ReferenceOpenHashSet<>(); blockPropertiesMap.forEach((intId, entries) -> { for (BlockEntry entry : entries) { - addBlockMetas(entry, blockMatches, intId); + addBlockMetas(entry, blockMatches, intId, skipFlattening, snowyBlocks); } }); + BlockRenderingSettings.INSTANCE.setHasSnowyEntries(!snowyBlocks.isEmpty()); + BlockRenderingSettings.INSTANCE.setSnowyBlocks(snowyBlocks); return blockMatches; } public static Map createBlockTypeMap(Map blockPropertiesMap) { - Map blockTypeIds = new Reference2ReferenceOpenHashMap<>(); + final Map blockTypeIds = new Reference2ReferenceOpenHashMap<>(); blockPropertiesMap.forEach((id, blockType) -> { - Block block = resolveBlock(id); + Block block = resolveBlockOrNull(id); - // Try flattening map for modern names - if ((block == null || block == Blocks.air) && "minecraft".equals(id.getNamespace())) { - List legacyEntries = FlatteningMap.toLegacy(id.getName(), Map.of()); + // Modern names like "grass_block" don't exist in 1.7.10's registry; fall back to flattening. + if (block == null && "minecraft".equals(id.getNamespace())) { + final List legacyEntries = FlatteningMap.toLegacy(id.getName(), Map.of()); if (legacyEntries != null) { - // Use the first entry for render type (render type applies per-block) - block = resolveBlock(legacyEntries.getFirst().getId()); + block = resolveBlockOrNull(legacyEntries.getFirst().getId()); } } - if (block == null || block == Blocks.air) { - return; - } - + if (block == null) return; final BlockRenderLayer layer = convertBlockToRenderLayer(blockType); - if (layer != null) { - blockTypeIds.put(block, layer); - } + if (layer != null) blockTypeIds.put(block, layer); }); return blockTypeIds; @@ -82,33 +84,29 @@ public class BlockMaterialMapping { * Adds block+metadata combinations to the material ID map. * Based on Iris's addBlockStates method, adapted for 1.7.10 metadata system. */ - private static void addBlockMetas(BlockEntry entry, Reference2ObjectMap idMap, int intId) { - NamespacedId id = entry.getId(); - String name = id.getName(); - boolean hasStateProps = !entry.getStateProperties().isEmpty(); - boolean hasExplicitMetas = !entry.getMetas().isEmpty(); + private static void addBlockMetas(BlockEntry entry, Reference2ObjectMap idMap, int intId, boolean skipFlattening, ReferenceSet snowyBlocks) { + final NamespacedId id = entry.getId(); + final Map stateProps = entry.getStateProperties(); + final String snowy = stateProps.get("snowy"); + final int snowyBit = "true".equals(snowy) ? SNOWY_META_BIT : 0; - if ("minecraft".equals(id.getNamespace()) && (hasStateProps || !hasExplicitMetas)) { - List legacyEntries = FlatteningMap.toLegacy(name, entry.getStateProperties()); - if (legacyEntries != null) { - for (BlockEntry legacy : legacyEntries) { - applyMetas(resolveBlock(legacy.getId()), legacy.getMetas(), idMap, intId); - } - return; - } + // Vanilla modern names go through FlatteningMap; legacy-section packs and modded blocks + // resolve directly from the registry. + List targets = null; + if (!skipFlattening && "minecraft".equals(id.getNamespace()) && (!stateProps.isEmpty() || entry.getMetas().isEmpty())) { + targets = FlatteningMap.toLegacy(id.getName(), stateProps); } + if (targets == null) targets = List.of(entry); - // Fall back to registry with the entry's own metas - Block block = resolveBlock(id); - applyMetas(block, entry.getMetas(), idMap, intId); + for (BlockEntry target : targets) { + final Block block = resolveBlockOrNull(target.getId()); + if (block == null) continue; + applyMetas(block, target.getMetas(), idMap, intId, snowyBit); + if (snowy != null) snowyBlocks.add(block); + } } - // If the block doesn't exist, by default the registry will return AIR. That probably isn't what we want. - // TODO: Assuming that Registry.BLOCK.getDefaultId() == "minecraft:air" here - private static void applyMetas(Block block, Set metas, Reference2ObjectMap idMap, int intId) { - if (block == null || block == Blocks.air) { - return; - } + private static void applyMetas(Block block, Set metas, Reference2ObjectMap idMap, int intId, int snowyBit) { Int2IntMap metaMap = idMap.get(block); if (metaMap == null) { metaMap = new Int2IntOpenHashMap(); @@ -117,18 +115,18 @@ public class BlockMaterialMapping { } if (metas.isEmpty()) { - for (int meta = 0; meta < 16; meta++) { - metaMap.putIfAbsent(meta, intId); - } + for (int meta = 0; meta < 16; meta++) metaMap.putIfAbsent(meta | snowyBit, intId); } else { - for (int meta : metas) { - metaMap.putIfAbsent(meta, intId); - } + for (int meta : metas) metaMap.putIfAbsent(meta | snowyBit, intId); } } - private static Block resolveBlock(NamespacedId id) { - final ResourceLocation resourceLocation = new ResourceLocation(id.getNamespace(), id.getName()); - return (Block) Block.blockRegistry.getObject(resourceLocation.toString()); + /** + * Returns the registered Block for an id, or null if unknown. + * The registry returns Blocks.air (its default) for missing keys, which we coerce to null. + */ + private static Block resolveBlockOrNull(NamespacedId id) { + final Block block = (Block) Block.blockRegistry.getObject(new ResourceLocation(id.getNamespace(), id.getName()).toString()); + return block == Blocks.air ? null : block; } } diff --git a/src/main/java/net/coderbot/iris/block_rendering/BlockRenderingSettings.java b/src/main/java/net/coderbot/iris/block_rendering/BlockRenderingSettings.java index 292b2a59f..2ab786f53 100644 --- a/src/main/java/net/coderbot/iris/block_rendering/BlockRenderingSettings.java +++ b/src/main/java/net/coderbot/iris/block_rendering/BlockRenderingSettings.java @@ -4,7 +4,10 @@ import com.gtnewhorizons.angelica.rendering.celeritas.BlockRenderLayer; import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntFunction; import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; +import it.unimi.dsi.fastutil.objects.ReferenceSet; +import it.unimi.dsi.fastutil.objects.ReferenceSets; import lombok.Getter; +import lombok.Setter; import net.coderbot.iris.shaderpack.materialmap.NamespacedId; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; @@ -19,12 +22,21 @@ public class BlockRenderingSettings { private boolean reloadRequired; private Reference2ObjectMap blockMetaMatches; private Map blockTypeIds; - private Object2IntFunction entityIds; - private Object2IntFunction itemIds; - private float ambientOcclusionLevel; + // note: no reload needed, entities are rebuilt every frame. + @Setter + private Object2IntFunction entityIds; + // note: no reload needed, items are rendered every frame. + @Setter + private Object2IntFunction itemIds; + @Getter + private float ambientOcclusionLevel; private boolean disableDirectionalShading; private boolean useSeparateAo; private boolean useExtendedVertexFormat; + @Setter + private boolean hasSnowyEntries; + @Getter + private ReferenceSet snowyBlocks = ReferenceSets.emptySet(); public BlockRenderingSettings() { reloadRequired = false; @@ -34,6 +46,7 @@ public class BlockRenderingSettings { disableDirectionalShading = false; useSeparateAo = false; useExtendedVertexFormat = false; + hasSnowyEntries = false; } public void clearReloadRequired() { @@ -74,6 +87,14 @@ public class BlockRenderingSettings { this.blockMetaMatches = blockMetaIds; } + public boolean hasSnowyEntries() { + return hasSnowyEntries; + } + + public void setSnowyBlocks(ReferenceSet snowyBlocks) { + this.snowyBlocks = snowyBlocks != null ? snowyBlocks : ReferenceSets.emptySet(); + } + public void setBlockTypeIds(Map blockTypeIds) { if (this.blockTypeIds != null && this.blockTypeIds.equals(blockTypeIds)) { return; @@ -83,21 +104,7 @@ public class BlockRenderingSettings { this.blockTypeIds = blockTypeIds; } - public void setEntityIds(Object2IntFunction entityIds) { - // note: no reload needed, entities are rebuilt every frame. - this.entityIds = entityIds; - } - - public void setItemIds(Object2IntFunction itemIds) { - // note: no reload needed, items are rendered every frame. - this.itemIds = itemIds; - } - - public float getAmbientOcclusionLevel() { - return ambientOcclusionLevel; - } - - public void setAmbientOcclusionLevel(float ambientOcclusionLevel) { + public void setAmbientOcclusionLevel(float ambientOcclusionLevel) { if (ambientOcclusionLevel == this.ambientOcclusionLevel) { return; } diff --git a/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java b/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java index bd44d358d..4eb9b0933 100644 --- a/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java +++ b/src/main/java/net/coderbot/iris/pipeline/DeferredWorldRenderingPipeline.java @@ -279,7 +279,9 @@ public class DeferredWorldRenderingPipeline implements WorldRenderingPipeline, R holder -> CommonUniforms.addNonDynamicUniforms(holder, programs.getPack().getIdMap(), programs.getPackDirectives(), this.updateNotifier) ); - BlockRenderingSettings.INSTANCE.setBlockMetaMatches(BlockMaterialMapping.createBlockMetaIdMap(programs.getPack().getIdMap().getBlockProperties())); + BlockRenderingSettings.INSTANCE.setBlockMetaMatches(BlockMaterialMapping.createBlockMetaIdMap( + programs.getPack().getIdMap().getBlockProperties(), + programs.getPack().getIdMap().hasLegacySection())); BlockRenderingSettings.INSTANCE.setBlockTypeIds(BlockMaterialMapping.createBlockTypeMap(programs.getPack().getIdMap().getBlockRenderTypeMap())); BlockRenderingSettings.INSTANCE.setEntityIds(programs.getPack().getIdMap().getEntityIdMap()); diff --git a/src/main/java/net/coderbot/iris/shaderpack/IdMap.java b/src/main/java/net/coderbot/iris/shaderpack/IdMap.java index 686acf211..edb2f12c1 100644 --- a/src/main/java/net/coderbot/iris/shaderpack/IdMap.java +++ b/src/main/java/net/coderbot/iris/shaderpack/IdMap.java @@ -35,6 +35,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Properties; +import java.util.regex.Pattern; /** * A utility class for parsing entries in item.properties, block.properties, and entities.properties files in shaderpacks @@ -61,14 +62,24 @@ public class IdMap { @Getter private Map blockRenderTypeMap; + /** True when block.properties contained a preprocessor directive guarding a 1.7.10-specific section. */ + private final boolean hasLegacySection; + + public boolean hasLegacySection() { + return hasLegacySection; + } + + private static final Pattern LEGACY_DIRECTIVE_PATTERN = Pattern.compile( + "(?m)^\\s*#\\s*(?:if|elif|ifdef|ifndef)\\b[^\\n]*\\bMC_VERSION\\b[^\\n]*\\b10710\\b"); + IdMap(Path shaderPath, ShaderPackOptions shaderPackOptions, Iterable environmentDefines) { // Check if block.properties has a dedicated 1.7.10 section String rawBlockProperties = readProperties(shaderPath, "block.properties"); - boolean hasLegacySection = rawBlockProperties != null - && rawBlockProperties.contains("MC_VERSION") && rawBlockProperties.contains("10710"); + this.hasLegacySection = rawBlockProperties != null + && LEGACY_DIRECTIVE_PATTERN.matcher(rawBlockProperties).find(); Iterable resolvedDefines; - if (hasLegacySection) { + if (this.hasLegacySection) { // Pack has a 1.7.10 section resolvedDefines = environmentDefines; loadProperties(shaderPath, "block.properties", shaderPackOptions, environmentDefines).ifPresent(blockProperties -> {