Add lazyloading util class and cache blockcolor-calculation to improve render-speed

This commit is contained in:
Blue (Lukas Rieger) 2020-01-04 21:13:21 +01:00
parent 636b9cdc89
commit d20e0843ab
2 changed files with 62 additions and 1 deletions

View File

@ -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<Vector3f> 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);

View File

@ -0,0 +1,56 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* 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<T> {
private Supplier<T> loader;
private T value;
public Lazy(Supplier<T> 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;
}
}