From c3a91872fecd0230dc273c3623c6bd47d65701b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20Amador?= Date: Sat, 9 Jul 2022 16:08:43 +0200 Subject: [PATCH] Make texture atlas in StandaloneGeometryBakingContext configurable (#8831) --- .../StandaloneGeometryBakingContext.java | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/minecraftforge/client/model/geometry/StandaloneGeometryBakingContext.java b/src/main/java/net/minecraftforge/client/model/geometry/StandaloneGeometryBakingContext.java index 623225d19e..e4c7f62d8c 100644 --- a/src/main/java/net/minecraftforge/client/model/geometry/StandaloneGeometryBakingContext.java +++ b/src/main/java/net/minecraftforge/client/model/geometry/StandaloneGeometryBakingContext.java @@ -10,9 +10,9 @@ import com.mojang.math.Transformation; import it.unimi.dsi.fastutil.objects.Object2BooleanMap; import net.minecraft.client.renderer.block.model.ItemTransforms; import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite; +import net.minecraft.client.renderer.texture.TextureAtlas; import net.minecraft.client.resources.model.Material; import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.client.textures.UnitTextureAtlasSprite; import org.jetbrains.annotations.Nullable; import java.util.Map; @@ -45,8 +45,8 @@ public class StandaloneGeometryBakingContext implements IGeometryBakingContext } private final ResourceLocation modelName; - private final Predicate textureCheck; - private final Function textureLookup; + private final Predicate materialCheck; + private final Function materialLookup; private final boolean isGui3d; private final boolean useBlockLight; private final boolean useAmbientOcclusion; @@ -56,16 +56,16 @@ public class StandaloneGeometryBakingContext implements IGeometryBakingContext private final ResourceLocation renderTypeHint; private final BiPredicate visibilityTest; - private StandaloneGeometryBakingContext(ResourceLocation modelName, Predicate textureCheck, - Function textureLookup, boolean isGui3d, + private StandaloneGeometryBakingContext(ResourceLocation modelName, Predicate materialCheck, + Function materialLookup, boolean isGui3d, boolean useBlockLight, boolean useAmbientOcclusion, ItemTransforms transforms, Transformation rootTransform, @Nullable ResourceLocation renderTypeHint, BiPredicate visibilityTest) { this.modelName = modelName; - this.textureCheck = textureCheck; - this.textureLookup = textureLookup; + this.materialCheck = materialCheck; + this.materialLookup = materialLookup; this.isGui3d = isGui3d; this.useBlockLight = useBlockLight; this.useAmbientOcclusion = useAmbientOcclusion; @@ -84,13 +84,13 @@ public class StandaloneGeometryBakingContext implements IGeometryBakingContext @Override public boolean hasMaterial(String name) { - return textureCheck.test(name); + return materialCheck.test(name); } @Override public Material getMaterial(String name) { - return new Material(UnitTextureAtlasSprite.LOCATION, textureLookup.apply(name)); + return materialLookup.apply(name); } @Override @@ -148,8 +148,9 @@ public class StandaloneGeometryBakingContext implements IGeometryBakingContext public static final class Builder { - private Predicate textureCheck = Predicates.alwaysFalse(); - private Function textureLookup = $ -> MissingTextureAtlasSprite.getLocation(); + private static final Material NO_MATERIAL = new Material(TextureAtlas.LOCATION_BLOCKS, MissingTextureAtlasSprite.getLocation()); + private Predicate materialCheck = Predicates.alwaysFalse(); + private Function materialLookup = $ -> NO_MATERIAL; private boolean isGui3d = true; private boolean useBlockLight = true; private boolean useAmbientOcclusion = true; @@ -165,8 +166,8 @@ public class StandaloneGeometryBakingContext implements IGeometryBakingContext private Builder(IGeometryBakingContext parent) { - this.textureCheck = parent::hasMaterial; - this.textureLookup = name -> parent.getMaterial(name).texture(); + this.materialCheck = parent::hasMaterial; + this.materialLookup = parent::getMaterial; this.isGui3d = parent.isGui3d(); this.useBlockLight = parent.useBlockLight(); this.useAmbientOcclusion = parent.useAmbientOcclusion(); @@ -178,8 +179,20 @@ public class StandaloneGeometryBakingContext implements IGeometryBakingContext public Builder withTextures(Map textures, ResourceLocation defaultTexture) { - this.textureCheck = textures::containsKey; - this.textureLookup = name -> textures.getOrDefault(name, defaultTexture); + return withTextures(TextureAtlas.LOCATION_BLOCKS, textures, defaultTexture); + } + + public Builder withTextures(ResourceLocation atlasLocation, Map textures, ResourceLocation defaultTexture) + { + this.materialCheck = textures::containsKey; + this.materialLookup = name -> new Material(atlasLocation, textures.getOrDefault(name, defaultTexture)); + return this; + } + + public Builder withMaterials(Map materials, Material defaultMaterial) + { + this.materialCheck = materials::containsKey; + this.materialLookup = name -> materials.getOrDefault(name, defaultMaterial); return this; } @@ -227,7 +240,7 @@ public class StandaloneGeometryBakingContext implements IGeometryBakingContext public StandaloneGeometryBakingContext build(ResourceLocation modelName) { - return new StandaloneGeometryBakingContext(modelName, textureCheck, textureLookup, isGui3d, useBlockLight, useAmbientOcclusion, transforms, rootTransform, renderTypeHint, visibilityTest); + return new StandaloneGeometryBakingContext(modelName, materialCheck, materialLookup, isGui3d, useBlockLight, useAmbientOcclusion, transforms, rootTransform, renderTypeHint, visibilityTest); } } }