diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/render/hires/blockmodel/ResourceModelBuilder.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/render/hires/blockmodel/ResourceModelBuilder.java index 80b97ffa..03868bb7 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/render/hires/blockmodel/ResourceModelBuilder.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/render/hires/blockmodel/ResourceModelBuilder.java @@ -44,6 +44,7 @@ import de.bluecolored.bluemap.core.resourcepack.Texture; import de.bluecolored.bluemap.core.resourcepack.TransformedBlockModelResource; import de.bluecolored.bluemap.core.util.Direction; +import de.bluecolored.bluemap.core.util.Lazy; import de.bluecolored.bluemap.core.world.Block; /** @@ -58,16 +59,20 @@ public class ResourceModelBuilder { private ExtendedBlockContext context; private RenderSettings renderSettings; private BlockColorCalculator colorCalculator; + private Lazy tintColor; public ResourceModelBuilder(RenderSettings renderSettings, ExtendedBlockContext context, BlockColorCalculator colorCalculator) { this.context = context; this.renderSettings = renderSettings; this.colorCalculator = colorCalculator; + this.tintColor = new Lazy<>(() -> colorCalculator.getBlockColor(context)); } public BlockStateModel build(TransformedBlockModelResource bmr) { BlockStateModel model = new BlockStateModel(); + colorCalculator.getBlockColor(context); + for (BlockModelResource.Element element : bmr.getModel().getElements()){ model.merge(fromModelElementResource(element, bmr)); } @@ -213,7 +218,7 @@ private void createElementFace(BlockStateModel model, TransformedBlockModelResou //tint the face Vector3f color = Vector3f.ONE; if (face.isTinted()){ - color = colorCalculator.getBlockColor(context); //TODO: cache this so we don't recalculate the tint color again for each face? + color = tintColor.getValue(); } color = color.mul(light); diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/util/Lazy.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/util/Lazy.java new file mode 100644 index 00000000..e13427c0 --- /dev/null +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/util/Lazy.java @@ -0,0 +1,56 @@ +/* + * This file is part of BlueMap, licensed under the MIT License (MIT). + * + * Copyright (c) Blue (Lukas Rieger) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package de.bluecolored.bluemap.core.util; + +import java.util.function.Supplier; + +public class Lazy { + + private Supplier loader; + private T value; + + public Lazy(Supplier loader) { + this.loader = loader; + this.value = null; + } + + public Lazy(T value) { + this.loader = null; + this.value = value; + } + + public T getValue() { + if (!isLoaded()) { + this.value = loader.get(); + } + + return this.value; + } + + public boolean isLoaded() { + return this.value != null; + } + +}