BlueMap/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/render/hires/HiresModelRenderer.java

116 lines
4.8 KiB
Java

/*
* 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.render.hires;
import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;
import com.flowpowered.math.vector.Vector4f;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.render.RenderSettings;
import de.bluecolored.bluemap.core.render.WorldTile;
import de.bluecolored.bluemap.core.render.context.SlicedWorldChunkBlockContext;
import de.bluecolored.bluemap.core.render.hires.blockmodel.BlockStateModel;
import de.bluecolored.bluemap.core.render.hires.blockmodel.BlockStateModelFactory;
import de.bluecolored.bluemap.core.resourcepack.InvalidResourceDeclarationException;
import de.bluecolored.bluemap.core.resourcepack.NoSuchResourceException;
import de.bluecolored.bluemap.core.resourcepack.NoSuchTextureException;
import de.bluecolored.bluemap.core.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.util.AABB;
import de.bluecolored.bluemap.core.util.MathUtil;
import de.bluecolored.bluemap.core.world.Block;
import de.bluecolored.bluemap.core.world.ChunkNotGeneratedException;
import de.bluecolored.bluemap.core.world.WorldChunk;
public class HiresModelRenderer {
private BlockStateModelFactory modelFactory;
public HiresModelRenderer(ResourcePack resourcePack) {
this(new BlockStateModelFactory(resourcePack));
}
public HiresModelRenderer(BlockStateModelFactory modelFactory) {
this.modelFactory = modelFactory;
}
public HiresModel render(WorldTile tile, AABB region, RenderSettings renderSettings) throws ChunkNotGeneratedException {
Vector3i min = region.getMin().toInt();
Vector3i max = region.getMax().toInt();
min = new Vector3i(min.getX(), Math.max(min.getY(), renderSettings.getMinY()), min.getZ());
max = new Vector3i(max.getX(), Math.min(max.getY(), Math.min(renderSettings.getMaxY(), renderSettings.getSliceY())), max.getZ());
WorldChunk chunk = tile.getWorld().getWorldChunk(region.expand(4, 0, 4));
if (!chunk.isGenerated()) throw new ChunkNotGeneratedException();
HiresModel model = new HiresModel(tile.getWorld().getUUID(), tile.getTile(), min, max);
for (int x = min.getX(); x <= max.getX(); x++){
for (int z = min.getZ(); z <= max.getZ(); z++){
int maxHeight = 0;
Vector4f color = Vector4f.ZERO;
for (int y = min.getY(); y <= max.getY(); y++){
Block block = chunk.getBlock(x, y, z);
if (block.getBlock().getId().equals("air")) continue;
maxHeight = y;
BlockStateModel blockModel;
try {
blockModel = modelFactory.createFrom(block.getBlock(), new SlicedWorldChunkBlockContext(chunk, new Vector3i(x, y, z), renderSettings.getSliceY()), renderSettings);
} catch (NoSuchResourceException | InvalidResourceDeclarationException | NoSuchTextureException e) {
blockModel = new BlockStateModel();
Logger.global.noFloodDebug(block.getBlock().getId() + "-hiresModelRenderer-blockmodelerr", "Failed to create BlockModel for BlockState: " + block.getBlock() + " (" + e.toString() + ")");
}
blockModel.translate(new Vector3f(x, y, z).sub(min.toFloat()));
color = MathUtil.overlayColors(blockModel.getMapColor(), color);
//TODO: quick hack to random offset grass
if (block.getBlock().getId().equals("grass")){
float dx = (MathUtil.hashToFloat(x, y, z, 123984) - 0.5f) * 0.75f;
float dz = (MathUtil.hashToFloat(x, y, z, 345542) - 0.5f) * 0.75f;
blockModel.translate(new Vector3f(dx, 0, dz));
}
model.merge(blockModel);
}
model.setHeight(x, z, maxHeight);
model.setColor(x, z, color);
}
}
return model;
}
}