use of org.terasology.engine.world.block.BlockAppearance in project Terasology by MovingBlocks.
the class BlockMeshGeneratorSingleShape method generateChunkMesh.
@Override
public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y, int z) {
final BlockAppearance blockAppearance = block.getPrimaryAppearance();
if (!blockAppearance.hasAppearance()) {
// perf: Skip mesh generation for blocks without appearance, e.g., air blocks.
return;
}
Color colorCache = new Color();
// Gather adjacent blocks
Block[] adjacentBlocks = new Block[Side.allSides().size()];
for (Side side : Side.allSides()) {
Vector3ic offset = side.direction();
Block blockToCheck = view.getBlock(x + offset.x(), y + offset.y(), z + offset.z());
adjacentBlocks[side.ordinal()] = blockToCheck;
}
final ChunkMesh.RenderType renderType = getRenderType(block);
final ChunkVertexFlag vertexFlag = getChunkVertexFlag(view, x, y, z, block);
boolean isRendered = false;
for (final Side side : Side.allSides()) {
if (isSideVisibleForBlockTypes(adjacentBlocks[side.ordinal()], block, side)) {
isRendered = true;
BlockMeshPart blockMeshPart = blockAppearance.getPart(BlockPart.fromSide(side));
// If the selfBlock isn't lowered, some more faces may have to be drawn
if (block.isLiquid()) {
final Block topBlock = adjacentBlocks[Side.TOP.ordinal()];
// Draw horizontal sides if visible from below
if (topBlock.isLiquid() && Side.horizontalSides().contains(side)) {
final Vector3ic offset = side.direction();
final Block adjacentAbove = view.getBlock(x + offset.x(), y + 1, z + offset.z());
final Block adjacent = adjacentBlocks[side.ordinal()];
if (adjacent.isLiquid() && !adjacentAbove.isLiquid()) {
blockMeshPart = block.getTopLiquidMesh(side);
}
} else {
if (blockMeshPart != null) {
blockMeshPart = block.getLowLiquidMesh(side);
}
}
}
if (blockMeshPart != null) {
// TODO: Needs review since the new per-vertex flags introduce a lot of special scenarios - probably a per-side setting?
ChunkVertexFlag sideVertexFlag = vertexFlag;
if (block.isGrass() && side != Side.TOP && side != Side.BOTTOM) {
sideVertexFlag = ChunkVertexFlag.COLOR_MASK;
}
Colorc colorOffset = block.getColorOffset(BlockPart.fromSide(side));
Colorc colorSource = block.getColorSource(BlockPart.fromSide(side)).calcColor(view, x, y, z);
colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
blockMeshPart.appendTo(chunkMesh, view, x, y, z, renderType, colorCache, sideVertexFlag);
}
}
}
if (isRendered && blockAppearance.getPart(BlockPart.CENTER) != null) {
Colorc colorOffset = block.getColorOffset(BlockPart.CENTER);
Colorc colorSource = block.getColorSource(BlockPart.CENTER).calcColor(view, x, y, z);
colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
blockAppearance.getPart(BlockPart.CENTER).appendTo(chunkMesh, view, x, y, z, renderType, colorCache, vertexFlag);
}
}
use of org.terasology.engine.world.block.BlockAppearance in project Terasology by MovingBlocks.
the class BlockBuilder method createAppearance.
private BlockAppearance createAppearance(BlockShape shape, Map<BlockPart, BlockTile> tiles, Rotation rot) {
Map<BlockPart, BlockMeshPart> meshParts = Maps.newEnumMap(BlockPart.class);
Map<BlockPart, Vector2f> textureAtlasPositions = Maps.newEnumMap(BlockPart.class);
for (BlockPart part : BlockPart.values()) {
// TODO: Need to be more sensible with the texture atlas.
// Because things like block particles read from a part that may not exist, we're being fairly lenient
Vector2f atlasPos;
int frameCount;
BlockTile tile = tiles.get(part);
if (tile == null) {
atlasPos = new Vector2f();
frameCount = 1;
} else {
atlasPos = worldAtlas.getTexCoords(tile, shape.getMeshPart(part) != null);
frameCount = tile.getLength();
}
BlockPart targetPart = part.rotate(rot);
textureAtlasPositions.put(targetPart, atlasPos);
if (shape.getMeshPart(part) != null) {
meshParts.put(targetPart, shape.getMeshPart(part).rotate(rot.orientation().get(new Quaternionf())).mapTexCoords(atlasPos, worldAtlas.getRelativeTileSize(), frameCount));
}
}
return new BlockAppearance(meshParts, textureAtlasPositions);
}
Aggregations